| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "ASTMatchersTest.h" |
| 10 | #include "clang/AST/PrettyPrinter.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 13 | #include "clang/Tooling/Tooling.h" |
| 14 | #include "llvm/ADT/Triple.h" |
| 15 | #include "llvm/Support/Host.h" |
| 16 | #include "gtest/gtest.h" |
| 17 | |
| 18 | namespace clang { |
| 19 | namespace ast_matchers { |
| 20 | |
| 21 | TEST(Finder, DynamicOnlyAcceptsSomeMatchers) { |
| 22 | MatchFinder Finder; |
| 23 | EXPECT_TRUE(Finder.addDynamicMatcher(decl(), nullptr)); |
| 24 | EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), nullptr)); |
| 25 | EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)), |
| 26 | nullptr)); |
| 27 | |
| 28 | |
| 29 | EXPECT_FALSE(Finder.addDynamicMatcher(isMain(), nullptr)); |
| 30 | EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), nullptr)); |
| 31 | } |
| 32 | |
| 33 | TEST(Decl, MatchesDeclarations) { |
| 34 | EXPECT_TRUE(notMatches("", decl(usingDecl()))); |
| 35 | EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;", |
| 36 | decl(usingDecl()))); |
| 37 | } |
| 38 | |
| 39 | TEST(NameableDeclaration, MatchesVariousDecls) { |
| 40 | DeclarationMatcher NamedX = namedDecl(hasName("X")); |
| 41 | EXPECT_TRUE(matches("typedef int X;", NamedX)); |
| 42 | EXPECT_TRUE(matches("int X;", NamedX)); |
| 43 | EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX)); |
| 44 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX)); |
| 45 | EXPECT_TRUE(matches("void foo() { int X; }", NamedX)); |
| 46 | EXPECT_TRUE(matches("namespace X { }", NamedX)); |
| 47 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
| 48 | |
| 49 | EXPECT_TRUE(notMatches("#define X 1", NamedX)); |
| 50 | } |
| 51 | |
| 52 | TEST(NameableDeclaration, REMatchesVariousDecls) { |
| 53 | DeclarationMatcher NamedX = namedDecl(matchesName("::X")); |
| 54 | EXPECT_TRUE(matches("typedef int Xa;", NamedX)); |
| 55 | EXPECT_TRUE(matches("int Xb;", NamedX)); |
| 56 | EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX)); |
| 57 | EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX)); |
| 58 | EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX)); |
| 59 | EXPECT_TRUE(matches("namespace Xij { }", NamedX)); |
| 60 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
| 61 | |
| 62 | EXPECT_TRUE(notMatches("#define Xkl 1", NamedX)); |
| 63 | |
| 64 | DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no")); |
| 65 | EXPECT_TRUE(matches("int no_foo;", StartsWithNo)); |
| 66 | EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo)); |
| 67 | |
| 68 | DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c")); |
| 69 | EXPECT_TRUE(matches("int abc;", Abc)); |
| 70 | EXPECT_TRUE(matches("int aFOObBARc;", Abc)); |
| 71 | EXPECT_TRUE(notMatches("int cab;", Abc)); |
| 72 | EXPECT_TRUE(matches("int cabc;", Abc)); |
| 73 | |
| 74 | DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$")); |
| 75 | EXPECT_TRUE(matches("int k;", StartsWithK)); |
| 76 | EXPECT_TRUE(matches("int kAbc;", StartsWithK)); |
| 77 | EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK)); |
| 78 | EXPECT_TRUE(matches("class C { int k; };", StartsWithK)); |
| 79 | EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK)); |
| 80 | } |
| 81 | |
| 82 | TEST(DeclarationMatcher, MatchClass) { |
| 83 | DeclarationMatcher ClassMatcher(recordDecl()); |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | EXPECT_FALSE(matches("", ClassMatcher)); |
| 89 | |
| 90 | DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X"))); |
| 91 | EXPECT_TRUE(matches("class X;", ClassX)); |
| 92 | EXPECT_TRUE(matches("class X {};", ClassX)); |
| 93 | EXPECT_TRUE(matches("template<class T> class X {};", ClassX)); |
| 94 | EXPECT_TRUE(notMatches("", ClassX)); |
| 95 | } |
| 96 | |
| 97 | TEST(DeclarationMatcher, translationUnitDecl) { |
| 98 | const std::string Code = "int MyVar1;\n" |
| 99 | "namespace NameSpace {\n" |
| 100 | "int MyVar2;\n" |
| 101 | "} // namespace NameSpace\n"; |
| 102 | EXPECT_TRUE(matches( |
| 103 | Code, varDecl(hasName("MyVar1"), hasDeclContext(translationUnitDecl())))); |
| 104 | EXPECT_FALSE(matches( |
| 105 | Code, varDecl(hasName("MyVar2"), hasDeclContext(translationUnitDecl())))); |
| 106 | EXPECT_TRUE(matches( |
| 107 | Code, |
| 108 | varDecl(hasName("MyVar2"), |
| 109 | hasDeclContext(decl(hasDeclContext(translationUnitDecl())))))); |
| 110 | } |
| 111 | |
| 112 | TEST(DeclarationMatcher, LinkageSpecification) { |
| 113 | EXPECT_TRUE(matches("extern \"C\" { void foo() {}; }", linkageSpecDecl())); |
| 114 | EXPECT_TRUE(notMatches("void foo() {};", linkageSpecDecl())); |
| 115 | } |
| 116 | |
| 117 | TEST(ClassTemplate, DoesNotMatchClass) { |
| 118 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
| 119 | EXPECT_TRUE(notMatches("class X;", ClassX)); |
| 120 | EXPECT_TRUE(notMatches("class X {};", ClassX)); |
| 121 | } |
| 122 | |
| 123 | TEST(ClassTemplate, MatchesClassTemplate) { |
| 124 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
| 125 | EXPECT_TRUE(matches("template<typename T> class X {};", ClassX)); |
| 126 | EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX)); |
| 127 | } |
| 128 | |
| 129 | TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) { |
| 130 | EXPECT_TRUE(notMatches("template<typename T> class X { };" |
| 131 | "template<> class X<int> { int a; };", |
| 132 | classTemplateDecl(hasName("X"), |
| 133 | hasDescendant(fieldDecl(hasName("a")))))); |
| 134 | } |
| 135 | |
| 136 | TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) { |
| 137 | EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };" |
| 138 | "template<typename T> class X<T, int> { int a; };", |
| 139 | classTemplateDecl(hasName("X"), |
| 140 | hasDescendant(fieldDecl(hasName("a")))))); |
| 141 | } |
| 142 | |
| 143 | TEST(DeclarationMatcher, MatchCudaDecl) { |
| 144 | EXPECT_TRUE(matchesWithCuda("__global__ void f() { }" |
| 145 | "void g() { f<<<1, 2>>>(); }", |
| 146 | cudaKernelCallExpr())); |
| 147 | EXPECT_TRUE(matchesWithCuda("__attribute__((device)) void f() {}", |
| 148 | hasAttr(clang::attr::CUDADevice))); |
| 149 | EXPECT_TRUE(notMatchesWithCuda("void f() {}", |
| 150 | cudaKernelCallExpr())); |
| 151 | EXPECT_FALSE(notMatchesWithCuda("__attribute__((global)) void f() {}", |
| 152 | hasAttr(clang::attr::CUDAGlobal))); |
| 153 | } |
| 154 | |
| 155 | TEST(ValueDecl, Matches) { |
| 156 | EXPECT_TRUE(matches("enum EnumType { EnumValue };", |
| 157 | valueDecl(hasType(asString("enum EnumType"))))); |
| 158 | EXPECT_TRUE(matches("void FunctionDecl();", |
| 159 | valueDecl(hasType(asString("void (void)"))))); |
| 160 | } |
| 161 | |
| 162 | TEST(FriendDecl, Matches) { |
| 163 | EXPECT_TRUE(matches("class Y { friend class X; };", |
| 164 | friendDecl(hasType(asString("class X"))))); |
| 165 | EXPECT_TRUE(matches("class Y { friend class X; };", |
| 166 | friendDecl(hasType(recordDecl(hasName("X")))))); |
| 167 | |
| 168 | EXPECT_TRUE(matches("class Y { friend void f(); };", |
| 169 | functionDecl(hasName("f"), hasParent(friendDecl())))); |
| 170 | } |
| 171 | |
| 172 | TEST(Enum, DoesNotMatchClasses) { |
| 173 | EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X")))); |
| 174 | } |
| 175 | |
| 176 | TEST(Enum, MatchesEnums) { |
| 177 | EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X")))); |
| 178 | } |
| 179 | |
| 180 | TEST(EnumConstant, Matches) { |
| 181 | DeclarationMatcher Matcher = enumConstantDecl(hasName("A")); |
| 182 | EXPECT_TRUE(matches("enum X{ A };", Matcher)); |
| 183 | EXPECT_TRUE(notMatches("enum X{ B };", Matcher)); |
| 184 | EXPECT_TRUE(notMatches("enum X {};", Matcher)); |
| 185 | } |
| 186 | |
| 187 | TEST(Matcher, UnresolvedLookupExpr) { |
| 188 | |
| 189 | |
| 190 | EXPECT_TRUE(matchesConditionally("template<typename T>" |
| 191 | "T foo() { T a; return a; }" |
| 192 | "template<typename T>" |
| 193 | "void bar() {" |
| 194 | " foo<T>();" |
| 195 | "}", |
| 196 | unresolvedLookupExpr(), |
| 197 | , |
| 198 | "-fno-delayed-template-parsing")); |
| 199 | } |
| 200 | |
| 201 | TEST(Matcher, ADLCall) { |
| 202 | StatementMatcher ADLMatch = callExpr(usesADL()); |
| 203 | StatementMatcher ADLMatchOper = cxxOperatorCallExpr(usesADL()); |
| 204 | auto NS_Str = R"cpp( |
| 205 | namespace NS { |
| 206 | struct X {}; |
| 207 | void f(X); |
| 208 | void operator+(X, X); |
| 209 | } |
| 210 | struct MyX {}; |
| 211 | void f(...); |
| 212 | void operator+(MyX, MyX); |
| 213 | )cpp"; |
| 214 | |
| 215 | auto MkStr = [&](std::string Body) -> std::string { |
| 216 | std::string S = NS_Str; |
| 217 | S += "void test_fn() { " + Body + " }"; |
| 218 | return S; |
| 219 | }; |
| 220 | |
| 221 | EXPECT_TRUE(matches(MkStr("NS::X x; f(x);"), ADLMatch)); |
| 222 | EXPECT_TRUE(notMatches(MkStr("NS::X x; NS::f(x);"), ADLMatch)); |
| 223 | EXPECT_TRUE(notMatches(MkStr("MyX x; f(x);"), ADLMatch)); |
| 224 | EXPECT_TRUE(notMatches(MkStr("NS::X x; using NS::f; f(x);"), ADLMatch)); |
| 225 | |
| 226 | |
| 227 | EXPECT_TRUE(matches(MkStr("NS::X x; x + x;"), ADLMatch)); |
| 228 | EXPECT_TRUE(matches(MkStr("NS::X x; x + x;"), ADLMatchOper)); |
| 229 | EXPECT_TRUE(notMatches(MkStr("MyX x; x + x;"), ADLMatch)); |
| 230 | EXPECT_TRUE(notMatches(MkStr("MyX x; x + x;"), ADLMatchOper)); |
| 231 | EXPECT_TRUE(matches(MkStr("NS::X x; operator+(x, x);"), ADLMatch)); |
| 232 | EXPECT_TRUE(notMatches(MkStr("NS::X x; NS::operator+(x, x);"), ADLMatch)); |
| 233 | } |
| 234 | |
| 235 | TEST(Matcher, Call) { |
| 236 | |
| 237 | |
| 238 | StatementMatcher MethodX = |
| 239 | callExpr(hasDeclaration(cxxMethodDecl(hasName("x")))); |
| 240 | |
| 241 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 242 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 243 | |
| 244 | StatementMatcher MethodOnY = |
| 245 | cxxMemberCallExpr(on(hasType(recordDecl(hasName("Y"))))); |
| 246 | |
| 247 | EXPECT_TRUE( |
| 248 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 249 | MethodOnY)); |
| 250 | EXPECT_TRUE( |
| 251 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 252 | MethodOnY)); |
| 253 | EXPECT_TRUE( |
| 254 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 255 | MethodOnY)); |
| 256 | EXPECT_TRUE( |
| 257 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 258 | MethodOnY)); |
| 259 | EXPECT_TRUE( |
| 260 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 261 | MethodOnY)); |
| 262 | |
| 263 | StatementMatcher MethodOnYPointer = |
| 264 | cxxMemberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y")))))); |
| 265 | |
| 266 | EXPECT_TRUE( |
| 267 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 268 | MethodOnYPointer)); |
| 269 | EXPECT_TRUE( |
| 270 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 271 | MethodOnYPointer)); |
| 272 | EXPECT_TRUE( |
| 273 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 274 | MethodOnYPointer)); |
| 275 | EXPECT_TRUE( |
| 276 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 277 | MethodOnYPointer)); |
| 278 | EXPECT_TRUE( |
| 279 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 280 | MethodOnYPointer)); |
| 281 | } |
| 282 | TEST(Matcher, Lambda) { |
| 283 | EXPECT_TRUE(matches("auto f = [] (int i) { return i; };", |
| 284 | lambdaExpr())); |
| 285 | } |
| 286 | |
| 287 | TEST(Matcher, ForRange) { |
| 288 | EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };" |
| 289 | "void f() { for (auto &a : as); }", |
| 290 | cxxForRangeStmt())); |
| 291 | EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }", |
| 292 | cxxForRangeStmt())); |
| 293 | } |
| 294 | |
| 295 | TEST(Matcher, SubstNonTypeTemplateParm) { |
| 296 | EXPECT_FALSE(matches("template<int N>\n" |
| 297 | "struct A { static const int n = 0; };\n" |
| 298 | "struct B : public A<42> {};", |
| 299 | substNonTypeTemplateParmExpr())); |
| 300 | EXPECT_TRUE(matches("template<int N>\n" |
| 301 | "struct A { static const int n = N; };\n" |
| 302 | "struct B : public A<42> {};", |
| 303 | substNonTypeTemplateParmExpr())); |
| 304 | } |
| 305 | |
| 306 | TEST(Matcher, NonTypeTemplateParmDecl) { |
| 307 | EXPECT_TRUE(matches("template <int N> void f();", |
| 308 | nonTypeTemplateParmDecl(hasName("N")))); |
| 309 | EXPECT_TRUE( |
| 310 | notMatches("template <typename T> void f();", nonTypeTemplateParmDecl())); |
| 311 | } |
| 312 | |
| 313 | TEST(Matcher, templateTypeParmDecl) { |
| 314 | EXPECT_TRUE(matches("template <typename T> void f();", |
| 315 | templateTypeParmDecl(hasName("T")))); |
| 316 | EXPECT_TRUE( |
| 317 | notMatches("template <int N> void f();", templateTypeParmDecl())); |
| 318 | } |
| 319 | |
| 320 | TEST(Matcher, UserDefinedLiteral) { |
| 321 | EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {" |
| 322 | " return i + 1;" |
| 323 | "}" |
| 324 | "char c = 'a'_inc;", |
| 325 | userDefinedLiteral())); |
| 326 | } |
| 327 | |
| 328 | TEST(Matcher, FlowControl) { |
| 329 | EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt())); |
| 330 | EXPECT_TRUE(matches("void f() { while(true) { continue; } }", |
| 331 | continueStmt())); |
| 332 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt())); |
| 333 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", |
| 334 | labelStmt( |
| 335 | hasDeclaration( |
| 336 | labelDecl(hasName("FOO")))))); |
| 337 | EXPECT_TRUE(matches("void f() { FOO: ; void *ptr = &&FOO; goto *ptr; }", |
| 338 | addrLabelExpr())); |
| 339 | EXPECT_TRUE(matches("void f() { return; }", returnStmt())); |
| 340 | } |
| 341 | |
| 342 | TEST(Matcher, OverloadedOperatorCall) { |
| 343 | StatementMatcher OpCall = cxxOperatorCallExpr(); |
| 344 | |
| 345 | EXPECT_TRUE(matches("class Y { }; " |
| 346 | "bool operator!(Y x) { return false; }; " |
| 347 | "Y y; bool c = !y;", OpCall)); |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | EXPECT_TRUE(notMatches("class Y { }; " |
| 356 | "void operator delete(void *p) { } " |
| 357 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 358 | |
| 359 | EXPECT_TRUE(matches("class Y { }; " |
| 360 | "bool operator&&(Y x, Y y) { return true; }; " |
| 361 | "Y a; Y b; bool c = a && b;", |
| 362 | OpCall)); |
| 363 | |
| 364 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 365 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 366 | } |
| 367 | |
| 368 | TEST(Matcher, ThisPointerType) { |
| 369 | StatementMatcher MethodOnY = |
| 370 | cxxMemberCallExpr(thisPointerType(recordDecl(hasName("Y")))); |
| 371 | |
| 372 | EXPECT_TRUE( |
| 373 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 374 | MethodOnY)); |
| 375 | EXPECT_TRUE( |
| 376 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 377 | MethodOnY)); |
| 378 | EXPECT_TRUE( |
| 379 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 380 | MethodOnY)); |
| 381 | EXPECT_TRUE( |
| 382 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 383 | MethodOnY)); |
| 384 | EXPECT_TRUE( |
| 385 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 386 | MethodOnY)); |
| 387 | |
| 388 | EXPECT_TRUE(matches( |
| 389 | "class Y {" |
| 390 | " public: virtual void x();" |
| 391 | "};" |
| 392 | "class X : public Y {" |
| 393 | " public: virtual void x();" |
| 394 | "};" |
| 395 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 396 | } |
| 397 | |
| 398 | TEST(Matcher, VariableUsage) { |
| 399 | StatementMatcher Reference = |
| 400 | declRefExpr(to( |
| 401 | varDecl(hasInitializer( |
| 402 | cxxMemberCallExpr(thisPointerType(recordDecl(hasName("Y")))))))); |
| 403 | |
| 404 | EXPECT_TRUE(matches( |
| 405 | "class Y {" |
| 406 | " public:" |
| 407 | " bool x() const;" |
| 408 | "};" |
| 409 | "void z(const Y &y) {" |
| 410 | " bool b = y.x();" |
| 411 | " if (b) {}" |
| 412 | "}", Reference)); |
| 413 | |
| 414 | EXPECT_TRUE(notMatches( |
| 415 | "class Y {" |
| 416 | " public:" |
| 417 | " bool x() const;" |
| 418 | "};" |
| 419 | "void z(const Y &y) {" |
| 420 | " bool b = y.x();" |
| 421 | "}", Reference)); |
| 422 | } |
| 423 | |
| 424 | TEST(Matcher, CalledVariable) { |
| 425 | StatementMatcher CallOnVariableY = |
| 426 | cxxMemberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))); |
| 427 | |
| 428 | EXPECT_TRUE(matches( |
| 429 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 430 | EXPECT_TRUE(matches( |
| 431 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 432 | EXPECT_TRUE(matches( |
| 433 | "class Y { public: void x(); };" |
| 434 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 435 | EXPECT_TRUE(matches( |
| 436 | "class Y { public: void x(); };" |
| 437 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 438 | EXPECT_TRUE(notMatches( |
| 439 | "class Y { public: void x(); };" |
| 440 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 441 | CallOnVariableY)); |
| 442 | } |
| 443 | |
| 444 | TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) { |
| 445 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 446 | unaryExprOrTypeTraitExpr())); |
| 447 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 448 | alignOfExpr(anything()))); |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | } |
| 455 | |
| 456 | TEST(MemberExpression, DoesNotMatchClasses) { |
| 457 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr())); |
| 458 | EXPECT_TRUE(notMatches("class Y { void x() {} };", unresolvedMemberExpr())); |
| 459 | EXPECT_TRUE( |
| 460 | notMatches("class Y { void x() {} };", cxxDependentScopeMemberExpr())); |
| 461 | } |
| 462 | |
| 463 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
| 464 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr())); |
| 465 | EXPECT_TRUE(matches("class Y { template <class T> void x() { x<T>(); } };", |
| 466 | unresolvedMemberExpr())); |
| 467 | EXPECT_TRUE(matches("template <class T> void x() { T t; t.f(); }", |
| 468 | cxxDependentScopeMemberExpr())); |
| 469 | } |
| 470 | |
| 471 | TEST(MemberExpression, MatchesVariable) { |
| 472 | EXPECT_TRUE( |
| 473 | matches("class Y { void x() { this->y; } int y; };", memberExpr())); |
| 474 | EXPECT_TRUE( |
| 475 | matches("class Y { void x() { y; } int y; };", memberExpr())); |
| 476 | EXPECT_TRUE( |
| 477 | matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr())); |
| 478 | EXPECT_TRUE(matches("template <class T>" |
| 479 | "class X : T { void f() { this->T::v; } };", |
| 480 | cxxDependentScopeMemberExpr())); |
| 481 | EXPECT_TRUE(matches("template <class T> class X : T { void f() { T::v; } };", |
| 482 | cxxDependentScopeMemberExpr())); |
| 483 | EXPECT_TRUE(matches("template <class T> void x() { T t; t.v; }", |
| 484 | cxxDependentScopeMemberExpr())); |
| 485 | } |
| 486 | |
| 487 | TEST(MemberExpression, MatchesStaticVariable) { |
| 488 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
| 489 | memberExpr())); |
| 490 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
| 491 | memberExpr())); |
| 492 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
| 493 | memberExpr())); |
| 494 | } |
| 495 | |
| 496 | TEST(Function, MatchesFunctionDeclarations) { |
| 497 | StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f")))); |
| 498 | |
| 499 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 500 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 501 | |
| 502 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() != |
| 503 | llvm::Triple::Win32) { |
| 504 | |
| 505 | |
| 506 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 507 | CallFunctionF)); |
| 508 | EXPECT_TRUE( |
| 509 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 510 | CallFunctionF)); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | EXPECT_TRUE( |
| 515 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 516 | CallFunctionF)); |
| 517 | EXPECT_TRUE( |
| 518 | notMatches("void f(int);" |
| 519 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 520 | CallFunctionF)); |
| 521 | |
| 522 | EXPECT_TRUE(matches("void f(...);", functionDecl(isVariadic()))); |
| 523 | EXPECT_TRUE(notMatches("void f(int);", functionDecl(isVariadic()))); |
| 524 | EXPECT_TRUE(notMatches("template <typename... Ts> void f(Ts...);", |
| 525 | functionDecl(isVariadic()))); |
| 526 | EXPECT_TRUE(notMatches("void f();", functionDecl(isVariadic()))); |
| 527 | EXPECT_TRUE(notMatchesC("void f();", functionDecl(isVariadic()))); |
| 528 | EXPECT_TRUE(matches("void f(...);", functionDecl(parameterCountIs(0)))); |
| 529 | EXPECT_TRUE(matchesC("void f();", functionDecl(parameterCountIs(0)))); |
| 530 | EXPECT_TRUE(matches("void f(int, ...);", functionDecl(parameterCountIs(1)))); |
| 531 | } |
| 532 | |
| 533 | TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) { |
| 534 | EXPECT_TRUE( |
| 535 | matches("template <typename T> void f(T t) {}", |
| 536 | functionTemplateDecl(hasName("f")))); |
| 537 | } |
| 538 | |
| 539 | TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) { |
| 540 | EXPECT_TRUE( |
| 541 | notMatches("void f(double d); void f(int t) {}", |
| 542 | functionTemplateDecl(hasName("f")))); |
| 543 | } |
| 544 | |
| 545 | TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) { |
| 546 | EXPECT_TRUE( |
| 547 | notMatches("void g(); template <typename T> void f(T t) {}" |
| 548 | "template <> void f(int t) { g(); }", |
| 549 | functionTemplateDecl(hasName("f"), |
| 550 | hasDescendant(declRefExpr(to( |
| 551 | functionDecl(hasName("g")))))))); |
| 552 | } |
| 553 | |
| 554 | TEST(Matcher, MatchesClassTemplateSpecialization) { |
| 555 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
| 556 | "template<> struct A<int> {};", |
| 557 | classTemplateSpecializationDecl())); |
| 558 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", |
| 559 | classTemplateSpecializationDecl())); |
| 560 | EXPECT_TRUE(notMatches("template<typename T> struct A {};", |
| 561 | classTemplateSpecializationDecl())); |
| 562 | } |
| 563 | |
| 564 | TEST(DeclaratorDecl, MatchesDeclaratorDecls) { |
| 565 | EXPECT_TRUE(matches("int x;", declaratorDecl())); |
| 566 | EXPECT_TRUE(notMatches("class A {};", declaratorDecl())); |
| 567 | } |
| 568 | |
| 569 | TEST(ParmVarDecl, MatchesParmVars) { |
| 570 | EXPECT_TRUE(matches("void f(int x);", parmVarDecl())); |
| 571 | EXPECT_TRUE(notMatches("void f();", parmVarDecl())); |
| 572 | } |
| 573 | |
| 574 | TEST(Matcher, ConstructorCall) { |
| 575 | StatementMatcher Constructor = cxxConstructExpr(); |
| 576 | |
| 577 | EXPECT_TRUE( |
| 578 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 579 | EXPECT_TRUE( |
| 580 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 581 | Constructor)); |
| 582 | EXPECT_TRUE( |
| 583 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 584 | Constructor)); |
| 585 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 586 | } |
| 587 | |
| 588 | TEST(Match, ConstructorInitializers) { |
| 589 | EXPECT_TRUE(matches("class C { int i; public: C(int ii) : i(ii) {} };", |
| 590 | cxxCtorInitializer(forField(hasName("i"))))); |
| 591 | } |
| 592 | |
| 593 | TEST(Matcher, ThisExpr) { |
| 594 | EXPECT_TRUE( |
| 595 | matches("struct X { int a; int f () { return a; } };", cxxThisExpr())); |
| 596 | EXPECT_TRUE( |
| 597 | notMatches("struct X { int f () { int a; return a; } };", cxxThisExpr())); |
| 598 | } |
| 599 | |
| 600 | TEST(Matcher, BindTemporaryExpression) { |
| 601 | StatementMatcher TempExpression = cxxBindTemporaryExpr(); |
| 602 | |
| 603 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 604 | |
| 605 | EXPECT_TRUE( |
| 606 | matches(ClassString + |
| 607 | "string GetStringByValue();" |
| 608 | "void FunctionTakesString(string s);" |
| 609 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 610 | TempExpression)); |
| 611 | |
| 612 | EXPECT_TRUE( |
| 613 | notMatches(ClassString + |
| 614 | "string* GetStringPointer(); " |
| 615 | "void FunctionTakesStringPtr(string* s);" |
| 616 | "void run() {" |
| 617 | " string* s = GetStringPointer();" |
| 618 | " FunctionTakesStringPtr(GetStringPointer());" |
| 619 | " FunctionTakesStringPtr(s);" |
| 620 | "}", |
| 621 | TempExpression)); |
| 622 | |
| 623 | EXPECT_TRUE( |
| 624 | notMatches("class no_dtor {};" |
| 625 | "no_dtor GetObjByValue();" |
| 626 | "void ConsumeObj(no_dtor param);" |
| 627 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 628 | TempExpression)); |
| 629 | } |
| 630 | |
| 631 | TEST(MaterializeTemporaryExpr, MatchesTemporary) { |
| 632 | std::string ClassString = |
| 633 | "class string { public: string(); int length(); }; "; |
| 634 | |
| 635 | EXPECT_TRUE( |
| 636 | matches(ClassString + |
| 637 | "string GetStringByValue();" |
| 638 | "void FunctionTakesString(string s);" |
| 639 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 640 | materializeTemporaryExpr())); |
| 641 | |
| 642 | EXPECT_TRUE( |
| 643 | notMatches(ClassString + |
| 644 | "string* GetStringPointer(); " |
| 645 | "void FunctionTakesStringPtr(string* s);" |
| 646 | "void run() {" |
| 647 | " string* s = GetStringPointer();" |
| 648 | " FunctionTakesStringPtr(GetStringPointer());" |
| 649 | " FunctionTakesStringPtr(s);" |
| 650 | "}", |
| 651 | materializeTemporaryExpr())); |
| 652 | |
| 653 | EXPECT_TRUE( |
| 654 | matches(ClassString + |
| 655 | "string GetStringByValue();" |
| 656 | "void run() { int k = GetStringByValue().length(); }", |
| 657 | materializeTemporaryExpr())); |
| 658 | |
| 659 | EXPECT_TRUE( |
| 660 | notMatches(ClassString + |
| 661 | "string GetStringByValue();" |
| 662 | "void run() { GetStringByValue(); }", |
| 663 | materializeTemporaryExpr())); |
| 664 | } |
| 665 | |
| 666 | TEST(Matcher, NewExpression) { |
| 667 | StatementMatcher New = cxxNewExpr(); |
| 668 | |
| 669 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 670 | EXPECT_TRUE( |
| 671 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 672 | EXPECT_TRUE( |
| 673 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 674 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 675 | } |
| 676 | |
| 677 | TEST(Matcher, DeleteExpression) { |
| 678 | EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }", |
| 679 | cxxDeleteExpr())); |
| 680 | } |
| 681 | |
| 682 | TEST(Matcher, DefaultArgument) { |
| 683 | StatementMatcher Arg = cxxDefaultArgExpr(); |
| 684 | |
| 685 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 686 | EXPECT_TRUE( |
| 687 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 688 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 689 | } |
| 690 | |
| 691 | TEST(Matcher, StringLiterals) { |
| 692 | StatementMatcher Literal = stringLiteral(); |
| 693 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 694 | |
| 695 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 696 | |
| 697 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 698 | |
| 699 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 700 | } |
| 701 | |
| 702 | TEST(Matcher, CharacterLiterals) { |
| 703 | StatementMatcher CharLiteral = characterLiteral(); |
| 704 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 705 | |
| 706 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 707 | |
| 708 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 709 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 710 | } |
| 711 | |
| 712 | TEST(Matcher, IntegerLiterals) { |
| 713 | StatementMatcher HasIntLiteral = integerLiteral(); |
| 714 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 715 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 716 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 717 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 718 | |
| 719 | |
| 720 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 721 | HasIntLiteral)); |
| 722 | |
| 723 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 724 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 725 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 726 | |
| 727 | |
| 728 | EXPECT_TRUE( |
| 729 | matches("int i = -10;", |
| 730 | unaryOperator(hasOperatorName("-"), |
| 731 | hasUnaryOperand(integerLiteral(equals(10)))))); |
| 732 | } |
| 733 | |
| 734 | TEST(Matcher, FloatLiterals) { |
| 735 | StatementMatcher HasFloatLiteral = floatLiteral(); |
| 736 | EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral)); |
| 737 | EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral)); |
| 738 | EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral)); |
| 739 | EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral)); |
| 740 | EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral)); |
| 741 | EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0)))); |
| 742 | EXPECT_TRUE(matches("double i = 5.0;", floatLiteral(equals(5.0f)))); |
| 743 | EXPECT_TRUE( |
| 744 | matches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(5.0))))); |
| 745 | |
| 746 | EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral)); |
| 747 | EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0)))); |
| 748 | EXPECT_TRUE(notMatches("double i = 5.0;", floatLiteral(equals(6.0f)))); |
| 749 | EXPECT_TRUE( |
| 750 | notMatches("double i = 5.0;", floatLiteral(equals(llvm::APFloat(6.0))))); |
| 751 | } |
| 752 | |
| 753 | TEST(Matcher, NullPtrLiteral) { |
| 754 | EXPECT_TRUE(matches("int* i = nullptr;", cxxNullPtrLiteralExpr())); |
| 755 | } |
| 756 | |
| 757 | TEST(Matcher, ChooseExpr) { |
| 758 | EXPECT_TRUE(matchesC("void f() { (void)__builtin_choose_expr(1, 2, 3); }", |
| 759 | chooseExpr())); |
| 760 | } |
| 761 | |
| 762 | TEST(Matcher, GNUNullExpr) { |
| 763 | EXPECT_TRUE(matches("int* i = __null;", gnuNullExpr())); |
| 764 | } |
| 765 | |
| 766 | TEST(Matcher, AtomicExpr) { |
| 767 | EXPECT_TRUE(matches("void foo() { int *ptr; __atomic_load_n(ptr, 1); }", |
| 768 | atomicExpr())); |
| 769 | } |
| 770 | |
| 771 | TEST(Matcher, Initializers) { |
| 772 | const char *ToMatch = "void foo() { struct point { double x; double y; };" |
| 773 | " struct point ptarray[10] = " |
| 774 | " { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 }; }"; |
| 775 | EXPECT_TRUE(matchesConditionally( |
| 776 | ToMatch, |
| 777 | initListExpr( |
| 778 | has( |
| 779 | cxxConstructExpr( |
| 780 | requiresZeroInitialization())), |
| 781 | has( |
| 782 | initListExpr( |
| 783 | hasType(asString("struct point")), |
| 784 | has(floatLiteral(equals(1.0))), |
| 785 | has(implicitValueInitExpr( |
| 786 | hasType(asString("double")))))), |
| 787 | has( |
| 788 | initListExpr( |
| 789 | hasType(asString("struct point")), |
| 790 | has(floatLiteral(equals(2.0))), |
| 791 | has(floatLiteral(equals(1.0))))) |
| 792 | ), true, "-std=gnu++98")); |
| 793 | |
| 794 | EXPECT_TRUE(matchesC99(ToMatch, |
| 795 | initListExpr( |
| 796 | hasSyntacticForm( |
| 797 | initListExpr( |
| 798 | has( |
| 799 | designatedInitExpr( |
| 800 | designatorCountIs(2), |
| 801 | hasDescendant(floatLiteral( |
| 802 | equals(1.0))), |
| 803 | hasDescendant(integerLiteral( |
| 804 | equals(2))))), |
| 805 | has( |
| 806 | designatedInitExpr( |
| 807 | designatorCountIs(2), |
| 808 | hasDescendant(floatLiteral( |
| 809 | equals(2.0))), |
| 810 | hasDescendant(integerLiteral( |
| 811 | equals(2))))), |
| 812 | has( |
| 813 | designatedInitExpr( |
| 814 | designatorCountIs(2), |
| 815 | hasDescendant(floatLiteral( |
| 816 | equals(1.0))), |
| 817 | hasDescendant(integerLiteral( |
| 818 | equals(0))))) |
| 819 | ))))); |
| 820 | } |
| 821 | |
| 822 | TEST(Matcher, ParenListExpr) { |
| 823 | EXPECT_TRUE( |
| 824 | matches("template<typename T> class foo { void bar() { foo X(*this); } };" |
| 825 | "template class foo<int>;", |
| 826 | varDecl(hasInitializer(parenListExpr(has(unaryOperator())))))); |
| 827 | } |
| 828 | |
| 829 | TEST(Matcher, StmtExpr) { |
| 830 | EXPECT_TRUE(matches("void declToImport() { int C = ({int X=4; X;}); }", |
| 831 | varDecl(hasInitializer(stmtExpr())))); |
| 832 | } |
| 833 | |
| 834 | TEST(Matcher, ImportPredefinedExpr) { |
| 835 | |
| 836 | EXPECT_TRUE(matches("void foo() { __func__; }", |
| 837 | predefinedExpr( |
| 838 | hasType(asString("const char [4]")), |
| 839 | has(stringLiteral())))); |
| 840 | } |
| 841 | |
| 842 | TEST(Matcher, AsmStatement) { |
| 843 | EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt())); |
| 844 | } |
| 845 | |
| 846 | TEST(Matcher, Conditions) { |
| 847 | StatementMatcher Condition = |
| 848 | ifStmt(hasCondition(cxxBoolLiteral(equals(true)))); |
| 849 | |
| 850 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 851 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 852 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 853 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 854 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 855 | } |
| 856 | |
| 857 | TEST(Matcher, ConditionalOperator) { |
| 858 | StatementMatcher Conditional = conditionalOperator( |
| 859 | hasCondition(cxxBoolLiteral(equals(true))), |
| 860 | hasTrueExpression(cxxBoolLiteral(equals(false)))); |
| 861 | |
| 862 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 863 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 864 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 865 | |
| 866 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 867 | hasFalseExpression(cxxBoolLiteral(equals(false)))); |
| 868 | |
| 869 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 870 | EXPECT_TRUE( |
| 871 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 872 | |
| 873 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 874 | EXPECT_TRUE( |
| 875 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 876 | } |
| 877 | |
| 878 | TEST(Matcher, BinaryConditionalOperator) { |
| 879 | StatementMatcher AlwaysOne = binaryConditionalOperator( |
| 880 | hasCondition(implicitCastExpr( |
| 881 | has( |
| 882 | opaqueValueExpr( |
| 883 | hasSourceExpression((integerLiteral(equals(1)))))))), |
| 884 | hasFalseExpression(integerLiteral(equals(0)))); |
| 885 | |
| 886 | EXPECT_TRUE(matches("void x() { 1 ?: 0; }", AlwaysOne)); |
| 887 | |
| 888 | StatementMatcher FourNotFive = binaryConditionalOperator( |
| 889 | hasTrueExpression(opaqueValueExpr( |
| 890 | hasSourceExpression((integerLiteral(equals(4)))))), |
| 891 | hasFalseExpression(integerLiteral(equals(5)))); |
| 892 | |
| 893 | EXPECT_TRUE(matches("void x() { 4 ?: 5; }", FourNotFive)); |
| 894 | } |
| 895 | |
| 896 | TEST(ArraySubscriptMatchers, ArraySubscripts) { |
| 897 | EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }", |
| 898 | arraySubscriptExpr())); |
| 899 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }", |
| 900 | arraySubscriptExpr())); |
| 901 | } |
| 902 | |
| 903 | TEST(For, FindsForLoops) { |
| 904 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 905 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
| 906 | EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };" |
| 907 | "void f() { for (auto &a : as); }", |
| 908 | forStmt())); |
| 909 | } |
| 910 | |
| 911 | TEST(For, ReportsNoFalsePositives) { |
| 912 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 913 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 914 | } |
| 915 | |
| 916 | TEST(CompoundStatement, HandlesSimpleCases) { |
| 917 | EXPECT_TRUE(notMatches("void f();", compoundStmt())); |
| 918 | EXPECT_TRUE(matches("void f() {}", compoundStmt())); |
| 919 | EXPECT_TRUE(matches("void f() {{}}", compoundStmt())); |
| 920 | } |
| 921 | |
| 922 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 923 | |
| 924 | |
| 925 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
| 926 | compoundStmt())); |
| 927 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
| 928 | compoundStmt())); |
| 929 | } |
| 930 | |
| 931 | TEST(CastExpression, MatchesExplicitCasts) { |
| 932 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr())); |
| 933 | EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr())); |
| 934 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr())); |
| 935 | EXPECT_TRUE(matches("char c = char(0);", castExpr())); |
| 936 | } |
| 937 | TEST(CastExpression, MatchesImplicitCasts) { |
| 938 | |
| 939 | EXPECT_TRUE(matches("char c = 0;", castExpr())); |
| 940 | |
| 941 | EXPECT_TRUE(matches("char c = 0, d = c;", castExpr())); |
| 942 | } |
| 943 | |
| 944 | TEST(CastExpression, DoesNotMatchNonCasts) { |
| 945 | EXPECT_TRUE(notMatches("char c = '0';", castExpr())); |
| 946 | EXPECT_TRUE(notMatches("char c, &q = c;", castExpr())); |
| 947 | EXPECT_TRUE(notMatches("int i = (0);", castExpr())); |
| 948 | EXPECT_TRUE(notMatches("int i = 0;", castExpr())); |
| 949 | } |
| 950 | |
| 951 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 952 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
| 953 | cxxReinterpretCastExpr())); |
| 954 | } |
| 955 | |
| 956 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
| 957 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", cxxReinterpretCastExpr())); |
| 958 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
| 959 | cxxReinterpretCastExpr())); |
| 960 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
| 961 | cxxReinterpretCastExpr())); |
| 962 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 963 | "B b;" |
| 964 | "D* p = dynamic_cast<D*>(&b);", |
| 965 | cxxReinterpretCastExpr())); |
| 966 | } |
| 967 | |
| 968 | TEST(FunctionalCast, MatchesSimpleCase) { |
| 969 | std::string foo_class = "class Foo { public: Foo(const char*); };"; |
| 970 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
| 971 | cxxFunctionalCastExpr())); |
| 972 | } |
| 973 | |
| 974 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
| 975 | std::string FooClass = "class Foo { public: Foo(const char*); };"; |
| 976 | EXPECT_TRUE( |
| 977 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
| 978 | cxxFunctionalCastExpr())); |
| 979 | EXPECT_TRUE( |
| 980 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
| 981 | cxxFunctionalCastExpr())); |
| 982 | } |
| 983 | |
| 984 | TEST(DynamicCast, MatchesSimpleCase) { |
| 985 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 986 | "B b;" |
| 987 | "D* p = dynamic_cast<D*>(&b);", |
| 988 | cxxDynamicCastExpr())); |
| 989 | } |
| 990 | |
| 991 | TEST(StaticCast, MatchesSimpleCase) { |
| 992 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
| 993 | cxxStaticCastExpr())); |
| 994 | } |
| 995 | |
| 996 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
| 997 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", cxxStaticCastExpr())); |
| 998 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
| 999 | cxxStaticCastExpr())); |
| 1000 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
| 1001 | cxxStaticCastExpr())); |
| 1002 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 1003 | "B b;" |
| 1004 | "D* p = dynamic_cast<D*>(&b);", |
| 1005 | cxxStaticCastExpr())); |
| 1006 | } |
| 1007 | |
| 1008 | TEST(CStyleCast, MatchesSimpleCase) { |
| 1009 | EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr())); |
| 1010 | } |
| 1011 | |
| 1012 | TEST(CStyleCast, DoesNotMatchOtherCasts) { |
| 1013 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);" |
| 1014 | "char q, *r = const_cast<char*>(&q);" |
| 1015 | "void* s = reinterpret_cast<char*>(&s);" |
| 1016 | "struct B { virtual ~B() {} }; struct D : B {};" |
| 1017 | "B b;" |
| 1018 | "D* t = dynamic_cast<D*>(&b);", |
| 1019 | cStyleCastExpr())); |
| 1020 | } |
| 1021 | |
| 1022 | TEST(ImplicitCast, MatchesSimpleCase) { |
| 1023 | |
| 1024 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
| 1025 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1026 | |
| 1027 | EXPECT_TRUE(matches("char c = 0;", |
| 1028 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1029 | |
| 1030 | EXPECT_TRUE(matches("int arr[6]; int *p = arr;", |
| 1031 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1032 | } |
| 1033 | |
| 1034 | TEST(ImplicitCast, DoesNotMatchIncorrectly) { |
| 1035 | |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | EXPECT_TRUE(notMatches("int x = 0;", |
| 1040 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1041 | EXPECT_TRUE(notMatches("int x = 0, &y = x;", |
| 1042 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1043 | |
| 1044 | EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;", |
| 1045 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1046 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);", |
| 1047 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1048 | |
| 1049 | EXPECT_TRUE(notMatches("int x = (0);", |
| 1050 | varDecl(hasInitializer(implicitCastExpr())))); |
| 1051 | } |
| 1052 | |
| 1053 | TEST(Statement, DoesNotMatchDeclarations) { |
| 1054 | EXPECT_TRUE(notMatches("class X {};", stmt())); |
| 1055 | } |
| 1056 | |
| 1057 | TEST(Statement, MatchesCompoundStatments) { |
| 1058 | EXPECT_TRUE(matches("void x() {}", stmt())); |
| 1059 | } |
| 1060 | |
| 1061 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
| 1062 | EXPECT_TRUE(notMatches("void x() {}", declStmt())); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
| 1066 | EXPECT_TRUE(matches("void x() { int a; }", declStmt())); |
| 1067 | } |
| 1068 | |
| 1069 | TEST(ExprWithCleanups, MatchesExprWithCleanups) { |
| 1070 | EXPECT_TRUE(matches("struct Foo { ~Foo(); };" |
| 1071 | "const Foo f = Foo();", |
| 1072 | varDecl(hasInitializer(exprWithCleanups())))); |
| 1073 | EXPECT_FALSE(matches("struct Foo { }; Foo a;" |
| 1074 | "const Foo f = a;", |
| 1075 | varDecl(hasInitializer(exprWithCleanups())))); |
| 1076 | } |
| 1077 | |
| 1078 | TEST(InitListExpression, MatchesInitListExpression) { |
| 1079 | EXPECT_TRUE(matches("int a[] = { 1, 2 };", |
| 1080 | initListExpr(hasType(asString("int [2]"))))); |
| 1081 | EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };", |
| 1082 | initListExpr(hasType(recordDecl(hasName("B")))))); |
| 1083 | EXPECT_TRUE(matches("struct S { S(void (*a)()); };" |
| 1084 | "void f();" |
| 1085 | "S s[1] = { &f };", |
| 1086 | declRefExpr(to(functionDecl(hasName("f")))))); |
| 1087 | EXPECT_TRUE( |
| 1088 | matches("int i[1] = {42, [0] = 43};", integerLiteral(equals(42)))); |
| 1089 | } |
| 1090 | |
| 1091 | TEST(CXXStdInitializerListExpression, MatchesCXXStdInitializerListExpression) { |
| 1092 | const std::string code = "namespace std {" |
| 1093 | "template <typename> class initializer_list {" |
| 1094 | " public: initializer_list() noexcept {}" |
| 1095 | "};" |
| 1096 | "}" |
| 1097 | "struct A {" |
| 1098 | " A(std::initializer_list<int>) {}" |
| 1099 | "};"; |
| 1100 | EXPECT_TRUE(matches(code + "A a{0};", |
| 1101 | cxxConstructExpr(has(cxxStdInitializerListExpr()), |
| 1102 | hasDeclaration(cxxConstructorDecl( |
| 1103 | ofClass(hasName("A"))))))); |
| 1104 | EXPECT_TRUE(matches(code + "A a = {0};", |
| 1105 | cxxConstructExpr(has(cxxStdInitializerListExpr()), |
| 1106 | hasDeclaration(cxxConstructorDecl( |
| 1107 | ofClass(hasName("A"))))))); |
| 1108 | |
| 1109 | EXPECT_TRUE(notMatches("int a[] = { 1, 2 };", cxxStdInitializerListExpr())); |
| 1110 | EXPECT_TRUE(notMatches("struct B { int x, y; }; B b = { 5, 6 };", |
| 1111 | cxxStdInitializerListExpr())); |
| 1112 | } |
| 1113 | |
| 1114 | TEST(UsingDeclaration, MatchesUsingDeclarations) { |
| 1115 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;", |
| 1116 | usingDecl())); |
| 1117 | } |
| 1118 | |
| 1119 | TEST(UsingDeclaration, MatchesShadowUsingDelcarations) { |
| 1120 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;", |
| 1121 | usingDecl(hasAnyUsingShadowDecl(hasName("a"))))); |
| 1122 | } |
| 1123 | |
| 1124 | TEST(UsingDirectiveDeclaration, MatchesUsingNamespace) { |
| 1125 | EXPECT_TRUE(matches("namespace X { int x; } using namespace X;", |
| 1126 | usingDirectiveDecl())); |
| 1127 | EXPECT_FALSE( |
| 1128 | matches("namespace X { int x; } using X::x;", usingDirectiveDecl())); |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | TEST(While, MatchesWhileLoops) { |
| 1133 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 1134 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 1135 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 1136 | } |
| 1137 | |
| 1138 | TEST(Do, MatchesDoLoops) { |
| 1139 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 1140 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 1141 | } |
| 1142 | |
| 1143 | TEST(Do, DoesNotMatchWhileLoops) { |
| 1144 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 1145 | } |
| 1146 | |
| 1147 | TEST(SwitchCase, MatchesCase) { |
| 1148 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 1149 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 1150 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 1151 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 1152 | } |
| 1153 | |
| 1154 | TEST(SwitchCase, MatchesSwitch) { |
| 1155 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt())); |
| 1156 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt())); |
| 1157 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt())); |
| 1158 | EXPECT_TRUE(notMatches("void x() {}", switchStmt())); |
| 1159 | } |
| 1160 | |
| 1161 | TEST(ExceptionHandling, SimpleCases) { |
| 1162 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", cxxCatchStmt())); |
| 1163 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", cxxTryStmt())); |
| 1164 | EXPECT_TRUE( |
| 1165 | notMatches("void foo() try { } catch(int X) { }", cxxThrowExpr())); |
| 1166 | EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }", |
| 1167 | cxxThrowExpr())); |
| 1168 | EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }", |
| 1169 | cxxThrowExpr())); |
| 1170 | EXPECT_TRUE(matches("void foo() try { throw; } catch(...) { }", |
| 1171 | cxxCatchStmt(isCatchAll()))); |
| 1172 | EXPECT_TRUE(notMatches("void foo() try { throw; } catch(int) { }", |
| 1173 | cxxCatchStmt(isCatchAll()))); |
| 1174 | EXPECT_TRUE(matches("void foo() try {} catch(int X) { }", |
| 1175 | varDecl(isExceptionVariable()))); |
| 1176 | EXPECT_TRUE(notMatches("void foo() try { int X; } catch (...) { }", |
| 1177 | varDecl(isExceptionVariable()))); |
| 1178 | } |
| 1179 | |
| 1180 | TEST(ParenExpression, SimpleCases) { |
| 1181 | EXPECT_TRUE(matches("int i = (3);", parenExpr())); |
| 1182 | EXPECT_TRUE(matches("int i = (3 + 7);", parenExpr())); |
| 1183 | EXPECT_TRUE(notMatches("int i = 3;", parenExpr())); |
| 1184 | EXPECT_TRUE(notMatches("int foo() { return 1; }; int a = foo();", |
| 1185 | parenExpr())); |
| 1186 | } |
| 1187 | |
| 1188 | TEST(ParenExpression, IgnoringParens) { |
| 1189 | EXPECT_FALSE(matches("const char* str = (\"my-string\");", |
| 1190 | implicitCastExpr(hasSourceExpression(stringLiteral())))); |
| 1191 | EXPECT_TRUE(matches( |
| 1192 | "const char* str = (\"my-string\");", |
| 1193 | implicitCastExpr(hasSourceExpression(ignoringParens(stringLiteral()))))); |
| 1194 | } |
| 1195 | |
| 1196 | TEST(TypeMatching, MatchesTypes) { |
| 1197 | EXPECT_TRUE(matches("struct S {};", qualType().bind("loc"))); |
| 1198 | } |
| 1199 | |
| 1200 | TEST(TypeMatching, MatchesConstantArrayTypes) { |
| 1201 | EXPECT_TRUE(matches("int a[2];", constantArrayType())); |
| 1202 | EXPECT_TRUE(notMatches( |
| 1203 | "void f() { int a[] = { 2, 3 }; int b[a[0]]; }", |
| 1204 | constantArrayType(hasElementType(builtinType())))); |
| 1205 | |
| 1206 | EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42)))); |
| 1207 | EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42)))); |
| 1208 | EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42)))); |
| 1209 | } |
| 1210 | |
| 1211 | TEST(TypeMatching, MatchesDependentSizedArrayTypes) { |
| 1212 | EXPECT_TRUE(matches( |
| 1213 | "template <typename T, int Size> class array { T data[Size]; };", |
| 1214 | dependentSizedArrayType())); |
| 1215 | EXPECT_TRUE(notMatches( |
| 1216 | "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }", |
| 1217 | dependentSizedArrayType())); |
| 1218 | } |
| 1219 | |
| 1220 | TEST(TypeMatching, MatchesIncompleteArrayType) { |
| 1221 | EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType())); |
| 1222 | EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType())); |
| 1223 | |
| 1224 | EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }", |
| 1225 | incompleteArrayType())); |
| 1226 | } |
| 1227 | |
| 1228 | TEST(TypeMatching, MatchesVariableArrayType) { |
| 1229 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType())); |
| 1230 | EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType())); |
| 1231 | |
| 1232 | EXPECT_TRUE(matches( |
| 1233 | "void f(int b) { int a[b]; }", |
| 1234 | variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( |
| 1235 | varDecl(hasName("b"))))))))); |
| 1236 | } |
| 1237 | |
| 1238 | |
| 1239 | TEST(TypeMatching, MatchesAtomicTypes) { |
| 1240 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() != |
| 1241 | llvm::Triple::Win32) { |
| 1242 | |
| 1243 | EXPECT_TRUE(matches("_Atomic(int) i;", atomicType())); |
| 1244 | |
| 1245 | EXPECT_TRUE(matches("_Atomic(int) i;", |
| 1246 | atomicType(hasValueType(isInteger())))); |
| 1247 | EXPECT_TRUE(notMatches("_Atomic(float) f;", |
| 1248 | atomicType(hasValueType(isInteger())))); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | TEST(TypeMatching, MatchesAutoTypes) { |
| 1253 | EXPECT_TRUE(matches("auto i = 2;", autoType())); |
| 1254 | EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }", |
| 1255 | autoType())); |
| 1256 | |
| 1257 | EXPECT_TRUE(matches("auto i = 2;", varDecl(hasType(isInteger())))); |
| 1258 | EXPECT_TRUE(matches("struct X{}; auto x = X{};", |
| 1259 | varDecl(hasType(recordDecl(hasName("X")))))); |
| 1260 | |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | |
| 1265 | |
| 1266 | |
| 1267 | } |
| 1268 | |
| 1269 | TEST(TypeMatching, MatchesDeclTypes) { |
| 1270 | EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;", decltypeType())); |
| 1271 | EXPECT_TRUE(matches("decltype(1 + 1) sum = 1 + 1;", |
| 1272 | decltypeType(hasUnderlyingType(isInteger())))); |
| 1273 | } |
| 1274 | |
| 1275 | TEST(TypeMatching, MatchesFunctionTypes) { |
| 1276 | EXPECT_TRUE(matches("int (*f)(int);", functionType())); |
| 1277 | EXPECT_TRUE(matches("void f(int i) {}", functionType())); |
| 1278 | } |
| 1279 | |
| 1280 | TEST(TypeMatching, IgnoringParens) { |
| 1281 | EXPECT_TRUE( |
| 1282 | notMatches("void (*fp)(void);", pointerType(pointee(functionType())))); |
| 1283 | EXPECT_TRUE(matches("void (*fp)(void);", |
| 1284 | pointerType(pointee(ignoringParens(functionType()))))); |
| 1285 | } |
| 1286 | |
| 1287 | TEST(TypeMatching, MatchesFunctionProtoTypes) { |
| 1288 | EXPECT_TRUE(matches("int (*f)(int);", functionProtoType())); |
| 1289 | EXPECT_TRUE(matches("void f(int i);", functionProtoType())); |
| 1290 | EXPECT_TRUE(matches("void f();", functionProtoType(parameterCountIs(0)))); |
| 1291 | EXPECT_TRUE(notMatchesC("void f();", functionProtoType())); |
| 1292 | EXPECT_TRUE( |
| 1293 | matchesC("void f(void);", functionProtoType(parameterCountIs(0)))); |
| 1294 | } |
| 1295 | |
| 1296 | TEST(TypeMatching, MatchesParenType) { |
| 1297 | EXPECT_TRUE( |
| 1298 | matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType()))))); |
| 1299 | EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType())))); |
| 1300 | |
| 1301 | EXPECT_TRUE(matches( |
| 1302 | "int (*ptr_to_func)(int);", |
| 1303 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 1304 | EXPECT_TRUE(notMatches( |
| 1305 | "int (*ptr_to_array)[4];", |
| 1306 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 1307 | } |
| 1308 | |
| 1309 | TEST(TypeMatching, PointerTypes) { |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | |
| 1315 | |
| 1316 | |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | EXPECT_TRUE(matches( |
| 1322 | "int** a;", |
| 1323 | loc(pointerType(pointee(qualType()))))); |
| 1324 | EXPECT_TRUE(matches( |
| 1325 | "int** a;", |
| 1326 | loc(pointerType(pointee(pointerType()))))); |
| 1327 | EXPECT_TRUE(matches( |
| 1328 | "int* b; int* * const a = &b;", |
| 1329 | loc(qualType(isConstQualified(), pointerType())))); |
| 1330 | |
| 1331 | std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; |
| 1332 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1333 | hasType(blockPointerType())))); |
| 1334 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 1335 | hasType(memberPointerType())))); |
| 1336 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1337 | hasType(pointerType())))); |
| 1338 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1339 | hasType(referenceType())))); |
| 1340 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1341 | hasType(lValueReferenceType())))); |
| 1342 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1343 | hasType(rValueReferenceType())))); |
| 1344 | |
| 1345 | Fragment = "int *ptr;"; |
| 1346 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1347 | hasType(blockPointerType())))); |
| 1348 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1349 | hasType(memberPointerType())))); |
| 1350 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 1351 | hasType(pointerType())))); |
| 1352 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 1353 | hasType(referenceType())))); |
| 1354 | |
| 1355 | Fragment = "int a; int &ref = a;"; |
| 1356 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1357 | hasType(blockPointerType())))); |
| 1358 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1359 | hasType(memberPointerType())))); |
| 1360 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1361 | hasType(pointerType())))); |
| 1362 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 1363 | hasType(referenceType())))); |
| 1364 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 1365 | hasType(lValueReferenceType())))); |
| 1366 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1367 | hasType(rValueReferenceType())))); |
| 1368 | |
| 1369 | Fragment = "int &&ref = 2;"; |
| 1370 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1371 | hasType(blockPointerType())))); |
| 1372 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1373 | hasType(memberPointerType())))); |
| 1374 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1375 | hasType(pointerType())))); |
| 1376 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 1377 | hasType(referenceType())))); |
| 1378 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 1379 | hasType(lValueReferenceType())))); |
| 1380 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 1381 | hasType(rValueReferenceType())))); |
| 1382 | } |
| 1383 | |
| 1384 | TEST(TypeMatching, AutoRefTypes) { |
| 1385 | std::string Fragment = "auto a = 1;" |
| 1386 | "auto b = a;" |
| 1387 | "auto &c = a;" |
| 1388 | "auto &&d = c;" |
| 1389 | "auto &&e = 2;"; |
| 1390 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"), |
| 1391 | hasType(referenceType())))); |
| 1392 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"), |
| 1393 | hasType(referenceType())))); |
| 1394 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 1395 | hasType(referenceType())))); |
| 1396 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 1397 | hasType(lValueReferenceType())))); |
| 1398 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"), |
| 1399 | hasType(rValueReferenceType())))); |
| 1400 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 1401 | hasType(referenceType())))); |
| 1402 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 1403 | hasType(lValueReferenceType())))); |
| 1404 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"), |
| 1405 | hasType(rValueReferenceType())))); |
| 1406 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 1407 | hasType(referenceType())))); |
| 1408 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"), |
| 1409 | hasType(lValueReferenceType())))); |
| 1410 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 1411 | hasType(rValueReferenceType())))); |
| 1412 | } |
| 1413 | |
| 1414 | TEST(TypeMatching, MatchesEnumTypes) { |
| 1415 | EXPECT_TRUE(matches("enum Color { Green }; Color color;", |
| 1416 | loc(enumType()))); |
| 1417 | EXPECT_TRUE(matches("enum class Color { Green }; Color color;", |
| 1418 | loc(enumType()))); |
| 1419 | } |
| 1420 | |
| 1421 | TEST(TypeMatching, MatchesPointersToConstTypes) { |
| 1422 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
| 1423 | loc(pointerType()))); |
| 1424 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
| 1425 | loc(pointerType()))); |
| 1426 | EXPECT_TRUE(matches( |
| 1427 | "int b; const int * a = &b;", |
| 1428 | loc(pointerType(pointee(builtinType()))))); |
| 1429 | EXPECT_TRUE(matches( |
| 1430 | "int b; const int * a = &b;", |
| 1431 | pointerType(pointee(builtinType())))); |
| 1432 | } |
| 1433 | |
| 1434 | TEST(TypeMatching, MatchesTypedefTypes) { |
| 1435 | EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"), |
| 1436 | hasType(typedefType())))); |
| 1437 | } |
| 1438 | |
| 1439 | TEST(TypeMatching, MatchesTemplateSpecializationType) { |
| 1440 | EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;", |
| 1441 | templateSpecializationType())); |
| 1442 | } |
| 1443 | |
| 1444 | TEST(TypeMatching, MatchesRecordType) { |
| 1445 | EXPECT_TRUE(matches("class C{}; C c;", recordType())); |
| 1446 | EXPECT_TRUE(matches("struct S{}; S s;", |
| 1447 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
| 1448 | EXPECT_TRUE(notMatches("int i;", |
| 1449 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
| 1450 | } |
| 1451 | |
| 1452 | TEST(TypeMatching, MatchesElaboratedType) { |
| 1453 | EXPECT_TRUE(matches( |
| 1454 | "namespace N {" |
| 1455 | " namespace M {" |
| 1456 | " class D {};" |
| 1457 | " }" |
| 1458 | "}" |
| 1459 | "N::M::D d;", elaboratedType())); |
| 1460 | EXPECT_TRUE(matches("class C {} c;", elaboratedType())); |
| 1461 | EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType())); |
| 1462 | } |
| 1463 | |
| 1464 | TEST(TypeMatching, MatchesSubstTemplateTypeParmType) { |
| 1465 | const std::string code = "template <typename T>" |
| 1466 | "int F() {" |
| 1467 | " return 1 + T();" |
| 1468 | "}" |
| 1469 | "int i = F<int>();"; |
| 1470 | EXPECT_FALSE(matches(code, binaryOperator(hasLHS( |
| 1471 | expr(hasType(substTemplateTypeParmType())))))); |
| 1472 | EXPECT_TRUE(matches(code, binaryOperator(hasRHS( |
| 1473 | expr(hasType(substTemplateTypeParmType())))))); |
| 1474 | } |
| 1475 | |
| 1476 | TEST(NNS, MatchesNestedNameSpecifiers) { |
| 1477 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", |
| 1478 | nestedNameSpecifier())); |
| 1479 | EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };", |
| 1480 | nestedNameSpecifier())); |
| 1481 | EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}", |
| 1482 | nestedNameSpecifier())); |
| 1483 | EXPECT_TRUE(matches("namespace a { namespace b {} } namespace ab = a::b;", |
| 1484 | nestedNameSpecifier())); |
| 1485 | |
| 1486 | EXPECT_TRUE(matches( |
| 1487 | "struct A { static void f() {} }; void g() { A::f(); }", |
| 1488 | nestedNameSpecifier())); |
| 1489 | EXPECT_TRUE(notMatches( |
| 1490 | "struct A { static void f() {} }; void g(A* a) { a->f(); }", |
| 1491 | nestedNameSpecifier())); |
| 1492 | } |
| 1493 | |
| 1494 | TEST(NullStatement, SimpleCases) { |
| 1495 | EXPECT_TRUE(matches("void f() {int i;;}", nullStmt())); |
| 1496 | EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt())); |
| 1497 | } |
| 1498 | |
| 1499 | TEST(NS, Alias) { |
| 1500 | EXPECT_TRUE(matches("namespace test {} namespace alias = ::test;", |
| 1501 | namespaceAliasDecl(hasName("alias")))); |
| 1502 | } |
| 1503 | |
| 1504 | TEST(NNS, MatchesTypes) { |
| 1505 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 1506 | specifiesType(hasDeclaration(recordDecl(hasName("A"))))); |
| 1507 | EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher)); |
| 1508 | EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 1509 | Matcher)); |
| 1510 | EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher)); |
| 1511 | } |
| 1512 | |
| 1513 | TEST(NNS, MatchesNamespaceDecls) { |
| 1514 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 1515 | specifiesNamespace(hasName("ns"))); |
| 1516 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher)); |
| 1517 | EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher)); |
| 1518 | EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher)); |
| 1519 | } |
| 1520 | |
| 1521 | TEST(NNS, MatchesNestedNameSpecifierPrefixes) { |
| 1522 | EXPECT_TRUE(matches( |
| 1523 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 1524 | nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))))); |
| 1525 | EXPECT_TRUE(matches( |
| 1526 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 1527 | nestedNameSpecifierLoc(hasPrefix( |
| 1528 | specifiesTypeLoc(loc(qualType(asString("struct A")))))))); |
| 1529 | EXPECT_TRUE(matches( |
| 1530 | "namespace N { struct A { struct B { struct C {}; }; }; } N::A::B::C c;", |
| 1531 | nestedNameSpecifierLoc(hasPrefix( |
| 1532 | specifiesTypeLoc(loc(qualType(asString("struct N::A")))))))); |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | template <typename T> |
| 1537 | class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { |
| 1538 | public: |
| 1539 | bool run(const BoundNodes *Nodes) override { return false; } |
| 1540 | |
| 1541 | bool run(const BoundNodes *Nodes, ASTContext *Context) override { |
| 1542 | const T *Node = Nodes->getNodeAs<T>(""); |
| 1543 | return verify(*Nodes, *Context, Node); |
| 1544 | } |
| 1545 | |
| 1546 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { |
| 1547 | |
| 1548 | |
| 1549 | const T *TypedNode = cast<T>(Node); |
| 1550 | return selectFirst<T>( |
| 1551 | "", match(stmt(hasParent( |
| 1552 | stmt(has(stmt(equalsNode(TypedNode)))).bind(""))), |
| 1553 | *Node, Context)) != nullptr; |
| 1554 | } |
| 1555 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { |
| 1556 | |
| 1557 | |
| 1558 | const T *TypedNode = cast<T>(Node); |
| 1559 | return selectFirst<T>( |
| 1560 | "", match(decl(hasParent( |
| 1561 | decl(has(decl(equalsNode(TypedNode)))).bind(""))), |
| 1562 | *Node, Context)) != nullptr; |
| 1563 | } |
| 1564 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Type *Node) { |
| 1565 | |
| 1566 | |
| 1567 | const T *TypedNode = cast<T>(Node); |
| 1568 | const auto *Dec = Nodes.getNodeAs<FieldDecl>("decl"); |
| 1569 | return selectFirst<T>( |
| 1570 | "", match(fieldDecl(hasParent(decl(has(fieldDecl( |
| 1571 | hasType(type(equalsNode(TypedNode)).bind(""))))))), |
| 1572 | *Dec, Context)) != nullptr; |
| 1573 | } |
| 1574 | }; |
| 1575 | |
| 1576 | TEST(IsEqualTo, MatchesNodesByIdentity) { |
| 1577 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1578 | "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""), |
| 1579 | llvm::make_unique<VerifyAncestorHasChildIsEqual<CXXRecordDecl>>())); |
| 1580 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1581 | "void f() { if (true) if(true) {} }", ifStmt().bind(""), |
| 1582 | llvm::make_unique<VerifyAncestorHasChildIsEqual<IfStmt>>())); |
| 1583 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1584 | "class X { class Y {} y; };", |
| 1585 | fieldDecl(hasName("y"), hasType(type().bind(""))).bind("decl"), |
| 1586 | llvm::make_unique<VerifyAncestorHasChildIsEqual<Type>>())); |
| 1587 | } |
| 1588 | |
| 1589 | TEST(TypedefDeclMatcher, Match) { |
| 1590 | EXPECT_TRUE(matches("typedef int typedefDeclTest;", |
| 1591 | typedefDecl(hasName("typedefDeclTest")))); |
| 1592 | EXPECT_TRUE(notMatches("using typedefDeclTest2 = int;", |
| 1593 | typedefDecl(hasName("typedefDeclTest2")))); |
| 1594 | } |
| 1595 | |
| 1596 | TEST(TypeAliasDeclMatcher, Match) { |
| 1597 | EXPECT_TRUE(matches("using typeAliasTest2 = int;", |
| 1598 | typeAliasDecl(hasName("typeAliasTest2")))); |
| 1599 | EXPECT_TRUE(notMatches("typedef int typeAliasTest;", |
| 1600 | typeAliasDecl(hasName("typeAliasTest")))); |
| 1601 | } |
| 1602 | |
| 1603 | TEST(TypedefNameDeclMatcher, Match) { |
| 1604 | EXPECT_TRUE(matches("typedef int typedefNameDeclTest1;", |
| 1605 | typedefNameDecl(hasName("typedefNameDeclTest1")))); |
| 1606 | EXPECT_TRUE(matches("using typedefNameDeclTest2 = int;", |
| 1607 | typedefNameDecl(hasName("typedefNameDeclTest2")))); |
| 1608 | } |
| 1609 | |
| 1610 | TEST(TypeAliasTemplateDeclMatcher, Match) { |
| 1611 | std::string Code = R"( |
| 1612 | template <typename T> |
| 1613 | class X { T t; }; |
| 1614 | |
| 1615 | template <typename T> |
| 1616 | using typeAliasTemplateDecl = X<T>; |
| 1617 | |
| 1618 | using typeAliasDecl = X<int>; |
| 1619 | )"; |
| 1620 | EXPECT_TRUE( |
| 1621 | matches(Code, typeAliasTemplateDecl(hasName("typeAliasTemplateDecl")))); |
| 1622 | EXPECT_TRUE( |
| 1623 | notMatches(Code, typeAliasTemplateDecl(hasName("typeAliasDecl")))); |
| 1624 | } |
| 1625 | |
| 1626 | TEST(ObjCMessageExprMatcher, SimpleExprs) { |
| 1627 | |
| 1628 | EXPECT_TRUE(notMatchesObjC("", objcMessageExpr(anything()))); |
| 1629 | |
| 1630 | std::string Objc1String = |
| 1631 | "@interface Str " |
| 1632 | " - (Str *)uppercaseString;" |
| 1633 | "@end " |
| 1634 | "@interface foo " |
| 1635 | "- (void)contents;" |
| 1636 | "- (void)meth:(Str *)text;" |
| 1637 | "@end " |
| 1638 | " " |
| 1639 | "@implementation foo " |
| 1640 | "- (void) meth:(Str *)text { " |
| 1641 | " [self contents];" |
| 1642 | " Str *up = [text uppercaseString];" |
| 1643 | "} " |
| 1644 | "@end "; |
| 1645 | EXPECT_TRUE(matchesObjC( |
| 1646 | Objc1String, |
| 1647 | objcMessageExpr(anything()))); |
| 1648 | EXPECT_TRUE(matchesObjC(Objc1String, |
| 1649 | objcMessageExpr(hasAnySelector({ |
| 1650 | "contents", "meth:"})) |
| 1651 | |
| 1652 | )); |
| 1653 | EXPECT_TRUE(matchesObjC( |
| 1654 | Objc1String, |
| 1655 | objcMessageExpr(hasSelector("contents")))); |
| 1656 | EXPECT_TRUE(matchesObjC( |
| 1657 | Objc1String, |
| 1658 | objcMessageExpr(hasAnySelector("contents", "contentsA")))); |
| 1659 | EXPECT_FALSE(matchesObjC( |
| 1660 | Objc1String, |
| 1661 | objcMessageExpr(hasAnySelector("contentsB", "contentsC")))); |
| 1662 | EXPECT_TRUE(matchesObjC( |
| 1663 | Objc1String, |
| 1664 | objcMessageExpr(matchesSelector("cont*")))); |
| 1665 | EXPECT_FALSE(matchesObjC( |
| 1666 | Objc1String, |
| 1667 | objcMessageExpr(matchesSelector("?cont*")))); |
| 1668 | EXPECT_TRUE(notMatchesObjC( |
| 1669 | Objc1String, |
| 1670 | objcMessageExpr(hasSelector("contents"), hasNullSelector()))); |
| 1671 | EXPECT_TRUE(matchesObjC( |
| 1672 | Objc1String, |
| 1673 | objcMessageExpr(hasSelector("contents"), hasUnarySelector()))); |
| 1674 | EXPECT_TRUE(matchesObjC( |
| 1675 | Objc1String, |
| 1676 | objcMessageExpr(hasSelector("contents"), numSelectorArgs(0)))); |
| 1677 | EXPECT_TRUE(matchesObjC( |
| 1678 | Objc1String, |
| 1679 | objcMessageExpr(matchesSelector("uppercase*"), |
| 1680 | argumentCountIs(0) |
| 1681 | ))); |
| 1682 | } |
| 1683 | |
| 1684 | TEST(ObjCDeclMatcher, CoreDecls) { |
| 1685 | std::string ObjCString = |
| 1686 | "@protocol Proto " |
| 1687 | "- (void)protoDidThing; " |
| 1688 | "@end " |
| 1689 | "@interface Thing " |
| 1690 | "@property int enabled; " |
| 1691 | "@end " |
| 1692 | "@interface Thing (ABC) " |
| 1693 | "- (void)abc_doThing; " |
| 1694 | "@end " |
| 1695 | "@implementation Thing " |
| 1696 | "{ id _ivar; } " |
| 1697 | "- (void)anything {} " |
| 1698 | "@end " |
| 1699 | "@implementation Thing (ABC) " |
| 1700 | "- (void)abc_doThing {} " |
| 1701 | "@end " |
| 1702 | ; |
| 1703 | |
| 1704 | EXPECT_TRUE(matchesObjC( |
| 1705 | ObjCString, |
| 1706 | objcProtocolDecl(hasName("Proto")))); |
| 1707 | EXPECT_TRUE(matchesObjC( |
| 1708 | ObjCString, |
| 1709 | objcImplementationDecl(hasName("Thing")))); |
| 1710 | EXPECT_TRUE(matchesObjC( |
| 1711 | ObjCString, |
| 1712 | objcCategoryDecl(hasName("ABC")))); |
| 1713 | EXPECT_TRUE(matchesObjC( |
| 1714 | ObjCString, |
| 1715 | objcCategoryImplDecl(hasName("ABC")))); |
| 1716 | EXPECT_TRUE(matchesObjC( |
| 1717 | ObjCString, |
| 1718 | objcMethodDecl(hasName("protoDidThing")))); |
| 1719 | EXPECT_TRUE(matchesObjC( |
| 1720 | ObjCString, |
| 1721 | objcMethodDecl(hasName("abc_doThing")))); |
| 1722 | EXPECT_TRUE(matchesObjC( |
| 1723 | ObjCString, |
| 1724 | objcMethodDecl(hasName("anything")))); |
| 1725 | EXPECT_TRUE(matchesObjC( |
| 1726 | ObjCString, |
| 1727 | objcIvarDecl(hasName("_ivar")))); |
| 1728 | EXPECT_TRUE(matchesObjC( |
| 1729 | ObjCString, |
| 1730 | objcPropertyDecl(hasName("enabled")))); |
| 1731 | } |
| 1732 | |
| 1733 | TEST(ObjCStmtMatcher, ExceptionStmts) { |
| 1734 | std::string ObjCString = |
| 1735 | "void f(id obj) {" |
| 1736 | " @try {" |
| 1737 | " @throw obj;" |
| 1738 | " } @catch (...) {" |
| 1739 | " } @finally {}" |
| 1740 | "}"; |
| 1741 | |
| 1742 | EXPECT_TRUE(matchesObjC( |
| 1743 | ObjCString, |
| 1744 | objcTryStmt())); |
| 1745 | EXPECT_TRUE(matchesObjC( |
| 1746 | ObjCString, |
| 1747 | objcThrowStmt())); |
| 1748 | EXPECT_TRUE(matchesObjC( |
| 1749 | ObjCString, |
| 1750 | objcCatchStmt())); |
| 1751 | EXPECT_TRUE(matchesObjC( |
| 1752 | ObjCString, |
| 1753 | objcFinallyStmt())); |
| 1754 | } |
| 1755 | |
| 1756 | TEST(ObjCAutoreleaseMatcher, AutoreleasePool) { |
| 1757 | std::string ObjCString = |
| 1758 | "void f() {" |
| 1759 | "@autoreleasepool {" |
| 1760 | " int x = 1;" |
| 1761 | "}" |
| 1762 | "}"; |
| 1763 | EXPECT_TRUE(matchesObjC(ObjCString, autoreleasePoolStmt())); |
| 1764 | std::string ObjCStringNoPool = "void f() { int x = 1; }"; |
| 1765 | EXPECT_FALSE(matchesObjC(ObjCStringNoPool, autoreleasePoolStmt())); |
| 1766 | } |
| 1767 | |
| 1768 | TEST(OMPExecutableDirective, Matches) { |
| 1769 | auto Matcher = stmt(ompExecutableDirective()); |
| 1770 | |
| 1771 | const std::string Source0 = R"( |
| 1772 | void x() { |
| 1773 | #pragma omp parallel |
| 1774 | ; |
| 1775 | })"; |
| 1776 | EXPECT_TRUE(matchesWithOpenMP(Source0, Matcher)); |
| 1777 | |
| 1778 | const std::string Source1 = R"( |
| 1779 | void x() { |
| 1780 | #pragma omp taskyield |
| 1781 | ; |
| 1782 | })"; |
| 1783 | EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); |
| 1784 | |
| 1785 | const std::string Source2 = R"( |
| 1786 | void x() { |
| 1787 | ; |
| 1788 | })"; |
| 1789 | EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher)); |
| 1790 | } |
| 1791 | |
| 1792 | TEST(OMPDefaultClause, Matches) { |
| 1793 | auto Matcher = ompExecutableDirective(hasAnyClause(ompDefaultClause())); |
| 1794 | |
| 1795 | const std::string Source0 = R"( |
| 1796 | void x() { |
| 1797 | ; |
| 1798 | })"; |
| 1799 | EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 1800 | |
| 1801 | const std::string Source1 = R"( |
| 1802 | void x() { |
| 1803 | #pragma omp parallel |
| 1804 | ; |
| 1805 | })"; |
| 1806 | EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 1807 | |
| 1808 | const std::string Source2 = R"( |
| 1809 | void x() { |
| 1810 | #pragma omp parallel default(none) |
| 1811 | ; |
| 1812 | })"; |
| 1813 | EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
| 1814 | |
| 1815 | const std::string Source3 = R"( |
| 1816 | void x() { |
| 1817 | #pragma omp parallel default(shared) |
| 1818 | ; |
| 1819 | })"; |
| 1820 | EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); |
| 1821 | |
| 1822 | const std::string Source4 = R"( |
| 1823 | void x(int x) { |
| 1824 | #pragma omp parallel num_threads(x) |
| 1825 | ; |
| 1826 | })"; |
| 1827 | EXPECT_TRUE(notMatchesWithOpenMP(Source4, Matcher)); |
| 1828 | } |
| 1829 | |
| 1830 | } |
| 1831 | } |
| 1832 | |