llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

cxxConstructExpr stopped matching templates

Open anforowicz opened this issue 6 years ago • 11 comments

Bugzilla Link 46287
Version trunk
OS All
CC @zygoloid,@steveire,@ymand

Extended Description

CLANG VERSION:

$ third_party/llvm-build/Release+Asserts/bin/clang++ --version
clang version 11.0.0 (https://github.com/llvm/llvm-project/ f7f1abdb8893af4a606ca1a8f5347a426e9c7f9e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/lukasza/src/chromium4/src/third_party/llvm-build/Release+Asserts/bin

REPRO INPUT FILE:

template <typename T>
struct MyTemplate {
  MyTemplate(int i) {}
};

void foo() {
  MyTemplate<char> mt(123);  // <- corresponds to the CXXConstructExpr node below
}

REPRO INPUT FILE RENDERED AS AST:

$ third_party/llvm-build/Release+Asserts/bin/clang++ -Xclang -ast-dump -std=c++14 tools/clang/rewrite_raw_ptr_fields/tests/affected-expr-original.cc
...
`-FunctionDecl 0x5b38300 <line:6:1, line:8:1> line:6:6 foo 'void ()'
  `-CompoundStmt 0x5b65318 <col:12, line:8:1>
    `-DeclStmt 0x5b65300 <line:7:3, col:27>
      `-VarDecl 0x5b385b8 <col:3, col:26> col:20 mt 'MyTemplate<char>':'MyTemplate<char>' callinit
        `-CXXConstructExpr 0x5b652d0 <col:20, col:26> 'MyTemplate<char>':'MyTemplate<char>' 'void (int)'
          `-IntegerLiteral 0x5b38620 <col:23> 'int' 123

REPRO MATCHER:

cxxConstructExpr().bind("ctor")

EXPECTED BEHAVIOR: The cxxConstructExpr matcher matches all CXXConstructExpr nodes from the AST

ACTUAL BEHAVIOR: The CXXConstructExpr above is not matched.

anforowicz avatar Jun 11 '20 16:06 anforowicz

Yes, this is a bug. Consider:

struct B {
  explicit B(int);
};
struct C {
  explicit C(int, int);
};

C foo() {
  B b = B(5); // <- No Match
  C x = C(6, 7); // <- Match
  return x;
}

These are both fully explicit, yet only the second matches.

ymand avatar Jun 18 '20 12:06 ymand

RE: #c5: this is indeed an intentional change.

Thank you for the link to the design doc.

I don't understand why CxxConstructExpr is hidden in my example:

  1. The AST node is shown by clang - see "repro input file rendered as ast" in the initial bug report/comment. When I see FooBarExpr in clang's AST dump, then I expect that fooBarExpr() matcher will match such FooBarExpr node - not seeing a match was rather surprising to me.

  2. Looking at the source code I would expect to see CxxConstructExpr (i.e. it doesn't seem like an "implicit" AST node to me).

Is it a bug that CxxConstructExpr is hidden/invisible in the example used here? If not, then what is the rule I should use going forward, when trying to understand which nodes are hidden and which aren't?

anforowicz avatar Jun 17 '20 22:06 anforowicz

Yes, this is indeed an intentional change.

See https://docs.google.com/document/d/17Z6gAwwc3HoRXvsy0OdwU0X5MFQEuiGeSu3i6ICOB90/edit#

clang-query will match on and show such nodes in AsIs traversal mode:

https://godbolt.org/z/tIS622

This is also in the release notes you link to.

steveire avatar Jun 17 '20 20:06 steveire

Maybe this is expected, given the release notes here https://clang.llvm.org/docs/ReleaseNotes.html#ast-matchers

anforowicz avatar Jun 16 '20 00:06 anforowicz

Interestingly, the following matcher returns no matches (*):

cxxConstructExpr().bind("expr")

But this matcher does return a match:

traverse(clang::ast_type_traits::TK_AsIs, cxxConstructExpr().bind("expr"))

The latter matcher is the matcher used in TEST(Matcher, ConstructorCall) in //clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp.

(*) "returns no matches" when used in ASTMatchersNodeTest.cpp unit tests like so:

 EXPECT_TRUE(
   matches("template <typename T> struct S { S(int i) {} };"
           "void f() { S<char> s(123); }",
           cxxConstructExpr()));  

I think that the observation above:

  1. Gives me a workaround (yay!)

  2. Explains why I've previously been unable to repro with a new unit test

  3. Might explain why #c2 reported no repro via clang-query. Unfortunately I wasn't able to confirm, because I couldn't figure out quickly how to spell "traverse" and "TK_AsIs" in clang-query. I might give it another try tomorrow.

I am not sure if the observed behavior is expected or not. AFAIK:

A) I think that the AST must have changed recently (because the old matcher, without |traverse| worked for me ~1 month ago). OTOH, I am not sure if such AST change would be considered a regression or not.

B) It seems that the dumped AST does differ between clang-query (#c2) and clang++ (initial bug report). Again - I am not sure if this is expected/okay or not.

C) It is not clear to my why |traverse| is needed now. In particular, I don't understand what additional AST nodes need to be traversed.

