clang-tutor icon indicating copy to clipboard operation
clang-tutor copied to clipboard

Questions about rationale of ASTMatcher and RecursiveASTVisitor?

Open for-just-we opened this issue 3 years ago • 1 comments

Hi, author. Thanks for you excellent tutorial, I have some questions about your code in UnusedForLoopVar?

  • why ASTMatcher would work? you use API LoopVar->isUsed(true) in line 64 of UnusedForLoopVar.cpp to check whether LoopVar is used? how the attribute used of LoopVar is set to true. Does clang do this when it parse source code into AST?

  • you use API RecursiveASTVisitor::TraverseStmt(S->getBody()); instead of this->TraverseStmt(S->getBody()); in line 134 of UnusedForLoopVar.cpp, what's the difference between them.

for-just-we avatar Feb 06 '22 14:02 for-just-we

Hello 👋🏻 !

Thanks for you excellent tutorial

Thank you for your kind words! :)

why ASTMatcher would work? you use API LoopVar->isUsed(true) in line 64 of UnusedForLoopVar.cpp to check whether LoopVar is used?

The approach with the AST Matcher works for i below (it is not used). It does not work for j (it is used):

for (int j = 0, i = 0; j < 20; j++) { }

Does it make sense?

how the attribute used of LoopVar is set to true. Does clang do this when it parse source code into AST?

Sorry, I don't know, I'm not that familiar with the implementation of Clang.

you use API RecursiveASTVisitor::TraverseStmt(S->getBody()); instead of this->TraverseStmt(S->getBody()); in line 134 of UnusedForLoopVar.cpp, what's the difference between them.

Both approaches should be fine - they will result in a call to TraverseStmt. Good catch :)

banach-space avatar Feb 09 '22 16:02 banach-space