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

Conversation

zsofiaschell
Copy link
Collaborator

Fix #509
TraverseCXXForRangeStmt and VisitCXXForRangeStmt added to handle foreach style loops correctly.

@mcserep mcserep added Kind: Bug ⚠️ Plugin: C++ Issues related to the parsing and presentation of C++ projects. labels Feb 10, 2023
@mcserep mcserep added this to the Release Gershwin milestone Feb 10, 2023
Copy link
Collaborator

@mcserep mcserep left a 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);
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.

Comment on lines 167 to 184
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);
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

@mcserep mcserep added the Status: At risk 🚨 Issue or PR which got stuck and progress in work is at risk. label Feb 4, 2024
@mcserep mcserep modified the milestones: Release Gershwin, Release H* Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Kind: Bug ⚠️ Plugin: C++ Issues related to the parsing and presentation of C++ projects. Status: At risk 🚨 Issue or PR which got stuck and progress in work is at risk.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrectly handled foreach style loops
3 participants