<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
Hi everyone, 
<div class=""><br class="">
</div>
<div class="">I'm trying to use AST Matchers in clang-query to find single-depth for-loops,</div>
<div class="">i.e. for-loops that are neither contained in any other for-loop nor contain for-loops themselves.</div>
<div class=""><br class="">
</div>
<div class="">Approach:</div>
<div class=""><span style="font-family: "Open Sans", sans-serif; font-variant-ligatures: normal; orphans: 2; white-space: pre-wrap; widows: 2;" class="">forStmt( unless(hasAncestor(forStmt())), unless(hasDescendant(forStmt())) )</span></div>
<div class=""><span style="font-family: "Open Sans", sans-serif; font-variant-ligatures: normal; orphans: 2; white-space: pre-wrap; widows: 2;" class=""><br class="">
</span></div>
<div style="orphans: 2; widows: 2;" class=""><font face="Open Sans, sans-serif" class=""><span style="white-space: pre-wrap;" class="">In the following code, I want it to match only the loop in foo1, but unfortunately it also matches the loop in foo2.</span></font></div>
<div style="orphans: 2; widows: 2;" class=""><font face="Open Sans, sans-serif" class=""><span style="white-space: pre-wrap;" class=""><br class="">
</span></font></div>
<div style="orphans: 2; widows: 2;" class=""><font face="Open Sans, sans-serif" class=""><span style="white-space: pre-wrap;" class="">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(); } } }</span></font></div>
<div style="orphans: 2; widows: 2;" class=""><font face="Open Sans, sans-serif" class=""><span style="white-space: pre-wrap;" class=""><br class="">
</span></font></div>
<div style="orphans: 2; widows: 2;" class="">
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">Match #1:</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">~/cq.cpp:4:5: </span>
<span style="font-variant-ligatures: no-common-ligatures;" class="">note: </span>
<span style="font-variant-ligatures: no-common-ligatures;" class="">"root" binds here</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">    for(int i1=0; i1<n; i1++){</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">    ^~~~~~~~~~~~~~~~~~~~~~~~~~</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono"; min-height: 21px;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">Match #2:</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">~/cq.cpp:11:5:
</span><span style="font-variant-ligatures: no-common-ligatures;" class="">note: </span>
<span style="font-variant-ligatures: no-common-ligatures;" class="">"root" binds here</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">    for(int i21=0; i21<n; i21++){</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span></div>
<div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">2 matches.</span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class="">
</span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">When I interchange "hasAncestor" and "hasDescendant" in the matcher, the second match changes:</span></div>
</div>
<div style="orphans: 2; widows: 2;" class=""><font face="Open Sans, sans-serif" class=""><span style="white-space: pre-wrap;" class=""><br class="">
</span></font></div>
<div class="">
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">Match #1:</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">~/cq.cpp:4:5: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">note: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">"root" binds here</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">    for(int i1=0; i1<n; i1++){</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">    ^~~~~~~~~~~~~~~~~~~~~~~~~~</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono"; min-height: 21px;" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class="">
</div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">Match #2:</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">~/cq.cpp:11:5: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">note: </span><span style="font-variant-ligatures: no-common-ligatures;" class="">"root" binds here</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">    for(int i22=0; i21<n; i22++){</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">2 matches.</span></div>
</div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal; font-family: "Ubuntu Mono";" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class=""><br class="">
</span></div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal;" class="">
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. </div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal;" class="">
<br class="">
</div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal;" class="">
Best regards,</div>
<div style="orphans: 2; widows: 2; margin: 0px; font-stretch: normal; line-height: normal;" class="">
Siegfried Hartogs</div>
</body>
</html>