[cfe-dev] ASTMatcher unexpected result
Hartogs Siegfried via cfe-dev
cfe-dev at lists.llvm.org
Tue Oct 6 03:25:39 PDT 2020
Hi everyone,
I'm trying to use AST Matchers in clang-query to find single-depth for-loops,
i.e. for-loops that are neither contained in any other for-loop nor contain for-loops themselves.
Approach:
forStmt( unless(hasAncestor(forStmt())), unless(hasDescendant(forStmt())) )
In the following code, I want it to match only the loop in foo1, but unfortunately it also matches the loop in foo2.
void foo1(){ int n = 10; for(int i1=0; i1<n; i1++){ // dosomething(); } } void foo2(){ int n = 10; for(int i21=0; i21<n; i21++){ for(int i22=0; i22<n; i22++){ // dosomething(); } } }
Match #1:
~/cq.cpp:4:5: note: "root" binds here
for(int i1=0; i1<n; i1++){
^~~~~~~~~~~~~~~~~~~~~~~~~~
Match #2:
~/cq.cpp:11:5: note: "root" binds here
for(int i21=0; i21<n; i21++){
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 matches.
When I interchange "hasAncestor" and "hasDescendant" in the matcher, the second match changes:
Match #1:
~/cq.cpp:4:5: note: "root" binds here
for(int i1=0; i1<n; i1++){
^~~~~~~~~~~~~~~~~~~~~~~~~~
Match #2:
~/cq.cpp:11:5: note: "root" binds here
for(int i22=0; i21<n; i22++){
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 matches.
To me, it doesn't make sense why the result changes (implicit allOf() matcher's argument order shouldn't matter), but I don't understand why the matcher returns a loop from foo2 in the first place.
Best regards,
Siegfried Hartogs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20201006/ba9f0f29/attachment-0001.html>
More information about the cfe-dev
mailing list