clang-tutor
clang-tutor copied to clipboard
Questions about rationale of ASTMatcher and RecursiveASTVisitor?
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 whetherLoopVar
is used? how the attributeused
ofLoopVar
is set totrue
. Does clang do this when it parse source code into AST? -
you use API
RecursiveASTVisitor::TraverseStmt(S->getBody());
instead ofthis->TraverseStmt(S->getBody());
in line 134 of UnusedForLoopVar.cpp, what's the difference between them.
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 :)