1//== unittests/ASTMatchers/ASTMatchersNodeTest.cpp - AST matcher unit tests ==//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ASTMatchersTest.h"
11#include "clang/AST/PrettyPrinter.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/ASTMatchers/ASTMatchers.h"
14#include "clang/Tooling/Tooling.h"
15#include "llvm/ADT/Triple.h"
16#include "llvm/Support/Host.h"
17#include "gtest/gtest.h"
18
19namespace clang {
20namespace ast_matchers {
21
22TEST(Finder, DynamicOnlyAcceptsSomeMatchers) {
23  MatchFinder Finder;
24  EXPECT_TRUE(Finder.addDynamicMatcher(decl(), nullptr));
25  EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), nullptr));
26  EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)),
27                                       nullptr));
28
29  // Do not accept non-toplevel matchers.
30  EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), nullptr));
31  EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr));
32}
33
34TEST(Decl, MatchesDeclarations) {
35  EXPECT_TRUE(notMatches("", decl(usingDecl())));
36  EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;",
37                      decl(usingDecl())));
38}
39
40TEST(NameableDeclaration, MatchesVariousDecls) {
41  DeclarationMatcher NamedX = namedDecl(hasName("X"));
42  EXPECT_TRUE(matches("typedef int X;", NamedX));
43  EXPECT_TRUE(matches("int X;", NamedX));
44  EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX));
45  EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX));
46  EXPECT_TRUE(matches("void foo() { int X; }", NamedX));
47  EXPECT_TRUE(matches("namespace X { }", NamedX));
48  EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
49
50  EXPECT_TRUE(notMatches("#define X 1", NamedX));
51}
52
53TEST(NameableDeclaration, REMatchesVariousDecls) {
54  DeclarationMatcher NamedX = namedDecl(matchesName("::X"));
55  EXPECT_TRUE(matches("typedef int Xa;", NamedX));
56  EXPECT_TRUE(matches("int Xb;", NamedX));
57  EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX));
58  EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX));
59  EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX));
60  EXPECT_TRUE(matches("namespace Xij { }", NamedX));
61  EXPECT_TRUE(matches("enum X { A, B, C };", NamedX));
62
63  EXPECT_TRUE(notMatches("#define Xkl 1", NamedX));
64
65  DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no"));
66  EXPECT_TRUE(matches("int no_foo;", StartsWithNo));
67  EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo));
68
69  DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c"));
70  EXPECT_TRUE(matches("int abc;", Abc));
71  EXPECT_TRUE(matches("int aFOObBARc;", Abc));
72  EXPECT_TRUE(notMatches("int cab;", Abc));
73  EXPECT_TRUE(matches("int cabc;", Abc));
74
75  DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$"));
76  EXPECT_TRUE(matches("int k;", StartsWithK));
77  EXPECT_TRUE(matches("int kAbc;", StartsWithK));
78  EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK));
79  EXPECT_TRUE(matches("class C { int k; };", StartsWithK));
80  EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK));
81}
82
83TEST(DeclarationMatcher, MatchClass) {
84  DeclarationMatcher ClassMatcher(recordDecl());
85
86  // This passes on Windows only because we explicitly pass -target
87  // i386-unknown-unknown.  If we were to compile with the default target
88  // triple, we'd want to EXPECT_TRUE if it's Win32 or MSVC.
89  EXPECT_FALSE(matches("", ClassMatcher));
90
91  DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X")));
92  EXPECT_TRUE(matches("class X;", ClassX));
93  EXPECT_TRUE(matches("class X {};", ClassX));
94  EXPECT_TRUE(matches("template<class T> class X {};", ClassX));
95  EXPECT_TRUE(notMatches("", ClassX));
96}
97
98TEST(DeclarationMatcher, translationUnitDecl) {
99  const std::string Code = "int MyVar1;\n"
100    "namespace NameSpace {\n"
101    "int MyVar2;\n"
102    "}  // namespace NameSpace\n";
103  EXPECT_TRUE(matches(
104    Code, varDecl(hasName("MyVar1"), hasDeclContext(translationUnitDecl()))));
105  EXPECT_FALSE(matches(
106    Code, varDecl(hasName("MyVar2"), hasDeclContext(translationUnitDecl()))));
107  EXPECT_TRUE(matches(
108    Code,
109    varDecl(hasName("MyVar2"),
110            hasDeclContext(decl(hasDeclContext(translationUnitDecl()))))));
111}
112
113TEST(DeclarationMatcher, LinkageSpecification) {
114  EXPECT_TRUE(matches("extern \"C\" { void foo() {}; }", linkageSpecDecl()));
115  EXPECT_TRUE(notMatches("void foo() {};", linkageSpecDecl()));
116}
117
118TEST(ClassTemplate, DoesNotMatchClass) {
119  DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
120  EXPECT_TRUE(notMatches("class X;", ClassX));
121  EXPECT_TRUE(notMatches("class X {};", ClassX));
122}
123
124TEST(ClassTemplate, MatchesClassTemplate) {
125  DeclarationMatcher ClassX = classTemplateDecl(hasName("X"));
126  EXPECT_TRUE(matches("template<typename T> class X {};", ClassX));
127  EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX));
128}
129
130TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) {
131  EXPECT_TRUE(notMatches("template<typename T> class X { };"
132                           "template<> class X<int> { int a; };",
133                         classTemplateDecl(hasName("X"),
134                                           hasDescendant(fieldDecl(hasName("a"))))));
135}
136
137TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) {
138  EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };"
139                           "template<typename T> class X<T, int> { int a; };",
140                         classTemplateDecl(hasName("X"),
141                                           hasDescendant(fieldDecl(hasName("a"))))));
142}
143
144TEST(DeclarationMatcher, MatchCudaDecl) {
145  EXPECT_TRUE(matchesWithCuda("__global__ void f() { }"
146                                "void g() { f<<<1, 2>>>(); }",
147                              cudaKernelCallExpr()));
148  EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}",
149                              hasAttr(clang::attr::CUDADevice)));
150  EXPECT_TRUE(notMatchesWithCuda("void f() {}",
151                                 cudaKernelCallExpr()));
152  EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}",
153                                  hasAttr(clang::attr::CUDAGlobal)));
154}
155
156TEST(ValueDecl, Matches) {
157  EXPECT_TRUE(matches("enum EnumType { EnumValue };",
158                      valueDecl(hasType(asString("enum EnumType")))));
159  EXPECT_TRUE(matches("void FunctionDecl();",
160                      valueDecl(hasType(asString("void (void)")))));
161}
162
163TEST(Enum, DoesNotMatchClasses) {
164  EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X"))));
165}
166
167TEST(Enum, MatchesEnums) {
168  EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X"))));
169}
170
171TEST(EnumConstant, Matches) {
172  DeclarationMatcher Matcher = enumConstantDecl(hasName("A"));
173  EXPECT_TRUE(matches("enum X{ A };", Matcher));
174  EXPECT_TRUE(notMatches("enum X{ B };", Matcher));
175  EXPECT_TRUE(notMatches("enum X {};", Matcher));
176}
177
178TEST(Matcher, UnresolvedLookupExpr) {
179  // FIXME: The test is known to be broken on Windows with delayed template
180  // parsing.
181  EXPECT_TRUE(matchesConditionally("template<typename T>"
182                                   "T foo() { T a; return a; }"
183                                   "template<typename T>"
184                                   "void bar() {"
185                                   "  foo<T>();"
186                                   "}",
187                                   unresolvedLookupExpr(),
188                                   /*ExpectMatch=*/true,
189                                   "-fno-delayed-template-parsing"));
190}
191
192TEST(Matcher, Call) {
193  // FIXME: Do we want to overload Call() to directly take
194  // Matcher<Decl>, too?
195  StatementMatcher MethodX =
196    callExpr(hasDeclaration(cxxMethodDecl(hasName("x"))));
197
198  EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX));
199  EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX));
200
201  StatementMatcher MethodOnY =
202    cxxMemberCallExpr(on(hasType(recordDecl(hasName("Y")))));
203
204  EXPECT_TRUE(
205    matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
206            MethodOnY));
207  EXPECT_TRUE(
208    matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
209            MethodOnY));
210  EXPECT_TRUE(
211    notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
212               MethodOnY));
213  EXPECT_TRUE(
214    notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
215               MethodOnY));
216  EXPECT_TRUE(
217    notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
218               MethodOnY));
219
220  StatementMatcher MethodOnYPointer =
221    cxxMemberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y"))))));
222
223  EXPECT_TRUE(
224    matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
225            MethodOnYPointer));
226  EXPECT_TRUE(
227    matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
228            MethodOnYPointer));
229  EXPECT_TRUE(
230    matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
231            MethodOnYPointer));
232  EXPECT_TRUE(
233    notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
234               MethodOnYPointer));
235  EXPECT_TRUE(
236    notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
237               MethodOnYPointer));
238}
239TEST(Matcher, Lambda) {
240  EXPECT_TRUE(matches("auto f = [] (int i) { return i; };",
241                      lambdaExpr()));
242}
243
244TEST(Matcher, ForRange) {
245  EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };"
246                        "void f() { for (auto &a : as); }",
247                      cxxForRangeStmt()));
248  EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }",
249                         cxxForRangeStmt()));
250}
251
252TEST(Matcher, SubstNonTypeTemplateParm) {
253  EXPECT_FALSE(matches("template<int N>\n"
254                         "struct A {  static const int n = 0; };\n"
255                         "struct B : public A<42> {};",
256                       substNonTypeTemplateParmExpr()));
257  EXPECT_TRUE(matches("template<int N>\n"
258                        "struct A {  static const int n = N; };\n"
259                        "struct B : public A<42> {};",
260                      substNonTypeTemplateParmExpr()));
261}
262
263TEST(Matcher, NonTypeTemplateParmDecl) {
264  EXPECT_TRUE(matches("template <int N> void f();",
265                      nonTypeTemplateParmDecl(hasName("N"))));
266  EXPECT_TRUE(
267    notMatches("template <typename T> void f();", nonTypeTemplateParmDecl()));
268}
269
270TEST(Matcher, templateTypeParmDecl) {
271  EXPECT_TRUE(matches("template <typename T> void f();",
272                      templateTypeParmDecl(hasName("T"))));
273  EXPECT_TRUE(
274    notMatches("template <int N> void f();", templateTypeParmDecl()));
275}
276
277TEST(Matcher, UserDefinedLiteral) {
278  EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {"
279                        "  return i + 1;"
280                        "}"
281                        "char c = 'a'_inc;",
282                      userDefinedLiteral()));
283}
284
285TEST(Matcher, FlowControl) {
286  EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt()));
287  EXPECT_TRUE(matches("void f() { while(true) { continue; } }",
288                      continueStmt()));
289  EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt()));
290  EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}",
291                      labelStmt(
292                        hasDeclaration(
293                          labelDecl(hasName("FOO"))))));
294  EXPECT_TRUE(matches("void f() { FOO: ; void *ptr = &&FOO; goto *ptr; }",
295                      addrLabelExpr()));
296  EXPECT_TRUE(matches("void f() { return; }", returnStmt()));
297}
298
299TEST(Matcher, OverloadedOperatorCall) {
300  StatementMatcher OpCall = cxxOperatorCallExpr();
301  // Unary operator
302  EXPECT_TRUE(matches("class Y { }; "
303                        "bool operator!(Y x) { return false; }; "
304                        "Y y; bool c = !y;", OpCall));
305  // No match -- special operators like "new", "delete"
306  // FIXME: operator new takes size_t, for which we need stddef.h, for which
307  // we need to figure out include paths in the test.
308  // EXPECT_TRUE(NotMatches("#include <stddef.h>\n"
309  //             "class Y { }; "
310  //             "void *operator new(size_t size) { return 0; } "
311  //             "Y *y = new Y;", OpCall));
312  EXPECT_TRUE(notMatches("class Y { }; "
313                           "void operator delete(void *p) { } "
314                           "void a() {Y *y = new Y; delete y;}", OpCall));
315  // Binary operator
316  EXPECT_TRUE(matches("class Y { }; "
317                        "bool operator&&(Y x, Y y) { return true; }; "
318                        "Y a; Y b; bool c = a && b;",
319                      OpCall));
320  // No match -- normal operator, not an overloaded one.
321  EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall));
322  EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall));
323}
324
325TEST(Matcher, ThisPointerType) {
326  StatementMatcher MethodOnY =
327    cxxMemberCallExpr(thisPointerType(recordDecl(hasName("Y"))));
328
329  EXPECT_TRUE(
330    matches("class Y { public: void x(); }; void z() { Y y; y.x(); }",
331            MethodOnY));
332  EXPECT_TRUE(
333    matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }",
334            MethodOnY));
335  EXPECT_TRUE(
336    matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }",
337            MethodOnY));
338  EXPECT_TRUE(
339    matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }",
340            MethodOnY));
341  EXPECT_TRUE(
342    matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }",
343            MethodOnY));
344
345  EXPECT_TRUE(matches(
346    "class Y {"
347      "  public: virtual void x();"
348      "};"
349      "class X : public Y {"
350      "  public: virtual void x();"
351      "};"
352      "void z() { X *x; x->Y::x(); }", MethodOnY));
353}
354
355TEST(Matcher, VariableUsage) {
356  StatementMatcher Reference =
357    declRefExpr(to(
358      varDecl(hasInitializer(
359        cxxMemberCallExpr(thisPointerType(recordDecl(hasName("Y"))))))));
360
361  EXPECT_TRUE(matches(
362    "class Y {"
363      " public:"
364      "  bool x() const;"
365      "};"
366      "void z(const Y &y) {"
367      "  bool b = y.x();"
368      "  if (b) {}"
369      "}", Reference));
370
371  EXPECT_TRUE(notMatches(
372    "class Y {"
373      " public:"
374      "  bool x() const;"
375      "};"
376      "void z(const Y &y) {"
377      "  bool b = y.x();"
378      "}", Reference));
379}
380
381TEST(Matcher, CalledVariable) {
382  StatementMatcher CallOnVariableY =
383    cxxMemberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))));
384
385  EXPECT_TRUE(matches(
386    "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY));
387  EXPECT_TRUE(matches(
388    "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY));
389  EXPECT_TRUE(matches(
390    "class Y { public: void x(); };"
391      "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY));
392  EXPECT_TRUE(matches(
393    "class Y { public: void x(); };"
394      "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY));
395  EXPECT_TRUE(notMatches(
396    "class Y { public: void x(); };"
397      "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };",
398    CallOnVariableY));
399}
400
401TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) {
402  EXPECT_TRUE(matches("void x() { int a = sizeof(a); }",
403                      unaryExprOrTypeTraitExpr()));
404  EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }",
405                         alignOfExpr(anything())));
406  // FIXME: Uncomment once alignof is enabled.
407  // EXPECT_TRUE(matches("void x() { int a = alignof(a); }",
408  //                     unaryExprOrTypeTraitExpr()));
409  // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }",
410  //                        sizeOfExpr()));
411}
412
413TEST(MemberExpression, DoesNotMatchClasses) {
414  EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr()));
415}
416
417TEST(MemberExpression, MatchesMemberFunctionCall) {
418  EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr()));
419}
420
421TEST(MemberExpression, MatchesVariable) {
422  EXPECT_TRUE(
423    matches("class Y { void x() { this->y; } int y; };", memberExpr()));
424  EXPECT_TRUE(
425    matches("class Y { void x() { y; } int y; };", memberExpr()));
426  EXPECT_TRUE(
427    matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr()));
428}
429
430TEST(MemberExpression, MatchesStaticVariable) {
431  EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };",
432                      memberExpr()));
433  EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };",
434                         memberExpr()));
435  EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };",
436                         memberExpr()));
437}
438
439TEST(Function, MatchesFunctionDeclarations) {
440  StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f"))));
441
442  EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF));
443  EXPECT_TRUE(notMatches("void f() { }", CallFunctionF));
444
445  if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
446    llvm::Triple::Win32) {
447    // FIXME: Make this work for MSVC.
448    // Dependent contexts, but a non-dependent call.
449    EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }",
450                        CallFunctionF));
451    EXPECT_TRUE(
452      matches("void f(); template <int N> struct S { void g() { f(); } };",
453              CallFunctionF));
454  }
455
456  // Depedent calls don't match.
457  EXPECT_TRUE(
458    notMatches("void f(int); template <typename T> void g(T t) { f(t); }",
459               CallFunctionF));
460  EXPECT_TRUE(
461    notMatches("void f(int);"
462                 "template <typename T> struct S { void g(T t) { f(t); } };",
463               CallFunctionF));
464
465  EXPECT_TRUE(matches("void f(...);", functionDecl(isVariadic())));
466  EXPECT_TRUE(notMatches("void f(int);", functionDecl(isVariadic())));
467  EXPECT_TRUE(notMatches("template <typename... Ts> void f(Ts...);",
468                         functionDecl(isVariadic())));
469  EXPECT_TRUE(notMatches("void f();", functionDecl(isVariadic())));
470  EXPECT_TRUE(notMatchesC("void f();", functionDecl(isVariadic())));
471  EXPECT_TRUE(matches("void f(...);", functionDecl(parameterCountIs(0))));
472  EXPECT_TRUE(matchesC("void f();", functionDecl(parameterCountIs(0))));
473  EXPECT_TRUE(matches("void f(int, ...);", functionDecl(parameterCountIs(1))));
474}
475
476TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) {
477  EXPECT_TRUE(
478    matches("template <typename T> void f(T t) {}",
479            functionTemplateDecl(hasName("f"))));
480}
481
482TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) {
483  EXPECT_TRUE(
484    notMatches("void f(double d); void f(int t) {}",
485               functionTemplateDecl(hasName("f"))));
486}
487
488TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) {
489  EXPECT_TRUE(
490    notMatches("void g(); template <typename T> void f(T t) {}"
491                 "template <> void f(int t) { g(); }",
492               functionTemplateDecl(hasName("f"),
493                                    hasDescendant(declRefExpr(to(
494                                      functionDecl(hasName("g"))))))));
495}
496
497TEST(Matcher, MatchesClassTemplateSpecialization) {
498  EXPECT_TRUE(matches("template<typename T> struct A {};"
499                        "template<> struct A<int> {};",
500                      classTemplateSpecializationDecl()));
501  EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;",
502                      classTemplateSpecializationDecl()));
503  EXPECT_TRUE(notMatches("template<typename T> struct A {};",
504                         classTemplateSpecializationDecl()));
505}
506
507TEST(DeclaratorDecl, MatchesDeclaratorDecls) {
508  EXPECT_TRUE(matches("int x;", declaratorDecl()));
509  EXPECT_TRUE(notMatches("class A {};", declaratorDecl()));
510}
511
512TEST(ParmVarDecl, MatchesParmVars) {
513  EXPECT_TRUE(matches("void f(int x);", parmVarDecl()));
514  EXPECT_TRUE(notMatches("void f();", parmVarDecl()));
515}
516
517TEST(Matcher, ConstructorCall) {
518  StatementMatcher Constructor = cxxConstructExpr();
519
520  EXPECT_TRUE(
521    matches("class X { public: X(); }; void x() { X x; }", Constructor));
522  EXPECT_TRUE(
523    matches("class X { public: X(); }; void x() { X x = X(); }",
524            Constructor));
525  EXPECT_TRUE(
526    matches("class X { public: X(int); }; void x() { X x = 0; }",
527            Constructor));
528  EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor));
529}
530
531TEST(Matcher, ThisExpr) {
532  EXPECT_TRUE(
533    matches("struct X { int a; int f () { return a; } };", cxxThisExpr()));
534  EXPECT_TRUE(
535    notMatches("struct X { int f () { int a; return a; } };", cxxThisExpr()));
536}
537
538TEST(Matcher, BindTemporaryExpression) {
539  StatementMatcher TempExpression = cxxBindTemporaryExpr();
540
541  std::string ClassString = "class string { public: string(); ~string(); }; ";
542
543  EXPECT_TRUE(
544    matches(ClassString +
545              "string GetStringByValue();"
546                "void FunctionTakesString(string s);"
547                "void run() { FunctionTakesString(GetStringByValue()); }",
548            TempExpression));
549
550  EXPECT_TRUE(
551    notMatches(ClassString +
552                 "string* GetStringPointer(); "
553                   "void FunctionTakesStringPtr(string* s);"
554                   "void run() {"
555                   "  string* s = GetStringPointer();"
556                   "  FunctionTakesStringPtr(GetStringPointer());"
557                   "  FunctionTakesStringPtr(s);"
558                   "}",
559               TempExpression));
560
561  EXPECT_TRUE(
562    notMatches("class no_dtor {};"
563                 "no_dtor GetObjByValue();"
564                 "void ConsumeObj(no_dtor param);"
565                 "void run() { ConsumeObj(GetObjByValue()); }",
566               TempExpression));
567}
568
569TEST(MaterializeTemporaryExpr, MatchesTemporary) {
570  std::string ClassString =
571    "class string { public: string(); int length(); }; ";
572
573  EXPECT_TRUE(
574    matches(ClassString +
575              "string GetStringByValue();"
576                "void FunctionTakesString(string s);"
577                "void run() { FunctionTakesString(GetStringByValue()); }",
578            materializeTemporaryExpr()));
579
580  EXPECT_TRUE(
581    notMatches(ClassString +
582                 "string* GetStringPointer(); "
583                   "void FunctionTakesStringPtr(string* s);"
584                   "void run() {"
585                   "  string* s = GetStringPointer();"
586                   "  FunctionTakesStringPtr(GetStringPointer());"
587                   "  FunctionTakesStringPtr(s);"
588                   "}",
589               materializeTemporaryExpr()));
590
591  EXPECT_TRUE(
592    notMatches(ClassString +
593                 "string GetStringByValue();"
594                   "void run() { int k = GetStringByValue().length(); }",
595               materializeTemporaryExpr()));
596
597  EXPECT_TRUE(
598    notMatches(ClassString +
599                 "string GetStringByValue();"
600                   "void run() { GetStringByValue(); }",
601               materializeTemporaryExpr()));
602}
603
604TEST(Matcher, NewExpression) {
605  StatementMatcher New = cxxNewExpr();
606
607  EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New));
608  EXPECT_TRUE(
609    matches("class X { public: X(); }; void x() { new X(); }", New));
610  EXPECT_TRUE(
611    matches("class X { public: X(int); }; void x() { new X(0); }", New));
612  EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New));
613}
614
615TEST(Matcher, DeleteExpression) {
616  EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }",
617                      cxxDeleteExpr()));
618}
619
620TEST(Matcher, DefaultArgument) {
621  StatementMatcher Arg = cxxDefaultArgExpr();
622
623  EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg));
624  EXPECT_TRUE(
625    matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg));
626  EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg));
627}
628
629TEST(Matcher, StringLiterals) {
630  StatementMatcher Literal = stringLiteral();
631  EXPECT_TRUE(matches("const char *s = \"string\";", Literal));
632  // wide string
633  EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal));
634  // with escaped characters
635  EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal));
636  // no matching -- though the data type is the same, there is no string literal
637  EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal));
638}
639
640TEST(Matcher, CharacterLiterals) {
641  StatementMatcher CharLiteral = characterLiteral();
642  EXPECT_TRUE(matches("const char c = 'c';", CharLiteral));
643  // wide character
644  EXPECT_TRUE(matches("const char c = L'c';", CharLiteral));
645  // wide character, Hex encoded, NOT MATCHED!
646  EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral));
647  EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral));
648}
649
650TEST(Matcher, IntegerLiterals) {
651  StatementMatcher HasIntLiteral = integerLiteral();
652  EXPECT_TRUE(matches("int i = 10;", HasIntLiteral));
653  EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral));
654  EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral));
655  EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral));
656
657  // Non-matching cases (character literals, float and double)
658  EXPECT_TRUE(notMatches("int i = L'a';",
659                         HasIntLiteral));  // this is actually a character
660  // literal cast to int
661  EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral));
662  EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral));
663  EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral));
664}
665
666TEST(Matcher, FloatLiterals) {
667  StatementMatcher HasFloatLiteral = floatLiteral();
668  EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral));
669  EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral));
670  EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral));
671  EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral));
672  EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral));
673  EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0))));
674  EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0f))));
675  EXPECT_TRUE(
676    matches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(5.0)))));
677
678  EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral));
679  EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0))));
680  EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0f))));
681  EXPECT_TRUE(
682    notMatches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(6.0)))));
683}
684
685TEST(Matcher, NullPtrLiteral) {
686  EXPECT_TRUE(matches("int* i = nullptr;", cxxNullPtrLiteralExpr()));
687}
688
689TEST(Matcher, GNUNullExpr) {
690  EXPECT_TRUE(matches("int* i = __null;", gnuNullExpr()));
691}
692
693TEST(Matcher, AtomicExpr) {
694  EXPECT_TRUE(matches("void foo() { int *ptr; __atomic_load_n(ptr, 1); }",
695                      atomicExpr()));
696}
697
698TEST(Matcher, Initializers) {
699  const char *ToMatch = "void foo() { struct point { double x; double y; };"
700    "  struct point ptarray[10] = "
701    "      { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }";
702  EXPECT_TRUE(matchesConditionally(
703    ToMatch,
704    initListExpr(
705      has(
706        cxxConstructExpr(
707          requiresZeroInitialization())),
708      has(
709        initListExpr(
710          hasType(asString("struct point")),
711          has(floatLiteral(equals(1.0))),
712          has(implicitValueInitExpr(
713            hasType(asString("double")))))),
714      has(
715        initListExpr(
716          hasType(asString("struct point")),
717          has(floatLiteral(equals(2.0))),
718          has(floatLiteral(equals(1.0)))))
719    ), true, "-std=gnu++98"));
720
721  EXPECT_TRUE(matchesC99(ToMatch,
722                         initListExpr(
723                           hasSyntacticForm(
724                             initListExpr(
725                               has(
726                                 designatedInitExpr(
727                                   designatorCountIs(2),
728                                   has(floatLiteral(
729                                     equals(1.0))),
730                                   has(integerLiteral(
731                                     equals(2))))),
732                               has(
733                                 designatedInitExpr(
734                                   designatorCountIs(2),
735                                   has(floatLiteral(
736                                     equals(2.0))),
737                                   has(integerLiteral(
738                                     equals(2))))),
739                               has(
740                                 designatedInitExpr(
741                                   designatorCountIs(2),
742                                   has(floatLiteral(
743                                     equals(1.0))),
744                                   has(integerLiteral(
745                                     equals(0)))))
746                             )))));
747}
748
749TEST(Matcher, ParenListExpr) {
750  EXPECT_TRUE(
751    matches("template<typename T> class foo { void bar() { foo X(*this); } };"
752              "template class foo<int>;",
753            varDecl(hasInitializer(parenListExpr(has(unaryOperator()))))));
754}
755
756TEST(Matcher, StmtExpr) {
757  EXPECT_TRUE(matches("void declToImport() { int C = ({int X=4; X;}); }",
758                      varDecl(hasInitializer(stmtExpr()))));
759}
760
761TEST(Matcher, ImportPredefinedExpr) {
762  // __func__ expands as StringLiteral("foo")
763  EXPECT_TRUE(matches("void foo() { __func__; }",
764                      predefinedExpr(
765                        hasType(asString("const char [4]")),
766                        has(stringLiteral()))));
767}
768
769TEST(Matcher, AsmStatement) {
770  EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt()));
771}
772
773TEST(Matcher, Conditions) {
774  StatementMatcher Condition =
775    ifStmt(hasCondition(cxxBoolLiteral(equals(true))));
776
777  EXPECT_TRUE(matches("void x() { if (true) {} }", Condition));
778  EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition));
779  EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition));
780  EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition));
781  EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition));
782}
783
784TEST(Matcher, ConditionalOperator) {
785  StatementMatcher Conditional = conditionalOperator(
786    hasCondition(cxxBoolLiteral(equals(true))),
787    hasTrueExpression(cxxBoolLiteral(equals(false))));
788
789  EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional));
790  EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional));
791  EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional));
792
793  StatementMatcher ConditionalFalse = conditionalOperator(
794    hasFalseExpression(cxxBoolLiteral(equals(false))));
795
796  EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
797  EXPECT_TRUE(
798    notMatches("void x() { true ? false : true; }", ConditionalFalse));
799
800  EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse));
801  EXPECT_TRUE(
802    notMatches("void x() { true ? false : true; }", ConditionalFalse));
803}
804
805TEST(Matcher, BinaryConditionalOperator) {
806  StatementMatcher AlwaysOne = binaryConditionalOperator(
807    hasCondition(implicitCastExpr(
808      has(
809        opaqueValueExpr(
810          hasSourceExpression((integerLiteral(equals(1)))))))),
811    hasFalseExpression(integerLiteral(equals(0))));
812
813  EXPECT_TRUE(matches("void x() { 1 ?: 0; }", AlwaysOne));
814
815  StatementMatcher FourNotFive = binaryConditionalOperator(
816    hasTrueExpression(opaqueValueExpr(
817      hasSourceExpression((integerLiteral(equals(4)))))),
818    hasFalseExpression(integerLiteral(equals(5))));
819
820  EXPECT_TRUE(matches("void x() { 4 ?: 5; }", FourNotFive));
821}
822
823TEST(ArraySubscriptMatchers, ArraySubscripts) {
824  EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }",
825                      arraySubscriptExpr()));
826  EXPECT_TRUE(notMatches("int i; void f() { i = 1; }",
827                         arraySubscriptExpr()));
828}
829
830TEST(For, FindsForLoops) {
831  EXPECT_TRUE(matches("void f() { for(;;); }", forStmt()));
832  EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt()));
833  EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };"
834                           "void f() { for (auto &a : as); }",
835                         forStmt()));
836}
837
838TEST(For, ReportsNoFalsePositives) {
839  EXPECT_TRUE(notMatches("void f() { ; }", forStmt()));
840  EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt()));
841}
842
843TEST(CompoundStatement, HandlesSimpleCases) {
844  EXPECT_TRUE(notMatches("void f();", compoundStmt()));
845  EXPECT_TRUE(matches("void f() {}", compoundStmt()));
846  EXPECT_TRUE(matches("void f() {{}}", compoundStmt()));
847}
848
849TEST(CompoundStatement, DoesNotMatchEmptyStruct) {
850  // It's not a compound statement just because there's "{}" in the source
851  // text. This is an AST search, not grep.
852  EXPECT_TRUE(notMatches("namespace n { struct S {}; }",
853                         compoundStmt()));
854  EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }",
855                      compoundStmt()));
856}
857
858TEST(CastExpression, MatchesExplicitCasts) {
859  EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr()));
860  EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr()));
861  EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr()));
862  EXPECT_TRUE(matches("char c = char(0);", castExpr()));
863}
864TEST(CastExpression, MatchesImplicitCasts) {
865  // This test creates an implicit cast from int to char.
866  EXPECT_TRUE(matches("char c = 0;", castExpr()));
867  // This test creates an implicit cast from lvalue to rvalue.
868  EXPECT_TRUE(matches("char c = 0, d = c;", castExpr()));
869}
870
871TEST(CastExpression, DoesNotMatchNonCasts) {
872  EXPECT_TRUE(notMatches("char c = '0';", castExpr()));
873  EXPECT_TRUE(notMatches("char c, &q = c;", castExpr()));
874  EXPECT_TRUE(notMatches("int i = (0);", castExpr()));
875  EXPECT_TRUE(notMatches("int i = 0;", castExpr()));
876}
877
878TEST(ReinterpretCast, MatchesSimpleCase) {
879  EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);",
880                      cxxReinterpretCastExpr()));
881}
882
883TEST(ReinterpretCast, DoesNotMatchOtherCasts) {
884  EXPECT_TRUE(notMatches("char* p = (char*)(&p);", cxxReinterpretCastExpr()));
885  EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
886                         cxxReinterpretCastExpr()));
887  EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);",
888                         cxxReinterpretCastExpr()));
889  EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
890                           "B b;"
891                           "D* p = dynamic_cast<D*>(&b);",
892                         cxxReinterpretCastExpr()));
893}
894
895TEST(FunctionalCast, MatchesSimpleCase) {
896  std::string foo_class = "class Foo { public: Foo(const char*); };";
897  EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }",
898                      cxxFunctionalCastExpr()));
899}
900
901TEST(FunctionalCast, DoesNotMatchOtherCasts) {
902  std::string FooClass = "class Foo { public: Foo(const char*); };";
903  EXPECT_TRUE(
904    notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }",
905               cxxFunctionalCastExpr()));
906  EXPECT_TRUE(
907    notMatches(FooClass + "void r() { Foo f = \"hello world\"; }",
908               cxxFunctionalCastExpr()));
909}
910
911TEST(DynamicCast, MatchesSimpleCase) {
912  EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};"
913                        "B b;"
914                        "D* p = dynamic_cast<D*>(&b);",
915                      cxxDynamicCastExpr()));
916}
917
918TEST(StaticCast, MatchesSimpleCase) {
919  EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));",
920                      cxxStaticCastExpr()));
921}
922
923TEST(StaticCast, DoesNotMatchOtherCasts) {
924  EXPECT_TRUE(notMatches("char* p = (char*)(&p);", cxxStaticCastExpr()));
925  EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);",
926                         cxxStaticCastExpr()));
927  EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);",
928                         cxxStaticCastExpr()));
929  EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};"
930                           "B b;"
931                           "D* p = dynamic_cast<D*>(&b);",
932                         cxxStaticCastExpr()));
933}
934
935TEST(CStyleCast, MatchesSimpleCase) {
936  EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr()));
937}
938
939TEST(CStyleCast, DoesNotMatchOtherCasts) {
940  EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);"
941                           "char q, *r = const_cast<char*>(&q);"
942                           "void* s = reinterpret_cast<char*>(&s);"
943                           "struct B { virtual ~B() {} }; struct D : B {};"
944                           "B b;"
945                           "D* t = dynamic_cast<D*>(&b);",
946                         cStyleCastExpr()));
947}
948
949TEST(ImplicitCast, MatchesSimpleCase) {
950  // This test creates an implicit const cast.
951  EXPECT_TRUE(matches("int x = 0; const int y = x;",
952                      varDecl(hasInitializer(implicitCastExpr()))));
953  // This test creates an implicit cast from int to char.
954  EXPECT_TRUE(matches("char c = 0;",
955                      varDecl(hasInitializer(implicitCastExpr()))));
956  // This test creates an implicit array-to-pointer cast.
957  EXPECT_TRUE(matches("int arr[6]; int *p = arr;",
958                      varDecl(hasInitializer(implicitCastExpr()))));
959}
960
961TEST(ImplicitCast, DoesNotMatchIncorrectly) {
962  // This test verifies that implicitCastExpr() matches exactly when implicit casts
963  // are present, and that it ignores explicit and paren casts.
964
965  // These two test cases have no casts.
966  EXPECT_TRUE(notMatches("int x = 0;",
967                         varDecl(hasInitializer(implicitCastExpr()))));
968  EXPECT_TRUE(notMatches("int x = 0, &y = x;",
969                         varDecl(hasInitializer(implicitCastExpr()))));
970
971  EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;",
972                         varDecl(hasInitializer(implicitCastExpr()))));
973  EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);",
974                         varDecl(hasInitializer(implicitCastExpr()))));
975
976  EXPECT_TRUE(notMatches("int x = (0);",
977                         varDecl(hasInitializer(implicitCastExpr()))));
978}
979
980TEST(Statement, DoesNotMatchDeclarations) {
981  EXPECT_TRUE(notMatches("class X {};", stmt()));
982}
983
984TEST(Statement, MatchesCompoundStatments) {
985  EXPECT_TRUE(matches("void x() {}", stmt()));
986}
987
988TEST(DeclarationStatement, DoesNotMatchCompoundStatements) {
989  EXPECT_TRUE(notMatches("void x() {}", declStmt()));
990}
991
992TEST(DeclarationStatement, MatchesVariableDeclarationStatements) {
993  EXPECT_TRUE(matches("void x() { int a; }", declStmt()));
994}
995
996TEST(ExprWithCleanups, MatchesExprWithCleanups) {
997  EXPECT_TRUE(matches("struct Foo { ~Foo(); };"
998                        "const Foo f = Foo();",
999                      varDecl(hasInitializer(exprWithCleanups()))));
1000  EXPECT_FALSE(matches("struct Foo { }; Foo a;"
1001                       "const Foo f = a;",
1002                       varDecl(hasInitializer(exprWithCleanups()))));
1003}
1004
1005TEST(InitListExpression, MatchesInitListExpression) {
1006  EXPECT_TRUE(matches("int a[] = { 1, 2 };",
1007                      initListExpr(hasType(asString("int [2]")))));
1008  EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };",
1009                      initListExpr(hasType(recordDecl(hasName("B"))))));
1010  EXPECT_TRUE(matches("struct S { S(void (*a)()); };"
1011                        "void f();"
1012                        "S s[1] = { &f };",
1013                      declRefExpr(to(functionDecl(hasName("f"))))));
1014  EXPECT_TRUE(
1015    matches("int i[1] = {42, [0] = 43};", integerLiteral(equals(42))));
1016}
1017
1018TEST(UsingDeclaration, MatchesUsingDeclarations) {
1019  EXPECT_TRUE(matches("namespace X { int x; } using X::x;",
1020                      usingDecl()));
1021}
1022
1023TEST(UsingDeclaration, MatchesShadowUsingDelcarations) {
1024  EXPECT_TRUE(matches("namespace f { int a; } using f::a;",
1025                      usingDecl(hasAnyUsingShadowDecl(hasName("a")))));
1026}
1027
1028TEST(UsingDirectiveDeclaration, MatchesUsingNamespace) {
1029  EXPECT_TRUE(matches("namespace X { int x; } using namespace X;",
1030                      usingDirectiveDecl()));
1031  EXPECT_FALSE(
1032    matches("namespace X { int x; } using X::x;", usingDirectiveDecl()));
1033}
1034
1035
1036TEST(While, MatchesWhileLoops) {
1037  EXPECT_TRUE(notMatches("void x() {}", whileStmt()));
1038  EXPECT_TRUE(matches("void x() { while(true); }", whileStmt()));
1039  EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt()));
1040}
1041
1042TEST(Do, MatchesDoLoops) {
1043  EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt()));
1044  EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt()));
1045}
1046
1047TEST(Do, DoesNotMatchWhileLoops) {
1048  EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt()));
1049}
1050
1051TEST(SwitchCase, MatchesCase) {
1052  EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase()));
1053  EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase()));
1054  EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase()));
1055  EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase()));
1056}
1057
1058TEST(SwitchCase, MatchesSwitch) {
1059  EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt()));
1060  EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt()));
1061  EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt()));
1062  EXPECT_TRUE(notMatches("void x() {}", switchStmt()));
1063}
1064
1065TEST(ExceptionHandling, SimpleCases) {
1066  EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", cxxCatchStmt()));
1067  EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", cxxTryStmt()));
1068  EXPECT_TRUE(
1069    notMatches("void foo() try { } catch(int X) { }", cxxThrowExpr()));
1070  EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }",
1071                      cxxThrowExpr()));
1072  EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }",
1073                      cxxThrowExpr()));
1074  EXPECT_TRUE(matches("void foo() try { throw; } catch(...) { }",
1075                      cxxCatchStmt(isCatchAll())));
1076  EXPECT_TRUE(notMatches("void foo() try { throw; } catch(int) { }",
1077                         cxxCatchStmt(isCatchAll())));
1078  EXPECT_TRUE(matches("void foo() try {} catch(int X) { }",
1079                      varDecl(isExceptionVariable())));
1080  EXPECT_TRUE(notMatches("void foo() try { int X; } catch (...) { }",
1081                         varDecl(isExceptionVariable())));
1082}
1083
1084TEST(ParenExpression, SimpleCases) {
1085  EXPECT_TRUE(matches("int i = (3);", parenExpr()));
1086  EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr()));
1087  EXPECT_TRUE(notMatches("int i = 3;", parenExpr()));
1088  EXPECT_TRUE(notMatches("int foo() { return 1; }; int a = foo();",
1089                         parenExpr()));
1090}
1091
1092TEST(TypeMatching, MatchesTypes) {
1093  EXPECT_TRUE(matches("struct S {};", qualType().bind("loc")));
1094}
1095
1096TEST(TypeMatching, MatchesConstantArrayTypes) {
1097  EXPECT_TRUE(matches("int a[2];", constantArrayType()));
1098  EXPECT_TRUE(notMatches(
1099    "void f() { int a[] = { 2, 3 }; int b[a[0]]; }",
1100    constantArrayType(hasElementType(builtinType()))));
1101
1102  EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42))));
1103  EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42))));
1104  EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42))));
1105}
1106
1107TEST(TypeMatching, MatchesDependentSizedArrayTypes) {
1108  EXPECT_TRUE(matches(
1109    "template <typename T, int Size> class array { T data[Size]; };",
1110    dependentSizedArrayType()));
1111  EXPECT_TRUE(notMatches(
1112    "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }",
1113    dependentSizedArrayType()));
1114}
1115
1116TEST(TypeMatching, MatchesIncompleteArrayType) {
1117  EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType()));
1118  EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType()));
1119
1120  EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }",
1121                         incompleteArrayType()));
1122}
1123
1124TEST(TypeMatching, MatchesVariableArrayType) {
1125  EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType()));
1126  EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType()));
1127
1128  EXPECT_TRUE(matches(
1129    "void f(int b) { int a[b]; }",
1130    variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
1131      varDecl(hasName("b")))))))));
1132}
1133
1134
1135TEST(TypeMatching, MatchesAtomicTypes) {
1136  if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() !=
1137    llvm::Triple::Win32) {
1138    // FIXME: Make this work for MSVC.
1139    EXPECT_TRUE(matches("_Atomic(int) i;", atomicType()));
1140
1141    EXPECT_TRUE(matches("_Atomic(int) i;",
1142                        atomicType(hasValueType(isInteger()))));
1143    EXPECT_TRUE(notMatches("_Atomic(float) f;",
1144                           atomicType(hasValueType(isInteger()))));
1145  }
1146}
1147
1148TEST(TypeMatching, MatchesAutoTypes) {
1149  EXPECT_TRUE(matches("auto i = 2;", autoType()));
1150  EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }",
1151                      autoType()));
1152
1153  // FIXME: Matching against the type-as-written can't work here, because the
1154  //        type as written was not deduced.
1155  //EXPECT_TRUE(matches("auto a = 1;",
1156  //                    autoType(hasDeducedType(isInteger()))));
1157  //EXPECT_TRUE(notMatches("auto b = 2.0;",
1158  //                       autoType(hasDeducedType(isInteger()))));
1159}
1160
1161TEST(TypeMatching, MatchesFunctionTypes) {
1162  EXPECT_TRUE(matches("int (*f)(int);", functionType()));
1163  EXPECT_TRUE(matches("void f(int i) {}", functionType()));
1164}
1165
1166TEST(TypeMatching, IgnoringParens) {
1167  EXPECT_TRUE(
1168      notMatches("void (*fp)(void);", pointerType(pointee(functionType()))));
1169  EXPECT_TRUE(matches("void (*fp)(void);",
1170                      pointerType(pointee(ignoringParens(functionType())))));
1171}
1172
1173TEST(TypeMatching, MatchesFunctionProtoTypes) {
1174  EXPECT_TRUE(matches("int (*f)(int);", functionProtoType()));
1175  EXPECT_TRUE(matches("void f(int i);", functionProtoType()));
1176  EXPECT_TRUE(matches("void f();", functionProtoType(parameterCountIs(0))));
1177  EXPECT_TRUE(notMatchesC("void f();", functionProtoType()));
1178  EXPECT_TRUE(
1179    matchesC("void f(void);", functionProtoType(parameterCountIs(0))));
1180}
1181
1182TEST(TypeMatching, MatchesParenType) {
1183  EXPECT_TRUE(
1184    matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType())))));
1185  EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType()))));
1186
1187  EXPECT_TRUE(matches(
1188    "int (*ptr_to_func)(int);",
1189    varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
1190  EXPECT_TRUE(notMatches(
1191    "int (*ptr_to_array)[4];",
1192    varDecl(hasType(pointsTo(parenType(innerType(functionType())))))));
1193}
1194
1195TEST(TypeMatching, PointerTypes) {
1196  // FIXME: Reactive when these tests can be more specific (not matching
1197  // implicit code on certain platforms), likely when we have hasDescendant for
1198  // Types/TypeLocs.
1199  //EXPECT_TRUE(matchAndVerifyResultTrue(
1200  //    "int* a;",
1201  //    pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))),
1202  //    llvm::make_unique<VerifyIdIsBoundTo<TypeLoc>>("loc", 1)));
1203  //EXPECT_TRUE(matchAndVerifyResultTrue(
1204  //    "int* a;",
1205  //    pointerTypeLoc().bind("loc"),
1206  //    llvm::make_unique<VerifyIdIsBoundTo<TypeLoc>>("loc", 1)));
1207  EXPECT_TRUE(matches(
1208    "int** a;",
1209    loc(pointerType(pointee(qualType())))));
1210  EXPECT_TRUE(matches(
1211    "int** a;",
1212    loc(pointerType(pointee(pointerType())))));
1213  EXPECT_TRUE(matches(
1214    "int* b; int* * const a = &b;",
1215    loc(qualType(isConstQualified(), pointerType()))));
1216
1217  std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;";
1218  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1219                                           hasType(blockPointerType()))));
1220  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
1221                                        hasType(memberPointerType()))));
1222  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1223                                           hasType(pointerType()))));
1224  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1225                                           hasType(referenceType()))));
1226  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1227                                           hasType(lValueReferenceType()))));
1228  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1229                                           hasType(rValueReferenceType()))));
1230
1231  Fragment = "int *ptr;";
1232  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1233                                           hasType(blockPointerType()))));
1234  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1235                                           hasType(memberPointerType()))));
1236  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"),
1237                                        hasType(pointerType()))));
1238  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"),
1239                                           hasType(referenceType()))));
1240
1241  Fragment = "int a; int &ref = a;";
1242  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1243                                           hasType(blockPointerType()))));
1244  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1245                                           hasType(memberPointerType()))));
1246  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1247                                           hasType(pointerType()))));
1248  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
1249                                        hasType(referenceType()))));
1250  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
1251                                        hasType(lValueReferenceType()))));
1252  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1253                                           hasType(rValueReferenceType()))));
1254
1255  Fragment = "int &&ref = 2;";
1256  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1257                                           hasType(blockPointerType()))));
1258  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1259                                           hasType(memberPointerType()))));
1260  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1261                                           hasType(pointerType()))));
1262  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
1263                                        hasType(referenceType()))));
1264  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"),
1265                                           hasType(lValueReferenceType()))));
1266  EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"),
1267                                        hasType(rValueReferenceType()))));
1268}
1269
1270TEST(TypeMatching, AutoRefTypes) {
1271  std::string Fragment = "auto a = 1;"
1272    "auto b = a;"
1273    "auto &c = a;"
1274    "auto &&d = c;"
1275    "auto &&e = 2;";
1276  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"),
1277                                           hasType(referenceType()))));
1278  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"),
1279                                           hasType(referenceType()))));
1280  EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
1281                                        hasType(referenceType()))));
1282  EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"),
1283                                        hasType(lValueReferenceType()))));
1284  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"),
1285                                           hasType(rValueReferenceType()))));
1286  EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
1287                                        hasType(referenceType()))));
1288  EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"),
1289                                        hasType(lValueReferenceType()))));
1290  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"),
1291                                           hasType(rValueReferenceType()))));
1292  EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
1293                                        hasType(referenceType()))));
1294  EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"),
1295                                           hasType(lValueReferenceType()))));
1296  EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"),
1297                                        hasType(rValueReferenceType()))));
1298}
1299
1300TEST(TypeMatching, MatchesEnumTypes) {
1301  EXPECT_TRUE(matches("enum Color { Green }; Color color;",
1302                      loc(enumType())));
1303  EXPECT_TRUE(matches("enum class Color { Green }; Color color;",
1304                      loc(enumType())));
1305}
1306
1307TEST(TypeMatching, MatchesPointersToConstTypes) {
1308  EXPECT_TRUE(matches("int b; int * const a = &b;",
1309                      loc(pointerType())));
1310  EXPECT_TRUE(matches("int b; int * const a = &b;",
1311                      loc(pointerType())));
1312  EXPECT_TRUE(matches(
1313    "int b; const int * a = &b;",
1314    loc(pointerType(pointee(builtinType())))));
1315  EXPECT_TRUE(matches(
1316    "int b; const int * a = &b;",
1317    pointerType(pointee(builtinType()))));
1318}
1319
1320TEST(TypeMatching, MatchesTypedefTypes) {
1321  EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"),
1322                                                     hasType(typedefType()))));
1323}
1324
1325TEST(TypeMatching, MatchesTemplateSpecializationType) {
1326  EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;",
1327                      templateSpecializationType()));
1328}
1329
1330TEST(TypeMatching, MatchesRecordType) {
1331  EXPECT_TRUE(matches("class C{}; C c;", recordType()));
1332  EXPECT_TRUE(matches("struct S{}; S s;",
1333                      recordType(hasDeclaration(recordDecl(hasName("S"))))));
1334  EXPECT_TRUE(notMatches("int i;",
1335                         recordType(hasDeclaration(recordDecl(hasName("S"))))));
1336}
1337
1338TEST(TypeMatching, MatchesElaboratedType) {
1339  EXPECT_TRUE(matches(
1340    "namespace N {"
1341      "  namespace M {"
1342      "    class D {};"
1343      "  }"
1344      "}"
1345      "N::M::D d;", elaboratedType()));
1346  EXPECT_TRUE(matches("class C {} c;", elaboratedType()));
1347  EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType()));
1348}
1349
1350TEST(TypeMatching, MatchesSubstTemplateTypeParmType) {
1351  const std::string code = "template <typename T>"
1352    "int F() {"
1353    "  return 1 + T();"
1354    "}"
1355    "int i = F<int>();";
1356  EXPECT_FALSE(matches(code, binaryOperator(hasLHS(
1357    expr(hasType(substTemplateTypeParmType()))))));
1358  EXPECT_TRUE(matches(code, binaryOperator(hasRHS(
1359    expr(hasType(substTemplateTypeParmType()))))));
1360}
1361
1362TEST(NNS, MatchesNestedNameSpecifiers) {
1363  EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;",
1364                      nestedNameSpecifier()));
1365  EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };",
1366                      nestedNameSpecifier()));
1367  EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}",
1368                      nestedNameSpecifier()));
1369  EXPECT_TRUE(matches("namespace a { namespace b {} } namespace ab = a::b;",
1370                      nestedNameSpecifier()));
1371
1372  EXPECT_TRUE(matches(
1373    "struct A { static void f() {} }; void g() { A::f(); }",
1374    nestedNameSpecifier()));
1375  EXPECT_TRUE(notMatches(
1376    "struct A { static void f() {} }; void g(A* a) { a->f(); }",
1377    nestedNameSpecifier()));
1378}
1379
1380TEST(NullStatement, SimpleCases) {
1381  EXPECT_TRUE(matches("void f() {int i;;}", nullStmt()));
1382  EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt()));
1383}
1384
1385TEST(NS, Alias) {
1386  EXPECT_TRUE(matches("namespace test {} namespace alias = ::test;",
1387                      namespaceAliasDecl(hasName("alias"))));
1388}
1389
1390TEST(NNS, MatchesTypes) {
1391  NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
1392    specifiesType(hasDeclaration(recordDecl(hasName("A")))));
1393  EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher));
1394  EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;",
1395                      Matcher));
1396  EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher));
1397}
1398
1399TEST(NNS, MatchesNamespaceDecls) {
1400  NestedNameSpecifierMatcher Matcher = nestedNameSpecifier(
1401    specifiesNamespace(hasName("ns")));
1402  EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher));
1403  EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher));
1404  EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher));
1405}
1406
1407TEST(NNS, MatchesNestedNameSpecifierPrefixes) {
1408  EXPECT_TRUE(matches(
1409    "struct A { struct B { struct C {}; }; }; A::B::C c;",
1410    nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A"))))));
1411  EXPECT_TRUE(matches(
1412    "struct A { struct B { struct C {}; }; }; A::B::C c;",
1413    nestedNameSpecifierLoc(hasPrefix(
1414      specifiesTypeLoc(loc(qualType(asString("struct A"))))))));
1415}
1416
1417
1418template <typename T>
1419class VerifyAncestorHasChildIsEqual : public BoundNodesCallback {
1420public:
1421  bool run(const BoundNodes *Nodes) override { return false; }
1422
1423  bool run(const BoundNodes *Nodes, ASTContext *Context) override {
1424    const T *Node = Nodes->getNodeAs<T>("");
1425    return verify(*Nodes, *Context, Node);
1426  }
1427
1428  bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) {
1429    // Use the original typed pointer to verify we can pass pointers to subtypes
1430    // to equalsNode.
1431    const T *TypedNode = cast<T>(Node);
1432    return selectFirst<T>(
1433      "", match(stmt(hasParent(
1434        stmt(has(stmt(equalsNode(TypedNode)))).bind(""))),
1435                *Node, Context)) != nullptr;
1436  }
1437  bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) {
1438    // Use the original typed pointer to verify we can pass pointers to subtypes
1439    // to equalsNode.
1440    const T *TypedNode = cast<T>(Node);
1441    return selectFirst<T>(
1442      "", match(decl(hasParent(
1443        decl(has(decl(equalsNode(TypedNode)))).bind(""))),
1444                *Node, Context)) != nullptr;
1445  }
1446  bool verify(const BoundNodes &Nodes, ASTContext &Context, const Type *Node) {
1447    // Use the original typed pointer to verify we can pass pointers to subtypes
1448    // to equalsNode.
1449    const T *TypedNode = cast<T>(Node);
1450    const auto *Dec = Nodes.getNodeAs<FieldDecl>("decl");
1451    return selectFirst<T>(
1452      "", match(fieldDecl(hasParent(decl(has(fieldDecl(
1453        hasType(type(equalsNode(TypedNode)).bind(""))))))),
1454                *Dec, Context)) != nullptr;
1455  }
1456};
1457
1458TEST(IsEqualTo, MatchesNodesByIdentity) {
1459  EXPECT_TRUE(matchAndVerifyResultTrue(
1460    "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""),
1461    llvm::make_unique<VerifyAncestorHasChildIsEqual<CXXRecordDecl>>()));
1462  EXPECT_TRUE(matchAndVerifyResultTrue(
1463    "void f() { if (true) if(true) {} }", ifStmt().bind(""),
1464    llvm::make_unique<VerifyAncestorHasChildIsEqual<IfStmt>>()));
1465  EXPECT_TRUE(matchAndVerifyResultTrue(
1466    "class X { class Y {} y; };",
1467    fieldDecl(hasName("y"), hasType(type().bind(""))).bind("decl"),
1468    llvm::make_unique<VerifyAncestorHasChildIsEqual<Type>>()));
1469}
1470
1471TEST(TypedefDeclMatcher, Match) {
1472  EXPECT_TRUE(matches("typedef int typedefDeclTest;",
1473                      typedefDecl(hasName("typedefDeclTest"))));
1474  EXPECT_TRUE(notMatches("using typedefDeclTest2 = int;",
1475                         typedefDecl(hasName("typedefDeclTest2"))));
1476}
1477
1478TEST(TypeAliasDeclMatcher, Match) {
1479  EXPECT_TRUE(matches("using typeAliasTest2 = int;",
1480                      typeAliasDecl(hasName("typeAliasTest2"))));
1481  EXPECT_TRUE(notMatches("typedef int typeAliasTest;",
1482                         typeAliasDecl(hasName("typeAliasTest"))));
1483}
1484
1485TEST(TypedefNameDeclMatcher, Match) {
1486  EXPECT_TRUE(matches("typedef int typedefNameDeclTest1;",
1487                      typedefNameDecl(hasName("typedefNameDeclTest1"))));
1488  EXPECT_TRUE(matches("using typedefNameDeclTest2 = int;",
1489                      typedefNameDecl(hasName("typedefNameDeclTest2"))));
1490}
1491
1492TEST(ObjCMessageExprMatcher, SimpleExprs) {
1493  // don't find ObjCMessageExpr where none are present
1494  EXPECT_TRUE(notMatchesObjC("", objcMessageExpr(anything())));
1495
1496  std::string Objc1String =
1497    "@interface Str "
1498      " - (Str *)uppercaseString:(Str *)str;"
1499      "@end "
1500      "@interface foo "
1501      "- (void)meth:(Str *)text;"
1502      "@end "
1503      " "
1504      "@implementation foo "
1505      "- (void) meth:(Str *)text { "
1506      "  [self contents];"
1507      "  Str *up = [text uppercaseString];"
1508      "} "
1509      "@end ";
1510  EXPECT_TRUE(matchesObjC(
1511    Objc1String,
1512    objcMessageExpr(anything())));
1513  EXPECT_TRUE(matchesObjC(
1514    Objc1String,
1515    objcMessageExpr(hasSelector("contents"))));
1516  EXPECT_TRUE(matchesObjC(
1517    Objc1String,
1518    objcMessageExpr(matchesSelector("cont*"))));
1519  EXPECT_FALSE(matchesObjC(
1520    Objc1String,
1521    objcMessageExpr(matchesSelector("?cont*"))));
1522  EXPECT_TRUE(notMatchesObjC(
1523    Objc1String,
1524    objcMessageExpr(hasSelector("contents"), hasNullSelector())));
1525  EXPECT_TRUE(matchesObjC(
1526    Objc1String,
1527    objcMessageExpr(hasSelector("contents"), hasUnarySelector())));
1528  EXPECT_TRUE(matchesObjC(
1529    Objc1String,
1530    objcMessageExpr(hasSelector("contents"), numSelectorArgs(0))));
1531  EXPECT_TRUE(matchesObjC(
1532    Objc1String,
1533    objcMessageExpr(matchesSelector("uppercase*"),
1534                    argumentCountIs(0)
1535    )));
1536}
1537
1538} // namespace ast_matchers
1539} // namespace clang
1540