Skip to content

Commit

Permalink
Map translation units to global namespaces
Browse files Browse the repository at this point in the history
#fix
  • Loading branch information
alandefreitas committed Dec 18, 2024
1 parent 75b1bc5 commit 266f49c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
35 changes: 27 additions & 8 deletions src/lib/AST/ASTVisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ build()
// declarations which satisfy all filter conditions.
// dependencies will be tracked, but not extracted
traverseAny(context_.getTranslationUnitDecl());

// Ensure the global namespace is always present
upsert<NamespaceInfo>(SymbolID::global);
MRDOCS_ASSERT(find(SymbolID::global));

// Dependency extraction is disabled: we are done
if(config_->referencedDeclarations ==
Expand Down Expand Up @@ -449,11 +447,7 @@ void
ASTVisitor::
traverse(UsingDirectiveDecl* D)
{
// A using directive such as `using namespace std;`
if (!shouldExtract(D, getAccess(D)))
{
return;
}
MRDOCS_CHECK_OR(shouldExtract(D));

Decl* PD = getParentDecl(D);
bool const isNamespaceScope = cast<DeclContext>(PD)->isFileContext();
Expand Down Expand Up @@ -713,6 +707,21 @@ populate(
populateNamespaces(I, D);
}

void
ASTVisitor::
populate(
NamespaceInfo& I,
bool const isNew,
TranslationUnitDecl*)
{
// Only extract Namespace data once
MRDOCS_CHECK_OR(isNew);
I.id = SymbolID::global;
I.IsAnonymous = false;
I.Name.clear();
I.IsInline = false;
}

void
ASTVisitor::
populate(
Expand Down Expand Up @@ -2624,6 +2633,12 @@ inExtractedFile(
}

FileInfo const* file = findFileInfo(D->getBeginLoc());
if (!file && isa<TranslationUnitDecl>(D))
{
// TranslationUnitDecl is a special case
// that doesn't have a valid SourceLocation
return true;
}
// KRYSTIAN NOTE: I'm unsure under what conditions
// this assert would fire.
MRDOCS_ASSERT(file);
Expand Down Expand Up @@ -2755,6 +2770,10 @@ ASTVisitor::FileInfo*
ASTVisitor::
findFileInfo(clang::SourceLocation const loc)
{
if (loc.isInvalid())
{
return nullptr;
}
// KRYSTIAN FIXME: we really should not be
// calling getPresumedLoc this often,
// it's quite expensive
Expand Down
21 changes: 20 additions & 1 deletion src/lib/AST/ASTVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ class ASTVisitor
void
populate(NamespaceInfo& I, bool isNew, NamespaceDecl* D);

static
void
populate(NamespaceInfo& I, bool isNew, TranslationUnitDecl* D);

void
populate(RecordInfo& I, bool isNew, CXXRecordDecl* D);

Expand Down Expand Up @@ -753,7 +757,22 @@ class ASTVisitor
and false otherwise.
*/
bool
shouldExtract(const Decl* D, AccessSpecifier access);
shouldExtract(Decl const* D, AccessSpecifier access);

static
bool
shouldExtract(TranslationUnitDecl const*, AccessSpecifier)
{
return true;
}

template <std::derived_from<Decl> DeclTy>
bool
shouldExtract(DeclTy const* D)
{
return shouldExtract(D, getAccess(D));
}


// Determine if a declaration passes the symbol filter
bool
Expand Down
6 changes: 5 additions & 1 deletion src/lib/AST/ClangHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ SubstituteConstraintExpressionWithoutSatisfaction(
template <class>
struct InfoTypeFor {};

// Extract NamespaceInfo from NamespaceDecl
// Extract NamespaceInfo from NamespaceDecl or TranslationUnitDecl
template <>
struct InfoTypeFor<NamespaceDecl>
: std::type_identity<NamespaceInfo> {};

template <>
struct InfoTypeFor<TranslationUnitDecl>
: std::type_identity<NamespaceInfo> {};

// Extract RecordInfo from anything derived from CXXRecordDecl
template <std::derived_from<CXXRecordDecl> DeclType>
struct InfoTypeFor<DeclType>
Expand Down

0 comments on commit 266f49c

Please sign in to comment.