Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CompleteDefinition of struct being dragged into output because of union fields #113

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions libcextract/Closure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ bool DeclClosureVisitor::TraverseDecl(Decl *decl)
{
DO_NOT_RUN_IF_ALREADY_ANALYZED(decl);
Mark_As_Analyzed(decl);
return RecursiveASTVisitor::TraverseDecl(decl);
Stack.push_back(decl);
bool ret = RecursiveASTVisitor::TraverseDecl(decl);
Stack.pop_back();
return ret;
}

bool DeclClosureVisitor::VisitFunctionDecl(FunctionDecl *decl)
Expand Down Expand Up @@ -506,7 +509,15 @@ bool DeclClosureVisitor::ParentRecordDeclHelper(TagDecl *decl)
/* In case this references a struct/union defined inside a struct (nested
struct), then we also need to analyze the parent struct. */
RecordDecl *parent = dyn_cast<RecordDecl>(decl->getLexicalDeclContext());
if (parent) {
Decl *analysis_parent = nullptr;

/* Check if we are not coming from an analysis from the RecordDecl, i.e. from
a stack perspective the Decl that is at the bottom of the Decl. */
if (Stack.size() >= 2) {
analysis_parent = Stack[Stack.size() - 2];
}

if (parent && analysis_parent != parent) {
/* If the parent struct is flagged as not needing a complete definition
then we need to set it to true, else the nested struct won't be
output as of only a partial definition of the parent struct is
Expand Down
15 changes: 14 additions & 1 deletion libcextract/Closure.hh
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>
public:
DeclClosureVisitor(ASTUnit *ast)
: RecursiveASTVisitor(),
AST(ast)
AST(ast),
Closure(),
AnalyzedDecls(),
Stack()
{
}

Expand Down Expand Up @@ -273,4 +276,14 @@ class DeclClosureVisitor : public RecursiveASTVisitor<DeclClosureVisitor>

/** The set of all analyzed Decls. */
std::unordered_set<Decl *> AnalyzedDecls;

/** Stack of Decls. Implement using a vector because we may need to access
the second element on the top, and we also need its continuity. */
llvm::SmallVector<Decl *, 128> Stack;

/** Return what is on top of our stack. */
inline Decl *Stack_Top(void)
{
return Stack[Stack.size() - 1];
}
};
20 changes: 20 additions & 0 deletions testsuite/small/record-nested-5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_NO_EXTERNALIZATION" }*/

struct ll;

int get(struct ll *);

struct ll {
union {
unsigned long key;
void *key_ptr;
};
struct ll *next;
};

int f(struct ll *l)
{
return get(l);
}

/* { dg-final { scan-tree-dump-not "struct ll *{" } } */
Loading