anforowicz avatar Jun 12 '20 04:06 anforowicz

I can reproduce this with clang-query:

$ cat /tmp/tmp.cpp template struct MyTemplate { MyTemplate(int i) {} };

void foo() {
  MyTemplate<char> mt(123);  // <- corresponds to the CXXConstructExpr node below
}

$ ./build/bin/clang-query /tmp/tmp.cpp -- clang-query> m cxxConstructExpr() 0 matches. clang-query> set output dump clang-query> m varDecl(hasName("mt"))

Match #​1:

Binding for "root": VarDecl 0x4a3dfb8 </tmp/tmp.cpp:7:7, col:30> col:24 mt 'MyTemplate':'MyTemplate' callinit `-IntegerLiteral 0x4a3e020 col:27 'int' 123

1 match.

That is the wrong AST representation for this initialization. If I use 'clang -cc1 -ast-dump' instead of 'clang-query', I get something more reasonable:

  `-VarDecl 0xf1d6ad8 <col:7, col:30> col:24 mt 'MyTemplate<char>':'MyTemplate<char>' callinit
    `-CXXConstructExpr 0xf206950 <col:24, col:30> 'MyTemplate<char>':'MyTemplate<char>' 'void (int)'
      `-IntegerLiteral 0xf1d6b40 <col:27> 'int' 123

... so somehow when clang-query parses the source file, we build a bogus AST.

Hmmm... unit tests for this scenario are missing from TEST(Matcher, ConstructorCall) in //clang/unittests/ASTMatchers/ASTMatchersNodeTest.cpp, but they seem to pass once I add them:

EXPECT_TRUE(
  matches("template <typename T> struct S { S(int i) {} };"
          "void x() { S<char> s(123); }",
          Constructor));

anforowicz avatar Jun 12 '20 00:06 anforowicz

This works on trunk: https://godbolt.org/z/EhnYab91b and matches in AsIs and IgnoreUnlessSpelledInSource mode. It is not possible to select the clang-query version to check in what version it starts working.

5chmidti avatar Oct 04 '23 22:10 5chmidti

@llvm/issue-subscribers-clang-query

| | | | --- | --- | | Bugzilla Link | [46287](https://llvm.org/bz46287) | | Version | trunk | | OS | All | | CC | @zygoloid,@steveire,@ymand |

Extended Description

CLANG VERSION:

$ third_party/llvm-build/Release+Asserts/bin/clang++ --version
clang version 11.0.0 (https://github.com/llvm/llvm-project/ f7f1abdb8893af4a606ca1a8f5347a426e9c7f9e)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/google/home/lukasza/src/chromium4/src/third_party/llvm-build/Release+Asserts/bin

REPRO INPUT FILE:

template &lt;typename T&gt;
struct MyTemplate {
  MyTemplate(int i) {}
};

void foo() {
  MyTemplate&lt;char&gt; mt(123);  // &lt;- corresponds to the CXXConstructExpr node below
}

REPRO INPUT FILE RENDERED AS AST:

$ third_party/llvm-build/Release+Asserts/bin/clang++ -Xclang -ast-dump -std=c++14 tools/clang/rewrite_raw_ptr_fields/tests/affected-expr-original.cc
...
`-FunctionDecl 0x5b38300 &lt;line:6:1, line:8:1&gt; line:6:6 foo 'void ()'
  `-CompoundStmt 0x5b65318 &lt;col:12, line:8:1&gt;
    `-DeclStmt 0x5b65300 &lt;line:7:3, col:27&gt;
      `-VarDecl 0x5b385b8 &lt;col:3, col:26&gt; col:20 mt 'MyTemplate&lt;char&gt;':'MyTemplate&lt;char&gt;' callinit
        `-CXXConstructExpr 0x5b652d0 &lt;col:20, col:26&gt; 'MyTemplate&lt;char&gt;':'MyTemplate&lt;char&gt;' 'void (int)'
          `-IntegerLiteral 0x5b38620 &lt;col:23&gt; 'int' 123

REPRO MATCHER:

cxxConstructExpr().bind("ctor")

EXPECTED BEHAVIOR: The cxxConstructExpr matcher matches all CXXConstructExpr nodes from the AST

ACTUAL BEHAVIOR: The CXXConstructExpr above is not matched.

llvmbot avatar Oct 05 '23 04:10 llvmbot

@5chmidti so it sounds like this can be closed?

shafik avatar Oct 05 '23 04:10 shafik

I also think this has been fixed, so closing the issue; if you're still seeing an issue, please reopen with more details on how to reproduce.

AaronBallman avatar Oct 05 '23 12:10 AaronBallman