Skip to content

Commit

Permalink
Make sure we add parent decls until the TranslationUnit
Browse files Browse the repository at this point in the history
Due to the recursive nature of C++, we must make sure we added all
Decls up to the root (TranslationUnitDecl) of the AST to the output,
else we might miss important decls.

Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Sep 26, 2024
1 parent 9c0254f commit 19a5ea3
Show file tree
Hide file tree
Showing 4 changed files with 36,303 additions and 7 deletions.
23 changes: 16 additions & 7 deletions libcextract/Closure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,6 @@ bool DeclClosureVisitor::VisitDeclaratorDecl(DeclaratorDecl *decl)
comming from comma-separated. */
TRY_TO(AnalyzeDeclsWithSameBeginlocHelper(decl));

/* Look for the LinkageSpecDecl in order to catch constructions such as:
* extern "C" memcpy(void *, void *, unsigned long n);
*/
if (LinkageSpecDecl *link = dyn_cast<LinkageSpecDecl>(decl->getLexicalDeclContext())) {
Closure.Add_Single_Decl(link);
}

return VISITOR_CONTINUE;
}

Expand Down Expand Up @@ -338,6 +331,22 @@ bool DeclClosureVisitor::VisitVarDecl(VarDecl *decl)
return VISITOR_CONTINUE;
}

bool DeclClosureVisitor::VisitNamedDecl(NamedDecl *decl)
{
/* Make sure we recusively add the parent decls in order to output this
decl we marked as visited. For example where we need this see
c++-linkage-4.cpp, where the definition of align_val_t is inside a
LinkageDecl inside a NamespaceDecl. */
DeclContext *ctx = decl->getLexicalDeclContext();
while (ctx) {
Decl *d = cast<Decl>(ctx);
Closure.Add_Single_Decl(d);
ctx = ctx->getParent();
}

return VISITOR_CONTINUE;
}

bool DeclClosureVisitor::VisitFunctionTemplateDecl(FunctionTemplateDecl *decl)
{
Closure.Add_Decl_And_Prevs(decl);
Expand Down
2 changes: 2 additions & 0 deletions libcextract/Closure.hh
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>

bool VisitVarDecl(VarDecl *decl);

bool VisitNamedDecl(NamedDecl *decl);

/* --------- C++ Declarations ---------- */

bool VisitFunctionTemplateDecl(FunctionTemplateDecl *decl);
Expand Down
24 changes: 24 additions & 0 deletions testsuite/small/c++-linkage-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=main -DCE_NO_EXTERNALIZATION" }*/
/* { dg-xfail }*/

typedef unsigned long size_t;
typedef int unused_t;

extern "C++" {
namespace std
{
enum class align_val_t: size_t {};
unsigned int unused;

};
};

std::align_val_t f(void)
{
std::align_val_t r;
return r;
}

/* { dg-final { scan-tree-dump "enum class align_val_t: size_t" } } */
/* { dg-final { scan-tree-dump-not "unused_t" } } */
/* { dg-final { scan-tree-dump-not "unused" } } */
Loading

0 comments on commit 19a5ea3

Please sign in to comment.