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

Incorrectly handled foreach style loops #591

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions plugins/cpp/parser/src/clangastvisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@ class ClangASTVisitor : public clang::RecursiveASTVisitor<ClangASTVisitor>
return b;
}


bool TraverseCXXForRangeStmt (clang::CXXForRangeStmt *forRangeStmt){
return Base::TraverseCXXForRangeStmt(forRangeStmt);
}

bool VisitCXXForRangeStmt(clang::CXXForRangeStmt *forRangeStmt)
{
model::CppAstNodePtr astNode = std::make_shared<model::CppAstNode>();

clang::DeclRefExpr* declRefExpr = llvm::dyn_cast<clang::DeclRefExpr>(forRangeStmt->getRangeInit());

clang::Stmt* stmt = llvm::dyn_cast<clang::Stmt>(forRangeStmt->getRangeInit());

clang::CallExpr* callExpr = llvm::dyn_cast<clang::CallExpr>(stmt);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we have a function or method call in the range.based for loop, the forRangeStmt->getRangeInit() will be a ExprWithCleanups statement, and one of its descendant in the AST will be the CallExpr or CXXMemberCallExpr.

Eg. for a function call:
image

And for a method call:
image

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@whisperity: is there an easy way to request the first descendant of a node which matches a certain type? E.g. to get the first CallExpr descendant of the ExprWithCleanups node in the AST?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I think no. Every node is implemented with different fields and accessor methods. The whole RecursiveASTVisitor is using lots of generated code in the background to facilitate the traversals. We could try using the AST Matchers library (that Clang-Tidy also uses...) that has a generic has() matcher which can bind a child node.


if(declRefExpr){ //lvalue Var
VisitDeclRefExpr(declRefExpr);
}

if(callExpr){ //lvalue Function
VisitCallExpr(callExpr);
}
return true;
}

bool TraverseFunctionDecl(clang::FunctionDecl* fd_)
{
_functionStack.push(std::make_shared<model::CppFunction>());
Expand Down