-
Notifications
You must be signed in to change notification settings - Fork 102
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
base: master
Are you sure you want to change the base?
Conversation
Sync forked repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regular variables seems to be handled correctly now, but iterating over function and method call results not.
I have used the following sample code for testing:
https://github.com/mcserep/cc-tests/blob/master/foreach/main.cpp
|
||
clang::Stmt* stmt = llvm::dyn_cast<clang::Stmt>(forRangeStmt->getRangeInit()); | ||
|
||
clang::CallExpr* callExpr = llvm::dyn_cast<clang::CallExpr>(stmt); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
astNode->astValue = getSourceText( | ||
_clangSrcMgr, | ||
forRangeStmt->getRangeStmt()->getBeginLoc(), | ||
forRangeStmt->getRangeStmt()->getEndLoc(), | ||
true); | ||
|
||
astNode->location = getFileLoc(forRangeStmt->getBeginLoc(), forRangeStmt->getEndLoc()); | ||
|
||
const clang::VarDecl* vd = llvm::dyn_cast<clang::VarDecl>(forRangeStmt->getRangeStmt()->getSingleDecl()); | ||
astNode->entityHash = util::fnvHash(getUSR(vd)); | ||
|
||
astNode->symbolType = model::CppAstNode::SymbolType::Variable; | ||
|
||
astNode->astType = model::CppAstNode::AstType::Read; | ||
|
||
astNode->id = model::createIdentifier(*astNode); | ||
|
||
_astNodes.push_back(astNode); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you also call VisitDeclRefExpr
, this seems to be redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed.
Fix #509
TraverseCXXForRangeStmt and VisitCXXForRangeStmt added to handle foreach style loops correctly.