<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - AST MatchFinder Does not Do Pre-order Traversal"
   href="https://bugs.llvm.org/show_bug.cgi?id=39190">39190</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>AST MatchFinder Does not Do Pre-order Traversal
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>7.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>libclang
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lunastorm@lunastorm.tw
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>klimek@google.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The documentation of AST MatchFinder says:
"The order of matches is guaranteed to be equivalent to doing a pre-order
traversal on the AST"

<a href="https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html#details">https://clang.llvm.org/doxygen/classclang_1_1ast__matchers_1_1MatchFinder.html#details</a>

After upgrade to Clang7, we noticed that the match order is changed.
For this example:

void foo()
{
    for (;;) {
        break;
    }
    for (;;) {
        break;
    }
    return;
}

Clang6's clang-query result of "m stmt()" is:
Match #1:

/Users/lunastorm/temp/foo.c:2:1: note: "root" binds here
{
^

Match #2:

/Users/lunastorm/temp/foo.c:3:5: note: "root" binds here
    for (;;) {
    ^~~~~~~~~~

Match #3:

/Users/lunastorm/temp/foo.c:3:14: note: "root" binds here
    for (;;) {
             ^

Match #4:

/Users/lunastorm/temp/foo.c:4:9: note: "root" binds here
        break;
        ^~~~~

Match #5:

/Users/lunastorm/temp/foo.c:6:5: note: "root" binds here
    for (;;) {
    ^~~~~~~~~~

Match #6:

/Users/lunastorm/temp/foo.c:6:14: note: "root" binds here
    for (;;) {
             ^

Match #7:

/Users/lunastorm/temp/foo.c:7:9: note: "root" binds here
        break;
        ^~~~~

Match #8:

/Users/lunastorm/temp/foo.c:9:5: note: "root" binds here
    return;
    ^~~~~~
8 matches.

====
While Clang7's clang-query result of "m stmt()" gives:
Match #1:

/Users/lunastorm/temp/foo.c:2:1: note: "root" binds here
{
^

Match #2:

/Users/lunastorm/temp/foo.c:3:5: note: "root" binds here
    for (;;) {
    ^~~~~~~~~~

Match #3:

/Users/lunastorm/temp/foo.c:6:5: note: "root" binds here
    for (;;) {
    ^~~~~~~~~~

Match #4:

/Users/lunastorm/temp/foo.c:9:5: note: "root" binds here
    return;
    ^~~~~~

Match #5:

/Users/lunastorm/temp/foo.c:3:14: note: "root" binds here
    for (;;) {
             ^

Match #6:

/Users/lunastorm/temp/foo.c:4:9: note: "root" binds here
        break;
        ^~~~~

Match #7:

/Users/lunastorm/temp/foo.c:6:14: note: "root" binds here
    for (;;) {
             ^

Match #8:

/Users/lunastorm/temp/foo.c:7:9: note: "root" binds here
        break;
        ^~~~~
8 matches.

====
We have a tool based on MatchFinder and relies on the pre-order traversal
guarantee.
It seems that it is related to r326624 which adds data recursion support to
TraverseStmt. Without this change there will be a stack overflow in
<a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED FIXED - Nesting of empty case-labels in AST"
   href="show_bug.cgi?id=36581">https://bugs.llvm.org/show_bug.cgi?id=36581</a>

Can the MatchFinder remain the same traverse order guarantee by default, and
take a MatchFinderOption to supply the Queue parameter of TraverseStmt
optionally?
Or the RAV can be modified to traverse in pre-order without hitting stack
limit?

Thanks!</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>