| 1 |
// unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp - AST matcher unit tests// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 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_P(ASTMatchersTest, IsExpandedFromMacro_MatchesInFile) { |
| 22 |
StringRef input = R"cc( |
| 23 |
#define MY_MACRO(a) (4 + (a)) |
| 24 |
void Test() { MY_MACRO(4); } |
| 25 |
)cc"; |
| 26 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("MY_MACRO")))); |
| 27 |
} |
| 28 |
|
| 29 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesNested) { |
| 30 |
StringRef input = R"cc( |
| 31 |
#define MY_MACRO(a) (4 + (a)) |
| 32 |
#define WRAPPER(a) MY_MACRO(a) |
| 33 |
void Test() { WRAPPER(4); } |
| 34 |
)cc"; |
| 35 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("MY_MACRO")))); |
| 36 |
} |
| 37 |
|
| 38 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesIntermediate) { |
| 39 |
StringRef input = R"cc( |
| 40 |
#define IMPL(a) (4 + (a)) |
| 41 |
#define MY_MACRO(a) IMPL(a) |
| 42 |
#define WRAPPER(a) MY_MACRO(a) |
| 43 |
void Test() { WRAPPER(4); } |
| 44 |
)cc"; |
| 45 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("MY_MACRO")))); |
| 46 |
} |
| 47 |
|
| 48 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesTransitive) { |
| 49 |
StringRef input = R"cc( |
| 50 |
#define MY_MACRO(a) (4 + (a)) |
| 51 |
#define WRAPPER(a) MY_MACRO(a) |
| 52 |
void Test() { WRAPPER(4); } |
| 53 |
)cc"; |
| 54 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("WRAPPER")))); |
| 55 |
} |
| 56 |
|
| 57 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesArgument) { |
| 58 |
StringRef input = R"cc( |
| 59 |
#define MY_MACRO(a) (4 + (a)) |
| 60 |
void Test() { |
| 61 |
int x = 5; |
| 62 |
MY_MACRO(x); |
| 63 |
} |
| 64 |
)cc"; |
| 65 |
EXPECT_TRUE(matches(input, declRefExpr(isExpandedFromMacro("MY_MACRO")))); |
| 66 |
} |
| 67 |
|
| 68 |
// Like IsExpandedFromMacro_MatchesArgument, but the argument is itself a |
| 69 |
// macro. |
| 70 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesArgumentMacroExpansion) { |
| 71 |
StringRef input = R"cc( |
| 72 |
#define MY_MACRO(a) (4 + (a)) |
| 73 |
#define IDENTITY(a) (a) |
| 74 |
void Test() { |
| 75 |
IDENTITY(MY_MACRO(2)); |
| 76 |
} |
| 77 |
)cc"; |
| 78 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("IDENTITY")))); |
| 79 |
} |
| 80 |
|
| 81 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesWhenInArgument) { |
| 82 |
StringRef input = R"cc( |
| 83 |
#define MY_MACRO(a) (4 + (a)) |
| 84 |
#define IDENTITY(a) (a) |
| 85 |
void Test() { |
| 86 |
IDENTITY(MY_MACRO(2)); |
| 87 |
} |
| 88 |
)cc"; |
| 89 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("MY_MACRO")))); |
| 90 |
} |
| 91 |
|
| 92 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_MatchesObjectMacro) { |
| 93 |
StringRef input = R"cc( |
| 94 |
#define PLUS (2 + 2) |
| 95 |
void Test() { |
| 96 |
PLUS; |
| 97 |
} |
| 98 |
)cc"; |
| 99 |
EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("PLUS")))); |
| 100 |
} |
| 101 |
|
| 102 |
TEST(IsExpandedFromMacro, MatchesFromCommandLine) { |
| 103 |
StringRef input = R"cc( |
| 104 |
void Test() { FOUR_PLUS_FOUR; } |
| 105 |
)cc"; |
| 106 |
EXPECT_TRUE(matchesConditionally( |
| 107 |
input, binaryOperator(isExpandedFromMacro("FOUR_PLUS_FOUR")), true, |
| 108 |
{"-std=c++11", "-DFOUR_PLUS_FOUR=4+4"})); |
| 109 |
} |
| 110 |
|
| 111 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_NotMatchesBeginOnly) { |
| 112 |
StringRef input = R"cc( |
| 113 |
#define ONE_PLUS 1+ |
| 114 |
void Test() { ONE_PLUS 4; } |
| 115 |
)cc"; |
| 116 |
EXPECT_TRUE( |
| 117 |
notMatches(input, binaryOperator(isExpandedFromMacro("ONE_PLUS")))); |
| 118 |
} |
| 119 |
|
| 120 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_NotMatchesEndOnly) { |
| 121 |
StringRef input = R"cc( |
| 122 |
#define PLUS_ONE +1 |
| 123 |
void Test() { 4 PLUS_ONE; } |
| 124 |
)cc"; |
| 125 |
EXPECT_TRUE( |
| 126 |
notMatches(input, binaryOperator(isExpandedFromMacro("PLUS_ONE")))); |
| 127 |
} |
| 128 |
|
| 129 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_NotMatchesDifferentMacro) { |
| 130 |
StringRef input = R"cc( |
| 131 |
#define MY_MACRO(a) (4 + (a)) |
| 132 |
void Test() { MY_MACRO(4); } |
| 133 |
)cc"; |
| 134 |
EXPECT_TRUE(notMatches(input, binaryOperator(isExpandedFromMacro("OTHER")))); |
| 135 |
} |
| 136 |
|
| 137 |
TEST_P(ASTMatchersTest, IsExpandedFromMacro_NotMatchesDifferentInstances) { |
| 138 |
StringRef input = R"cc( |
| 139 |
#define FOUR 4 |
| 140 |
void Test() { FOUR + FOUR; } |
| 141 |
)cc"; |
| 142 |
EXPECT_TRUE(notMatches(input, binaryOperator(isExpandedFromMacro("FOUR")))); |
| 143 |
} |
| 144 |
|
| 145 |
TEST(IsExpandedFromMacro, IsExpandedFromMacro_MatchesDecls) { |
| 146 |
StringRef input = R"cc( |
| 147 |
#define MY_MACRO(a) int i = a; |
| 148 |
void Test() { MY_MACRO(4); } |
| 149 |
)cc"; |
| 150 |
EXPECT_TRUE(matches(input, varDecl(isExpandedFromMacro("MY_MACRO")))); |
| 151 |
} |
| 152 |
|
| 153 |
TEST(IsExpandedFromMacro, IsExpandedFromMacro_MatchesTypelocs) { |
| 154 |
StringRef input = R"cc( |
| 155 |
#define MY_TYPE int |
| 156 |
void Test() { MY_TYPE i = 4; } |
| 157 |
)cc"; |
| 158 |
EXPECT_TRUE(matches(input, typeLoc(isExpandedFromMacro("MY_TYPE")))); |
| 159 |
} |
| 160 |
|
| 161 |
TEST_P(ASTMatchersTest, AllOf) { |
| 162 |
const char Program[] = "struct T { };" |
| 163 |
"int f(int, struct T*, int, int);" |
| 164 |
"void g(int x) { struct T t; f(x, &t, 3, 4); }"; |
| 165 |
EXPECT_TRUE(matches( |
| 166 |
Program, callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 167 |
hasArgument(0, declRefExpr(to(varDecl()))))))); |
| 168 |
EXPECT_TRUE(matches( |
| 169 |
Program, |
| 170 |
callExpr( |
| 171 |
allOf(callee(functionDecl(hasName("f"))), |
| 172 |
hasArgument(0, declRefExpr(to(varDecl()))), |
| 173 |
hasArgument(1, hasType(pointsTo(recordDecl(hasName("T"))))))))); |
| 174 |
EXPECT_TRUE(matches( |
| 175 |
Program, callExpr(allOf( |
| 176 |
callee(functionDecl(hasName("f"))), |
| 177 |
hasArgument(0, declRefExpr(to(varDecl()))), |
| 178 |
hasArgument(1, hasType(pointsTo(recordDecl(hasName("T"))))), |
| 179 |
hasArgument(2, integerLiteral(equals(3))))))); |
| 180 |
EXPECT_TRUE(matches( |
| 181 |
Program, callExpr(allOf( |
| 182 |
callee(functionDecl(hasName("f"))), |
| 183 |
hasArgument(0, declRefExpr(to(varDecl()))), |
| 184 |
hasArgument(1, hasType(pointsTo(recordDecl(hasName("T"))))), |
| 185 |
hasArgument(2, integerLiteral(equals(3))), |
| 186 |
hasArgument(3, integerLiteral(equals(4))))))); |
| 187 |
} |
| 188 |
|
| 189 |
TEST_P(ASTMatchersTest, Has) { |
| 190 |
if (!GetParam().isCXX()) { |
| 191 |
// FIXME: Add a test for `has()` that does not depend on C++. |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X")))); |
| 196 |
EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); |
| 197 |
EXPECT_TRUE(matches("class X {};", HasClassX)); |
| 198 |
|
| 199 |
DeclarationMatcher YHasClassX = |
| 200 |
recordDecl(hasName("Y"), has(recordDecl(hasName("X")))); |
| 201 |
EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX)); |
| 202 |
EXPECT_TRUE(notMatches("class X {};", YHasClassX)); |
| 203 |
EXPECT_TRUE(notMatches("class Y { class Z { class X {}; }; };", YHasClassX)); |
| 204 |
} |
| 205 |
|
| 206 |
TEST_P(ASTMatchersTest, Has_RecursiveAllOf) { |
| 207 |
if (!GetParam().isCXX()) { |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
DeclarationMatcher Recursive = |
| 212 |
recordDecl(has(recordDecl(has(recordDecl(hasName("X"))), |
| 213 |
has(recordDecl(hasName("Y"))), hasName("Z"))), |
| 214 |
has(recordDecl(has(recordDecl(hasName("A"))), |
| 215 |
has(recordDecl(hasName("B"))), hasName("C"))), |
| 216 |
hasName("F")); |
| 217 |
|
| 218 |
EXPECT_TRUE(matches("class F {" |
| 219 |
" class Z {" |
| 220 |
" class X {};" |
| 221 |
" class Y {};" |
| 222 |
" };" |
| 223 |
" class C {" |
| 224 |
" class A {};" |
| 225 |
" class B {};" |
| 226 |
" };" |
| 227 |
"};", |
| 228 |
Recursive)); |
| 229 |
|
| 230 |
EXPECT_TRUE(matches("class F {" |
| 231 |
" class Z {" |
| 232 |
" class A {};" |
| 233 |
" class X {};" |
| 234 |
" class Y {};" |
| 235 |
" };" |
| 236 |
" class C {" |
| 237 |
" class X {};" |
| 238 |
" class A {};" |
| 239 |
" class B {};" |
| 240 |
" };" |
| 241 |
"};", |
| 242 |
Recursive)); |
| 243 |
|
| 244 |
EXPECT_TRUE(matches("class O1 {" |
| 245 |
" class O2 {" |
| 246 |
" class F {" |
| 247 |
" class Z {" |
| 248 |
" class A {};" |
| 249 |
" class X {};" |
| 250 |
" class Y {};" |
| 251 |
" };" |
| 252 |
" class C {" |
| 253 |
" class X {};" |
| 254 |
" class A {};" |
| 255 |
" class B {};" |
| 256 |
" };" |
| 257 |
" };" |
| 258 |
" };" |
| 259 |
"};", |
| 260 |
Recursive)); |
| 261 |
} |
| 262 |
|
| 263 |
TEST_P(ASTMatchersTest, Has_RecursiveAnyOf) { |
| 264 |
if (!GetParam().isCXX()) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
DeclarationMatcher Recursive = recordDecl( |
| 269 |
anyOf(has(recordDecl(anyOf(has(recordDecl(hasName("X"))), |
| 270 |
has(recordDecl(hasName("Y"))), hasName("Z")))), |
| 271 |
has(recordDecl(anyOf(hasName("C"), has(recordDecl(hasName("A"))), |
| 272 |
has(recordDecl(hasName("B")))))), |
| 273 |
hasName("F"))); |
| 274 |
|
| 275 |
EXPECT_TRUE(matches("class F {};", Recursive)); |
| 276 |
EXPECT_TRUE(matches("class Z {};", Recursive)); |
| 277 |
EXPECT_TRUE(matches("class C {};", Recursive)); |
| 278 |
EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive)); |
| 279 |
EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive)); |
| 280 |
EXPECT_TRUE(matches("class O1 { class O2 {" |
| 281 |
" class M { class N { class B {}; }; }; " |
| 282 |
"}; };", |
| 283 |
Recursive)); |
| 284 |
} |
| 285 |
|
| 286 |
TEST_P(ASTMatchersTest, Unless) { |
| 287 |
if (!GetParam().isCXX()) { |
| 288 |
// FIXME: Add a test for `unless()` that does not depend on C++. |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
DeclarationMatcher NotClassX = |
| 293 |
cxxRecordDecl(isDerivedFrom("Y"), unless(hasName("X"))); |
| 294 |
EXPECT_TRUE(notMatches("", NotClassX)); |
| 295 |
EXPECT_TRUE(notMatches("class Y {};", NotClassX)); |
| 296 |
EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX)); |
| 297 |
EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX)); |
| 298 |
EXPECT_TRUE( |
| 299 |
notMatches("class Y {}; class Z {}; class X : public Y {};", NotClassX)); |
| 300 |
|
| 301 |
DeclarationMatcher ClassXHasNotClassY = |
| 302 |
recordDecl(hasName("X"), has(recordDecl(hasName("Z"))), |
| 303 |
unless(has(recordDecl(hasName("Y"))))); |
| 304 |
EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY)); |
| 305 |
EXPECT_TRUE( |
| 306 |
notMatches("class X { class Y {}; class Z {}; };", ClassXHasNotClassY)); |
| 307 |
|
| 308 |
DeclarationMatcher NamedNotRecord = |
| 309 |
namedDecl(hasName("Foo"), unless(recordDecl())); |
| 310 |
EXPECT_TRUE(matches("void Foo(){}", NamedNotRecord)); |
| 311 |
EXPECT_TRUE(notMatches("struct Foo {};", NamedNotRecord)); |
| 312 |
} |
| 313 |
|
| 314 |
TEST_P(ASTMatchersTest, HasCastKind) { |
| 315 |
EXPECT_TRUE( |
| 316 |
matches("char *p = 0;", |
| 317 |
traverse(TK_AsIs, |
| 318 |
varDecl(has(castExpr(hasCastKind(CK_NullToPointer))))))); |
| 319 |
EXPECT_TRUE(notMatches( |
| 320 |
"char *p = 0;", |
| 321 |
traverse(TK_AsIs, |
| 322 |
varDecl(has(castExpr(hasCastKind(CK_DerivedToBase))))))); |
| 323 |
EXPECT_TRUE(matches("char *p = 0;", |
| 324 |
traverse(TK_AsIs, varDecl(has(implicitCastExpr( |
| 325 |
hasCastKind(CK_NullToPointer))))))); |
| 326 |
} |
| 327 |
|
| 328 |
TEST_P(ASTMatchersTest, HasDescendant) { |
| 329 |
if (!GetParam().isCXX()) { |
| 330 |
// FIXME: Add a test for `hasDescendant()` that does not depend on C++. |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
DeclarationMatcher ZDescendantClassX = |
| 335 |
recordDecl(hasDescendant(recordDecl(hasName("X"))), hasName("Z")); |
| 336 |
EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX)); |
| 337 |
EXPECT_TRUE( |
| 338 |
matches("class Z { class Y { class X {}; }; };", ZDescendantClassX)); |
| 339 |
EXPECT_TRUE(matches("class Z { class A { class Y { class X {}; }; }; };", |
| 340 |
ZDescendantClassX)); |
| 341 |
EXPECT_TRUE( |
| 342 |
matches("class Z { class A { class B { class Y { class X {}; }; }; }; };", |
| 343 |
ZDescendantClassX)); |
| 344 |
EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX)); |
| 345 |
|
| 346 |
DeclarationMatcher ZDescendantClassXHasClassY = recordDecl( |
| 347 |
hasDescendant(recordDecl(has(recordDecl(hasName("Y"))), hasName("X"))), |
| 348 |
hasName("Z")); |
| 349 |
EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };", |
| 350 |
ZDescendantClassXHasClassY)); |
| 351 |
EXPECT_TRUE( |
| 352 |
matches("class Z { class A { class B { class X { class Y {}; }; }; }; };", |
| 353 |
ZDescendantClassXHasClassY)); |
| 354 |
EXPECT_TRUE(notMatches("class Z {" |
| 355 |
" class A {" |
| 356 |
" class B {" |
| 357 |
" class X {" |
| 358 |
" class C {" |
| 359 |
" class Y {};" |
| 360 |
" };" |
| 361 |
" };" |
| 362 |
" }; " |
| 363 |
" };" |
| 364 |
"};", |
| 365 |
ZDescendantClassXHasClassY)); |
| 366 |
|
| 367 |
DeclarationMatcher ZDescendantClassXDescendantClassY = |
| 368 |
recordDecl(hasDescendant(recordDecl( |
| 369 |
hasDescendant(recordDecl(hasName("Y"))), hasName("X"))), |
| 370 |
hasName("Z")); |
| 371 |
EXPECT_TRUE( |
| 372 |
matches("class Z { class A { class X { class B { class Y {}; }; }; }; };", |
| 373 |
ZDescendantClassXDescendantClassY)); |
| 374 |
EXPECT_TRUE(matches("class Z {" |
| 375 |
" class A {" |
| 376 |
" class X {" |
| 377 |
" class B {" |
| 378 |
" class Y {};" |
| 379 |
" };" |
| 380 |
" class Y {};" |
| 381 |
" };" |
| 382 |
" };" |
| 383 |
"};", |
| 384 |
ZDescendantClassXDescendantClassY)); |
| 385 |
} |
| 386 |
|
| 387 |
TEST_P(ASTMatchersTest, HasDescendant_Memoization) { |
| 388 |
DeclarationMatcher CannotMemoize = |
| 389 |
decl(hasDescendant(typeLoc().bind("x")), has(decl())); |
| 390 |
EXPECT_TRUE(matches("void f() { int i; }", CannotMemoize)); |
| 391 |
} |
| 392 |
|
| 393 |
TEST_P(ASTMatchersTest, HasDescendant_MemoizationUsesRestrictKind) { |
| 394 |
auto Name = hasName("i"); |
| 395 |
auto VD = internal::Matcher<VarDecl>(Name).dynCastTo<Decl>(); |
| 396 |
auto RD = internal::Matcher<RecordDecl>(Name).dynCastTo<Decl>(); |
| 397 |
// Matching VD first should not make a cache hit for RD. |
| 398 |
EXPECT_TRUE(notMatches("void f() { int i; }", |
| 399 |
decl(hasDescendant(VD), hasDescendant(RD)))); |
| 400 |
EXPECT_TRUE(notMatches("void f() { int i; }", |
| 401 |
decl(hasDescendant(RD), hasDescendant(VD)))); |
| 402 |
// Not matching RD first should not make a cache hit for VD either. |
| 403 |
EXPECT_TRUE(matches("void f() { int i; }", |
| 404 |
decl(anyOf(hasDescendant(RD), hasDescendant(VD))))); |
| 405 |
} |
| 406 |
|
| 407 |
TEST_P(ASTMatchersTest, HasAncestor_Memoization) { |
| 408 |
if (!GetParam().isCXX()) { |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
// This triggers an hasAncestor with a TemplateArgument in the bound nodes. |
| 413 |
// That node can't be memoized so we have to check for it before trying to put |
| 414 |
// it on the cache. |
| 415 |
DeclarationMatcher CannotMemoize = classTemplateSpecializationDecl( |
| 416 |
hasAnyTemplateArgument(templateArgument().bind("targ")), |
| 417 |
forEach(fieldDecl(hasAncestor(forStmt())))); |
| 418 |
|
| 419 |
EXPECT_TRUE(notMatches("template <typename T> struct S;" |
| 420 |
"template <> struct S<int>{ int i; int j; };", |
| 421 |
CannotMemoize)); |
| 422 |
} |
| 423 |
|
| 424 |
TEST_P(ASTMatchersTest, HasAttr) { |
| 425 |
EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};", |
| 426 |
decl(hasAttr(clang::attr::WarnUnused)))); |
| 427 |
EXPECT_FALSE(matches("struct X {};", decl(hasAttr(clang::attr::WarnUnused)))); |
| 428 |
} |
| 429 |
|
| 430 |
TEST_P(ASTMatchersTest, AnyOf) { |
| 431 |
if (!GetParam().isCXX()) { |
| 432 |
// FIXME: Add a test for `anyOf()` that does not depend on C++. |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
DeclarationMatcher YOrZDerivedFromX = cxxRecordDecl( |
| 437 |
anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z")))); |
| 438 |
EXPECT_TRUE(matches("class X {}; class Z : public X {};", YOrZDerivedFromX)); |
| 439 |
EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX)); |
| 440 |
EXPECT_TRUE( |
| 441 |
notMatches("class X {}; class W : public X {};", YOrZDerivedFromX)); |
| 442 |
EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX)); |
| 443 |
|
| 444 |
DeclarationMatcher XOrYOrZOrU = |
| 445 |
recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"))); |
| 446 |
EXPECT_TRUE(matches("class X {};", XOrYOrZOrU)); |
| 447 |
EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU)); |
| 448 |
|
| 449 |
DeclarationMatcher XOrYOrZOrUOrV = recordDecl(anyOf( |
| 450 |
hasName("X"), hasName("Y"), hasName("Z"), hasName("U"), hasName("V"))); |
| 451 |
EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV)); |
| 452 |
EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV)); |
| 453 |
EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV)); |
| 454 |
EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); |
| 455 |
EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); |
| 456 |
EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); |
| 457 |
|
| 458 |
StatementMatcher MixedTypes = stmt(anyOf(ifStmt(), binaryOperator())); |
| 459 |
EXPECT_TRUE(matches("int F() { return 1 + 2; }", MixedTypes)); |
| 460 |
EXPECT_TRUE(matches("int F() { if (true) return 1; }", MixedTypes)); |
| 461 |
EXPECT_TRUE(notMatches("int F() { return 1; }", MixedTypes)); |
| 462 |
|
| 463 |
EXPECT_TRUE( |
| 464 |
matches("void f() try { } catch (int) { } catch (...) { }", |
| 465 |
cxxCatchStmt(anyOf(hasDescendant(varDecl()), isCatchAll())))); |
| 466 |
} |
| 467 |
|
| 468 |
TEST_P(ASTMatchersTest, MapAnyOf) { |
| 469 |
if (!GetParam().isCXX()) { |
| 470 |
return; |
| 471 |
} |
| 472 |
|
| 473 |
if (GetParam().hasDelayedTemplateParsing()) { |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
StringRef Code = R"cpp( |
| 478 |
void F() { |
| 479 |
if (true) {} |
| 480 |
for ( ; false; ) {} |
| 481 |
} |
| 482 |
)cpp"; |
| 483 |
|
| 484 |
auto trueExpr = cxxBoolLiteral(equals(true)); |
| 485 |
auto falseExpr = cxxBoolLiteral(equals(false)); |
| 486 |
|
| 487 |
EXPECT_TRUE(matches( |
| 488 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 489 |
mapAnyOf(ifStmt, forStmt).with(hasCondition(trueExpr))))); |
| 490 |
EXPECT_TRUE(matches( |
| 491 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 492 |
mapAnyOf(ifStmt, forStmt).with(hasCondition(falseExpr))))); |
| 493 |
|
| 494 |
EXPECT_TRUE( |
| 495 |
matches(Code, cxxBoolLiteral(equals(true), |
| 496 |
hasAncestor(mapAnyOf(ifStmt, forStmt))))); |
| 497 |
|
| 498 |
EXPECT_TRUE( |
| 499 |
matches(Code, cxxBoolLiteral(equals(false), |
| 500 |
hasAncestor(mapAnyOf(ifStmt, forStmt))))); |
| 501 |
|
| 502 |
EXPECT_TRUE( |
| 503 |
notMatches(Code, floatLiteral(hasAncestor(mapAnyOf(ifStmt, forStmt))))); |
| 504 |
|
| 505 |
Code = R"cpp( |
| 506 |
void func(bool b) {} |
| 507 |
struct S { |
| 508 |
S(bool b) {} |
| 509 |
}; |
| 510 |
void F() { |
| 511 |
func(false); |
| 512 |
S s(true); |
| 513 |
} |
| 514 |
)cpp"; |
| 515 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 516 |
mapAnyOf(callExpr, cxxConstructExpr) |
| 517 |
.with(hasArgument(0, trueExpr))))); |
| 518 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 519 |
mapAnyOf(callExpr, cxxConstructExpr) |
| 520 |
.with(hasArgument(0, falseExpr))))); |
| 521 |
|
| 522 |
EXPECT_TRUE( |
| 523 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 524 |
mapAnyOf(callExpr, cxxConstructExpr) |
| 525 |
.with(hasArgument(0, expr()), |
| 526 |
hasDeclaration(functionDecl()))))); |
| 527 |
|
| 528 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 529 |
mapAnyOf(callExpr, cxxConstructExpr)))); |
| 530 |
|
| 531 |
EXPECT_TRUE(matches( |
| 532 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 533 |
mapAnyOf(callExpr, cxxConstructExpr).bind("call")))); |
| 534 |
|
| 535 |
Code = R"cpp( |
| 536 |
struct HasOpNeqMem |
| 537 |
{ |
| 538 |
bool operator!=(const HasOpNeqMem& other) const |
| 539 |
{ |
| 540 |
return true; |
| 541 |
} |
| 542 |
}; |
| 543 |
struct HasOpFree |
| 544 |
{ |
| 545 |
}; |
| 546 |
bool operator!=(const HasOpFree& lhs, const HasOpFree& rhs) |
| 547 |
{ |
| 548 |
return true; |
| 549 |
} |
| 550 |
|
| 551 |
void binop() |
| 552 |
{ |
| 553 |
int s1; |
| 554 |
int s2; |
| 555 |
if (s1 != s2) |
| 556 |
return; |
| 557 |
} |
| 558 |
|
| 559 |
void opMem() |
| 560 |
{ |
| 561 |
HasOpNeqMem s1; |
| 562 |
HasOpNeqMem s2; |
| 563 |
if (s1 != s2) |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
void opFree() |
| 568 |
{ |
| 569 |
HasOpFree s1; |
| 570 |
HasOpFree s2; |
| 571 |
if (s1 != s2) |
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
template<typename T> |
| 576 |
void templ() |
| 577 |
{ |
| 578 |
T s1; |
| 579 |
T s2; |
| 580 |
if (s1 != s2) |
| 581 |
return; |
| 582 |
} |
| 583 |
)cpp"; |
| 584 |
|
| 585 |
EXPECT_TRUE(matches( |
| 586 |
Code, |
| 587 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 588 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 589 |
.with(hasOperatorName("!="), |
| 590 |
forFunction(functionDecl(hasName("binop"))), |
| 591 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 592 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 593 |
|
| 594 |
EXPECT_TRUE(matches( |
| 595 |
Code, |
| 596 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 597 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 598 |
.with(hasOperatorName("!="), |
| 599 |
forFunction(functionDecl(hasName("opMem"))), |
| 600 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 601 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 602 |
|
| 603 |
EXPECT_TRUE(matches( |
| 604 |
Code, |
| 605 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 606 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 607 |
.with(hasOperatorName("!="), |
| 608 |
forFunction(functionDecl(hasName("opFree"))), |
| 609 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 610 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 611 |
|
| 612 |
EXPECT_TRUE(matches( |
| 613 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 614 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 615 |
.with(hasOperatorName("!="), |
| 616 |
forFunction(functionDecl(hasName("binop"))), |
| 617 |
hasEitherOperand( |
| 618 |
declRefExpr(to(varDecl(hasName("s1"))))), |
| 619 |
hasEitherOperand( |
| 620 |
declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 621 |
|
| 622 |
EXPECT_TRUE(matches( |
| 623 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 624 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 625 |
.with(hasOperatorName("!="), |
| 626 |
forFunction(functionDecl(hasName("opMem"))), |
| 627 |
hasEitherOperand( |
| 628 |
declRefExpr(to(varDecl(hasName("s1"))))), |
| 629 |
hasEitherOperand( |
| 630 |
declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 631 |
|
| 632 |
EXPECT_TRUE(matches( |
| 633 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 634 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 635 |
.with(hasOperatorName("!="), |
| 636 |
forFunction(functionDecl(hasName("opFree"))), |
| 637 |
hasEitherOperand( |
| 638 |
declRefExpr(to(varDecl(hasName("s1"))))), |
| 639 |
hasEitherOperand( |
| 640 |
declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 641 |
|
| 642 |
EXPECT_TRUE(matches( |
| 643 |
Code, |
| 644 |
traverse( |
| 645 |
TK_IgnoreUnlessSpelledInSource, |
| 646 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 647 |
.with(hasOperatorName("!="), |
| 648 |
forFunction(functionDecl(hasName("binop"))), |
| 649 |
hasOperands(declRefExpr(to(varDecl(hasName("s1")))), |
| 650 |
declRefExpr(to(varDecl(hasName("s2"))))), |
| 651 |
hasOperands(declRefExpr(to(varDecl(hasName("s2")))), |
| 652 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 653 |
|
| 654 |
EXPECT_TRUE(matches( |
| 655 |
Code, |
| 656 |
traverse( |
| 657 |
TK_IgnoreUnlessSpelledInSource, |
| 658 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 659 |
.with(hasOperatorName("!="), |
| 660 |
forFunction(functionDecl(hasName("opMem"))), |
| 661 |
hasOperands(declRefExpr(to(varDecl(hasName("s1")))), |
| 662 |
declRefExpr(to(varDecl(hasName("s2"))))), |
| 663 |
hasOperands(declRefExpr(to(varDecl(hasName("s2")))), |
| 664 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 665 |
|
| 666 |
EXPECT_TRUE(matches( |
| 667 |
Code, |
| 668 |
traverse( |
| 669 |
TK_IgnoreUnlessSpelledInSource, |
| 670 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 671 |
.with(hasOperatorName("!="), |
| 672 |
forFunction(functionDecl(hasName("opFree"))), |
| 673 |
hasOperands(declRefExpr(to(varDecl(hasName("s1")))), |
| 674 |
declRefExpr(to(varDecl(hasName("s2"))))), |
| 675 |
hasOperands(declRefExpr(to(varDecl(hasName("s2")))), |
| 676 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 677 |
|
| 678 |
EXPECT_TRUE(matches( |
| 679 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 680 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 681 |
.with(hasAnyOperatorName("==", "!="), |
| 682 |
forFunction(functionDecl(hasName("binop"))))))); |
| 683 |
|
| 684 |
EXPECT_TRUE(matches( |
| 685 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 686 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 687 |
.with(hasAnyOperatorName("==", "!="), |
| 688 |
forFunction(functionDecl(hasName("opMem"))))))); |
| 689 |
|
| 690 |
EXPECT_TRUE(matches( |
| 691 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 692 |
mapAnyOf(binaryOperator, cxxOperatorCallExpr) |
| 693 |
.with(hasAnyOperatorName("==", "!="), |
| 694 |
forFunction(functionDecl(hasName("opFree"))))))); |
| 695 |
|
| 696 |
EXPECT_TRUE(matches( |
| 697 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 698 |
binaryOperation( |
| 699 |
hasOperatorName("!="), |
| 700 |
forFunction(functionDecl(hasName("binop"))), |
| 701 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 702 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 703 |
|
| 704 |
EXPECT_TRUE(matches( |
| 705 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 706 |
binaryOperation( |
| 707 |
hasOperatorName("!="), |
| 708 |
forFunction(functionDecl(hasName("opMem"))), |
| 709 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 710 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 711 |
|
| 712 |
EXPECT_TRUE(matches( |
| 713 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 714 |
binaryOperation( |
| 715 |
hasOperatorName("!="), |
| 716 |
forFunction(functionDecl(hasName("opFree"))), |
| 717 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 718 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 719 |
|
| 720 |
EXPECT_TRUE(matches( |
| 721 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 722 |
binaryOperation( |
| 723 |
hasOperatorName("!="), |
| 724 |
forFunction(functionDecl(hasName("templ"))), |
| 725 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 726 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))))); |
| 727 |
|
| 728 |
Code = R"cpp( |
| 729 |
struct HasOpEq |
| 730 |
{ |
| 731 |
bool operator==(const HasOpEq &) const; |
| 732 |
}; |
| 733 |
|
| 734 |
void inverse() |
| 735 |
{ |
| 736 |
HasOpEq s1; |
| 737 |
HasOpEq s2; |
| 738 |
if (s1 != s2) |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
namespace std { |
| 743 |
struct strong_ordering { |
| 744 |
int n; |
| 745 |
constexpr operator int() const { return n; } |
| 746 |
static const strong_ordering equal, greater, less; |
| 747 |
}; |
| 748 |
constexpr strong_ordering strong_ordering::equal = {0}; |
| 749 |
constexpr strong_ordering strong_ordering::greater = {1}; |
| 750 |
constexpr strong_ordering strong_ordering::less = {-1}; |
| 751 |
} |
| 752 |
|
| 753 |
struct HasSpaceshipMem { |
| 754 |
int a; |
| 755 |
constexpr auto operator<=>(const HasSpaceshipMem&) const = default; |
| 756 |
}; |
| 757 |
|
| 758 |
void rewritten() |
| 759 |
{ |
| 760 |
HasSpaceshipMem s1; |
| 761 |
HasSpaceshipMem s2; |
| 762 |
if (s1 != s2) |
| 763 |
return; |
| 764 |
} |
| 765 |
)cpp"; |
| 766 |
|
| 767 |
EXPECT_TRUE(matchesConditionally( |
| 768 |
Code, |
| 769 |
traverse( |
| 770 |
TK_IgnoreUnlessSpelledInSource, |
| 771 |
binaryOperation(hasOperatorName("!="), |
| 772 |
forFunction(functionDecl(hasName("inverse"))), |
| 773 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 774 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))), |
| 775 |
true, {"-std=c++20"})); |
| 776 |
|
| 777 |
EXPECT_TRUE(matchesConditionally( |
| 778 |
Code, |
| 779 |
traverse( |
| 780 |
TK_IgnoreUnlessSpelledInSource, |
| 781 |
binaryOperation(hasOperatorName("!="), |
| 782 |
forFunction(functionDecl(hasName("rewritten"))), |
| 783 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 784 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))))), |
| 785 |
true, {"-std=c++20"})); |
| 786 |
|
| 787 |
Code = R"cpp( |
| 788 |
struct HasOpBangMem |
| 789 |
{ |
| 790 |
bool operator!() const |
| 791 |
{ |
| 792 |
return false; |
| 793 |
} |
| 794 |
}; |
| 795 |
struct HasOpBangFree |
| 796 |
{ |
| 797 |
}; |
| 798 |
bool operator!(HasOpBangFree const&) |
| 799 |
{ |
| 800 |
return false; |
| 801 |
} |
| 802 |
|
| 803 |
void unop() |
| 804 |
{ |
| 805 |
int s1; |
| 806 |
if (!s1) |
| 807 |
return; |
| 808 |
} |
| 809 |
|
| 810 |
void opMem() |
| 811 |
{ |
| 812 |
HasOpBangMem s1; |
| 813 |
if (!s1) |
| 814 |
return; |
| 815 |
} |
| 816 |
|
| 817 |
void opFree() |
| 818 |
{ |
| 819 |
HasOpBangFree s1; |
| 820 |
if (!s1) |
| 821 |
return; |
| 822 |
} |
| 823 |
)cpp"; |
| 824 |
|
| 825 |
EXPECT_TRUE(matches( |
| 826 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 827 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 828 |
.with(hasOperatorName("!"), |
| 829 |
forFunction(functionDecl(hasName("unop"))), |
| 830 |
hasUnaryOperand( |
| 831 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 832 |
|
| 833 |
EXPECT_TRUE(matches( |
| 834 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 835 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 836 |
.with(hasOperatorName("!"), |
| 837 |
forFunction(functionDecl(hasName("opMem"))), |
| 838 |
hasUnaryOperand( |
| 839 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 840 |
|
| 841 |
EXPECT_TRUE(matches( |
| 842 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 843 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 844 |
.with(hasOperatorName("!"), |
| 845 |
forFunction(functionDecl(hasName("opFree"))), |
| 846 |
hasUnaryOperand( |
| 847 |
declRefExpr(to(varDecl(hasName("s1"))))))))); |
| 848 |
|
| 849 |
EXPECT_TRUE(matches( |
| 850 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 851 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 852 |
.with(hasAnyOperatorName("+", "!"), |
| 853 |
forFunction(functionDecl(hasName("unop"))))))); |
| 854 |
|
| 855 |
EXPECT_TRUE(matches( |
| 856 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 857 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 858 |
.with(hasAnyOperatorName("+", "!"), |
| 859 |
forFunction(functionDecl(hasName("opMem"))))))); |
| 860 |
|
| 861 |
EXPECT_TRUE(matches( |
| 862 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 863 |
mapAnyOf(unaryOperator, cxxOperatorCallExpr) |
| 864 |
.with(hasAnyOperatorName("+", "!"), |
| 865 |
forFunction(functionDecl(hasName("opFree"))))))); |
| 866 |
|
| 867 |
Code = R"cpp( |
| 868 |
struct ConstructorTakesInt |
| 869 |
{ |
| 870 |
ConstructorTakesInt(int i) {} |
| 871 |
}; |
| 872 |
|
| 873 |
void callTakesInt(int i) |
| 874 |
{ |
| 875 |
|
| 876 |
} |
| 877 |
|
| 878 |
void doCall() |
| 879 |
{ |
| 880 |
callTakesInt(42); |
| 881 |
} |
| 882 |
|
| 883 |
void doConstruct() |
| 884 |
{ |
| 885 |
ConstructorTakesInt cti(42); |
| 886 |
} |
| 887 |
)cpp"; |
| 888 |
|
| 889 |
EXPECT_TRUE(matches( |
| 890 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 891 |
invocation(forFunction(functionDecl(hasName("doCall"))), |
| 892 |
hasArgument(0, integerLiteral(equals(42))), |
| 893 |
hasAnyArgument(integerLiteral(equals(42))), |
| 894 |
forEachArgumentWithParam( |
| 895 |
integerLiteral(equals(42)), |
| 896 |
parmVarDecl(hasName("i"))))))); |
| 897 |
|
| 898 |
EXPECT_TRUE(matches( |
| 899 |
Code, |
| 900 |
traverse( |
| 901 |
TK_IgnoreUnlessSpelledInSource, |
| 902 |
invocation(forFunction(functionDecl(hasName("doConstruct"))), |
| 903 |
hasArgument(0, integerLiteral(equals(42))), |
| 904 |
hasAnyArgument(integerLiteral(equals(42))), |
| 905 |
forEachArgumentWithParam(integerLiteral(equals(42)), |
| 906 |
parmVarDecl(hasName("i"))))))); |
| 907 |
} |
| 908 |
|
| 909 |
TEST_P(ASTMatchersTest, IsDerivedFrom) { |
| 910 |
if (!GetParam().isCXX()) { |
| 911 |
return; |
| 912 |
} |
| 913 |
|
| 914 |
DeclarationMatcher IsDerivedFromX = cxxRecordDecl(isDerivedFrom("X")); |
| 915 |
|
| 916 |
EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX)); |
| 917 |
EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX)); |
| 918 |
EXPECT_TRUE(notMatches("class X;", IsDerivedFromX)); |
| 919 |
EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX)); |
| 920 |
EXPECT_TRUE(notMatches("", IsDerivedFromX)); |
| 921 |
EXPECT_TRUE(matches("class X {}; template<int N> class Y : Y<N-1>, X {};", |
| 922 |
IsDerivedFromX)); |
| 923 |
EXPECT_TRUE(matches("class X {}; template<int N> class Y : X, Y<N-1> {};", |
| 924 |
IsDerivedFromX)); |
| 925 |
|
| 926 |
DeclarationMatcher IsZDerivedFromX = |
| 927 |
cxxRecordDecl(hasName("Z"), isDerivedFrom("X")); |
| 928 |
EXPECT_TRUE(matches("class X {};" |
| 929 |
"template<int N> class Y : Y<N-1> {};" |
| 930 |
"template<> class Y<0> : X {};" |
| 931 |
"class Z : Y<1> {};", |
| 932 |
IsZDerivedFromX)); |
| 933 |
|
| 934 |
DeclarationMatcher IsDirectlyDerivedFromX = |
| 935 |
cxxRecordDecl(isDirectlyDerivedFrom("X")); |
| 936 |
|
| 937 |
EXPECT_TRUE( |
| 938 |
matches("class X {}; class Y : public X {};", IsDirectlyDerivedFromX)); |
| 939 |
EXPECT_TRUE(notMatches("class X {};", IsDirectlyDerivedFromX)); |
| 940 |
EXPECT_TRUE(notMatches("class X;", IsDirectlyDerivedFromX)); |
| 941 |
EXPECT_TRUE(notMatches("class Y;", IsDirectlyDerivedFromX)); |
| 942 |
EXPECT_TRUE(notMatches("", IsDirectlyDerivedFromX)); |
| 943 |
|
| 944 |
DeclarationMatcher IsAX = cxxRecordDecl(isSameOrDerivedFrom("X")); |
| 945 |
|
| 946 |
EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX)); |
| 947 |
EXPECT_TRUE(matches("class X {};", IsAX)); |
| 948 |
EXPECT_TRUE(matches("class X;", IsAX)); |
| 949 |
EXPECT_TRUE(notMatches("class Y;", IsAX)); |
| 950 |
EXPECT_TRUE(notMatches("", IsAX)); |
| 951 |
|
| 952 |
DeclarationMatcher ZIsDerivedFromX = |
| 953 |
cxxRecordDecl(hasName("Z"), isDerivedFrom("X")); |
| 954 |
DeclarationMatcher ZIsDirectlyDerivedFromX = |
| 955 |
cxxRecordDecl(hasName("Z"), isDirectlyDerivedFrom("X")); |
| 956 |
EXPECT_TRUE( |
| 957 |
matches("class X {}; class Y : public X {}; class Z : public Y {};", |
| 958 |
ZIsDerivedFromX)); |
| 959 |
EXPECT_TRUE( |
| 960 |
notMatches("class X {}; class Y : public X {}; class Z : public Y {};", |
| 961 |
ZIsDirectlyDerivedFromX)); |
| 962 |
EXPECT_TRUE(matches("class X {};" |
| 963 |
"template<class T> class Y : public X {};" |
| 964 |
"class Z : public Y<int> {};", |
| 965 |
ZIsDerivedFromX)); |
| 966 |
EXPECT_TRUE(notMatches("class X {};" |
| 967 |
"template<class T> class Y : public X {};" |
| 968 |
"class Z : public Y<int> {};", |
| 969 |
ZIsDirectlyDerivedFromX)); |
| 970 |
EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};", |
| 971 |
ZIsDerivedFromX)); |
| 972 |
EXPECT_TRUE(matches("template<class T> class X {}; " |
| 973 |
"template<class T> class Z : public X<T> {};", |
| 974 |
ZIsDerivedFromX)); |
| 975 |
EXPECT_TRUE(matches("template<class T, class U=T> class X {}; " |
| 976 |
"template<class T> class Z : public X<T> {};", |
| 977 |
ZIsDerivedFromX)); |
| 978 |
EXPECT_TRUE( |
| 979 |
notMatches("template<class X> class A { class Z : public X {}; };", |
| 980 |
ZIsDerivedFromX)); |
| 981 |
EXPECT_TRUE( |
| 982 |
matches("template<class X> class A { public: class Z : public X {}; }; " |
| 983 |
"class X{}; void y() { A<X>::Z z; }", |
| 984 |
ZIsDerivedFromX)); |
| 985 |
EXPECT_TRUE( |
| 986 |
matches("template <class T> class X {}; " |
| 987 |
"template<class Y> class A { class Z : public X<Y> {}; };", |
| 988 |
ZIsDerivedFromX)); |
| 989 |
EXPECT_TRUE(notMatches("template<template<class T> class X> class A { " |
| 990 |
" class Z : public X<int> {}; };", |
| 991 |
ZIsDerivedFromX)); |
| 992 |
EXPECT_TRUE(matches("template<template<class T> class X> class A { " |
| 993 |
" public: class Z : public X<int> {}; }; " |
| 994 |
"template<class T> class X {}; void y() { A<X>::Z z; }", |
| 995 |
ZIsDerivedFromX)); |
| 996 |
EXPECT_TRUE( |
| 997 |
notMatches("template<class X> class A { class Z : public X::D {}; };", |
| 998 |
ZIsDerivedFromX)); |
| 999 |
EXPECT_TRUE(matches("template<class X> class A { public: " |
| 1000 |
" class Z : public X::D {}; }; " |
| 1001 |
"class Y { public: class X {}; typedef X D; }; " |
| 1002 |
"void y() { A<Y>::Z z; }", |
| 1003 |
ZIsDerivedFromX)); |
| 1004 |
EXPECT_TRUE(matches("class X {}; typedef X Y; class Z : public Y {};", |
| 1005 |
ZIsDerivedFromX)); |
| 1006 |
EXPECT_TRUE(matches("template<class T> class Y { typedef typename T::U X; " |
| 1007 |
" class Z : public X {}; };", |
| 1008 |
ZIsDerivedFromX)); |
| 1009 |
EXPECT_TRUE(matches("class X {}; class Z : public ::X {};", ZIsDerivedFromX)); |
| 1010 |
EXPECT_TRUE( |
| 1011 |
notMatches("template<class T> class X {}; " |
| 1012 |
"template<class T> class A { class Z : public X<T>::D {}; };", |
| 1013 |
ZIsDerivedFromX)); |
| 1014 |
EXPECT_TRUE( |
| 1015 |
matches("template<class T> class X { public: typedef X<T> D; }; " |
| 1016 |
"template<class T> class A { public: " |
| 1017 |
" class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }", |
| 1018 |
ZIsDerivedFromX)); |
| 1019 |
EXPECT_TRUE( |
| 1020 |
notMatches("template<class X> class A { class Z : public X::D::E {}; };", |
| 1021 |
ZIsDerivedFromX)); |
| 1022 |
EXPECT_TRUE( |
| 1023 |
matches("class X {}; typedef X V; typedef V W; class Z : public W {};", |
| 1024 |
ZIsDerivedFromX)); |
| 1025 |
EXPECT_TRUE(matches("class X {}; class Y : public X {}; " |
| 1026 |
"typedef Y V; typedef V W; class Z : public W {};", |
| 1027 |
ZIsDerivedFromX)); |
| 1028 |
EXPECT_TRUE(notMatches("class X {}; class Y : public X {}; " |
| 1029 |
"typedef Y V; typedef V W; class Z : public W {};", |
| 1030 |
ZIsDirectlyDerivedFromX)); |
| 1031 |
EXPECT_TRUE( |
| 1032 |
matches("template<class T, class U> class X {}; " |
| 1033 |
"template<class T> class A { class Z : public X<T, int> {}; };", |
| 1034 |
ZIsDerivedFromX)); |
| 1035 |
EXPECT_TRUE( |
| 1036 |
notMatches("template<class X> class D { typedef X A; typedef A B; " |
| 1037 |
" typedef B C; class Z : public C {}; };", |
| 1038 |
ZIsDerivedFromX)); |
| 1039 |
EXPECT_TRUE(matches("class X {}; typedef X A; typedef A B; " |
| 1040 |
"class Z : public B {};", |
| 1041 |
ZIsDerivedFromX)); |
| 1042 |
EXPECT_TRUE(matches("class X {}; typedef X A; typedef A B; typedef B C; " |
| 1043 |
"class Z : public C {};", |
| 1044 |
ZIsDerivedFromX)); |
| 1045 |
EXPECT_TRUE(matches("class U {}; typedef U X; typedef X V; " |
| 1046 |
"class Z : public V {};", |
| 1047 |
ZIsDerivedFromX)); |
| 1048 |
EXPECT_TRUE(matches("class Base {}; typedef Base X; " |
| 1049 |
"class Z : public Base {};", |
| 1050 |
ZIsDerivedFromX)); |
| 1051 |
EXPECT_TRUE(matches("class Base {}; typedef Base Base2; typedef Base2 X; " |
| 1052 |
"class Z : public Base {};", |
| 1053 |
ZIsDerivedFromX)); |
| 1054 |
EXPECT_TRUE(notMatches("class Base {}; class Base2 {}; typedef Base2 X; " |
| 1055 |
"class Z : public Base {};", |
| 1056 |
ZIsDerivedFromX)); |
| 1057 |
EXPECT_TRUE(matches("class A {}; typedef A X; typedef A Y; " |
| 1058 |
"class Z : public Y {};", |
| 1059 |
ZIsDerivedFromX)); |
| 1060 |
EXPECT_TRUE(notMatches("template <typename T> class Z;" |
| 1061 |
"template <> class Z<void> {};" |
| 1062 |
"template <typename T> class Z : public Z<void> {};", |
| 1063 |
IsDerivedFromX)); |
| 1064 |
EXPECT_TRUE(matches("template <typename T> class X;" |
| 1065 |
"template <> class X<void> {};" |
| 1066 |
"template <typename T> class X : public X<void> {};", |
| 1067 |
IsDerivedFromX)); |
| 1068 |
EXPECT_TRUE( |
| 1069 |
matches("class X {};" |
| 1070 |
"template <typename T> class Z;" |
| 1071 |
"template <> class Z<void> {};" |
| 1072 |
"template <typename T> class Z : public Z<void>, public X {};", |
| 1073 |
ZIsDerivedFromX)); |
| 1074 |
EXPECT_TRUE( |
| 1075 |
notMatches("template<int> struct X;" |
| 1076 |
"template<int i> struct X : public X<i-1> {};", |
| 1077 |
cxxRecordDecl(isDerivedFrom(recordDecl(hasName("Some")))))); |
| 1078 |
EXPECT_TRUE(matches( |
| 1079 |
"struct A {};" |
| 1080 |
"template<int> struct X;" |
| 1081 |
"template<int i> struct X : public X<i-1> {};" |
| 1082 |
"template<> struct X<0> : public A {};" |
| 1083 |
"struct B : public X<42> {};", |
| 1084 |
cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A")))))); |
| 1085 |
EXPECT_TRUE(notMatches( |
| 1086 |
"struct A {};" |
| 1087 |
"template<int> struct X;" |
| 1088 |
"template<int i> struct X : public X<i-1> {};" |
| 1089 |
"template<> struct X<0> : public A {};" |
| 1090 |
"struct B : public X<42> {};", |
| 1091 |
cxxRecordDecl(hasName("B"), |
| 1092 |
isDirectlyDerivedFrom(recordDecl(hasName("A")))))); |
| 1093 |
|
| 1094 |
// FIXME: Once we have better matchers for template type matching, |
| 1095 |
// get rid of the Variable(...) matching and match the right template |
| 1096 |
// declarations directly. |
| 1097 |
const char *RecursiveTemplateOneParameter = |
| 1098 |
"class Base1 {}; class Base2 {};" |
| 1099 |
"template <typename T> class Z;" |
| 1100 |
"template <> class Z<void> : public Base1 {};" |
| 1101 |
"template <> class Z<int> : public Base2 {};" |
| 1102 |
"template <> class Z<float> : public Z<void> {};" |
| 1103 |
"template <> class Z<double> : public Z<int> {};" |
| 1104 |
"template <typename T> class Z : public Z<float>, public Z<double> {};" |
| 1105 |
"void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }"; |
| 1106 |
EXPECT_TRUE(matches( |
| 1107 |
RecursiveTemplateOneParameter, |
| 1108 |
varDecl(hasName("z_float"), |
| 1109 |
hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1"))))))); |
| 1110 |
EXPECT_TRUE(notMatches( |
| 1111 |
RecursiveTemplateOneParameter, |
| 1112 |
varDecl(hasName("z_float"), |
| 1113 |
hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base2"))))))); |
| 1114 |
EXPECT_TRUE( |
| 1115 |
matches(RecursiveTemplateOneParameter, |
| 1116 |
varDecl(hasName("z_char"), |
| 1117 |
hasInitializer(hasType(cxxRecordDecl( |
| 1118 |
isDerivedFrom("Base1"), isDerivedFrom("Base2"))))))); |
| 1119 |
|
| 1120 |
const char *RecursiveTemplateTwoParameters = |
| 1121 |
"class Base1 {}; class Base2 {};" |
| 1122 |
"template <typename T1, typename T2> class Z;" |
| 1123 |
"template <typename T> class Z<void, T> : public Base1 {};" |
| 1124 |
"template <typename T> class Z<int, T> : public Base2 {};" |
| 1125 |
"template <typename T> class Z<float, T> : public Z<void, T> {};" |
| 1126 |
"template <typename T> class Z<double, T> : public Z<int, T> {};" |
| 1127 |
"template <typename T1, typename T2> class Z : " |
| 1128 |
" public Z<float, T2>, public Z<double, T2> {};" |
| 1129 |
"void f() { Z<float, void> z_float; Z<double, void> z_double; " |
| 1130 |
" Z<char, void> z_char; }"; |
| 1131 |
EXPECT_TRUE(matches( |
| 1132 |
RecursiveTemplateTwoParameters, |
| 1133 |
varDecl(hasName("z_float"), |
| 1134 |
hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base1"))))))); |
| 1135 |
EXPECT_TRUE(notMatches( |
| 1136 |
RecursiveTemplateTwoParameters, |
| 1137 |
varDecl(hasName("z_float"), |
| 1138 |
hasInitializer(hasType(cxxRecordDecl(isDerivedFrom("Base2"))))))); |
| 1139 |
EXPECT_TRUE( |
| 1140 |
matches(RecursiveTemplateTwoParameters, |
| 1141 |
varDecl(hasName("z_char"), |
| 1142 |
hasInitializer(hasType(cxxRecordDecl( |
| 1143 |
isDerivedFrom("Base1"), isDerivedFrom("Base2"))))))); |
| 1144 |
EXPECT_TRUE(matches("namespace ns { class X {}; class Y : public X {}; }", |
| 1145 |
cxxRecordDecl(isDerivedFrom("::ns::X")))); |
| 1146 |
EXPECT_TRUE(notMatches("class X {}; class Y : public X {};", |
| 1147 |
cxxRecordDecl(isDerivedFrom("::ns::X")))); |
| 1148 |
|
| 1149 |
EXPECT_TRUE(matches( |
| 1150 |
"class X {}; class Y : public X {};", |
| 1151 |
cxxRecordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test"))))); |
| 1152 |
|
| 1153 |
EXPECT_TRUE(matches("template<typename T> class X {};" |
| 1154 |
"template<typename T> using Z = X<T>;" |
| 1155 |
"template <typename T> class Y : Z<T> {};", |
| 1156 |
cxxRecordDecl(isDerivedFrom(namedDecl(hasName("X")))))); |
| 1157 |
} |
| 1158 |
|
| 1159 |
TEST_P(ASTMatchersTest, IsDerivedFrom_EmptyName) { |
| 1160 |
if (!GetParam().isCXX()) { |
| 1161 |
return; |
| 1162 |
} |
| 1163 |
|
| 1164 |
const char *const Code = "class X {}; class Y : public X {};"; |
| 1165 |
EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isDerivedFrom("")))); |
| 1166 |
EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isDirectlyDerivedFrom("")))); |
| 1167 |
EXPECT_TRUE(notMatches(Code, cxxRecordDecl(isSameOrDerivedFrom("")))); |
| 1168 |
} |
| 1169 |
|
| 1170 |
TEST_P(ASTMatchersTest, IsDerivedFrom_ObjC) { |
| 1171 |
DeclarationMatcher IsDerivedFromX = objcInterfaceDecl(isDerivedFrom("X")); |
| 1172 |
EXPECT_TRUE( |
| 1173 |
matchesObjC("@interface X @end @interface Y : X @end", IsDerivedFromX)); |
| 1174 |
EXPECT_TRUE(matchesObjC( |
| 1175 |
"@interface X @end @interface Y<__covariant ObjectType> : X @end", |
| 1176 |
IsDerivedFromX)); |
| 1177 |
EXPECT_TRUE(matchesObjC( |
| 1178 |
"@interface X @end @compatibility_alias Y X; @interface Z : Y @end", |
| 1179 |
IsDerivedFromX)); |
| 1180 |
EXPECT_TRUE(matchesObjC( |
| 1181 |
"@interface X @end typedef X Y; @interface Z : Y @end", IsDerivedFromX)); |
| 1182 |
EXPECT_TRUE(notMatchesObjC("@interface X @end", IsDerivedFromX)); |
| 1183 |
EXPECT_TRUE(notMatchesObjC("@class X;", IsDerivedFromX)); |
| 1184 |
EXPECT_TRUE(notMatchesObjC("@class Y;", IsDerivedFromX)); |
| 1185 |
EXPECT_TRUE(notMatchesObjC("@interface X @end @compatibility_alias Y X;", |
| 1186 |
IsDerivedFromX)); |
| 1187 |
EXPECT_TRUE(notMatchesObjC("@interface X @end typedef X Y;", IsDerivedFromX)); |
| 1188 |
|
| 1189 |
DeclarationMatcher IsDirectlyDerivedFromX = |
| 1190 |
objcInterfaceDecl(isDirectlyDerivedFrom("X")); |
| 1191 |
EXPECT_TRUE(matchesObjC("@interface X @end @interface Y : X @end", |
| 1192 |
IsDirectlyDerivedFromX)); |
| 1193 |
EXPECT_TRUE(matchesObjC( |
| 1194 |
"@interface X @end @interface Y<__covariant ObjectType> : X @end", |
| 1195 |
IsDirectlyDerivedFromX)); |
| 1196 |
EXPECT_TRUE(matchesObjC( |
| 1197 |
"@interface X @end @compatibility_alias Y X; @interface Z : Y @end", |
| 1198 |
IsDirectlyDerivedFromX)); |
| 1199 |
EXPECT_TRUE( |
| 1200 |
matchesObjC("@interface X @end typedef X Y; @interface Z : Y @end", |
| 1201 |
IsDirectlyDerivedFromX)); |
| 1202 |
EXPECT_TRUE(notMatchesObjC("@interface X @end", IsDirectlyDerivedFromX)); |
| 1203 |
EXPECT_TRUE(notMatchesObjC("@class X;", IsDirectlyDerivedFromX)); |
| 1204 |
EXPECT_TRUE(notMatchesObjC("@class Y;", IsDirectlyDerivedFromX)); |
| 1205 |
EXPECT_TRUE(notMatchesObjC("@interface X @end @compatibility_alias Y X;", |
| 1206 |
IsDirectlyDerivedFromX)); |
| 1207 |
EXPECT_TRUE( |
| 1208 |
notMatchesObjC("@interface X @end typedef X Y;", IsDirectlyDerivedFromX)); |
| 1209 |
|
| 1210 |
DeclarationMatcher IsAX = objcInterfaceDecl(isSameOrDerivedFrom("X")); |
| 1211 |
EXPECT_TRUE(matchesObjC("@interface X @end @interface Y : X @end", IsAX)); |
| 1212 |
EXPECT_TRUE(matchesObjC("@interface X @end", IsAX)); |
| 1213 |
EXPECT_TRUE(matchesObjC("@class X;", IsAX)); |
| 1214 |
EXPECT_TRUE(notMatchesObjC("@interface Y @end", IsAX)); |
| 1215 |
EXPECT_TRUE(notMatchesObjC("@class Y;", IsAX)); |
| 1216 |
|
| 1217 |
DeclarationMatcher ZIsDerivedFromX = |
| 1218 |
objcInterfaceDecl(hasName("Z"), isDerivedFrom("X")); |
| 1219 |
DeclarationMatcher ZIsDirectlyDerivedFromX = |
| 1220 |
objcInterfaceDecl(hasName("Z"), isDirectlyDerivedFrom("X")); |
| 1221 |
EXPECT_TRUE(matchesObjC( |
| 1222 |
"@interface X @end @interface Y : X @end @interface Z : Y @end", |
| 1223 |
ZIsDerivedFromX)); |
| 1224 |
EXPECT_TRUE(matchesObjC("@interface X @end @interface Y : X @end typedef Y " |
| 1225 |
"V; typedef V W; @interface Z : W @end", |
| 1226 |
ZIsDerivedFromX)); |
| 1227 |
EXPECT_TRUE(matchesObjC( |
| 1228 |
"@interface X @end typedef X Y; @interface Z : Y @end", ZIsDerivedFromX)); |
| 1229 |
EXPECT_TRUE( |
| 1230 |
matchesObjC("@interface X @end typedef X Y; @interface Z : Y @end", |
| 1231 |
ZIsDirectlyDerivedFromX)); |
| 1232 |
EXPECT_TRUE(matchesObjC( |
| 1233 |
"@interface A @end typedef A X; typedef A Y; @interface Z : Y @end", |
| 1234 |
ZIsDerivedFromX)); |
| 1235 |
EXPECT_TRUE(matchesObjC( |
| 1236 |
"@interface A @end typedef A X; typedef A Y; @interface Z : Y @end", |
| 1237 |
ZIsDirectlyDerivedFromX)); |
| 1238 |
EXPECT_TRUE(matchesObjC( |
| 1239 |
"@interface X @end @compatibility_alias Y X; @interface Z : Y @end", |
| 1240 |
ZIsDerivedFromX)); |
| 1241 |
EXPECT_TRUE(matchesObjC( |
| 1242 |
"@interface X @end @compatibility_alias Y X; @interface Z : Y @end", |
| 1243 |
ZIsDirectlyDerivedFromX)); |
| 1244 |
EXPECT_TRUE(matchesObjC( |
| 1245 |
"@interface Y @end @compatibility_alias X Y; @interface Z : Y @end", |
| 1246 |
ZIsDerivedFromX)); |
| 1247 |
EXPECT_TRUE(matchesObjC( |
| 1248 |
"@interface Y @end @compatibility_alias X Y; @interface Z : Y @end", |
| 1249 |
ZIsDirectlyDerivedFromX)); |
| 1250 |
EXPECT_TRUE(matchesObjC( |
| 1251 |
"@interface A @end @compatibility_alias X A; @compatibility_alias Y A;" |
| 1252 |
"@interface Z : Y @end", |
| 1253 |
ZIsDerivedFromX)); |
| 1254 |
EXPECT_TRUE(matchesObjC( |
| 1255 |
"@interface A @end @compatibility_alias X A; @compatibility_alias Y A;" |
| 1256 |
"@interface Z : Y @end", |
| 1257 |
ZIsDirectlyDerivedFromX)); |
| 1258 |
EXPECT_TRUE(matchesObjC( |
| 1259 |
"@interface Y @end typedef Y X; @interface Z : X @end", ZIsDerivedFromX)); |
| 1260 |
EXPECT_TRUE( |
| 1261 |
matchesObjC("@interface Y @end typedef Y X; @interface Z : X @end", |
| 1262 |
ZIsDirectlyDerivedFromX)); |
| 1263 |
EXPECT_TRUE( |
| 1264 |
matchesObjC("@interface A @end @compatibility_alias Y A; typedef Y X;" |
| 1265 |
"@interface Z : A @end", |
| 1266 |
ZIsDerivedFromX)); |
| 1267 |
EXPECT_TRUE( |
| 1268 |
matchesObjC("@interface A @end @compatibility_alias Y A; typedef Y X;" |
| 1269 |
"@interface Z : A @end", |
| 1270 |
ZIsDirectlyDerivedFromX)); |
| 1271 |
EXPECT_TRUE( |
| 1272 |
matchesObjC("@interface A @end typedef A Y; @compatibility_alias X Y;" |
| 1273 |
"@interface Z : A @end", |
| 1274 |
ZIsDerivedFromX)); |
| 1275 |
EXPECT_TRUE( |
| 1276 |
matchesObjC("@interface A @end typedef A Y; @compatibility_alias X Y;" |
| 1277 |
"@interface Z : A @end", |
| 1278 |
ZIsDirectlyDerivedFromX)); |
| 1279 |
} |
| 1280 |
|
| 1281 |
TEST_P(ASTMatchersTest, IsLambda) { |
| 1282 |
if (!GetParam().isCXX11OrLater()) { |
| 1283 |
return; |
| 1284 |
} |
| 1285 |
|
| 1286 |
const auto IsLambda = cxxMethodDecl(ofClass(cxxRecordDecl(isLambda()))); |
| 1287 |
EXPECT_TRUE(matches("auto x = []{};", IsLambda)); |
| 1288 |
EXPECT_TRUE(notMatches("struct S { void operator()() const; };", IsLambda)); |
| 1289 |
} |
| 1290 |
|
| 1291 |
TEST_P(ASTMatchersTest, Bind) { |
| 1292 |
DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x")); |
| 1293 |
|
| 1294 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1295 |
"class X {};", ClassX, |
| 1296 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("x"))); |
| 1297 |
|
| 1298 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 1299 |
"class X {};", ClassX, |
| 1300 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("other-id"))); |
| 1301 |
|
| 1302 |
TypeMatcher TypeAHasClassB = hasDeclaration( |
| 1303 |
recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b")))); |
| 1304 |
|
| 1305 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1306 |
"class A { public: A *a; class B {}; };", TypeAHasClassB, |
| 1307 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("b"))); |
| 1308 |
|
| 1309 |
StatementMatcher MethodX = |
| 1310 |
callExpr(callee(cxxMethodDecl(hasName("x")))).bind("x"); |
| 1311 |
|
| 1312 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1313 |
"class A { void x() { x(); } };", MethodX, |
| 1314 |
std::make_unique<VerifyIdIsBoundTo<CXXMemberCallExpr>>("x"))); |
| 1315 |
} |
| 1316 |
|
| 1317 |
TEST_P(ASTMatchersTest, Bind_SameNameInAlternatives) { |
| 1318 |
StatementMatcher matcher = anyOf( |
| 1319 |
binaryOperator(hasOperatorName("+"), hasLHS(expr().bind("x")), |
| 1320 |
hasRHS(integerLiteral(equals(0)))), |
| 1321 |
binaryOperator(hasOperatorName("+"), hasLHS(integerLiteral(equals(0))), |
| 1322 |
hasRHS(expr().bind("x")))); |
| 1323 |
|
| 1324 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1325 |
// The first branch of the matcher binds x to 0 but then fails. |
| 1326 |
// The second branch binds x to f() and succeeds. |
| 1327 |
"int f() { return 0 + f(); }", matcher, |
| 1328 |
std::make_unique<VerifyIdIsBoundTo<CallExpr>>("x"))); |
| 1329 |
} |
| 1330 |
|
| 1331 |
TEST_P(ASTMatchersTest, Bind_BindsIDForMemoizedResults) { |
| 1332 |
// Using the same matcher in two match expressions will make memoization |
| 1333 |
// kick in. |
| 1334 |
DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x"); |
| 1335 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1336 |
"class A { class B { class X {}; }; };", |
| 1337 |
DeclarationMatcher( |
| 1338 |
anyOf(recordDecl(hasName("A"), hasDescendant(ClassX)), |
| 1339 |
recordDecl(hasName("B"), hasDescendant(ClassX)))), |
| 1340 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 2))); |
| 1341 |
} |
| 1342 |
|
| 1343 |
TEST_P(ASTMatchersTest, HasType_MatchesAsString) { |
| 1344 |
if (!GetParam().isCXX()) { |
| 1345 |
// FIXME: Add a test for `hasType()` that does not depend on C++. |
| 1346 |
return; |
| 1347 |
} |
| 1348 |
|
| 1349 |
EXPECT_TRUE( |
| 1350 |
matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }", |
| 1351 |
cxxMemberCallExpr(on(hasType(asString("Y *")))))); |
| 1352 |
EXPECT_TRUE( |
| 1353 |
matches("class X { void x(int x) {} };", |
| 1354 |
cxxMethodDecl(hasParameter(0, hasType(asString("int")))))); |
| 1355 |
EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };", |
| 1356 |
fieldDecl(hasType(asString("ns::A"))))); |
| 1357 |
EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };", |
| 1358 |
fieldDecl(hasType(asString("A"))))); |
| 1359 |
} |
| 1360 |
|
| 1361 |
TEST_P(ASTMatchersTest, HasOverloadedOperatorName) { |
| 1362 |
if (!GetParam().isCXX()) { |
| 1363 |
return; |
| 1364 |
} |
| 1365 |
|
| 1366 |
StatementMatcher OpCallAndAnd = |
| 1367 |
cxxOperatorCallExpr(hasOverloadedOperatorName("&&")); |
| 1368 |
EXPECT_TRUE(matches("class Y { }; " |
| 1369 |
"bool operator&&(Y x, Y y) { return true; }; " |
| 1370 |
"Y a; Y b; bool c = a && b;", |
| 1371 |
OpCallAndAnd)); |
| 1372 |
StatementMatcher OpCallLessLess = |
| 1373 |
cxxOperatorCallExpr(hasOverloadedOperatorName("<<")); |
| 1374 |
EXPECT_TRUE(notMatches("class Y { }; " |
| 1375 |
"bool operator&&(Y x, Y y) { return true; }; " |
| 1376 |
"Y a; Y b; bool c = a && b;", |
| 1377 |
OpCallLessLess)); |
| 1378 |
StatementMatcher OpStarCall = |
| 1379 |
cxxOperatorCallExpr(hasOverloadedOperatorName("*")); |
| 1380 |
EXPECT_TRUE( |
| 1381 |
matches("class Y; int operator*(Y &); void f(Y &y) { *y; }", OpStarCall)); |
| 1382 |
DeclarationMatcher ClassWithOpStar = |
| 1383 |
cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*"))); |
| 1384 |
EXPECT_TRUE(matches("class Y { int operator*(); };", ClassWithOpStar)); |
| 1385 |
EXPECT_TRUE(notMatches("class Y { void myOperator(); };", ClassWithOpStar)); |
| 1386 |
DeclarationMatcher AnyOpStar = functionDecl(hasOverloadedOperatorName("*")); |
| 1387 |
EXPECT_TRUE(matches("class Y; int operator*(Y &);", AnyOpStar)); |
| 1388 |
EXPECT_TRUE(matches("class Y { int operator*(); };", AnyOpStar)); |
| 1389 |
DeclarationMatcher AnyAndOp = |
| 1390 |
functionDecl(hasAnyOverloadedOperatorName("&", "&&")); |
| 1391 |
EXPECT_TRUE(matches("class Y; Y operator&(Y &, Y &);", AnyAndOp)); |
| 1392 |
EXPECT_TRUE(matches("class Y; Y operator&&(Y &, Y &);", AnyAndOp)); |
| 1393 |
EXPECT_TRUE(matches("class Y { Y operator&(Y &); };", AnyAndOp)); |
| 1394 |
EXPECT_TRUE(matches("class Y { Y operator&&(Y &); };", AnyAndOp)); |
| 1395 |
} |
| 1396 |
|
| 1397 |
TEST_P(ASTMatchersTest, HasOverloadedOperatorName_MatchesNestedCalls) { |
| 1398 |
if (!GetParam().isCXX()) { |
| 1399 |
return; |
| 1400 |
} |
| 1401 |
|
| 1402 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1403 |
"class Y { }; " |
| 1404 |
"Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1405 |
"Y a; Y b; Y c; Y d = a && b && c;", |
| 1406 |
cxxOperatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"), |
| 1407 |
std::make_unique<VerifyIdIsBoundTo<CXXOperatorCallExpr>>("x", 2))); |
| 1408 |
EXPECT_TRUE(matches("class Y { }; " |
| 1409 |
"Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1410 |
"Y a; Y b; Y c; Y d = a && b && c;", |
| 1411 |
cxxOperatorCallExpr(hasParent(cxxOperatorCallExpr())))); |
| 1412 |
EXPECT_TRUE( |
| 1413 |
matches("class Y { }; " |
| 1414 |
"Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1415 |
"Y a; Y b; Y c; Y d = a && b && c;", |
| 1416 |
cxxOperatorCallExpr(hasDescendant(cxxOperatorCallExpr())))); |
| 1417 |
} |
| 1418 |
|
| 1419 |
TEST_P(ASTMatchersTest, HasLocalStorage) { |
| 1420 |
auto M = varDecl(hasName("X"), hasLocalStorage()); |
| 1421 |
EXPECT_TRUE(matches("void f() { int X; }", M)); |
| 1422 |
EXPECT_TRUE(notMatches("int X;", M)); |
| 1423 |
EXPECT_TRUE(notMatches("void f() { static int X; }", M)); |
| 1424 |
} |
| 1425 |
|
| 1426 |
TEST_P(ASTMatchersTest, HasGlobalStorage) { |
| 1427 |
auto M = varDecl(hasName("X"), hasGlobalStorage()); |
| 1428 |
EXPECT_TRUE(notMatches("void f() { int X; }", M)); |
| 1429 |
EXPECT_TRUE(matches("int X;", M)); |
| 1430 |
EXPECT_TRUE(matches("void f() { static int X; }", M)); |
| 1431 |
} |
| 1432 |
|
| 1433 |
TEST_P(ASTMatchersTest, IsStaticLocal) { |
| 1434 |
auto M = varDecl(isStaticLocal()); |
| 1435 |
EXPECT_TRUE(matches("void f() { static int X; }", M)); |
| 1436 |
EXPECT_TRUE(notMatches("static int X;", M)); |
| 1437 |
EXPECT_TRUE(notMatches("void f() { int X; }", M)); |
| 1438 |
EXPECT_TRUE(notMatches("int X;", M)); |
| 1439 |
} |
| 1440 |
|
| 1441 |
TEST_P(ASTMatchersTest, IsInitCapture) { |
| 1442 |
if (!GetParam().isCXX11OrLater()) { |
| 1443 |
return; |
| 1444 |
} |
| 1445 |
auto M = varDecl(hasName("vd"), isInitCapture()); |
| 1446 |
EXPECT_TRUE(notMatches( |
| 1447 |
"int main() { int vd = 3; auto f = [vd]() { return vd; }; }", M)); |
| 1448 |
|
| 1449 |
if (!GetParam().isCXX14OrLater()) { |
| 1450 |
return; |
| 1451 |
} |
| 1452 |
EXPECT_TRUE(matches("int main() { auto f = [vd=3]() { return vd; }; }", M)); |
| 1453 |
EXPECT_TRUE(matches( |
| 1454 |
"int main() { int x = 3; auto f = [vd=x]() { return vd; }; }", M)); |
| 1455 |
} |
| 1456 |
|
| 1457 |
TEST_P(ASTMatchersTest, StorageDuration) { |
| 1458 |
StringRef T = |
| 1459 |
"void f() { int x; static int y; } int a;static int b;extern int c;"; |
| 1460 |
|
| 1461 |
EXPECT_TRUE(matches(T, varDecl(hasName("x"), hasAutomaticStorageDuration()))); |
| 1462 |
EXPECT_TRUE( |
| 1463 |
notMatches(T, varDecl(hasName("y"), hasAutomaticStorageDuration()))); |
| 1464 |
EXPECT_TRUE( |
| 1465 |
notMatches(T, varDecl(hasName("a"), hasAutomaticStorageDuration()))); |
| 1466 |
|
| 1467 |
EXPECT_TRUE(matches(T, varDecl(hasName("y"), hasStaticStorageDuration()))); |
| 1468 |
EXPECT_TRUE(matches(T, varDecl(hasName("a"), hasStaticStorageDuration()))); |
| 1469 |
EXPECT_TRUE(matches(T, varDecl(hasName("b"), hasStaticStorageDuration()))); |
| 1470 |
EXPECT_TRUE(matches(T, varDecl(hasName("c"), hasStaticStorageDuration()))); |
| 1471 |
EXPECT_TRUE(notMatches(T, varDecl(hasName("x"), hasStaticStorageDuration()))); |
| 1472 |
|
| 1473 |
// FIXME: Add thread_local variables to the source code snippet. |
| 1474 |
EXPECT_TRUE(notMatches(T, varDecl(hasName("x"), hasThreadStorageDuration()))); |
| 1475 |
EXPECT_TRUE(notMatches(T, varDecl(hasName("y"), hasThreadStorageDuration()))); |
| 1476 |
EXPECT_TRUE(notMatches(T, varDecl(hasName("a"), hasThreadStorageDuration()))); |
| 1477 |
} |
| 1478 |
|
| 1479 |
TEST_P(ASTMatchersTest, VarDecl_MatchesFunctionParameter) { |
| 1480 |
EXPECT_TRUE(matches("void f(int i) {}", varDecl(hasName("i")))); |
| 1481 |
} |
| 1482 |
|
| 1483 |
TEST_P(ASTMatchersTest, SizeOfExpr_MatchesCorrectType) { |
| 1484 |
EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 1485 |
sizeOfExpr(hasArgumentOfType(asString("int"))))); |
| 1486 |
EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 1487 |
sizeOfExpr(hasArgumentOfType(asString("float"))))); |
| 1488 |
EXPECT_TRUE(matches( |
| 1489 |
"struct A {}; void x() { struct A a; int b = sizeof(a); }", |
| 1490 |
sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A"))))))); |
| 1491 |
EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 1492 |
sizeOfExpr(hasArgumentOfType( |
| 1493 |
hasDeclaration(recordDecl(hasName("string"))))))); |
| 1494 |
} |
| 1495 |
|
| 1496 |
TEST_P(ASTMatchersTest, IsInteger_MatchesIntegers) { |
| 1497 |
EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger())))); |
| 1498 |
EXPECT_TRUE( |
| 1499 |
matches("long long i = 0; void f(long long) { }; void g() {f(i);}", |
| 1500 |
callExpr(hasArgument( |
| 1501 |
0, declRefExpr(to(varDecl(hasType(isInteger())))))))); |
| 1502 |
} |
| 1503 |
|
| 1504 |
TEST_P(ASTMatchersTest, IsInteger_ReportsNoFalsePositives) { |
| 1505 |
if (!GetParam().isCXX()) { |
| 1506 |
// FIXME: Add a similar negative test for `isInteger()` that does not depend |
| 1507 |
// on C++. |
| 1508 |
return; |
| 1509 |
} |
| 1510 |
|
| 1511 |
EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger())))); |
| 1512 |
EXPECT_TRUE( |
| 1513 |
notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}", |
| 1514 |
callExpr(hasArgument( |
| 1515 |
0, declRefExpr(to(varDecl(hasType(isInteger())))))))); |
| 1516 |
} |
| 1517 |
|
| 1518 |
TEST_P(ASTMatchersTest, IsSignedInteger_MatchesSignedIntegers) { |
| 1519 |
EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isSignedInteger())))); |
| 1520 |
EXPECT_TRUE( |
| 1521 |
notMatches("unsigned i = 0;", varDecl(hasType(isSignedInteger())))); |
| 1522 |
} |
| 1523 |
|
| 1524 |
TEST_P(ASTMatchersTest, IsUnsignedInteger_MatchesUnsignedIntegers) { |
| 1525 |
EXPECT_TRUE(notMatches("int i = 0;", varDecl(hasType(isUnsignedInteger())))); |
| 1526 |
EXPECT_TRUE( |
| 1527 |
matches("unsigned i = 0;", varDecl(hasType(isUnsignedInteger())))); |
| 1528 |
} |
| 1529 |
|
| 1530 |
TEST_P(ASTMatchersTest, IsAnyPointer_MatchesPointers) { |
| 1531 |
if (!GetParam().isCXX11OrLater()) { |
| 1532 |
// FIXME: Add a test for `isAnyPointer()` that does not depend on C++. |
| 1533 |
return; |
| 1534 |
} |
| 1535 |
|
| 1536 |
EXPECT_TRUE(matches("int* i = nullptr;", varDecl(hasType(isAnyPointer())))); |
| 1537 |
} |
| 1538 |
|
| 1539 |
TEST_P(ASTMatchersTest, IsAnyPointer_MatchesObjcPointer) { |
| 1540 |
EXPECT_TRUE(matchesObjC("@interface Foo @end Foo *f;", |
| 1541 |
varDecl(hasType(isAnyPointer())))); |
| 1542 |
} |
| 1543 |
|
| 1544 |
TEST_P(ASTMatchersTest, IsAnyPointer_ReportsNoFalsePositives) { |
| 1545 |
EXPECT_TRUE(notMatches("int i = 0;", varDecl(hasType(isAnyPointer())))); |
| 1546 |
} |
| 1547 |
|
| 1548 |
TEST_P(ASTMatchersTest, IsAnyCharacter_MatchesCharacters) { |
| 1549 |
EXPECT_TRUE(matches("char i = 0;", varDecl(hasType(isAnyCharacter())))); |
| 1550 |
} |
| 1551 |
|
| 1552 |
TEST_P(ASTMatchersTest, IsAnyCharacter_ReportsNoFalsePositives) { |
| 1553 |
EXPECT_TRUE(notMatches("int i;", varDecl(hasType(isAnyCharacter())))); |
| 1554 |
} |
| 1555 |
|
| 1556 |
TEST_P(ASTMatchersTest, IsArrow_MatchesMemberVariablesViaArrow) { |
| 1557 |
if (!GetParam().isCXX()) { |
| 1558 |
// FIXME: Add a test for `isArrow()` that does not depend on C++. |
| 1559 |
return; |
| 1560 |
} |
| 1561 |
if (GetParam().hasDelayedTemplateParsing()) { |
| 1562 |
// FIXME: Fix this test to work with delayed template parsing. |
| 1563 |
return; |
| 1564 |
} |
| 1565 |
|
| 1566 |
EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
| 1567 |
memberExpr(isArrow()))); |
| 1568 |
EXPECT_TRUE( |
| 1569 |
matches("class Y { void x() { y; } int y; };", memberExpr(isArrow()))); |
| 1570 |
EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
| 1571 |
memberExpr(isArrow()))); |
| 1572 |
EXPECT_TRUE(matches("template <class T> class Y { void x() { this->m; } };", |
| 1573 |
cxxDependentScopeMemberExpr(isArrow()))); |
| 1574 |
EXPECT_TRUE( |
| 1575 |
notMatches("template <class T> class Y { void x() { (*this).m; } };", |
| 1576 |
cxxDependentScopeMemberExpr(isArrow()))); |
| 1577 |
} |
| 1578 |
|
| 1579 |
TEST_P(ASTMatchersTest, IsArrow_MatchesStaticMemberVariablesViaArrow) { |
| 1580 |
if (!GetParam().isCXX()) { |
| 1581 |
// FIXME: Add a test for `isArrow()` that does not depend on C++. |
| 1582 |
return; |
| 1583 |
} |
| 1584 |
|
| 1585 |
EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
| 1586 |
memberExpr(isArrow()))); |
| 1587 |
EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
| 1588 |
memberExpr(isArrow()))); |
| 1589 |
EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
| 1590 |
memberExpr(isArrow()))); |
| 1591 |
} |
| 1592 |
|
| 1593 |
TEST_P(ASTMatchersTest, IsArrow_MatchesMemberCallsViaArrow) { |
| 1594 |
if (!GetParam().isCXX()) { |
| 1595 |
// FIXME: Add a test for `isArrow()` that does not depend on C++. |
| 1596 |
return; |
| 1597 |
} |
| 1598 |
if (GetParam().hasDelayedTemplateParsing()) { |
| 1599 |
// FIXME: Fix this test to work with delayed template parsing. |
| 1600 |
return; |
| 1601 |
} |
| 1602 |
|
| 1603 |
EXPECT_TRUE( |
| 1604 |
matches("class Y { void x() { this->x(); } };", memberExpr(isArrow()))); |
| 1605 |
EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr(isArrow()))); |
| 1606 |
EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
| 1607 |
memberExpr(isArrow()))); |
| 1608 |
EXPECT_TRUE( |
| 1609 |
matches("class Y { template <class T> void x() { this->x<T>(); } };", |
| 1610 |
unresolvedMemberExpr(isArrow()))); |
| 1611 |
EXPECT_TRUE(matches("class Y { template <class T> void x() { x<T>(); } };", |
| 1612 |
unresolvedMemberExpr(isArrow()))); |
| 1613 |
EXPECT_TRUE( |
| 1614 |
notMatches("class Y { template <class T> void x() { (*this).x<T>(); } };", |
| 1615 |
unresolvedMemberExpr(isArrow()))); |
| 1616 |
} |
| 1617 |
|
| 1618 |
TEST_P(ASTMatchersTest, IsExplicit_CXXConversionDecl) { |
| 1619 |
if (!GetParam().isCXX11OrLater()) { |
| 1620 |
return; |
| 1621 |
} |
| 1622 |
|
| 1623 |
EXPECT_TRUE(matches("struct S { explicit operator int(); };", |
| 1624 |
cxxConversionDecl(isExplicit()))); |
| 1625 |
EXPECT_TRUE(notMatches("struct S { operator int(); };", |
| 1626 |
cxxConversionDecl(isExplicit()))); |
| 1627 |
} |
| 1628 |
|
| 1629 |
TEST_P(ASTMatchersTest, IsExplicit_CXXConversionDecl_CXX20) { |
| 1630 |
if (!GetParam().isCXX20OrLater()) { |
| 1631 |
return; |
| 1632 |
} |
| 1633 |
|
| 1634 |
EXPECT_TRUE( |
| 1635 |
notMatches("template<bool b> struct S { explicit(b) operator int(); };", |
| 1636 |
cxxConversionDecl(isExplicit()))); |
| 1637 |
EXPECT_TRUE(matches("struct S { explicit(true) operator int(); };", |
| 1638 |
cxxConversionDecl(isExplicit()))); |
| 1639 |
EXPECT_TRUE(notMatches("struct S { explicit(false) operator int(); };", |
| 1640 |
cxxConversionDecl(isExplicit()))); |
| 1641 |
} |
| 1642 |
|
| 1643 |
TEST_P(ASTMatchersTest, ArgumentCountIs_CallExpr) { |
| 1644 |
StatementMatcher Call1Arg = callExpr(argumentCountIs(1)); |
| 1645 |
|
| 1646 |
EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 1647 |
EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 1648 |
} |
| 1649 |
|
| 1650 |
TEST_P(ASTMatchersTest, ArgumentCountIs_CallExpr_CXX) { |
| 1651 |
if (!GetParam().isCXX()) { |
| 1652 |
return; |
| 1653 |
} |
| 1654 |
|
| 1655 |
StatementMatcher Call1Arg = callExpr(argumentCountIs(1)); |
| 1656 |
EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 1657 |
} |
| 1658 |
|
| 1659 |
TEST_P(ASTMatchersTest, ParameterCountIs) { |
| 1660 |
DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1)); |
| 1661 |
EXPECT_TRUE(matches("void f(int i) {}", Function1Arg)); |
| 1662 |
EXPECT_TRUE(notMatches("void f() {}", Function1Arg)); |
| 1663 |
EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg)); |
| 1664 |
EXPECT_TRUE(matches("void f(int i, ...) {};", Function1Arg)); |
| 1665 |
} |
| 1666 |
|
| 1667 |
TEST_P(ASTMatchersTest, ParameterCountIs_CXX) { |
| 1668 |
if (!GetParam().isCXX()) { |
| 1669 |
return; |
| 1670 |
} |
| 1671 |
|
| 1672 |
DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1)); |
| 1673 |
EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg)); |
| 1674 |
} |
| 1675 |
|
| 1676 |
TEST_P(ASTMatchersTest, References) { |
| 1677 |
if (!GetParam().isCXX()) { |
| 1678 |
// FIXME: Add a test for `references()` that does not depend on C++. |
| 1679 |
return; |
| 1680 |
} |
| 1681 |
|
| 1682 |
DeclarationMatcher ReferenceClassX = |
| 1683 |
varDecl(hasType(references(recordDecl(hasName("X"))))); |
| 1684 |
EXPECT_TRUE( |
| 1685 |
matches("class X {}; void y(X y) { X &x = y; }", ReferenceClassX)); |
| 1686 |
EXPECT_TRUE( |
| 1687 |
matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
| 1688 |
// The match here is on the implicit copy constructor code for |
| 1689 |
// class X, not on code 'X x = y'. |
| 1690 |
EXPECT_TRUE(matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 1691 |
EXPECT_TRUE(notMatches("class X {}; extern X x;", ReferenceClassX)); |
| 1692 |
EXPECT_TRUE( |
| 1693 |
notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 1694 |
} |
| 1695 |
|
| 1696 |
TEST_P(ASTMatchersTest, HasLocalQualifiers) { |
| 1697 |
if (!GetParam().isCXX11OrLater()) { |
| 1698 |
// FIXME: Add a test for `hasLocalQualifiers()` that does not depend on C++. |
| 1699 |
return; |
| 1700 |
} |
| 1701 |
|
| 1702 |
EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;", |
| 1703 |
varDecl(hasType(hasLocalQualifiers())))); |
| 1704 |
EXPECT_TRUE(matches("int *const j = nullptr;", |
| 1705 |
varDecl(hasType(hasLocalQualifiers())))); |
| 1706 |
EXPECT_TRUE( |
| 1707 |
matches("int *volatile k;", varDecl(hasType(hasLocalQualifiers())))); |
| 1708 |
EXPECT_TRUE(notMatches("int m;", varDecl(hasType(hasLocalQualifiers())))); |
| 1709 |
} |
| 1710 |
|
| 1711 |
TEST_P(ASTMatchersTest, IsExternC_MatchesExternCFunctionDeclarations) { |
| 1712 |
if (!GetParam().isCXX()) { |
| 1713 |
return; |
| 1714 |
} |
| 1715 |
|
| 1716 |
EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC()))); |
| 1717 |
EXPECT_TRUE( |
| 1718 |
matches("extern \"C\" { void f() {} }", functionDecl(isExternC()))); |
| 1719 |
EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC()))); |
| 1720 |
} |
| 1721 |
|
| 1722 |
TEST_P(ASTMatchersTest, IsExternC_MatchesExternCVariableDeclarations) { |
| 1723 |
if (!GetParam().isCXX()) { |
| 1724 |
return; |
| 1725 |
} |
| 1726 |
|
| 1727 |
EXPECT_TRUE(matches("extern \"C\" int i;", varDecl(isExternC()))); |
| 1728 |
EXPECT_TRUE(matches("extern \"C\" { int i; }", varDecl(isExternC()))); |
| 1729 |
EXPECT_TRUE(notMatches("int i;", varDecl(isExternC()))); |
| 1730 |
} |
| 1731 |
|
| 1732 |
TEST_P(ASTMatchersTest, IsStaticStorageClass) { |
| 1733 |
EXPECT_TRUE( |
| 1734 |
matches("static void f() {}", functionDecl(isStaticStorageClass()))); |
| 1735 |
EXPECT_TRUE(matches("static int i = 1;", varDecl(isStaticStorageClass()))); |
| 1736 |
EXPECT_TRUE(notMatches("int i = 1;", varDecl(isStaticStorageClass()))); |
| 1737 |
EXPECT_TRUE(notMatches("extern int i;", varDecl(isStaticStorageClass()))); |
| 1738 |
EXPECT_TRUE(notMatches("void f() {}", functionDecl(isStaticStorageClass()))); |
| 1739 |
} |
| 1740 |
|
| 1741 |
TEST_P(ASTMatchersTest, IsDefaulted) { |
| 1742 |
if (!GetParam().isCXX()) { |
| 1743 |
return; |
| 1744 |
} |
| 1745 |
|
| 1746 |
EXPECT_TRUE(notMatches("class A { ~A(); };", |
| 1747 |
functionDecl(hasName("~A"), isDefaulted()))); |
| 1748 |
EXPECT_TRUE(matches("class B { ~B() = default; };", |
| 1749 |
functionDecl(hasName("~B"), isDefaulted()))); |
| 1750 |
} |
| 1751 |
|
| 1752 |
TEST_P(ASTMatchersTest, IsDeleted) { |
| 1753 |
if (!GetParam().isCXX()) { |
| 1754 |
return; |
| 1755 |
} |
| 1756 |
|
| 1757 |
EXPECT_TRUE( |
| 1758 |
notMatches("void Func();", functionDecl(hasName("Func"), isDeleted()))); |
| 1759 |
EXPECT_TRUE(matches("void Func() = delete;", |
| 1760 |
functionDecl(hasName("Func"), isDeleted()))); |
| 1761 |
} |
| 1762 |
|
| 1763 |
TEST_P(ASTMatchersTest, IsNoThrow_DynamicExceptionSpec) { |
| 1764 |
if (!GetParam().supportsCXXDynamicExceptionSpecification()) { |
| 1765 |
return; |
| 1766 |
} |
| 1767 |
|
| 1768 |
EXPECT_TRUE(notMatches("void f();", functionDecl(isNoThrow()))); |
| 1769 |
EXPECT_TRUE(notMatches("void f() throw(int);", functionDecl(isNoThrow()))); |
| 1770 |
EXPECT_TRUE(matches("void f() throw();", functionDecl(isNoThrow()))); |
| 1771 |
|
| 1772 |
EXPECT_TRUE(notMatches("void f();", functionProtoType(isNoThrow()))); |
| 1773 |
EXPECT_TRUE( |
| 1774 |
notMatches("void f() throw(int);", functionProtoType(isNoThrow()))); |
| 1775 |
EXPECT_TRUE(matches("void f() throw();", functionProtoType(isNoThrow()))); |
| 1776 |
} |
| 1777 |
|
| 1778 |
TEST_P(ASTMatchersTest, IsNoThrow_CXX11) { |
| 1779 |
if (!GetParam().isCXX11OrLater()) { |
| 1780 |
return; |
| 1781 |
} |
| 1782 |
|
| 1783 |
EXPECT_TRUE( |
| 1784 |
notMatches("void f() noexcept(false);", functionDecl(isNoThrow()))); |
| 1785 |
EXPECT_TRUE(matches("void f() noexcept;", functionDecl(isNoThrow()))); |
| 1786 |
|
| 1787 |
EXPECT_TRUE( |
| 1788 |
notMatches("void f() noexcept(false);", functionProtoType(isNoThrow()))); |
| 1789 |
EXPECT_TRUE(matches("void f() noexcept;", functionProtoType(isNoThrow()))); |
| 1790 |
} |
| 1791 |
|
| 1792 |
TEST_P(ASTMatchersTest, IsConsteval) { |
| 1793 |
if (!GetParam().isCXX20OrLater()) |
| 1794 |
return; |
| 1795 |
|
| 1796 |
EXPECT_TRUE(matches("consteval int bar();", |
| 1797 |
functionDecl(hasName("bar"), isConsteval()))); |
| 1798 |
EXPECT_TRUE(notMatches("constexpr int bar();", |
| 1799 |
functionDecl(hasName("bar"), isConsteval()))); |
| 1800 |
EXPECT_TRUE( |
| 1801 |
notMatches("int bar();", functionDecl(hasName("bar"), isConsteval()))); |
| 1802 |
} |
| 1803 |
|
| 1804 |
TEST_P(ASTMatchersTest, IsConsteval_MatchesIfConsteval) { |
| 1805 |
if (!GetParam().isCXX20OrLater()) |
| 1806 |
return; |
| 1807 |
|
| 1808 |
EXPECT_TRUE(matches("void baz() { if consteval {} }", ifStmt(isConsteval()))); |
| 1809 |
EXPECT_TRUE( |
| 1810 |
matches("void baz() { if ! consteval {} }", ifStmt(isConsteval()))); |
| 1811 |
EXPECT_TRUE(matches("void baz() { if ! consteval {} else {} }", |
| 1812 |
ifStmt(isConsteval()))); |
| 1813 |
EXPECT_TRUE( |
| 1814 |
matches("void baz() { if not consteval {} }", ifStmt(isConsteval()))); |
| 1815 |
EXPECT_TRUE(notMatches("void baz() { if constexpr(1 > 0) {} }", |
| 1816 |
ifStmt(isConsteval()))); |
| 1817 |
EXPECT_TRUE( |
| 1818 |
notMatches("void baz() { if (1 > 0) {} }", ifStmt(isConsteval()))); |
| 1819 |
} |
| 1820 |
|
| 1821 |
TEST_P(ASTMatchersTest, IsConstexpr) { |
| 1822 |
if (!GetParam().isCXX11OrLater()) { |
| 1823 |
return; |
| 1824 |
} |
| 1825 |
|
| 1826 |
EXPECT_TRUE(matches("constexpr int foo = 42;", |
| 1827 |
varDecl(hasName("foo"), isConstexpr()))); |
| 1828 |
EXPECT_TRUE(matches("constexpr int bar();", |
| 1829 |
functionDecl(hasName("bar"), isConstexpr()))); |
| 1830 |
} |
| 1831 |
|
| 1832 |
TEST_P(ASTMatchersTest, IsConstexpr_MatchesIfConstexpr) { |
| 1833 |
if (!GetParam().isCXX17OrLater()) { |
| 1834 |
return; |
| 1835 |
} |
| 1836 |
|
| 1837 |
EXPECT_TRUE( |
| 1838 |
matches("void baz() { if constexpr(1 > 0) {} }", ifStmt(isConstexpr()))); |
| 1839 |
EXPECT_TRUE( |
| 1840 |
notMatches("void baz() { if (1 > 0) {} }", ifStmt(isConstexpr()))); |
| 1841 |
} |
| 1842 |
|
| 1843 |
TEST_P(ASTMatchersTest, IsConstinit) { |
| 1844 |
if (!GetParam().isCXX20OrLater()) |
| 1845 |
return; |
| 1846 |
|
| 1847 |
EXPECT_TRUE(matches("constinit int foo = 1;", |
| 1848 |
varDecl(hasName("foo"), isConstinit()))); |
| 1849 |
EXPECT_TRUE(matches("extern constinit int foo;", |
| 1850 |
varDecl(hasName("foo"), isConstinit()))); |
| 1851 |
EXPECT_TRUE(matches("constinit const char* foo = \"bar\";", |
| 1852 |
varDecl(hasName("foo"), isConstinit()))); |
| 1853 |
EXPECT_TRUE( |
| 1854 |
notMatches("[[clang::require_constant_initialization]] int foo = 1;", |
| 1855 |
varDecl(hasName("foo"), isConstinit()))); |
| 1856 |
EXPECT_TRUE(notMatches("constexpr int foo = 1;", |
| 1857 |
varDecl(hasName("foo"), isConstinit()))); |
| 1858 |
EXPECT_TRUE(notMatches("static inline int foo = 1;", |
| 1859 |
varDecl(hasName("foo"), isConstinit()))); |
| 1860 |
} |
| 1861 |
|
| 1862 |
TEST_P(ASTMatchersTest, HasInitStatement_MatchesSelectionInitializers) { |
| 1863 |
EXPECT_TRUE(notMatches("void baz() { if (1 > 0) {} }", |
| 1864 |
ifStmt(hasInitStatement(anything())))); |
| 1865 |
EXPECT_TRUE(notMatches("void baz(int i) { switch (i) { default: break; } }", |
| 1866 |
switchStmt(hasInitStatement(anything())))); |
| 1867 |
} |
| 1868 |
|
| 1869 |
TEST_P(ASTMatchersTest, HasInitStatement_MatchesSelectionInitializers_CXX) { |
| 1870 |
if (!GetParam().isCXX()) { |
| 1871 |
return; |
| 1872 |
} |
| 1873 |
|
| 1874 |
EXPECT_TRUE(notMatches("void baz() { if (int i = 1) {} }", |
| 1875 |
ifStmt(hasInitStatement(anything())))); |
| 1876 |
} |
| 1877 |
|
| 1878 |
TEST_P(ASTMatchersTest, HasInitStatement_MatchesSelectionInitializers_CXX17) { |
| 1879 |
if (!GetParam().isCXX17OrLater()) { |
| 1880 |
return; |
| 1881 |
} |
| 1882 |
|
| 1883 |
EXPECT_TRUE(matches("void baz() { if (int i = 1; i > 0) {} }", |
| 1884 |
ifStmt(hasInitStatement(anything())))); |
| 1885 |
EXPECT_TRUE( |
| 1886 |
matches("void baz(int i) { switch (int j = i; j) { default: break; } }", |
| 1887 |
switchStmt(hasInitStatement(anything())))); |
| 1888 |
} |
| 1889 |
|
| 1890 |
TEST_P(ASTMatchersTest, HasInitStatement_MatchesRangeForInitializers) { |
| 1891 |
if (!GetParam().isCXX20OrLater()) { |
| 1892 |
return; |
| 1893 |
} |
| 1894 |
|
| 1895 |
EXPECT_TRUE(matches("void baz() {" |
| 1896 |
"int items[] = {};" |
| 1897 |
"for (auto &arr = items; auto &item : arr) {}" |
| 1898 |
"}", |
| 1899 |
cxxForRangeStmt(hasInitStatement(anything())))); |
| 1900 |
EXPECT_TRUE(notMatches("void baz() {" |
| 1901 |
"int items[] = {};" |
| 1902 |
"for (auto &item : items) {}" |
| 1903 |
"}", |
| 1904 |
cxxForRangeStmt(hasInitStatement(anything())))); |
| 1905 |
} |
| 1906 |
|
| 1907 |
TEST_P(ASTMatchersTest, TemplateArgumentCountIs) { |
| 1908 |
if (!GetParam().isCXX()) { |
| 1909 |
return; |
| 1910 |
} |
| 1911 |
|
| 1912 |
EXPECT_TRUE( |
| 1913 |
matches("template<typename T> struct C {}; C<int> c;", |
| 1914 |
classTemplateSpecializationDecl(templateArgumentCountIs(1)))); |
| 1915 |
EXPECT_TRUE( |
| 1916 |
notMatches("template<typename T> struct C {}; C<int> c;", |
| 1917 |
classTemplateSpecializationDecl(templateArgumentCountIs(2)))); |
| 1918 |
|
| 1919 |
EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;", |
| 1920 |
templateSpecializationType(templateArgumentCountIs(1)))); |
| 1921 |
EXPECT_TRUE( |
| 1922 |
notMatches("template<typename T> struct C {}; C<int> c;", |
| 1923 |
templateSpecializationType(templateArgumentCountIs(2)))); |
| 1924 |
} |
| 1925 |
|
| 1926 |
TEST_P(ASTMatchersTest, IsIntegral) { |
| 1927 |
if (!GetParam().isCXX()) { |
| 1928 |
return; |
| 1929 |
} |
| 1930 |
|
| 1931 |
EXPECT_TRUE(matches( |
| 1932 |
"template<int T> struct C {}; C<42> c;", |
| 1933 |
classTemplateSpecializationDecl(hasAnyTemplateArgument(isIntegral())))); |
| 1934 |
EXPECT_TRUE(notMatches("template<typename T> struct C {}; C<int> c;", |
| 1935 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1936 |
templateArgument(isIntegral()))))); |
| 1937 |
} |
| 1938 |
|
| 1939 |
TEST_P(ASTMatchersTest, EqualsIntegralValue) { |
| 1940 |
if (!GetParam().isCXX()) { |
| 1941 |
return; |
| 1942 |
} |
| 1943 |
|
| 1944 |
EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;", |
| 1945 |
classTemplateSpecializationDecl( |
| 1946 |
hasAnyTemplateArgument(equalsIntegralValue("42"))))); |
| 1947 |
EXPECT_TRUE(matches("template<int T> struct C {}; C<-42> c;", |
| 1948 |
classTemplateSpecializationDecl( |
| 1949 |
hasAnyTemplateArgument(equalsIntegralValue("-42"))))); |
| 1950 |
EXPECT_TRUE(matches("template<int T> struct C {}; C<-0042> c;", |
| 1951 |
classTemplateSpecializationDecl( |
| 1952 |
hasAnyTemplateArgument(equalsIntegralValue("-34"))))); |
| 1953 |
EXPECT_TRUE(notMatches("template<int T> struct C {}; C<42> c;", |
| 1954 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1955 |
equalsIntegralValue("0042"))))); |
| 1956 |
} |
| 1957 |
|
| 1958 |
TEST_P(ASTMatchersTest, AccessSpecDecl) { |
| 1959 |
if (!GetParam().isCXX()) { |
| 1960 |
return; |
| 1961 |
} |
| 1962 |
|
| 1963 |
EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl())); |
| 1964 |
EXPECT_TRUE( |
| 1965 |
matches("class C { public: int i; };", accessSpecDecl(isPublic()))); |
| 1966 |
EXPECT_TRUE( |
| 1967 |
notMatches("class C { public: int i; };", accessSpecDecl(isProtected()))); |
| 1968 |
EXPECT_TRUE( |
| 1969 |
notMatches("class C { public: int i; };", accessSpecDecl(isPrivate()))); |
| 1970 |
|
| 1971 |
EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl())); |
| 1972 |
} |
| 1973 |
|
| 1974 |
TEST_P(ASTMatchersTest, IsFinal) { |
| 1975 |
if (!GetParam().isCXX11OrLater()) { |
| 1976 |
return; |
| 1977 |
} |
| 1978 |
|
| 1979 |
EXPECT_TRUE(matches("class X final {};", cxxRecordDecl(isFinal()))); |
| 1980 |
EXPECT_TRUE(matches("class X { virtual void f() final; };", |
| 1981 |
cxxMethodDecl(isFinal()))); |
| 1982 |
EXPECT_TRUE(notMatches("class X {};", cxxRecordDecl(isFinal()))); |
| 1983 |
EXPECT_TRUE( |
| 1984 |
notMatches("class X { virtual void f(); };", cxxMethodDecl(isFinal()))); |
| 1985 |
} |
| 1986 |
|
| 1987 |
TEST_P(ASTMatchersTest, IsVirtual) { |
| 1988 |
if (!GetParam().isCXX()) { |
| 1989 |
return; |
| 1990 |
} |
| 1991 |
|
| 1992 |
EXPECT_TRUE(matches("class X { virtual int f(); };", |
| 1993 |
cxxMethodDecl(isVirtual(), hasName("::X::f")))); |
| 1994 |
EXPECT_TRUE(notMatches("class X { int f(); };", cxxMethodDecl(isVirtual()))); |
| 1995 |
} |
| 1996 |
|
| 1997 |
TEST_P(ASTMatchersTest, IsVirtualAsWritten) { |
| 1998 |
if (!GetParam().isCXX()) { |
| 1999 |
return; |
| 2000 |
} |
| 2001 |
|
| 2002 |
EXPECT_TRUE(matches("class A { virtual int f(); };" |
| 2003 |
"class B : public A { int f(); };", |
| 2004 |
cxxMethodDecl(isVirtualAsWritten(), hasName("::A::f")))); |
| 2005 |
EXPECT_TRUE( |
| 2006 |
notMatches("class A { virtual int f(); };" |
| 2007 |
"class B : public A { int f(); };", |
| 2008 |
cxxMethodDecl(isVirtualAsWritten(), hasName("::B::f")))); |
| 2009 |
} |
| 2010 |
|
| 2011 |
TEST_P(ASTMatchersTest, IsPure) { |
| 2012 |
if (!GetParam().isCXX()) { |
| 2013 |
return; |
| 2014 |
} |
| 2015 |
|
| 2016 |
EXPECT_TRUE(matches("class X { virtual int f() = 0; };", |
| 2017 |
cxxMethodDecl(isPure(), hasName("::X::f")))); |
| 2018 |
EXPECT_TRUE(notMatches("class X { int f(); };", cxxMethodDecl(isPure()))); |
| 2019 |
} |
| 2020 |
|
| 2021 |
TEST_P(ASTMatchersTest, IsCopyAssignmentOperator) { |
| 2022 |
if (!GetParam().isCXX()) { |
| 2023 |
return; |
| 2024 |
} |
| 2025 |
|
| 2026 |
auto CopyAssignment = |
| 2027 |
cxxMethodDecl(isCopyAssignmentOperator(), unless(isImplicit())); |
| 2028 |
EXPECT_TRUE(matches("class X { X &operator=(X); };", CopyAssignment)); |
| 2029 |
EXPECT_TRUE(matches("class X { X &operator=(X &); };", CopyAssignment)); |
| 2030 |
EXPECT_TRUE(matches("class X { X &operator=(const X &); };", CopyAssignment)); |
| 2031 |
EXPECT_TRUE(matches("class X { X &operator=(volatile X &); };", // |
| 2032 |
CopyAssignment)); |
| 2033 |
EXPECT_TRUE(matches("class X { X &operator=(const volatile X &); };", |
| 2034 |
CopyAssignment)); |
| 2035 |
EXPECT_TRUE(notMatches("class X { X &operator=(X &&); };", CopyAssignment)); |
| 2036 |
} |
| 2037 |
|
| 2038 |
TEST_P(ASTMatchersTest, IsMoveAssignmentOperator) { |
| 2039 |
if (!GetParam().isCXX()) { |
| 2040 |
return; |
| 2041 |
} |
| 2042 |
|
| 2043 |
auto MoveAssignment = |
| 2044 |
cxxMethodDecl(isMoveAssignmentOperator(), unless(isImplicit())); |
| 2045 |
EXPECT_TRUE(notMatches("class X { X &operator=(X); };", MoveAssignment)); |
| 2046 |
EXPECT_TRUE(matches("class X { X &operator=(X &&); };", MoveAssignment)); |
| 2047 |
EXPECT_TRUE(matches("class X { X &operator=(const X &&); };", // |
| 2048 |
MoveAssignment)); |
| 2049 |
EXPECT_TRUE(matches("class X { X &operator=(volatile X &&); };", // |
| 2050 |
MoveAssignment)); |
| 2051 |
EXPECT_TRUE(matches("class X { X &operator=(const volatile X &&); };", |
| 2052 |
MoveAssignment)); |
| 2053 |
EXPECT_TRUE(notMatches("class X { X &operator=(X &); };", MoveAssignment)); |
| 2054 |
} |
| 2055 |
|
| 2056 |
TEST_P(ASTMatchersTest, IsConst) { |
| 2057 |
if (!GetParam().isCXX()) { |
| 2058 |
return; |
| 2059 |
} |
| 2060 |
|
| 2061 |
EXPECT_TRUE( |
| 2062 |
matches("struct A { void foo() const; };", cxxMethodDecl(isConst()))); |
| 2063 |
EXPECT_TRUE( |
| 2064 |
notMatches("struct A { void foo(); };", cxxMethodDecl(isConst()))); |
| 2065 |
} |
| 2066 |
|
| 2067 |
TEST_P(ASTMatchersTest, IsOverride) { |
| 2068 |
if (!GetParam().isCXX()) { |
| 2069 |
return; |
| 2070 |
} |
| 2071 |
|
| 2072 |
EXPECT_TRUE(matches("class X { virtual int f(); }; " |
| 2073 |
"class Y : public X { int f(); };", |
| 2074 |
cxxMethodDecl(isOverride(), hasName("::Y::f")))); |
| 2075 |
EXPECT_TRUE(notMatches("class X { virtual int f(); }; " |
| 2076 |
"class Y : public X { int f(); };", |
| 2077 |
cxxMethodDecl(isOverride(), hasName("::X::f")))); |
| 2078 |
EXPECT_TRUE(notMatches("class X { int f(); }; " |
| 2079 |
"class Y : public X { int f(); };", |
| 2080 |
cxxMethodDecl(isOverride()))); |
| 2081 |
EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ", |
| 2082 |
cxxMethodDecl(isOverride()))); |
| 2083 |
EXPECT_TRUE( |
| 2084 |
matches("template <typename Base> struct Y : Base { void f() override;};", |
| 2085 |
cxxMethodDecl(isOverride(), hasName("::Y::f")))); |
| 2086 |
} |
| 2087 |
|
| 2088 |
TEST_P(ASTMatchersTest, HasArgument_CXXConstructorDecl) { |
| 2089 |
if (!GetParam().isCXX()) { |
| 2090 |
return; |
| 2091 |
} |
| 2092 |
|
| 2093 |
auto Constructor = traverse( |
| 2094 |
TK_AsIs, |
| 2095 |
cxxConstructExpr(hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))))); |
| 2096 |
|
| 2097 |
EXPECT_TRUE(matches( |
| 2098 |
"class X { public: X(int); }; void x() { int y; X x(y); }", Constructor)); |
| 2099 |
EXPECT_TRUE( |
| 2100 |
matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 2101 |
Constructor)); |
| 2102 |
EXPECT_TRUE( |
| 2103 |
matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 2104 |
Constructor)); |
| 2105 |
EXPECT_TRUE(notMatches( |
| 2106 |
"class X { public: X(int); }; void x() { int z; X x(z); }", Constructor)); |
| 2107 |
|
| 2108 |
StatementMatcher WrongIndex = |
| 2109 |
traverse(TK_AsIs, cxxConstructExpr(hasArgument( |
| 2110 |
42, declRefExpr(to(varDecl(hasName("y"))))))); |
| 2111 |
EXPECT_TRUE(notMatches( |
| 2112 |
"class X { public: X(int); }; void x() { int y; X x(y); }", WrongIndex)); |
| 2113 |
} |
| 2114 |
|
| 2115 |
TEST_P(ASTMatchersTest, ArgumentCountIs_CXXConstructExpr) { |
| 2116 |
if (!GetParam().isCXX()) { |
| 2117 |
return; |
| 2118 |
} |
| 2119 |
|
| 2120 |
auto Constructor1Arg = |
| 2121 |
traverse(TK_AsIs, cxxConstructExpr(argumentCountIs(1))); |
| 2122 |
|
| 2123 |
EXPECT_TRUE(matches("class X { public: X(int); }; void x() { X x(0); }", |
| 2124 |
Constructor1Arg)); |
| 2125 |
EXPECT_TRUE(matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 2126 |
Constructor1Arg)); |
| 2127 |
EXPECT_TRUE(matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 2128 |
Constructor1Arg)); |
| 2129 |
EXPECT_TRUE( |
| 2130 |
notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 2131 |
Constructor1Arg)); |
| 2132 |
} |
| 2133 |
|
| 2134 |
TEST(ASTMatchersTest, NamesMember_CXXDependentScopeMemberExpr) { |
| 2135 |
|
| 2136 |
// Member functions: |
| 2137 |
{ |
| 2138 |
auto Code = "template <typename T> struct S{ void mem(); }; template " |
| 2139 |
"<typename T> void x() { S<T> s; s.mem(); }"; |
| 2140 |
|
| 2141 |
EXPECT_TRUE(matches( |
| 2142 |
Code, |
| 2143 |
cxxDependentScopeMemberExpr( |
| 2144 |
hasObjectExpression(declRefExpr(hasType(elaboratedType(namesType( |
| 2145 |
templateSpecializationType(hasDeclaration(classTemplateDecl( |
| 2146 |
has(cxxRecordDecl(has(cxxMethodDecl(hasName("mem")) |
| 2147 |
.bind("templMem")))))))))))), |
| 2148 |
memberHasSameNameAsBoundNode("templMem")))); |
| 2149 |
|
| 2150 |
EXPECT_TRUE( |
| 2151 |
matches(Code, cxxDependentScopeMemberExpr(hasMemberName("mem")))); |
| 2152 |
} |
| 2153 |
|
| 2154 |
// Member variables: |
| 2155 |
{ |
| 2156 |
auto Code = "template <typename T> struct S{ int mem; }; template " |
| 2157 |
"<typename T> void x() { S<T> s; s.mem; }"; |
| 2158 |
|
| 2159 |
EXPECT_TRUE( |
| 2160 |
matches(Code, cxxDependentScopeMemberExpr(hasMemberName("mem")))); |
| 2161 |
|
| 2162 |
EXPECT_TRUE(matches( |
| 2163 |
Code, |
| 2164 |
cxxDependentScopeMemberExpr( |
| 2165 |
hasObjectExpression(declRefExpr( |
| 2166 |
hasType(elaboratedType(namesType(templateSpecializationType( |
| 2167 |
hasDeclaration(classTemplateDecl(has(cxxRecordDecl(has( |
| 2168 |
fieldDecl(hasName("mem")).bind("templMem")))))))))))), |
| 2169 |
memberHasSameNameAsBoundNode("templMem")))); |
| 2170 |
} |
| 2171 |
|
| 2172 |
// static member variables: |
| 2173 |
{ |
| 2174 |
auto Code = "template <typename T> struct S{ static int mem; }; template " |
| 2175 |
"<typename T> void x() { S<T> s; s.mem; }"; |
| 2176 |
|
| 2177 |
EXPECT_TRUE( |
| 2178 |
matches(Code, cxxDependentScopeMemberExpr(hasMemberName("mem")))); |
| 2179 |
|
| 2180 |
EXPECT_TRUE(matches( |
| 2181 |
Code, |
| 2182 |
cxxDependentScopeMemberExpr( |
| 2183 |
hasObjectExpression(declRefExpr( |
| 2184 |
hasType(elaboratedType(namesType(templateSpecializationType( |
| 2185 |
hasDeclaration(classTemplateDecl(has(cxxRecordDecl( |
| 2186 |
has(varDecl(hasName("mem")).bind("templMem")))))))))))), |
| 2187 |
memberHasSameNameAsBoundNode("templMem")))); |
| 2188 |
} |
| 2189 |
{ |
| 2190 |
auto Code = R"cpp( |
| 2191 |
template <typename T> |
| 2192 |
struct S { |
| 2193 |
bool operator==(int) const { return true; } |
| 2194 |
}; |
| 2195 |
|
| 2196 |
template <typename T> |
| 2197 |
void func(T t) { |
| 2198 |
S<T> s; |
| 2199 |
s.operator==(1); |
| 2200 |
} |
| 2201 |
)cpp"; |
| 2202 |
|
| 2203 |
EXPECT_TRUE(matches( |
| 2204 |
Code, cxxDependentScopeMemberExpr(hasMemberName("operator==")))); |
| 2205 |
} |
| 2206 |
|
| 2207 |
// other named decl: |
| 2208 |
{ |
| 2209 |
auto Code = "template <typename T> struct S{ static int mem; }; struct " |
| 2210 |
"mem{}; template " |
| 2211 |
"<typename T> void x() { S<T> s; s.mem; }"; |
| 2212 |
|
| 2213 |
EXPECT_TRUE(matches( |
| 2214 |
Code, |
| 2215 |
translationUnitDecl(has(cxxRecordDecl(hasName("mem"))), |
| 2216 |
hasDescendant(cxxDependentScopeMemberExpr())))); |
| 2217 |
|
| 2218 |
EXPECT_FALSE(matches( |
| 2219 |
Code, |
| 2220 |
translationUnitDecl(has(cxxRecordDecl(hasName("mem")).bind("templMem")), |
| 2221 |
hasDescendant(cxxDependentScopeMemberExpr( |
| 2222 |
memberHasSameNameAsBoundNode("templMem")))))); |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
TEST(ASTMatchersTest, ArgumentCountIs_CXXUnresolvedConstructExpr) { |
| 2227 |
const auto *Code = |
| 2228 |
"template <typename T> struct S{}; template <typename T> void " |
| 2229 |
"x() { auto s = S<T>(); }"; |
| 2230 |
|
| 2231 |
EXPECT_TRUE(matches(Code, cxxUnresolvedConstructExpr(argumentCountIs(0)))); |
| 2232 |
EXPECT_TRUE(notMatches(Code, cxxUnresolvedConstructExpr(argumentCountIs(1)))); |
| 2233 |
} |
| 2234 |
|
| 2235 |
TEST(ASTMatchersTest, HasArgument_CXXUnresolvedConstructExpr) { |
| 2236 |
const auto *Code = |
| 2237 |
"template <typename T> struct S{ S(int){} }; template <typename " |
| 2238 |
"T> void x() { int y; auto s = S<T>(y); }"; |
| 2239 |
EXPECT_TRUE(matches(Code, cxxUnresolvedConstructExpr(hasArgument( |
| 2240 |
0, declRefExpr(to(varDecl(hasName("y")))))))); |
| 2241 |
EXPECT_TRUE( |
| 2242 |
notMatches(Code, cxxUnresolvedConstructExpr(hasArgument( |
| 2243 |
0, declRefExpr(to(varDecl(hasName("x")))))))); |
| 2244 |
} |
| 2245 |
|
| 2246 |
TEST_P(ASTMatchersTest, IsListInitialization) { |
| 2247 |
if (!GetParam().isCXX11OrLater()) { |
| 2248 |
return; |
| 2249 |
} |
| 2250 |
|
| 2251 |
auto ConstructorListInit = |
| 2252 |
traverse(TK_AsIs, varDecl(has(cxxConstructExpr(isListInitialization())))); |
| 2253 |
|
| 2254 |
EXPECT_TRUE(matches("class X { public: X(int); }; void x() { X x{0}; }", |
| 2255 |
ConstructorListInit)); |
| 2256 |
EXPECT_FALSE(matches("class X { public: X(int); }; void x() { X x(0); }", |
| 2257 |
ConstructorListInit)); |
| 2258 |
} |
| 2259 |
|
| 2260 |
TEST_P(ASTMatchersTest, IsImplicit_CXXConstructorDecl) { |
| 2261 |
if (!GetParam().isCXX()) { |
| 2262 |
return; |
| 2263 |
} |
| 2264 |
|
| 2265 |
// This one doesn't match because the constructor is not added by the |
| 2266 |
// compiler (it is not needed). |
| 2267 |
EXPECT_TRUE(notMatches("class Foo { };", cxxConstructorDecl(isImplicit()))); |
| 2268 |
// The compiler added the implicit default constructor. |
| 2269 |
EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
| 2270 |
cxxConstructorDecl(isImplicit()))); |
| 2271 |
EXPECT_TRUE(matches("class Foo { Foo(){} };", |
| 2272 |
cxxConstructorDecl(unless(isImplicit())))); |
| 2273 |
// The compiler added an implicit assignment operator. |
| 2274 |
EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }", |
| 2275 |
cxxMethodDecl(isImplicit(), hasName("operator=")))); |
| 2276 |
} |
| 2277 |
|
| 2278 |
TEST_P(ASTMatchersTest, IsExplicit_CXXConstructorDecl) { |
| 2279 |
if (!GetParam().isCXX()) { |
| 2280 |
return; |
| 2281 |
} |
| 2282 |
|
| 2283 |
EXPECT_TRUE(matches("struct S { explicit S(int); };", |
| 2284 |
cxxConstructorDecl(isExplicit()))); |
| 2285 |
EXPECT_TRUE( |
| 2286 |
notMatches("struct S { S(int); };", cxxConstructorDecl(isExplicit()))); |
| 2287 |
} |
| 2288 |
|
| 2289 |
TEST_P(ASTMatchersTest, IsExplicit_CXXConstructorDecl_CXX20) { |
| 2290 |
if (!GetParam().isCXX20OrLater()) { |
| 2291 |
return; |
| 2292 |
} |
| 2293 |
|
| 2294 |
EXPECT_TRUE(notMatches("template<bool b> struct S { explicit(b) S(int);};", |
| 2295 |
cxxConstructorDecl(isExplicit()))); |
| 2296 |
EXPECT_TRUE(matches("struct S { explicit(true) S(int);};", |
| 2297 |
cxxConstructorDecl(isExplicit()))); |
| 2298 |
EXPECT_TRUE(notMatches("struct S { explicit(false) S(int);};", |
| 2299 |
cxxConstructorDecl(isExplicit()))); |
| 2300 |
} |
| 2301 |
|
| 2302 |
TEST_P(ASTMatchersTest, IsExplicit_CXXDeductionGuideDecl) { |
| 2303 |
if (!GetParam().isCXX17OrLater()) { |
| 2304 |
return; |
| 2305 |
} |
| 2306 |
|
| 2307 |
EXPECT_TRUE(notMatches("template<typename T> struct S { S(int);};" |
| 2308 |
"S(int) -> S<int>;", |
| 2309 |
cxxDeductionGuideDecl(isExplicit()))); |
| 2310 |
EXPECT_TRUE(matches("template<typename T> struct S { S(int);};" |
| 2311 |
"explicit S(int) -> S<int>;", |
| 2312 |
cxxDeductionGuideDecl(isExplicit()))); |
| 2313 |
} |
| 2314 |
|
| 2315 |
TEST_P(ASTMatchersTest, IsExplicit_CXXDeductionGuideDecl_CXX20) { |
| 2316 |
if (!GetParam().isCXX20OrLater()) { |
| 2317 |
return; |
| 2318 |
} |
| 2319 |
|
| 2320 |
EXPECT_TRUE(matches("template<typename T> struct S { S(int);};" |
| 2321 |
"explicit(true) S(int) -> S<int>;", |
| 2322 |
cxxDeductionGuideDecl(isExplicit()))); |
| 2323 |
EXPECT_TRUE(notMatches("template<typename T> struct S { S(int);};" |
| 2324 |
"explicit(false) S(int) -> S<int>;", |
| 2325 |
cxxDeductionGuideDecl(isExplicit()))); |
| 2326 |
EXPECT_TRUE( |
| 2327 |
notMatches("template<typename T> struct S { S(int);};" |
| 2328 |
"template<bool b = true> explicit(b) S(int) -> S<int>;", |
| 2329 |
cxxDeductionGuideDecl(isExplicit()))); |
| 2330 |
} |
| 2331 |
|
| 2332 |
TEST_P(ASTMatchersTest, CXXConstructorDecl_Kinds) { |
| 2333 |
if (!GetParam().isCXX()) { |
| 2334 |
return; |
| 2335 |
} |
| 2336 |
|
| 2337 |
EXPECT_TRUE( |
| 2338 |
matches("struct S { S(); };", cxxConstructorDecl(isDefaultConstructor(), |
| 2339 |
unless(isImplicit())))); |
| 2340 |
EXPECT_TRUE(notMatches( |
| 2341 |
"struct S { S(); };", |
| 2342 |
cxxConstructorDecl(isCopyConstructor(), unless(isImplicit())))); |
| 2343 |
EXPECT_TRUE(notMatches( |
| 2344 |
"struct S { S(); };", |
| 2345 |
cxxConstructorDecl(isMoveConstructor(), unless(isImplicit())))); |
| 2346 |
|
| 2347 |
EXPECT_TRUE(notMatches( |
| 2348 |
"struct S { S(const S&); };", |
| 2349 |
cxxConstructorDecl(isDefaultConstructor(), unless(isImplicit())))); |
| 2350 |
EXPECT_TRUE( |
| 2351 |
matches("struct S { S(const S&); };", |
| 2352 |
cxxConstructorDecl(isCopyConstructor(), unless(isImplicit())))); |
| 2353 |
EXPECT_TRUE(notMatches( |
| 2354 |
"struct S { S(const S&); };", |
| 2355 |
cxxConstructorDecl(isMoveConstructor(), unless(isImplicit())))); |
| 2356 |
|
| 2357 |
EXPECT_TRUE(notMatches( |
| 2358 |
"struct S { S(S&&); };", |
| 2359 |
cxxConstructorDecl(isDefaultConstructor(), unless(isImplicit())))); |
| 2360 |
EXPECT_TRUE(notMatches( |
| 2361 |
"struct S { S(S&&); };", |
| 2362 |
cxxConstructorDecl(isCopyConstructor(), unless(isImplicit())))); |
| 2363 |
EXPECT_TRUE( |
| 2364 |
matches("struct S { S(S&&); };", |
| 2365 |
cxxConstructorDecl(isMoveConstructor(), unless(isImplicit())))); |
| 2366 |
} |
| 2367 |
|
| 2368 |
TEST_P(ASTMatchersTest, IsUserProvided) { |
| 2369 |
if (!GetParam().isCXX11OrLater()) { |
| 2370 |
return; |
| 2371 |
} |
| 2372 |
|
| 2373 |
EXPECT_TRUE(notMatches("struct S { int X = 0; };", |
| 2374 |
cxxConstructorDecl(isUserProvided()))); |
| 2375 |
EXPECT_TRUE(notMatches("struct S { S() = default; };", |
| 2376 |
cxxConstructorDecl(isUserProvided()))); |
| 2377 |
EXPECT_TRUE(notMatches("struct S { S() = delete; };", |
| 2378 |
cxxConstructorDecl(isUserProvided()))); |
| 2379 |
EXPECT_TRUE( |
| 2380 |
matches("struct S { S(); };", cxxConstructorDecl(isUserProvided()))); |
| 2381 |
EXPECT_TRUE(matches("struct S { S(); }; S::S(){}", |
| 2382 |
cxxConstructorDecl(isUserProvided()))); |
| 2383 |
} |
| 2384 |
|
| 2385 |
TEST_P(ASTMatchersTest, IsDelegatingConstructor) { |
| 2386 |
if (!GetParam().isCXX11OrLater()) { |
| 2387 |
return; |
| 2388 |
} |
| 2389 |
|
| 2390 |
EXPECT_TRUE(notMatches("struct S { S(); S(int); int X; };", |
| 2391 |
cxxConstructorDecl(isDelegatingConstructor()))); |
| 2392 |
EXPECT_TRUE(notMatches("struct S { S(){} S(int X) : X(X) {} int X; };", |
| 2393 |
cxxConstructorDecl(isDelegatingConstructor()))); |
| 2394 |
EXPECT_TRUE(matches( |
| 2395 |
"struct S { S() : S(0) {} S(int X) : X(X) {} int X; };", |
| 2396 |
cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(0)))); |
| 2397 |
EXPECT_TRUE(matches( |
| 2398 |
"struct S { S(); S(int X); int X; }; S::S(int X) : S() {}", |
| 2399 |
cxxConstructorDecl(isDelegatingConstructor(), parameterCountIs(1)))); |
| 2400 |
} |
| 2401 |
|
| 2402 |
TEST_P(ASTMatchersTest, HasSize) { |
| 2403 |
StatementMatcher Literal = stringLiteral(hasSize(4)); |
| 2404 |
EXPECT_TRUE(matches("const char *s = \"abcd\";", Literal)); |
| 2405 |
// with escaped characters |
| 2406 |
EXPECT_TRUE(matches("const char *s = \"\x05\x06\x07\x08\";", Literal)); |
| 2407 |
// no matching, too small |
| 2408 |
EXPECT_TRUE(notMatches("const char *s = \"ab\";", Literal)); |
| 2409 |
} |
| 2410 |
|
| 2411 |
TEST_P(ASTMatchersTest, HasSize_CXX) { |
| 2412 |
if (!GetParam().isCXX()) { |
| 2413 |
// FIXME: Fix this test to also work in non-C++ language modes. |
| 2414 |
return; |
| 2415 |
} |
| 2416 |
|
| 2417 |
StatementMatcher Literal = stringLiteral(hasSize(4)); |
| 2418 |
// wide string |
| 2419 |
EXPECT_TRUE(matches("const wchar_t *s = L\"abcd\";", Literal)); |
| 2420 |
} |
| 2421 |
|
| 2422 |
TEST_P(ASTMatchersTest, HasName_MatchesNamespaces) { |
| 2423 |
if (!GetParam().isCXX()) { |
| 2424 |
return; |
| 2425 |
} |
| 2426 |
|
| 2427 |
EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 2428 |
recordDecl(hasName("a::b::C")))); |
| 2429 |
EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 2430 |
recordDecl(hasName("::a::b::C")))); |
| 2431 |
EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 2432 |
recordDecl(hasName("b::C")))); |
| 2433 |
EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 2434 |
recordDecl(hasName("C")))); |
| 2435 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2436 |
recordDecl(hasName("c::b::C")))); |
| 2437 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2438 |
recordDecl(hasName("a::c::C")))); |
| 2439 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2440 |
recordDecl(hasName("a::b::A")))); |
| 2441 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2442 |
recordDecl(hasName("::C")))); |
| 2443 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2444 |
recordDecl(hasName("::b::C")))); |
| 2445 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2446 |
recordDecl(hasName("z::a::b::C")))); |
| 2447 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 2448 |
recordDecl(hasName("a+b::C")))); |
| 2449 |
EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
| 2450 |
recordDecl(hasName("C")))); |
| 2451 |
} |
| 2452 |
|
| 2453 |
TEST_P(ASTMatchersTest, HasName_MatchesOuterClasses) { |
| 2454 |
if (!GetParam().isCXX()) { |
| 2455 |
return; |
| 2456 |
} |
| 2457 |
|
| 2458 |
EXPECT_TRUE(matches("class A { class B { class C; }; };", |
| 2459 |
recordDecl(hasName("A::B::C")))); |
| 2460 |
EXPECT_TRUE(matches("class A { class B { class C; }; };", |
| 2461 |
recordDecl(hasName("::A::B::C")))); |
| 2462 |
EXPECT_TRUE(matches("class A { class B { class C; }; };", |
| 2463 |
recordDecl(hasName("B::C")))); |
| 2464 |
EXPECT_TRUE( |
| 2465 |
matches("class A { class B { class C; }; };", recordDecl(hasName("C")))); |
| 2466 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2467 |
recordDecl(hasName("c::B::C")))); |
| 2468 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2469 |
recordDecl(hasName("A::c::C")))); |
| 2470 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2471 |
recordDecl(hasName("A::B::A")))); |
| 2472 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2473 |
recordDecl(hasName("::C")))); |
| 2474 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2475 |
recordDecl(hasName("::B::C")))); |
| 2476 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2477 |
recordDecl(hasName("z::A::B::C")))); |
| 2478 |
EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 2479 |
recordDecl(hasName("A+B::C")))); |
| 2480 |
} |
| 2481 |
|
| 2482 |
TEST_P(ASTMatchersTest, HasName_MatchesInlinedNamespaces) { |
| 2483 |
if (!GetParam().isCXX()) { |
| 2484 |
return; |
| 2485 |
} |
| 2486 |
|
| 2487 |
StringRef code = "namespace a { inline namespace b { class C; } }"; |
| 2488 |
EXPECT_TRUE(matches(code, recordDecl(hasName("a::b::C")))); |
| 2489 |
EXPECT_TRUE(matches(code, recordDecl(hasName("a::C")))); |
| 2490 |
EXPECT_TRUE(matches(code, recordDecl(hasName("::a::b::C")))); |
| 2491 |
EXPECT_TRUE(matches(code, recordDecl(hasName("::a::C")))); |
| 2492 |
} |
| 2493 |
|
| 2494 |
TEST_P(ASTMatchersTest, HasName_MatchesAnonymousNamespaces) { |
| 2495 |
if (!GetParam().isCXX()) { |
| 2496 |
return; |
| 2497 |
} |
| 2498 |
|
| 2499 |
StringRef code = "namespace a { namespace { class C; } }"; |
| 2500 |
EXPECT_TRUE( |
| 2501 |
matches(code, recordDecl(hasName("a::(anonymous namespace)::C")))); |
| 2502 |
EXPECT_TRUE(matches(code, recordDecl(hasName("a::C")))); |
| 2503 |
EXPECT_TRUE( |
| 2504 |
matches(code, recordDecl(hasName("::a::(anonymous namespace)::C")))); |
| 2505 |
EXPECT_TRUE(matches(code, recordDecl(hasName("::a::C")))); |
| 2506 |
} |
| 2507 |
|
| 2508 |
TEST_P(ASTMatchersTest, HasName_MatchesAnonymousOuterClasses) { |
| 2509 |
if (!GetParam().isCXX()) { |
| 2510 |
return; |
| 2511 |
} |
| 2512 |
|
| 2513 |
EXPECT_TRUE(matches("class A { class { class C; } x; };", |
| 2514 |
recordDecl(hasName("A::(anonymous class)::C")))); |
| 2515 |
EXPECT_TRUE(matches("class A { class { class C; } x; };", |
| 2516 |
recordDecl(hasName("::A::(anonymous class)::C")))); |
| 2517 |
EXPECT_FALSE(matches("class A { class { class C; } x; };", |
| 2518 |
recordDecl(hasName("::A::C")))); |
| 2519 |
EXPECT_TRUE(matches("class A { struct { class C; } x; };", |
| 2520 |
recordDecl(hasName("A::(anonymous struct)::C")))); |
| 2521 |
EXPECT_TRUE(matches("class A { struct { class C; } x; };", |
| 2522 |
recordDecl(hasName("::A::(anonymous struct)::C")))); |
| 2523 |
EXPECT_FALSE(matches("class A { struct { class C; } x; };", |
| 2524 |
recordDecl(hasName("::A::C")))); |
| 2525 |
} |
| 2526 |
|
| 2527 |
TEST_P(ASTMatchersTest, HasName_MatchesFunctionScope) { |
| 2528 |
if (!GetParam().isCXX()) { |
| 2529 |
return; |
| 2530 |
} |
| 2531 |
|
| 2532 |
StringRef code = |
| 2533 |
"namespace a { void F(int a) { struct S { int m; }; int i; } }"; |
| 2534 |
EXPECT_TRUE(matches(code, varDecl(hasName("i")))); |
| 2535 |
EXPECT_FALSE(matches(code, varDecl(hasName("F()::i")))); |
| 2536 |
|
| 2537 |
EXPECT_TRUE(matches(code, fieldDecl(hasName("m")))); |
| 2538 |
EXPECT_TRUE(matches(code, fieldDecl(hasName("S::m")))); |
| 2539 |
EXPECT_TRUE(matches(code, fieldDecl(hasName("F(int)::S::m")))); |
| 2540 |
EXPECT_TRUE(matches(code, fieldDecl(hasName("a::F(int)::S::m")))); |
| 2541 |
EXPECT_TRUE(matches(code, fieldDecl(hasName("::a::F(int)::S::m")))); |
| 2542 |
} |
| 2543 |
|
| 2544 |
TEST_P(ASTMatchersTest, HasName_QualifiedStringMatchesThroughLinkage) { |
| 2545 |
if (!GetParam().isCXX()) { |
| 2546 |
return; |
| 2547 |
} |
| 2548 |
|
| 2549 |
// https://bugs.llvm.org/show_bug.cgi?id=42193 |
| 2550 |
StringRef code = R"cpp(namespace foo { extern "C" void test(); })cpp"; |
| 2551 |
EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); |
| 2552 |
EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); |
| 2553 |
EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); |
| 2554 |
EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); |
| 2555 |
|
| 2556 |
code = R"cpp(namespace foo { extern "C" { void test(); } })cpp"; |
| 2557 |
EXPECT_TRUE(matches(code, functionDecl(hasName("test")))); |
| 2558 |
EXPECT_TRUE(matches(code, functionDecl(hasName("foo::test")))); |
| 2559 |
EXPECT_TRUE(matches(code, functionDecl(hasName("::foo::test")))); |
| 2560 |
EXPECT_TRUE(notMatches(code, functionDecl(hasName("::test")))); |
| 2561 |
} |
| 2562 |
|
| 2563 |
TEST_P(ASTMatchersTest, HasAnyName) { |
| 2564 |
if (!GetParam().isCXX()) { |
| 2565 |
// FIXME: Add a test for `hasAnyName()` that does not depend on C++. |
| 2566 |
return; |
| 2567 |
} |
| 2568 |
|
| 2569 |
StringRef Code = "namespace a { namespace b { class C; } }"; |
| 2570 |
|
| 2571 |
EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX", "a::b::C")))); |
| 2572 |
EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("a::b::C", "XX")))); |
| 2573 |
EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX::C", "a::b::C")))); |
| 2574 |
EXPECT_TRUE(matches(Code, recordDecl(hasAnyName("XX", "C")))); |
| 2575 |
|
| 2576 |
EXPECT_TRUE(notMatches(Code, recordDecl(hasAnyName("::C", "::b::C")))); |
| 2577 |
EXPECT_TRUE( |
| 2578 |
matches(Code, recordDecl(hasAnyName("::C", "::b::C", "::a::b::C")))); |
| 2579 |
|
| 2580 |
std::vector<StringRef> Names = {"::C", "::b::C", "::a::b::C"}; |
| 2581 |
EXPECT_TRUE(matches(Code, recordDecl(hasAnyName(Names)))); |
| 2582 |
} |
| 2583 |
|
| 2584 |
TEST_P(ASTMatchersTest, IsDefinition) { |
| 2585 |
DeclarationMatcher DefinitionOfClassA = |
| 2586 |
recordDecl(hasName("A"), isDefinition()); |
| 2587 |
EXPECT_TRUE(matches("struct A {};", DefinitionOfClassA)); |
| 2588 |
EXPECT_TRUE(notMatches("struct A;", DefinitionOfClassA)); |
| 2589 |
|
| 2590 |
DeclarationMatcher DefinitionOfVariableA = |
| 2591 |
varDecl(hasName("a"), isDefinition()); |
| 2592 |
EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 2593 |
EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 2594 |
} |
| 2595 |
|
| 2596 |
TEST_P(ASTMatchersTest, IsDefinition_CXX) { |
| 2597 |
if (!GetParam().isCXX()) { |
| 2598 |
return; |
| 2599 |
} |
| 2600 |
|
| 2601 |
DeclarationMatcher DefinitionOfMethodA = |
| 2602 |
cxxMethodDecl(hasName("a"), isDefinition()); |
| 2603 |
EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 2604 |
EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 2605 |
|
| 2606 |
DeclarationMatcher DefinitionOfObjCMethodA = |
| 2607 |
objcMethodDecl(hasName("a"), isDefinition()); |
| 2608 |
EXPECT_TRUE(matchesObjC("@interface A @end " |
| 2609 |
"@implementation A; -(void)a {} @end", |
| 2610 |
DefinitionOfObjCMethodA)); |
| 2611 |
EXPECT_TRUE( |
| 2612 |
notMatchesObjC("@interface A; - (void)a; @end", DefinitionOfObjCMethodA)); |
| 2613 |
} |
| 2614 |
|
| 2615 |
TEST_P(ASTMatchersTest, HandlesNullQualTypes) { |
| 2616 |
if (!GetParam().isCXX()) { |
| 2617 |
// FIXME: Add an equivalent test that does not depend on C++. |
| 2618 |
return; |
| 2619 |
} |
| 2620 |
|
| 2621 |
// FIXME: Add a Type matcher so we can replace uses of this |
| 2622 |
// variable with Type(True()) |
| 2623 |
const TypeMatcher AnyType = anything(); |
| 2624 |
|
| 2625 |
// We don't really care whether this matcher succeeds; we're testing that |
| 2626 |
// it completes without crashing. |
| 2627 |
EXPECT_TRUE(matches( |
| 2628 |
"struct A { };" |
| 2629 |
"template <typename T>" |
| 2630 |
"void f(T t) {" |
| 2631 |
" T local_t(t /* this becomes a null QualType in the AST */);" |
| 2632 |
"}" |
| 2633 |
"void g() {" |
| 2634 |
" f(0);" |
| 2635 |
"}", |
| 2636 |
expr(hasType(TypeMatcher(anyOf(TypeMatcher(hasDeclaration(anything())), |
| 2637 |
pointsTo(AnyType), references(AnyType) |
| 2638 |
// Other QualType matchers should go here. |
| 2639 |
)))))); |
| 2640 |
} |
| 2641 |
|
| 2642 |
TEST_P(ASTMatchersTest, ObjCIvarRefExpr) { |
| 2643 |
StringRef ObjCString = |
| 2644 |
"@interface A @end " |
| 2645 |
"@implementation A { A *x; } - (void) func { x = 0; } @end"; |
| 2646 |
EXPECT_TRUE(matchesObjC(ObjCString, objcIvarRefExpr())); |
| 2647 |
EXPECT_TRUE(matchesObjC( |
| 2648 |
ObjCString, objcIvarRefExpr(hasDeclaration(namedDecl(hasName("x")))))); |
| 2649 |
EXPECT_FALSE(matchesObjC( |
| 2650 |
ObjCString, objcIvarRefExpr(hasDeclaration(namedDecl(hasName("y")))))); |
| 2651 |
} |
| 2652 |
|
| 2653 |
TEST_P(ASTMatchersTest, BlockExpr) { |
| 2654 |
EXPECT_TRUE(matchesObjC("void f() { ^{}(); }", blockExpr())); |
| 2655 |
} |
| 2656 |
|
| 2657 |
TEST_P(ASTMatchersTest, |
| 2658 |
StatementCountIs_FindsNoStatementsInAnEmptyCompoundStatement) { |
| 2659 |
EXPECT_TRUE(matches("void f() { }", compoundStmt(statementCountIs(0)))); |
| 2660 |
EXPECT_TRUE(notMatches("void f() {}", compoundStmt(statementCountIs(1)))); |
| 2661 |
} |
| 2662 |
|
| 2663 |
TEST_P(ASTMatchersTest, StatementCountIs_AppearsToMatchOnlyOneCount) { |
| 2664 |
EXPECT_TRUE(matches("void f() { 1; }", compoundStmt(statementCountIs(1)))); |
| 2665 |
EXPECT_TRUE(notMatches("void f() { 1; }", compoundStmt(statementCountIs(0)))); |
| 2666 |
EXPECT_TRUE(notMatches("void f() { 1; }", compoundStmt(statementCountIs(2)))); |
| 2667 |
} |
| 2668 |
|
| 2669 |
TEST_P(ASTMatchersTest, StatementCountIs_WorksWithMultipleStatements) { |
| 2670 |
EXPECT_TRUE( |
| 2671 |
matches("void f() { 1; 2; 3; }", compoundStmt(statementCountIs(3)))); |
| 2672 |
} |
| 2673 |
|
| 2674 |
TEST_P(ASTMatchersTest, StatementCountIs_WorksWithNestedCompoundStatements) { |
| 2675 |
EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 2676 |
compoundStmt(statementCountIs(1)))); |
| 2677 |
EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 2678 |
compoundStmt(statementCountIs(2)))); |
| 2679 |
EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 2680 |
compoundStmt(statementCountIs(3)))); |
| 2681 |
EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 2682 |
compoundStmt(statementCountIs(4)))); |
| 2683 |
} |
| 2684 |
|
| 2685 |
TEST_P(ASTMatchersTest, Member_WorksInSimplestCase) { |
| 2686 |
if (!GetParam().isCXX()) { |
| 2687 |
// FIXME: Add a test for `member()` that does not depend on C++. |
| 2688 |
return; |
| 2689 |
} |
| 2690 |
EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
| 2691 |
memberExpr(member(hasName("first"))))); |
| 2692 |
} |
| 2693 |
|
| 2694 |
TEST_P(ASTMatchersTest, Member_DoesNotMatchTheBaseExpression) { |
| 2695 |
if (!GetParam().isCXX()) { |
| 2696 |
// FIXME: Add a test for `member()` that does not depend on C++. |
| 2697 |
return; |
| 2698 |
} |
| 2699 |
|
| 2700 |
// Don't pick out the wrong part of the member expression, this should |
| 2701 |
// be checking the member (name) only. |
| 2702 |
EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
| 2703 |
memberExpr(member(hasName("first"))))); |
| 2704 |
} |
| 2705 |
|
| 2706 |
TEST_P(ASTMatchersTest, Member_MatchesInMemberFunctionCall) { |
| 2707 |
if (!GetParam().isCXX()) { |
| 2708 |
return; |
| 2709 |
} |
| 2710 |
|
| 2711 |
EXPECT_TRUE(matches("void f() {" |
| 2712 |
" struct { void first() {}; } s;" |
| 2713 |
" s.first();" |
| 2714 |
"};", |
| 2715 |
memberExpr(member(hasName("first"))))); |
| 2716 |
} |
| 2717 |
|
| 2718 |
TEST_P(ASTMatchersTest, FieldDecl) { |
| 2719 |
EXPECT_TRUE( |
| 2720 |
matches("struct A { int i; }; void f() { struct A a; a.i = 2; }", |
| 2721 |
memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2722 |
EXPECT_TRUE( |
| 2723 |
notMatches("struct A { float f; }; void f() { struct A a; a.f = 2.0f; }", |
| 2724 |
memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2725 |
} |
| 2726 |
|
| 2727 |
TEST_P(ASTMatchersTest, IsBitField) { |
| 2728 |
EXPECT_TRUE(matches("struct C { int a : 2; int b; };", |
| 2729 |
fieldDecl(isBitField(), hasName("a")))); |
| 2730 |
EXPECT_TRUE(notMatches("struct C { int a : 2; int b; };", |
| 2731 |
fieldDecl(isBitField(), hasName("b")))); |
| 2732 |
EXPECT_TRUE(matches("struct C { int a : 2; int b : 4; };", |
| 2733 |
fieldDecl(isBitField(), hasBitWidth(2), hasName("a")))); |
| 2734 |
} |
| 2735 |
|
| 2736 |
TEST_P(ASTMatchersTest, HasInClassInitializer) { |
| 2737 |
if (!GetParam().isCXX()) { |
| 2738 |
return; |
| 2739 |
} |
| 2740 |
|
| 2741 |
EXPECT_TRUE( |
| 2742 |
matches("class C { int a = 2; int b; };", |
| 2743 |
fieldDecl(hasInClassInitializer(integerLiteral(equals(2))), |
| 2744 |
hasName("a")))); |
| 2745 |
EXPECT_TRUE( |
| 2746 |
notMatches("class C { int a = 2; int b; };", |
| 2747 |
fieldDecl(hasInClassInitializer(anything()), hasName("b")))); |
| 2748 |
} |
| 2749 |
|
| 2750 |
TEST_P(ASTMatchersTest, IsPublic_IsProtected_IsPrivate) { |
| 2751 |
if (!GetParam().isCXX()) { |
| 2752 |
return; |
| 2753 |
} |
| 2754 |
|
| 2755 |
EXPECT_TRUE( |
| 2756 |
matches("struct A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2757 |
EXPECT_TRUE(notMatches("struct A { int i; };", |
| 2758 |
fieldDecl(isProtected(), hasName("i")))); |
| 2759 |
EXPECT_TRUE( |
| 2760 |
notMatches("struct A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2761 |
|
| 2762 |
EXPECT_TRUE( |
| 2763 |
notMatches("class A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2764 |
EXPECT_TRUE(notMatches("class A { int i; };", |
| 2765 |
fieldDecl(isProtected(), hasName("i")))); |
| 2766 |
EXPECT_TRUE( |
| 2767 |
matches("class A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2768 |
|
| 2769 |
EXPECT_TRUE(notMatches("class A { protected: int i; };", |
| 2770 |
fieldDecl(isPublic(), hasName("i")))); |
| 2771 |
EXPECT_TRUE(matches("class A { protected: int i; };", |
| 2772 |
fieldDecl(isProtected(), hasName("i")))); |
| 2773 |
EXPECT_TRUE(notMatches("class A { protected: int i; };", |
| 2774 |
fieldDecl(isPrivate(), hasName("i")))); |
| 2775 |
|
| 2776 |
// Non-member decls have the AccessSpecifier AS_none and thus aren't matched. |
| 2777 |
EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i")))); |
| 2778 |
EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i")))); |
| 2779 |
EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i")))); |
| 2780 |
} |
| 2781 |
|
| 2782 |
TEST_P(ASTMatchersTest, |
| 2783 |
HasDynamicExceptionSpec_MatchesDynamicExceptionSpecifications) { |
| 2784 |
if (!GetParam().supportsCXXDynamicExceptionSpecification()) { |
| 2785 |
return; |
| 2786 |
} |
| 2787 |
|
| 2788 |
EXPECT_TRUE(notMatches("void f();", functionDecl(hasDynamicExceptionSpec()))); |
| 2789 |
EXPECT_TRUE( |
| 2790 |
matches("void j() throw();", functionDecl(hasDynamicExceptionSpec()))); |
| 2791 |
EXPECT_TRUE( |
| 2792 |
matches("void k() throw(int);", functionDecl(hasDynamicExceptionSpec()))); |
| 2793 |
EXPECT_TRUE( |
| 2794 |
matches("void l() throw(...);", functionDecl(hasDynamicExceptionSpec()))); |
| 2795 |
|
| 2796 |
EXPECT_TRUE( |
| 2797 |
notMatches("void f();", functionProtoType(hasDynamicExceptionSpec()))); |
| 2798 |
EXPECT_TRUE(matches("void j() throw();", |
| 2799 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2800 |
EXPECT_TRUE(matches("void k() throw(int);", |
| 2801 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2802 |
EXPECT_TRUE(matches("void l() throw(...);", |
| 2803 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2804 |
} |
| 2805 |
|
| 2806 |
TEST_P(ASTMatchersTest, |
| 2807 |
HasDynamicExceptionSpec_MatchesDynamicExceptionSpecifications_CXX11) { |
| 2808 |
if (!GetParam().isCXX11OrLater()) { |
| 2809 |
return; |
| 2810 |
} |
| 2811 |
|
| 2812 |
EXPECT_TRUE(notMatches("void g() noexcept;", |
| 2813 |
functionDecl(hasDynamicExceptionSpec()))); |
| 2814 |
EXPECT_TRUE(notMatches("void h() noexcept(true);", |
| 2815 |
functionDecl(hasDynamicExceptionSpec()))); |
| 2816 |
EXPECT_TRUE(notMatches("void i() noexcept(false);", |
| 2817 |
functionDecl(hasDynamicExceptionSpec()))); |
| 2818 |
|
| 2819 |
EXPECT_TRUE(notMatches("void g() noexcept;", |
| 2820 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2821 |
EXPECT_TRUE(notMatches("void h() noexcept(true);", |
| 2822 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2823 |
EXPECT_TRUE(notMatches("void i() noexcept(false);", |
| 2824 |
functionProtoType(hasDynamicExceptionSpec()))); |
| 2825 |
} |
| 2826 |
|
| 2827 |
TEST_P(ASTMatchersTest, HasObjectExpression_DoesNotMatchMember) { |
| 2828 |
if (!GetParam().isCXX()) { |
| 2829 |
return; |
| 2830 |
} |
| 2831 |
|
| 2832 |
EXPECT_TRUE(notMatches( |
| 2833 |
"class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
| 2834 |
memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
| 2835 |
} |
| 2836 |
|
| 2837 |
TEST_P(ASTMatchersTest, HasObjectExpression_MatchesBaseOfVariable) { |
| 2838 |
EXPECT_TRUE(matches( |
| 2839 |
"struct X { int m; }; void f(struct X x) { x.m; }", |
| 2840 |
memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
| 2841 |
EXPECT_TRUE(matches("struct X { int m; }; void f(struct X* x) { x->m; }", |
| 2842 |
memberExpr(hasObjectExpression( |
| 2843 |
hasType(pointsTo(recordDecl(hasName("X")))))))); |
| 2844 |
} |
| 2845 |
|
| 2846 |
TEST_P(ASTMatchersTest, HasObjectExpression_MatchesBaseOfVariable_CXX) { |
| 2847 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 2848 |
// FIXME: Fix this test to work with delayed template parsing. |
| 2849 |
return; |
| 2850 |
} |
| 2851 |
|
| 2852 |
EXPECT_TRUE(matches("template <class T> struct X { void f() { T t; t.m; } };", |
| 2853 |
cxxDependentScopeMemberExpr(hasObjectExpression( |
| 2854 |
declRefExpr(to(namedDecl(hasName("t")))))))); |
| 2855 |
EXPECT_TRUE( |
| 2856 |
matches("template <class T> struct X { void f() { T t; t->m; } };", |
| 2857 |
cxxDependentScopeMemberExpr(hasObjectExpression( |
| 2858 |
declRefExpr(to(namedDecl(hasName("t")))))))); |
| 2859 |
} |
| 2860 |
|
| 2861 |
TEST_P(ASTMatchersTest, HasObjectExpression_MatchesBaseOfMemberFunc) { |
| 2862 |
if (!GetParam().isCXX()) { |
| 2863 |
return; |
| 2864 |
} |
| 2865 |
|
| 2866 |
EXPECT_TRUE(matches( |
| 2867 |
"struct X { void f(); }; void g(X x) { x.f(); }", |
| 2868 |
memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
| 2869 |
} |
| 2870 |
|
| 2871 |
TEST_P(ASTMatchersTest, HasObjectExpression_MatchesBaseOfMemberFunc_Template) { |
| 2872 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 2873 |
// FIXME: Fix this test to work with delayed template parsing. |
| 2874 |
return; |
| 2875 |
} |
| 2876 |
|
| 2877 |
EXPECT_TRUE(matches("struct X { template <class T> void f(); };" |
| 2878 |
"template <class T> void g(X x) { x.f<T>(); }", |
| 2879 |
unresolvedMemberExpr(hasObjectExpression( |
| 2880 |
hasType(recordDecl(hasName("X"))))))); |
| 2881 |
EXPECT_TRUE(matches("template <class T> void f(T t) { t.g(); }", |
| 2882 |
cxxDependentScopeMemberExpr(hasObjectExpression( |
| 2883 |
declRefExpr(to(namedDecl(hasName("t")))))))); |
| 2884 |
} |
| 2885 |
|
| 2886 |
TEST_P(ASTMatchersTest, HasObjectExpression_ImplicitlyFormedMemberExpression) { |
| 2887 |
if (!GetParam().isCXX()) { |
| 2888 |
return; |
| 2889 |
} |
| 2890 |
|
| 2891 |
EXPECT_TRUE(matches("class X {}; struct S { X m; void f() { this->m; } };", |
| 2892 |
memberExpr(hasObjectExpression( |
| 2893 |
hasType(pointsTo(recordDecl(hasName("S")))))))); |
| 2894 |
EXPECT_TRUE(matches("class X {}; struct S { X m; void f() { m; } };", |
| 2895 |
memberExpr(hasObjectExpression( |
| 2896 |
hasType(pointsTo(recordDecl(hasName("S")))))))); |
| 2897 |
} |
| 2898 |
|
| 2899 |
TEST_P(ASTMatchersTest, FieldDecl_DoesNotMatchNonFieldMembers) { |
| 2900 |
if (!GetParam().isCXX()) { |
| 2901 |
return; |
| 2902 |
} |
| 2903 |
|
| 2904 |
EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m")))); |
| 2905 |
EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m")))); |
| 2906 |
EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m")))); |
| 2907 |
EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m")))); |
| 2908 |
} |
| 2909 |
|
| 2910 |
TEST_P(ASTMatchersTest, FieldDecl_MatchesField) { |
| 2911 |
EXPECT_TRUE(matches("struct X { int m; };", fieldDecl(hasName("m")))); |
| 2912 |
} |
| 2913 |
|
| 2914 |
TEST_P(ASTMatchersTest, IsVolatileQualified) { |
| 2915 |
EXPECT_TRUE( |
| 2916 |
matches("volatile int i = 42;", varDecl(hasType(isVolatileQualified())))); |
| 2917 |
EXPECT_TRUE( |
| 2918 |
notMatches("volatile int *i;", varDecl(hasType(isVolatileQualified())))); |
| 2919 |
EXPECT_TRUE(matches("typedef volatile int v_int; v_int i = 42;", |
| 2920 |
varDecl(hasType(isVolatileQualified())))); |
| 2921 |
} |
| 2922 |
|
| 2923 |
TEST_P(ASTMatchersTest, IsConstQualified_MatchesConstInt) { |
| 2924 |
EXPECT_TRUE( |
| 2925 |
matches("const int i = 42;", varDecl(hasType(isConstQualified())))); |
| 2926 |
} |
| 2927 |
|
| 2928 |
TEST_P(ASTMatchersTest, IsConstQualified_MatchesConstPointer) { |
| 2929 |
EXPECT_TRUE(matches("int i = 42; int* const p = &i;", |
| 2930 |
varDecl(hasType(isConstQualified())))); |
| 2931 |
} |
| 2932 |
|
| 2933 |
TEST_P(ASTMatchersTest, IsConstQualified_MatchesThroughTypedef) { |
| 2934 |
EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
| 2935 |
varDecl(hasType(isConstQualified())))); |
| 2936 |
EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p = ((int*)0);", |
| 2937 |
varDecl(hasType(isConstQualified())))); |
| 2938 |
} |
| 2939 |
|
| 2940 |
TEST_P(ASTMatchersTest, IsConstQualified_DoesNotMatchInappropriately) { |
| 2941 |
EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
| 2942 |
varDecl(hasType(isConstQualified())))); |
| 2943 |
EXPECT_TRUE( |
| 2944 |
notMatches("int const* p;", varDecl(hasType(isConstQualified())))); |
| 2945 |
} |
| 2946 |
|
| 2947 |
TEST_P(ASTMatchersTest, DeclCountIs_DeclCountIsCorrect) { |
| 2948 |
EXPECT_TRUE(matches("void f() {int i,j;}", declStmt(declCountIs(2)))); |
| 2949 |
EXPECT_TRUE( |
| 2950 |
notMatches("void f() {int i,j; int k;}", declStmt(declCountIs(3)))); |
| 2951 |
EXPECT_TRUE( |
| 2952 |
notMatches("void f() {int i,j, k, l;}", declStmt(declCountIs(3)))); |
| 2953 |
} |
| 2954 |
|
| 2955 |
TEST_P(ASTMatchersTest, EachOf_TriggersForEachMatch) { |
| 2956 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2957 |
"class A { int a; int b; };", |
| 2958 |
recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2959 |
has(fieldDecl(hasName("b")).bind("v")))), |
| 2960 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 2))); |
| 2961 |
} |
| 2962 |
|
| 2963 |
TEST_P(ASTMatchersTest, EachOf_BehavesLikeAnyOfUnlessBothMatch) { |
| 2964 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2965 |
"struct A { int a; int c; };", |
| 2966 |
recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2967 |
has(fieldDecl(hasName("b")).bind("v")))), |
| 2968 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 1))); |
| 2969 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2970 |
"struct A { int c; int b; };", |
| 2971 |
recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2972 |
has(fieldDecl(hasName("b")).bind("v")))), |
| 2973 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 1))); |
| 2974 |
EXPECT_TRUE( |
| 2975 |
notMatches("struct A { int c; int d; };", |
| 2976 |
recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2977 |
has(fieldDecl(hasName("b")).bind("v")))))); |
| 2978 |
} |
| 2979 |
|
| 2980 |
TEST_P(ASTMatchersTest, Optionally_SubmatchersDoNotMatch) { |
| 2981 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 2982 |
"class A { int a; int b; };", |
| 2983 |
recordDecl(optionally(has(fieldDecl(hasName("c")).bind("c")))), |
| 2984 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("c"))); |
| 2985 |
} |
| 2986 |
|
| 2987 |
// Regression test. |
| 2988 |
TEST_P(ASTMatchersTest, Optionally_SubmatchersDoNotMatchButPreserveBindings) { |
| 2989 |
StringRef Code = "class A { int a; int b; };"; |
| 2990 |
auto Matcher = recordDecl(decl().bind("decl"), |
| 2991 |
optionally(has(fieldDecl(hasName("c")).bind("v")))); |
| 2992 |
// "decl" is still bound. |
| 2993 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2994 |
Code, Matcher, std::make_unique<VerifyIdIsBoundTo<RecordDecl>>("decl"))); |
| 2995 |
// "v" is not bound, but the match still suceeded. |
| 2996 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 2997 |
Code, Matcher, std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v"))); |
| 2998 |
} |
| 2999 |
|
| 3000 |
TEST_P(ASTMatchersTest, Optionally_SubmatchersMatch) { |
| 3001 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3002 |
"class A { int a; int c; };", |
| 3003 |
recordDecl(optionally(has(fieldDecl(hasName("a")).bind("v")))), |
| 3004 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v"))); |
| 3005 |
} |
| 3006 |
|
| 3007 |
TEST_P(ASTMatchersTest, |
| 3008 |
IsTemplateInstantiation_MatchesImplicitClassTemplateInstantiation) { |
| 3009 |
if (!GetParam().isCXX()) { |
| 3010 |
return; |
| 3011 |
} |
| 3012 |
|
| 3013 |
// Make sure that we can both match the class by name (::X) and by the type |
| 3014 |
// the template was instantiated with (via a field). |
| 3015 |
|
| 3016 |
EXPECT_TRUE( |
| 3017 |
matches("template <typename T> class X {}; class A {}; X<A> x;", |
| 3018 |
cxxRecordDecl(hasName("::X"), isTemplateInstantiation()))); |
| 3019 |
|
| 3020 |
EXPECT_TRUE(matches( |
| 3021 |
"template <typename T> class X { T t; }; class A {}; X<A> x;", |
| 3022 |
cxxRecordDecl( |
| 3023 |
isTemplateInstantiation(), |
| 3024 |
hasDescendant(fieldDecl(hasType(recordDecl(hasName("A")))))))); |
| 3025 |
} |
| 3026 |
|
| 3027 |
TEST_P(ASTMatchersTest, |
| 3028 |
IsTemplateInstantiation_MatchesImplicitFunctionTemplateInstantiation) { |
| 3029 |
if (!GetParam().isCXX()) { |
| 3030 |
return; |
| 3031 |
} |
| 3032 |
|
| 3033 |
EXPECT_TRUE(matches( |
| 3034 |
"template <typename T> void f(T t) {} class A {}; void g() { f(A()); }", |
| 3035 |
functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), |
| 3036 |
isTemplateInstantiation()))); |
| 3037 |
} |
| 3038 |
|
| 3039 |
TEST_P(ASTMatchersTest, |
| 3040 |
IsTemplateInstantiation_MatchesExplicitClassTemplateInstantiation) { |
| 3041 |
if (!GetParam().isCXX()) { |
| 3042 |
return; |
| 3043 |
} |
| 3044 |
|
| 3045 |
EXPECT_TRUE(matches("template <typename T> class X { T t; }; class A {};" |
| 3046 |
"template class X<A>;", |
| 3047 |
cxxRecordDecl(isTemplateInstantiation(), |
| 3048 |
hasDescendant(fieldDecl( |
| 3049 |
hasType(recordDecl(hasName("A")))))))); |
| 3050 |
|
| 3051 |
// Make sure that we match the instantiation instead of the template |
| 3052 |
// definition by checking whether the member function is present. |
| 3053 |
EXPECT_TRUE( |
| 3054 |
matches("template <typename T> class X { void f() { T t; } };" |
| 3055 |
"extern template class X<int>;", |
| 3056 |
cxxRecordDecl(isTemplateInstantiation(), |
| 3057 |
unless(hasDescendant(varDecl(hasName("t"))))))); |
| 3058 |
} |
| 3059 |
|
| 3060 |
TEST_P( |
| 3061 |
ASTMatchersTest, |
| 3062 |
IsTemplateInstantiation_MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 3063 |
if (!GetParam().isCXX()) { |
| 3064 |
return; |
| 3065 |
} |
| 3066 |
|
| 3067 |
EXPECT_TRUE( |
| 3068 |
matches("template <typename T> class X {};" |
| 3069 |
"template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
| 3070 |
cxxRecordDecl(hasName("::X"), isTemplateInstantiation()))); |
| 3071 |
} |
| 3072 |
|
| 3073 |
TEST_P( |
| 3074 |
ASTMatchersTest, |
| 3075 |
IsTemplateInstantiation_MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 3076 |
if (!GetParam().isCXX()) { |
| 3077 |
return; |
| 3078 |
} |
| 3079 |
|
| 3080 |
EXPECT_TRUE( |
| 3081 |
matches("class A {};" |
| 3082 |
"class X {" |
| 3083 |
" template <typename U> class Y { U u; };" |
| 3084 |
" Y<A> y;" |
| 3085 |
"};", |
| 3086 |
cxxRecordDecl(hasName("::X::Y"), isTemplateInstantiation()))); |
| 3087 |
} |
| 3088 |
|
| 3089 |
TEST_P( |
| 3090 |
ASTMatchersTest, |
| 3091 |
IsTemplateInstantiation_DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 3092 |
if (!GetParam().isCXX()) { |
| 3093 |
return; |
| 3094 |
} |
| 3095 |
|
| 3096 |
// FIXME: Figure out whether this makes sense. It doesn't affect the |
| 3097 |
// normal use case as long as the uppermost instantiation always is marked |
| 3098 |
// as template instantiation, but it might be confusing as a predicate. |
| 3099 |
EXPECT_TRUE(matches( |
| 3100 |
"class A {};" |
| 3101 |
"template <typename T> class X {" |
| 3102 |
" template <typename U> class Y { U u; };" |
| 3103 |
" Y<T> y;" |
| 3104 |
"}; X<A> x;", |
| 3105 |
cxxRecordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
| 3106 |
} |
| 3107 |
|
| 3108 |
TEST_P( |
| 3109 |
ASTMatchersTest, |
| 3110 |
IsTemplateInstantiation_DoesNotMatchExplicitClassTemplateSpecialization) { |
| 3111 |
if (!GetParam().isCXX()) { |
| 3112 |
return; |
| 3113 |
} |
| 3114 |
|
| 3115 |
EXPECT_TRUE( |
| 3116 |
notMatches("template <typename T> class X {}; class A {};" |
| 3117 |
"template <> class X<A> {}; X<A> x;", |
| 3118 |
cxxRecordDecl(hasName("::X"), isTemplateInstantiation()))); |
| 3119 |
} |
| 3120 |
|
| 3121 |
TEST_P(ASTMatchersTest, IsTemplateInstantiation_DoesNotMatchNonTemplate) { |
| 3122 |
if (!GetParam().isCXX()) { |
| 3123 |
return; |
| 3124 |
} |
| 3125 |
|
| 3126 |
EXPECT_TRUE(notMatches("class A {}; class Y { A a; };", |
| 3127 |
cxxRecordDecl(isTemplateInstantiation()))); |
| 3128 |
} |
| 3129 |
|
| 3130 |
TEST_P(ASTMatchersTest, IsInstantiated_MatchesInstantiation) { |
| 3131 |
if (!GetParam().isCXX()) { |
| 3132 |
return; |
| 3133 |
} |
| 3134 |
|
| 3135 |
EXPECT_TRUE( |
| 3136 |
matches("template<typename T> class A { T i; }; class Y { A<int> a; };", |
| 3137 |
cxxRecordDecl(isInstantiated()))); |
| 3138 |
} |
| 3139 |
|
| 3140 |
TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesDefinition) { |
| 3141 |
if (!GetParam().isCXX()) { |
| 3142 |
return; |
| 3143 |
} |
| 3144 |
|
| 3145 |
EXPECT_TRUE(notMatches("template<typename T> class A { T i; };", |
| 3146 |
cxxRecordDecl(isInstantiated()))); |
| 3147 |
} |
| 3148 |
|
| 3149 |
TEST_P(ASTMatchersTest, IsInTemplateInstantiation_MatchesInstantiationStmt) { |
| 3150 |
if (!GetParam().isCXX()) { |
| 3151 |
return; |
| 3152 |
} |
| 3153 |
|
| 3154 |
EXPECT_TRUE(matches("template<typename T> struct A { A() { T i; } };" |
| 3155 |
"class Y { A<int> a; }; Y y;", |
| 3156 |
declStmt(isInTemplateInstantiation()))); |
| 3157 |
} |
| 3158 |
|
| 3159 |
TEST_P(ASTMatchersTest, IsInTemplateInstantiation_NotMatchesDefinitionStmt) { |
| 3160 |
if (!GetParam().isCXX()) { |
| 3161 |
return; |
| 3162 |
} |
| 3163 |
|
| 3164 |
EXPECT_TRUE(notMatches("template<typename T> struct A { void x() { T i; } };", |
| 3165 |
declStmt(isInTemplateInstantiation()))); |
| 3166 |
} |
| 3167 |
|
| 3168 |
TEST_P(ASTMatchersTest, IsInstantiated_MatchesFunctionInstantiation) { |
| 3169 |
if (!GetParam().isCXX()) { |
| 3170 |
return; |
| 3171 |
} |
| 3172 |
|
| 3173 |
EXPECT_TRUE( |
| 3174 |
matches("template<typename T> void A(T t) { T i; } void x() { A(0); }", |
| 3175 |
functionDecl(isInstantiated()))); |
| 3176 |
} |
| 3177 |
|
| 3178 |
TEST_P(ASTMatchersTest, IsInstantiated_NotMatchesFunctionDefinition) { |
| 3179 |
if (!GetParam().isCXX()) { |
| 3180 |
return; |
| 3181 |
} |
| 3182 |
|
| 3183 |
EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }", |
| 3184 |
varDecl(isInstantiated()))); |
| 3185 |
} |
| 3186 |
|
| 3187 |
TEST_P(ASTMatchersTest, |
| 3188 |
IsInTemplateInstantiation_MatchesFunctionInstantiationStmt) { |
| 3189 |
if (!GetParam().isCXX()) { |
| 3190 |
return; |
| 3191 |
} |
| 3192 |
|
| 3193 |
EXPECT_TRUE( |
| 3194 |
matches("template<typename T> void A(T t) { T i; } void x() { A(0); }", |
| 3195 |
declStmt(isInTemplateInstantiation()))); |
| 3196 |
} |
| 3197 |
|
| 3198 |
TEST_P(ASTMatchersTest, |
| 3199 |
IsInTemplateInstantiation_NotMatchesFunctionDefinitionStmt) { |
| 3200 |
if (!GetParam().isCXX()) { |
| 3201 |
return; |
| 3202 |
} |
| 3203 |
|
| 3204 |
EXPECT_TRUE(notMatches("template<typename T> void A(T t) { T i; }", |
| 3205 |
declStmt(isInTemplateInstantiation()))); |
| 3206 |
} |
| 3207 |
|
| 3208 |
TEST_P(ASTMatchersTest, IsInTemplateInstantiation_Sharing) { |
| 3209 |
if (!GetParam().isCXX()) { |
| 3210 |
return; |
| 3211 |
} |
| 3212 |
|
| 3213 |
auto Matcher = binaryOperator(unless(isInTemplateInstantiation())); |
| 3214 |
// FIXME: Node sharing is an implementation detail, exposing it is ugly |
| 3215 |
// and makes the matcher behave in non-obvious ways. |
| 3216 |
EXPECT_TRUE(notMatches( |
| 3217 |
"int j; template<typename T> void A(T t) { j += 42; } void x() { A(0); }", |
| 3218 |
Matcher)); |
| 3219 |
EXPECT_TRUE(matches( |
| 3220 |
"int j; template<typename T> void A(T t) { j += t; } void x() { A(0); }", |
| 3221 |
Matcher)); |
| 3222 |
} |
| 3223 |
|
| 3224 |
TEST_P(ASTMatchersTest, IsInstantiationDependent_MatchesNonValueTypeDependent) { |
| 3225 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3226 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3227 |
return; |
| 3228 |
} |
| 3229 |
|
| 3230 |
EXPECT_TRUE(matches( |
| 3231 |
"template<typename T> void f() { (void) sizeof(sizeof(T() + T())); }", |
| 3232 |
expr(isInstantiationDependent()))); |
| 3233 |
} |
| 3234 |
|
| 3235 |
TEST_P(ASTMatchersTest, IsInstantiationDependent_MatchesValueDependent) { |
| 3236 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3237 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3238 |
return; |
| 3239 |
} |
| 3240 |
|
| 3241 |
EXPECT_TRUE(matches("template<int T> int f() { return T; }", |
| 3242 |
expr(isInstantiationDependent()))); |
| 3243 |
} |
| 3244 |
|
| 3245 |
TEST_P(ASTMatchersTest, IsInstantiationDependent_MatchesTypeDependent) { |
| 3246 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3247 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3248 |
return; |
| 3249 |
} |
| 3250 |
|
| 3251 |
EXPECT_TRUE(matches("template<typename T> T f() { return T(); }", |
| 3252 |
expr(isInstantiationDependent()))); |
| 3253 |
} |
| 3254 |
|
| 3255 |
TEST_P(ASTMatchersTest, IsTypeDependent_MatchesTypeDependent) { |
| 3256 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3257 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3258 |
return; |
| 3259 |
} |
| 3260 |
|
| 3261 |
EXPECT_TRUE(matches("template<typename T> T f() { return T(); }", |
| 3262 |
expr(isTypeDependent()))); |
| 3263 |
} |
| 3264 |
|
| 3265 |
TEST_P(ASTMatchersTest, IsTypeDependent_NotMatchesValueDependent) { |
| 3266 |
if (!GetParam().isCXX()) { |
| 3267 |
return; |
| 3268 |
} |
| 3269 |
|
| 3270 |
EXPECT_TRUE(notMatches("template<int T> int f() { return T; }", |
| 3271 |
expr(isTypeDependent()))); |
| 3272 |
} |
| 3273 |
|
| 3274 |
TEST_P(ASTMatchersTest, IsValueDependent_MatchesValueDependent) { |
| 3275 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3276 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3277 |
return; |
| 3278 |
} |
| 3279 |
|
| 3280 |
EXPECT_TRUE(matches("template<int T> int f() { return T; }", |
| 3281 |
expr(isValueDependent()))); |
| 3282 |
} |
| 3283 |
|
| 3284 |
TEST_P(ASTMatchersTest, IsValueDependent_MatchesTypeDependent) { |
| 3285 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3286 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3287 |
return; |
| 3288 |
} |
| 3289 |
|
| 3290 |
EXPECT_TRUE(matches("template<typename T> T f() { return T(); }", |
| 3291 |
expr(isValueDependent()))); |
| 3292 |
} |
| 3293 |
|
| 3294 |
TEST_P(ASTMatchersTest, IsValueDependent_MatchesInstantiationDependent) { |
| 3295 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3296 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3297 |
return; |
| 3298 |
} |
| 3299 |
|
| 3300 |
EXPECT_TRUE(matches( |
| 3301 |
"template<typename T> void f() { (void) sizeof(sizeof(T() + T())); }", |
| 3302 |
expr(isValueDependent()))); |
| 3303 |
} |
| 3304 |
|
| 3305 |
TEST_P(ASTMatchersTest, |
| 3306 |
IsExplicitTemplateSpecialization_DoesNotMatchPrimaryTemplate) { |
| 3307 |
if (!GetParam().isCXX()) { |
| 3308 |
return; |
| 3309 |
} |
| 3310 |
|
| 3311 |
EXPECT_TRUE(notMatches("template <typename T> class X {};", |
| 3312 |
cxxRecordDecl(isExplicitTemplateSpecialization()))); |
| 3313 |
EXPECT_TRUE(notMatches("template <typename T> void f(T t);", |
| 3314 |
functionDecl(isExplicitTemplateSpecialization()))); |
| 3315 |
} |
| 3316 |
|
| 3317 |
TEST_P( |
| 3318 |
ASTMatchersTest, |
| 3319 |
IsExplicitTemplateSpecialization_DoesNotMatchExplicitTemplateInstantiations) { |
| 3320 |
if (!GetParam().isCXX()) { |
| 3321 |
return; |
| 3322 |
} |
| 3323 |
|
| 3324 |
EXPECT_TRUE( |
| 3325 |
notMatches("template <typename T> class X {};" |
| 3326 |
"template class X<int>; extern template class X<long>;", |
| 3327 |
cxxRecordDecl(isExplicitTemplateSpecialization()))); |
| 3328 |
EXPECT_TRUE( |
| 3329 |
notMatches("template <typename T> void f(T t) {}" |
| 3330 |
"template void f(int t); extern template void f(long t);", |
| 3331 |
functionDecl(isExplicitTemplateSpecialization()))); |
| 3332 |
} |
| 3333 |
|
| 3334 |
TEST_P( |
| 3335 |
ASTMatchersTest, |
| 3336 |
IsExplicitTemplateSpecialization_DoesNotMatchImplicitTemplateInstantiations) { |
| 3337 |
if (!GetParam().isCXX()) { |
| 3338 |
return; |
| 3339 |
} |
| 3340 |
|
| 3341 |
EXPECT_TRUE(notMatches("template <typename T> class X {}; X<int> x;", |
| 3342 |
cxxRecordDecl(isExplicitTemplateSpecialization()))); |
| 3343 |
EXPECT_TRUE( |
| 3344 |
notMatches("template <typename T> void f(T t); void g() { f(10); }", |
| 3345 |
functionDecl(isExplicitTemplateSpecialization()))); |
| 3346 |
} |
| 3347 |
|
| 3348 |
TEST_P( |
| 3349 |
ASTMatchersTest, |
| 3350 |
IsExplicitTemplateSpecialization_MatchesExplicitTemplateSpecializations) { |
| 3351 |
if (!GetParam().isCXX()) { |
| 3352 |
return; |
| 3353 |
} |
| 3354 |
|
| 3355 |
EXPECT_TRUE(matches("template <typename T> class X {};" |
| 3356 |
"template<> class X<int> {};", |
| 3357 |
cxxRecordDecl(isExplicitTemplateSpecialization()))); |
| 3358 |
EXPECT_TRUE(matches("template <typename T> void f(T t) {}" |
| 3359 |
"template<> void f(int t) {}", |
| 3360 |
functionDecl(isExplicitTemplateSpecialization()))); |
| 3361 |
} |
| 3362 |
|
| 3363 |
TEST_P(ASTMatchersTest, IsNoReturn) { |
| 3364 |
EXPECT_TRUE(notMatches("void func();", functionDecl(isNoReturn()))); |
| 3365 |
EXPECT_TRUE(notMatches("void func() {}", functionDecl(isNoReturn()))); |
| 3366 |
|
| 3367 |
EXPECT_TRUE(matches("__attribute__((noreturn)) void func();", |
| 3368 |
functionDecl(isNoReturn()))); |
| 3369 |
EXPECT_TRUE(matches("__attribute__((noreturn)) void func() {}", |
| 3370 |
functionDecl(isNoReturn()))); |
| 3371 |
|
| 3372 |
EXPECT_TRUE(matches("_Noreturn void func();", functionDecl(isNoReturn()))); |
| 3373 |
EXPECT_TRUE(matches("_Noreturn void func() {}", functionDecl(isNoReturn()))); |
| 3374 |
} |
| 3375 |
|
| 3376 |
TEST_P(ASTMatchersTest, IsNoReturn_CXX) { |
| 3377 |
if (!GetParam().isCXX()) { |
| 3378 |
return; |
| 3379 |
} |
| 3380 |
|
| 3381 |
EXPECT_TRUE( |
| 3382 |
notMatches("struct S { void func(); };", functionDecl(isNoReturn()))); |
| 3383 |
EXPECT_TRUE( |
| 3384 |
notMatches("struct S { void func() {} };", functionDecl(isNoReturn()))); |
| 3385 |
|
| 3386 |
EXPECT_TRUE(notMatches("struct S { static void func(); };", |
| 3387 |
functionDecl(isNoReturn()))); |
| 3388 |
EXPECT_TRUE(notMatches("struct S { static void func() {} };", |
| 3389 |
functionDecl(isNoReturn()))); |
| 3390 |
|
| 3391 |
EXPECT_TRUE(notMatches("struct S { S(); };", functionDecl(isNoReturn()))); |
| 3392 |
EXPECT_TRUE(notMatches("struct S { S() {} };", functionDecl(isNoReturn()))); |
| 3393 |
|
| 3394 |
// --- |
| 3395 |
|
| 3396 |
EXPECT_TRUE(matches("struct S { __attribute__((noreturn)) void func(); };", |
| 3397 |
functionDecl(isNoReturn()))); |
| 3398 |
EXPECT_TRUE(matches("struct S { __attribute__((noreturn)) void func() {} };", |
| 3399 |
functionDecl(isNoReturn()))); |
| 3400 |
|
| 3401 |
EXPECT_TRUE( |
| 3402 |
matches("struct S { __attribute__((noreturn)) static void func(); };", |
| 3403 |
functionDecl(isNoReturn()))); |
| 3404 |
EXPECT_TRUE( |
| 3405 |
matches("struct S { __attribute__((noreturn)) static void func() {} };", |
| 3406 |
functionDecl(isNoReturn()))); |
| 3407 |
|
| 3408 |
EXPECT_TRUE(matches("struct S { __attribute__((noreturn)) S(); };", |
| 3409 |
functionDecl(isNoReturn()))); |
| 3410 |
EXPECT_TRUE(matches("struct S { __attribute__((noreturn)) S() {} };", |
| 3411 |
functionDecl(isNoReturn()))); |
| 3412 |
} |
| 3413 |
|
| 3414 |
TEST_P(ASTMatchersTest, IsNoReturn_CXX11Attribute) { |
| 3415 |
if (!GetParam().isCXX11OrLater()) { |
| 3416 |
return; |
| 3417 |
} |
| 3418 |
|
| 3419 |
EXPECT_TRUE(matches("[[noreturn]] void func();", functionDecl(isNoReturn()))); |
| 3420 |
EXPECT_TRUE( |
| 3421 |
matches("[[noreturn]] void func() {}", functionDecl(isNoReturn()))); |
| 3422 |
|
| 3423 |
EXPECT_TRUE(matches("struct S { [[noreturn]] void func(); };", |
| 3424 |
functionDecl(isNoReturn()))); |
| 3425 |
EXPECT_TRUE(matches("struct S { [[noreturn]] void func() {} };", |
| 3426 |
functionDecl(isNoReturn()))); |
| 3427 |
|
| 3428 |
EXPECT_TRUE(matches("struct S { [[noreturn]] static void func(); };", |
| 3429 |
functionDecl(isNoReturn()))); |
| 3430 |
EXPECT_TRUE(matches("struct S { [[noreturn]] static void func() {} };", |
| 3431 |
functionDecl(isNoReturn()))); |
| 3432 |
|
| 3433 |
EXPECT_TRUE( |
| 3434 |
matches("struct S { [[noreturn]] S(); };", functionDecl(isNoReturn()))); |
| 3435 |
EXPECT_TRUE( |
| 3436 |
matches("struct S { [[noreturn]] S() {} };", functionDecl(isNoReturn()))); |
| 3437 |
} |
| 3438 |
|
| 3439 |
TEST_P(ASTMatchersTest, BooleanType) { |
| 3440 |
if (!GetParam().isCXX()) { |
| 3441 |
// FIXME: Add a test for `booleanType()` that does not depend on C++. |
| 3442 |
return; |
| 3443 |
} |
| 3444 |
|
| 3445 |
EXPECT_TRUE(matches("struct S { bool func(); };", |
| 3446 |
cxxMethodDecl(returns(booleanType())))); |
| 3447 |
EXPECT_TRUE(notMatches("struct S { void func(); };", |
| 3448 |
cxxMethodDecl(returns(booleanType())))); |
| 3449 |
} |
| 3450 |
|
| 3451 |
TEST_P(ASTMatchersTest, VoidType) { |
| 3452 |
if (!GetParam().isCXX()) { |
| 3453 |
// FIXME: Add a test for `voidType()` that does not depend on C++. |
| 3454 |
return; |
| 3455 |
} |
| 3456 |
|
| 3457 |
EXPECT_TRUE(matches("struct S { void func(); };", |
| 3458 |
cxxMethodDecl(returns(voidType())))); |
| 3459 |
} |
| 3460 |
|
| 3461 |
TEST_P(ASTMatchersTest, RealFloatingPointType) { |
| 3462 |
if (!GetParam().isCXX()) { |
| 3463 |
// FIXME: Add a test for `realFloatingPointType()` that does not depend on |
| 3464 |
// C++. |
| 3465 |
return; |
| 3466 |
} |
| 3467 |
|
| 3468 |
EXPECT_TRUE(matches("struct S { float func(); };", |
| 3469 |
cxxMethodDecl(returns(realFloatingPointType())))); |
| 3470 |
EXPECT_TRUE(notMatches("struct S { int func(); };", |
| 3471 |
cxxMethodDecl(returns(realFloatingPointType())))); |
| 3472 |
EXPECT_TRUE(matches("struct S { long double func(); };", |
| 3473 |
cxxMethodDecl(returns(realFloatingPointType())))); |
| 3474 |
} |
| 3475 |
|
| 3476 |
TEST_P(ASTMatchersTest, ArrayType) { |
| 3477 |
EXPECT_TRUE(matches("int a[] = {2,3};", arrayType())); |
| 3478 |
EXPECT_TRUE(matches("int a[42];", arrayType())); |
| 3479 |
EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType())); |
| 3480 |
|
| 3481 |
EXPECT_TRUE(notMatches("struct A {}; struct A a[7];", |
| 3482 |
arrayType(hasElementType(builtinType())))); |
| 3483 |
|
| 3484 |
EXPECT_TRUE(matches("int const a[] = { 2, 3 };", |
| 3485 |
qualType(arrayType(hasElementType(builtinType()))))); |
| 3486 |
EXPECT_TRUE(matches( |
| 3487 |
"int const a[] = { 2, 3 };", |
| 3488 |
qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3489 |
EXPECT_TRUE(matches("typedef const int T; T x[] = { 1, 2 };", |
| 3490 |
qualType(isConstQualified(), arrayType()))); |
| 3491 |
|
| 3492 |
EXPECT_TRUE(notMatches( |
| 3493 |
"int a[] = { 2, 3 };", |
| 3494 |
qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3495 |
EXPECT_TRUE(notMatches( |
| 3496 |
"int a[] = { 2, 3 };", |
| 3497 |
qualType(arrayType(hasElementType(isConstQualified(), builtinType()))))); |
| 3498 |
EXPECT_TRUE(notMatches("int const a[] = { 2, 3 };", |
| 3499 |
qualType(arrayType(hasElementType(builtinType())), |
| 3500 |
unless(isConstQualified())))); |
| 3501 |
|
| 3502 |
EXPECT_TRUE( |
| 3503 |
matches("int a[2];", constantArrayType(hasElementType(builtinType())))); |
| 3504 |
EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger()))); |
| 3505 |
} |
| 3506 |
|
| 3507 |
TEST_P(ASTMatchersTest, DecayedType) { |
| 3508 |
EXPECT_TRUE( |
| 3509 |
matches("void f(int i[]);", |
| 3510 |
valueDecl(hasType(decayedType(hasDecayedType(pointerType())))))); |
| 3511 |
EXPECT_TRUE(notMatches("int i[7];", decayedType())); |
| 3512 |
} |
| 3513 |
|
| 3514 |
TEST_P(ASTMatchersTest, ComplexType) { |
| 3515 |
EXPECT_TRUE(matches("_Complex float f;", complexType())); |
| 3516 |
EXPECT_TRUE( |
| 3517 |
matches("_Complex float f;", complexType(hasElementType(builtinType())))); |
| 3518 |
EXPECT_TRUE(notMatches("_Complex float f;", |
| 3519 |
complexType(hasElementType(isInteger())))); |
| 3520 |
} |
| 3521 |
|
| 3522 |
TEST_P(ASTMatchersTest, IsAnonymous) { |
| 3523 |
if (!GetParam().isCXX()) { |
| 3524 |
return; |
| 3525 |
} |
| 3526 |
|
| 3527 |
EXPECT_TRUE(notMatches("namespace N {}", namespaceDecl(isAnonymous()))); |
| 3528 |
EXPECT_TRUE(matches("namespace {}", namespaceDecl(isAnonymous()))); |
| 3529 |
} |
| 3530 |
|
| 3531 |
TEST_P(ASTMatchersTest, InStdNamespace) { |
| 3532 |
if (!GetParam().isCXX()) { |
| 3533 |
return; |
| 3534 |
} |
| 3535 |
|
| 3536 |
EXPECT_TRUE(notMatches("class vector {};" |
| 3537 |
"namespace foo {" |
| 3538 |
" class vector {};" |
| 3539 |
"}" |
| 3540 |
"namespace foo {" |
| 3541 |
" namespace std {" |
| 3542 |
" class vector {};" |
| 3543 |
" }" |
| 3544 |
"}", |
| 3545 |
cxxRecordDecl(hasName("vector"), isInStdNamespace()))); |
| 3546 |
|
| 3547 |
EXPECT_TRUE(matches("namespace std {" |
| 3548 |
" class vector {};" |
| 3549 |
"}", |
| 3550 |
cxxRecordDecl(hasName("vector"), isInStdNamespace()))); |
| 3551 |
} |
| 3552 |
|
| 3553 |
TEST_P(ASTMatchersTest, InAnonymousNamespace) { |
| 3554 |
if (!GetParam().isCXX()) { |
| 3555 |
return; |
| 3556 |
} |
| 3557 |
|
| 3558 |
EXPECT_TRUE( |
| 3559 |
notMatches("class vector {};" |
| 3560 |
"namespace foo {" |
| 3561 |
" class vector {};" |
| 3562 |
"}", |
| 3563 |
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()))); |
| 3564 |
|
| 3565 |
EXPECT_TRUE( |
| 3566 |
matches("namespace {" |
| 3567 |
" class vector {};" |
| 3568 |
"}", |
| 3569 |
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()))); |
| 3570 |
|
| 3571 |
EXPECT_TRUE( |
| 3572 |
matches("namespace foo {" |
| 3573 |
" namespace {" |
| 3574 |
" class vector {};" |
| 3575 |
" }" |
| 3576 |
"}", |
| 3577 |
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()))); |
| 3578 |
|
| 3579 |
EXPECT_TRUE( |
| 3580 |
matches("namespace {" |
| 3581 |
" namespace foo {" |
| 3582 |
" class vector {};" |
| 3583 |
" }" |
| 3584 |
"}", |
| 3585 |
cxxRecordDecl(hasName("vector"), isInAnonymousNamespace()))); |
| 3586 |
} |
| 3587 |
|
| 3588 |
TEST_P(ASTMatchersTest, InStdNamespace_CXX11) { |
| 3589 |
if (!GetParam().isCXX11OrLater()) { |
| 3590 |
return; |
| 3591 |
} |
| 3592 |
|
| 3593 |
EXPECT_TRUE(matches("namespace std {" |
| 3594 |
" inline namespace __1 {" |
| 3595 |
" class vector {};" |
| 3596 |
" }" |
| 3597 |
"}", |
| 3598 |
cxxRecordDecl(hasName("vector"), isInStdNamespace()))); |
| 3599 |
EXPECT_TRUE(notMatches("namespace std {" |
| 3600 |
" inline namespace __1 {" |
| 3601 |
" inline namespace __fs {" |
| 3602 |
" namespace filesystem {" |
| 3603 |
" inline namespace v1 {" |
| 3604 |
" class path {};" |
| 3605 |
" }" |
| 3606 |
" }" |
| 3607 |
" }" |
| 3608 |
" }" |
| 3609 |
"}", |
| 3610 |
cxxRecordDecl(hasName("path"), isInStdNamespace()))); |
| 3611 |
EXPECT_TRUE( |
| 3612 |
matches("namespace std {" |
| 3613 |
" inline namespace __1 {" |
| 3614 |
" inline namespace __fs {" |
| 3615 |
" namespace filesystem {" |
| 3616 |
" inline namespace v1 {" |
| 3617 |
" class path {};" |
| 3618 |
" }" |
| 3619 |
" }" |
| 3620 |
" }" |
| 3621 |
" }" |
| 3622 |
"}", |
| 3623 |
cxxRecordDecl(hasName("path"), |
| 3624 |
hasAncestor(namespaceDecl(hasName("filesystem"), |
| 3625 |
isInStdNamespace()))))); |
| 3626 |
} |
| 3627 |
|
| 3628 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_QualType) { |
| 3629 |
EXPECT_TRUE(matches( |
| 3630 |
"int i = 1;", varDecl(hasType(qualType().bind("type")), |
| 3631 |
hasInitializer(ignoringParenImpCasts( |
| 3632 |
hasType(qualType(equalsBoundNode("type")))))))); |
| 3633 |
EXPECT_TRUE(notMatches("int i = 1.f;", |
| 3634 |
varDecl(hasType(qualType().bind("type")), |
| 3635 |
hasInitializer(ignoringParenImpCasts(hasType( |
| 3636 |
qualType(equalsBoundNode("type")))))))); |
| 3637 |
} |
| 3638 |
|
| 3639 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_NonMatchingTypes) { |
| 3640 |
EXPECT_TRUE(notMatches( |
| 3641 |
"int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"), |
| 3642 |
hasInitializer(ignoringParenImpCasts( |
| 3643 |
hasType(qualType(equalsBoundNode("type")))))))); |
| 3644 |
} |
| 3645 |
|
| 3646 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_Stmt) { |
| 3647 |
EXPECT_TRUE( |
| 3648 |
matches("void f() { if(1) {} }", |
| 3649 |
stmt(allOf(ifStmt().bind("if"), |
| 3650 |
hasParent(stmt(has(stmt(equalsBoundNode("if"))))))))); |
| 3651 |
|
| 3652 |
EXPECT_TRUE(notMatches( |
| 3653 |
"void f() { if(1) { if (1) {} } }", |
| 3654 |
stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if"))))))); |
| 3655 |
} |
| 3656 |
|
| 3657 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_Decl) { |
| 3658 |
if (!GetParam().isCXX()) { |
| 3659 |
// FIXME: Add a test for `equalsBoundNode()` for declarations that does not |
| 3660 |
// depend on C++. |
| 3661 |
return; |
| 3662 |
} |
| 3663 |
|
| 3664 |
EXPECT_TRUE(matches( |
| 3665 |
"class X { class Y {}; };", |
| 3666 |
decl(allOf(recordDecl(hasName("::X::Y")).bind("record"), |
| 3667 |
hasParent(decl(has(decl(equalsBoundNode("record"))))))))); |
| 3668 |
|
| 3669 |
EXPECT_TRUE(notMatches("class X { class Y {}; };", |
| 3670 |
decl(allOf(recordDecl(hasName("::X")).bind("record"), |
| 3671 |
has(decl(equalsBoundNode("record"))))))); |
| 3672 |
} |
| 3673 |
|
| 3674 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_Type) { |
| 3675 |
if (!GetParam().isCXX()) { |
| 3676 |
// FIXME: Add a test for `equalsBoundNode()` for types that does not depend |
| 3677 |
// on C++. |
| 3678 |
return; |
| 3679 |
} |
| 3680 |
EXPECT_TRUE(matches( |
| 3681 |
"class X { int a; int b; };", |
| 3682 |
recordDecl( |
| 3683 |
has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 3684 |
has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 3685 |
|
| 3686 |
EXPECT_TRUE(notMatches( |
| 3687 |
"class X { int a; double b; };", |
| 3688 |
recordDecl( |
| 3689 |
has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 3690 |
has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 3691 |
} |
| 3692 |
|
| 3693 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_UsingForEachDescendant) { |
| 3694 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3695 |
"int f() {" |
| 3696 |
" if (1) {" |
| 3697 |
" int i = 9;" |
| 3698 |
" }" |
| 3699 |
" int j = 10;" |
| 3700 |
" {" |
| 3701 |
" float k = 9.0;" |
| 3702 |
" }" |
| 3703 |
" return 0;" |
| 3704 |
"}", |
| 3705 |
// Look for variable declarations within functions whose type is the same |
| 3706 |
// as the function return type. |
| 3707 |
functionDecl( |
| 3708 |
returns(qualType().bind("type")), |
| 3709 |
forEachDescendant(varDecl(hasType(qualType(equalsBoundNode("type")))) |
| 3710 |
.bind("decl"))), |
| 3711 |
// Only i and j should match, not k. |
| 3712 |
std::make_unique<VerifyIdIsBoundTo<VarDecl>>("decl", 2))); |
| 3713 |
} |
| 3714 |
|
| 3715 |
TEST_P(ASTMatchersTest, EqualsBoundNodeMatcher_FiltersMatchedCombinations) { |
| 3716 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3717 |
"void f() {" |
| 3718 |
" int x;" |
| 3719 |
" double d;" |
| 3720 |
" x = d + x - d + x;" |
| 3721 |
"}", |
| 3722 |
functionDecl( |
| 3723 |
hasName("f"), forEachDescendant(varDecl().bind("d")), |
| 3724 |
forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))), |
| 3725 |
std::make_unique<VerifyIdIsBoundTo<VarDecl>>("d", 5))); |
| 3726 |
} |
| 3727 |
|
| 3728 |
TEST_P(ASTMatchersTest, |
| 3729 |
EqualsBoundNodeMatcher_UnlessDescendantsOfAncestorsMatch) { |
| 3730 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3731 |
"struct StringRef { int size() const; const char* data() const; };" |
| 3732 |
"void f(StringRef v) {" |
| 3733 |
" v.data();" |
| 3734 |
"}", |
| 3735 |
cxxMemberCallExpr( |
| 3736 |
callee(cxxMethodDecl(hasName("data"))), |
| 3737 |
on(declRefExpr(to( |
| 3738 |
varDecl(hasType(recordDecl(hasName("StringRef")))).bind("var")))), |
| 3739 |
unless(hasAncestor(stmt(hasDescendant(cxxMemberCallExpr( |
| 3740 |
callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))), |
| 3741 |
on(declRefExpr(to(varDecl(equalsBoundNode("var"))))))))))) |
| 3742 |
.bind("data"), |
| 3743 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("data", 1))); |
| 3744 |
|
| 3745 |
EXPECT_FALSE(matches( |
| 3746 |
"struct StringRef { int size() const; const char* data() const; };" |
| 3747 |
"void f(StringRef v) {" |
| 3748 |
" v.data();" |
| 3749 |
" v.size();" |
| 3750 |
"}", |
| 3751 |
cxxMemberCallExpr( |
| 3752 |
callee(cxxMethodDecl(hasName("data"))), |
| 3753 |
on(declRefExpr(to( |
| 3754 |
varDecl(hasType(recordDecl(hasName("StringRef")))).bind("var")))), |
| 3755 |
unless(hasAncestor(stmt(hasDescendant(cxxMemberCallExpr( |
| 3756 |
callee(cxxMethodDecl(anyOf(hasName("size"), hasName("length")))), |
| 3757 |
on(declRefExpr(to(varDecl(equalsBoundNode("var"))))))))))) |
| 3758 |
.bind("data"))); |
| 3759 |
} |
| 3760 |
|
| 3761 |
TEST_P(ASTMatchersTest, NullPointerConstant) { |
| 3762 |
EXPECT_TRUE(matches("#define NULL ((void *)0)\n" |
| 3763 |
"void *v1 = NULL;", |
| 3764 |
expr(nullPointerConstant()))); |
| 3765 |
EXPECT_TRUE(matches("char *cp = (char *)0;", expr(nullPointerConstant()))); |
| 3766 |
EXPECT_TRUE(matches("int *ip = 0;", expr(nullPointerConstant()))); |
| 3767 |
EXPECT_FALSE(matches("int i = 0;", expr(nullPointerConstant()))); |
| 3768 |
} |
| 3769 |
|
| 3770 |
TEST_P(ASTMatchersTest, NullPointerConstant_GNUNull) { |
| 3771 |
if (!GetParam().isCXX()) { |
| 3772 |
return; |
| 3773 |
} |
| 3774 |
|
| 3775 |
EXPECT_TRUE(matches("void *p = __null;", expr(nullPointerConstant()))); |
| 3776 |
} |
| 3777 |
|
| 3778 |
TEST_P(ASTMatchersTest, NullPointerConstant_GNUNullInTemplate) { |
| 3779 |
if (!GetParam().isCXX() || GetParam().hasDelayedTemplateParsing()) { |
| 3780 |
// FIXME: Fix this test to work with delayed template parsing. |
| 3781 |
return; |
| 3782 |
} |
| 3783 |
|
| 3784 |
const char kTest[] = R"( |
| 3785 |
template <typename T> |
| 3786 |
struct MyTemplate { |
| 3787 |
MyTemplate() : field_(__null) {} |
| 3788 |
T* field_; |
| 3789 |
}; |
| 3790 |
)"; |
| 3791 |
EXPECT_TRUE(matches(kTest, expr(nullPointerConstant()))); |
| 3792 |
} |
| 3793 |
|
| 3794 |
TEST_P(ASTMatchersTest, NullPointerConstant_CXX11Nullptr) { |
| 3795 |
if (!GetParam().isCXX11OrLater()) { |
| 3796 |
return; |
| 3797 |
} |
| 3798 |
|
| 3799 |
EXPECT_TRUE(matches("void *p = nullptr;", expr(nullPointerConstant()))); |
| 3800 |
} |
| 3801 |
|
| 3802 |
TEST_P(ASTMatchersTest, HasExternalFormalLinkage) { |
| 3803 |
EXPECT_TRUE(matches("int a = 0;", |
| 3804 |
namedDecl(hasName("a"), hasExternalFormalLinkage()))); |
| 3805 |
EXPECT_TRUE(notMatches("static int a = 0;", |
| 3806 |
namedDecl(hasName("a"), hasExternalFormalLinkage()))); |
| 3807 |
EXPECT_TRUE(notMatches("static void f(void) { int a = 0; }", |
| 3808 |
namedDecl(hasName("a"), hasExternalFormalLinkage()))); |
| 3809 |
EXPECT_TRUE(notMatches("void f(void) { int a = 0; }", |
| 3810 |
namedDecl(hasName("a"), hasExternalFormalLinkage()))); |
| 3811 |
} |
| 3812 |
|
| 3813 |
TEST_P(ASTMatchersTest, HasExternalFormalLinkage_CXX) { |
| 3814 |
if (!GetParam().isCXX()) { |
| 3815 |
return; |
| 3816 |
} |
| 3817 |
|
| 3818 |
EXPECT_TRUE(notMatches("namespace { int a = 0; }", |
| 3819 |
namedDecl(hasName("a"), hasExternalFormalLinkage()))); |
| 3820 |
} |
| 3821 |
|
| 3822 |
TEST_P(ASTMatchersTest, HasDefaultArgument) { |
| 3823 |
if (!GetParam().isCXX()) { |
| 3824 |
return; |
| 3825 |
} |
| 3826 |
|
| 3827 |
EXPECT_TRUE( |
| 3828 |
matches("void x(int val = 0) {}", parmVarDecl(hasDefaultArgument()))); |
| 3829 |
EXPECT_TRUE( |
| 3830 |
notMatches("void x(int val) {}", parmVarDecl(hasDefaultArgument()))); |
| 3831 |
} |
| 3832 |
|
| 3833 |
TEST_P(ASTMatchersTest, IsAtPosition) { |
| 3834 |
EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); |
| 3835 |
EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(0)))); |
| 3836 |
EXPECT_TRUE(matches("void x(int a, int b) {}", parmVarDecl(isAtPosition(1)))); |
| 3837 |
EXPECT_TRUE(notMatches("void x(int val) {}", parmVarDecl(isAtPosition(1)))); |
| 3838 |
} |
| 3839 |
|
| 3840 |
TEST_P(ASTMatchersTest, IsAtPosition_FunctionDecl) { |
| 3841 |
EXPECT_TRUE(matches("void x(int a);", parmVarDecl(isAtPosition(0)))); |
| 3842 |
EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(0)))); |
| 3843 |
EXPECT_TRUE(matches("void x(int a, int b);", parmVarDecl(isAtPosition(1)))); |
| 3844 |
EXPECT_TRUE(notMatches("void x(int val);", parmVarDecl(isAtPosition(1)))); |
| 3845 |
} |
| 3846 |
|
| 3847 |
TEST_P(ASTMatchersTest, IsAtPosition_Lambda) { |
| 3848 |
if (!GetParam().isCXX11OrLater()) { |
| 3849 |
return; |
| 3850 |
} |
| 3851 |
|
| 3852 |
EXPECT_TRUE( |
| 3853 |
matches("void x() { [](int a) {}; }", parmVarDecl(isAtPosition(0)))); |
| 3854 |
EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }", |
| 3855 |
parmVarDecl(isAtPosition(0)))); |
| 3856 |
EXPECT_TRUE(matches("void x() { [](int a, int b) {}; }", |
| 3857 |
parmVarDecl(isAtPosition(1)))); |
| 3858 |
EXPECT_TRUE( |
| 3859 |
notMatches("void x() { [](int val) {}; }", parmVarDecl(isAtPosition(1)))); |
| 3860 |
} |
| 3861 |
|
| 3862 |
TEST_P(ASTMatchersTest, IsAtPosition_BlockDecl) { |
| 3863 |
EXPECT_TRUE(matchesObjC( |
| 3864 |
"void func() { void (^my_block)(int arg) = ^void(int arg) {}; } ", |
| 3865 |
parmVarDecl(isAtPosition(0)))); |
| 3866 |
|
| 3867 |
EXPECT_TRUE(matchesObjC("void func() { void (^my_block)(int x, int y) = " |
| 3868 |
"^void(int x, int y) {}; } ", |
| 3869 |
parmVarDecl(isAtPosition(1)))); |
| 3870 |
|
| 3871 |
EXPECT_TRUE(notMatchesObjC( |
| 3872 |
"void func() { void (^my_block)(int arg) = ^void(int arg) {}; } ", |
| 3873 |
parmVarDecl(isAtPosition(1)))); |
| 3874 |
} |
| 3875 |
|
| 3876 |
TEST_P(ASTMatchersTest, IsArray) { |
| 3877 |
if (!GetParam().isCXX()) { |
| 3878 |
return; |
| 3879 |
} |
| 3880 |
|
| 3881 |
EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", |
| 3882 |
cxxNewExpr(isArray()))); |
| 3883 |
} |
| 3884 |
|
| 3885 |
TEST_P(ASTMatchersTest, HasArraySize) { |
| 3886 |
if (!GetParam().isCXX()) { |
| 3887 |
return; |
| 3888 |
} |
| 3889 |
|
| 3890 |
EXPECT_TRUE(matches("struct MyClass {}; MyClass *p1 = new MyClass[10];", |
| 3891 |
cxxNewExpr(hasArraySize( |
| 3892 |
ignoringParenImpCasts(integerLiteral(equals(10))))))); |
| 3893 |
} |
| 3894 |
|
| 3895 |
TEST_P(ASTMatchersTest, HasDefinition_MatchesStructDefinition) { |
| 3896 |
if (!GetParam().isCXX()) { |
| 3897 |
return; |
| 3898 |
} |
| 3899 |
|
| 3900 |
EXPECT_TRUE(matches("struct x {};", cxxRecordDecl(hasDefinition()))); |
| 3901 |
EXPECT_TRUE(notMatches("struct x;", cxxRecordDecl(hasDefinition()))); |
| 3902 |
} |
| 3903 |
|
| 3904 |
TEST_P(ASTMatchersTest, HasDefinition_MatchesClassDefinition) { |
| 3905 |
if (!GetParam().isCXX()) { |
| 3906 |
return; |
| 3907 |
} |
| 3908 |
|
| 3909 |
EXPECT_TRUE(matches("class x {};", cxxRecordDecl(hasDefinition()))); |
| 3910 |
EXPECT_TRUE(notMatches("class x;", cxxRecordDecl(hasDefinition()))); |
| 3911 |
} |
| 3912 |
|
| 3913 |
TEST_P(ASTMatchersTest, HasDefinition_MatchesUnionDefinition) { |
| 3914 |
if (!GetParam().isCXX()) { |
| 3915 |
return; |
| 3916 |
} |
| 3917 |
|
| 3918 |
EXPECT_TRUE(matches("union x {};", cxxRecordDecl(hasDefinition()))); |
| 3919 |
EXPECT_TRUE(notMatches("union x;", cxxRecordDecl(hasDefinition()))); |
| 3920 |
} |
| 3921 |
|
| 3922 |
TEST_P(ASTMatchersTest, IsScoped_MatchesScopedEnum) { |
| 3923 |
if (!GetParam().isCXX11OrLater()) { |
| 3924 |
return; |
| 3925 |
} |
| 3926 |
EXPECT_TRUE(matches("enum class X {};", enumDecl(isScoped()))); |
| 3927 |
} |
| 3928 |
|
| 3929 |
TEST_P(ASTMatchersTest, IsScoped_NotMatchesRegularEnum) { |
| 3930 |
EXPECT_TRUE(notMatches("enum E { E1 };", enumDecl(isScoped()))); |
| 3931 |
} |
| 3932 |
|
| 3933 |
TEST_P(ASTMatchersTest, IsStruct) { |
| 3934 |
EXPECT_TRUE(matches("struct S {};", tagDecl(isStruct()))); |
| 3935 |
} |
| 3936 |
|
| 3937 |
TEST_P(ASTMatchersTest, IsUnion) { |
| 3938 |
EXPECT_TRUE(matches("union U {};", tagDecl(isUnion()))); |
| 3939 |
} |
| 3940 |
|
| 3941 |
TEST_P(ASTMatchersTest, IsEnum) { |
| 3942 |
EXPECT_TRUE(matches("enum E { E1 };", tagDecl(isEnum()))); |
| 3943 |
} |
| 3944 |
|
| 3945 |
TEST_P(ASTMatchersTest, IsClass) { |
| 3946 |
if (!GetParam().isCXX()) { |
| 3947 |
return; |
| 3948 |
} |
| 3949 |
|
| 3950 |
EXPECT_TRUE(matches("class C {};", tagDecl(isClass()))); |
| 3951 |
} |
| 3952 |
|
| 3953 |
TEST_P(ASTMatchersTest, HasTrailingReturn_MatchesTrailingReturn) { |
| 3954 |
if (!GetParam().isCXX11OrLater()) { |
| 3955 |
return; |
| 3956 |
} |
| 3957 |
|
| 3958 |
EXPECT_TRUE(matches("auto Y() -> int { return 0; }", |
| 3959 |
functionDecl(hasTrailingReturn()))); |
| 3960 |
EXPECT_TRUE(matches("auto X() -> int;", functionDecl(hasTrailingReturn()))); |
| 3961 |
EXPECT_TRUE( |
| 3962 |
notMatches("int X() { return 0; }", functionDecl(hasTrailingReturn()))); |
| 3963 |
EXPECT_TRUE(notMatches("int X();", functionDecl(hasTrailingReturn()))); |
| 3964 |
EXPECT_TRUE(notMatches("void X();", functionDecl(hasTrailingReturn()))); |
| 3965 |
} |
| 3966 |
|
| 3967 |
TEST_P(ASTMatchersTest, HasTrailingReturn_MatchesLambdaTrailingReturn) { |
| 3968 |
if (!GetParam().isCXX11OrLater()) { |
| 3969 |
return; |
| 3970 |
} |
| 3971 |
|
| 3972 |
EXPECT_TRUE(matches( |
| 3973 |
"auto lambda2 = [](double x, double y) -> double {return x + y;};", |
| 3974 |
functionDecl(hasTrailingReturn()))); |
| 3975 |
EXPECT_TRUE( |
| 3976 |
notMatches("auto lambda2 = [](double x, double y) {return x + y;};", |
| 3977 |
functionDecl(hasTrailingReturn()))); |
| 3978 |
} |
| 3979 |
|
| 3980 |
TEST_P(ASTMatchersTest, IsAssignmentOperator) { |
| 3981 |
if (!GetParam().isCXX()) { |
| 3982 |
return; |
| 3983 |
} |
| 3984 |
|
| 3985 |
StatementMatcher BinAsgmtOperator = binaryOperator(isAssignmentOperator()); |
| 3986 |
StatementMatcher CXXAsgmtOperator = |
| 3987 |
cxxOperatorCallExpr(isAssignmentOperator()); |
| 3988 |
|
| 3989 |
EXPECT_TRUE(matches("void x() { int a; a += 1; }", BinAsgmtOperator)); |
| 3990 |
EXPECT_TRUE(matches("void x() { int a; a = 2; }", BinAsgmtOperator)); |
| 3991 |
EXPECT_TRUE(matches("void x() { int a; a &= 3; }", BinAsgmtOperator)); |
| 3992 |
EXPECT_TRUE(matches("struct S { S& operator=(const S&); };" |
| 3993 |
"void x() { S s1, s2; s1 = s2; }", |
| 3994 |
CXXAsgmtOperator)); |
| 3995 |
EXPECT_TRUE( |
| 3996 |
notMatches("void x() { int a; if(a == 0) return; }", BinAsgmtOperator)); |
| 3997 |
} |
| 3998 |
|
| 3999 |
TEST_P(ASTMatchersTest, IsComparisonOperator) { |
| 4000 |
if (!GetParam().isCXX()) { |
| 4001 |
return; |
| 4002 |
} |
| 4003 |
|
| 4004 |
StatementMatcher BinCompOperator = binaryOperator(isComparisonOperator()); |
| 4005 |
StatementMatcher CXXCompOperator = |
| 4006 |
cxxOperatorCallExpr(isComparisonOperator()); |
| 4007 |
|
| 4008 |
EXPECT_TRUE(matches("void x() { int a; a == 1; }", BinCompOperator)); |
| 4009 |
EXPECT_TRUE(matches("void x() { int a; a > 2; }", BinCompOperator)); |
| 4010 |
EXPECT_TRUE(matches("struct S { bool operator==(const S&); };" |
| 4011 |
"void x() { S s1, s2; bool b1 = s1 == s2; }", |
| 4012 |
CXXCompOperator)); |
| 4013 |
EXPECT_TRUE( |
| 4014 |
notMatches("void x() { int a; if(a = 0) return; }", BinCompOperator)); |
| 4015 |
} |
| 4016 |
|
| 4017 |
TEST_P(ASTMatchersTest, HasInit) { |
| 4018 |
if (!GetParam().isCXX11OrLater()) { |
| 4019 |
// FIXME: Add a test for `hasInit()` that does not depend on C++. |
| 4020 |
return; |
| 4021 |
} |
| 4022 |
|
| 4023 |
EXPECT_TRUE(matches("int x{0};", initListExpr(hasInit(0, expr())))); |
| 4024 |
EXPECT_FALSE(matches("int x{0};", initListExpr(hasInit(1, expr())))); |
| 4025 |
EXPECT_FALSE(matches("int x;", initListExpr(hasInit(0, expr())))); |
| 4026 |
} |
| 4027 |
|
| 4028 |
TEST_P(ASTMatchersTest, IsMain) { |
| 4029 |
EXPECT_TRUE(matches("int main() {}", functionDecl(isMain()))); |
| 4030 |
|
| 4031 |
EXPECT_TRUE(notMatches("int main2() {}", functionDecl(isMain()))); |
| 4032 |
} |
| 4033 |
|
| 4034 |
TEST_P(ASTMatchersTest, OMPExecutableDirective_IsStandaloneDirective) { |
| 4035 |
auto Matcher = ompExecutableDirective(isStandaloneDirective()); |
| 4036 |
|
| 4037 |
StringRef Source0 = R"( |
| 4038 |
void x() { |
| 4039 |
#pragma omp parallel |
| 4040 |
; |
| 4041 |
})"; |
| 4042 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4043 |
|
| 4044 |
StringRef Source1 = R"( |
| 4045 |
void x() { |
| 4046 |
#pragma omp taskyield |
| 4047 |
})"; |
| 4048 |
EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); |
| 4049 |
} |
| 4050 |
|
| 4051 |
TEST_P(ASTMatchersTest, OMPExecutableDirective_HasStructuredBlock) { |
| 4052 |
StringRef Source0 = R"( |
| 4053 |
void x() { |
| 4054 |
#pragma omp parallel |
| 4055 |
; |
| 4056 |
})"; |
| 4057 |
EXPECT_TRUE(matchesWithOpenMP( |
| 4058 |
Source0, ompExecutableDirective(hasStructuredBlock(nullStmt())))); |
| 4059 |
|
| 4060 |
StringRef Source1 = R"( |
| 4061 |
void x() { |
| 4062 |
#pragma omp parallel |
| 4063 |
{;} |
| 4064 |
})"; |
| 4065 |
EXPECT_TRUE(notMatchesWithOpenMP( |
| 4066 |
Source1, ompExecutableDirective(hasStructuredBlock(nullStmt())))); |
| 4067 |
EXPECT_TRUE(matchesWithOpenMP( |
| 4068 |
Source1, ompExecutableDirective(hasStructuredBlock(compoundStmt())))); |
| 4069 |
|
| 4070 |
StringRef Source2 = R"( |
| 4071 |
void x() { |
| 4072 |
#pragma omp taskyield |
| 4073 |
{;} |
| 4074 |
})"; |
| 4075 |
EXPECT_TRUE(notMatchesWithOpenMP( |
| 4076 |
Source2, ompExecutableDirective(hasStructuredBlock(anything())))); |
| 4077 |
} |
| 4078 |
|
| 4079 |
TEST_P(ASTMatchersTest, OMPExecutableDirective_HasClause) { |
| 4080 |
auto Matcher = ompExecutableDirective(hasAnyClause(anything())); |
| 4081 |
|
| 4082 |
StringRef Source0 = R"( |
| 4083 |
void x() { |
| 4084 |
; |
| 4085 |
})"; |
| 4086 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4087 |
|
| 4088 |
StringRef Source1 = R"( |
| 4089 |
void x() { |
| 4090 |
#pragma omp parallel |
| 4091 |
; |
| 4092 |
})"; |
| 4093 |
EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 4094 |
|
| 4095 |
StringRef Source2 = R"( |
| 4096 |
void x() { |
| 4097 |
#pragma omp parallel default(none) |
| 4098 |
; |
| 4099 |
})"; |
| 4100 |
EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
| 4101 |
|
| 4102 |
StringRef Source3 = R"( |
| 4103 |
void x() { |
| 4104 |
#pragma omp parallel default(shared) |
| 4105 |
; |
| 4106 |
})"; |
| 4107 |
EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); |
| 4108 |
|
| 4109 |
StringRef Source4 = R"( |
| 4110 |
void x() { |
| 4111 |
#pragma omp parallel default(firstprivate) |
| 4112 |
; |
| 4113 |
})"; |
| 4114 |
EXPECT_TRUE(matchesWithOpenMP51(Source4, Matcher)); |
| 4115 |
|
| 4116 |
StringRef Source5 = R"( |
| 4117 |
void x(int x) { |
| 4118 |
#pragma omp parallel num_threads(x) |
| 4119 |
; |
| 4120 |
})"; |
| 4121 |
EXPECT_TRUE(matchesWithOpenMP(Source5, Matcher)); |
| 4122 |
} |
| 4123 |
|
| 4124 |
TEST_P(ASTMatchersTest, OMPDefaultClause_IsNoneKind) { |
| 4125 |
auto Matcher = |
| 4126 |
ompExecutableDirective(hasAnyClause(ompDefaultClause(isNoneKind()))); |
| 4127 |
|
| 4128 |
StringRef Source0 = R"( |
| 4129 |
void x() { |
| 4130 |
; |
| 4131 |
})"; |
| 4132 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4133 |
|
| 4134 |
StringRef Source1 = R"( |
| 4135 |
void x() { |
| 4136 |
#pragma omp parallel |
| 4137 |
; |
| 4138 |
})"; |
| 4139 |
EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 4140 |
|
| 4141 |
StringRef Source2 = R"( |
| 4142 |
void x() { |
| 4143 |
#pragma omp parallel default(none) |
| 4144 |
; |
| 4145 |
})"; |
| 4146 |
EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
| 4147 |
|
| 4148 |
StringRef Source3 = R"( |
| 4149 |
void x() { |
| 4150 |
#pragma omp parallel default(shared) |
| 4151 |
; |
| 4152 |
})"; |
| 4153 |
EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); |
| 4154 |
|
| 4155 |
StringRef Source4 = R"( |
| 4156 |
void x(int x) { |
| 4157 |
#pragma omp parallel default(firstprivate) |
| 4158 |
; |
| 4159 |
})"; |
| 4160 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source4, Matcher)); |
| 4161 |
|
| 4162 |
StringRef Source5 = R"( |
| 4163 |
void x(int x) { |
| 4164 |
#pragma omp parallel default(private) |
| 4165 |
; |
| 4166 |
})"; |
| 4167 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source5, Matcher)); |
| 4168 |
|
| 4169 |
const std::string Source6 = R"( |
| 4170 |
void x(int x) { |
| 4171 |
#pragma omp parallel num_threads(x) |
| 4172 |
; |
| 4173 |
})"; |
| 4174 |
EXPECT_TRUE(notMatchesWithOpenMP(Source6, Matcher)); |
| 4175 |
} |
| 4176 |
|
| 4177 |
TEST_P(ASTMatchersTest, OMPDefaultClause_IsSharedKind) { |
| 4178 |
auto Matcher = |
| 4179 |
ompExecutableDirective(hasAnyClause(ompDefaultClause(isSharedKind()))); |
| 4180 |
|
| 4181 |
StringRef Source0 = R"( |
| 4182 |
void x() { |
| 4183 |
; |
| 4184 |
})"; |
| 4185 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4186 |
|
| 4187 |
StringRef Source1 = R"( |
| 4188 |
void x() { |
| 4189 |
#pragma omp parallel |
| 4190 |
; |
| 4191 |
})"; |
| 4192 |
EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 4193 |
|
| 4194 |
StringRef Source2 = R"( |
| 4195 |
void x() { |
| 4196 |
#pragma omp parallel default(shared) |
| 4197 |
; |
| 4198 |
})"; |
| 4199 |
EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
| 4200 |
|
| 4201 |
StringRef Source3 = R"( |
| 4202 |
void x() { |
| 4203 |
#pragma omp parallel default(none) |
| 4204 |
; |
| 4205 |
})"; |
| 4206 |
EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); |
| 4207 |
|
| 4208 |
StringRef Source4 = R"( |
| 4209 |
void x(int x) { |
| 4210 |
#pragma omp parallel default(firstprivate) |
| 4211 |
; |
| 4212 |
})"; |
| 4213 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source4, Matcher)); |
| 4214 |
|
| 4215 |
StringRef Source5 = R"( |
| 4216 |
void x(int x) { |
| 4217 |
#pragma omp parallel default(private) |
| 4218 |
; |
| 4219 |
})"; |
| 4220 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source5, Matcher)); |
| 4221 |
|
| 4222 |
const std::string Source6 = R"( |
| 4223 |
void x(int x) { |
| 4224 |
#pragma omp parallel num_threads(x) |
| 4225 |
; |
| 4226 |
})"; |
| 4227 |
EXPECT_TRUE(notMatchesWithOpenMP(Source6, Matcher)); |
| 4228 |
} |
| 4229 |
|
| 4230 |
TEST(OMPDefaultClause, isFirstPrivateKind) { |
| 4231 |
auto Matcher = ompExecutableDirective( |
| 4232 |
hasAnyClause(ompDefaultClause(isFirstPrivateKind()))); |
| 4233 |
|
| 4234 |
const std::string Source0 = R"( |
| 4235 |
void x() { |
| 4236 |
; |
| 4237 |
})"; |
| 4238 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4239 |
|
| 4240 |
const std::string Source1 = R"( |
| 4241 |
void x() { |
| 4242 |
#pragma omp parallel |
| 4243 |
; |
| 4244 |
})"; |
| 4245 |
EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 4246 |
|
| 4247 |
const std::string Source2 = R"( |
| 4248 |
void x() { |
| 4249 |
#pragma omp parallel default(shared) |
| 4250 |
; |
| 4251 |
})"; |
| 4252 |
EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher)); |
| 4253 |
|
| 4254 |
const std::string Source3 = R"( |
| 4255 |
void x() { |
| 4256 |
#pragma omp parallel default(none) |
| 4257 |
; |
| 4258 |
})"; |
| 4259 |
EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); |
| 4260 |
|
| 4261 |
const std::string Source4 = R"( |
| 4262 |
void x(int x) { |
| 4263 |
#pragma omp parallel default(firstprivate) |
| 4264 |
; |
| 4265 |
})"; |
| 4266 |
EXPECT_TRUE(matchesWithOpenMP51(Source4, Matcher)); |
| 4267 |
|
| 4268 |
const std::string Source5 = R"( |
| 4269 |
void x(int x) { |
| 4270 |
#pragma omp parallel default(private) |
| 4271 |
; |
| 4272 |
})"; |
| 4273 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source5, Matcher)); |
| 4274 |
|
| 4275 |
const std::string Source6 = R"( |
| 4276 |
void x(int x) { |
| 4277 |
#pragma omp parallel num_threads(x) |
| 4278 |
; |
| 4279 |
})"; |
| 4280 |
EXPECT_TRUE(notMatchesWithOpenMP(Source6, Matcher)); |
| 4281 |
} |
| 4282 |
|
| 4283 |
TEST(OMPDefaultClause, istPrivateKind) { |
| 4284 |
auto Matcher = |
| 4285 |
ompExecutableDirective(hasAnyClause(ompDefaultClause(isPrivateKind()))); |
| 4286 |
|
| 4287 |
const std::string Source0 = R"( |
| 4288 |
void x() { |
| 4289 |
; |
| 4290 |
})"; |
| 4291 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4292 |
|
| 4293 |
const std::string Source1 = R"( |
| 4294 |
void x() { |
| 4295 |
#pragma omp parallel |
| 4296 |
; |
| 4297 |
})"; |
| 4298 |
EXPECT_TRUE(notMatchesWithOpenMP(Source1, Matcher)); |
| 4299 |
|
| 4300 |
const std::string Source2 = R"( |
| 4301 |
void x() { |
| 4302 |
#pragma omp parallel default(shared) |
| 4303 |
; |
| 4304 |
})"; |
| 4305 |
EXPECT_TRUE(notMatchesWithOpenMP(Source2, Matcher)); |
| 4306 |
|
| 4307 |
const std::string Source3 = R"( |
| 4308 |
void x() { |
| 4309 |
#pragma omp parallel default(none) |
| 4310 |
; |
| 4311 |
})"; |
| 4312 |
EXPECT_TRUE(notMatchesWithOpenMP(Source3, Matcher)); |
| 4313 |
|
| 4314 |
const std::string Source4 = R"( |
| 4315 |
void x(int x) { |
| 4316 |
#pragma omp parallel default(firstprivate) |
| 4317 |
; |
| 4318 |
})"; |
| 4319 |
EXPECT_TRUE(notMatchesWithOpenMP51(Source4, Matcher)); |
| 4320 |
|
| 4321 |
const std::string Source5 = R"( |
| 4322 |
void x(int x) { |
| 4323 |
#pragma omp parallel default(private) |
| 4324 |
; |
| 4325 |
})"; |
| 4326 |
EXPECT_TRUE(matchesWithOpenMP51(Source5, Matcher)); |
| 4327 |
|
| 4328 |
const std::string Source6 = R"( |
| 4329 |
void x(int x) { |
| 4330 |
#pragma omp parallel num_threads(x) |
| 4331 |
; |
| 4332 |
})"; |
| 4333 |
EXPECT_TRUE(notMatchesWithOpenMP(Source6, Matcher)); |
| 4334 |
} |
| 4335 |
|
| 4336 |
TEST_P(ASTMatchersTest, OMPExecutableDirective_IsAllowedToContainClauseKind) { |
| 4337 |
auto Matcher = ompExecutableDirective( |
| 4338 |
isAllowedToContainClauseKind(llvm::omp::OMPC_default)); |
| 4339 |
|
| 4340 |
StringRef Source0 = R"( |
| 4341 |
void x() { |
| 4342 |
; |
| 4343 |
})"; |
| 4344 |
EXPECT_TRUE(notMatchesWithOpenMP(Source0, Matcher)); |
| 4345 |
|
| 4346 |
StringRef Source1 = R"( |
| 4347 |
void x() { |
| 4348 |
#pragma omp parallel |
| 4349 |
; |
| 4350 |
})"; |
| 4351 |
EXPECT_TRUE(matchesWithOpenMP(Source1, Matcher)); |
| 4352 |
|
| 4353 |
StringRef Source2 = R"( |
| 4354 |
void x() { |
| 4355 |
#pragma omp parallel default(none) |
| 4356 |
; |
| 4357 |
})"; |
| 4358 |
EXPECT_TRUE(matchesWithOpenMP(Source2, Matcher)); |
| 4359 |
|
| 4360 |
StringRef Source3 = R"( |
| 4361 |
void x() { |
| 4362 |
#pragma omp parallel default(shared) |
| 4363 |
; |
| 4364 |
})"; |
| 4365 |
EXPECT_TRUE(matchesWithOpenMP(Source3, Matcher)); |
| 4366 |
|
| 4367 |
StringRef Source4 = R"( |
| 4368 |
void x() { |
| 4369 |
#pragma omp parallel default(firstprivate) |
| 4370 |
; |
| 4371 |
})"; |
| 4372 |
EXPECT_TRUE(matchesWithOpenMP51(Source4, Matcher)); |
| 4373 |
|
| 4374 |
StringRef Source5 = R"( |
| 4375 |
void x() { |
| 4376 |
#pragma omp parallel default(private) |
| 4377 |
; |
| 4378 |
})"; |
| 4379 |
EXPECT_TRUE(matchesWithOpenMP51(Source5, Matcher)); |
| 4380 |
|
| 4381 |
StringRef Source6 = R"( |
| 4382 |
void x(int x) { |
| 4383 |
#pragma omp parallel num_threads(x) |
| 4384 |
; |
| 4385 |
})"; |
| 4386 |
EXPECT_TRUE(matchesWithOpenMP(Source6, Matcher)); |
| 4387 |
|
| 4388 |
StringRef Source7 = R"( |
| 4389 |
void x() { |
| 4390 |
#pragma omp taskyield |
| 4391 |
})"; |
| 4392 |
EXPECT_TRUE(notMatchesWithOpenMP(Source7, Matcher)); |
| 4393 |
|
| 4394 |
StringRef Source8 = R"( |
| 4395 |
void x() { |
| 4396 |
#pragma omp task |
| 4397 |
; |
| 4398 |
})"; |
| 4399 |
EXPECT_TRUE(matchesWithOpenMP(Source8, Matcher)); |
| 4400 |
} |
| 4401 |
|
| 4402 |
TEST_P(ASTMatchersTest, HasAnyBase_DirectBase) { |
| 4403 |
if (!GetParam().isCXX()) { |
| 4404 |
return; |
| 4405 |
} |
| 4406 |
EXPECT_TRUE(matches( |
| 4407 |
"struct Base {};" |
| 4408 |
"struct ExpectedMatch : Base {};", |
| 4409 |
cxxRecordDecl(hasName("ExpectedMatch"), |
| 4410 |
hasAnyBase(hasType(cxxRecordDecl(hasName("Base"))))))); |
| 4411 |
} |
| 4412 |
|
| 4413 |
TEST_P(ASTMatchersTest, HasAnyBase_IndirectBase) { |
| 4414 |
if (!GetParam().isCXX()) { |
| 4415 |
return; |
| 4416 |
} |
| 4417 |
EXPECT_TRUE(matches( |
| 4418 |
"struct Base {};" |
| 4419 |
"struct Intermediate : Base {};" |
| 4420 |
"struct ExpectedMatch : Intermediate {};", |
| 4421 |
cxxRecordDecl(hasName("ExpectedMatch"), |
| 4422 |
hasAnyBase(hasType(cxxRecordDecl(hasName("Base"))))))); |
| 4423 |
} |
| 4424 |
|
| 4425 |
TEST_P(ASTMatchersTest, HasAnyBase_NoBase) { |
| 4426 |
if (!GetParam().isCXX()) { |
| 4427 |
return; |
| 4428 |
} |
| 4429 |
EXPECT_TRUE(notMatches("struct Foo {};" |
| 4430 |
"struct Bar {};", |
| 4431 |
cxxRecordDecl(hasAnyBase(hasType(cxxRecordDecl()))))); |
| 4432 |
} |
| 4433 |
|
| 4434 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPublic_Public) { |
| 4435 |
if (!GetParam().isCXX()) { |
| 4436 |
return; |
| 4437 |
} |
| 4438 |
EXPECT_TRUE(matches("class Base {};" |
| 4439 |
"class Derived : public Base {};", |
| 4440 |
cxxRecordDecl(hasAnyBase(isPublic())))); |
| 4441 |
} |
| 4442 |
|
| 4443 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPublic_DefaultAccessSpecifierPublic) { |
| 4444 |
if (!GetParam().isCXX()) { |
| 4445 |
return; |
| 4446 |
} |
| 4447 |
EXPECT_TRUE(matches("class Base {};" |
| 4448 |
"struct Derived : Base {};", |
| 4449 |
cxxRecordDecl(hasAnyBase(isPublic())))); |
| 4450 |
} |
| 4451 |
|
| 4452 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPublic_Private) { |
| 4453 |
if (!GetParam().isCXX()) { |
| 4454 |
return; |
| 4455 |
} |
| 4456 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4457 |
"class Derived : private Base {};", |
| 4458 |
cxxRecordDecl(hasAnyBase(isPublic())))); |
| 4459 |
} |
| 4460 |
|
| 4461 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPublic_DefaultAccessSpecifierPrivate) { |
| 4462 |
if (!GetParam().isCXX()) { |
| 4463 |
return; |
| 4464 |
} |
| 4465 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4466 |
"class Derived : Base {};", |
| 4467 |
cxxRecordDecl(hasAnyBase(isPublic())))); |
| 4468 |
} |
| 4469 |
|
| 4470 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPublic_Protected) { |
| 4471 |
if (!GetParam().isCXX()) { |
| 4472 |
return; |
| 4473 |
} |
| 4474 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4475 |
"class Derived : protected Base {};", |
| 4476 |
cxxRecordDecl(hasAnyBase(isPublic())))); |
| 4477 |
} |
| 4478 |
|
| 4479 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPrivate_Private) { |
| 4480 |
if (!GetParam().isCXX()) { |
| 4481 |
return; |
| 4482 |
} |
| 4483 |
EXPECT_TRUE(matches("class Base {};" |
| 4484 |
"class Derived : private Base {};", |
| 4485 |
cxxRecordDecl(hasAnyBase(isPrivate())))); |
| 4486 |
} |
| 4487 |
|
| 4488 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPrivate_DefaultAccessSpecifierPrivate) { |
| 4489 |
if (!GetParam().isCXX()) { |
| 4490 |
return; |
| 4491 |
} |
| 4492 |
EXPECT_TRUE(matches("struct Base {};" |
| 4493 |
"class Derived : Base {};", |
| 4494 |
cxxRecordDecl(hasAnyBase(isPrivate())))); |
| 4495 |
} |
| 4496 |
|
| 4497 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPrivate_Public) { |
| 4498 |
if (!GetParam().isCXX()) { |
| 4499 |
return; |
| 4500 |
} |
| 4501 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4502 |
"class Derived : public Base {};", |
| 4503 |
cxxRecordDecl(hasAnyBase(isPrivate())))); |
| 4504 |
} |
| 4505 |
|
| 4506 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPrivate_DefaultAccessSpecifierPublic) { |
| 4507 |
if (!GetParam().isCXX()) { |
| 4508 |
return; |
| 4509 |
} |
| 4510 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4511 |
"struct Derived : Base {};", |
| 4512 |
cxxRecordDecl(hasAnyBase(isPrivate())))); |
| 4513 |
} |
| 4514 |
|
| 4515 |
TEST_P(ASTMatchersTest, HasAnyBase_IsPrivate_Protected) { |
| 4516 |
if (!GetParam().isCXX()) { |
| 4517 |
return; |
| 4518 |
} |
| 4519 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4520 |
"class Derived : protected Base {};", |
| 4521 |
cxxRecordDecl(hasAnyBase(isPrivate())))); |
| 4522 |
} |
| 4523 |
|
| 4524 |
TEST_P(ASTMatchersTest, HasAnyBase_IsProtected_Protected) { |
| 4525 |
if (!GetParam().isCXX()) { |
| 4526 |
return; |
| 4527 |
} |
| 4528 |
EXPECT_TRUE(matches("class Base {};" |
| 4529 |
"class Derived : protected Base {};", |
| 4530 |
cxxRecordDecl(hasAnyBase(isProtected())))); |
| 4531 |
} |
| 4532 |
|
| 4533 |
TEST_P(ASTMatchersTest, HasAnyBase_IsProtected_Public) { |
| 4534 |
if (!GetParam().isCXX()) { |
| 4535 |
return; |
| 4536 |
} |
| 4537 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4538 |
"class Derived : public Base {};", |
| 4539 |
cxxRecordDecl(hasAnyBase(isProtected())))); |
| 4540 |
} |
| 4541 |
|
| 4542 |
TEST_P(ASTMatchersTest, HasAnyBase_IsProtected_Private) { |
| 4543 |
if (!GetParam().isCXX()) { |
| 4544 |
return; |
| 4545 |
} |
| 4546 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4547 |
"class Derived : private Base {};", |
| 4548 |
cxxRecordDecl(hasAnyBase(isProtected())))); |
| 4549 |
} |
| 4550 |
|
| 4551 |
TEST_P(ASTMatchersTest, HasAnyBase_IsVirtual_Directly) { |
| 4552 |
if (!GetParam().isCXX()) { |
| 4553 |
return; |
| 4554 |
} |
| 4555 |
EXPECT_TRUE(matches("class Base {};" |
| 4556 |
"class Derived : virtual Base {};", |
| 4557 |
cxxRecordDecl(hasAnyBase(isVirtual())))); |
| 4558 |
} |
| 4559 |
|
| 4560 |
TEST_P(ASTMatchersTest, HasAnyBase_IsVirtual_Indirectly) { |
| 4561 |
if (!GetParam().isCXX()) { |
| 4562 |
return; |
| 4563 |
} |
| 4564 |
EXPECT_TRUE( |
| 4565 |
matches("class Base {};" |
| 4566 |
"class Intermediate : virtual Base {};" |
| 4567 |
"class Derived : Intermediate {};", |
| 4568 |
cxxRecordDecl(hasName("Derived"), hasAnyBase(isVirtual())))); |
| 4569 |
} |
| 4570 |
|
| 4571 |
TEST_P(ASTMatchersTest, HasAnyBase_IsVirtual_NoVirtualBase) { |
| 4572 |
if (!GetParam().isCXX()) { |
| 4573 |
return; |
| 4574 |
} |
| 4575 |
EXPECT_TRUE(notMatches("class Base {};" |
| 4576 |
"class Derived : Base {};", |
| 4577 |
cxxRecordDecl(hasAnyBase(isVirtual())))); |
| 4578 |
} |
| 4579 |
|
| 4580 |
TEST_P(ASTMatchersTest, HasDirectBase) { |
| 4581 |
if (!GetParam().isCXX()) { |
| 4582 |
return; |
| 4583 |
} |
| 4584 |
|
| 4585 |
DeclarationMatcher ClassHasAnyDirectBase = |
| 4586 |
cxxRecordDecl(hasDirectBase(cxxBaseSpecifier())); |
| 4587 |
EXPECT_TRUE(notMatches("class X {};", ClassHasAnyDirectBase)); |
| 4588 |
EXPECT_TRUE(matches("class X {}; class Y : X {};", ClassHasAnyDirectBase)); |
| 4589 |
EXPECT_TRUE(matches("class X {}; class Y : public virtual X {};", |
| 4590 |
ClassHasAnyDirectBase)); |
| 4591 |
|
| 4592 |
EXPECT_TRUE(matches( |
| 4593 |
R"cc( |
| 4594 |
class Base {}; |
| 4595 |
class Derived : Base{}; |
| 4596 |
)cc", |
| 4597 |
cxxRecordDecl(hasName("Derived"), |
| 4598 |
hasDirectBase(hasType(cxxRecordDecl(hasName("Base"))))))); |
| 4599 |
|
| 4600 |
StringRef MultiDerived = R"cc( |
| 4601 |
class Base {}; |
| 4602 |
class Base2 {}; |
| 4603 |
class Derived : Base, Base2{}; |
| 4604 |
)cc"; |
| 4605 |
|
| 4606 |
EXPECT_TRUE(matches( |
| 4607 |
MultiDerived, |
| 4608 |
cxxRecordDecl(hasName("Derived"), |
| 4609 |
hasDirectBase(hasType(cxxRecordDecl(hasName("Base"))))))); |
| 4610 |
EXPECT_TRUE(matches( |
| 4611 |
MultiDerived, |
| 4612 |
cxxRecordDecl(hasName("Derived"), |
| 4613 |
hasDirectBase(hasType(cxxRecordDecl(hasName("Base2"))))))); |
| 4614 |
|
| 4615 |
StringRef Indirect = R"cc( |
| 4616 |
class Base {}; |
| 4617 |
class Intermediate : Base {}; |
| 4618 |
class Derived : Intermediate{}; |
| 4619 |
)cc"; |
| 4620 |
|
| 4621 |
EXPECT_TRUE( |
| 4622 |
matches(Indirect, cxxRecordDecl(hasName("Derived"), |
| 4623 |
hasDirectBase(hasType(cxxRecordDecl( |
| 4624 |
hasName("Intermediate"))))))); |
| 4625 |
EXPECT_TRUE(notMatches( |
| 4626 |
Indirect, |
| 4627 |
cxxRecordDecl(hasName("Derived"), |
| 4628 |
hasDirectBase(hasType(cxxRecordDecl(hasName("Base"))))))); |
| 4629 |
} |
| 4630 |
|
| 4631 |
TEST_P(ASTMatchersTest, CapturesThis) { |
| 4632 |
if (!GetParam().isCXX11OrLater()) { |
| 4633 |
return; |
| 4634 |
} |
| 4635 |
auto matcher = lambdaExpr(hasAnyCapture(lambdaCapture(capturesThis()))); |
| 4636 |
EXPECT_TRUE(matches("class C { int cc; int f() { auto l = [this](){ return " |
| 4637 |
"cc; }; return l(); } };", |
| 4638 |
matcher)); |
| 4639 |
EXPECT_TRUE(matches("class C { int cc; int f() { auto l = [=](){ return cc; " |
| 4640 |
"}; return l(); } };", |
| 4641 |
matcher)); |
| 4642 |
EXPECT_TRUE(matches("class C { int cc; int f() { auto l = [&](){ return cc; " |
| 4643 |
"}; return l(); } };", |
| 4644 |
matcher)); |
| 4645 |
EXPECT_FALSE(matches("class C { int cc; int f() { auto l = [cc](){ return " |
| 4646 |
"cc; }; return l(); } };", |
| 4647 |
matcher)); |
| 4648 |
EXPECT_FALSE(matches("class C { int this; int f() { auto l = [this](){ " |
| 4649 |
"return this; }; return l(); } };", |
| 4650 |
matcher)); |
| 4651 |
} |
| 4652 |
|
| 4653 |
TEST_P(ASTMatchersTest, IsImplicit_LambdaCapture) { |
| 4654 |
if (!GetParam().isCXX11OrLater()) { |
| 4655 |
return; |
| 4656 |
} |
| 4657 |
auto matcher = lambdaExpr(hasAnyCapture( |
| 4658 |
lambdaCapture(isImplicit(), capturesVar(varDecl(hasName("cc")))))); |
| 4659 |
EXPECT_TRUE( |
| 4660 |
matches("int main() { int cc; auto f = [&](){ return cc; }; }", matcher)); |
| 4661 |
EXPECT_TRUE( |
| 4662 |
matches("int main() { int cc; auto f = [=](){ return cc; }; }", matcher)); |
| 4663 |
EXPECT_FALSE(matches("int main() { int cc; auto f = [cc](){ return cc; }; }", |
| 4664 |
matcher)); |
| 4665 |
} |
| 4666 |
|
| 4667 |
} // namespace ast_matchers |
| 4668 |
} // namespace clang |