Skip to content

Commit 266f49c

Browse files
committed
Map translation units to global namespaces
#fix
1 parent 75b1bc5 commit 266f49c

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/lib/AST/ASTVisitor.cpp

+27-8
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,7 @@ build()
212212
// declarations which satisfy all filter conditions.
213213
// dependencies will be tracked, but not extracted
214214
traverseAny(context_.getTranslationUnitDecl());
215-
216-
// Ensure the global namespace is always present
217-
upsert<NamespaceInfo>(SymbolID::global);
215+
MRDOCS_ASSERT(find(SymbolID::global));
218216

219217
// Dependency extraction is disabled: we are done
220218
if(config_->referencedDeclarations ==
@@ -449,11 +447,7 @@ void
449447
ASTVisitor::
450448
traverse(UsingDirectiveDecl* D)
451449
{
452-
// A using directive such as `using namespace std;`
453-
if (!shouldExtract(D, getAccess(D)))
454-
{
455-
return;
456-
}
450+
MRDOCS_CHECK_OR(shouldExtract(D));
457451

458452
Decl* PD = getParentDecl(D);
459453
bool const isNamespaceScope = cast<DeclContext>(PD)->isFileContext();
@@ -713,6 +707,21 @@ populate(
713707
populateNamespaces(I, D);
714708
}
715709

710+
void
711+
ASTVisitor::
712+
populate(
713+
NamespaceInfo& I,
714+
bool const isNew,
715+
TranslationUnitDecl*)
716+
{
717+
// Only extract Namespace data once
718+
MRDOCS_CHECK_OR(isNew);
719+
I.id = SymbolID::global;
720+
I.IsAnonymous = false;
721+
I.Name.clear();
722+
I.IsInline = false;
723+
}
724+
716725
void
717726
ASTVisitor::
718727
populate(
@@ -2624,6 +2633,12 @@ inExtractedFile(
26242633
}
26252634

26262635
FileInfo const* file = findFileInfo(D->getBeginLoc());
2636+
if (!file && isa<TranslationUnitDecl>(D))
2637+
{
2638+
// TranslationUnitDecl is a special case
2639+
// that doesn't have a valid SourceLocation
2640+
return true;
2641+
}
26272642
// KRYSTIAN NOTE: I'm unsure under what conditions
26282643
// this assert would fire.
26292644
MRDOCS_ASSERT(file);
@@ -2755,6 +2770,10 @@ ASTVisitor::FileInfo*
27552770
ASTVisitor::
27562771
findFileInfo(clang::SourceLocation const loc)
27572772
{
2773+
if (loc.isInvalid())
2774+
{
2775+
return nullptr;
2776+
}
27582777
// KRYSTIAN FIXME: we really should not be
27592778
// calling getPresumedLoc this often,
27602779
// it's quite expensive

src/lib/AST/ASTVisitor.hpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,10 @@ class ASTVisitor
481481
void
482482
populate(NamespaceInfo& I, bool isNew, NamespaceDecl* D);
483483

484+
static
485+
void
486+
populate(NamespaceInfo& I, bool isNew, TranslationUnitDecl* D);
487+
484488
void
485489
populate(RecordInfo& I, bool isNew, CXXRecordDecl* D);
486490

@@ -753,7 +757,22 @@ class ASTVisitor
753757
and false otherwise.
754758
*/
755759
bool
756-
shouldExtract(const Decl* D, AccessSpecifier access);
760+
shouldExtract(Decl const* D, AccessSpecifier access);
761+
762+
static
763+
bool
764+
shouldExtract(TranslationUnitDecl const*, AccessSpecifier)
765+
{
766+
return true;
767+
}
768+
769+
template <std::derived_from<Decl> DeclTy>
770+
bool
771+
shouldExtract(DeclTy const* D)
772+
{
773+
return shouldExtract(D, getAccess(D));
774+
}
775+
757776

758777
// Determine if a declaration passes the symbol filter
759778
bool

src/lib/AST/ClangHelpers.hpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ SubstituteConstraintExpressionWithoutSatisfaction(
6767
template <class>
6868
struct InfoTypeFor {};
6969

70-
// Extract NamespaceInfo from NamespaceDecl
70+
// Extract NamespaceInfo from NamespaceDecl or TranslationUnitDecl
7171
template <>
7272
struct InfoTypeFor<NamespaceDecl>
7373
: std::type_identity<NamespaceInfo> {};
7474

75+
template <>
76+
struct InfoTypeFor<TranslationUnitDecl>
77+
: std::type_identity<NamespaceInfo> {};
78+
7579
// Extract RecordInfo from anything derived from CXXRecordDecl
7680
template <std::derived_from<CXXRecordDecl> DeclType>
7781
struct InfoTypeFor<DeclType>

0 commit comments

Comments
 (0)