banach-space / clang-tutor

A collection of out-of-tree Clang plugins for teaching and learning
The Unlicense
659 stars 62 forks source link

Questions about rationale of ASTMatcher and RecursiveASTVisitor? #25

Open for-just-we opened 2 years ago

for-just-we commented 2 years ago

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

banach-space commented 2 years ago

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 :)