Skip to content

Commit

Permalink
Fixed the parent statement retrieval issue.
Browse files Browse the repository at this point in the history
A new _statements stack now stores all parent statements of the currently visited statement.
  • Loading branch information
dbukki committed Nov 1, 2023
1 parent 5bd85b0 commit be5f66d
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
return b;
}

bool TraverseStmt(clang::Stmt* s_)
{
_statements.push(s_);
bool b = Base::TraverseStmt(s_);
_statements.pop();
return b;
}

bool VisitTypedefTypeLoc(clang::TypedefTypeLoc tl_)
{
const clang::TypedefType* type = tl_.getTypePtr();
Expand Down Expand Up @@ -940,26 +948,21 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
return true;
}

bool VisitStmt(clang::Stmt* s_)
bool VisitDeclStmt(clang::DeclStmt* ds_)
{
if (clang::DeclStmt* ds = llvm::dyn_cast<clang::DeclStmt>(s_))
assert(_statements.top() == ds_ &&
"ds_ was expected to be the top-level statement.");
_statements.pop();
clang::Stmt* scopeSt = _statements.top();
_statements.push(ds_);

for (auto it = ds_->decl_begin(); it != ds_->decl_end(); ++it)
{
// FIXME: Using the parent map is expensive.
// My original idea was to simply use the second top-most statement from
// _contextStatementStack as scopeSt, but the stack is always empty
// for some reason, except in VisitDeclRefExpr... (why?)
auto parents = _astContext.getParentMapContext()
.getParents<clang::Stmt>(*s_);
const clang::Stmt* scopeSt = parents[0].get<clang::Stmt>();

for (auto it = ds->decl_begin(); it != ds->decl_end(); ++it)
if (clang::VarDecl* vd = llvm::dyn_cast<clang::VarDecl>(*it))
{
if (clang::VarDecl* vd = llvm::dyn_cast<clang::VarDecl>(*it))
{
model::FileLoc loc =
getFileLoc(scopeSt->getEndLoc(), scopeSt->getEndLoc());
addDestructorUsage(vd->getType(), loc, vd);
}
model::FileLoc loc =
getFileLoc(scopeSt->getEndLoc(), scopeSt->getEndLoc());
addDestructorUsage(vd->getType(), loc, vd);
}
}

Expand Down Expand Up @@ -1576,6 +1579,8 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
std::unordered_map<unsigned, model::CppAstNode::AstType> _locToAstType;
std::unordered_map<unsigned, std::string> _locToAstValue;

// This stack contains the parent chain of the current Stmt.
std::stack<clang::Stmt*> _statements;
// This stack has the same role as _locTo* maps. In case of
// clang::DeclRefExpr objects we need to determine the contect of the given
// expression. In this stack we store the deepest statement node in AST which
Expand Down

0 comments on commit be5f66d

Please sign in to comment.