| 1 |
//= unittests/ASTMatchers/ASTMatchersTraversalTest.cpp - matchers 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/Attrs.inc" |
| 11 |
#include "clang/AST/PrettyPrinter.h" |
| 12 |
#include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 |
#include "clang/ASTMatchers/ASTMatchers.h" |
| 14 |
#include "clang/Tooling/Tooling.h" |
| 15 |
#include "llvm/ADT/StringRef.h" |
| 16 |
#include "llvm/ADT/Triple.h" |
| 17 |
#include "llvm/Support/Host.h" |
| 18 |
#include "gtest/gtest.h" |
| 19 |
|
| 20 |
namespace clang { |
| 21 |
namespace ast_matchers { |
| 22 |
|
| 23 |
TEST(DeclarationMatcher, hasMethod) { |
| 24 |
EXPECT_TRUE(matches("class A { void func(); };", |
| 25 |
cxxRecordDecl(hasMethod(hasName("func"))))); |
| 26 |
EXPECT_TRUE(notMatches("class A { void func(); };", |
| 27 |
cxxRecordDecl(hasMethod(isPublic())))); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) { |
| 31 |
EXPECT_TRUE(matches( |
| 32 |
"template <typename T> struct A {" |
| 33 |
" template <typename T2> struct F {};" |
| 34 |
"};" |
| 35 |
"template <typename T> struct B : A<T>::template F<T> {};" |
| 36 |
"B<int> b;", |
| 37 |
cxxRecordDecl(hasName("B"), isDerivedFrom(recordDecl())))); |
| 38 |
} |
| 39 |
|
| 40 |
TEST(DeclarationMatcher, hasDeclContext) { |
| 41 |
EXPECT_TRUE(matches( |
| 42 |
"namespace N {" |
| 43 |
" namespace M {" |
| 44 |
" class D {};" |
| 45 |
" }" |
| 46 |
"}", |
| 47 |
recordDecl(hasDeclContext(namespaceDecl(hasName("M")))))); |
| 48 |
EXPECT_TRUE(notMatches( |
| 49 |
"namespace N {" |
| 50 |
" namespace M {" |
| 51 |
" class D {};" |
| 52 |
" }" |
| 53 |
"}", |
| 54 |
recordDecl(hasDeclContext(namespaceDecl(hasName("N")))))); |
| 55 |
|
| 56 |
EXPECT_TRUE(matches("namespace {" |
| 57 |
" namespace M {" |
| 58 |
" class D {};" |
| 59 |
" }" |
| 60 |
"}", |
| 61 |
recordDecl(hasDeclContext(namespaceDecl( |
| 62 |
hasName("M"), hasDeclContext(namespaceDecl())))))); |
| 63 |
|
| 64 |
EXPECT_TRUE(matches("class D{};", decl(hasDeclContext(decl())))); |
| 65 |
} |
| 66 |
|
| 67 |
TEST(HasDescendant, MatchesDescendantTypes) { |
| 68 |
EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 69 |
decl(hasDescendant(loc(builtinType()))))); |
| 70 |
EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 71 |
stmt(hasDescendant(builtinType())))); |
| 72 |
|
| 73 |
EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 74 |
stmt(hasDescendant(loc(builtinType()))))); |
| 75 |
EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 76 |
stmt(hasDescendant(qualType(builtinType()))))); |
| 77 |
|
| 78 |
EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }", |
| 79 |
stmt(hasDescendant(isInteger())))); |
| 80 |
|
| 81 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 82 |
"void f() { int a; float c; int d; int e; }", |
| 83 |
functionDecl(forEachDescendant( |
| 84 |
varDecl(hasDescendant(isInteger())).bind("x"))), |
| 85 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 3))); |
| 86 |
} |
| 87 |
|
| 88 |
TEST(HasDescendant, MatchesDescendantsOfTypes) { |
| 89 |
EXPECT_TRUE(matches("void f() { int*** i; }", |
| 90 |
qualType(hasDescendant(builtinType())))); |
| 91 |
EXPECT_TRUE(matches("void f() { int*** i; }", |
| 92 |
qualType(hasDescendant( |
| 93 |
pointerType(pointee(builtinType())))))); |
| 94 |
EXPECT_TRUE(matches("void f() { int*** i; }", |
| 95 |
typeLoc(hasDescendant(loc(builtinType()))))); |
| 96 |
|
| 97 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 98 |
"void f() { int*** i; }", |
| 99 |
qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))), |
| 100 |
std::make_unique<VerifyIdIsBoundTo<Type>>("x", 2))); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
TEST(Has, MatchesChildrenOfTypes) { |
| 105 |
EXPECT_TRUE(matches("int i;", |
| 106 |
varDecl(hasName("i"), has(isInteger())))); |
| 107 |
EXPECT_TRUE(notMatches("int** i;", |
| 108 |
varDecl(hasName("i"), has(isInteger())))); |
| 109 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 110 |
"int (*f)(float, int);", |
| 111 |
qualType(functionType(), forEach(qualType(isInteger()).bind("x"))), |
| 112 |
std::make_unique<VerifyIdIsBoundTo<QualType>>("x", 2))); |
| 113 |
} |
| 114 |
|
| 115 |
TEST(Has, MatchesChildTypes) { |
| 116 |
EXPECT_TRUE(matches( |
| 117 |
"int* i;", |
| 118 |
varDecl(hasName("i"), hasType(qualType(has(builtinType())))))); |
| 119 |
EXPECT_TRUE(notMatches( |
| 120 |
"int* i;", |
| 121 |
varDecl(hasName("i"), hasType(qualType(has(pointerType())))))); |
| 122 |
} |
| 123 |
|
| 124 |
TEST(StatementMatcher, Has) { |
| 125 |
StatementMatcher HasVariableI = |
| 126 |
expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 127 |
has(ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("i"))))))); |
| 128 |
|
| 129 |
EXPECT_TRUE(matches( |
| 130 |
"class X; X *x(int); void c() { int i; x(i); }", HasVariableI)); |
| 131 |
EXPECT_TRUE(notMatches( |
| 132 |
"class X; X *x(int); void c() { int i; x(42); }", HasVariableI)); |
| 133 |
} |
| 134 |
|
| 135 |
TEST(StatementMatcher, HasDescendant) { |
| 136 |
StatementMatcher HasDescendantVariableI = |
| 137 |
expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 138 |
hasDescendant(declRefExpr(to(varDecl(hasName("i")))))); |
| 139 |
|
| 140 |
EXPECT_TRUE(matches( |
| 141 |
"class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }", |
| 142 |
HasDescendantVariableI)); |
| 143 |
EXPECT_TRUE(notMatches( |
| 144 |
"class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }", |
| 145 |
HasDescendantVariableI)); |
| 146 |
} |
| 147 |
|
| 148 |
TEST(TypeMatcher, MatchesClassType) { |
| 149 |
TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A"))); |
| 150 |
|
| 151 |
EXPECT_TRUE(matches("class A { public: A *a; };", TypeA)); |
| 152 |
EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 153 |
|
| 154 |
TypeMatcher TypeDerivedFromA = |
| 155 |
hasDeclaration(cxxRecordDecl(isDerivedFrom("A"))); |
| 156 |
|
| 157 |
EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };", |
| 158 |
TypeDerivedFromA)); |
| 159 |
EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 160 |
|
| 161 |
TypeMatcher TypeAHasClassB = hasDeclaration( |
| 162 |
recordDecl(hasName("A"), has(recordDecl(hasName("B"))))); |
| 163 |
|
| 164 |
EXPECT_TRUE( |
| 165 |
matches("class A { public: A *a; class B {}; };", TypeAHasClassB)); |
| 166 |
|
| 167 |
EXPECT_TRUE(matchesC("struct S {}; void f(void) { struct S s; }", |
| 168 |
varDecl(hasType(namedDecl(hasName("S")))))); |
| 169 |
} |
| 170 |
|
| 171 |
TEST(TypeMatcher, MatchesDeclTypes) { |
| 172 |
// TypedefType -> TypedefNameDecl |
| 173 |
EXPECT_TRUE(matches("typedef int I; void f(I i);", |
| 174 |
parmVarDecl(hasType(namedDecl(hasName("I")))))); |
| 175 |
// ObjCObjectPointerType |
| 176 |
EXPECT_TRUE(matchesObjC("@interface Foo @end void f(Foo *f);", |
| 177 |
parmVarDecl(hasType(objcObjectPointerType())))); |
| 178 |
// ObjCObjectPointerType -> ObjCInterfaceType -> ObjCInterfaceDecl |
| 179 |
EXPECT_TRUE(matchesObjC( |
| 180 |
"@interface Foo @end void f(Foo *f);", |
| 181 |
parmVarDecl(hasType(pointsTo(objcInterfaceDecl(hasName("Foo"))))))); |
| 182 |
// TemplateTypeParmType |
| 183 |
EXPECT_TRUE(matches("template <typename T> void f(T t);", |
| 184 |
parmVarDecl(hasType(templateTypeParmType())))); |
| 185 |
// TemplateTypeParmType -> TemplateTypeParmDecl |
| 186 |
EXPECT_TRUE(matches("template <typename T> void f(T t);", |
| 187 |
parmVarDecl(hasType(namedDecl(hasName("T")))))); |
| 188 |
// InjectedClassNameType |
| 189 |
EXPECT_TRUE(matches("template <typename T> struct S {" |
| 190 |
" void f(S s);" |
| 191 |
"};", |
| 192 |
parmVarDecl(hasType(elaboratedType( |
| 193 |
namesType(injectedClassNameType())))))); |
| 194 |
EXPECT_TRUE(notMatches("template <typename T> struct S {" |
| 195 |
" void g(S<T> s);" |
| 196 |
"};", |
| 197 |
parmVarDecl(hasType(elaboratedType( |
| 198 |
namesType(injectedClassNameType())))))); |
| 199 |
// InjectedClassNameType -> CXXRecordDecl |
| 200 |
EXPECT_TRUE(matches("template <typename T> struct S {" |
| 201 |
" void f(S s);" |
| 202 |
"};", |
| 203 |
parmVarDecl(hasType(namedDecl(hasName("S")))))); |
| 204 |
|
| 205 |
static const char Using[] = "template <typename T>" |
| 206 |
"struct Base {" |
| 207 |
" typedef T Foo;" |
| 208 |
"};" |
| 209 |
"" |
| 210 |
"template <typename T>" |
| 211 |
"struct S : private Base<T> {" |
| 212 |
" using typename Base<T>::Foo;" |
| 213 |
" void f(Foo);" |
| 214 |
"};"; |
| 215 |
// UnresolvedUsingTypenameDecl |
| 216 |
EXPECT_TRUE(matches(Using, unresolvedUsingTypenameDecl(hasName("Foo")))); |
| 217 |
// UnresolvedUsingTypenameType -> UnresolvedUsingTypenameDecl |
| 218 |
EXPECT_TRUE(matches(Using, parmVarDecl(hasType(namedDecl(hasName("Foo")))))); |
| 219 |
} |
| 220 |
|
| 221 |
TEST(HasDeclaration, HasDeclarationOfEnumType) { |
| 222 |
EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }", |
| 223 |
expr(hasType(pointsTo( |
| 224 |
qualType(hasDeclaration(enumDecl(hasName("X"))))))))); |
| 225 |
} |
| 226 |
|
| 227 |
TEST(HasDeclaration, HasGetDeclTraitTest) { |
| 228 |
static_assert(internal::has_getDecl<TypedefType>::value, |
| 229 |
"Expected TypedefType to have a getDecl."); |
| 230 |
static_assert(internal::has_getDecl<RecordType>::value, |
| 231 |
"Expected RecordType to have a getDecl."); |
| 232 |
static_assert(!internal::has_getDecl<TemplateSpecializationType>::value, |
| 233 |
"Expected TemplateSpecializationType to *not* have a getDecl."); |
| 234 |
} |
| 235 |
|
| 236 |
TEST(HasDeclaration, ElaboratedType) { |
| 237 |
EXPECT_TRUE(matches( |
| 238 |
"namespace n { template <typename T> struct X {}; }" |
| 239 |
"void f(n::X<int>);", |
| 240 |
parmVarDecl(hasType(qualType(hasDeclaration(cxxRecordDecl())))))); |
| 241 |
EXPECT_TRUE(matches( |
| 242 |
"namespace n { template <typename T> struct X {}; }" |
| 243 |
"void f(n::X<int>);", |
| 244 |
parmVarDecl(hasType(elaboratedType(hasDeclaration(cxxRecordDecl())))))); |
| 245 |
} |
| 246 |
|
| 247 |
TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) { |
| 248 |
EXPECT_TRUE(matches( |
| 249 |
"typedef int X; X a;", |
| 250 |
varDecl(hasName("a"), hasType(elaboratedType(namesType( |
| 251 |
typedefType(hasDeclaration(decl())))))))); |
| 252 |
|
| 253 |
// FIXME: Add tests for other types with getDecl() (e.g. RecordType) |
| 254 |
} |
| 255 |
|
| 256 |
TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) { |
| 257 |
EXPECT_TRUE(matches( |
| 258 |
"template <typename T> class A {}; A<int> a;", |
| 259 |
varDecl(hasType(elaboratedType(namesType(templateSpecializationType( |
| 260 |
hasDeclaration(namedDecl(hasName("A")))))))))); |
| 261 |
EXPECT_TRUE(matches( |
| 262 |
"template <typename T> class A {};" |
| 263 |
"template <typename T> class B { A<T> a; };", |
| 264 |
fieldDecl(hasType(elaboratedType(namesType(templateSpecializationType( |
| 265 |
hasDeclaration(namedDecl(hasName("A")))))))))); |
| 266 |
EXPECT_TRUE(matches( |
| 267 |
"template <typename T> class A {}; A<int> a;", |
| 268 |
varDecl(hasType(elaboratedType(namesType( |
| 269 |
templateSpecializationType(hasDeclaration(cxxRecordDecl())))))))); |
| 270 |
} |
| 271 |
|
| 272 |
TEST(HasDeclaration, HasDeclarationOfCXXNewExpr) { |
| 273 |
EXPECT_TRUE( |
| 274 |
matches("int *A = new int();", |
| 275 |
cxxNewExpr(hasDeclaration(functionDecl(parameterCountIs(1)))))); |
| 276 |
} |
| 277 |
|
| 278 |
TEST(HasDeclaration, HasDeclarationOfTypeAlias) { |
| 279 |
EXPECT_TRUE(matches( |
| 280 |
"template <typename T> using C = T; C<int> c;", |
| 281 |
varDecl(hasType(elaboratedType(namesType(templateSpecializationType( |
| 282 |
hasDeclaration(typeAliasTemplateDecl())))))))); |
| 283 |
} |
| 284 |
|
| 285 |
TEST(HasUnqualifiedDesugaredType, DesugarsUsing) { |
| 286 |
EXPECT_TRUE( |
| 287 |
matches("struct A {}; using B = A; B b;", |
| 288 |
varDecl(hasType(hasUnqualifiedDesugaredType(recordType()))))); |
| 289 |
EXPECT_TRUE( |
| 290 |
matches("struct A {}; using B = A; using C = B; C b;", |
| 291 |
varDecl(hasType(hasUnqualifiedDesugaredType(recordType()))))); |
| 292 |
} |
| 293 |
|
| 294 |
TEST(HasUnderlyingDecl, Matches) { |
| 295 |
EXPECT_TRUE(matches("namespace N { template <class T> void f(T t); }" |
| 296 |
"template <class T> void g() { using N::f; f(T()); }", |
| 297 |
unresolvedLookupExpr(hasAnyDeclaration( |
| 298 |
namedDecl(hasUnderlyingDecl(hasName("::N::f"))))))); |
| 299 |
EXPECT_TRUE(matches( |
| 300 |
"namespace N { template <class T> void f(T t); }" |
| 301 |
"template <class T> void g() { N::f(T()); }", |
| 302 |
unresolvedLookupExpr(hasAnyDeclaration(namedDecl(hasName("::N::f")))))); |
| 303 |
EXPECT_TRUE(notMatches( |
| 304 |
"namespace N { template <class T> void f(T t); }" |
| 305 |
"template <class T> void g() { using N::f; f(T()); }", |
| 306 |
unresolvedLookupExpr(hasAnyDeclaration(namedDecl(hasName("::N::f")))))); |
| 307 |
} |
| 308 |
|
| 309 |
TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
| 310 |
TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
| 311 |
EXPECT_TRUE( |
| 312 |
matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
| 313 |
EXPECT_TRUE( |
| 314 |
notMatches("class X {}; void y(X *x) { x; }", |
| 315 |
expr(hasType(ClassX)))); |
| 316 |
EXPECT_TRUE( |
| 317 |
matches("class X {}; void y(X *x) { x; }", |
| 318 |
expr(hasType(pointsTo(ClassX))))); |
| 319 |
} |
| 320 |
|
| 321 |
TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
| 322 |
TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
| 323 |
EXPECT_TRUE( |
| 324 |
matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
| 325 |
EXPECT_TRUE( |
| 326 |
notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
| 327 |
EXPECT_TRUE( |
| 328 |
matches("class X {}; void y() { X *x; }", |
| 329 |
varDecl(hasType(pointsTo(ClassX))))); |
| 330 |
} |
| 331 |
|
| 332 |
TEST(HasType, TakesQualTypeMatcherAndMatchesCXXBaseSpecifier) { |
| 333 |
TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
| 334 |
CXXBaseSpecifierMatcher BaseClassX = cxxBaseSpecifier(hasType(ClassX)); |
| 335 |
DeclarationMatcher ClassHasBaseClassX = |
| 336 |
cxxRecordDecl(hasDirectBase(BaseClassX)); |
| 337 |
EXPECT_TRUE(matches("class X {}; class Y : X {};", ClassHasBaseClassX)); |
| 338 |
EXPECT_TRUE(notMatches("class Z {}; class Y : Z {};", ClassHasBaseClassX)); |
| 339 |
} |
| 340 |
|
| 341 |
TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
| 342 |
DeclarationMatcher ClassX = recordDecl(hasName("X")); |
| 343 |
EXPECT_TRUE( |
| 344 |
matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
| 345 |
EXPECT_TRUE( |
| 346 |
notMatches("class X {}; void y(X *x) { x; }", |
| 347 |
expr(hasType(ClassX)))); |
| 348 |
} |
| 349 |
|
| 350 |
TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
| 351 |
DeclarationMatcher ClassX = recordDecl(hasName("X")); |
| 352 |
EXPECT_TRUE( |
| 353 |
matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
| 354 |
EXPECT_TRUE( |
| 355 |
notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
| 356 |
} |
| 357 |
|
| 358 |
TEST(HasType, TakesDeclMatcherAndMatchesCXXBaseSpecifier) { |
| 359 |
DeclarationMatcher ClassX = recordDecl(hasName("X")); |
| 360 |
CXXBaseSpecifierMatcher BaseClassX = cxxBaseSpecifier(hasType(ClassX)); |
| 361 |
DeclarationMatcher ClassHasBaseClassX = |
| 362 |
cxxRecordDecl(hasDirectBase(BaseClassX)); |
| 363 |
EXPECT_TRUE(matches("class X {}; class Y : X {};", ClassHasBaseClassX)); |
| 364 |
EXPECT_TRUE(notMatches("class Z {}; class Y : Z {};", ClassHasBaseClassX)); |
| 365 |
} |
| 366 |
|
| 367 |
TEST(HasType, MatchesTypedefDecl) { |
| 368 |
EXPECT_TRUE(matches("typedef int X;", typedefDecl(hasType(asString("int"))))); |
| 369 |
EXPECT_TRUE(matches("typedef const int T;", |
| 370 |
typedefDecl(hasType(asString("const int"))))); |
| 371 |
EXPECT_TRUE(notMatches("typedef const int T;", |
| 372 |
typedefDecl(hasType(asString("int"))))); |
| 373 |
EXPECT_TRUE(matches("typedef int foo; typedef foo bar;", |
| 374 |
typedefDecl(hasType(asString("foo")), hasName("bar")))); |
| 375 |
} |
| 376 |
|
| 377 |
TEST(HasType, MatchesTypedefNameDecl) { |
| 378 |
EXPECT_TRUE(matches("using X = int;", typedefNameDecl(hasType(asString("int"))))); |
| 379 |
EXPECT_TRUE(matches("using T = const int;", |
| 380 |
typedefNameDecl(hasType(asString("const int"))))); |
| 381 |
EXPECT_TRUE(notMatches("using T = const int;", |
| 382 |
typedefNameDecl(hasType(asString("int"))))); |
| 383 |
EXPECT_TRUE(matches("using foo = int; using bar = foo;", |
| 384 |
typedefNameDecl(hasType(asString("foo")), hasName("bar")))); |
| 385 |
} |
| 386 |
|
| 387 |
TEST(HasTypeLoc, MatchesBlockDecl) { |
| 388 |
EXPECT_TRUE(matchesConditionally( |
| 389 |
"auto x = ^int (int a, int b) { return a + b; };", |
| 390 |
blockDecl(hasTypeLoc(loc(asString("int (int, int)")))), true, |
| 391 |
{"-fblocks"})); |
| 392 |
} |
| 393 |
|
| 394 |
TEST(HasTypeLoc, MatchesCXXBaseSpecifierAndCtorInitializer) { |
| 395 |
llvm::StringRef code = R"cpp( |
| 396 |
class Foo {}; |
| 397 |
class Bar : public Foo { |
| 398 |
Bar() : Foo() {} |
| 399 |
}; |
| 400 |
)cpp"; |
| 401 |
|
| 402 |
EXPECT_TRUE(matches( |
| 403 |
code, cxxRecordDecl(hasAnyBase(hasTypeLoc(loc(asString("Foo"))))))); |
| 404 |
EXPECT_TRUE( |
| 405 |
matches(code, cxxCtorInitializer(hasTypeLoc(loc(asString("Foo")))))); |
| 406 |
} |
| 407 |
|
| 408 |
TEST(HasTypeLoc, MatchesCXXFunctionalCastExpr) { |
| 409 |
EXPECT_TRUE(matches("auto x = int(3);", |
| 410 |
cxxFunctionalCastExpr(hasTypeLoc(loc(asString("int")))))); |
| 411 |
} |
| 412 |
|
| 413 |
TEST(HasTypeLoc, MatchesCXXNewExpr) { |
| 414 |
EXPECT_TRUE(matches("auto* x = new int(3);", |
| 415 |
cxxNewExpr(hasTypeLoc(loc(asString("int")))))); |
| 416 |
EXPECT_TRUE(matches("class Foo{}; auto* x = new Foo();", |
| 417 |
cxxNewExpr(hasTypeLoc(loc(asString("Foo")))))); |
| 418 |
} |
| 419 |
|
| 420 |
TEST(HasTypeLoc, MatchesCXXTemporaryObjectExpr) { |
| 421 |
EXPECT_TRUE( |
| 422 |
matches("struct Foo { Foo(int, int); }; auto x = Foo(1, 2);", |
| 423 |
cxxTemporaryObjectExpr(hasTypeLoc(loc(asString("Foo")))))); |
| 424 |
} |
| 425 |
|
| 426 |
TEST(HasTypeLoc, MatchesCXXUnresolvedConstructExpr) { |
| 427 |
EXPECT_TRUE( |
| 428 |
matches("template <typename T> T make() { return T(); }", |
| 429 |
cxxUnresolvedConstructExpr(hasTypeLoc(loc(asString("T")))))); |
| 430 |
} |
| 431 |
|
| 432 |
TEST(HasTypeLoc, MatchesClassTemplateSpecializationDecl) { |
| 433 |
EXPECT_TRUE(matches( |
| 434 |
"template <typename T> class Foo; template <> class Foo<int> {};", |
| 435 |
classTemplateSpecializationDecl(hasTypeLoc(loc(asString("Foo<int>")))))); |
| 436 |
} |
| 437 |
|
| 438 |
TEST(HasTypeLoc, MatchesCompoundLiteralExpr) { |
| 439 |
EXPECT_TRUE( |
| 440 |
matches("int* x = (int[2]) { 0, 1 };", |
| 441 |
compoundLiteralExpr(hasTypeLoc(loc(asString("int[2]")))))); |
| 442 |
} |
| 443 |
|
| 444 |
TEST(HasTypeLoc, MatchesDeclaratorDecl) { |
| 445 |
EXPECT_TRUE(matches("int x;", |
| 446 |
varDecl(hasName("x"), hasTypeLoc(loc(asString("int")))))); |
| 447 |
EXPECT_TRUE(matches("int x(3);", |
| 448 |
varDecl(hasName("x"), hasTypeLoc(loc(asString("int")))))); |
| 449 |
EXPECT_TRUE(matches("struct Foo { Foo(int, int); }; Foo x(1, 2);", |
| 450 |
varDecl(hasName("x"), hasTypeLoc(loc(asString("Foo")))))); |
| 451 |
|
| 452 |
// Make sure we don't crash on implicit constructors. |
| 453 |
EXPECT_TRUE(notMatches("class X {}; X x;", |
| 454 |
declaratorDecl(hasTypeLoc(loc(asString("int")))))); |
| 455 |
} |
| 456 |
|
| 457 |
TEST(HasTypeLoc, MatchesExplicitCastExpr) { |
| 458 |
EXPECT_TRUE(matches("auto x = (int) 3;", |
| 459 |
explicitCastExpr(hasTypeLoc(loc(asString("int")))))); |
| 460 |
EXPECT_TRUE(matches("auto x = static_cast<int>(3);", |
| 461 |
explicitCastExpr(hasTypeLoc(loc(asString("int")))))); |
| 462 |
} |
| 463 |
|
| 464 |
TEST(HasTypeLoc, MatchesObjCPropertyDecl) { |
| 465 |
EXPECT_TRUE(matchesObjC(R"objc( |
| 466 |
@interface Foo |
| 467 |
@property int enabled; |
| 468 |
@end |
| 469 |
)objc", |
| 470 |
objcPropertyDecl(hasTypeLoc(loc(asString("int")))))); |
| 471 |
} |
| 472 |
|
| 473 |
TEST(HasTypeLoc, MatchesTemplateArgumentLoc) { |
| 474 |
EXPECT_TRUE(matches("template <typename T> class Foo {}; Foo<int> x;", |
| 475 |
templateArgumentLoc(hasTypeLoc(loc(asString("int")))))); |
| 476 |
} |
| 477 |
|
| 478 |
TEST(HasTypeLoc, MatchesTypedefNameDecl) { |
| 479 |
EXPECT_TRUE(matches("typedef int X;", |
| 480 |
typedefNameDecl(hasTypeLoc(loc(asString("int")))))); |
| 481 |
EXPECT_TRUE(matches("using X = int;", |
| 482 |
typedefNameDecl(hasTypeLoc(loc(asString("int")))))); |
| 483 |
} |
| 484 |
|
| 485 |
TEST(Callee, MatchesDeclarations) { |
| 486 |
StatementMatcher CallMethodX = callExpr(callee(cxxMethodDecl(hasName("x")))); |
| 487 |
|
| 488 |
EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 489 |
EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 490 |
|
| 491 |
CallMethodX = traverse(TK_AsIs, callExpr(callee(cxxConversionDecl()))); |
| 492 |
EXPECT_TRUE( |
| 493 |
matches("struct Y { operator int() const; }; int i = Y();", CallMethodX)); |
| 494 |
EXPECT_TRUE(notMatches("struct Y { operator int() const; }; Y y = Y();", |
| 495 |
CallMethodX)); |
| 496 |
} |
| 497 |
|
| 498 |
TEST(Callee, MatchesMemberExpressions) { |
| 499 |
EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
| 500 |
callExpr(callee(memberExpr())))); |
| 501 |
EXPECT_TRUE( |
| 502 |
notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr())))); |
| 503 |
} |
| 504 |
|
| 505 |
TEST(Matcher, Argument) { |
| 506 |
StatementMatcher CallArgumentY = callExpr( |
| 507 |
hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
| 508 |
|
| 509 |
EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 510 |
EXPECT_TRUE( |
| 511 |
matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 512 |
EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 513 |
|
| 514 |
StatementMatcher WrongIndex = callExpr( |
| 515 |
hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
| 516 |
EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 517 |
} |
| 518 |
|
| 519 |
TEST(Matcher, AnyArgument) { |
| 520 |
auto HasArgumentY = hasAnyArgument( |
| 521 |
ignoringParenImpCasts(declRefExpr(to(varDecl(hasName("y")))))); |
| 522 |
StatementMatcher CallArgumentY = callExpr(HasArgumentY); |
| 523 |
StatementMatcher CtorArgumentY = cxxConstructExpr(HasArgumentY); |
| 524 |
StatementMatcher UnresolvedCtorArgumentY = |
| 525 |
cxxUnresolvedConstructExpr(HasArgumentY); |
| 526 |
StatementMatcher ObjCCallArgumentY = objcMessageExpr(HasArgumentY); |
| 527 |
EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 528 |
EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 529 |
EXPECT_TRUE(matches("struct Y { Y(int, int); };" |
| 530 |
"void x() { int y; (void)Y(1, y); }", |
| 531 |
CtorArgumentY)); |
| 532 |
EXPECT_TRUE(matches("struct Y { Y(int, int); };" |
| 533 |
"void x() { int y; (void)Y(y, 42); }", |
| 534 |
CtorArgumentY)); |
| 535 |
EXPECT_TRUE(matches("template <class Y> void x() { int y; (void)Y(1, y); }", |
| 536 |
UnresolvedCtorArgumentY)); |
| 537 |
EXPECT_TRUE(matches("template <class Y> void x() { int y; (void)Y(y, 42); }", |
| 538 |
UnresolvedCtorArgumentY)); |
| 539 |
EXPECT_TRUE(matchesObjC("@interface I -(void)f:(int) y; @end " |
| 540 |
"void x(I* i) { int y; [i f:y]; }", |
| 541 |
ObjCCallArgumentY)); |
| 542 |
EXPECT_FALSE(matchesObjC("@interface I -(void)f:(int) z; @end " |
| 543 |
"void x(I* i) { int z; [i f:z]; }", |
| 544 |
ObjCCallArgumentY)); |
| 545 |
EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 546 |
EXPECT_TRUE(notMatches("struct Y { Y(int, int); };" |
| 547 |
"void x() { int y; (void)Y(1, 2); }", |
| 548 |
CtorArgumentY)); |
| 549 |
EXPECT_TRUE(notMatches("template <class Y>" |
| 550 |
"void x() { int y; (void)Y(1, 2); }", |
| 551 |
UnresolvedCtorArgumentY)); |
| 552 |
|
| 553 |
StatementMatcher ImplicitCastedArgument = |
| 554 |
traverse(TK_AsIs, callExpr(hasAnyArgument(implicitCastExpr()))); |
| 555 |
EXPECT_TRUE(matches("void x(long) { int y; x(y); }", ImplicitCastedArgument)); |
| 556 |
} |
| 557 |
|
| 558 |
TEST(Matcher, HasReceiver) { |
| 559 |
EXPECT_TRUE(matchesObjC( |
| 560 |
"@interface NSString @end " |
| 561 |
"void f(NSString *x) {" |
| 562 |
"[x containsString];" |
| 563 |
"}", |
| 564 |
objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))))); |
| 565 |
|
| 566 |
EXPECT_FALSE(matchesObjC( |
| 567 |
"@interface NSString +(NSString *) stringWithFormat; @end " |
| 568 |
"void f() { [NSString stringWithFormat]; }", |
| 569 |
objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x")))))))); |
| 570 |
} |
| 571 |
|
| 572 |
TEST(Matcher, MatchesMethodsOnLambda) { |
| 573 |
StringRef Code = R"cpp( |
| 574 |
struct A { |
| 575 |
~A() {} |
| 576 |
}; |
| 577 |
void foo() |
| 578 |
{ |
| 579 |
A a; |
| 580 |
auto l = [a] { }; |
| 581 |
auto lCopy = l; |
| 582 |
auto lPtrDecay = +[] { }; |
| 583 |
(void)lPtrDecay; |
| 584 |
} |
| 585 |
)cpp"; |
| 586 |
|
| 587 |
EXPECT_TRUE(matches( |
| 588 |
Code, cxxConstructorDecl( |
| 589 |
hasBody(compoundStmt()), |
| 590 |
hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l"))))), |
| 591 |
isCopyConstructor()))); |
| 592 |
EXPECT_TRUE(matches( |
| 593 |
Code, cxxConstructorDecl( |
| 594 |
hasBody(compoundStmt()), |
| 595 |
hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l"))))), |
| 596 |
isMoveConstructor()))); |
| 597 |
EXPECT_TRUE(matches( |
| 598 |
Code, cxxDestructorDecl( |
| 599 |
hasBody(compoundStmt()), |
| 600 |
hasAncestor(lambdaExpr(hasAncestor(varDecl(hasName("l")))))))); |
| 601 |
EXPECT_TRUE(matches( |
| 602 |
Code, cxxConversionDecl(hasBody(compoundStmt(has(returnStmt( |
| 603 |
hasReturnValue(implicitCastExpr()))))), |
| 604 |
hasAncestor(lambdaExpr(hasAncestor( |
| 605 |
varDecl(hasName("lPtrDecay")))))))); |
| 606 |
} |
| 607 |
|
| 608 |
TEST(Matcher, MatchesCoroutine) { |
| 609 |
FileContentMappings M; |
| 610 |
M.push_back(std::make_pair("/coro_header", R"cpp( |
| 611 |
namespace std { |
| 612 |
|
| 613 |
template <class... Args> |
| 614 |
struct void_t_imp { |
| 615 |
using type = void; |
| 616 |
}; |
| 617 |
template <class... Args> |
| 618 |
using void_t = typename void_t_imp<Args...>::type; |
| 619 |
|
| 620 |
template <class T, class = void> |
| 621 |
struct traits_sfinae_base {}; |
| 622 |
|
| 623 |
template <class T> |
| 624 |
struct traits_sfinae_base<T, void_t<typename T::promise_type>> { |
| 625 |
using promise_type = typename T::promise_type; |
| 626 |
}; |
| 627 |
|
| 628 |
template <class Ret, class... Args> |
| 629 |
struct coroutine_traits : public traits_sfinae_base<Ret> {}; |
| 630 |
} // namespace std |
| 631 |
struct awaitable { |
| 632 |
bool await_ready() noexcept; |
| 633 |
template <typename F> |
| 634 |
void await_suspend(F) noexcept; |
| 635 |
void await_resume() noexcept; |
| 636 |
} a; |
| 637 |
struct promise { |
| 638 |
void get_return_object(); |
| 639 |
awaitable initial_suspend(); |
| 640 |
awaitable final_suspend() noexcept; |
| 641 |
awaitable yield_value(int); // expected-note 2{{candidate}} |
| 642 |
void return_value(int); // expected-note 2{{here}} |
| 643 |
void unhandled_exception(); |
| 644 |
}; |
| 645 |
template <typename... T> |
| 646 |
struct std::coroutine_traits<void, T...> { using promise_type = promise; }; |
| 647 |
namespace std { |
| 648 |
template <class PromiseType = void> |
| 649 |
struct coroutine_handle { |
| 650 |
static coroutine_handle from_address(void *) noexcept; |
| 651 |
}; |
| 652 |
} // namespace std |
| 653 |
)cpp")); |
| 654 |
StringRef CoReturnCode = R"cpp( |
| 655 |
#include <coro_header> |
| 656 |
void check_match_co_return() { |
| 657 |
co_return 1; |
| 658 |
} |
| 659 |
)cpp"; |
| 660 |
EXPECT_TRUE(matchesConditionally(CoReturnCode, |
| 661 |
coreturnStmt(isExpansionInMainFile()), |
| 662 |
true, {"-std=c++20", "-I/"}, M)); |
| 663 |
StringRef CoAwaitCode = R"cpp( |
| 664 |
#include <coro_header> |
| 665 |
void check_match_co_await() { |
| 666 |
co_await a; |
| 667 |
} |
| 668 |
)cpp"; |
| 669 |
EXPECT_TRUE(matchesConditionally(CoAwaitCode, |
| 670 |
coawaitExpr(isExpansionInMainFile()), |
| 671 |
true, {"-std=c++20", "-I/"}, M)); |
| 672 |
StringRef CoYieldCode = R"cpp( |
| 673 |
#include <coro_header> |
| 674 |
void check_match_co_yield() { |
| 675 |
co_yield 1.0; |
| 676 |
} |
| 677 |
)cpp"; |
| 678 |
EXPECT_TRUE(matchesConditionally(CoYieldCode, |
| 679 |
coyieldExpr(isExpansionInMainFile()), |
| 680 |
true, {"-std=c++20", "-I/"}, M)); |
| 681 |
} |
| 682 |
|
| 683 |
TEST(Matcher, isClassMessage) { |
| 684 |
EXPECT_TRUE(matchesObjC( |
| 685 |
"@interface NSString +(NSString *) stringWithFormat; @end " |
| 686 |
"void f() { [NSString stringWithFormat]; }", |
| 687 |
objcMessageExpr(isClassMessage()))); |
| 688 |
|
| 689 |
EXPECT_FALSE(matchesObjC( |
| 690 |
"@interface NSString @end " |
| 691 |
"void f(NSString *x) {" |
| 692 |
"[x containsString];" |
| 693 |
"}", |
| 694 |
objcMessageExpr(isClassMessage()))); |
| 695 |
} |
| 696 |
|
| 697 |
TEST(Matcher, isInstanceMessage) { |
| 698 |
EXPECT_TRUE(matchesObjC( |
| 699 |
"@interface NSString @end " |
| 700 |
"void f(NSString *x) {" |
| 701 |
"[x containsString];" |
| 702 |
"}", |
| 703 |
objcMessageExpr(isInstanceMessage()))); |
| 704 |
|
| 705 |
EXPECT_FALSE(matchesObjC( |
| 706 |
"@interface NSString +(NSString *) stringWithFormat; @end " |
| 707 |
"void f() { [NSString stringWithFormat]; }", |
| 708 |
objcMessageExpr(isInstanceMessage()))); |
| 709 |
|
| 710 |
} |
| 711 |
|
| 712 |
TEST(Matcher, isClassMethod) { |
| 713 |
EXPECT_TRUE(matchesObjC( |
| 714 |
"@interface Bar + (void)bar; @end", |
| 715 |
objcMethodDecl(isClassMethod()))); |
| 716 |
|
| 717 |
EXPECT_TRUE(matchesObjC( |
| 718 |
"@interface Bar @end" |
| 719 |
"@implementation Bar + (void)bar {} @end", |
| 720 |
objcMethodDecl(isClassMethod()))); |
| 721 |
|
| 722 |
EXPECT_FALSE(matchesObjC( |
| 723 |
"@interface Foo - (void)foo; @end", |
| 724 |
objcMethodDecl(isClassMethod()))); |
| 725 |
|
| 726 |
EXPECT_FALSE(matchesObjC( |
| 727 |
"@interface Foo @end " |
| 728 |
"@implementation Foo - (void)foo {} @end", |
| 729 |
objcMethodDecl(isClassMethod()))); |
| 730 |
} |
| 731 |
|
| 732 |
TEST(Matcher, isInstanceMethod) { |
| 733 |
EXPECT_TRUE(matchesObjC( |
| 734 |
"@interface Foo - (void)foo; @end", |
| 735 |
objcMethodDecl(isInstanceMethod()))); |
| 736 |
|
| 737 |
EXPECT_TRUE(matchesObjC( |
| 738 |
"@interface Foo @end " |
| 739 |
"@implementation Foo - (void)foo {} @end", |
| 740 |
objcMethodDecl(isInstanceMethod()))); |
| 741 |
|
| 742 |
EXPECT_FALSE(matchesObjC( |
| 743 |
"@interface Bar + (void)bar; @end", |
| 744 |
objcMethodDecl(isInstanceMethod()))); |
| 745 |
|
| 746 |
EXPECT_FALSE(matchesObjC( |
| 747 |
"@interface Bar @end" |
| 748 |
"@implementation Bar + (void)bar {} @end", |
| 749 |
objcMethodDecl(isInstanceMethod()))); |
| 750 |
} |
| 751 |
|
| 752 |
TEST(MatcherCXXMemberCallExpr, On) { |
| 753 |
StringRef Snippet1 = R"cc( |
| 754 |
struct Y { |
| 755 |
void m(); |
| 756 |
}; |
| 757 |
void z(Y y) { y.m(); } |
| 758 |
)cc"; |
| 759 |
StringRef Snippet2 = R"cc( |
| 760 |
struct Y { |
| 761 |
void m(); |
| 762 |
}; |
| 763 |
struct X : public Y {}; |
| 764 |
void z(X x) { x.m(); } |
| 765 |
)cc"; |
| 766 |
auto MatchesY = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y"))))); |
| 767 |
EXPECT_TRUE(matches(Snippet1, MatchesY)); |
| 768 |
EXPECT_TRUE(notMatches(Snippet2, MatchesY)); |
| 769 |
|
| 770 |
auto MatchesX = cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("X"))))); |
| 771 |
EXPECT_TRUE(matches(Snippet2, MatchesX)); |
| 772 |
|
| 773 |
// Parens are ignored. |
| 774 |
StringRef Snippet3 = R"cc( |
| 775 |
struct Y { |
| 776 |
void m(); |
| 777 |
}; |
| 778 |
Y g(); |
| 779 |
void z(Y y) { (g()).m(); } |
| 780 |
)cc"; |
| 781 |
auto MatchesCall = cxxMemberCallExpr(on(callExpr())); |
| 782 |
EXPECT_TRUE(matches(Snippet3, MatchesCall)); |
| 783 |
} |
| 784 |
|
| 785 |
TEST(MatcherCXXMemberCallExpr, OnImplicitObjectArgument) { |
| 786 |
StringRef Snippet1 = R"cc( |
| 787 |
struct Y { |
| 788 |
void m(); |
| 789 |
}; |
| 790 |
void z(Y y) { y.m(); } |
| 791 |
)cc"; |
| 792 |
StringRef Snippet2 = R"cc( |
| 793 |
struct Y { |
| 794 |
void m(); |
| 795 |
}; |
| 796 |
struct X : public Y {}; |
| 797 |
void z(X x) { x.m(); } |
| 798 |
)cc"; |
| 799 |
auto MatchesY = traverse(TK_AsIs, cxxMemberCallExpr(onImplicitObjectArgument( |
| 800 |
hasType(cxxRecordDecl(hasName("Y")))))); |
| 801 |
EXPECT_TRUE(matches(Snippet1, MatchesY)); |
| 802 |
EXPECT_TRUE(matches(Snippet2, MatchesY)); |
| 803 |
|
| 804 |
auto MatchesX = traverse(TK_AsIs, cxxMemberCallExpr(onImplicitObjectArgument( |
| 805 |
hasType(cxxRecordDecl(hasName("X")))))); |
| 806 |
EXPECT_TRUE(notMatches(Snippet2, MatchesX)); |
| 807 |
|
| 808 |
// Parens are not ignored. |
| 809 |
StringRef Snippet3 = R"cc( |
| 810 |
struct Y { |
| 811 |
void m(); |
| 812 |
}; |
| 813 |
Y g(); |
| 814 |
void z(Y y) { (g()).m(); } |
| 815 |
)cc"; |
| 816 |
auto MatchesCall = traverse( |
| 817 |
TK_AsIs, cxxMemberCallExpr(onImplicitObjectArgument(callExpr()))); |
| 818 |
EXPECT_TRUE(notMatches(Snippet3, MatchesCall)); |
| 819 |
} |
| 820 |
|
| 821 |
TEST(Matcher, HasObjectExpr) { |
| 822 |
StringRef Snippet1 = R"cc( |
| 823 |
struct X { |
| 824 |
int m; |
| 825 |
int f(X x) { return x.m; } |
| 826 |
}; |
| 827 |
)cc"; |
| 828 |
StringRef Snippet2 = R"cc( |
| 829 |
struct X { |
| 830 |
int m; |
| 831 |
int f(X x) { return m; } |
| 832 |
}; |
| 833 |
)cc"; |
| 834 |
auto MatchesX = |
| 835 |
memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X"))))); |
| 836 |
EXPECT_TRUE(matches(Snippet1, MatchesX)); |
| 837 |
EXPECT_TRUE(notMatches(Snippet2, MatchesX)); |
| 838 |
|
| 839 |
auto MatchesXPointer = memberExpr( |
| 840 |
hasObjectExpression(hasType(pointsTo(cxxRecordDecl(hasName("X")))))); |
| 841 |
EXPECT_TRUE(notMatches(Snippet1, MatchesXPointer)); |
| 842 |
EXPECT_TRUE(matches(Snippet2, MatchesXPointer)); |
| 843 |
} |
| 844 |
|
| 845 |
TEST(ForEachArgumentWithParam, ReportsNoFalsePositives) { |
| 846 |
StatementMatcher ArgumentY = |
| 847 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 848 |
DeclarationMatcher IntParam = parmVarDecl(hasType(isInteger())).bind("param"); |
| 849 |
StatementMatcher CallExpr = |
| 850 |
callExpr(forEachArgumentWithParam(ArgumentY, IntParam)); |
| 851 |
|
| 852 |
// IntParam does not match. |
| 853 |
EXPECT_TRUE(notMatches("void f(int* i) { int* y; f(y); }", CallExpr)); |
| 854 |
// ArgumentY does not match. |
| 855 |
EXPECT_TRUE(notMatches("void f(int i) { int x; f(x); }", CallExpr)); |
| 856 |
} |
| 857 |
|
| 858 |
TEST(ForEachArgumentWithParam, MatchesCXXMemberCallExpr) { |
| 859 |
StatementMatcher ArgumentY = |
| 860 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 861 |
DeclarationMatcher IntParam = parmVarDecl(hasType(isInteger())).bind("param"); |
| 862 |
StatementMatcher CallExpr = |
| 863 |
callExpr(forEachArgumentWithParam(ArgumentY, IntParam)); |
| 864 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 865 |
"struct S {" |
| 866 |
" const S& operator[](int i) { return *this; }" |
| 867 |
"};" |
| 868 |
"void f(S S1) {" |
| 869 |
" int y = 1;" |
| 870 |
" S1[y];" |
| 871 |
"}", |
| 872 |
CallExpr, std::make_unique<VerifyIdIsBoundTo<ParmVarDecl>>("param", 1))); |
| 873 |
|
| 874 |
StatementMatcher CallExpr2 = |
| 875 |
callExpr(forEachArgumentWithParam(ArgumentY, IntParam)); |
| 876 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 877 |
"struct S {" |
| 878 |
" static void g(int i);" |
| 879 |
"};" |
| 880 |
"void f() {" |
| 881 |
" int y = 1;" |
| 882 |
" S::g(y);" |
| 883 |
"}", |
| 884 |
CallExpr2, std::make_unique<VerifyIdIsBoundTo<ParmVarDecl>>("param", 1))); |
| 885 |
} |
| 886 |
|
| 887 |
TEST(ForEachArgumentWithParam, MatchesCallExpr) { |
| 888 |
StatementMatcher ArgumentY = |
| 889 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 890 |
DeclarationMatcher IntParam = parmVarDecl(hasType(isInteger())).bind("param"); |
| 891 |
StatementMatcher CallExpr = |
| 892 |
callExpr(forEachArgumentWithParam(ArgumentY, IntParam)); |
| 893 |
|
| 894 |
EXPECT_TRUE( |
| 895 |
matchAndVerifyResultTrue("void f(int i) { int y; f(y); }", CallExpr, |
| 896 |
std::make_unique<VerifyIdIsBoundTo<ParmVarDecl>>( |
| 897 |
"param"))); |
| 898 |
EXPECT_TRUE( |
| 899 |
matchAndVerifyResultTrue("void f(int i) { int y; f(y); }", CallExpr, |
| 900 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>( |
| 901 |
"arg"))); |
| 902 |
|
| 903 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 904 |
"void f(int i, int j) { int y; f(y, y); }", CallExpr, |
| 905 |
std::make_unique<VerifyIdIsBoundTo<ParmVarDecl>>("param", 2))); |
| 906 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 907 |
"void f(int i, int j) { int y; f(y, y); }", CallExpr, |
| 908 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg", 2))); |
| 909 |
} |
| 910 |
|
| 911 |
TEST(ForEachArgumentWithParam, MatchesConstructExpr) { |
| 912 |
StatementMatcher ArgumentY = |
| 913 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 914 |
DeclarationMatcher IntParam = parmVarDecl(hasType(isInteger())).bind("param"); |
| 915 |
StatementMatcher ConstructExpr = traverse( |
| 916 |
TK_AsIs, cxxConstructExpr(forEachArgumentWithParam(ArgumentY, IntParam))); |
| 917 |
|
| 918 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 919 |
"struct C {" |
| 920 |
" C(int i) {}" |
| 921 |
"};" |
| 922 |
"int y = 0;" |
| 923 |
"C Obj(y);", |
| 924 |
ConstructExpr, |
| 925 |
std::make_unique<VerifyIdIsBoundTo<ParmVarDecl>>("param"))); |
| 926 |
} |
| 927 |
|
| 928 |
TEST(ForEachArgumentWithParam, HandlesBoundNodesForNonMatches) { |
| 929 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 930 |
"void g(int i, int j) {" |
| 931 |
" int a;" |
| 932 |
" int b;" |
| 933 |
" int c;" |
| 934 |
" g(a, 0);" |
| 935 |
" g(a, b);" |
| 936 |
" g(0, b);" |
| 937 |
"}", |
| 938 |
functionDecl( |
| 939 |
forEachDescendant(varDecl().bind("v")), |
| 940 |
forEachDescendant(callExpr(forEachArgumentWithParam( |
| 941 |
declRefExpr(to(decl(equalsBoundNode("v")))), parmVarDecl())))), |
| 942 |
std::make_unique<VerifyIdIsBoundTo<VarDecl>>("v", 4))); |
| 943 |
} |
| 944 |
|
| 945 |
TEST(ForEachArgumentWithParamType, ReportsNoFalsePositives) { |
| 946 |
StatementMatcher ArgumentY = |
| 947 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 948 |
TypeMatcher IntType = qualType(isInteger()).bind("type"); |
| 949 |
StatementMatcher CallExpr = |
| 950 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 951 |
|
| 952 |
// IntParam does not match. |
| 953 |
EXPECT_TRUE(notMatches("void f(int* i) { int* y; f(y); }", CallExpr)); |
| 954 |
// ArgumentY does not match. |
| 955 |
EXPECT_TRUE(notMatches("void f(int i) { int x; f(x); }", CallExpr)); |
| 956 |
} |
| 957 |
|
| 958 |
TEST(ForEachArgumentWithParamType, MatchesCXXMemberCallExpr) { |
| 959 |
StatementMatcher ArgumentY = |
| 960 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 961 |
TypeMatcher IntType = qualType(isInteger()).bind("type"); |
| 962 |
StatementMatcher CallExpr = |
| 963 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 964 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 965 |
"struct S {" |
| 966 |
" const S& operator[](int i) { return *this; }" |
| 967 |
"};" |
| 968 |
"void f(S S1) {" |
| 969 |
" int y = 1;" |
| 970 |
" S1[y];" |
| 971 |
"}", |
| 972 |
CallExpr, std::make_unique<VerifyIdIsBoundTo<QualType>>("type", 1))); |
| 973 |
|
| 974 |
StatementMatcher CallExpr2 = |
| 975 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 976 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 977 |
"struct S {" |
| 978 |
" static void g(int i);" |
| 979 |
"};" |
| 980 |
"void f() {" |
| 981 |
" int y = 1;" |
| 982 |
" S::g(y);" |
| 983 |
"}", |
| 984 |
CallExpr2, std::make_unique<VerifyIdIsBoundTo<QualType>>("type", 1))); |
| 985 |
} |
| 986 |
|
| 987 |
TEST(ForEachArgumentWithParamType, MatchesCallExpr) { |
| 988 |
StatementMatcher ArgumentY = |
| 989 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 990 |
TypeMatcher IntType = qualType(isInteger()).bind("type"); |
| 991 |
StatementMatcher CallExpr = |
| 992 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 993 |
|
| 994 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 995 |
"void f(int i) { int y; f(y); }", CallExpr, |
| 996 |
std::make_unique<VerifyIdIsBoundTo<QualType>>("type"))); |
| 997 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 998 |
"void f(int i) { int y; f(y); }", CallExpr, |
| 999 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg"))); |
| 1000 |
|
| 1001 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1002 |
"void f(int i, int j) { int y; f(y, y); }", CallExpr, |
| 1003 |
std::make_unique<VerifyIdIsBoundTo<QualType>>("type", 2))); |
| 1004 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1005 |
"void f(int i, int j) { int y; f(y, y); }", CallExpr, |
| 1006 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg", 2))); |
| 1007 |
} |
| 1008 |
|
| 1009 |
TEST(ForEachArgumentWithParamType, MatchesConstructExpr) { |
| 1010 |
StatementMatcher ArgumentY = |
| 1011 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 1012 |
TypeMatcher IntType = qualType(isInteger()).bind("type"); |
| 1013 |
StatementMatcher ConstructExpr = |
| 1014 |
cxxConstructExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 1015 |
|
| 1016 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1017 |
"struct C {" |
| 1018 |
" C(int i) {}" |
| 1019 |
"};" |
| 1020 |
"int y = 0;" |
| 1021 |
"C Obj(y);", |
| 1022 |
ConstructExpr, std::make_unique<VerifyIdIsBoundTo<QualType>>("type"))); |
| 1023 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1024 |
"struct C {" |
| 1025 |
" C(int i) {}" |
| 1026 |
"};" |
| 1027 |
"int y = 0;" |
| 1028 |
"C Obj(y);", |
| 1029 |
ConstructExpr, std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg"))); |
| 1030 |
} |
| 1031 |
|
| 1032 |
TEST(ForEachArgumentWithParamType, HandlesKandRFunctions) { |
| 1033 |
StatementMatcher ArgumentY = |
| 1034 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 1035 |
TypeMatcher IntType = qualType(isInteger()).bind("type"); |
| 1036 |
StatementMatcher CallExpr = |
| 1037 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 1038 |
|
| 1039 |
EXPECT_TRUE(matchesC("void f();\n" |
| 1040 |
"void call_it(void) { int x, y; f(x, y); }\n" |
| 1041 |
"void f(a, b) int a, b; {}\n" |
| 1042 |
"void call_it2(void) { int x, y; f(x, y); }", |
| 1043 |
CallExpr)); |
| 1044 |
} |
| 1045 |
|
| 1046 |
TEST(ForEachArgumentWithParamType, HandlesBoundNodesForNonMatches) { |
| 1047 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1048 |
"void g(int i, int j) {" |
| 1049 |
" int a;" |
| 1050 |
" int b;" |
| 1051 |
" int c;" |
| 1052 |
" g(a, 0);" |
| 1053 |
" g(a, b);" |
| 1054 |
" g(0, b);" |
| 1055 |
"}", |
| 1056 |
functionDecl( |
| 1057 |
forEachDescendant(varDecl().bind("v")), |
| 1058 |
forEachDescendant(callExpr(forEachArgumentWithParamType( |
| 1059 |
declRefExpr(to(decl(equalsBoundNode("v")))), qualType())))), |
| 1060 |
std::make_unique<VerifyIdIsBoundTo<VarDecl>>("v", 4))); |
| 1061 |
} |
| 1062 |
|
| 1063 |
TEST(ForEachArgumentWithParamType, MatchesFunctionPtrCalls) { |
| 1064 |
StatementMatcher ArgumentY = |
| 1065 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 1066 |
TypeMatcher IntType = qualType(builtinType()).bind("type"); |
| 1067 |
StatementMatcher CallExpr = |
| 1068 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 1069 |
|
| 1070 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1071 |
"void f(int i) {" |
| 1072 |
"void (*f_ptr)(int) = f; int y; f_ptr(y); }", |
| 1073 |
CallExpr, std::make_unique<VerifyIdIsBoundTo<QualType>>("type"))); |
| 1074 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1075 |
"void f(int i) {" |
| 1076 |
"void (*f_ptr)(int) = f; int y; f_ptr(y); }", |
| 1077 |
CallExpr, std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg"))); |
| 1078 |
} |
| 1079 |
|
| 1080 |
TEST(ForEachArgumentWithParamType, MatchesMemberFunctionPtrCalls) { |
| 1081 |
StatementMatcher ArgumentY = |
| 1082 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 1083 |
TypeMatcher IntType = qualType(builtinType()).bind("type"); |
| 1084 |
StatementMatcher CallExpr = |
| 1085 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 1086 |
|
| 1087 |
StringRef S = "struct A {\n" |
| 1088 |
" int f(int i) { return i + 1; }\n" |
| 1089 |
" int (A::*x)(int);\n" |
| 1090 |
"};\n" |
| 1091 |
"void f() {\n" |
| 1092 |
" int y = 42;\n" |
| 1093 |
" A a;\n" |
| 1094 |
" a.x = &A::f;\n" |
| 1095 |
" (a.*(a.x))(y);\n" |
| 1096 |
"}"; |
| 1097 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1098 |
S, CallExpr, std::make_unique<VerifyIdIsBoundTo<QualType>>("type"))); |
| 1099 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1100 |
S, CallExpr, std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg"))); |
| 1101 |
} |
| 1102 |
|
| 1103 |
TEST(ForEachArgumentWithParamType, MatchesVariadicFunctionPtrCalls) { |
| 1104 |
StatementMatcher ArgumentY = |
| 1105 |
declRefExpr(to(varDecl(hasName("y")))).bind("arg"); |
| 1106 |
TypeMatcher IntType = qualType(builtinType()).bind("type"); |
| 1107 |
StatementMatcher CallExpr = |
| 1108 |
callExpr(forEachArgumentWithParamType(ArgumentY, IntType)); |
| 1109 |
|
| 1110 |
StringRef S = R"cpp( |
| 1111 |
void fcntl(int fd, int cmd, ...) {} |
| 1112 |
|
| 1113 |
template <typename Func> |
| 1114 |
void f(Func F) { |
| 1115 |
int y = 42; |
| 1116 |
F(y, 1, 3); |
| 1117 |
} |
| 1118 |
|
| 1119 |
void g() { f(fcntl); } |
| 1120 |
)cpp"; |
| 1121 |
|
| 1122 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1123 |
S, CallExpr, std::make_unique<VerifyIdIsBoundTo<QualType>>("type"))); |
| 1124 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1125 |
S, CallExpr, std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("arg"))); |
| 1126 |
} |
| 1127 |
|
| 1128 |
TEST(QualType, hasCanonicalType) { |
| 1129 |
EXPECT_TRUE(notMatches("typedef int &int_ref;" |
| 1130 |
"int a;" |
| 1131 |
"int_ref b = a;", |
| 1132 |
varDecl(hasType(qualType(referenceType()))))); |
| 1133 |
EXPECT_TRUE( |
| 1134 |
matches("typedef int &int_ref;" |
| 1135 |
"int a;" |
| 1136 |
"int_ref b = a;", |
| 1137 |
varDecl(hasType(qualType(hasCanonicalType(referenceType())))))); |
| 1138 |
} |
| 1139 |
|
| 1140 |
TEST(HasParameter, CallsInnerMatcher) { |
| 1141 |
EXPECT_TRUE(matches("class X { void x(int) {} };", |
| 1142 |
cxxMethodDecl(hasParameter(0, varDecl())))); |
| 1143 |
EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
| 1144 |
cxxMethodDecl(hasParameter(0, hasName("x"))))); |
| 1145 |
EXPECT_TRUE(matchesObjC("@interface I -(void)f:(int) x; @end", |
| 1146 |
objcMethodDecl(hasParameter(0, hasName("x"))))); |
| 1147 |
EXPECT_TRUE(matchesObjC("int main() { void (^b)(int) = ^(int p) {}; }", |
| 1148 |
blockDecl(hasParameter(0, hasName("p"))))); |
| 1149 |
} |
| 1150 |
|
| 1151 |
TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 1152 |
EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
| 1153 |
cxxMethodDecl(hasParameter(42, varDecl())))); |
| 1154 |
} |
| 1155 |
|
| 1156 |
TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 1157 |
EXPECT_TRUE(matches( |
| 1158 |
"class X { void x(X x) {} };", |
| 1159 |
cxxMethodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
| 1160 |
EXPECT_TRUE(notMatches( |
| 1161 |
"class X { void x(const X &x) {} };", |
| 1162 |
cxxMethodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
| 1163 |
EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
| 1164 |
cxxMethodDecl(hasParameter( |
| 1165 |
0, hasType(pointsTo(recordDecl(hasName("X")))))))); |
| 1166 |
EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
| 1167 |
cxxMethodDecl(hasParameter( |
| 1168 |
0, hasType(references(recordDecl(hasName("X")))))))); |
| 1169 |
} |
| 1170 |
|
| 1171 |
TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 1172 |
EXPECT_TRUE(matches( |
| 1173 |
"class Y {}; class X { void x(X x, Y y) {} };", |
| 1174 |
cxxMethodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
| 1175 |
EXPECT_TRUE(matches( |
| 1176 |
"class Y {}; class X { void x(Y y, X x) {} };", |
| 1177 |
cxxMethodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
| 1178 |
EXPECT_TRUE(matchesObjC("@interface I -(void)f:(int) x; @end", |
| 1179 |
objcMethodDecl(hasAnyParameter(hasName("x"))))); |
| 1180 |
EXPECT_TRUE(matchesObjC("int main() { void (^b)(int) = ^(int p) {}; }", |
| 1181 |
blockDecl(hasAnyParameter(hasName("p"))))); |
| 1182 |
} |
| 1183 |
|
| 1184 |
TEST(Returns, MatchesReturnTypes) { |
| 1185 |
EXPECT_TRUE(matches("class Y { int f() { return 1; } };", |
| 1186 |
functionDecl(returns(asString("int"))))); |
| 1187 |
EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };", |
| 1188 |
functionDecl(returns(asString("float"))))); |
| 1189 |
EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };", |
| 1190 |
functionDecl(returns(hasDeclaration( |
| 1191 |
recordDecl(hasName("Y"))))))); |
| 1192 |
} |
| 1193 |
|
| 1194 |
TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 1195 |
EXPECT_TRUE(notMatches( |
| 1196 |
"class Y {}; class X { void x(int) {} };", |
| 1197 |
cxxMethodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
| 1198 |
} |
| 1199 |
|
| 1200 |
TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 1201 |
EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
| 1202 |
cxxMethodDecl(hasAnyParameter( |
| 1203 |
hasType(pointsTo(recordDecl(hasName("X")))))))); |
| 1204 |
} |
| 1205 |
|
| 1206 |
TEST(HasName, MatchesParameterVariableDeclarations) { |
| 1207 |
EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
| 1208 |
cxxMethodDecl(hasAnyParameter(hasName("x"))))); |
| 1209 |
EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
| 1210 |
cxxMethodDecl(hasAnyParameter(hasName("x"))))); |
| 1211 |
} |
| 1212 |
|
| 1213 |
TEST(Matcher, MatchesTypeTemplateArgument) { |
| 1214 |
EXPECT_TRUE(matches( |
| 1215 |
"template<typename T> struct B {};" |
| 1216 |
"B<int> b;", |
| 1217 |
classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( |
| 1218 |
asString("int")))))); |
| 1219 |
} |
| 1220 |
|
| 1221 |
TEST(Matcher, MatchesTemplateTemplateArgument) { |
| 1222 |
EXPECT_TRUE(matches("template<template <typename> class S> class X {};" |
| 1223 |
"template<typename T> class Y {};" |
| 1224 |
"X<Y> xi;", |
| 1225 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1226 |
refersToTemplate(templateName()))))); |
| 1227 |
} |
| 1228 |
|
| 1229 |
TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { |
| 1230 |
EXPECT_TRUE(matches( |
| 1231 |
"struct B { int next; };" |
| 1232 |
"template<int(B::*next_ptr)> struct A {};" |
| 1233 |
"A<&B::next> a;", |
| 1234 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1235 |
refersToDeclaration(fieldDecl(hasName("next"))))))); |
| 1236 |
|
| 1237 |
EXPECT_TRUE(notMatches( |
| 1238 |
"template <typename T> struct A {};" |
| 1239 |
"A<int> a;", |
| 1240 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1241 |
refersToDeclaration(decl()))))); |
| 1242 |
|
| 1243 |
EXPECT_TRUE(matches( |
| 1244 |
"struct B { int next; };" |
| 1245 |
"template<int(B::*next_ptr)> struct A {};" |
| 1246 |
"A<&B::next> a;", |
| 1247 |
templateSpecializationType(hasAnyTemplateArgument(isExpr( |
| 1248 |
hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))))); |
| 1249 |
|
| 1250 |
EXPECT_TRUE(notMatches( |
| 1251 |
"template <typename T> struct A {};" |
| 1252 |
"A<int> a;", |
| 1253 |
templateSpecializationType(hasAnyTemplateArgument( |
| 1254 |
refersToDeclaration(decl()))))); |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
TEST(Matcher, MatchesSpecificArgument) { |
| 1259 |
EXPECT_TRUE(matches( |
| 1260 |
"template<typename T, typename U> class A {};" |
| 1261 |
"A<bool, int> a;", |
| 1262 |
classTemplateSpecializationDecl(hasTemplateArgument( |
| 1263 |
1, refersToType(asString("int")))))); |
| 1264 |
EXPECT_TRUE(notMatches( |
| 1265 |
"template<typename T, typename U> class A {};" |
| 1266 |
"A<int, bool> a;", |
| 1267 |
classTemplateSpecializationDecl(hasTemplateArgument( |
| 1268 |
1, refersToType(asString("int")))))); |
| 1269 |
|
| 1270 |
EXPECT_TRUE(matches( |
| 1271 |
"template<typename T, typename U> class A {};" |
| 1272 |
"A<bool, int> a;", |
| 1273 |
templateSpecializationType(hasTemplateArgument( |
| 1274 |
1, refersToType(asString("int")))))); |
| 1275 |
EXPECT_TRUE(notMatches( |
| 1276 |
"template<typename T, typename U> class A {};" |
| 1277 |
"A<int, bool> a;", |
| 1278 |
templateSpecializationType(hasTemplateArgument( |
| 1279 |
1, refersToType(asString("int")))))); |
| 1280 |
|
| 1281 |
EXPECT_TRUE(matches( |
| 1282 |
"template<typename T> void f() {};" |
| 1283 |
"void func() { f<int>(); }", |
| 1284 |
functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); |
| 1285 |
EXPECT_TRUE(notMatches( |
| 1286 |
"template<typename T> void f() {};", |
| 1287 |
functionDecl(hasTemplateArgument(0, refersToType(asString("int")))))); |
| 1288 |
} |
| 1289 |
|
| 1290 |
TEST(TemplateArgument, Matches) { |
| 1291 |
EXPECT_TRUE(matches("template<typename T> struct C {}; C<int> c;", |
| 1292 |
classTemplateSpecializationDecl( |
| 1293 |
hasAnyTemplateArgument(templateArgument())))); |
| 1294 |
EXPECT_TRUE(matches( |
| 1295 |
"template<typename T> struct C {}; C<int> c;", |
| 1296 |
templateSpecializationType(hasAnyTemplateArgument(templateArgument())))); |
| 1297 |
|
| 1298 |
EXPECT_TRUE(matches( |
| 1299 |
"template<typename T> void f() {};" |
| 1300 |
"void func() { f<int>(); }", |
| 1301 |
functionDecl(hasAnyTemplateArgument(templateArgument())))); |
| 1302 |
} |
| 1303 |
|
| 1304 |
TEST(TemplateTypeParmDecl, CXXMethodDecl) { |
| 1305 |
const char input[] = |
| 1306 |
"template<typename T>\n" |
| 1307 |
"class Class {\n" |
| 1308 |
" void method();\n" |
| 1309 |
"};\n" |
| 1310 |
"template<typename U>\n" |
| 1311 |
"void Class<U>::method() {}\n"; |
| 1312 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T")))); |
| 1313 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U")))); |
| 1314 |
} |
| 1315 |
|
| 1316 |
TEST(TemplateTypeParmDecl, VarDecl) { |
| 1317 |
const char input[] = |
| 1318 |
"template<typename T>\n" |
| 1319 |
"class Class {\n" |
| 1320 |
" static T pi;\n" |
| 1321 |
"};\n" |
| 1322 |
"template<typename U>\n" |
| 1323 |
"U Class<U>::pi = U(3.1415926535897932385);\n"; |
| 1324 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T")))); |
| 1325 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U")))); |
| 1326 |
} |
| 1327 |
|
| 1328 |
TEST(TemplateTypeParmDecl, VarTemplatePartialSpecializationDecl) { |
| 1329 |
const char input[] = |
| 1330 |
"template<typename T>\n" |
| 1331 |
"struct Struct {\n" |
| 1332 |
" template<typename T2> static int field;\n" |
| 1333 |
"};\n" |
| 1334 |
"template<typename U>\n" |
| 1335 |
"template<typename U2>\n" |
| 1336 |
"int Struct<U>::field<U2*> = 123;\n"; |
| 1337 |
EXPECT_TRUE( |
| 1338 |
matches(input, templateTypeParmDecl(hasName("T")), langCxx14OrLater())); |
| 1339 |
EXPECT_TRUE( |
| 1340 |
matches(input, templateTypeParmDecl(hasName("T2")), langCxx14OrLater())); |
| 1341 |
EXPECT_TRUE( |
| 1342 |
matches(input, templateTypeParmDecl(hasName("U")), langCxx14OrLater())); |
| 1343 |
EXPECT_TRUE( |
| 1344 |
matches(input, templateTypeParmDecl(hasName("U2")), langCxx14OrLater())); |
| 1345 |
} |
| 1346 |
|
| 1347 |
TEST(TemplateTypeParmDecl, ClassTemplatePartialSpecializationDecl) { |
| 1348 |
const char input[] = |
| 1349 |
"template<typename T>\n" |
| 1350 |
"class Class {\n" |
| 1351 |
" template<typename T2> struct Struct;\n" |
| 1352 |
"};\n" |
| 1353 |
"template<typename U>\n" |
| 1354 |
"template<typename U2>\n" |
| 1355 |
"struct Class<U>::Struct<U2*> {};\n"; |
| 1356 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T")))); |
| 1357 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T2")))); |
| 1358 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U")))); |
| 1359 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U2")))); |
| 1360 |
} |
| 1361 |
|
| 1362 |
TEST(TemplateTypeParmDecl, EnumDecl) { |
| 1363 |
const char input[] = |
| 1364 |
"template<typename T>\n" |
| 1365 |
"struct Struct {\n" |
| 1366 |
" enum class Enum : T;\n" |
| 1367 |
"};\n" |
| 1368 |
"template<typename U>\n" |
| 1369 |
"enum class Struct<U>::Enum : U {\n" |
| 1370 |
" e1,\n" |
| 1371 |
" e2\n" |
| 1372 |
"};\n"; |
| 1373 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T")))); |
| 1374 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U")))); |
| 1375 |
} |
| 1376 |
|
| 1377 |
TEST(TemplateTypeParmDecl, RecordDecl) { |
| 1378 |
const char input[] = |
| 1379 |
"template<typename T>\n" |
| 1380 |
"class Class {\n" |
| 1381 |
" struct Struct;\n" |
| 1382 |
"};\n" |
| 1383 |
"template<typename U>\n" |
| 1384 |
"struct Class<U>::Struct {\n" |
| 1385 |
" U field;\n" |
| 1386 |
"};\n"; |
| 1387 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("T")))); |
| 1388 |
EXPECT_TRUE(matches(input, templateTypeParmDecl(hasName("U")))); |
| 1389 |
} |
| 1390 |
|
| 1391 |
TEST(RefersToIntegralType, Matches) { |
| 1392 |
EXPECT_TRUE(matches("template<int T> struct C {}; C<42> c;", |
| 1393 |
classTemplateSpecializationDecl( |
| 1394 |
hasAnyTemplateArgument(refersToIntegralType( |
| 1395 |
asString("int")))))); |
| 1396 |
EXPECT_TRUE(notMatches("template<unsigned T> struct C {}; C<42> c;", |
| 1397 |
classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1398 |
refersToIntegralType(asString("int")))))); |
| 1399 |
} |
| 1400 |
|
| 1401 |
TEST(ConstructorDeclaration, SimpleCase) { |
| 1402 |
EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
| 1403 |
cxxConstructorDecl(ofClass(hasName("Foo"))))); |
| 1404 |
EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
| 1405 |
cxxConstructorDecl(ofClass(hasName("Bar"))))); |
| 1406 |
} |
| 1407 |
|
| 1408 |
TEST(DestructorDeclaration, MatchesVirtualDestructor) { |
| 1409 |
EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };", |
| 1410 |
cxxDestructorDecl(ofClass(hasName("Foo"))))); |
| 1411 |
} |
| 1412 |
|
| 1413 |
TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) { |
| 1414 |
EXPECT_TRUE(notMatches("class Foo {};", |
| 1415 |
cxxDestructorDecl(ofClass(hasName("Foo"))))); |
| 1416 |
} |
| 1417 |
|
| 1418 |
TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1419 |
EXPECT_TRUE( |
| 1420 |
notMatches("class Foo { Foo() { } };", |
| 1421 |
cxxConstructorDecl(hasAnyConstructorInitializer(anything())))); |
| 1422 |
EXPECT_TRUE( |
| 1423 |
matches("class Foo {" |
| 1424 |
" Foo() : foo_() { }" |
| 1425 |
" int foo_;" |
| 1426 |
"};", |
| 1427 |
cxxConstructorDecl(hasAnyConstructorInitializer(anything())))); |
| 1428 |
} |
| 1429 |
|
| 1430 |
TEST(HasAnyConstructorInitializer, ForField) { |
| 1431 |
static const char Code[] = |
| 1432 |
"class Baz { };" |
| 1433 |
"class Foo {" |
| 1434 |
" Foo() : foo_(), bar_() { }" |
| 1435 |
" Baz foo_;" |
| 1436 |
" struct {" |
| 1437 |
" Baz bar_;" |
| 1438 |
" };" |
| 1439 |
"};"; |
| 1440 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1441 |
forField(hasType(recordDecl(hasName("Baz")))))))); |
| 1442 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1443 |
forField(hasName("foo_")))))); |
| 1444 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1445 |
forField(hasName("bar_")))))); |
| 1446 |
EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1447 |
forField(hasType(recordDecl(hasName("Bar")))))))); |
| 1448 |
} |
| 1449 |
|
| 1450 |
TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1451 |
static const char Code[] = |
| 1452 |
"class Foo {" |
| 1453 |
" Foo() : foo_(0) { }" |
| 1454 |
" int foo_;" |
| 1455 |
"};"; |
| 1456 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1457 |
withInitializer(integerLiteral(equals(0))))))); |
| 1458 |
EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1459 |
withInitializer(integerLiteral(equals(1))))))); |
| 1460 |
} |
| 1461 |
|
| 1462 |
TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1463 |
static const char Code[] = |
| 1464 |
"struct Bar { Bar(){} };" |
| 1465 |
"class Foo {" |
| 1466 |
" Foo() : foo_() { }" |
| 1467 |
" Bar foo_;" |
| 1468 |
" Bar bar_;" |
| 1469 |
"};"; |
| 1470 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1471 |
allOf(forField(hasName("foo_")), isWritten()))))); |
| 1472 |
EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1473 |
allOf(forField(hasName("bar_")), isWritten()))))); |
| 1474 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(hasAnyConstructorInitializer( |
| 1475 |
allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1476 |
} |
| 1477 |
|
| 1478 |
TEST(HasAnyConstructorInitializer, IsBaseInitializer) { |
| 1479 |
static const char Code[] = |
| 1480 |
"struct B {};" |
| 1481 |
"struct D : B {" |
| 1482 |
" int I;" |
| 1483 |
" D(int i) : I(i) {}" |
| 1484 |
"};" |
| 1485 |
"struct E : B {" |
| 1486 |
" E() : B() {}" |
| 1487 |
"};"; |
| 1488 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(allOf( |
| 1489 |
hasAnyConstructorInitializer(allOf(isBaseInitializer(), isWritten())), |
| 1490 |
hasName("E"))))); |
| 1491 |
EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(allOf( |
| 1492 |
hasAnyConstructorInitializer(allOf(isBaseInitializer(), isWritten())), |
| 1493 |
hasName("D"))))); |
| 1494 |
EXPECT_TRUE(matches(Code, cxxConstructorDecl(allOf( |
| 1495 |
hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())), |
| 1496 |
hasName("D"))))); |
| 1497 |
EXPECT_TRUE(notMatches(Code, cxxConstructorDecl(allOf( |
| 1498 |
hasAnyConstructorInitializer(allOf(isMemberInitializer(), isWritten())), |
| 1499 |
hasName("E"))))); |
| 1500 |
} |
| 1501 |
|
| 1502 |
TEST(IfStmt, ChildTraversalMatchers) { |
| 1503 |
EXPECT_TRUE(matches("void f() { if (false) true; else false; }", |
| 1504 |
ifStmt(hasThen(cxxBoolLiteral(equals(true)))))); |
| 1505 |
EXPECT_TRUE(notMatches("void f() { if (false) false; else true; }", |
| 1506 |
ifStmt(hasThen(cxxBoolLiteral(equals(true)))))); |
| 1507 |
EXPECT_TRUE(matches("void f() { if (false) false; else true; }", |
| 1508 |
ifStmt(hasElse(cxxBoolLiteral(equals(true)))))); |
| 1509 |
EXPECT_TRUE(notMatches("void f() { if (false) true; else false; }", |
| 1510 |
ifStmt(hasElse(cxxBoolLiteral(equals(true)))))); |
| 1511 |
} |
| 1512 |
|
| 1513 |
TEST(MatchBinaryOperator, HasOperatorName) { |
| 1514 |
StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1515 |
|
| 1516 |
EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1517 |
EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1518 |
} |
| 1519 |
|
| 1520 |
TEST(MatchBinaryOperator, HasAnyOperatorName) { |
| 1521 |
StatementMatcher Matcher = |
| 1522 |
binaryOperator(hasAnyOperatorName("+", "-", "*", "/")); |
| 1523 |
|
| 1524 |
EXPECT_TRUE(matches("int x(int I) { return I + 2; }", Matcher)); |
| 1525 |
EXPECT_TRUE(matches("int x(int I) { return I - 2; }", Matcher)); |
| 1526 |
EXPECT_TRUE(matches("int x(int I) { return I * 2; }", Matcher)); |
| 1527 |
EXPECT_TRUE(matches("int x(int I) { return I / 2; }", Matcher)); |
| 1528 |
EXPECT_TRUE(notMatches("int x(int I) { return I % 2; }", Matcher)); |
| 1529 |
// Ensure '+= isn't mistaken. |
| 1530 |
EXPECT_TRUE(notMatches("void x(int &I) { I += 1; }", Matcher)); |
| 1531 |
} |
| 1532 |
|
| 1533 |
TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1534 |
StatementMatcher OperatorTrueFalse = |
| 1535 |
binaryOperator(hasLHS(cxxBoolLiteral(equals(true))), |
| 1536 |
hasRHS(cxxBoolLiteral(equals(false)))); |
| 1537 |
|
| 1538 |
EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1539 |
EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1540 |
EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1541 |
|
| 1542 |
StatementMatcher OperatorIntPointer = arraySubscriptExpr( |
| 1543 |
hasLHS(hasType(isInteger())), |
| 1544 |
traverse(TK_AsIs, hasRHS(hasType(pointsTo(qualType()))))); |
| 1545 |
EXPECT_TRUE(matches("void x() { 1[\"abc\"]; }", OperatorIntPointer)); |
| 1546 |
EXPECT_TRUE(notMatches("void x() { \"abc\"[1]; }", OperatorIntPointer)); |
| 1547 |
|
| 1548 |
StringRef Code = R"cpp( |
| 1549 |
struct HasOpEqMem |
| 1550 |
{ |
| 1551 |
bool operator==(const HasOpEqMem& other) const |
| 1552 |
{ |
| 1553 |
return true; |
| 1554 |
} |
| 1555 |
}; |
| 1556 |
|
| 1557 |
struct HasOpFree |
| 1558 |
{ |
| 1559 |
}; |
| 1560 |
bool operator==(const HasOpFree& lhs, const HasOpFree& rhs) |
| 1561 |
{ |
| 1562 |
return true; |
| 1563 |
} |
| 1564 |
|
| 1565 |
void opMem() |
| 1566 |
{ |
| 1567 |
HasOpEqMem s1; |
| 1568 |
HasOpEqMem s2; |
| 1569 |
if (s1 == s2) |
| 1570 |
return; |
| 1571 |
} |
| 1572 |
|
| 1573 |
void opFree() |
| 1574 |
{ |
| 1575 |
HasOpFree s1; |
| 1576 |
HasOpFree s2; |
| 1577 |
if (s1 == s2) |
| 1578 |
return; |
| 1579 |
} |
| 1580 |
)cpp"; |
| 1581 |
auto s1Expr = declRefExpr(to(varDecl(hasName("s1")))); |
| 1582 |
auto s2Expr = declRefExpr(to(varDecl(hasName("s2")))); |
| 1583 |
EXPECT_TRUE(matches( |
| 1584 |
Code, |
| 1585 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1586 |
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opMem"))), |
| 1587 |
hasOperatorName("=="), hasLHS(s1Expr), |
| 1588 |
hasRHS(s2Expr))))); |
| 1589 |
EXPECT_TRUE(matches( |
| 1590 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1591 |
cxxOperatorCallExpr( |
| 1592 |
forFunction(functionDecl(hasName("opMem"))), |
| 1593 |
hasAnyOperatorName("!=", "=="), hasLHS(s1Expr))))); |
| 1594 |
EXPECT_TRUE(matches( |
| 1595 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1596 |
cxxOperatorCallExpr( |
| 1597 |
forFunction(functionDecl(hasName("opMem"))), |
| 1598 |
hasOperatorName("=="), hasOperands(s1Expr, s2Expr))))); |
| 1599 |
EXPECT_TRUE(matches( |
| 1600 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1601 |
cxxOperatorCallExpr( |
| 1602 |
forFunction(functionDecl(hasName("opMem"))), |
| 1603 |
hasOperatorName("=="), hasEitherOperand(s2Expr))))); |
| 1604 |
|
| 1605 |
EXPECT_TRUE(matches( |
| 1606 |
Code, |
| 1607 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1608 |
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opFree"))), |
| 1609 |
hasOperatorName("=="), hasLHS(s1Expr), |
| 1610 |
hasRHS(s2Expr))))); |
| 1611 |
EXPECT_TRUE(matches( |
| 1612 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1613 |
cxxOperatorCallExpr( |
| 1614 |
forFunction(functionDecl(hasName("opFree"))), |
| 1615 |
hasAnyOperatorName("!=", "=="), hasLHS(s1Expr))))); |
| 1616 |
EXPECT_TRUE(matches( |
| 1617 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1618 |
cxxOperatorCallExpr( |
| 1619 |
forFunction(functionDecl(hasName("opFree"))), |
| 1620 |
hasOperatorName("=="), hasOperands(s1Expr, s2Expr))))); |
| 1621 |
EXPECT_TRUE(matches( |
| 1622 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1623 |
cxxOperatorCallExpr( |
| 1624 |
forFunction(functionDecl(hasName("opFree"))), |
| 1625 |
hasOperatorName("=="), hasEitherOperand(s2Expr))))); |
| 1626 |
} |
| 1627 |
|
| 1628 |
TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1629 |
StatementMatcher HasOperand = |
| 1630 |
binaryOperator(hasEitherOperand(cxxBoolLiteral(equals(false)))); |
| 1631 |
|
| 1632 |
EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 1633 |
EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 1634 |
EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 1635 |
} |
| 1636 |
|
| 1637 |
TEST(MatchBinaryOperator, HasOperands) { |
| 1638 |
StatementMatcher HasOperands = binaryOperator( |
| 1639 |
hasOperands(integerLiteral(equals(1)), integerLiteral(equals(2)))); |
| 1640 |
EXPECT_TRUE(matches("void x() { 1 + 2; }", HasOperands)); |
| 1641 |
EXPECT_TRUE(matches("void x() { 2 + 1; }", HasOperands)); |
| 1642 |
EXPECT_TRUE(notMatches("void x() { 1 + 1; }", HasOperands)); |
| 1643 |
EXPECT_TRUE(notMatches("void x() { 2 + 2; }", HasOperands)); |
| 1644 |
EXPECT_TRUE(notMatches("void x() { 0 + 0; }", HasOperands)); |
| 1645 |
EXPECT_TRUE(notMatches("void x() { 0 + 1; }", HasOperands)); |
| 1646 |
} |
| 1647 |
|
| 1648 |
TEST(Matcher, BinaryOperatorTypes) { |
| 1649 |
// Integration test that verifies the AST provides all binary operators in |
| 1650 |
// a way we expect. |
| 1651 |
// FIXME: Operator ',' |
| 1652 |
EXPECT_TRUE( |
| 1653 |
matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 1654 |
EXPECT_TRUE( |
| 1655 |
matches("bool b; bool c = (b = true);", |
| 1656 |
binaryOperator(hasOperatorName("=")))); |
| 1657 |
EXPECT_TRUE( |
| 1658 |
matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 1659 |
EXPECT_TRUE( |
| 1660 |
matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 1661 |
EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 1662 |
EXPECT_TRUE( |
| 1663 |
matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 1664 |
EXPECT_TRUE( |
| 1665 |
matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 1666 |
EXPECT_TRUE( |
| 1667 |
matches("int i = 1; int j = (i <<= 2);", |
| 1668 |
binaryOperator(hasOperatorName("<<=")))); |
| 1669 |
EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 1670 |
EXPECT_TRUE( |
| 1671 |
matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 1672 |
EXPECT_TRUE( |
| 1673 |
matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 1674 |
EXPECT_TRUE( |
| 1675 |
matches("int i = 1; int j = (i >>= 2);", |
| 1676 |
binaryOperator(hasOperatorName(">>=")))); |
| 1677 |
EXPECT_TRUE( |
| 1678 |
matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 1679 |
EXPECT_TRUE( |
| 1680 |
matches("int i = 42; int j = (i ^= 42);", |
| 1681 |
binaryOperator(hasOperatorName("^=")))); |
| 1682 |
EXPECT_TRUE( |
| 1683 |
matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 1684 |
EXPECT_TRUE( |
| 1685 |
matches("int i = 42; int j = (i %= 42);", |
| 1686 |
binaryOperator(hasOperatorName("%=")))); |
| 1687 |
EXPECT_TRUE( |
| 1688 |
matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 1689 |
EXPECT_TRUE( |
| 1690 |
matches("bool b = true && false;", |
| 1691 |
binaryOperator(hasOperatorName("&&")))); |
| 1692 |
EXPECT_TRUE( |
| 1693 |
matches("bool b = true; bool c = (b &= false);", |
| 1694 |
binaryOperator(hasOperatorName("&=")))); |
| 1695 |
EXPECT_TRUE( |
| 1696 |
matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 1697 |
EXPECT_TRUE( |
| 1698 |
matches("bool b = true || false;", |
| 1699 |
binaryOperator(hasOperatorName("||")))); |
| 1700 |
EXPECT_TRUE( |
| 1701 |
matches("bool b = true; bool c = (b |= false);", |
| 1702 |
binaryOperator(hasOperatorName("|=")))); |
| 1703 |
EXPECT_TRUE( |
| 1704 |
matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 1705 |
EXPECT_TRUE( |
| 1706 |
matches("int i = 42; int j = (i *= 23);", |
| 1707 |
binaryOperator(hasOperatorName("*=")))); |
| 1708 |
EXPECT_TRUE( |
| 1709 |
matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 1710 |
EXPECT_TRUE( |
| 1711 |
matches("int i = 42; int j = (i /= 23);", |
| 1712 |
binaryOperator(hasOperatorName("/=")))); |
| 1713 |
EXPECT_TRUE( |
| 1714 |
matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 1715 |
EXPECT_TRUE( |
| 1716 |
matches("int i = 42; int j = (i += 23);", |
| 1717 |
binaryOperator(hasOperatorName("+=")))); |
| 1718 |
EXPECT_TRUE( |
| 1719 |
matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 1720 |
EXPECT_TRUE( |
| 1721 |
matches("int i = 42; int j = (i -= 23);", |
| 1722 |
binaryOperator(hasOperatorName("-=")))); |
| 1723 |
EXPECT_TRUE( |
| 1724 |
matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 1725 |
binaryOperator(hasOperatorName("->*")))); |
| 1726 |
EXPECT_TRUE( |
| 1727 |
matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 1728 |
binaryOperator(hasOperatorName(".*")))); |
| 1729 |
|
| 1730 |
// Member expressions as operators are not supported in matches. |
| 1731 |
EXPECT_TRUE( |
| 1732 |
notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 1733 |
binaryOperator(hasOperatorName("->")))); |
| 1734 |
|
| 1735 |
// Initializer assignments are not represented as operator equals. |
| 1736 |
EXPECT_TRUE( |
| 1737 |
notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 1738 |
|
| 1739 |
// Array indexing is not represented as operator. |
| 1740 |
EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 1741 |
|
| 1742 |
// Overloaded operators do not match at all. |
| 1743 |
EXPECT_TRUE(notMatches( |
| 1744 |
"struct A { bool operator&&(const A &a) const { return false; } };" |
| 1745 |
"void x() { A a, b; a && b; }", |
| 1746 |
binaryOperator())); |
| 1747 |
} |
| 1748 |
|
| 1749 |
TEST(MatchUnaryOperator, HasOperatorName) { |
| 1750 |
StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 1751 |
|
| 1752 |
EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 1753 |
EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 1754 |
} |
| 1755 |
|
| 1756 |
TEST(MatchUnaryOperator, HasAnyOperatorName) { |
| 1757 |
StatementMatcher Matcher = unaryOperator(hasAnyOperatorName("-", "*", "++")); |
| 1758 |
|
| 1759 |
EXPECT_TRUE(matches("int x(int *I) { return *I; }", Matcher)); |
| 1760 |
EXPECT_TRUE(matches("int x(int I) { return -I; }", Matcher)); |
| 1761 |
EXPECT_TRUE(matches("void x(int &I) { I++; }", Matcher)); |
| 1762 |
EXPECT_TRUE(matches("void x(int &I) { ++I; }", Matcher)); |
| 1763 |
EXPECT_TRUE(notMatches("void x(int &I) { I--; }", Matcher)); |
| 1764 |
EXPECT_TRUE(notMatches("void x(int &I) { --I; }", Matcher)); |
| 1765 |
EXPECT_TRUE(notMatches("int *x(int &I) { return &I; }", Matcher)); |
| 1766 |
} |
| 1767 |
|
| 1768 |
TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 1769 |
StatementMatcher OperatorOnFalse = |
| 1770 |
unaryOperator(hasUnaryOperand(cxxBoolLiteral(equals(false)))); |
| 1771 |
|
| 1772 |
EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 1773 |
EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 1774 |
|
| 1775 |
StringRef Code = R"cpp( |
| 1776 |
struct HasOpBangMem |
| 1777 |
{ |
| 1778 |
bool operator!() const |
| 1779 |
{ |
| 1780 |
return false; |
| 1781 |
} |
| 1782 |
}; |
| 1783 |
struct HasOpBangFree |
| 1784 |
{ |
| 1785 |
}; |
| 1786 |
bool operator!(HasOpBangFree const&) |
| 1787 |
{ |
| 1788 |
return false; |
| 1789 |
} |
| 1790 |
|
| 1791 |
void opMem() |
| 1792 |
{ |
| 1793 |
HasOpBangMem s1; |
| 1794 |
if (!s1) |
| 1795 |
return; |
| 1796 |
} |
| 1797 |
void opFree() |
| 1798 |
{ |
| 1799 |
HasOpBangFree s1; |
| 1800 |
if (!s1) |
| 1801 |
return; |
| 1802 |
} |
| 1803 |
)cpp"; |
| 1804 |
auto s1Expr = declRefExpr(to(varDecl(hasName("s1")))); |
| 1805 |
EXPECT_TRUE(matches( |
| 1806 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1807 |
cxxOperatorCallExpr( |
| 1808 |
forFunction(functionDecl(hasName("opMem"))), |
| 1809 |
hasOperatorName("!"), hasUnaryOperand(s1Expr))))); |
| 1810 |
EXPECT_TRUE(matches( |
| 1811 |
Code, |
| 1812 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1813 |
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opMem"))), |
| 1814 |
hasAnyOperatorName("+", "!"), |
| 1815 |
hasUnaryOperand(s1Expr))))); |
| 1816 |
|
| 1817 |
EXPECT_TRUE(matches( |
| 1818 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1819 |
cxxOperatorCallExpr( |
| 1820 |
forFunction(functionDecl(hasName("opFree"))), |
| 1821 |
hasOperatorName("!"), hasUnaryOperand(s1Expr))))); |
| 1822 |
EXPECT_TRUE(matches( |
| 1823 |
Code, |
| 1824 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1825 |
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opFree"))), |
| 1826 |
hasAnyOperatorName("+", "!"), |
| 1827 |
hasUnaryOperand(s1Expr))))); |
| 1828 |
|
| 1829 |
Code = R"cpp( |
| 1830 |
struct HasIncOperatorsMem |
| 1831 |
{ |
| 1832 |
HasIncOperatorsMem& operator++(); |
| 1833 |
HasIncOperatorsMem operator++(int); |
| 1834 |
}; |
| 1835 |
struct HasIncOperatorsFree |
| 1836 |
{ |
| 1837 |
}; |
| 1838 |
HasIncOperatorsFree& operator++(HasIncOperatorsFree&); |
| 1839 |
HasIncOperatorsFree operator++(HasIncOperatorsFree&, int); |
| 1840 |
|
| 1841 |
void prefixIncOperatorMem() |
| 1842 |
{ |
| 1843 |
HasIncOperatorsMem s1; |
| 1844 |
++s1; |
| 1845 |
} |
| 1846 |
void prefixIncOperatorFree() |
| 1847 |
{ |
| 1848 |
HasIncOperatorsFree s1; |
| 1849 |
++s1; |
| 1850 |
} |
| 1851 |
void postfixIncOperatorMem() |
| 1852 |
{ |
| 1853 |
HasIncOperatorsMem s1; |
| 1854 |
s1++; |
| 1855 |
} |
| 1856 |
void postfixIncOperatorFree() |
| 1857 |
{ |
| 1858 |
HasIncOperatorsFree s1; |
| 1859 |
s1++; |
| 1860 |
} |
| 1861 |
|
| 1862 |
struct HasOpPlusInt |
| 1863 |
{ |
| 1864 |
HasOpPlusInt& operator+(int); |
| 1865 |
}; |
| 1866 |
void plusIntOperator() |
| 1867 |
{ |
| 1868 |
HasOpPlusInt s1; |
| 1869 |
s1+1; |
| 1870 |
} |
| 1871 |
)cpp"; |
| 1872 |
|
| 1873 |
EXPECT_TRUE(matches( |
| 1874 |
Code, |
| 1875 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1876 |
cxxOperatorCallExpr( |
| 1877 |
forFunction(functionDecl(hasName("prefixIncOperatorMem"))), |
| 1878 |
hasOperatorName("++"), hasUnaryOperand(declRefExpr()))))); |
| 1879 |
|
| 1880 |
EXPECT_TRUE(matches( |
| 1881 |
Code, |
| 1882 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1883 |
cxxOperatorCallExpr( |
| 1884 |
forFunction(functionDecl(hasName("prefixIncOperatorFree"))), |
| 1885 |
hasOperatorName("++"), hasUnaryOperand(declRefExpr()))))); |
| 1886 |
|
| 1887 |
EXPECT_TRUE(matches( |
| 1888 |
Code, |
| 1889 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1890 |
cxxOperatorCallExpr( |
| 1891 |
forFunction(functionDecl(hasName("postfixIncOperatorMem"))), |
| 1892 |
hasOperatorName("++"), hasUnaryOperand(declRefExpr()))))); |
| 1893 |
|
| 1894 |
EXPECT_TRUE(matches( |
| 1895 |
Code, |
| 1896 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 1897 |
cxxOperatorCallExpr( |
| 1898 |
forFunction(functionDecl(hasName("postfixIncOperatorFree"))), |
| 1899 |
hasOperatorName("++"), hasUnaryOperand(declRefExpr()))))); |
| 1900 |
|
| 1901 |
EXPECT_FALSE(matches( |
| 1902 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1903 |
cxxOperatorCallExpr( |
| 1904 |
forFunction(functionDecl(hasName("plusIntOperator"))), |
| 1905 |
hasOperatorName("+"), hasUnaryOperand(expr()))))); |
| 1906 |
|
| 1907 |
Code = R"cpp( |
| 1908 |
struct HasOpArrow |
| 1909 |
{ |
| 1910 |
int& operator*(); |
| 1911 |
}; |
| 1912 |
void foo() |
| 1913 |
{ |
| 1914 |
HasOpArrow s1; |
| 1915 |
*s1; |
| 1916 |
} |
| 1917 |
)cpp"; |
| 1918 |
|
| 1919 |
EXPECT_TRUE( |
| 1920 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 1921 |
cxxOperatorCallExpr(hasOperatorName("*"), |
| 1922 |
hasUnaryOperand(expr()))))); |
| 1923 |
} |
| 1924 |
|
| 1925 |
TEST(Matcher, UnaryOperatorTypes) { |
| 1926 |
// Integration test that verifies the AST provides all unary operators in |
| 1927 |
// a way we expect. |
| 1928 |
EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 1929 |
EXPECT_TRUE( |
| 1930 |
matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 1931 |
EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 1932 |
EXPECT_TRUE( |
| 1933 |
matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 1934 |
EXPECT_TRUE( |
| 1935 |
matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 1936 |
EXPECT_TRUE( |
| 1937 |
matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 1938 |
EXPECT_TRUE( |
| 1939 |
matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 1940 |
EXPECT_TRUE( |
| 1941 |
matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 1942 |
EXPECT_TRUE( |
| 1943 |
matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 1944 |
EXPECT_TRUE( |
| 1945 |
matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 1946 |
|
| 1947 |
// We don't match conversion operators. |
| 1948 |
EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 1949 |
|
| 1950 |
// Function calls are not represented as operator. |
| 1951 |
EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 1952 |
|
| 1953 |
// Overloaded operators do not match at all. |
| 1954 |
// FIXME: We probably want to add that. |
| 1955 |
EXPECT_TRUE(notMatches( |
| 1956 |
"struct A { bool operator!() const { return false; } };" |
| 1957 |
"void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 1958 |
} |
| 1959 |
|
| 1960 |
TEST(ArraySubscriptMatchers, ArrayIndex) { |
| 1961 |
EXPECT_TRUE(matches( |
| 1962 |
"int i[2]; void f() { i[1] = 1; }", |
| 1963 |
arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1964 |
EXPECT_TRUE(matches( |
| 1965 |
"int i[2]; void f() { 1[i] = 1; }", |
| 1966 |
arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1967 |
EXPECT_TRUE(notMatches( |
| 1968 |
"int i[2]; void f() { i[1] = 1; }", |
| 1969 |
arraySubscriptExpr(hasIndex(integerLiteral(equals(0)))))); |
| 1970 |
} |
| 1971 |
|
| 1972 |
TEST(ArraySubscriptMatchers, MatchesArrayBase) { |
| 1973 |
EXPECT_TRUE( |
| 1974 |
matches("int i[2]; void f() { i[1] = 2; }", |
| 1975 |
traverse(TK_AsIs, arraySubscriptExpr(hasBase(implicitCastExpr( |
| 1976 |
hasSourceExpression(declRefExpr()))))))); |
| 1977 |
} |
| 1978 |
|
| 1979 |
TEST(Matcher, OfClass) { |
| 1980 |
StatementMatcher Constructor = cxxConstructExpr(hasDeclaration(cxxMethodDecl( |
| 1981 |
ofClass(hasName("X"))))); |
| 1982 |
|
| 1983 |
EXPECT_TRUE( |
| 1984 |
matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 1985 |
EXPECT_TRUE( |
| 1986 |
matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 1987 |
Constructor)); |
| 1988 |
EXPECT_TRUE( |
| 1989 |
notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 1990 |
Constructor)); |
| 1991 |
} |
| 1992 |
|
| 1993 |
TEST(Matcher, VisitsTemplateInstantiations) { |
| 1994 |
EXPECT_TRUE(matches( |
| 1995 |
"class A { public: void x(); };" |
| 1996 |
"template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 1997 |
"void f() { B<A> b; b.y(); }", |
| 1998 |
callExpr(callee(cxxMethodDecl(hasName("x")))))); |
| 1999 |
|
| 2000 |
EXPECT_TRUE(matches( |
| 2001 |
"class A { public: void x(); };" |
| 2002 |
"class C {" |
| 2003 |
" public:" |
| 2004 |
" template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 2005 |
"};" |
| 2006 |
"void f() {" |
| 2007 |
" C::B<A> b; b.y();" |
| 2008 |
"}", |
| 2009 |
recordDecl(hasName("C"), hasDescendant(callExpr( |
| 2010 |
callee(cxxMethodDecl(hasName("x")))))))); |
| 2011 |
} |
| 2012 |
|
| 2013 |
TEST(Matcher, HasCondition) { |
| 2014 |
StatementMatcher IfStmt = |
| 2015 |
ifStmt(hasCondition(cxxBoolLiteral(equals(true)))); |
| 2016 |
EXPECT_TRUE(matches("void x() { if (true) {} }", IfStmt)); |
| 2017 |
EXPECT_TRUE(notMatches("void x() { if (false) {} }", IfStmt)); |
| 2018 |
|
| 2019 |
StatementMatcher ForStmt = |
| 2020 |
forStmt(hasCondition(cxxBoolLiteral(equals(true)))); |
| 2021 |
EXPECT_TRUE(matches("void x() { for (;true;) {} }", ForStmt)); |
| 2022 |
EXPECT_TRUE(notMatches("void x() { for (;false;) {} }", ForStmt)); |
| 2023 |
|
| 2024 |
StatementMatcher WhileStmt = |
| 2025 |
whileStmt(hasCondition(cxxBoolLiteral(equals(true)))); |
| 2026 |
EXPECT_TRUE(matches("void x() { while (true) {} }", WhileStmt)); |
| 2027 |
EXPECT_TRUE(notMatches("void x() { while (false) {} }", WhileStmt)); |
| 2028 |
|
| 2029 |
StatementMatcher SwitchStmt = |
| 2030 |
switchStmt(hasCondition(integerLiteral(equals(42)))); |
| 2031 |
EXPECT_TRUE(matches("void x() { switch (42) {case 42:;} }", SwitchStmt)); |
| 2032 |
EXPECT_TRUE(notMatches("void x() { switch (43) {case 43:;} }", SwitchStmt)); |
| 2033 |
} |
| 2034 |
|
| 2035 |
TEST(For, ForLoopInternals) { |
| 2036 |
EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", |
| 2037 |
forStmt(hasCondition(anything())))); |
| 2038 |
EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }", |
| 2039 |
forStmt(hasLoopInit(anything())))); |
| 2040 |
} |
| 2041 |
|
| 2042 |
TEST(For, ForRangeLoopInternals) { |
| 2043 |
EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }", |
| 2044 |
cxxForRangeStmt(hasLoopVariable(anything())))); |
| 2045 |
EXPECT_TRUE(matches( |
| 2046 |
"void f(){ int a[] {1, 2}; for (int i : a); }", |
| 2047 |
cxxForRangeStmt(hasRangeInit(declRefExpr(to(varDecl(hasName("a")))))))); |
| 2048 |
} |
| 2049 |
|
| 2050 |
TEST(For, NegativeForLoopInternals) { |
| 2051 |
EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }", |
| 2052 |
forStmt(hasCondition(expr())))); |
| 2053 |
EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }", |
| 2054 |
forStmt(hasLoopInit(anything())))); |
| 2055 |
} |
| 2056 |
|
| 2057 |
TEST(HasBody, FindsBodyOfForWhileDoLoops) { |
| 2058 |
EXPECT_TRUE(matches("void f() { for(;;) {} }", |
| 2059 |
forStmt(hasBody(compoundStmt())))); |
| 2060 |
EXPECT_TRUE(notMatches("void f() { for(;;); }", |
| 2061 |
forStmt(hasBody(compoundStmt())))); |
| 2062 |
EXPECT_TRUE(matches("void f() { while(true) {} }", |
| 2063 |
whileStmt(hasBody(compoundStmt())))); |
| 2064 |
EXPECT_TRUE(matches("void f() { do {} while(true); }", |
| 2065 |
doStmt(hasBody(compoundStmt())))); |
| 2066 |
EXPECT_TRUE(matches("void f() { int p[2]; for (auto x : p) {} }", |
| 2067 |
cxxForRangeStmt(hasBody(compoundStmt())))); |
| 2068 |
} |
| 2069 |
|
| 2070 |
TEST(HasBody, FindsBodyOfFunctions) { |
| 2071 |
EXPECT_TRUE(matches("void f() {}", functionDecl(hasBody(compoundStmt())))); |
| 2072 |
EXPECT_TRUE(notMatches("void f();", functionDecl(hasBody(compoundStmt())))); |
| 2073 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2074 |
"void f(); void f() {}", |
| 2075 |
functionDecl(hasBody(compoundStmt())).bind("func"), |
| 2076 |
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("func", 1))); |
| 2077 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2078 |
"class C { void f(); }; void C::f() {}", |
| 2079 |
cxxMethodDecl(hasBody(compoundStmt())).bind("met"), |
| 2080 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("met", 1))); |
| 2081 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2082 |
"class C { C(); }; C::C() {}", |
| 2083 |
cxxConstructorDecl(hasBody(compoundStmt())).bind("ctr"), |
| 2084 |
std::make_unique<VerifyIdIsBoundTo<CXXConstructorDecl>>("ctr", 1))); |
| 2085 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2086 |
"class C { ~C(); }; C::~C() {}", |
| 2087 |
cxxDestructorDecl(hasBody(compoundStmt())).bind("dtr"), |
| 2088 |
std::make_unique<VerifyIdIsBoundTo<CXXDestructorDecl>>("dtr", 1))); |
| 2089 |
} |
| 2090 |
|
| 2091 |
TEST(HasAnyBody, FindsAnyBodyOfFunctions) { |
| 2092 |
EXPECT_TRUE(matches("void f() {}", functionDecl(hasAnyBody(compoundStmt())))); |
| 2093 |
EXPECT_TRUE(notMatches("void f();", |
| 2094 |
functionDecl(hasAnyBody(compoundStmt())))); |
| 2095 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2096 |
"void f(); void f() {}", |
| 2097 |
functionDecl(hasAnyBody(compoundStmt())).bind("func"), |
| 2098 |
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("func", 2))); |
| 2099 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2100 |
"class C { void f(); }; void C::f() {}", |
| 2101 |
cxxMethodDecl(hasAnyBody(compoundStmt())).bind("met"), |
| 2102 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("met", 2))); |
| 2103 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2104 |
"class C { C(); }; C::C() {}", |
| 2105 |
cxxConstructorDecl(hasAnyBody(compoundStmt())).bind("ctr"), |
| 2106 |
std::make_unique<VerifyIdIsBoundTo<CXXConstructorDecl>>("ctr", 2))); |
| 2107 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2108 |
"class C { ~C(); }; C::~C() {}", |
| 2109 |
cxxDestructorDecl(hasAnyBody(compoundStmt())).bind("dtr"), |
| 2110 |
std::make_unique<VerifyIdIsBoundTo<CXXDestructorDecl>>("dtr", 2))); |
| 2111 |
} |
| 2112 |
|
| 2113 |
TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 2114 |
// The simplest case: every compound statement is in a function |
| 2115 |
// definition, and the function body itself must be a compound |
| 2116 |
// statement. |
| 2117 |
EXPECT_TRUE(matches("void f() { for (;;); }", |
| 2118 |
compoundStmt(hasAnySubstatement(forStmt())))); |
| 2119 |
} |
| 2120 |
|
| 2121 |
TEST(HasAnySubstatement, IsNotRecursive) { |
| 2122 |
// It's really "has any immediate substatement". |
| 2123 |
EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
| 2124 |
compoundStmt(hasAnySubstatement(forStmt())))); |
| 2125 |
} |
| 2126 |
|
| 2127 |
TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 2128 |
EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
| 2129 |
compoundStmt(hasAnySubstatement(forStmt())))); |
| 2130 |
} |
| 2131 |
|
| 2132 |
TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 2133 |
EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
| 2134 |
compoundStmt(hasAnySubstatement(forStmt())))); |
| 2135 |
} |
| 2136 |
|
| 2137 |
TEST(Member, MatchesMemberAllocationFunction) { |
| 2138 |
// Fails in C++11 mode |
| 2139 |
EXPECT_TRUE(matchesConditionally( |
| 2140 |
"namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2141 |
"class X { void *operator new(std::size_t); };", |
| 2142 |
cxxMethodDecl(ofClass(hasName("X"))), true, {"-std=gnu++03"})); |
| 2143 |
|
| 2144 |
EXPECT_TRUE(matches("class X { void operator delete(void*); };", |
| 2145 |
cxxMethodDecl(ofClass(hasName("X"))))); |
| 2146 |
|
| 2147 |
// Fails in C++11 mode |
| 2148 |
EXPECT_TRUE(matchesConditionally( |
| 2149 |
"namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2150 |
"class X { void operator delete[](void*, std::size_t); };", |
| 2151 |
cxxMethodDecl(ofClass(hasName("X"))), true, {"-std=gnu++03"})); |
| 2152 |
} |
| 2153 |
|
| 2154 |
TEST(HasDestinationType, MatchesSimpleCase) { |
| 2155 |
EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
| 2156 |
cxxStaticCastExpr(hasDestinationType( |
| 2157 |
pointsTo(TypeMatcher(anything())))))); |
| 2158 |
} |
| 2159 |
|
| 2160 |
TEST(HasImplicitDestinationType, MatchesSimpleCase) { |
| 2161 |
// This test creates an implicit const cast. |
| 2162 |
EXPECT_TRUE(matches( |
| 2163 |
"int x; const int i = x;", |
| 2164 |
traverse(TK_AsIs, |
| 2165 |
implicitCastExpr(hasImplicitDestinationType(isInteger()))))); |
| 2166 |
// This test creates an implicit array-to-pointer cast. |
| 2167 |
EXPECT_TRUE( |
| 2168 |
matches("int arr[3]; int *p = arr;", |
| 2169 |
traverse(TK_AsIs, implicitCastExpr(hasImplicitDestinationType( |
| 2170 |
pointsTo(TypeMatcher(anything()))))))); |
| 2171 |
} |
| 2172 |
|
| 2173 |
TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) { |
| 2174 |
// This test creates an implicit cast from int to char. |
| 2175 |
EXPECT_TRUE(notMatches("char c = 0;", |
| 2176 |
implicitCastExpr(hasImplicitDestinationType( |
| 2177 |
unless(anything()))))); |
| 2178 |
// This test creates an implicit array-to-pointer cast. |
| 2179 |
EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;", |
| 2180 |
implicitCastExpr(hasImplicitDestinationType( |
| 2181 |
unless(anything()))))); |
| 2182 |
} |
| 2183 |
|
| 2184 |
TEST(Matcher, IgnoresElidableConstructors) { |
| 2185 |
EXPECT_TRUE( |
| 2186 |
matches("struct H {};" |
| 2187 |
"template<typename T> H B(T A);" |
| 2188 |
"void f() {" |
| 2189 |
" H D1;" |
| 2190 |
" D1 = B(B(1));" |
| 2191 |
"}", |
| 2192 |
cxxOperatorCallExpr(hasArgument( |
| 2193 |
1, callExpr(hasArgument( |
| 2194 |
0, ignoringElidableConstructorCall(callExpr()))))), |
| 2195 |
langCxx11OrLater())); |
| 2196 |
EXPECT_TRUE( |
| 2197 |
matches("struct H {};" |
| 2198 |
"template<typename T> H B(T A);" |
| 2199 |
"void f() {" |
| 2200 |
" H D1;" |
| 2201 |
" D1 = B(1);" |
| 2202 |
"}", |
| 2203 |
cxxOperatorCallExpr(hasArgument( |
| 2204 |
1, callExpr(hasArgument(0, ignoringElidableConstructorCall( |
| 2205 |
integerLiteral()))))), |
| 2206 |
langCxx11OrLater())); |
| 2207 |
EXPECT_TRUE(matches( |
| 2208 |
"struct H {};" |
| 2209 |
"H G();" |
| 2210 |
"void f() {" |
| 2211 |
" H D = G();" |
| 2212 |
"}", |
| 2213 |
varDecl(hasInitializer(anyOf( |
| 2214 |
ignoringElidableConstructorCall(callExpr()), |
| 2215 |
exprWithCleanups(has(ignoringElidableConstructorCall(callExpr())))))), |
| 2216 |
langCxx11OrLater())); |
| 2217 |
} |
| 2218 |
|
| 2219 |
TEST(Matcher, IgnoresElidableInReturn) { |
| 2220 |
auto matcher = expr(ignoringElidableConstructorCall(declRefExpr())); |
| 2221 |
EXPECT_TRUE(matches("struct H {};" |
| 2222 |
"H f() {" |
| 2223 |
" H g;" |
| 2224 |
" return g;" |
| 2225 |
"}", |
| 2226 |
matcher, langCxx11OrLater())); |
| 2227 |
EXPECT_TRUE(notMatches("struct H {};" |
| 2228 |
"H f() {" |
| 2229 |
" return H();" |
| 2230 |
"}", |
| 2231 |
matcher, langCxx11OrLater())); |
| 2232 |
} |
| 2233 |
|
| 2234 |
TEST(Matcher, IgnoreElidableConstructorDoesNotMatchConstructors) { |
| 2235 |
EXPECT_TRUE(matches("struct H {};" |
| 2236 |
"void f() {" |
| 2237 |
" H D;" |
| 2238 |
"}", |
| 2239 |
varDecl(hasInitializer( |
| 2240 |
ignoringElidableConstructorCall(cxxConstructExpr()))), |
| 2241 |
langCxx11OrLater())); |
| 2242 |
} |
| 2243 |
|
| 2244 |
TEST(Matcher, IgnoresElidableDoesNotPreventMatches) { |
| 2245 |
EXPECT_TRUE(matches("void f() {" |
| 2246 |
" int D = 10;" |
| 2247 |
"}", |
| 2248 |
expr(ignoringElidableConstructorCall(integerLiteral())), |
| 2249 |
langCxx11OrLater())); |
| 2250 |
} |
| 2251 |
|
| 2252 |
TEST(Matcher, IgnoresElidableInVarInit) { |
| 2253 |
auto matcher = |
| 2254 |
varDecl(hasInitializer(ignoringElidableConstructorCall(callExpr()))); |
| 2255 |
EXPECT_TRUE(matches("struct H {};" |
| 2256 |
"H G();" |
| 2257 |
"void f(H D = G()) {" |
| 2258 |
" return;" |
| 2259 |
"}", |
| 2260 |
matcher, langCxx11OrLater())); |
| 2261 |
EXPECT_TRUE(matches("struct H {};" |
| 2262 |
"H G();" |
| 2263 |
"void f() {" |
| 2264 |
" H D = G();" |
| 2265 |
"}", |
| 2266 |
matcher, langCxx11OrLater())); |
| 2267 |
} |
| 2268 |
|
| 2269 |
TEST(IgnoringImplicit, MatchesImplicit) { |
| 2270 |
EXPECT_TRUE(matches("class C {}; C a = C();", |
| 2271 |
varDecl(has(ignoringImplicit(cxxConstructExpr()))))); |
| 2272 |
} |
| 2273 |
|
| 2274 |
TEST(IgnoringImplicit, MatchesNestedImplicit) { |
| 2275 |
StringRef Code = R"( |
| 2276 |
|
| 2277 |
struct OtherType; |
| 2278 |
|
| 2279 |
struct SomeType |
| 2280 |
{ |
| 2281 |
SomeType() {} |
| 2282 |
SomeType(const OtherType&) {} |
| 2283 |
SomeType& operator=(OtherType const&) { return *this; } |
| 2284 |
}; |
| 2285 |
|
| 2286 |
struct OtherType |
| 2287 |
{ |
| 2288 |
OtherType() {} |
| 2289 |
~OtherType() {} |
| 2290 |
}; |
| 2291 |
|
| 2292 |
OtherType something() |
| 2293 |
{ |
| 2294 |
return {}; |
| 2295 |
} |
| 2296 |
|
| 2297 |
int main() |
| 2298 |
{ |
| 2299 |
SomeType i = something(); |
| 2300 |
} |
| 2301 |
)"; |
| 2302 |
EXPECT_TRUE(matches( |
| 2303 |
Code, |
| 2304 |
traverse(TK_AsIs, |
| 2305 |
varDecl(hasName("i"), |
| 2306 |
hasInitializer(exprWithCleanups(has(cxxConstructExpr( |
| 2307 |
has(expr(ignoringImplicit(cxxConstructExpr(has( |
| 2308 |
expr(ignoringImplicit(callExpr()))))))))))))))); |
| 2309 |
} |
| 2310 |
|
| 2311 |
TEST(IgnoringImplicit, DoesNotMatchIncorrectly) { |
| 2312 |
EXPECT_TRUE(notMatches("class C {}; C a = C();", |
| 2313 |
traverse(TK_AsIs, varDecl(has(cxxConstructExpr()))))); |
| 2314 |
} |
| 2315 |
|
| 2316 |
TEST(Traversal, traverseMatcher) { |
| 2317 |
|
| 2318 |
StringRef VarDeclCode = R"cpp( |
| 2319 |
void foo() |
| 2320 |
{ |
| 2321 |
int i = 3.0; |
| 2322 |
} |
| 2323 |
)cpp"; |
| 2324 |
|
| 2325 |
auto Matcher = varDecl(hasInitializer(floatLiteral())); |
| 2326 |
|
| 2327 |
EXPECT_TRUE(notMatches(VarDeclCode, traverse(TK_AsIs, Matcher))); |
| 2328 |
EXPECT_TRUE( |
| 2329 |
matches(VarDeclCode, traverse(TK_IgnoreUnlessSpelledInSource, Matcher))); |
| 2330 |
|
| 2331 |
auto ParentMatcher = floatLiteral(hasParent(varDecl(hasName("i")))); |
| 2332 |
|
| 2333 |
EXPECT_TRUE(notMatches(VarDeclCode, traverse(TK_AsIs, ParentMatcher))); |
| 2334 |
EXPECT_TRUE(matches(VarDeclCode, |
| 2335 |
traverse(TK_IgnoreUnlessSpelledInSource, ParentMatcher))); |
| 2336 |
|
| 2337 |
EXPECT_TRUE(matches( |
| 2338 |
VarDeclCode, decl(traverse(TK_AsIs, anyOf(cxxRecordDecl(), varDecl()))))); |
| 2339 |
|
| 2340 |
EXPECT_TRUE( |
| 2341 |
matches(VarDeclCode, |
| 2342 |
floatLiteral(traverse(TK_AsIs, hasParent(implicitCastExpr()))))); |
| 2343 |
|
| 2344 |
EXPECT_TRUE( |
| 2345 |
matches(VarDeclCode, floatLiteral(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2346 |
hasParent(varDecl()))))); |
| 2347 |
|
| 2348 |
EXPECT_TRUE( |
| 2349 |
matches(VarDeclCode, varDecl(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2350 |
unless(parmVarDecl()))))); |
| 2351 |
|
| 2352 |
EXPECT_TRUE( |
| 2353 |
notMatches(VarDeclCode, varDecl(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2354 |
has(implicitCastExpr()))))); |
| 2355 |
|
| 2356 |
EXPECT_TRUE(matches(VarDeclCode, |
| 2357 |
varDecl(traverse(TK_AsIs, has(implicitCastExpr()))))); |
| 2358 |
|
| 2359 |
EXPECT_TRUE(matches( |
| 2360 |
VarDeclCode, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2361 |
// The has() below strips away the ImplicitCastExpr |
| 2362 |
// before the traverse(AsIs) gets to process it. |
| 2363 |
varDecl(has(traverse(TK_AsIs, floatLiteral())))))); |
| 2364 |
|
| 2365 |
EXPECT_TRUE( |
| 2366 |
matches(VarDeclCode, functionDecl(traverse(TK_AsIs, hasName("foo"))))); |
| 2367 |
|
| 2368 |
EXPECT_TRUE(matches( |
| 2369 |
VarDeclCode, |
| 2370 |
functionDecl(traverse(TK_IgnoreUnlessSpelledInSource, hasName("foo"))))); |
| 2371 |
|
| 2372 |
EXPECT_TRUE(matches( |
| 2373 |
VarDeclCode, functionDecl(traverse(TK_AsIs, hasAnyName("foo", "bar"))))); |
| 2374 |
|
| 2375 |
EXPECT_TRUE( |
| 2376 |
matches(VarDeclCode, functionDecl(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2377 |
hasAnyName("foo", "bar"))))); |
| 2378 |
|
| 2379 |
StringRef Code = R"cpp( |
| 2380 |
void foo(int a) |
| 2381 |
{ |
| 2382 |
int i = 3.0 + a; |
| 2383 |
} |
| 2384 |
void bar() |
| 2385 |
{ |
| 2386 |
foo(7.0); |
| 2387 |
} |
| 2388 |
)cpp"; |
| 2389 |
EXPECT_TRUE( |
| 2390 |
matches(Code, callExpr(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2391 |
hasArgument(0, floatLiteral()))))); |
| 2392 |
|
| 2393 |
EXPECT_TRUE( |
| 2394 |
matches(Code, callExpr(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2395 |
hasAnyArgument(floatLiteral()))))); |
| 2396 |
|
| 2397 |
EXPECT_TRUE(matches( |
| 2398 |
R"cpp( |
| 2399 |
void takesBool(bool){} |
| 2400 |
|
| 2401 |
template <typename T> |
| 2402 |
void neverInstantiatedTemplate() { |
| 2403 |
takesBool(T{}); |
| 2404 |
} |
| 2405 |
)cpp", |
| 2406 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2407 |
callExpr(unless(callExpr(hasDeclaration(functionDecl()))))))); |
| 2408 |
|
| 2409 |
EXPECT_TRUE( |
| 2410 |
matches(VarDeclCode, varDecl(traverse(TK_IgnoreUnlessSpelledInSource, |
| 2411 |
hasType(builtinType()))))); |
| 2412 |
|
| 2413 |
EXPECT_TRUE( |
| 2414 |
matches(VarDeclCode, |
| 2415 |
functionDecl(hasName("foo"), |
| 2416 |
traverse(TK_AsIs, hasDescendant(floatLiteral()))))); |
| 2417 |
|
| 2418 |
EXPECT_TRUE(notMatches( |
| 2419 |
Code, traverse(TK_AsIs, floatLiteral(hasParent(callExpr( |
| 2420 |
callee(functionDecl(hasName("foo"))))))))); |
| 2421 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2422 |
floatLiteral(hasParent(callExpr(callee( |
| 2423 |
functionDecl(hasName("foo"))))))))); |
| 2424 |
|
| 2425 |
Code = R"cpp( |
| 2426 |
void foo() |
| 2427 |
{ |
| 2428 |
int i = (3); |
| 2429 |
} |
| 2430 |
)cpp"; |
| 2431 |
EXPECT_TRUE(matches( |
| 2432 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2433 |
varDecl(hasInitializer(integerLiteral(equals(3))))))); |
| 2434 |
EXPECT_TRUE(matches( |
| 2435 |
Code, |
| 2436 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2437 |
integerLiteral(equals(3), hasParent(varDecl(hasName("i"))))))); |
| 2438 |
|
| 2439 |
Code = R"cpp( |
| 2440 |
const char *SomeString{"str"}; |
| 2441 |
)cpp"; |
| 2442 |
EXPECT_TRUE( |
| 2443 |
matches(Code, traverse(TK_AsIs, stringLiteral(hasParent(implicitCastExpr( |
| 2444 |
hasParent(initListExpr()))))))); |
| 2445 |
EXPECT_TRUE( |
| 2446 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2447 |
stringLiteral(hasParent(initListExpr()))))); |
| 2448 |
|
| 2449 |
Code = R"cpp( |
| 2450 |
struct String |
| 2451 |
{ |
| 2452 |
String(const char*, int = -1) {} |
| 2453 |
}; |
| 2454 |
|
| 2455 |
void stringConstruct() |
| 2456 |
{ |
| 2457 |
String s = "foo"; |
| 2458 |
s = "bar"; |
| 2459 |
} |
| 2460 |
)cpp"; |
| 2461 |
EXPECT_TRUE(matches( |
| 2462 |
Code, |
| 2463 |
traverse( |
| 2464 |
TK_AsIs, |
| 2465 |
functionDecl( |
| 2466 |
hasName("stringConstruct"), |
| 2467 |
hasDescendant(varDecl( |
| 2468 |
hasName("s"), |
| 2469 |
hasInitializer(ignoringImplicit(cxxConstructExpr(hasArgument( |
| 2470 |
0, ignoringImplicit(cxxConstructExpr(hasArgument( |
| 2471 |
0, ignoringImplicit(stringLiteral())))))))))))))); |
| 2472 |
|
| 2473 |
EXPECT_TRUE(matches( |
| 2474 |
Code, |
| 2475 |
traverse( |
| 2476 |
TK_AsIs, |
| 2477 |
functionDecl(hasName("stringConstruct"), |
| 2478 |
hasDescendant(cxxOperatorCallExpr( |
| 2479 |
isAssignmentOperator(), |
| 2480 |
hasArgument(1, ignoringImplicit( |
| 2481 |
cxxConstructExpr(hasArgument( |
| 2482 |
0, ignoringImplicit(stringLiteral()))))) |
| 2483 |
)))))); |
| 2484 |
|
| 2485 |
EXPECT_TRUE(matches( |
| 2486 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2487 |
functionDecl(hasName("stringConstruct"), |
| 2488 |
hasDescendant(varDecl( |
| 2489 |
hasName("s"), |
| 2490 |
hasInitializer(stringLiteral()))))))); |
| 2491 |
|
| 2492 |
EXPECT_TRUE( |
| 2493 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2494 |
functionDecl(hasName("stringConstruct"), |
| 2495 |
hasDescendant(cxxOperatorCallExpr( |
| 2496 |
isAssignmentOperator(), |
| 2497 |
hasArgument(1, stringLiteral()))))))); |
| 2498 |
|
| 2499 |
Code = R"cpp( |
| 2500 |
|
| 2501 |
struct C1 {}; |
| 2502 |
struct C2 { operator C1(); }; |
| 2503 |
|
| 2504 |
void conversionOperator() |
| 2505 |
{ |
| 2506 |
C2* c2; |
| 2507 |
C1 c1 = (*c2); |
| 2508 |
} |
| 2509 |
|
| 2510 |
)cpp"; |
| 2511 |
EXPECT_TRUE(matches( |
| 2512 |
Code, |
| 2513 |
traverse( |
| 2514 |
TK_AsIs, |
| 2515 |
functionDecl( |
| 2516 |
hasName("conversionOperator"), |
| 2517 |
hasDescendant( |
| 2518 |
varDecl( |
| 2519 |
hasName("c1"), |
| 2520 |
hasInitializer( |
| 2521 |
ignoringImplicit(cxxConstructExpr(hasArgument( |
| 2522 |
0, ignoringImplicit( |
| 2523 |
cxxMemberCallExpr(onImplicitObjectArgument( |
| 2524 |
ignoringParenImpCasts(unaryOperator( |
| 2525 |
hasOperatorName("*"))))))))))) |
| 2526 |
.bind("c1")))))); |
| 2527 |
|
| 2528 |
EXPECT_TRUE(matches( |
| 2529 |
Code, |
| 2530 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2531 |
functionDecl(hasName("conversionOperator"), |
| 2532 |
hasDescendant(varDecl( |
| 2533 |
hasName("c1"), hasInitializer(unaryOperator( |
| 2534 |
hasOperatorName("*"))))))))); |
| 2535 |
|
| 2536 |
Code = R"cpp( |
| 2537 |
|
| 2538 |
template <unsigned alignment> |
| 2539 |
void template_test() { |
| 2540 |
static_assert(alignment, ""); |
| 2541 |
} |
| 2542 |
void actual_template_test() { |
| 2543 |
template_test<4>(); |
| 2544 |
} |
| 2545 |
|
| 2546 |
)cpp"; |
| 2547 |
EXPECT_TRUE(matches( |
| 2548 |
Code, |
| 2549 |
traverse(TK_AsIs, |
| 2550 |
staticAssertDecl(has(implicitCastExpr(has( |
| 2551 |
substNonTypeTemplateParmExpr(has(integerLiteral()))))))))); |
| 2552 |
EXPECT_TRUE(matches( |
| 2553 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2554 |
staticAssertDecl(has(declRefExpr( |
| 2555 |
to(nonTypeTemplateParmDecl(hasName("alignment"))), |
| 2556 |
hasType(asString("unsigned int")))))))); |
| 2557 |
|
| 2558 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, staticAssertDecl(hasDescendant( |
| 2559 |
integerLiteral()))))); |
| 2560 |
EXPECT_FALSE(matches( |
| 2561 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 2562 |
staticAssertDecl(hasDescendant(integerLiteral()))))); |
| 2563 |
|
| 2564 |
Code = R"cpp( |
| 2565 |
|
| 2566 |
struct OneParamCtor { |
| 2567 |
explicit OneParamCtor(int); |
| 2568 |
}; |
| 2569 |
struct TwoParamCtor { |
| 2570 |
explicit TwoParamCtor(int, int); |
| 2571 |
}; |
| 2572 |
|
| 2573 |
void varDeclCtors() { |
| 2574 |
{ |
| 2575 |
auto var1 = OneParamCtor(5); |
| 2576 |
auto var2 = TwoParamCtor(6, 7); |
| 2577 |
} |
| 2578 |
{ |
| 2579 |
OneParamCtor var3(5); |
| 2580 |
TwoParamCtor var4(6, 7); |
| 2581 |
} |
| 2582 |
int i = 0; |
| 2583 |
{ |
| 2584 |
auto var5 = OneParamCtor(i); |
| 2585 |
auto var6 = TwoParamCtor(i, 7); |
| 2586 |
} |
| 2587 |
{ |
| 2588 |
OneParamCtor var7(i); |
| 2589 |
TwoParamCtor var8(i, 7); |
| 2590 |
} |
| 2591 |
} |
| 2592 |
|
| 2593 |
)cpp"; |
| 2594 |
EXPECT_TRUE(matches( |
| 2595 |
Code, |
| 2596 |
traverse(TK_AsIs, varDecl(hasName("var1"), hasInitializer(hasDescendant( |
| 2597 |
cxxConstructExpr())))))); |
| 2598 |
EXPECT_TRUE(matches( |
| 2599 |
Code, |
| 2600 |
traverse(TK_AsIs, varDecl(hasName("var2"), hasInitializer(hasDescendant( |
| 2601 |
cxxConstructExpr())))))); |
| 2602 |
EXPECT_TRUE(matches( |
| 2603 |
Code, traverse(TK_AsIs, varDecl(hasName("var3"), |
| 2604 |
hasInitializer(cxxConstructExpr()))))); |
| 2605 |
EXPECT_TRUE(matches( |
| 2606 |
Code, traverse(TK_AsIs, varDecl(hasName("var4"), |
| 2607 |
hasInitializer(cxxConstructExpr()))))); |
| 2608 |
EXPECT_TRUE(matches( |
| 2609 |
Code, |
| 2610 |
traverse(TK_AsIs, varDecl(hasName("var5"), hasInitializer(hasDescendant( |
| 2611 |
cxxConstructExpr())))))); |
| 2612 |
EXPECT_TRUE(matches( |
| 2613 |
Code, |
| 2614 |
traverse(TK_AsIs, varDecl(hasName("var6"), hasInitializer(hasDescendant( |
| 2615 |
cxxConstructExpr())))))); |
| 2616 |
EXPECT_TRUE(matches( |
| 2617 |
Code, traverse(TK_AsIs, varDecl(hasName("var7"), |
| 2618 |
hasInitializer(cxxConstructExpr()))))); |
| 2619 |
EXPECT_TRUE(matches( |
| 2620 |
Code, traverse(TK_AsIs, varDecl(hasName("var8"), |
| 2621 |
hasInitializer(cxxConstructExpr()))))); |
| 2622 |
|
| 2623 |
EXPECT_TRUE(matches( |
| 2624 |
Code, |
| 2625 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2626 |
varDecl(hasName("var1"), hasInitializer(cxxConstructExpr()))))); |
| 2627 |
EXPECT_TRUE(matches( |
| 2628 |
Code, |
| 2629 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2630 |
varDecl(hasName("var2"), hasInitializer(cxxConstructExpr()))))); |
| 2631 |
EXPECT_TRUE(matches( |
| 2632 |
Code, |
| 2633 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2634 |
varDecl(hasName("var3"), hasInitializer(cxxConstructExpr()))))); |
| 2635 |
EXPECT_TRUE(matches( |
| 2636 |
Code, |
| 2637 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2638 |
varDecl(hasName("var4"), hasInitializer(cxxConstructExpr()))))); |
| 2639 |
EXPECT_TRUE(matches( |
| 2640 |
Code, |
| 2641 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2642 |
varDecl(hasName("var5"), hasInitializer(cxxConstructExpr()))))); |
| 2643 |
EXPECT_TRUE(matches( |
| 2644 |
Code, |
| 2645 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2646 |
varDecl(hasName("var6"), hasInitializer(cxxConstructExpr()))))); |
| 2647 |
EXPECT_TRUE(matches( |
| 2648 |
Code, |
| 2649 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2650 |
varDecl(hasName("var7"), hasInitializer(cxxConstructExpr()))))); |
| 2651 |
EXPECT_TRUE(matches( |
| 2652 |
Code, |
| 2653 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 2654 |
varDecl(hasName("var8"), hasInitializer(cxxConstructExpr()))))); |
| 2655 |
|
| 2656 |
Code = R"cpp( |
| 2657 |
|
| 2658 |
template<typename T> |
| 2659 |
struct TemplStruct { |
| 2660 |
TemplStruct() {} |
| 2661 |
~TemplStruct() {} |
| 2662 |
|
| 2663 |
void outOfLine(T); |
| 2664 |
|
| 2665 |
private: |
| 2666 |
T m_t; |
| 2667 |
}; |
| 2668 |
|
| 2669 |
template<typename T> |
| 2670 |
void TemplStruct<T>::outOfLine(T) |
| 2671 |
{ |
| 2672 |
|
| 2673 |
} |
| 2674 |
|
| 2675 |
template<typename T> |
| 2676 |
T timesTwo(T input) |
| 2677 |
{ |
| 2678 |
return input * 2; |
| 2679 |
} |
| 2680 |
|
| 2681 |
void instantiate() |
| 2682 |
{ |
| 2683 |
TemplStruct<int> ti; |
| 2684 |
TemplStruct<double> td; |
| 2685 |
(void)timesTwo<int>(2); |
| 2686 |
(void)timesTwo<double>(2); |
| 2687 |
} |
| 2688 |
|
| 2689 |
template class TemplStruct<float>; |
| 2690 |
|
| 2691 |
extern template class TemplStruct<long>; |
| 2692 |
|
| 2693 |
template<> class TemplStruct<bool> { |
| 2694 |
TemplStruct() {} |
| 2695 |
~TemplStruct() {} |
| 2696 |
|
| 2697 |
void boolSpecializationMethodOnly() {} |
| 2698 |
private: |
| 2699 |
bool m_t; |
| 2700 |
}; |
| 2701 |
|
| 2702 |
template float timesTwo(float); |
| 2703 |
template<> bool timesTwo<bool>(bool){ |
| 2704 |
return true; |
| 2705 |
} |
| 2706 |
)cpp"; |
| 2707 |
{ |
| 2708 |
auto M = cxxRecordDecl(hasName("TemplStruct"), |
| 2709 |
has(fieldDecl(hasType(asString("int"))))); |
| 2710 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2711 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2712 |
} |
| 2713 |
{ |
| 2714 |
auto M = cxxRecordDecl(hasName("TemplStruct"), |
| 2715 |
has(fieldDecl(hasType(asString("double"))))); |
| 2716 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2717 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2718 |
} |
| 2719 |
{ |
| 2720 |
auto M = |
| 2721 |
functionDecl(hasName("timesTwo"), |
| 2722 |
hasParameter(0, parmVarDecl(hasType(asString("int"))))); |
| 2723 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2724 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2725 |
} |
| 2726 |
{ |
| 2727 |
auto M = |
| 2728 |
functionDecl(hasName("timesTwo"), |
| 2729 |
hasParameter(0, parmVarDecl(hasType(asString("double"))))); |
| 2730 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2731 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2732 |
} |
| 2733 |
{ |
| 2734 |
// Match on the integer literal in the explicit instantiation: |
| 2735 |
auto MDef = |
| 2736 |
functionDecl(hasName("timesTwo"), |
| 2737 |
hasParameter(0, parmVarDecl(hasType(asString("float")))), |
| 2738 |
hasDescendant(integerLiteral(equals(2)))); |
| 2739 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MDef))); |
| 2740 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MDef))); |
| 2741 |
|
| 2742 |
auto MTempl = |
| 2743 |
functionDecl(hasName("timesTwo"), |
| 2744 |
hasTemplateArgument(0, refersToType(asString("float")))); |
| 2745 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MTempl))); |
| 2746 |
// TODO: If we could match on explicit instantiations of function templates, |
| 2747 |
// this would be EXPECT_TRUE. See Sema::ActOnExplicitInstantiation. |
| 2748 |
EXPECT_FALSE( |
| 2749 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MTempl))); |
| 2750 |
} |
| 2751 |
{ |
| 2752 |
auto M = functionDecl(hasName("timesTwo"), |
| 2753 |
hasParameter(0, parmVarDecl(hasType(booleanType())))); |
| 2754 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2755 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2756 |
} |
| 2757 |
{ |
| 2758 |
// Match on the field within the explicit instantiation: |
| 2759 |
auto MRecord = cxxRecordDecl(hasName("TemplStruct"), |
| 2760 |
has(fieldDecl(hasType(asString("float"))))); |
| 2761 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MRecord))); |
| 2762 |
EXPECT_FALSE( |
| 2763 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MRecord))); |
| 2764 |
|
| 2765 |
// Match on the explicit template instantiation itself: |
| 2766 |
auto MTempl = classTemplateSpecializationDecl( |
| 2767 |
hasName("TemplStruct"), |
| 2768 |
hasTemplateArgument(0, |
| 2769 |
templateArgument(refersToType(asString("float"))))); |
| 2770 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MTempl))); |
| 2771 |
EXPECT_TRUE( |
| 2772 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MTempl))); |
| 2773 |
} |
| 2774 |
{ |
| 2775 |
// The template argument is matchable, but the instantiation is not: |
| 2776 |
auto M = classTemplateSpecializationDecl( |
| 2777 |
hasName("TemplStruct"), |
| 2778 |
hasTemplateArgument(0, |
| 2779 |
templateArgument(refersToType(asString("float")))), |
| 2780 |
has(cxxConstructorDecl(hasName("TemplStruct")))); |
| 2781 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2782 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2783 |
} |
| 2784 |
{ |
| 2785 |
// The template argument is matchable, but the instantiation is not: |
| 2786 |
auto M = classTemplateSpecializationDecl( |
| 2787 |
hasName("TemplStruct"), |
| 2788 |
hasTemplateArgument(0, |
| 2789 |
templateArgument(refersToType(asString("long")))), |
| 2790 |
has(cxxConstructorDecl(hasName("TemplStruct")))); |
| 2791 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2792 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2793 |
} |
| 2794 |
{ |
| 2795 |
// Instantiated, out-of-line methods are not matchable. |
| 2796 |
auto M = |
| 2797 |
cxxMethodDecl(hasName("outOfLine"), isDefinition(), |
| 2798 |
hasParameter(0, parmVarDecl(hasType(asString("float"))))); |
| 2799 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2800 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2801 |
} |
| 2802 |
{ |
| 2803 |
// Explicit specialization is written in source and it matches: |
| 2804 |
auto M = classTemplateSpecializationDecl( |
| 2805 |
hasName("TemplStruct"), |
| 2806 |
hasTemplateArgument(0, templateArgument(refersToType(booleanType()))), |
| 2807 |
has(cxxConstructorDecl(hasName("TemplStruct"))), |
| 2808 |
has(cxxMethodDecl(hasName("boolSpecializationMethodOnly")))); |
| 2809 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2810 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2811 |
} |
| 2812 |
|
| 2813 |
Code = R"cpp( |
| 2814 |
struct B { |
| 2815 |
B(int); |
| 2816 |
}; |
| 2817 |
|
| 2818 |
B func1() { return 42; } |
| 2819 |
)cpp"; |
| 2820 |
{ |
| 2821 |
auto M = expr(ignoringImplicit(integerLiteral(equals(42)).bind("intLit"))); |
| 2822 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2823 |
Code, traverse(TK_AsIs, M), |
| 2824 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2825 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2826 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2827 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2828 |
} |
| 2829 |
{ |
| 2830 |
auto M = expr(unless(integerLiteral(equals(24)))).bind("intLit"); |
| 2831 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2832 |
Code, traverse(TK_AsIs, M), |
| 2833 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 6))); |
| 2834 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2835 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2836 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2837 |
} |
| 2838 |
{ |
| 2839 |
auto M = |
| 2840 |
expr(anyOf(integerLiteral(equals(42)).bind("intLit"), unless(expr()))); |
| 2841 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2842 |
Code, traverse(TK_AsIs, M), |
| 2843 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2844 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2845 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2846 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2847 |
} |
| 2848 |
{ |
| 2849 |
auto M = expr(allOf(integerLiteral(equals(42)).bind("intLit"), expr())); |
| 2850 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2851 |
Code, traverse(TK_AsIs, M), |
| 2852 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2853 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2854 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2855 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2856 |
} |
| 2857 |
{ |
| 2858 |
auto M = expr(integerLiteral(equals(42)).bind("intLit"), expr()); |
| 2859 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2860 |
Code, traverse(TK_AsIs, M), |
| 2861 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2862 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2863 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2864 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2865 |
} |
| 2866 |
{ |
| 2867 |
auto M = expr(optionally(integerLiteral(equals(42)).bind("intLit"))); |
| 2868 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2869 |
Code, traverse(TK_AsIs, M), |
| 2870 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2871 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2872 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2873 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("intLit", 1))); |
| 2874 |
} |
| 2875 |
{ |
| 2876 |
auto M = expr().bind("allExprs"); |
| 2877 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2878 |
Code, traverse(TK_AsIs, M), |
| 2879 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("allExprs", 6))); |
| 2880 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2881 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2882 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("allExprs", 1))); |
| 2883 |
} |
| 2884 |
|
| 2885 |
Code = R"cpp( |
| 2886 |
void foo() |
| 2887 |
{ |
| 2888 |
int arr[3]; |
| 2889 |
auto &[f, s, t] = arr; |
| 2890 |
|
| 2891 |
f = 42; |
| 2892 |
} |
| 2893 |
)cpp"; |
| 2894 |
{ |
| 2895 |
auto M = bindingDecl(hasName("f")); |
| 2896 |
EXPECT_TRUE( |
| 2897 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++17"})); |
| 2898 |
EXPECT_TRUE( |
| 2899 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2900 |
true, {"-std=c++17"})); |
| 2901 |
} |
| 2902 |
{ |
| 2903 |
auto M = bindingDecl(hasName("f"), has(expr())); |
| 2904 |
EXPECT_TRUE( |
| 2905 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++17"})); |
| 2906 |
EXPECT_FALSE( |
| 2907 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2908 |
true, {"-std=c++17"})); |
| 2909 |
} |
| 2910 |
{ |
| 2911 |
auto M = integerLiteral(hasAncestor(bindingDecl(hasName("f")))); |
| 2912 |
EXPECT_TRUE( |
| 2913 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++17"})); |
| 2914 |
EXPECT_FALSE( |
| 2915 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2916 |
true, {"-std=c++17"})); |
| 2917 |
} |
| 2918 |
{ |
| 2919 |
auto M = declRefExpr(hasAncestor(bindingDecl(hasName("f")))); |
| 2920 |
EXPECT_TRUE( |
| 2921 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++17"})); |
| 2922 |
EXPECT_FALSE( |
| 2923 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 2924 |
true, {"-std=c++17"})); |
| 2925 |
} |
| 2926 |
} |
| 2927 |
|
| 2928 |
TEST(Traversal, traverseNoImplicit) { |
| 2929 |
StringRef Code = R"cpp( |
| 2930 |
struct NonTrivial { |
| 2931 |
NonTrivial() {} |
| 2932 |
NonTrivial(const NonTrivial&) {} |
| 2933 |
NonTrivial& operator=(const NonTrivial&) { return *this; } |
| 2934 |
|
| 2935 |
~NonTrivial() {} |
| 2936 |
}; |
| 2937 |
|
| 2938 |
struct NoSpecialMethods { |
| 2939 |
NonTrivial nt; |
| 2940 |
}; |
| 2941 |
|
| 2942 |
struct ContainsArray { |
| 2943 |
NonTrivial arr[2]; |
| 2944 |
ContainsArray& operator=(const ContainsArray &other) = default; |
| 2945 |
}; |
| 2946 |
|
| 2947 |
void copyIt() |
| 2948 |
{ |
| 2949 |
NoSpecialMethods nc1; |
| 2950 |
NoSpecialMethods nc2(nc1); |
| 2951 |
nc2 = nc1; |
| 2952 |
|
| 2953 |
ContainsArray ca; |
| 2954 |
ContainsArray ca2; |
| 2955 |
ca2 = ca; |
| 2956 |
} |
| 2957 |
|
| 2958 |
struct HasCtorInits : NoSpecialMethods, NonTrivial |
| 2959 |
{ |
| 2960 |
int m_i; |
| 2961 |
NonTrivial m_nt; |
| 2962 |
HasCtorInits() : NoSpecialMethods(), m_i(42) {} |
| 2963 |
}; |
| 2964 |
|
| 2965 |
struct CtorInitsNonTrivial : NonTrivial |
| 2966 |
{ |
| 2967 |
int m_i; |
| 2968 |
NonTrivial m_nt; |
| 2969 |
CtorInitsNonTrivial() : NonTrivial(), m_i(42) {} |
| 2970 |
}; |
| 2971 |
|
| 2972 |
)cpp"; |
| 2973 |
{ |
| 2974 |
auto M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 2975 |
has(cxxRecordDecl(hasName("NoSpecialMethods")))); |
| 2976 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2977 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2978 |
|
| 2979 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 2980 |
has(cxxConstructorDecl(isCopyConstructor()))); |
| 2981 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2982 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2983 |
|
| 2984 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 2985 |
has(cxxMethodDecl(isCopyAssignmentOperator()))); |
| 2986 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2987 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2988 |
|
| 2989 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 2990 |
has(cxxConstructorDecl(isDefaultConstructor()))); |
| 2991 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2992 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2993 |
|
| 2994 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), has(cxxDestructorDecl())); |
| 2995 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 2996 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 2997 |
|
| 2998 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 2999 |
hasMethod(cxxConstructorDecl(isCopyConstructor()))); |
| 3000 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3001 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3002 |
|
| 3003 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 3004 |
hasMethod(cxxMethodDecl(isCopyAssignmentOperator()))); |
| 3005 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3006 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3007 |
|
| 3008 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 3009 |
hasMethod(cxxConstructorDecl(isDefaultConstructor()))); |
| 3010 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3011 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3012 |
|
| 3013 |
M = cxxRecordDecl(hasName("NoSpecialMethods"), |
| 3014 |
hasMethod(cxxDestructorDecl())); |
| 3015 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3016 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3017 |
} |
| 3018 |
{ |
| 3019 |
// Because the copy-assignment operator is not spelled in the |
| 3020 |
// source (ie, isImplicit()), we don't match it |
| 3021 |
auto M = |
| 3022 |
cxxOperatorCallExpr(hasType(cxxRecordDecl(hasName("NoSpecialMethods"))), |
| 3023 |
callee(cxxMethodDecl(isCopyAssignmentOperator()))); |
| 3024 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3025 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3026 |
} |
| 3027 |
{ |
| 3028 |
// Compiler generates a forStmt to copy the array |
| 3029 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, forStmt()))); |
| 3030 |
EXPECT_FALSE( |
| 3031 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, forStmt()))); |
| 3032 |
} |
| 3033 |
{ |
| 3034 |
// The defaulted method declaration can be matched, but not its |
| 3035 |
// definition, in IgnoreUnlessSpelledInSource mode |
| 3036 |
auto MDecl = cxxMethodDecl(ofClass(cxxRecordDecl(hasName("ContainsArray"))), |
| 3037 |
isCopyAssignmentOperator(), isDefaulted()); |
| 3038 |
|
| 3039 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MDecl))); |
| 3040 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MDecl))); |
| 3041 |
|
| 3042 |
auto MDef = cxxMethodDecl(MDecl, has(compoundStmt())); |
| 3043 |
|
| 3044 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MDef))); |
| 3045 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MDef))); |
| 3046 |
|
| 3047 |
auto MBody = cxxMethodDecl(MDecl, hasBody(compoundStmt())); |
| 3048 |
|
| 3049 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MBody))); |
| 3050 |
EXPECT_FALSE( |
| 3051 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MBody))); |
| 3052 |
|
| 3053 |
auto MIsDefn = cxxMethodDecl(MDecl, isDefinition()); |
| 3054 |
|
| 3055 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MIsDefn))); |
| 3056 |
EXPECT_TRUE( |
| 3057 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MIsDefn))); |
| 3058 |
|
| 3059 |
auto MIsInline = cxxMethodDecl(MDecl, isInline()); |
| 3060 |
|
| 3061 |
EXPECT_FALSE(matches(Code, traverse(TK_AsIs, MIsInline))); |
| 3062 |
EXPECT_FALSE( |
| 3063 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MIsInline))); |
| 3064 |
|
| 3065 |
// The parameter of the defaulted method can still be matched. |
| 3066 |
auto MParm = |
| 3067 |
cxxMethodDecl(MDecl, hasParameter(0, parmVarDecl(hasName("other")))); |
| 3068 |
|
| 3069 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, MParm))); |
| 3070 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, MParm))); |
| 3071 |
} |
| 3072 |
{ |
| 3073 |
auto M = |
| 3074 |
cxxConstructorDecl(hasName("HasCtorInits"), |
| 3075 |
has(cxxCtorInitializer(forField(hasName("m_i"))))); |
| 3076 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3077 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3078 |
} |
| 3079 |
{ |
| 3080 |
auto M = |
| 3081 |
cxxConstructorDecl(hasName("HasCtorInits"), |
| 3082 |
has(cxxCtorInitializer(forField(hasName("m_nt"))))); |
| 3083 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3084 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3085 |
} |
| 3086 |
{ |
| 3087 |
auto M = cxxConstructorDecl(hasName("HasCtorInits"), |
| 3088 |
hasAnyConstructorInitializer(cxxCtorInitializer( |
| 3089 |
forField(hasName("m_nt"))))); |
| 3090 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3091 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3092 |
} |
| 3093 |
{ |
| 3094 |
auto M = |
| 3095 |
cxxConstructorDecl(hasName("HasCtorInits"), |
| 3096 |
forEachConstructorInitializer( |
| 3097 |
cxxCtorInitializer(forField(hasName("m_nt"))))); |
| 3098 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3099 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3100 |
} |
| 3101 |
{ |
| 3102 |
auto M = cxxConstructorDecl( |
| 3103 |
hasName("CtorInitsNonTrivial"), |
| 3104 |
has(cxxCtorInitializer(withInitializer(cxxConstructExpr( |
| 3105 |
hasDeclaration(cxxConstructorDecl(hasName("NonTrivial")))))))); |
| 3106 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3107 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3108 |
} |
| 3109 |
{ |
| 3110 |
auto M = cxxConstructorDecl( |
| 3111 |
hasName("HasCtorInits"), |
| 3112 |
has(cxxCtorInitializer(withInitializer(cxxConstructExpr(hasDeclaration( |
| 3113 |
cxxConstructorDecl(hasName("NoSpecialMethods")))))))); |
| 3114 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3115 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3116 |
} |
| 3117 |
{ |
| 3118 |
auto M = cxxCtorInitializer(forField(hasName("m_nt"))); |
| 3119 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3120 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3121 |
} |
| 3122 |
|
| 3123 |
Code = R"cpp( |
| 3124 |
void rangeFor() |
| 3125 |
{ |
| 3126 |
int arr[2]; |
| 3127 |
for (auto i : arr) |
| 3128 |
{ |
| 3129 |
if (true) |
| 3130 |
{ |
| 3131 |
} |
| 3132 |
} |
| 3133 |
} |
| 3134 |
)cpp"; |
| 3135 |
{ |
| 3136 |
auto M = cxxForRangeStmt(has(binaryOperator(hasOperatorName("!=")))); |
| 3137 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3138 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3139 |
} |
| 3140 |
{ |
| 3141 |
auto M = |
| 3142 |
cxxForRangeStmt(hasDescendant(binaryOperator(hasOperatorName("+")))); |
| 3143 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3144 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3145 |
} |
| 3146 |
{ |
| 3147 |
auto M = |
| 3148 |
cxxForRangeStmt(hasDescendant(unaryOperator(hasOperatorName("++")))); |
| 3149 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3150 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3151 |
} |
| 3152 |
{ |
| 3153 |
auto M = cxxForRangeStmt(has(declStmt())); |
| 3154 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3155 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3156 |
} |
| 3157 |
{ |
| 3158 |
auto M = |
| 3159 |
cxxForRangeStmt(hasLoopVariable(varDecl(hasName("i"))), |
| 3160 |
hasRangeInit(declRefExpr(to(varDecl(hasName("arr")))))); |
| 3161 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3162 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3163 |
} |
| 3164 |
{ |
| 3165 |
auto M = cxxForRangeStmt(unless(hasInitStatement(stmt()))); |
| 3166 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3167 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3168 |
} |
| 3169 |
{ |
| 3170 |
auto M = cxxForRangeStmt(hasBody(stmt())); |
| 3171 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3172 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3173 |
} |
| 3174 |
{ |
| 3175 |
auto M = cxxForRangeStmt(hasDescendant(ifStmt())); |
| 3176 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3177 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3178 |
} |
| 3179 |
{ |
| 3180 |
EXPECT_TRUE(matches( |
| 3181 |
Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt( |
| 3182 |
hasSingleDecl(varDecl(hasName("i"))))))))); |
| 3183 |
EXPECT_TRUE( |
| 3184 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 3185 |
cxxForRangeStmt(has(varDecl(hasName("i"))))))); |
| 3186 |
} |
| 3187 |
{ |
| 3188 |
EXPECT_TRUE(matches( |
| 3189 |
Code, traverse(TK_AsIs, cxxForRangeStmt(has(declStmt(hasSingleDecl( |
| 3190 |
varDecl(hasInitializer(declRefExpr( |
| 3191 |
to(varDecl(hasName("arr"))))))))))))); |
| 3192 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 3193 |
cxxForRangeStmt(has(declRefExpr( |
| 3194 |
to(varDecl(hasName("arr"))))))))); |
| 3195 |
} |
| 3196 |
{ |
| 3197 |
auto M = cxxForRangeStmt(has(compoundStmt())); |
| 3198 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3199 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3200 |
} |
| 3201 |
{ |
| 3202 |
auto M = binaryOperator(hasOperatorName("!=")); |
| 3203 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3204 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3205 |
} |
| 3206 |
{ |
| 3207 |
auto M = unaryOperator(hasOperatorName("++")); |
| 3208 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3209 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3210 |
} |
| 3211 |
{ |
| 3212 |
auto M = declStmt(hasSingleDecl(varDecl(matchesName("__range")))); |
| 3213 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3214 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3215 |
} |
| 3216 |
{ |
| 3217 |
auto M = declStmt(hasSingleDecl(varDecl(matchesName("__begin")))); |
| 3218 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3219 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3220 |
} |
| 3221 |
{ |
| 3222 |
auto M = declStmt(hasSingleDecl(varDecl(matchesName("__end")))); |
| 3223 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3224 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3225 |
} |
| 3226 |
{ |
| 3227 |
auto M = ifStmt(hasParent(compoundStmt(hasParent(cxxForRangeStmt())))); |
| 3228 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3229 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3230 |
} |
| 3231 |
{ |
| 3232 |
auto M = cxxForRangeStmt( |
| 3233 |
has(varDecl(hasName("i"), hasParent(cxxForRangeStmt())))); |
| 3234 |
EXPECT_FALSE(matches(Code, traverse(TK_AsIs, M))); |
| 3235 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3236 |
} |
| 3237 |
{ |
| 3238 |
auto M = cxxForRangeStmt(hasDescendant(varDecl( |
| 3239 |
hasName("i"), hasParent(declStmt(hasParent(cxxForRangeStmt())))))); |
| 3240 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3241 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3242 |
} |
| 3243 |
{ |
| 3244 |
auto M = cxxForRangeStmt(hasRangeInit(declRefExpr( |
| 3245 |
to(varDecl(hasName("arr"))), hasParent(cxxForRangeStmt())))); |
| 3246 |
EXPECT_FALSE(matches(Code, traverse(TK_AsIs, M))); |
| 3247 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3248 |
} |
| 3249 |
|
| 3250 |
{ |
| 3251 |
auto M = cxxForRangeStmt(hasRangeInit(declRefExpr( |
| 3252 |
to(varDecl(hasName("arr"))), hasParent(varDecl(hasParent(declStmt( |
| 3253 |
hasParent(cxxForRangeStmt())))))))); |
| 3254 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3255 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3256 |
} |
| 3257 |
|
| 3258 |
Code = R"cpp( |
| 3259 |
struct Range { |
| 3260 |
int* begin() const; |
| 3261 |
int* end() const; |
| 3262 |
}; |
| 3263 |
Range getRange(int); |
| 3264 |
|
| 3265 |
void rangeFor() |
| 3266 |
{ |
| 3267 |
for (auto i : getRange(42)) |
| 3268 |
{ |
| 3269 |
} |
| 3270 |
} |
| 3271 |
)cpp"; |
| 3272 |
{ |
| 3273 |
auto M = integerLiteral(equals(42)); |
| 3274 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3275 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3276 |
} |
| 3277 |
{ |
| 3278 |
auto M = callExpr(hasDescendant(integerLiteral(equals(42)))); |
| 3279 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3280 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3281 |
} |
| 3282 |
{ |
| 3283 |
auto M = compoundStmt(hasDescendant(integerLiteral(equals(42)))); |
| 3284 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3285 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3286 |
} |
| 3287 |
|
| 3288 |
Code = R"cpp( |
| 3289 |
void rangeFor() |
| 3290 |
{ |
| 3291 |
int arr[2]; |
| 3292 |
for (auto& a = arr; auto i : a) |
| 3293 |
{ |
| 3294 |
|
| 3295 |
} |
| 3296 |
} |
| 3297 |
)cpp"; |
| 3298 |
{ |
| 3299 |
auto M = cxxForRangeStmt(has(binaryOperator(hasOperatorName("!=")))); |
| 3300 |
EXPECT_TRUE( |
| 3301 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3302 |
EXPECT_FALSE( |
| 3303 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3304 |
true, {"-std=c++20"})); |
| 3305 |
} |
| 3306 |
{ |
| 3307 |
auto M = |
| 3308 |
cxxForRangeStmt(hasDescendant(binaryOperator(hasOperatorName("+")))); |
| 3309 |
EXPECT_TRUE( |
| 3310 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3311 |
EXPECT_FALSE( |
| 3312 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3313 |
true, {"-std=c++20"})); |
| 3314 |
} |
| 3315 |
{ |
| 3316 |
auto M = |
| 3317 |
cxxForRangeStmt(hasDescendant(unaryOperator(hasOperatorName("++")))); |
| 3318 |
EXPECT_TRUE( |
| 3319 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3320 |
EXPECT_FALSE( |
| 3321 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3322 |
true, {"-std=c++20"})); |
| 3323 |
} |
| 3324 |
{ |
| 3325 |
auto M = |
| 3326 |
cxxForRangeStmt(has(declStmt(hasSingleDecl(varDecl(hasName("i")))))); |
| 3327 |
EXPECT_TRUE( |
| 3328 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3329 |
EXPECT_FALSE( |
| 3330 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3331 |
true, {"-std=c++20"})); |
| 3332 |
} |
| 3333 |
{ |
| 3334 |
auto M = cxxForRangeStmt( |
| 3335 |
hasInitStatement(declStmt(hasSingleDecl(varDecl( |
| 3336 |
hasName("a"), |
| 3337 |
hasInitializer(declRefExpr(to(varDecl(hasName("arr"))))))))), |
| 3338 |
hasLoopVariable(varDecl(hasName("i"))), |
| 3339 |
hasRangeInit(declRefExpr(to(varDecl(hasName("a")))))); |
| 3340 |
EXPECT_TRUE( |
| 3341 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3342 |
EXPECT_TRUE( |
| 3343 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3344 |
true, {"-std=c++20"})); |
| 3345 |
} |
| 3346 |
{ |
| 3347 |
auto M = cxxForRangeStmt( |
| 3348 |
has(declStmt(hasSingleDecl(varDecl( |
| 3349 |
hasName("a"), |
| 3350 |
hasInitializer(declRefExpr(to(varDecl(hasName("arr"))))))))), |
| 3351 |
hasLoopVariable(varDecl(hasName("i"))), |
| 3352 |
hasRangeInit(declRefExpr(to(varDecl(hasName("a")))))); |
| 3353 |
EXPECT_TRUE( |
| 3354 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3355 |
EXPECT_TRUE( |
| 3356 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3357 |
true, {"-std=c++20"})); |
| 3358 |
} |
| 3359 |
{ |
| 3360 |
auto M = cxxForRangeStmt(hasInitStatement(declStmt( |
| 3361 |
hasSingleDecl(varDecl(hasName("a"))), hasParent(cxxForRangeStmt())))); |
| 3362 |
EXPECT_TRUE( |
| 3363 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3364 |
EXPECT_TRUE( |
| 3365 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3366 |
true, {"-std=c++20"})); |
| 3367 |
} |
| 3368 |
|
| 3369 |
Code = R"cpp( |
| 3370 |
struct Range { |
| 3371 |
int* begin() const; |
| 3372 |
int* end() const; |
| 3373 |
}; |
| 3374 |
Range getRange(int); |
| 3375 |
|
| 3376 |
int getNum(int); |
| 3377 |
|
| 3378 |
void rangeFor() |
| 3379 |
{ |
| 3380 |
for (auto j = getNum(42); auto i : getRange(j)) |
| 3381 |
{ |
| 3382 |
} |
| 3383 |
} |
| 3384 |
)cpp"; |
| 3385 |
{ |
| 3386 |
auto M = integerLiteral(equals(42)); |
| 3387 |
EXPECT_TRUE( |
| 3388 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3389 |
EXPECT_TRUE( |
| 3390 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3391 |
true, {"-std=c++20"})); |
| 3392 |
} |
| 3393 |
{ |
| 3394 |
auto M = compoundStmt(hasDescendant(integerLiteral(equals(42)))); |
| 3395 |
EXPECT_TRUE( |
| 3396 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3397 |
EXPECT_TRUE( |
| 3398 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3399 |
true, {"-std=c++20"})); |
| 3400 |
} |
| 3401 |
|
| 3402 |
Code = R"cpp( |
| 3403 |
void hasDefaultArg(int i, int j = 0) |
| 3404 |
{ |
| 3405 |
} |
| 3406 |
void callDefaultArg() |
| 3407 |
{ |
| 3408 |
hasDefaultArg(42); |
| 3409 |
} |
| 3410 |
)cpp"; |
| 3411 |
auto hasDefaultArgCall = [](auto InnerMatcher) { |
| 3412 |
return callExpr(callee(functionDecl(hasName("hasDefaultArg"))), |
| 3413 |
InnerMatcher); |
| 3414 |
}; |
| 3415 |
{ |
| 3416 |
auto M = hasDefaultArgCall(has(integerLiteral(equals(42)))); |
| 3417 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3418 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3419 |
} |
| 3420 |
{ |
| 3421 |
auto M = hasDefaultArgCall(has(cxxDefaultArgExpr())); |
| 3422 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3423 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3424 |
} |
| 3425 |
{ |
| 3426 |
auto M = hasDefaultArgCall(argumentCountIs(2)); |
| 3427 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3428 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3429 |
} |
| 3430 |
{ |
| 3431 |
auto M = hasDefaultArgCall(argumentCountIs(1)); |
| 3432 |
EXPECT_FALSE(matches(Code, traverse(TK_AsIs, M))); |
| 3433 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3434 |
} |
| 3435 |
{ |
| 3436 |
auto M = hasDefaultArgCall(hasArgument(1, cxxDefaultArgExpr())); |
| 3437 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3438 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3439 |
} |
| 3440 |
{ |
| 3441 |
auto M = hasDefaultArgCall(hasAnyArgument(cxxDefaultArgExpr())); |
| 3442 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3443 |
EXPECT_FALSE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3444 |
} |
| 3445 |
Code = R"cpp( |
| 3446 |
struct A |
| 3447 |
{ |
| 3448 |
~A(); |
| 3449 |
private: |
| 3450 |
int i; |
| 3451 |
}; |
| 3452 |
|
| 3453 |
A::~A() = default; |
| 3454 |
)cpp"; |
| 3455 |
{ |
| 3456 |
auto M = cxxDestructorDecl(isDefaulted(), |
| 3457 |
ofClass(cxxRecordDecl(has(fieldDecl())))); |
| 3458 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, M))); |
| 3459 |
EXPECT_TRUE(matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, M))); |
| 3460 |
} |
| 3461 |
Code = R"cpp( |
| 3462 |
struct S |
| 3463 |
{ |
| 3464 |
static constexpr bool getTrue() { return true; } |
| 3465 |
}; |
| 3466 |
|
| 3467 |
struct A |
| 3468 |
{ |
| 3469 |
explicit(S::getTrue()) A(); |
| 3470 |
}; |
| 3471 |
|
| 3472 |
A::A() = default; |
| 3473 |
)cpp"; |
| 3474 |
{ |
| 3475 |
EXPECT_TRUE(matchesConditionally( |
| 3476 |
Code, |
| 3477 |
traverse(TK_AsIs, |
| 3478 |
cxxConstructorDecl( |
| 3479 |
isDefaulted(), |
| 3480 |
hasExplicitSpecifier(expr(ignoringImplicit( |
| 3481 |
callExpr(has(ignoringImplicit(declRefExpr())))))))), |
| 3482 |
true, {"-std=c++20"})); |
| 3483 |
EXPECT_TRUE(matchesConditionally( |
| 3484 |
Code, |
| 3485 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3486 |
cxxConstructorDecl( |
| 3487 |
isDefaulted(), |
| 3488 |
hasExplicitSpecifier(callExpr(has(declRefExpr()))))), |
| 3489 |
true, {"-std=c++20"})); |
| 3490 |
} |
| 3491 |
} |
| 3492 |
|
| 3493 |
template <typename MatcherT> |
| 3494 |
bool matcherTemplateWithBinding(StringRef Code, const MatcherT &M) { |
| 3495 |
return matchAndVerifyResultTrue( |
| 3496 |
Code, M.bind("matchedStmt"), |
| 3497 |
std::make_unique<VerifyIdIsBoundTo<ReturnStmt>>("matchedStmt", 1)); |
| 3498 |
} |
| 3499 |
|
| 3500 |
TEST(Traversal, traverseWithBinding) { |
| 3501 |
// Some existing matcher code expects to take a matcher as a |
| 3502 |
// template arg and bind to it. Verify that that works. |
| 3503 |
|
| 3504 |
llvm::StringRef Code = R"cpp( |
| 3505 |
int foo() |
| 3506 |
{ |
| 3507 |
return 42.0; |
| 3508 |
} |
| 3509 |
)cpp"; |
| 3510 |
EXPECT_TRUE(matcherTemplateWithBinding( |
| 3511 |
Code, traverse(TK_AsIs, |
| 3512 |
returnStmt(has(implicitCastExpr(has(floatLiteral()))))))); |
| 3513 |
} |
| 3514 |
|
| 3515 |
TEST(Traversal, traverseMatcherNesting) { |
| 3516 |
|
| 3517 |
StringRef Code = R"cpp( |
| 3518 |
float bar(int i) |
| 3519 |
{ |
| 3520 |
return i; |
| 3521 |
} |
| 3522 |
|
| 3523 |
void foo() |
| 3524 |
{ |
| 3525 |
bar(bar(3.0)); |
| 3526 |
} |
| 3527 |
)cpp"; |
| 3528 |
|
| 3529 |
EXPECT_TRUE( |
| 3530 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, |
| 3531 |
callExpr(has(callExpr(traverse( |
| 3532 |
TK_AsIs, callExpr(has(implicitCastExpr( |
| 3533 |
has(floatLiteral()))))))))))); |
| 3534 |
|
| 3535 |
EXPECT_TRUE(matches( |
| 3536 |
Code, |
| 3537 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3538 |
traverse(TK_AsIs, implicitCastExpr(has(floatLiteral())))))); |
| 3539 |
} |
| 3540 |
|
| 3541 |
TEST(Traversal, traverseMatcherThroughImplicit) { |
| 3542 |
StringRef Code = R"cpp( |
| 3543 |
struct S { |
| 3544 |
S(int x); |
| 3545 |
}; |
| 3546 |
|
| 3547 |
void constructImplicit() { |
| 3548 |
int a = 8; |
| 3549 |
S s(a); |
| 3550 |
} |
| 3551 |
)cpp"; |
| 3552 |
|
| 3553 |
auto Matcher = traverse(TK_IgnoreUnlessSpelledInSource, implicitCastExpr()); |
| 3554 |
|
| 3555 |
// Verfiy that it does not segfault |
| 3556 |
EXPECT_FALSE(matches(Code, Matcher)); |
| 3557 |
} |
| 3558 |
|
| 3559 |
TEST(Traversal, traverseMatcherThroughMemoization) { |
| 3560 |
|
| 3561 |
StringRef Code = R"cpp( |
| 3562 |
void foo() |
| 3563 |
{ |
| 3564 |
int i = 3.0; |
| 3565 |
} |
| 3566 |
)cpp"; |
| 3567 |
|
| 3568 |
auto Matcher = varDecl(hasInitializer(floatLiteral())); |
| 3569 |
|
| 3570 |
// Matchers such as hasDescendant memoize their result regarding AST |
| 3571 |
// nodes. In the matcher below, the first use of hasDescendant(Matcher) |
| 3572 |
// fails, and the use of it inside the traverse() matcher should pass |
| 3573 |
// causing the overall matcher to be a true match. |
| 3574 |
// This test verifies that the first false result is not re-used, which |
| 3575 |
// would cause the overall matcher to be incorrectly false. |
| 3576 |
|
| 3577 |
EXPECT_TRUE(matches( |
| 3578 |
Code, |
| 3579 |
functionDecl(anyOf(hasDescendant(Matcher), |
| 3580 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3581 |
functionDecl(hasDescendant(Matcher))))))); |
| 3582 |
} |
| 3583 |
|
| 3584 |
TEST(Traversal, traverseUnlessSpelledInSource) { |
| 3585 |
|
| 3586 |
StringRef Code = R"cpp( |
| 3587 |
|
| 3588 |
struct A |
| 3589 |
{ |
| 3590 |
}; |
| 3591 |
|
| 3592 |
struct B |
| 3593 |
{ |
| 3594 |
B(int); |
| 3595 |
B(A const& a); |
| 3596 |
B(); |
| 3597 |
}; |
| 3598 |
|
| 3599 |
struct C |
| 3600 |
{ |
| 3601 |
operator B(); |
| 3602 |
}; |
| 3603 |
|
| 3604 |
B func1() { |
| 3605 |
return 42; |
| 3606 |
} |
| 3607 |
|
| 3608 |
B func2() { |
| 3609 |
return B{42}; |
| 3610 |
} |
| 3611 |
|
| 3612 |
B func3() { |
| 3613 |
return B(42); |
| 3614 |
} |
| 3615 |
|
| 3616 |
B func4() { |
| 3617 |
return B(); |
| 3618 |
} |
| 3619 |
|
| 3620 |
B func5() { |
| 3621 |
return B{}; |
| 3622 |
} |
| 3623 |
|
| 3624 |
B func6() { |
| 3625 |
return C(); |
| 3626 |
} |
| 3627 |
|
| 3628 |
B func7() { |
| 3629 |
return A(); |
| 3630 |
} |
| 3631 |
|
| 3632 |
B func8() { |
| 3633 |
return C{}; |
| 3634 |
} |
| 3635 |
|
| 3636 |
B func9() { |
| 3637 |
return A{}; |
| 3638 |
} |
| 3639 |
|
| 3640 |
B func10() { |
| 3641 |
A a; |
| 3642 |
return a; |
| 3643 |
} |
| 3644 |
|
| 3645 |
B func11() { |
| 3646 |
B b; |
| 3647 |
return b; |
| 3648 |
} |
| 3649 |
|
| 3650 |
B func12() { |
| 3651 |
C c; |
| 3652 |
return c; |
| 3653 |
} |
| 3654 |
|
| 3655 |
void func13() { |
| 3656 |
int a = 0; |
| 3657 |
int c = 0; |
| 3658 |
|
| 3659 |
[a, b = c](int d) { int e = d; }; |
| 3660 |
} |
| 3661 |
|
| 3662 |
void func14() { |
| 3663 |
[] <typename TemplateType> (TemplateType t, TemplateType u) { int e = t + u; }; |
| 3664 |
float i = 42.0; |
| 3665 |
} |
| 3666 |
|
| 3667 |
void func15() { |
| 3668 |
int count = 0; |
| 3669 |
auto l = [&] { ++count; }; |
| 3670 |
(void)l; |
| 3671 |
} |
| 3672 |
|
| 3673 |
)cpp"; |
| 3674 |
|
| 3675 |
EXPECT_TRUE( |
| 3676 |
matches(Code, |
| 3677 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3678 |
returnStmt(forFunction(functionDecl(hasName("func1"))), |
| 3679 |
hasReturnValue(integerLiteral(equals(42))))), |
| 3680 |
langCxx20OrLater())); |
| 3681 |
|
| 3682 |
EXPECT_TRUE( |
| 3683 |
matches(Code, |
| 3684 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3685 |
integerLiteral(equals(42), |
| 3686 |
hasParent(returnStmt(forFunction( |
| 3687 |
functionDecl(hasName("func1"))))))), |
| 3688 |
langCxx20OrLater())); |
| 3689 |
|
| 3690 |
EXPECT_TRUE(matches( |
| 3691 |
Code, |
| 3692 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3693 |
returnStmt(forFunction(functionDecl(hasName("func2"))), |
| 3694 |
hasReturnValue(cxxTemporaryObjectExpr( |
| 3695 |
hasArgument(0, integerLiteral(equals(42))))))), |
| 3696 |
langCxx20OrLater())); |
| 3697 |
EXPECT_TRUE(matches( |
| 3698 |
Code, |
| 3699 |
traverse( |
| 3700 |
TK_IgnoreUnlessSpelledInSource, |
| 3701 |
integerLiteral(equals(42), |
| 3702 |
hasParent(cxxTemporaryObjectExpr(hasParent(returnStmt( |
| 3703 |
forFunction(functionDecl(hasName("func2"))))))))), |
| 3704 |
langCxx20OrLater())); |
| 3705 |
|
| 3706 |
EXPECT_TRUE( |
| 3707 |
matches(Code, |
| 3708 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3709 |
returnStmt(forFunction(functionDecl(hasName("func3"))), |
| 3710 |
hasReturnValue(cxxConstructExpr(hasArgument( |
| 3711 |
0, integerLiteral(equals(42))))))), |
| 3712 |
langCxx20OrLater())); |
| 3713 |
|
| 3714 |
EXPECT_TRUE(matches( |
| 3715 |
Code, |
| 3716 |
traverse( |
| 3717 |
TK_IgnoreUnlessSpelledInSource, |
| 3718 |
integerLiteral(equals(42), |
| 3719 |
hasParent(cxxConstructExpr(hasParent(returnStmt( |
| 3720 |
forFunction(functionDecl(hasName("func3"))))))))), |
| 3721 |
langCxx20OrLater())); |
| 3722 |
|
| 3723 |
EXPECT_TRUE( |
| 3724 |
matches(Code, |
| 3725 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3726 |
returnStmt(forFunction(functionDecl(hasName("func4"))), |
| 3727 |
hasReturnValue(cxxTemporaryObjectExpr()))), |
| 3728 |
langCxx20OrLater())); |
| 3729 |
|
| 3730 |
EXPECT_TRUE( |
| 3731 |
matches(Code, |
| 3732 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3733 |
returnStmt(forFunction(functionDecl(hasName("func5"))), |
| 3734 |
hasReturnValue(cxxTemporaryObjectExpr()))), |
| 3735 |
langCxx20OrLater())); |
| 3736 |
|
| 3737 |
EXPECT_TRUE( |
| 3738 |
matches(Code, |
| 3739 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3740 |
returnStmt(forFunction(functionDecl(hasName("func6"))), |
| 3741 |
hasReturnValue(cxxTemporaryObjectExpr()))), |
| 3742 |
langCxx20OrLater())); |
| 3743 |
|
| 3744 |
EXPECT_TRUE( |
| 3745 |
matches(Code, |
| 3746 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3747 |
returnStmt(forFunction(functionDecl(hasName("func7"))), |
| 3748 |
hasReturnValue(cxxTemporaryObjectExpr()))), |
| 3749 |
langCxx20OrLater())); |
| 3750 |
|
| 3751 |
EXPECT_TRUE( |
| 3752 |
matches(Code, |
| 3753 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3754 |
returnStmt(forFunction(functionDecl(hasName("func8"))), |
| 3755 |
hasReturnValue(cxxFunctionalCastExpr( |
| 3756 |
hasSourceExpression(initListExpr()))))), |
| 3757 |
langCxx20OrLater())); |
| 3758 |
|
| 3759 |
EXPECT_TRUE( |
| 3760 |
matches(Code, |
| 3761 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3762 |
returnStmt(forFunction(functionDecl(hasName("func9"))), |
| 3763 |
hasReturnValue(cxxFunctionalCastExpr( |
| 3764 |
hasSourceExpression(initListExpr()))))), |
| 3765 |
langCxx20OrLater())); |
| 3766 |
|
| 3767 |
EXPECT_TRUE(matches( |
| 3768 |
Code, |
| 3769 |
traverse( |
| 3770 |
TK_IgnoreUnlessSpelledInSource, |
| 3771 |
returnStmt(forFunction(functionDecl(hasName("func10"))), |
| 3772 |
hasReturnValue(declRefExpr(to(varDecl(hasName("a"))))))), |
| 3773 |
langCxx20OrLater())); |
| 3774 |
|
| 3775 |
EXPECT_TRUE( |
| 3776 |
matches(Code, |
| 3777 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3778 |
declRefExpr(to(varDecl(hasName("a"))), |
| 3779 |
hasParent(returnStmt(forFunction( |
| 3780 |
functionDecl(hasName("func10"))))))), |
| 3781 |
langCxx20OrLater())); |
| 3782 |
|
| 3783 |
EXPECT_TRUE(matches( |
| 3784 |
Code, |
| 3785 |
traverse( |
| 3786 |
TK_IgnoreUnlessSpelledInSource, |
| 3787 |
returnStmt(forFunction(functionDecl(hasName("func11"))), |
| 3788 |
hasReturnValue(declRefExpr(to(varDecl(hasName("b"))))))), |
| 3789 |
langCxx20OrLater())); |
| 3790 |
|
| 3791 |
EXPECT_TRUE( |
| 3792 |
matches(Code, |
| 3793 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3794 |
declRefExpr(to(varDecl(hasName("b"))), |
| 3795 |
hasParent(returnStmt(forFunction( |
| 3796 |
functionDecl(hasName("func11"))))))), |
| 3797 |
langCxx20OrLater())); |
| 3798 |
|
| 3799 |
EXPECT_TRUE(matches( |
| 3800 |
Code, |
| 3801 |
traverse( |
| 3802 |
TK_IgnoreUnlessSpelledInSource, |
| 3803 |
returnStmt(forFunction(functionDecl(hasName("func12"))), |
| 3804 |
hasReturnValue(declRefExpr(to(varDecl(hasName("c"))))))), |
| 3805 |
langCxx20OrLater())); |
| 3806 |
|
| 3807 |
EXPECT_TRUE( |
| 3808 |
matches(Code, |
| 3809 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3810 |
declRefExpr(to(varDecl(hasName("c"))), |
| 3811 |
hasParent(returnStmt(forFunction( |
| 3812 |
functionDecl(hasName("func12"))))))), |
| 3813 |
langCxx20OrLater())); |
| 3814 |
|
| 3815 |
EXPECT_TRUE(matches( |
| 3816 |
Code, |
| 3817 |
traverse( |
| 3818 |
TK_IgnoreUnlessSpelledInSource, |
| 3819 |
lambdaExpr(forFunction(functionDecl(hasName("func13"))), |
| 3820 |
has(compoundStmt(hasDescendant(varDecl(hasName("e"))))), |
| 3821 |
has(declRefExpr(to(varDecl(hasName("a"))))), |
| 3822 |
has(varDecl(hasName("b"), hasInitializer(declRefExpr(to( |
| 3823 |
varDecl(hasName("c"))))))), |
| 3824 |
has(parmVarDecl(hasName("d"))))), |
| 3825 |
langCxx20OrLater())); |
| 3826 |
|
| 3827 |
EXPECT_TRUE( |
| 3828 |
matches(Code, |
| 3829 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3830 |
declRefExpr(to(varDecl(hasName("a"))), |
| 3831 |
hasParent(lambdaExpr(forFunction( |
| 3832 |
functionDecl(hasName("func13"))))))), |
| 3833 |
langCxx20OrLater())); |
| 3834 |
|
| 3835 |
EXPECT_TRUE(matches( |
| 3836 |
Code, |
| 3837 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3838 |
varDecl(hasName("b"), |
| 3839 |
hasInitializer(declRefExpr(to(varDecl(hasName("c"))))), |
| 3840 |
hasParent(lambdaExpr( |
| 3841 |
forFunction(functionDecl(hasName("func13"))))))), |
| 3842 |
langCxx20OrLater())); |
| 3843 |
|
| 3844 |
EXPECT_TRUE(matches(Code, |
| 3845 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3846 |
compoundStmt(hasParent(lambdaExpr(forFunction( |
| 3847 |
functionDecl(hasName("func13"))))))), |
| 3848 |
langCxx20OrLater())); |
| 3849 |
|
| 3850 |
EXPECT_TRUE(matches( |
| 3851 |
Code, |
| 3852 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3853 |
templateTypeParmDecl(hasName("TemplateType"), |
| 3854 |
hasParent(lambdaExpr(forFunction( |
| 3855 |
functionDecl(hasName("func14"))))))), |
| 3856 |
langCxx20OrLater())); |
| 3857 |
|
| 3858 |
EXPECT_TRUE(matches( |
| 3859 |
Code, |
| 3860 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3861 |
lambdaExpr(forFunction(functionDecl(hasName("func14"))), |
| 3862 |
has(templateTypeParmDecl(hasName("TemplateType"))))), |
| 3863 |
langCxx20OrLater())); |
| 3864 |
|
| 3865 |
EXPECT_TRUE(matches( |
| 3866 |
Code, |
| 3867 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3868 |
functionDecl(hasName("func14"), hasDescendant(floatLiteral()))), |
| 3869 |
langCxx20OrLater())); |
| 3870 |
|
| 3871 |
EXPECT_TRUE(matches( |
| 3872 |
Code, |
| 3873 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 3874 |
compoundStmt( |
| 3875 |
hasDescendant(varDecl(hasName("count")).bind("countVar")), |
| 3876 |
hasDescendant( |
| 3877 |
declRefExpr(to(varDecl(equalsBoundNode("countVar"))))))), |
| 3878 |
langCxx20OrLater())); |
| 3879 |
|
| 3880 |
Code = R"cpp( |
| 3881 |
void foo() { |
| 3882 |
int explicit_captured = 0; |
| 3883 |
int implicit_captured = 0; |
| 3884 |
auto l = [&, explicit_captured](int i) { |
| 3885 |
if (i || explicit_captured || implicit_captured) return; |
| 3886 |
}; |
| 3887 |
} |
| 3888 |
)cpp"; |
| 3889 |
|
| 3890 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, ifStmt()))); |
| 3891 |
EXPECT_TRUE( |
| 3892 |
matches(Code, traverse(TK_IgnoreUnlessSpelledInSource, ifStmt()))); |
| 3893 |
|
| 3894 |
auto lambdaExplicitCapture = declRefExpr( |
| 3895 |
to(varDecl(hasName("explicit_captured"))), unless(hasAncestor(ifStmt()))); |
| 3896 |
auto lambdaImplicitCapture = declRefExpr( |
| 3897 |
to(varDecl(hasName("implicit_captured"))), unless(hasAncestor(ifStmt()))); |
| 3898 |
|
| 3899 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, lambdaExplicitCapture))); |
| 3900 |
EXPECT_TRUE(matches( |
| 3901 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, lambdaExplicitCapture))); |
| 3902 |
|
| 3903 |
EXPECT_TRUE(matches(Code, traverse(TK_AsIs, lambdaImplicitCapture))); |
| 3904 |
EXPECT_FALSE(matches( |
| 3905 |
Code, traverse(TK_IgnoreUnlessSpelledInSource, lambdaImplicitCapture))); |
| 3906 |
|
| 3907 |
Code = R"cpp( |
| 3908 |
struct S {}; |
| 3909 |
|
| 3910 |
struct HasOpEq |
| 3911 |
{ |
| 3912 |
bool operator==(const S& other) |
| 3913 |
{ |
| 3914 |
return true; |
| 3915 |
} |
| 3916 |
}; |
| 3917 |
|
| 3918 |
void binop() |
| 3919 |
{ |
| 3920 |
HasOpEq s1; |
| 3921 |
S s2; |
| 3922 |
if (s1 != s2) |
| 3923 |
return; |
| 3924 |
} |
| 3925 |
)cpp"; |
| 3926 |
{ |
| 3927 |
auto M = unaryOperator( |
| 3928 |
hasOperatorName("!"), |
| 3929 |
has(cxxOperatorCallExpr(hasOverloadedOperatorName("==")))); |
| 3930 |
EXPECT_TRUE( |
| 3931 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3932 |
EXPECT_FALSE( |
| 3933 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3934 |
true, {"-std=c++20"})); |
| 3935 |
} |
| 3936 |
{ |
| 3937 |
auto M = declRefExpr(to(varDecl(hasName("s1")))); |
| 3938 |
EXPECT_TRUE( |
| 3939 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3940 |
EXPECT_TRUE( |
| 3941 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3942 |
true, {"-std=c++20"})); |
| 3943 |
} |
| 3944 |
{ |
| 3945 |
auto M = cxxOperatorCallExpr(hasOverloadedOperatorName("==")); |
| 3946 |
EXPECT_TRUE( |
| 3947 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3948 |
EXPECT_FALSE( |
| 3949 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3950 |
true, {"-std=c++20"})); |
| 3951 |
} |
| 3952 |
{ |
| 3953 |
auto M = cxxOperatorCallExpr(hasOverloadedOperatorName("!=")); |
| 3954 |
EXPECT_FALSE( |
| 3955 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3956 |
EXPECT_FALSE( |
| 3957 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3958 |
true, {"-std=c++20"})); |
| 3959 |
} |
| 3960 |
auto withDescendants = [](StringRef lName, StringRef rName) { |
| 3961 |
return stmt(hasDescendant(declRefExpr(to(varDecl(hasName(lName))))), |
| 3962 |
hasDescendant(declRefExpr(to(varDecl(hasName(rName)))))); |
| 3963 |
}; |
| 3964 |
{ |
| 3965 |
auto M = cxxRewrittenBinaryOperator(withDescendants("s1", "s2")); |
| 3966 |
EXPECT_TRUE( |
| 3967 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3968 |
EXPECT_TRUE( |
| 3969 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3970 |
true, {"-std=c++20"})); |
| 3971 |
} |
| 3972 |
{ |
| 3973 |
auto M = cxxRewrittenBinaryOperator( |
| 3974 |
has(declRefExpr(to(varDecl(hasName("s1"))))), |
| 3975 |
has(declRefExpr(to(varDecl(hasName("s2")))))); |
| 3976 |
EXPECT_FALSE( |
| 3977 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3978 |
EXPECT_TRUE( |
| 3979 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3980 |
true, {"-std=c++20"})); |
| 3981 |
} |
| 3982 |
{ |
| 3983 |
auto M = cxxRewrittenBinaryOperator( |
| 3984 |
hasLHS(expr(hasParent(cxxRewrittenBinaryOperator()))), |
| 3985 |
hasRHS(expr(hasParent(cxxRewrittenBinaryOperator())))); |
| 3986 |
EXPECT_FALSE( |
| 3987 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 3988 |
EXPECT_TRUE( |
| 3989 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 3990 |
true, {"-std=c++20"})); |
| 3991 |
} |
| 3992 |
{ |
| 3993 |
EXPECT_TRUE(matchesConditionally( |
| 3994 |
Code, |
| 3995 |
traverse(TK_AsIs, |
| 3996 |
cxxRewrittenBinaryOperator( |
| 3997 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 3998 |
isComparisonOperator(), |
| 3999 |
hasLHS(ignoringImplicit( |
| 4000 |
declRefExpr(to(varDecl(hasName("s1")))))), |
| 4001 |
hasRHS(ignoringImplicit( |
| 4002 |
declRefExpr(to(varDecl(hasName("s2")))))), |
| 4003 |
hasEitherOperand(ignoringImplicit( |
| 4004 |
declRefExpr(to(varDecl(hasName("s2")))))), |
| 4005 |
hasOperands(ignoringImplicit( |
| 4006 |
declRefExpr(to(varDecl(hasName("s1"))))), |
| 4007 |
ignoringImplicit(declRefExpr( |
| 4008 |
to(varDecl(hasName("s2")))))))), |
| 4009 |
true, {"-std=c++20"})); |
| 4010 |
EXPECT_TRUE(matchesConditionally( |
| 4011 |
Code, |
| 4012 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4013 |
cxxRewrittenBinaryOperator( |
| 4014 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 4015 |
isComparisonOperator(), |
| 4016 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 4017 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))), |
| 4018 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("s2"))))), |
| 4019 |
hasOperands(declRefExpr(to(varDecl(hasName("s1")))), |
| 4020 |
declRefExpr(to(varDecl(hasName("s2"))))))), |
| 4021 |
true, {"-std=c++20"})); |
| 4022 |
} |
| 4023 |
|
| 4024 |
Code = R"cpp( |
| 4025 |
namespace std { |
| 4026 |
struct strong_ordering { |
| 4027 |
int n; |
| 4028 |
constexpr operator int() const { return n; } |
| 4029 |
static const strong_ordering equal, greater, less; |
| 4030 |
}; |
| 4031 |
constexpr strong_ordering strong_ordering::equal = {0}; |
| 4032 |
constexpr strong_ordering strong_ordering::greater = {1}; |
| 4033 |
constexpr strong_ordering strong_ordering::less = {-1}; |
| 4034 |
} |
| 4035 |
|
| 4036 |
struct HasSpaceshipMem { |
| 4037 |
int a; |
| 4038 |
constexpr auto operator<=>(const HasSpaceshipMem&) const = default; |
| 4039 |
}; |
| 4040 |
|
| 4041 |
void binop() |
| 4042 |
{ |
| 4043 |
HasSpaceshipMem hs1, hs2; |
| 4044 |
if (hs1 == hs2) |
| 4045 |
return; |
| 4046 |
|
| 4047 |
HasSpaceshipMem hs3, hs4; |
| 4048 |
if (hs3 != hs4) |
| 4049 |
return; |
| 4050 |
|
| 4051 |
HasSpaceshipMem hs5, hs6; |
| 4052 |
if (hs5 < hs6) |
| 4053 |
return; |
| 4054 |
|
| 4055 |
HasSpaceshipMem hs7, hs8; |
| 4056 |
if (hs7 > hs8) |
| 4057 |
return; |
| 4058 |
|
| 4059 |
HasSpaceshipMem hs9, hs10; |
| 4060 |
if (hs9 <= hs10) |
| 4061 |
return; |
| 4062 |
|
| 4063 |
HasSpaceshipMem hs11, hs12; |
| 4064 |
if (hs11 >= hs12) |
| 4065 |
return; |
| 4066 |
} |
| 4067 |
)cpp"; |
| 4068 |
auto withArgs = [](StringRef lName, StringRef rName) { |
| 4069 |
return cxxOperatorCallExpr( |
| 4070 |
hasArgument(0, declRefExpr(to(varDecl(hasName(lName))))), |
| 4071 |
hasArgument(1, declRefExpr(to(varDecl(hasName(rName)))))); |
| 4072 |
}; |
| 4073 |
{ |
| 4074 |
auto M = ifStmt(hasCondition(cxxOperatorCallExpr( |
| 4075 |
hasOverloadedOperatorName("=="), withArgs("hs1", "hs2")))); |
| 4076 |
EXPECT_TRUE( |
| 4077 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4078 |
EXPECT_TRUE( |
| 4079 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4080 |
true, {"-std=c++20"})); |
| 4081 |
} |
| 4082 |
{ |
| 4083 |
auto M = |
| 4084 |
unaryOperator(hasOperatorName("!"), |
| 4085 |
has(cxxOperatorCallExpr(hasOverloadedOperatorName("=="), |
| 4086 |
withArgs("hs3", "hs4")))); |
| 4087 |
EXPECT_TRUE( |
| 4088 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4089 |
EXPECT_FALSE( |
| 4090 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4091 |
true, {"-std=c++20"})); |
| 4092 |
} |
| 4093 |
{ |
| 4094 |
auto M = |
| 4095 |
unaryOperator(hasOperatorName("!"), |
| 4096 |
has(cxxOperatorCallExpr(hasOverloadedOperatorName("=="), |
| 4097 |
withArgs("hs3", "hs4")))); |
| 4098 |
EXPECT_TRUE( |
| 4099 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4100 |
EXPECT_FALSE( |
| 4101 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4102 |
true, {"-std=c++20"})); |
| 4103 |
} |
| 4104 |
{ |
| 4105 |
auto M = binaryOperator( |
| 4106 |
hasOperatorName("<"), |
| 4107 |
hasLHS(hasDescendant(cxxOperatorCallExpr( |
| 4108 |
hasOverloadedOperatorName("<=>"), withArgs("hs5", "hs6")))), |
| 4109 |
hasRHS(integerLiteral(equals(0)))); |
| 4110 |
EXPECT_TRUE( |
| 4111 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4112 |
EXPECT_FALSE( |
| 4113 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4114 |
true, {"-std=c++20"})); |
| 4115 |
} |
| 4116 |
{ |
| 4117 |
auto M = cxxRewrittenBinaryOperator(withDescendants("hs3", "hs4")); |
| 4118 |
EXPECT_TRUE( |
| 4119 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4120 |
EXPECT_TRUE( |
| 4121 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4122 |
true, {"-std=c++20"})); |
| 4123 |
} |
| 4124 |
{ |
| 4125 |
auto M = declRefExpr(to(varDecl(hasName("hs3")))); |
| 4126 |
EXPECT_TRUE( |
| 4127 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4128 |
EXPECT_TRUE( |
| 4129 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4130 |
true, {"-std=c++20"})); |
| 4131 |
} |
| 4132 |
{ |
| 4133 |
auto M = cxxRewrittenBinaryOperator(has( |
| 4134 |
unaryOperator(hasOperatorName("!"), withDescendants("hs3", "hs4")))); |
| 4135 |
EXPECT_TRUE( |
| 4136 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4137 |
EXPECT_FALSE( |
| 4138 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4139 |
true, {"-std=c++20"})); |
| 4140 |
} |
| 4141 |
{ |
| 4142 |
auto M = cxxRewrittenBinaryOperator( |
| 4143 |
has(declRefExpr(to(varDecl(hasName("hs3"))))), |
| 4144 |
has(declRefExpr(to(varDecl(hasName("hs4")))))); |
| 4145 |
EXPECT_FALSE( |
| 4146 |
matchesConditionally(Code, traverse(TK_AsIs, M), true, {"-std=c++20"})); |
| 4147 |
EXPECT_TRUE( |
| 4148 |
matchesConditionally(Code, traverse(TK_IgnoreUnlessSpelledInSource, M), |
| 4149 |
true, {"-std=c++20"})); |
| 4150 |
} |
| 4151 |
{ |
| 4152 |
EXPECT_TRUE(matchesConditionally( |
| 4153 |
Code, |
| 4154 |
traverse(TK_AsIs, |
| 4155 |
cxxRewrittenBinaryOperator( |
| 4156 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 4157 |
isComparisonOperator(), |
| 4158 |
hasLHS(ignoringImplicit( |
| 4159 |
declRefExpr(to(varDecl(hasName("hs3")))))), |
| 4160 |
hasRHS(ignoringImplicit( |
| 4161 |
declRefExpr(to(varDecl(hasName("hs4")))))), |
| 4162 |
hasEitherOperand(ignoringImplicit( |
| 4163 |
declRefExpr(to(varDecl(hasName("hs3")))))), |
| 4164 |
hasOperands(ignoringImplicit( |
| 4165 |
declRefExpr(to(varDecl(hasName("hs3"))))), |
| 4166 |
ignoringImplicit(declRefExpr( |
| 4167 |
to(varDecl(hasName("hs4")))))))), |
| 4168 |
true, {"-std=c++20"})); |
| 4169 |
EXPECT_TRUE(matchesConditionally( |
| 4170 |
Code, |
| 4171 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4172 |
cxxRewrittenBinaryOperator( |
| 4173 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 4174 |
isComparisonOperator(), |
| 4175 |
hasLHS(declRefExpr(to(varDecl(hasName("hs3"))))), |
| 4176 |
hasRHS(declRefExpr(to(varDecl(hasName("hs4"))))), |
| 4177 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("hs3"))))), |
| 4178 |
hasOperands(declRefExpr(to(varDecl(hasName("hs3")))), |
| 4179 |
declRefExpr(to(varDecl(hasName("hs4"))))))), |
| 4180 |
true, {"-std=c++20"})); |
| 4181 |
} |
| 4182 |
{ |
| 4183 |
EXPECT_TRUE(matchesConditionally( |
| 4184 |
Code, |
| 4185 |
traverse(TK_AsIs, |
| 4186 |
cxxRewrittenBinaryOperator( |
| 4187 |
hasOperatorName("<"), hasAnyOperatorName("<", "!="), |
| 4188 |
isComparisonOperator(), |
| 4189 |
hasLHS(ignoringImplicit( |
| 4190 |
declRefExpr(to(varDecl(hasName("hs5")))))), |
| 4191 |
hasRHS(ignoringImplicit( |
| 4192 |
declRefExpr(to(varDecl(hasName("hs6")))))), |
| 4193 |
hasEitherOperand(ignoringImplicit( |
| 4194 |
declRefExpr(to(varDecl(hasName("hs5")))))), |
| 4195 |
hasOperands(ignoringImplicit( |
| 4196 |
declRefExpr(to(varDecl(hasName("hs5"))))), |
| 4197 |
ignoringImplicit(declRefExpr( |
| 4198 |
to(varDecl(hasName("hs6")))))))), |
| 4199 |
true, {"-std=c++20"})); |
| 4200 |
EXPECT_TRUE(matchesConditionally( |
| 4201 |
Code, |
| 4202 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4203 |
cxxRewrittenBinaryOperator( |
| 4204 |
hasOperatorName("<"), hasAnyOperatorName("<", "!="), |
| 4205 |
isComparisonOperator(), |
| 4206 |
hasLHS(declRefExpr(to(varDecl(hasName("hs5"))))), |
| 4207 |
hasRHS(declRefExpr(to(varDecl(hasName("hs6"))))), |
| 4208 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("hs5"))))), |
| 4209 |
hasOperands(declRefExpr(to(varDecl(hasName("hs5")))), |
| 4210 |
declRefExpr(to(varDecl(hasName("hs6"))))))), |
| 4211 |
true, {"-std=c++20"})); |
| 4212 |
} |
| 4213 |
{ |
| 4214 |
EXPECT_TRUE(matchesConditionally( |
| 4215 |
Code, |
| 4216 |
traverse(TK_AsIs, |
| 4217 |
cxxRewrittenBinaryOperator( |
| 4218 |
hasOperatorName(">"), hasAnyOperatorName("<", ">"), |
| 4219 |
isComparisonOperator(), |
| 4220 |
hasLHS(ignoringImplicit( |
| 4221 |
declRefExpr(to(varDecl(hasName("hs7")))))), |
| 4222 |
hasRHS(ignoringImplicit( |
| 4223 |
declRefExpr(to(varDecl(hasName("hs8")))))), |
| 4224 |
hasEitherOperand(ignoringImplicit( |
| 4225 |
declRefExpr(to(varDecl(hasName("hs7")))))), |
| 4226 |
hasOperands(ignoringImplicit( |
| 4227 |
declRefExpr(to(varDecl(hasName("hs7"))))), |
| 4228 |
ignoringImplicit(declRefExpr( |
| 4229 |
to(varDecl(hasName("hs8")))))))), |
| 4230 |
true, {"-std=c++20"})); |
| 4231 |
EXPECT_TRUE(matchesConditionally( |
| 4232 |
Code, |
| 4233 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4234 |
cxxRewrittenBinaryOperator( |
| 4235 |
hasOperatorName(">"), hasAnyOperatorName("<", ">"), |
| 4236 |
isComparisonOperator(), |
| 4237 |
hasLHS(declRefExpr(to(varDecl(hasName("hs7"))))), |
| 4238 |
hasRHS(declRefExpr(to(varDecl(hasName("hs8"))))), |
| 4239 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("hs7"))))), |
| 4240 |
hasOperands(declRefExpr(to(varDecl(hasName("hs7")))), |
| 4241 |
declRefExpr(to(varDecl(hasName("hs8"))))))), |
| 4242 |
true, {"-std=c++20"})); |
| 4243 |
} |
| 4244 |
{ |
| 4245 |
EXPECT_TRUE(matchesConditionally( |
| 4246 |
Code, |
| 4247 |
traverse(TK_AsIs, |
| 4248 |
cxxRewrittenBinaryOperator( |
| 4249 |
hasOperatorName("<="), hasAnyOperatorName("<", "<="), |
| 4250 |
isComparisonOperator(), |
| 4251 |
hasLHS(ignoringImplicit( |
| 4252 |
declRefExpr(to(varDecl(hasName("hs9")))))), |
| 4253 |
hasRHS(ignoringImplicit( |
| 4254 |
declRefExpr(to(varDecl(hasName("hs10")))))), |
| 4255 |
hasEitherOperand(ignoringImplicit( |
| 4256 |
declRefExpr(to(varDecl(hasName("hs9")))))), |
| 4257 |
hasOperands(ignoringImplicit( |
| 4258 |
declRefExpr(to(varDecl(hasName("hs9"))))), |
| 4259 |
ignoringImplicit(declRefExpr( |
| 4260 |
to(varDecl(hasName("hs10")))))))), |
| 4261 |
true, {"-std=c++20"})); |
| 4262 |
EXPECT_TRUE(matchesConditionally( |
| 4263 |
Code, |
| 4264 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4265 |
cxxRewrittenBinaryOperator( |
| 4266 |
hasOperatorName("<="), hasAnyOperatorName("<", "<="), |
| 4267 |
isComparisonOperator(), |
| 4268 |
hasLHS(declRefExpr(to(varDecl(hasName("hs9"))))), |
| 4269 |
hasRHS(declRefExpr(to(varDecl(hasName("hs10"))))), |
| 4270 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("hs9"))))), |
| 4271 |
hasOperands(declRefExpr(to(varDecl(hasName("hs9")))), |
| 4272 |
declRefExpr(to(varDecl(hasName("hs10"))))))), |
| 4273 |
true, {"-std=c++20"})); |
| 4274 |
} |
| 4275 |
{ |
| 4276 |
EXPECT_TRUE(matchesConditionally( |
| 4277 |
Code, |
| 4278 |
traverse(TK_AsIs, |
| 4279 |
cxxRewrittenBinaryOperator( |
| 4280 |
hasOperatorName(">="), hasAnyOperatorName("<", ">="), |
| 4281 |
isComparisonOperator(), |
| 4282 |
hasLHS(ignoringImplicit( |
| 4283 |
declRefExpr(to(varDecl(hasName("hs11")))))), |
| 4284 |
hasRHS(ignoringImplicit( |
| 4285 |
declRefExpr(to(varDecl(hasName("hs12")))))), |
| 4286 |
hasEitherOperand(ignoringImplicit( |
| 4287 |
declRefExpr(to(varDecl(hasName("hs11")))))), |
| 4288 |
hasOperands(ignoringImplicit( |
| 4289 |
declRefExpr(to(varDecl(hasName("hs11"))))), |
| 4290 |
ignoringImplicit(declRefExpr( |
| 4291 |
to(varDecl(hasName("hs12")))))))), |
| 4292 |
true, {"-std=c++20"})); |
| 4293 |
EXPECT_TRUE(matchesConditionally( |
| 4294 |
Code, |
| 4295 |
traverse( |
| 4296 |
TK_IgnoreUnlessSpelledInSource, |
| 4297 |
cxxRewrittenBinaryOperator( |
| 4298 |
hasOperatorName(">="), hasAnyOperatorName("<", ">="), |
| 4299 |
isComparisonOperator(), |
| 4300 |
hasLHS(declRefExpr(to(varDecl(hasName("hs11"))))), |
| 4301 |
hasRHS(declRefExpr(to(varDecl(hasName("hs12"))))), |
| 4302 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("hs11"))))), |
| 4303 |
hasOperands(declRefExpr(to(varDecl(hasName("hs11")))), |
| 4304 |
declRefExpr(to(varDecl(hasName("hs12"))))))), |
| 4305 |
true, {"-std=c++20"})); |
| 4306 |
} |
| 4307 |
|
| 4308 |
Code = R"cpp( |
| 4309 |
struct S {}; |
| 4310 |
|
| 4311 |
struct HasOpEq |
| 4312 |
{ |
| 4313 |
bool operator==(const S& other) const |
| 4314 |
{ |
| 4315 |
return true; |
| 4316 |
} |
| 4317 |
}; |
| 4318 |
|
| 4319 |
struct HasOpEqMem { |
| 4320 |
bool operator==(const HasOpEqMem&) const { return true; } |
| 4321 |
}; |
| 4322 |
|
| 4323 |
struct HasOpEqFree { |
| 4324 |
}; |
| 4325 |
bool operator==(const HasOpEqFree&, const HasOpEqFree&) { return true; } |
| 4326 |
|
| 4327 |
void binop() |
| 4328 |
{ |
| 4329 |
{ |
| 4330 |
HasOpEq s1; |
| 4331 |
S s2; |
| 4332 |
if (s1 != s2) |
| 4333 |
return; |
| 4334 |
} |
| 4335 |
|
| 4336 |
{ |
| 4337 |
int i1; |
| 4338 |
int i2; |
| 4339 |
if (i1 != i2) |
| 4340 |
return; |
| 4341 |
} |
| 4342 |
|
| 4343 |
{ |
| 4344 |
HasOpEqMem M1; |
| 4345 |
HasOpEqMem M2; |
| 4346 |
if (M1 == M2) |
| 4347 |
return; |
| 4348 |
} |
| 4349 |
|
| 4350 |
{ |
| 4351 |
HasOpEqFree F1; |
| 4352 |
HasOpEqFree F2; |
| 4353 |
if (F1 == F2) |
| 4354 |
return; |
| 4355 |
} |
| 4356 |
} |
| 4357 |
)cpp"; |
| 4358 |
{ |
| 4359 |
EXPECT_TRUE(matchesConditionally( |
| 4360 |
Code, |
| 4361 |
traverse(TK_AsIs, |
| 4362 |
binaryOperation( |
| 4363 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 4364 |
isComparisonOperator(), |
| 4365 |
hasLHS(ignoringImplicit( |
| 4366 |
declRefExpr(to(varDecl(hasName("s1")))))), |
| 4367 |
hasRHS(ignoringImplicit( |
| 4368 |
declRefExpr(to(varDecl(hasName("s2")))))), |
| 4369 |
hasEitherOperand(ignoringImplicit( |
| 4370 |
declRefExpr(to(varDecl(hasName("s2")))))), |
| 4371 |
hasOperands(ignoringImplicit( |
| 4372 |
declRefExpr(to(varDecl(hasName("s1"))))), |
| 4373 |
ignoringImplicit(declRefExpr( |
| 4374 |
to(varDecl(hasName("s2")))))))), |
| 4375 |
true, {"-std=c++20"})); |
| 4376 |
EXPECT_TRUE(matchesConditionally( |
| 4377 |
Code, |
| 4378 |
traverse(TK_AsIs, binaryOperation(hasOperatorName("!="), |
| 4379 |
hasLHS(ignoringImplicit(declRefExpr( |
| 4380 |
to(varDecl(hasName("i1")))))), |
| 4381 |
hasRHS(ignoringImplicit(declRefExpr( |
| 4382 |
to(varDecl(hasName("i2")))))))), |
| 4383 |
true, {"-std=c++20"})); |
| 4384 |
EXPECT_TRUE(matchesConditionally( |
| 4385 |
Code, |
| 4386 |
traverse(TK_AsIs, binaryOperation(hasOperatorName("=="), |
| 4387 |
hasLHS(ignoringImplicit(declRefExpr( |
| 4388 |
to(varDecl(hasName("M1")))))), |
| 4389 |
hasRHS(ignoringImplicit(declRefExpr( |
| 4390 |
to(varDecl(hasName("M2")))))))), |
| 4391 |
true, {"-std=c++20"})); |
| 4392 |
EXPECT_TRUE(matchesConditionally( |
| 4393 |
Code, |
| 4394 |
traverse(TK_AsIs, binaryOperation(hasOperatorName("=="), |
| 4395 |
hasLHS(ignoringImplicit(declRefExpr( |
| 4396 |
to(varDecl(hasName("F1")))))), |
| 4397 |
hasRHS(ignoringImplicit(declRefExpr( |
| 4398 |
to(varDecl(hasName("F2")))))))), |
| 4399 |
true, {"-std=c++20"})); |
| 4400 |
EXPECT_TRUE(matchesConditionally( |
| 4401 |
Code, |
| 4402 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4403 |
binaryOperation( |
| 4404 |
hasOperatorName("!="), hasAnyOperatorName("<", "!="), |
| 4405 |
isComparisonOperator(), |
| 4406 |
hasLHS(declRefExpr(to(varDecl(hasName("s1"))))), |
| 4407 |
hasRHS(declRefExpr(to(varDecl(hasName("s2"))))), |
| 4408 |
hasEitherOperand(declRefExpr(to(varDecl(hasName("s2"))))), |
| 4409 |
hasOperands(declRefExpr(to(varDecl(hasName("s1")))), |
| 4410 |
declRefExpr(to(varDecl(hasName("s2"))))))), |
| 4411 |
true, {"-std=c++20"})); |
| 4412 |
EXPECT_TRUE(matchesConditionally( |
| 4413 |
Code, |
| 4414 |
traverse( |
| 4415 |
TK_IgnoreUnlessSpelledInSource, |
| 4416 |
binaryOperation(hasOperatorName("!="), |
| 4417 |
hasLHS(declRefExpr(to(varDecl(hasName("i1"))))), |
| 4418 |
hasRHS(declRefExpr(to(varDecl(hasName("i2"))))))), |
| 4419 |
true, {"-std=c++20"})); |
| 4420 |
EXPECT_TRUE(matchesConditionally( |
| 4421 |
Code, |
| 4422 |
traverse( |
| 4423 |
TK_IgnoreUnlessSpelledInSource, |
| 4424 |
binaryOperation(hasOperatorName("=="), |
| 4425 |
hasLHS(declRefExpr(to(varDecl(hasName("M1"))))), |
| 4426 |
hasRHS(declRefExpr(to(varDecl(hasName("M2"))))))), |
| 4427 |
true, {"-std=c++20"})); |
| 4428 |
EXPECT_TRUE(matchesConditionally( |
| 4429 |
Code, |
| 4430 |
traverse( |
| 4431 |
TK_IgnoreUnlessSpelledInSource, |
| 4432 |
binaryOperation(hasOperatorName("=="), |
| 4433 |
hasLHS(declRefExpr(to(varDecl(hasName("F1"))))), |
| 4434 |
hasRHS(declRefExpr(to(varDecl(hasName("F2"))))))), |
| 4435 |
true, {"-std=c++20"})); |
| 4436 |
} |
| 4437 |
} |
| 4438 |
|
| 4439 |
TEST(IgnoringImpCasts, PathologicalLambda) { |
| 4440 |
|
| 4441 |
// Test that deeply nested lambdas are not a performance penalty |
| 4442 |
StringRef Code = R"cpp( |
| 4443 |
void f() { |
| 4444 |
[] { |
| 4445 |
[] { |
| 4446 |
[] { |
| 4447 |
[] { |
| 4448 |
[] { |
| 4449 |
[] { |
| 4450 |
[] { |
| 4451 |
[] { |
| 4452 |
[] { |
| 4453 |
[] { |
| 4454 |
[] { |
| 4455 |
[] { |
| 4456 |
[] { |
| 4457 |
[] { |
| 4458 |
[] { |
| 4459 |
[] { |
| 4460 |
[] { |
| 4461 |
[] { |
| 4462 |
[] { |
| 4463 |
[] { |
| 4464 |
[] { |
| 4465 |
[] { |
| 4466 |
[] { |
| 4467 |
[] { |
| 4468 |
[] { |
| 4469 |
[] { |
| 4470 |
[] { |
| 4471 |
[] { |
| 4472 |
[] { |
| 4473 |
int i = 42; |
| 4474 |
(void)i; |
| 4475 |
}(); |
| 4476 |
}(); |
| 4477 |
}(); |
| 4478 |
}(); |
| 4479 |
}(); |
| 4480 |
}(); |
| 4481 |
}(); |
| 4482 |
}(); |
| 4483 |
}(); |
| 4484 |
}(); |
| 4485 |
}(); |
| 4486 |
}(); |
| 4487 |
}(); |
| 4488 |
}(); |
| 4489 |
}(); |
| 4490 |
}(); |
| 4491 |
}(); |
| 4492 |
}(); |
| 4493 |
}(); |
| 4494 |
}(); |
| 4495 |
}(); |
| 4496 |
}(); |
| 4497 |
}(); |
| 4498 |
}(); |
| 4499 |
}(); |
| 4500 |
}(); |
| 4501 |
}(); |
| 4502 |
}(); |
| 4503 |
}(); |
| 4504 |
} |
| 4505 |
)cpp"; |
| 4506 |
|
| 4507 |
EXPECT_TRUE(matches(Code, integerLiteral(equals(42)))); |
| 4508 |
EXPECT_TRUE(matches(Code, functionDecl(hasDescendant(integerLiteral(equals(42)))))); |
| 4509 |
} |
| 4510 |
|
| 4511 |
TEST(IgnoringImpCasts, MatchesImpCasts) { |
| 4512 |
// This test checks that ignoringImpCasts matches when implicit casts are |
| 4513 |
// present and its inner matcher alone does not match. |
| 4514 |
// Note that this test creates an implicit const cast. |
| 4515 |
EXPECT_TRUE(matches("int x = 0; const int y = x;", |
| 4516 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4517 |
declRefExpr(to(varDecl(hasName("x"))))))))); |
| 4518 |
// This test creates an implict cast from int to char. |
| 4519 |
EXPECT_TRUE(matches("char x = 0;", |
| 4520 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4521 |
integerLiteral(equals(0))))))); |
| 4522 |
} |
| 4523 |
|
| 4524 |
TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) { |
| 4525 |
// These tests verify that ignoringImpCasts does not match if the inner |
| 4526 |
// matcher does not match. |
| 4527 |
// Note that the first test creates an implicit const cast. |
| 4528 |
EXPECT_TRUE(notMatches("int x; const int y = x;", |
| 4529 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4530 |
unless(anything())))))); |
| 4531 |
EXPECT_TRUE(notMatches("int x; int y = x;", |
| 4532 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4533 |
unless(anything())))))); |
| 4534 |
|
| 4535 |
// These tests verify that ignoringImplictCasts does not look through explicit |
| 4536 |
// casts or parentheses. |
| 4537 |
EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
| 4538 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4539 |
integerLiteral()))))); |
| 4540 |
EXPECT_TRUE(notMatches( |
| 4541 |
"int i = (0);", |
| 4542 |
traverse(TK_AsIs, |
| 4543 |
varDecl(hasInitializer(ignoringImpCasts(integerLiteral())))))); |
| 4544 |
EXPECT_TRUE(notMatches("float i = (float)0;", |
| 4545 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4546 |
integerLiteral()))))); |
| 4547 |
EXPECT_TRUE(notMatches("float i = float(0);", |
| 4548 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4549 |
integerLiteral()))))); |
| 4550 |
} |
| 4551 |
|
| 4552 |
TEST(IgnoringImpCasts, MatchesWithoutImpCasts) { |
| 4553 |
// This test verifies that expressions that do not have implicit casts |
| 4554 |
// still match the inner matcher. |
| 4555 |
EXPECT_TRUE(matches("int x = 0; int &y = x;", |
| 4556 |
varDecl(hasInitializer(ignoringImpCasts( |
| 4557 |
declRefExpr(to(varDecl(hasName("x"))))))))); |
| 4558 |
} |
| 4559 |
|
| 4560 |
TEST(IgnoringParenCasts, MatchesParenCasts) { |
| 4561 |
// This test checks that ignoringParenCasts matches when parentheses and/or |
| 4562 |
// casts are present and its inner matcher alone does not match. |
| 4563 |
EXPECT_TRUE(matches("int x = (0);", |
| 4564 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4565 |
integerLiteral(equals(0))))))); |
| 4566 |
EXPECT_TRUE(matches("int x = (((((0)))));", |
| 4567 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4568 |
integerLiteral(equals(0))))))); |
| 4569 |
|
| 4570 |
// This test creates an implict cast from int to char in addition to the |
| 4571 |
// parentheses. |
| 4572 |
EXPECT_TRUE(matches("char x = (0);", |
| 4573 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4574 |
integerLiteral(equals(0))))))); |
| 4575 |
|
| 4576 |
EXPECT_TRUE(matches("char x = (char)0;", |
| 4577 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4578 |
integerLiteral(equals(0))))))); |
| 4579 |
EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
| 4580 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4581 |
integerLiteral(equals(0))))))); |
| 4582 |
} |
| 4583 |
|
| 4584 |
TEST(IgnoringParenCasts, MatchesWithoutParenCasts) { |
| 4585 |
// This test verifies that expressions that do not have any casts still match. |
| 4586 |
EXPECT_TRUE(matches("int x = 0;", |
| 4587 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4588 |
integerLiteral(equals(0))))))); |
| 4589 |
} |
| 4590 |
|
| 4591 |
TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) { |
| 4592 |
// These tests verify that ignoringImpCasts does not match if the inner |
| 4593 |
// matcher does not match. |
| 4594 |
EXPECT_TRUE(notMatches("int x = ((0));", |
| 4595 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4596 |
unless(anything())))))); |
| 4597 |
|
| 4598 |
// This test creates an implicit cast from int to char in addition to the |
| 4599 |
// parentheses. |
| 4600 |
EXPECT_TRUE(notMatches("char x = ((0));", |
| 4601 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4602 |
unless(anything())))))); |
| 4603 |
|
| 4604 |
EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));", |
| 4605 |
varDecl(hasInitializer(ignoringParenCasts( |
| 4606 |
unless(anything())))))); |
| 4607 |
} |
| 4608 |
|
| 4609 |
TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) { |
| 4610 |
// This test checks that ignoringParenAndImpCasts matches when |
| 4611 |
// parentheses and/or implicit casts are present and its inner matcher alone |
| 4612 |
// does not match. |
| 4613 |
// Note that this test creates an implicit const cast. |
| 4614 |
EXPECT_TRUE(matches("int x = 0; const int y = x;", |
| 4615 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4616 |
declRefExpr(to(varDecl(hasName("x"))))))))); |
| 4617 |
// This test creates an implicit cast from int to char. |
| 4618 |
EXPECT_TRUE(matches("const char x = (0);", |
| 4619 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4620 |
integerLiteral(equals(0))))))); |
| 4621 |
} |
| 4622 |
|
| 4623 |
TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) { |
| 4624 |
// This test verifies that expressions that do not have parentheses or |
| 4625 |
// implicit casts still match. |
| 4626 |
EXPECT_TRUE(matches("int x = 0; int &y = x;", |
| 4627 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4628 |
declRefExpr(to(varDecl(hasName("x"))))))))); |
| 4629 |
EXPECT_TRUE(matches("int x = 0;", |
| 4630 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4631 |
integerLiteral(equals(0))))))); |
| 4632 |
} |
| 4633 |
|
| 4634 |
TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) { |
| 4635 |
// These tests verify that ignoringParenImpCasts does not match if |
| 4636 |
// the inner matcher does not match. |
| 4637 |
// This test creates an implicit cast. |
| 4638 |
EXPECT_TRUE(notMatches("char c = ((3));", |
| 4639 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4640 |
unless(anything())))))); |
| 4641 |
// These tests verify that ignoringParenAndImplictCasts does not look |
| 4642 |
// through explicit casts. |
| 4643 |
EXPECT_TRUE(notMatches("float y = (float(0));", |
| 4644 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4645 |
integerLiteral()))))); |
| 4646 |
EXPECT_TRUE(notMatches("float y = (float)0;", |
| 4647 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4648 |
integerLiteral()))))); |
| 4649 |
EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
| 4650 |
varDecl(hasInitializer(ignoringParenImpCasts( |
| 4651 |
integerLiteral()))))); |
| 4652 |
} |
| 4653 |
|
| 4654 |
TEST(HasSourceExpression, MatchesImplicitCasts) { |
| 4655 |
EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 4656 |
"void r() {string a_string; URL url = a_string; }", |
| 4657 |
traverse(TK_AsIs, implicitCastExpr(hasSourceExpression( |
| 4658 |
cxxConstructExpr()))))); |
| 4659 |
} |
| 4660 |
|
| 4661 |
TEST(HasSourceExpression, MatchesExplicitCasts) { |
| 4662 |
EXPECT_TRUE( |
| 4663 |
matches("float x = static_cast<float>(42);", |
| 4664 |
traverse(TK_AsIs, explicitCastExpr(hasSourceExpression( |
| 4665 |
hasDescendant(expr(integerLiteral()))))))); |
| 4666 |
} |
| 4667 |
|
| 4668 |
TEST(UsingDeclaration, MatchesSpecificTarget) { |
| 4669 |
EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;", |
| 4670 |
usingDecl(hasAnyUsingShadowDecl( |
| 4671 |
hasTargetDecl(functionDecl()))))); |
| 4672 |
EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;", |
| 4673 |
usingDecl(hasAnyUsingShadowDecl( |
| 4674 |
hasTargetDecl(functionDecl()))))); |
| 4675 |
} |
| 4676 |
|
| 4677 |
TEST(UsingDeclaration, ThroughUsingDeclaration) { |
| 4678 |
EXPECT_TRUE(matches( |
| 4679 |
"namespace a { void f(); } using a::f; void g() { f(); }", |
| 4680 |
declRefExpr(throughUsingDecl(anything())))); |
| 4681 |
EXPECT_TRUE(notMatches( |
| 4682 |
"namespace a { void f(); } using a::f; void g() { a::f(); }", |
| 4683 |
declRefExpr(throughUsingDecl(anything())))); |
| 4684 |
} |
| 4685 |
|
| 4686 |
TEST(SingleDecl, IsSingleDecl) { |
| 4687 |
StatementMatcher SingleDeclStmt = |
| 4688 |
declStmt(hasSingleDecl(varDecl(hasInitializer(anything())))); |
| 4689 |
EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt)); |
| 4690 |
EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt)); |
| 4691 |
EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 4692 |
SingleDeclStmt)); |
| 4693 |
} |
| 4694 |
|
| 4695 |
TEST(DeclStmt, ContainsDeclaration) { |
| 4696 |
DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything())); |
| 4697 |
|
| 4698 |
EXPECT_TRUE(matches("void f() {int a = 4;}", |
| 4699 |
declStmt(containsDeclaration(0, MatchesInit)))); |
| 4700 |
EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}", |
| 4701 |
declStmt(containsDeclaration(0, MatchesInit), |
| 4702 |
containsDeclaration(1, MatchesInit)))); |
| 4703 |
unsigned WrongIndex = 42; |
| 4704 |
EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 4705 |
declStmt(containsDeclaration(WrongIndex, |
| 4706 |
MatchesInit)))); |
| 4707 |
} |
| 4708 |
|
| 4709 |
TEST(SwitchCase, MatchesEachCase) { |
| 4710 |
EXPECT_TRUE(notMatches("void x() { switch(42); }", |
| 4711 |
switchStmt(forEachSwitchCase(caseStmt())))); |
| 4712 |
EXPECT_TRUE(matches("void x() { switch(42) case 42:; }", |
| 4713 |
switchStmt(forEachSwitchCase(caseStmt())))); |
| 4714 |
EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", |
| 4715 |
switchStmt(forEachSwitchCase(caseStmt())))); |
| 4716 |
EXPECT_TRUE(notMatches( |
| 4717 |
"void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }", |
| 4718 |
ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt())))))); |
| 4719 |
EXPECT_TRUE(matches( |
| 4720 |
"void x() { switch(42) { case 1+1: case 4:; } }", |
| 4721 |
traverse(TK_AsIs, switchStmt(forEachSwitchCase(caseStmt(hasCaseConstant( |
| 4722 |
constantExpr(has(integerLiteral()))))))))); |
| 4723 |
EXPECT_TRUE(notMatches( |
| 4724 |
"void x() { switch(42) { case 1+1: case 2+2:; } }", |
| 4725 |
traverse(TK_AsIs, switchStmt(forEachSwitchCase(caseStmt(hasCaseConstant( |
| 4726 |
constantExpr(has(integerLiteral()))))))))); |
| 4727 |
EXPECT_TRUE(notMatches( |
| 4728 |
"void x() { switch(42) { case 1 ... 2:; } }", |
| 4729 |
traverse(TK_AsIs, switchStmt(forEachSwitchCase(caseStmt(hasCaseConstant( |
| 4730 |
constantExpr(has(integerLiteral()))))))))); |
| 4731 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4732 |
"void x() { switch (42) { case 1: case 2: case 3: default:; } }", |
| 4733 |
switchStmt(forEachSwitchCase(caseStmt().bind("x"))), |
| 4734 |
std::make_unique<VerifyIdIsBoundTo<CaseStmt>>("x", 3))); |
| 4735 |
} |
| 4736 |
|
| 4737 |
TEST(Declaration, HasExplicitSpecifier) { |
| 4738 |
|
| 4739 |
EXPECT_TRUE(notMatches("void f();", |
| 4740 |
functionDecl(hasExplicitSpecifier(constantExpr())), |
| 4741 |
langCxx20OrLater())); |
| 4742 |
EXPECT_TRUE( |
| 4743 |
notMatches("template<bool b> struct S { explicit operator int(); };", |
| 4744 |
cxxConversionDecl( |
| 4745 |
hasExplicitSpecifier(constantExpr(has(cxxBoolLiteral())))), |
| 4746 |
langCxx20OrLater())); |
| 4747 |
EXPECT_TRUE( |
| 4748 |
notMatches("template<bool b> struct S { explicit(b) operator int(); };", |
| 4749 |
cxxConversionDecl( |
| 4750 |
hasExplicitSpecifier(constantExpr(has(cxxBoolLiteral())))), |
| 4751 |
langCxx20OrLater())); |
| 4752 |
EXPECT_TRUE( |
| 4753 |
matches("struct S { explicit(true) operator int(); };", |
| 4754 |
traverse(TK_AsIs, cxxConversionDecl(hasExplicitSpecifier( |
| 4755 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4756 |
langCxx20OrLater())); |
| 4757 |
EXPECT_TRUE( |
| 4758 |
matches("struct S { explicit(false) operator int(); };", |
| 4759 |
traverse(TK_AsIs, cxxConversionDecl(hasExplicitSpecifier( |
| 4760 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4761 |
langCxx20OrLater())); |
| 4762 |
EXPECT_TRUE( |
| 4763 |
notMatches("template<bool b> struct S { explicit(b) S(int); };", |
| 4764 |
traverse(TK_AsIs, cxxConstructorDecl(hasExplicitSpecifier( |
| 4765 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4766 |
langCxx20OrLater())); |
| 4767 |
EXPECT_TRUE( |
| 4768 |
matches("struct S { explicit(true) S(int); };", |
| 4769 |
traverse(TK_AsIs, cxxConstructorDecl(hasExplicitSpecifier( |
| 4770 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4771 |
langCxx20OrLater())); |
| 4772 |
EXPECT_TRUE( |
| 4773 |
matches("struct S { explicit(false) S(int); };", |
| 4774 |
traverse(TK_AsIs, cxxConstructorDecl(hasExplicitSpecifier( |
| 4775 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4776 |
langCxx20OrLater())); |
| 4777 |
EXPECT_TRUE( |
| 4778 |
notMatches("template<typename T> struct S { S(int); };" |
| 4779 |
"template<bool b = true> explicit(b) S(int) -> S<int>;", |
| 4780 |
traverse(TK_AsIs, cxxDeductionGuideDecl(hasExplicitSpecifier( |
| 4781 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4782 |
langCxx20OrLater())); |
| 4783 |
EXPECT_TRUE( |
| 4784 |
matches("template<typename T> struct S { S(int); };" |
| 4785 |
"explicit(true) S(int) -> S<int>;", |
| 4786 |
traverse(TK_AsIs, cxxDeductionGuideDecl(hasExplicitSpecifier( |
| 4787 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4788 |
langCxx20OrLater())); |
| 4789 |
EXPECT_TRUE( |
| 4790 |
matches("template<typename T> struct S { S(int); };" |
| 4791 |
"explicit(false) S(int) -> S<int>;", |
| 4792 |
traverse(TK_AsIs, cxxDeductionGuideDecl(hasExplicitSpecifier( |
| 4793 |
constantExpr(has(cxxBoolLiteral()))))), |
| 4794 |
langCxx20OrLater())); |
| 4795 |
} |
| 4796 |
|
| 4797 |
TEST(ForEachConstructorInitializer, MatchesInitializers) { |
| 4798 |
EXPECT_TRUE(matches( |
| 4799 |
"struct X { X() : i(42), j(42) {} int i, j; };", |
| 4800 |
cxxConstructorDecl(forEachConstructorInitializer(cxxCtorInitializer())))); |
| 4801 |
} |
| 4802 |
|
| 4803 |
TEST(ForEachLambdaCapture, MatchesCaptures) { |
| 4804 |
EXPECT_TRUE(matches( |
| 4805 |
"int main() { int x, y; auto f = [x, y]() { return x + y; }; }", |
| 4806 |
lambdaExpr(forEachLambdaCapture(lambdaCapture())), langCxx11OrLater())); |
| 4807 |
auto matcher = lambdaExpr(forEachLambdaCapture( |
| 4808 |
lambdaCapture(capturesVar(varDecl(hasType(isInteger())))).bind("LC"))); |
| 4809 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4810 |
"int main() { int x, y; float z; auto f = [=]() { return x + y + z; }; }", |
| 4811 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 2))); |
| 4812 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4813 |
"int main() { int x, y; float z; auto f = [x, y, z]() { return x + y + " |
| 4814 |
"z; }; }", |
| 4815 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 2))); |
| 4816 |
} |
| 4817 |
|
| 4818 |
TEST(ForEachLambdaCapture, IgnoreUnlessSpelledInSource) { |
| 4819 |
auto matcher = |
| 4820 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4821 |
lambdaExpr(forEachLambdaCapture( |
| 4822 |
lambdaCapture(capturesVar(varDecl(hasType(isInteger())))) |
| 4823 |
.bind("LC")))); |
| 4824 |
EXPECT_TRUE( |
| 4825 |
notMatches("int main() { int x, y; auto f = [=]() { return x + y; }; }", |
| 4826 |
matcher, langCxx11OrLater())); |
| 4827 |
EXPECT_TRUE( |
| 4828 |
notMatches("int main() { int x, y; auto f = [&]() { return x + y; }; }", |
| 4829 |
matcher, langCxx11OrLater())); |
| 4830 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4831 |
R"cc( |
| 4832 |
int main() { |
| 4833 |
int x, y; |
| 4834 |
float z; |
| 4835 |
auto f = [=, &y]() { return x + y + z; }; |
| 4836 |
} |
| 4837 |
)cc", |
| 4838 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 1))); |
| 4839 |
} |
| 4840 |
|
| 4841 |
TEST(ForEachLambdaCapture, MatchImplicitCapturesOnly) { |
| 4842 |
auto matcher = |
| 4843 |
lambdaExpr(forEachLambdaCapture(lambdaCapture(isImplicit()).bind("LC"))); |
| 4844 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4845 |
"int main() { int x, y, z; auto f = [=, &z]() { return x + y + z; }; }", |
| 4846 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 2))); |
| 4847 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4848 |
"int main() { int x, y, z; auto f = [&, z]() { return x + y + z; }; }", |
| 4849 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 2))); |
| 4850 |
} |
| 4851 |
|
| 4852 |
TEST(ForEachLambdaCapture, MatchExplicitCapturesOnly) { |
| 4853 |
auto matcher = lambdaExpr( |
| 4854 |
forEachLambdaCapture(lambdaCapture(unless(isImplicit())).bind("LC"))); |
| 4855 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4856 |
"int main() { int x, y, z; auto f = [=, &z]() { return x + y + z; }; }", |
| 4857 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 1))); |
| 4858 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4859 |
"int main() { int x, y, z; auto f = [&, z]() { return x + y + z; }; }", |
| 4860 |
matcher, std::make_unique<VerifyIdIsBoundTo<LambdaCapture>>("LC", 1))); |
| 4861 |
} |
| 4862 |
|
| 4863 |
TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 4864 |
EXPECT_TRUE(notMatches( |
| 4865 |
"void x() { if(true) {} }", |
| 4866 |
ifStmt(hasConditionVariableStatement(declStmt())))); |
| 4867 |
EXPECT_TRUE(notMatches( |
| 4868 |
"void x() { int x; if((x = 42)) {} }", |
| 4869 |
ifStmt(hasConditionVariableStatement(declStmt())))); |
| 4870 |
} |
| 4871 |
|
| 4872 |
TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 4873 |
EXPECT_TRUE(matches( |
| 4874 |
"void x() { if(int* a = 0) {} }", |
| 4875 |
ifStmt(hasConditionVariableStatement(declStmt())))); |
| 4876 |
} |
| 4877 |
|
| 4878 |
TEST(ForEach, BindsOneNode) { |
| 4879 |
EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
| 4880 |
recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))), |
| 4881 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("x", 1))); |
| 4882 |
} |
| 4883 |
|
| 4884 |
TEST(ForEach, BindsMultipleNodes) { |
| 4885 |
EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
| 4886 |
recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))), |
| 4887 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("f", 3))); |
| 4888 |
} |
| 4889 |
|
| 4890 |
TEST(ForEach, BindsRecursiveCombinations) { |
| 4891 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4892 |
"class C { class D { int x; int y; }; class E { int y; int z; }; };", |
| 4893 |
recordDecl(hasName("C"), |
| 4894 |
forEach(recordDecl(forEach(fieldDecl().bind("f"))))), |
| 4895 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("f", 4))); |
| 4896 |
} |
| 4897 |
|
| 4898 |
TEST(ForEach, DoesNotIgnoreImplicit) { |
| 4899 |
StringRef Code = R"cpp( |
| 4900 |
void foo() |
| 4901 |
{ |
| 4902 |
int i = 0; |
| 4903 |
int b = 4; |
| 4904 |
i < b; |
| 4905 |
} |
| 4906 |
)cpp"; |
| 4907 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4908 |
Code, binaryOperator(forEach(declRefExpr().bind("dre"))), |
| 4909 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 0))); |
| 4910 |
|
| 4911 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4912 |
Code, |
| 4913 |
binaryOperator(forEach( |
| 4914 |
implicitCastExpr(hasSourceExpression(declRefExpr().bind("dre"))))), |
| 4915 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 2))); |
| 4916 |
|
| 4917 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4918 |
Code, |
| 4919 |
binaryOperator( |
| 4920 |
forEach(expr(ignoringImplicit(declRefExpr().bind("dre"))))), |
| 4921 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 2))); |
| 4922 |
|
| 4923 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4924 |
Code, |
| 4925 |
traverse(TK_IgnoreUnlessSpelledInSource, |
| 4926 |
binaryOperator(forEach(declRefExpr().bind("dre")))), |
| 4927 |
std::make_unique<VerifyIdIsBoundTo<DeclRefExpr>>("dre", 2))); |
| 4928 |
} |
| 4929 |
|
| 4930 |
TEST(ForEachDescendant, BindsOneNode) { |
| 4931 |
EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
| 4932 |
recordDecl(hasName("C"), |
| 4933 |
forEachDescendant(fieldDecl(hasName("x")).bind("x"))), |
| 4934 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("x", 1))); |
| 4935 |
} |
| 4936 |
|
| 4937 |
TEST(ForEachDescendant, NestedForEachDescendant) { |
| 4938 |
DeclarationMatcher m = recordDecl( |
| 4939 |
isDefinition(), decl().bind("x"), hasName("C")); |
| 4940 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4941 |
"class A { class B { class C {}; }; };", |
| 4942 |
recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))), |
| 4943 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", "C"))); |
| 4944 |
|
| 4945 |
// Check that a partial match of 'm' that binds 'x' in the |
| 4946 |
// first part of anyOf(m, anything()) will not overwrite the |
| 4947 |
// binding created by the earlier binding in the hasDescendant. |
| 4948 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4949 |
"class A { class B { class C {}; }; };", |
| 4950 |
recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))), |
| 4951 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", "C"))); |
| 4952 |
} |
| 4953 |
|
| 4954 |
TEST(ForEachDescendant, BindsMultipleNodes) { |
| 4955 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4956 |
"class C { class D { int x; int y; }; " |
| 4957 |
" class E { class F { int y; int z; }; }; };", |
| 4958 |
recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))), |
| 4959 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("f", 4))); |
| 4960 |
} |
| 4961 |
|
| 4962 |
TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 4963 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4964 |
"class C { class D { " |
| 4965 |
" class E { class F { class G { int y; int z; }; }; }; }; };", |
| 4966 |
recordDecl(hasName("C"), forEachDescendant(recordDecl( |
| 4967 |
forEachDescendant(fieldDecl().bind("f"))))), |
| 4968 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("f", 8))); |
| 4969 |
} |
| 4970 |
|
| 4971 |
TEST(ForEachDescendant, BindsCombinations) { |
| 4972 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4973 |
"void f() { if(true) {} if (true) {} while (true) {} if (true) {} while " |
| 4974 |
"(true) {} }", |
| 4975 |
compoundStmt(forEachDescendant(ifStmt().bind("if")), |
| 4976 |
forEachDescendant(whileStmt().bind("while"))), |
| 4977 |
std::make_unique<VerifyIdIsBoundTo<IfStmt>>("if", 6))); |
| 4978 |
} |
| 4979 |
|
| 4980 |
TEST(ForEachTemplateArgument, OnFunctionDecl) { |
| 4981 |
const std::string Code = R"( |
| 4982 |
template <typename T, typename U> void f(T, U) {} |
| 4983 |
void test() { |
| 4984 |
int I = 1; |
| 4985 |
bool B = false; |
| 4986 |
f(I, B); |
| 4987 |
})"; |
| 4988 |
EXPECT_TRUE(matches( |
| 4989 |
Code, functionDecl(forEachTemplateArgument(refersToType(builtinType()))), |
| 4990 |
langCxx11OrLater())); |
| 4991 |
auto matcher = |
| 4992 |
functionDecl(forEachTemplateArgument( |
| 4993 |
templateArgument(refersToType(builtinType().bind("BT"))) |
| 4994 |
.bind("TA"))) |
| 4995 |
.bind("FN"); |
| 4996 |
|
| 4997 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4998 |
Code, matcher, |
| 4999 |
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("FN", 2))); |
| 5000 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5001 |
Code, matcher, |
| 5002 |
std::make_unique<VerifyIdIsBoundTo<TemplateArgument>>("TA", 2))); |
| 5003 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5004 |
Code, matcher, |
| 5005 |
std::make_unique<VerifyIdIsBoundTo<BuiltinType>>("BT", 2))); |
| 5006 |
} |
| 5007 |
|
| 5008 |
TEST(ForEachTemplateArgument, OnClassTemplateSpecialization) { |
| 5009 |
const std::string Code = R"( |
| 5010 |
template <typename T, unsigned N, unsigned M> |
| 5011 |
struct Matrix {}; |
| 5012 |
|
| 5013 |
static constexpr unsigned R = 2; |
| 5014 |
|
| 5015 |
Matrix<int, R * 2, R * 4> M; |
| 5016 |
)"; |
| 5017 |
EXPECT_TRUE(matches( |
| 5018 |
Code, templateSpecializationType(forEachTemplateArgument(isExpr(expr()))), |
| 5019 |
langCxx11OrLater())); |
| 5020 |
auto matcher = templateSpecializationType( |
| 5021 |
forEachTemplateArgument( |
| 5022 |
templateArgument(isExpr(expr().bind("E"))).bind("TA"))) |
| 5023 |
.bind("TST"); |
| 5024 |
|
| 5025 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5026 |
Code, matcher, |
| 5027 |
std::make_unique<VerifyIdIsBoundTo<TemplateSpecializationType>>("TST", |
| 5028 |
2))); |
| 5029 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5030 |
Code, matcher, |
| 5031 |
std::make_unique<VerifyIdIsBoundTo<TemplateArgument>>("TA", 2))); |
| 5032 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5033 |
Code, matcher, std::make_unique<VerifyIdIsBoundTo<Expr>>("E", 2))); |
| 5034 |
} |
| 5035 |
|
| 5036 |
TEST(Has, DoesNotDeleteBindings) { |
| 5037 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5038 |
"class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())), |
| 5039 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5040 |
} |
| 5041 |
|
| 5042 |
TEST(TemplateArgumentLoc, Matches) { |
| 5043 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5044 |
R"cpp( |
| 5045 |
template <typename A, int B, template <typename> class C> class X {}; |
| 5046 |
class A {}; |
| 5047 |
const int B = 42; |
| 5048 |
template <typename> class C {}; |
| 5049 |
X<A, B, C> x; |
| 5050 |
)cpp", |
| 5051 |
templateArgumentLoc().bind("x"), |
| 5052 |
std::make_unique<VerifyIdIsBoundTo<TemplateArgumentLoc>>("x", 3))); |
| 5053 |
} |
| 5054 |
|
| 5055 |
TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) { |
| 5056 |
// Those matchers cover all the cases where an inner matcher is called |
| 5057 |
// and there is not a 1:1 relationship between the match of the outer |
| 5058 |
// matcher and the match of the inner matcher. |
| 5059 |
// The pattern to look for is: |
| 5060 |
// ... return InnerMatcher.matches(...); ... |
| 5061 |
// In which case no special handling is needed. |
| 5062 |
// |
| 5063 |
// On the other hand, if there are multiple alternative matches |
| 5064 |
// (for example forEach*) or matches might be discarded (for example has*) |
| 5065 |
// the implementation must make sure that the discarded matches do not |
| 5066 |
// affect the bindings. |
| 5067 |
// When new such matchers are added, add a test here that: |
| 5068 |
// - matches a simple node, and binds it as the first thing in the matcher: |
| 5069 |
// recordDecl(decl().bind("x"), hasName("X"))) |
| 5070 |
// - uses the matcher under test afterwards in a way that not the first |
| 5071 |
// alternative is matched; for anyOf, that means the first branch |
| 5072 |
// would need to return false; for hasAncestor, it means that not |
| 5073 |
// the direct parent matches the inner matcher. |
| 5074 |
|
| 5075 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5076 |
"class X { int y; };", |
| 5077 |
recordDecl( |
| 5078 |
recordDecl().bind("x"), hasName("::X"), |
| 5079 |
anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())), |
| 5080 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("x", 1))); |
| 5081 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5082 |
"class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"), |
| 5083 |
anyOf(unless(anything()), anything())), |
| 5084 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("x", 1))); |
| 5085 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5086 |
"template<typename T1, typename T2> class X {}; X<float, int> x;", |
| 5087 |
classTemplateSpecializationDecl( |
| 5088 |
decl().bind("x"), |
| 5089 |
hasAnyTemplateArgument(refersToType(asString("int")))), |
| 5090 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5091 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5092 |
"class X { void f(); void g(); };", |
| 5093 |
cxxRecordDecl(decl().bind("x"), hasMethod(hasName("g"))), |
| 5094 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5095 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5096 |
"class X { X() : a(1), b(2) {} double a; int b; };", |
| 5097 |
recordDecl(decl().bind("x"), |
| 5098 |
has(cxxConstructorDecl( |
| 5099 |
hasAnyConstructorInitializer(forField(hasName("b")))))), |
| 5100 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5101 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5102 |
"void x(int, int) { x(0, 42); }", |
| 5103 |
callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))), |
| 5104 |
std::make_unique<VerifyIdIsBoundTo<Expr>>("x", 1))); |
| 5105 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5106 |
"void x(int, int y) {}", |
| 5107 |
functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))), |
| 5108 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5109 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5110 |
"void x() { return; if (true) {} }", |
| 5111 |
functionDecl(decl().bind("x"), |
| 5112 |
has(compoundStmt(hasAnySubstatement(ifStmt())))), |
| 5113 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5114 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5115 |
"namespace X { void b(int); void b(); }" |
| 5116 |
"using X::b;", |
| 5117 |
usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl( |
| 5118 |
functionDecl(parameterCountIs(1))))), |
| 5119 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5120 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5121 |
"class A{}; class B{}; class C : B, A {};", |
| 5122 |
cxxRecordDecl(decl().bind("x"), isDerivedFrom("::A")), |
| 5123 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5124 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5125 |
"class A{}; typedef A B; typedef A C; typedef A D;" |
| 5126 |
"class E : A {};", |
| 5127 |
cxxRecordDecl(decl().bind("x"), isDerivedFrom("C")), |
| 5128 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5129 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5130 |
"class A { class B { void f() {} }; };", |
| 5131 |
functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 5132 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5133 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5134 |
"template <typename T> struct A { struct B {" |
| 5135 |
" void f() { if(true) {} }" |
| 5136 |
"}; };" |
| 5137 |
"void t() { A<int>::B b; b.f(); }", |
| 5138 |
ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 5139 |
std::make_unique<VerifyIdIsBoundTo<Stmt>>("x", 2))); |
| 5140 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5141 |
"class A {};", |
| 5142 |
recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))), |
| 5143 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5144 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5145 |
"class A { A() : s(), i(42) {} const char *s; int i; };", |
| 5146 |
cxxConstructorDecl(hasName("::A::A"), decl().bind("x"), |
| 5147 |
forEachConstructorInitializer(forField(hasName("i")))), |
| 5148 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("x", 1))); |
| 5149 |
} |
| 5150 |
|
| 5151 |
TEST(ForEachDescendant, BindsCorrectNodes) { |
| 5152 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5153 |
"class C { void f(); int i; };", |
| 5154 |
recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 5155 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("decl", 1))); |
| 5156 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5157 |
"class C { void f() {} int i; };", |
| 5158 |
recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 5159 |
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("decl", 1))); |
| 5160 |
} |
| 5161 |
|
| 5162 |
TEST(FindAll, BindsNodeOnMatch) { |
| 5163 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5164 |
"class A {};", |
| 5165 |
recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))), |
| 5166 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("v", 1))); |
| 5167 |
} |
| 5168 |
|
| 5169 |
TEST(FindAll, BindsDescendantNodeOnMatch) { |
| 5170 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5171 |
"class A { int a; int b; };", |
| 5172 |
recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))), |
| 5173 |
std::make_unique<VerifyIdIsBoundTo<FieldDecl>>("v", 2))); |
| 5174 |
} |
| 5175 |
|
| 5176 |
TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) { |
| 5177 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5178 |
"class A { int a; int b; };", |
| 5179 |
recordDecl(hasName("::A"), |
| 5180 |
findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"), |
| 5181 |
fieldDecl().bind("v"))))), |
| 5182 |
std::make_unique<VerifyIdIsBoundTo<Decl>>("v", 3))); |
| 5183 |
|
| 5184 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5185 |
"class A { class B {}; class C {}; };", |
| 5186 |
recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))), |
| 5187 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("v", 3))); |
| 5188 |
} |
| 5189 |
|
| 5190 |
TEST(HasAncenstor, MatchesDeclarationAncestors) { |
| 5191 |
EXPECT_TRUE(matches( |
| 5192 |
"class A { class B { class C {}; }; };", |
| 5193 |
recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A")))))); |
| 5194 |
} |
| 5195 |
|
| 5196 |
TEST(HasAncenstor, FailsIfNoAncestorMatches) { |
| 5197 |
EXPECT_TRUE(notMatches( |
| 5198 |
"class A { class B { class C {}; }; };", |
| 5199 |
recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X")))))); |
| 5200 |
} |
| 5201 |
|
| 5202 |
TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) { |
| 5203 |
EXPECT_TRUE(matches( |
| 5204 |
"class A { class B { void f() { C c; } class C {}; }; };", |
| 5205 |
varDecl(hasName("c"), hasType(recordDecl(hasName("C"), |
| 5206 |
hasAncestor(recordDecl(hasName("A")))))))); |
| 5207 |
} |
| 5208 |
|
| 5209 |
TEST(HasAncenstor, MatchesStatementAncestors) { |
| 5210 |
EXPECT_TRUE(matches( |
| 5211 |
"void f() { if (true) { while (false) { 42; } } }", |
| 5212 |
integerLiteral(equals(42), hasAncestor(ifStmt())))); |
| 5213 |
} |
| 5214 |
|
| 5215 |
TEST(HasAncestor, DrillsThroughDifferentHierarchies) { |
| 5216 |
EXPECT_TRUE(matches( |
| 5217 |
"void f() { if (true) { int x = 42; } }", |
| 5218 |
integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f")))))); |
| 5219 |
} |
| 5220 |
|
| 5221 |
TEST(HasAncestor, BindsRecursiveCombinations) { |
| 5222 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5223 |
"class C { class D { class E { class F { int y; }; }; }; };", |
| 5224 |
fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))), |
| 5225 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("r", 1))); |
| 5226 |
} |
| 5227 |
|
| 5228 |
TEST(HasAncestor, BindsCombinationsWithHasDescendant) { |
| 5229 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5230 |
"class C { class D { class E { class F { int y; }; }; }; };", |
| 5231 |
fieldDecl(hasAncestor( |
| 5232 |
decl( |
| 5233 |
hasDescendant(recordDecl(isDefinition(), |
| 5234 |
hasAncestor(recordDecl()))) |
| 5235 |
).bind("d") |
| 5236 |
)), |
| 5237 |
std::make_unique<VerifyIdIsBoundTo<CXXRecordDecl>>("d", "E"))); |
| 5238 |
} |
| 5239 |
|
| 5240 |
TEST(HasAncestor, MatchesClosestAncestor) { |
| 5241 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5242 |
"template <typename T> struct C {" |
| 5243 |
" void f(int) {" |
| 5244 |
" struct I { void g(T) { int x; } } i; i.g(42);" |
| 5245 |
" }" |
| 5246 |
"};" |
| 5247 |
"template struct C<int>;", |
| 5248 |
varDecl(hasName("x"), |
| 5249 |
hasAncestor(functionDecl(hasParameter( |
| 5250 |
0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"), |
| 5251 |
std::make_unique<VerifyIdIsBoundTo<FunctionDecl>>("f", "g", 2))); |
| 5252 |
} |
| 5253 |
|
| 5254 |
TEST(HasAncestor, MatchesInTemplateInstantiations) { |
| 5255 |
EXPECT_TRUE(matches( |
| 5256 |
"template <typename T> struct A { struct B { struct C { T t; }; }; }; " |
| 5257 |
"A<int>::B::C a;", |
| 5258 |
fieldDecl(hasType(asString("int")), |
| 5259 |
hasAncestor(recordDecl(hasName("A")))))); |
| 5260 |
} |
| 5261 |
|
| 5262 |
TEST(HasAncestor, MatchesInImplicitCode) { |
| 5263 |
EXPECT_TRUE(matches( |
| 5264 |
"struct X {}; struct A { A() {} X x; };", |
| 5265 |
cxxConstructorDecl( |
| 5266 |
hasAnyConstructorInitializer(withInitializer(expr( |
| 5267 |
hasAncestor(recordDecl(hasName("A"))))))))); |
| 5268 |
} |
| 5269 |
|
| 5270 |
TEST(HasParent, MatchesOnlyParent) { |
| 5271 |
EXPECT_TRUE(matches( |
| 5272 |
"void f() { if (true) { int x = 42; } }", |
| 5273 |
compoundStmt(hasParent(ifStmt())))); |
| 5274 |
EXPECT_TRUE(notMatches( |
| 5275 |
"void f() { for (;;) { int x = 42; } }", |
| 5276 |
compoundStmt(hasParent(ifStmt())))); |
| 5277 |
EXPECT_TRUE(notMatches( |
| 5278 |
"void f() { if (true) for (;;) { int x = 42; } }", |
| 5279 |
compoundStmt(hasParent(ifStmt())))); |
| 5280 |
} |
| 5281 |
|
| 5282 |
TEST(MatcherMemoize, HasParentDiffersFromHas) { |
| 5283 |
// Test introduced after detecting a bug in memoization |
| 5284 |
constexpr auto code = "void f() { throw 1; }"; |
| 5285 |
EXPECT_TRUE(notMatches( |
| 5286 |
code, |
| 5287 |
cxxThrowExpr(hasParent(expr())))); |
| 5288 |
EXPECT_TRUE(matches( |
| 5289 |
code, |
| 5290 |
cxxThrowExpr(has(expr())))); |
| 5291 |
EXPECT_TRUE(matches( |
| 5292 |
code, |
| 5293 |
cxxThrowExpr(anyOf(hasParent(expr()), has(expr()))))); |
| 5294 |
} |
| 5295 |
|
| 5296 |
TEST(MatcherMemoize, HasDiffersFromHasDescendant) { |
| 5297 |
// Test introduced after detecting a bug in memoization |
| 5298 |
constexpr auto code = "void f() { throw 1+1; }"; |
| 5299 |
EXPECT_TRUE(notMatches( |
| 5300 |
code, |
| 5301 |
cxxThrowExpr(has(integerLiteral())))); |
| 5302 |
EXPECT_TRUE(matches( |
| 5303 |
code, |
| 5304 |
cxxThrowExpr(hasDescendant(integerLiteral())))); |
| 5305 |
EXPECT_TRUE( |
| 5306 |
notMatches(code, cxxThrowExpr(allOf(hasDescendant(integerLiteral()), |
| 5307 |
has(integerLiteral()))))); |
| 5308 |
} |
| 5309 |
TEST(HasAncestor, MatchesAllAncestors) { |
| 5310 |
EXPECT_TRUE(matches( |
| 5311 |
"template <typename T> struct C { static void f() { 42; } };" |
| 5312 |
"void t() { C<int>::f(); }", |
| 5313 |
integerLiteral( |
| 5314 |
equals(42), |
| 5315 |
allOf( |
| 5316 |
hasAncestor(cxxRecordDecl(isTemplateInstantiation())), |
| 5317 |
hasAncestor(cxxRecordDecl(unless(isTemplateInstantiation()))))))); |
| 5318 |
} |
| 5319 |
|
| 5320 |
TEST(HasAncestor, ImplicitArrayCopyCtorDeclRefExpr) { |
| 5321 |
EXPECT_TRUE(matches("struct MyClass {\n" |
| 5322 |
" int c[1];\n" |
| 5323 |
" static MyClass Create() { return MyClass(); }\n" |
| 5324 |
"};", |
| 5325 |
declRefExpr(to(decl(hasAncestor(decl())))))); |
| 5326 |
} |
| 5327 |
|
| 5328 |
TEST(HasAncestor, AnonymousUnionMemberExpr) { |
| 5329 |
EXPECT_TRUE(matches("int F() {\n" |
| 5330 |
" union { int i; };\n" |
| 5331 |
" return i;\n" |
| 5332 |
"}\n", |
| 5333 |
memberExpr(member(hasAncestor(decl()))))); |
| 5334 |
EXPECT_TRUE(matches("void f() {\n" |
| 5335 |
" struct {\n" |
| 5336 |
" struct { int a; int b; };\n" |
| 5337 |
" } s;\n" |
| 5338 |
" s.a = 4;\n" |
| 5339 |
"}\n", |
| 5340 |
memberExpr(member(hasAncestor(decl()))))); |
| 5341 |
EXPECT_TRUE(matches("void f() {\n" |
| 5342 |
" struct {\n" |
| 5343 |
" struct { int a; int b; };\n" |
| 5344 |
" } s;\n" |
| 5345 |
" s.a = 4;\n" |
| 5346 |
"}\n", |
| 5347 |
declRefExpr(to(decl(hasAncestor(decl())))))); |
| 5348 |
} |
| 5349 |
TEST(HasAncestor, NonParmDependentTemplateParmVarDeclRefExpr) { |
| 5350 |
EXPECT_TRUE(matches("struct PartitionAllocator {\n" |
| 5351 |
" template<typename T>\n" |
| 5352 |
" static int quantizedSize(int count) {\n" |
| 5353 |
" return count;\n" |
| 5354 |
" }\n" |
| 5355 |
" void f() { quantizedSize<int>(10); }\n" |
| 5356 |
"};", |
| 5357 |
declRefExpr(to(decl(hasAncestor(decl())))))); |
| 5358 |
} |
| 5359 |
|
| 5360 |
TEST(HasAncestor, AddressOfExplicitSpecializationFunction) { |
| 5361 |
EXPECT_TRUE(matches("template <class T> void f();\n" |
| 5362 |
"template <> void f<int>();\n" |
| 5363 |
"void (*get_f())() { return f<int>; }\n", |
| 5364 |
declRefExpr(to(decl(hasAncestor(decl())))))); |
| 5365 |
} |
| 5366 |
|
| 5367 |
TEST(HasParent, MatchesAllParents) { |
| 5368 |
EXPECT_TRUE(matches( |
| 5369 |
"template <typename T> struct C { static void f() { 42; } };" |
| 5370 |
"void t() { C<int>::f(); }", |
| 5371 |
integerLiteral( |
| 5372 |
equals(42), |
| 5373 |
hasParent(compoundStmt(hasParent(functionDecl( |
| 5374 |
hasParent(cxxRecordDecl(isTemplateInstantiation()))))))))); |
| 5375 |
EXPECT_TRUE( |
| 5376 |
matches("template <typename T> struct C { static void f() { 42; } };" |
| 5377 |
"void t() { C<int>::f(); }", |
| 5378 |
integerLiteral( |
| 5379 |
equals(42), |
| 5380 |
hasParent(compoundStmt(hasParent(functionDecl(hasParent( |
| 5381 |
cxxRecordDecl(unless(isTemplateInstantiation())))))))))); |
| 5382 |
EXPECT_TRUE(matches( |
| 5383 |
"template <typename T> struct C { static void f() { 42; } };" |
| 5384 |
"void t() { C<int>::f(); }", |
| 5385 |
integerLiteral(equals(42), |
| 5386 |
hasParent(compoundStmt( |
| 5387 |
allOf(hasParent(functionDecl(hasParent( |
| 5388 |
cxxRecordDecl(isTemplateInstantiation())))), |
| 5389 |
hasParent(functionDecl(hasParent(cxxRecordDecl( |
| 5390 |
unless(isTemplateInstantiation()))))))))))); |
| 5391 |
EXPECT_TRUE( |
| 5392 |
notMatches("template <typename T> struct C { static void f() {} };" |
| 5393 |
"void t() { C<int>::f(); }", |
| 5394 |
compoundStmt(hasParent(recordDecl())))); |
| 5395 |
} |
| 5396 |
|
| 5397 |
TEST(HasParent, NoDuplicateParents) { |
| 5398 |
class HasDuplicateParents : public BoundNodesCallback { |
| 5399 |
public: |
| 5400 |
bool run(const BoundNodes *Nodes) override { return false; } |
| 5401 |
bool run(const BoundNodes *Nodes, ASTContext *Context) override { |
| 5402 |
const Stmt *Node = Nodes->getNodeAs<Stmt>("node"); |
| 5403 |
std::set<const void *> Parents; |
| 5404 |
for (const auto &Parent : Context->getParents(*Node)) { |
| 5405 |
if (!Parents.insert(Parent.getMemoizationData()).second) { |
| 5406 |
return true; |
| 5407 |
} |
| 5408 |
} |
| 5409 |
return false; |
| 5410 |
} |
| 5411 |
}; |
| 5412 |
EXPECT_FALSE(matchAndVerifyResultTrue( |
| 5413 |
"template <typename T> int Foo() { return 1 + 2; }\n" |
| 5414 |
"int x = Foo<int>() + Foo<unsigned>();", |
| 5415 |
stmt().bind("node"), std::make_unique<HasDuplicateParents>())); |
| 5416 |
} |
| 5417 |
|
| 5418 |
TEST(TypeMatching, PointeeTypes) { |
| 5419 |
EXPECT_TRUE(matches("int b; int &a = b;", |
| 5420 |
referenceType(pointee(builtinType())))); |
| 5421 |
EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType())))); |
| 5422 |
|
| 5423 |
EXPECT_TRUE(matches("int *a;", |
| 5424 |
loc(pointerType(pointee(builtinType()))))); |
| 5425 |
|
| 5426 |
EXPECT_TRUE(matches( |
| 5427 |
"int const *A;", |
| 5428 |
pointerType(pointee(isConstQualified(), builtinType())))); |
| 5429 |
EXPECT_TRUE(notMatches( |
| 5430 |
"int *A;", |
| 5431 |
pointerType(pointee(isConstQualified(), builtinType())))); |
| 5432 |
} |
| 5433 |
|
| 5434 |
TEST(ElaboratedTypeNarrowing, hasQualifier) { |
| 5435 |
EXPECT_TRUE(matches( |
| 5436 |
"namespace N {" |
| 5437 |
" namespace M {" |
| 5438 |
" class D {};" |
| 5439 |
" }" |
| 5440 |
"}" |
| 5441 |
"N::M::D d;", |
| 5442 |
elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
| 5443 |
EXPECT_TRUE(notMatches( |
| 5444 |
"namespace M {" |
| 5445 |
" class D {};" |
| 5446 |
"}" |
| 5447 |
"M::D d;", |
| 5448 |
elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
| 5449 |
EXPECT_TRUE(notMatches( |
| 5450 |
"struct D {" |
| 5451 |
"} d;", |
| 5452 |
elaboratedType(hasQualifier(nestedNameSpecifier())))); |
| 5453 |
} |
| 5454 |
|
| 5455 |
TEST(ElaboratedTypeNarrowing, namesType) { |
| 5456 |
EXPECT_TRUE(matches( |
| 5457 |
"namespace N {" |
| 5458 |
" namespace M {" |
| 5459 |
" class D {};" |
| 5460 |
" }" |
| 5461 |
"}" |
| 5462 |
"N::M::D d;", |
| 5463 |
elaboratedType(elaboratedType(namesType(recordType( |
| 5464 |
hasDeclaration(namedDecl(hasName("D"))))))))); |
| 5465 |
EXPECT_TRUE(notMatches( |
| 5466 |
"namespace M {" |
| 5467 |
" class D {};" |
| 5468 |
"}" |
| 5469 |
"M::D d;", |
| 5470 |
elaboratedType(elaboratedType(namesType(typedefType()))))); |
| 5471 |
} |
| 5472 |
|
| 5473 |
TEST(NNS, BindsNestedNameSpecifiers) { |
| 5474 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5475 |
"namespace ns { struct E { struct B {}; }; } ns::E::B b;", |
| 5476 |
nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"), |
| 5477 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifier>>( |
| 5478 |
"nns", "ns::struct E::"))); |
| 5479 |
} |
| 5480 |
|
| 5481 |
TEST(NNS, BindsNestedNameSpecifierLocs) { |
| 5482 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5483 |
"namespace ns { struct B {}; } ns::B b;", |
| 5484 |
loc(nestedNameSpecifier()).bind("loc"), |
| 5485 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifierLoc>>("loc", 1))); |
| 5486 |
} |
| 5487 |
|
| 5488 |
TEST(NNS, DescendantsOfNestedNameSpecifiers) { |
| 5489 |
StringRef Fragment = |
| 5490 |
"namespace a { struct A { struct B { struct C {}; }; }; };" |
| 5491 |
"void f() { a::A::B::C c; }"; |
| 5492 |
EXPECT_TRUE(matches( |
| 5493 |
Fragment, |
| 5494 |
nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 5495 |
hasDescendant(nestedNameSpecifier( |
| 5496 |
specifiesNamespace(hasName("a"))))))); |
| 5497 |
EXPECT_TRUE(notMatches( |
| 5498 |
Fragment, |
| 5499 |
nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 5500 |
has(nestedNameSpecifier( |
| 5501 |
specifiesNamespace(hasName("a"))))))); |
| 5502 |
EXPECT_TRUE(matches( |
| 5503 |
Fragment, |
| 5504 |
nestedNameSpecifier(specifiesType(asString("struct a::A")), |
| 5505 |
has(nestedNameSpecifier( |
| 5506 |
specifiesNamespace(hasName("a"))))))); |
| 5507 |
|
| 5508 |
// Not really useful because a NestedNameSpecifier can af at most one child, |
| 5509 |
// but to complete the interface. |
| 5510 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5511 |
Fragment, |
| 5512 |
nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 5513 |
forEach(nestedNameSpecifier().bind("x"))), |
| 5514 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifier>>("x", 1))); |
| 5515 |
} |
| 5516 |
|
| 5517 |
TEST(NNS, NestedNameSpecifiersAsDescendants) { |
| 5518 |
StringRef Fragment = |
| 5519 |
"namespace a { struct A { struct B { struct C {}; }; }; };" |
| 5520 |
"void f() { a::A::B::C c; }"; |
| 5521 |
EXPECT_TRUE(matches( |
| 5522 |
Fragment, |
| 5523 |
decl(hasDescendant(nestedNameSpecifier(specifiesType( |
| 5524 |
asString("struct a::A"))))))); |
| 5525 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5526 |
Fragment, |
| 5527 |
functionDecl(hasName("f"), |
| 5528 |
forEachDescendant(nestedNameSpecifier().bind("x"))), |
| 5529 |
// Nested names: a, a::A and a::A::B. |
| 5530 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifier>>("x", 3))); |
| 5531 |
} |
| 5532 |
|
| 5533 |
TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { |
| 5534 |
StringRef Fragment = |
| 5535 |
"namespace a { struct A { struct B { struct C {}; }; }; };" |
| 5536 |
"void f() { a::A::B::C c; }"; |
| 5537 |
EXPECT_TRUE(matches( |
| 5538 |
Fragment, |
| 5539 |
nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 5540 |
hasDescendant(loc(nestedNameSpecifier( |
| 5541 |
specifiesNamespace(hasName("a")))))))); |
| 5542 |
EXPECT_TRUE(notMatches( |
| 5543 |
Fragment, |
| 5544 |
nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 5545 |
has(loc(nestedNameSpecifier( |
| 5546 |
specifiesNamespace(hasName("a")))))))); |
| 5547 |
EXPECT_TRUE(matches( |
| 5548 |
Fragment, |
| 5549 |
nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))), |
| 5550 |
has(loc(nestedNameSpecifier( |
| 5551 |
specifiesNamespace(hasName("a")))))))); |
| 5552 |
|
| 5553 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5554 |
Fragment, |
| 5555 |
nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 5556 |
forEach(nestedNameSpecifierLoc().bind("x"))), |
| 5557 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifierLoc>>("x", 1))); |
| 5558 |
} |
| 5559 |
|
| 5560 |
TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { |
| 5561 |
StringRef Fragment = |
| 5562 |
"namespace a { struct A { struct B { struct C {}; }; }; };" |
| 5563 |
"void f() { a::A::B::C c; }"; |
| 5564 |
EXPECT_TRUE(matches( |
| 5565 |
Fragment, |
| 5566 |
decl(hasDescendant(loc(nestedNameSpecifier(specifiesType( |
| 5567 |
asString("struct a::A")))))))); |
| 5568 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5569 |
Fragment, |
| 5570 |
functionDecl(hasName("f"), |
| 5571 |
forEachDescendant(nestedNameSpecifierLoc().bind("x"))), |
| 5572 |
// Nested names: a, a::A and a::A::B. |
| 5573 |
std::make_unique<VerifyIdIsBoundTo<NestedNameSpecifierLoc>>("x", 3))); |
| 5574 |
} |
| 5575 |
|
| 5576 |
TEST(Attr, AttrsAsDescendants) { |
| 5577 |
StringRef Fragment = "namespace a { struct [[clang::warn_unused_result]] " |
| 5578 |
"F{}; [[noreturn]] void foo(); }"; |
| 5579 |
EXPECT_TRUE(matches(Fragment, namespaceDecl(hasDescendant(attr())))); |
| 5580 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5581 |
Fragment, |
| 5582 |
namespaceDecl(hasName("a"), |
| 5583 |
forEachDescendant(attr(unless(isImplicit())).bind("x"))), |
| 5584 |
std::make_unique<VerifyIdIsBoundTo<Attr>>("x", 2))); |
| 5585 |
} |
| 5586 |
|
| 5587 |
TEST(Attr, ParentsOfAttrs) { |
| 5588 |
StringRef Fragment = |
| 5589 |
"namespace a { struct [[clang::warn_unused_result]] F{}; }"; |
| 5590 |
EXPECT_TRUE(matches(Fragment, attr(hasAncestor(namespaceDecl())))); |
| 5591 |
} |
| 5592 |
|
| 5593 |
template <typename T> class VerifyMatchOnNode : public BoundNodesCallback { |
| 5594 |
public: |
| 5595 |
VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher, |
| 5596 |
StringRef InnerId) |
| 5597 |
: Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) { |
| 5598 |
} |
| 5599 |
|
| 5600 |
bool run(const BoundNodes *Nodes) override { return false; } |
| 5601 |
|
| 5602 |
bool run(const BoundNodes *Nodes, ASTContext *Context) override { |
| 5603 |
const T *Node = Nodes->getNodeAs<T>(Id); |
| 5604 |
return selectFirst<T>(InnerId, match(InnerMatcher, *Node, *Context)) != |
| 5605 |
nullptr; |
| 5606 |
} |
| 5607 |
private: |
| 5608 |
std::string Id; |
| 5609 |
internal::Matcher<T> InnerMatcher; |
| 5610 |
std::string InnerId; |
| 5611 |
}; |
| 5612 |
|
| 5613 |
TEST(MatchFinder, CanMatchDeclarationsRecursively) { |
| 5614 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5615 |
"class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 5616 |
std::make_unique<VerifyMatchOnNode<Decl>>( |
| 5617 |
"X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))), |
| 5618 |
"Y"))); |
| 5619 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 5620 |
"class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 5621 |
std::make_unique<VerifyMatchOnNode<Decl>>( |
| 5622 |
"X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))), |
| 5623 |
"Z"))); |
| 5624 |
} |
| 5625 |
|
| 5626 |
TEST(MatchFinder, CanMatchStatementsRecursively) { |
| 5627 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5628 |
"void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
| 5629 |
std::make_unique<VerifyMatchOnNode<Stmt>>( |
| 5630 |
"if", stmt(hasDescendant(forStmt().bind("for"))), "for"))); |
| 5631 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 5632 |
"void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
| 5633 |
std::make_unique<VerifyMatchOnNode<Stmt>>( |
| 5634 |
"if", stmt(hasDescendant(declStmt().bind("decl"))), "decl"))); |
| 5635 |
} |
| 5636 |
|
| 5637 |
TEST(MatchFinder, CanMatchSingleNodesRecursively) { |
| 5638 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5639 |
"class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 5640 |
std::make_unique<VerifyMatchOnNode<Decl>>( |
| 5641 |
"X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y"))); |
| 5642 |
EXPECT_TRUE(matchAndVerifyResultFalse( |
| 5643 |
"class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 5644 |
std::make_unique<VerifyMatchOnNode<Decl>>( |
| 5645 |
"X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z"))); |
| 5646 |
} |
| 5647 |
|
| 5648 |
TEST(StatementMatcher, HasReturnValue) { |
| 5649 |
StatementMatcher RetVal = returnStmt(hasReturnValue(binaryOperator())); |
| 5650 |
EXPECT_TRUE(matches("int F() { int a, b; return a + b; }", RetVal)); |
| 5651 |
EXPECT_FALSE(matches("int F() { int a; return a; }", RetVal)); |
| 5652 |
EXPECT_FALSE(matches("void F() { return; }", RetVal)); |
| 5653 |
} |
| 5654 |
|
| 5655 |
TEST(StatementMatcher, ForFunction) { |
| 5656 |
StringRef CppString1 = "struct PosVec {" |
| 5657 |
" PosVec& operator=(const PosVec&) {" |
| 5658 |
" auto x = [] { return 1; };" |
| 5659 |
" return *this;" |
| 5660 |
" }" |
| 5661 |
"};"; |
| 5662 |
StringRef CppString2 = "void F() {" |
| 5663 |
" struct S {" |
| 5664 |
" void F2() {" |
| 5665 |
" return;" |
| 5666 |
" }" |
| 5667 |
" };" |
| 5668 |
"}"; |
| 5669 |
EXPECT_TRUE( |
| 5670 |
matches( |
| 5671 |
CppString1, |
| 5672 |
returnStmt(forFunction(hasName("operator=")), |
| 5673 |
has(unaryOperator(hasOperatorName("*")))))); |
| 5674 |
EXPECT_TRUE( |
| 5675 |
notMatches( |
| 5676 |
CppString1, |
| 5677 |
returnStmt(forFunction(hasName("operator=")), |
| 5678 |
has(integerLiteral())))); |
| 5679 |
EXPECT_TRUE( |
| 5680 |
matches( |
| 5681 |
CppString1, |
| 5682 |
returnStmt(forFunction(hasName("operator()")), |
| 5683 |
has(integerLiteral())))); |
| 5684 |
EXPECT_TRUE(matches(CppString2, returnStmt(forFunction(hasName("F2"))))); |
| 5685 |
EXPECT_TRUE(notMatches(CppString2, returnStmt(forFunction(hasName("F"))))); |
| 5686 |
} |
| 5687 |
|
| 5688 |
TEST(StatementMatcher, ForCallable) { |
| 5689 |
// These tests are copied over from the forFunction() test above. |
| 5690 |
StringRef CppString1 = "struct PosVec {" |
| 5691 |
" PosVec& operator=(const PosVec&) {" |
| 5692 |
" auto x = [] { return 1; };" |
| 5693 |
" return *this;" |
| 5694 |
" }" |
| 5695 |
"};"; |
| 5696 |
StringRef CppString2 = "void F() {" |
| 5697 |
" struct S {" |
| 5698 |
" void F2() {" |
| 5699 |
" return;" |
| 5700 |
" }" |
| 5701 |
" };" |
| 5702 |
"}"; |
| 5703 |
|
| 5704 |
EXPECT_TRUE( |
| 5705 |
matches( |
| 5706 |
CppString1, |
| 5707 |
returnStmt(forCallable(functionDecl(hasName("operator="))), |
| 5708 |
has(unaryOperator(hasOperatorName("*")))))); |
| 5709 |
EXPECT_TRUE( |
| 5710 |
notMatches( |
| 5711 |
CppString1, |
| 5712 |
returnStmt(forCallable(functionDecl(hasName("operator="))), |
| 5713 |
has(integerLiteral())))); |
| 5714 |
EXPECT_TRUE( |
| 5715 |
matches( |
| 5716 |
CppString1, |
| 5717 |
returnStmt(forCallable(functionDecl(hasName("operator()"))), |
| 5718 |
has(integerLiteral())))); |
| 5719 |
EXPECT_TRUE(matches(CppString2, |
| 5720 |
returnStmt(forCallable(functionDecl(hasName("F2")))))); |
| 5721 |
EXPECT_TRUE(notMatches(CppString2, |
| 5722 |
returnStmt(forCallable(functionDecl(hasName("F")))))); |
| 5723 |
|
| 5724 |
// These tests are specific to forCallable(). |
| 5725 |
StringRef ObjCString1 = "@interface I" |
| 5726 |
"-(void) foo;" |
| 5727 |
"@end" |
| 5728 |
"@implementation I" |
| 5729 |
"-(void) foo {" |
| 5730 |
" void (^block)() = ^{ 0x2b | ~0x2b; };" |
| 5731 |
"}" |
| 5732 |
"@end"; |
| 5733 |
|
| 5734 |
EXPECT_TRUE( |
| 5735 |
matchesObjC( |
| 5736 |
ObjCString1, |
| 5737 |
binaryOperator(forCallable(blockDecl())))); |
| 5738 |
|
| 5739 |
EXPECT_TRUE( |
| 5740 |
notMatchesObjC( |
| 5741 |
ObjCString1, |
| 5742 |
binaryOperator(forCallable(objcMethodDecl())))); |
| 5743 |
|
| 5744 |
StringRef ObjCString2 = "@interface I" |
| 5745 |
"-(void) foo;" |
| 5746 |
"@end" |
| 5747 |
"@implementation I" |
| 5748 |
"-(void) foo {" |
| 5749 |
" 0x2b | ~0x2b;" |
| 5750 |
" void (^block)() = ^{};" |
| 5751 |
"}" |
| 5752 |
"@end"; |
| 5753 |
|
| 5754 |
EXPECT_TRUE( |
| 5755 |
matchesObjC( |
| 5756 |
ObjCString2, |
| 5757 |
binaryOperator(forCallable(objcMethodDecl())))); |
| 5758 |
|
| 5759 |
EXPECT_TRUE( |
| 5760 |
notMatchesObjC( |
| 5761 |
ObjCString2, |
| 5762 |
binaryOperator(forCallable(blockDecl())))); |
| 5763 |
} |
| 5764 |
|
| 5765 |
TEST(Matcher, ForEachOverriden) { |
| 5766 |
const auto ForEachOverriddenInClass = [](const char *ClassName) { |
| 5767 |
return cxxMethodDecl(ofClass(hasName(ClassName)), isVirtual(), |
| 5768 |
forEachOverridden(cxxMethodDecl().bind("overridden"))) |
| 5769 |
.bind("override"); |
| 5770 |
}; |
| 5771 |
static const char Code1[] = "class A { virtual void f(); };" |
| 5772 |
"class B : public A { void f(); };" |
| 5773 |
"class C : public B { void f(); };"; |
| 5774 |
// C::f overrides A::f. |
| 5775 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5776 |
Code1, ForEachOverriddenInClass("C"), |
| 5777 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("override", "f", 1))); |
| 5778 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5779 |
Code1, ForEachOverriddenInClass("C"), |
| 5780 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("overridden", "f", |
| 5781 |
1))); |
| 5782 |
// B::f overrides A::f. |
| 5783 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5784 |
Code1, ForEachOverriddenInClass("B"), |
| 5785 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("override", "f", 1))); |
| 5786 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5787 |
Code1, ForEachOverriddenInClass("B"), |
| 5788 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("overridden", "f", |
| 5789 |
1))); |
| 5790 |
// A::f overrides nothing. |
| 5791 |
EXPECT_TRUE(notMatches(Code1, ForEachOverriddenInClass("A"))); |
| 5792 |
|
| 5793 |
static const char Code2[] = |
| 5794 |
"class A1 { virtual void f(); };" |
| 5795 |
"class A2 { virtual void f(); };" |
| 5796 |
"class B : public A1, public A2 { void f(); };"; |
| 5797 |
// B::f overrides A1::f and A2::f. This produces two matches. |
| 5798 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5799 |
Code2, ForEachOverriddenInClass("B"), |
| 5800 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("override", "f", 2))); |
| 5801 |
EXPECT_TRUE(matchAndVerifyResultTrue( |
| 5802 |
Code2, ForEachOverriddenInClass("B"), |
| 5803 |
std::make_unique<VerifyIdIsBoundTo<CXXMethodDecl>>("overridden", "f", |
| 5804 |
2))); |
| 5805 |
// A1::f overrides nothing. |
| 5806 |
EXPECT_TRUE(notMatches(Code2, ForEachOverriddenInClass("A1"))); |
| 5807 |
} |
| 5808 |
|
| 5809 |
TEST(Matcher, HasAnyDeclaration) { |
| 5810 |
StringRef Fragment = "void foo(int p1);" |
| 5811 |
"void foo(int *p2);" |
| 5812 |
"void bar(int p3);" |
| 5813 |
"template <typename T> void baz(T t) { foo(t); }"; |
| 5814 |
|
| 5815 |
EXPECT_TRUE( |
| 5816 |
matches(Fragment, unresolvedLookupExpr(hasAnyDeclaration(functionDecl( |
| 5817 |
hasParameter(0, parmVarDecl(hasName("p1")))))))); |
| 5818 |
EXPECT_TRUE( |
| 5819 |
matches(Fragment, unresolvedLookupExpr(hasAnyDeclaration(functionDecl( |
| 5820 |
hasParameter(0, parmVarDecl(hasName("p2")))))))); |
| 5821 |
EXPECT_TRUE( |
| 5822 |
notMatches(Fragment, unresolvedLookupExpr(hasAnyDeclaration(functionDecl( |
| 5823 |
hasParameter(0, parmVarDecl(hasName("p3")))))))); |
| 5824 |
EXPECT_TRUE(notMatches(Fragment, unresolvedLookupExpr(hasAnyDeclaration( |
| 5825 |
functionDecl(hasName("bar")))))); |
| 5826 |
} |
| 5827 |
|
| 5828 |
TEST(SubstTemplateTypeParmType, HasReplacementType) { |
| 5829 |
StringRef Fragment = "template<typename T>" |
| 5830 |
"double F(T t);" |
| 5831 |
"int i;" |
| 5832 |
"double j = F(i);"; |
| 5833 |
EXPECT_TRUE(matches(Fragment, substTemplateTypeParmType(hasReplacementType( |
| 5834 |
qualType(asString("int")))))); |
| 5835 |
EXPECT_TRUE(notMatches(Fragment, substTemplateTypeParmType(hasReplacementType( |
| 5836 |
qualType(asString("double")))))); |
| 5837 |
EXPECT_TRUE( |
| 5838 |
notMatches("template<int N>" |
| 5839 |
"double F();" |
| 5840 |
"double j = F<5>();", |
| 5841 |
substTemplateTypeParmType(hasReplacementType(qualType())))); |
| 5842 |
} |
| 5843 |
|
| 5844 |
TEST(ClassTemplateSpecializationDecl, HasSpecializedTemplate) { |
| 5845 |
auto Matcher = classTemplateSpecializationDecl( |
| 5846 |
hasSpecializedTemplate(classTemplateDecl())); |
| 5847 |
EXPECT_TRUE( |
| 5848 |
matches("template<typename T> class A {}; typedef A<int> B;", Matcher)); |
| 5849 |
EXPECT_TRUE(notMatches("template<typename T> class A {};", Matcher)); |
| 5850 |
} |
| 5851 |
|
| 5852 |
TEST(CXXNewExpr, Array) { |
| 5853 |
StatementMatcher NewArray = cxxNewExpr(isArray()); |
| 5854 |
|
| 5855 |
EXPECT_TRUE(matches("void foo() { int *Ptr = new int[10]; }", NewArray)); |
| 5856 |
EXPECT_TRUE(notMatches("void foo() { int *Ptr = new int; }", NewArray)); |
| 5857 |
|
| 5858 |
StatementMatcher NewArraySize10 = |
| 5859 |
cxxNewExpr(hasArraySize(integerLiteral(equals(10)))); |
| 5860 |
EXPECT_TRUE( |
| 5861 |
matches("void foo() { int *Ptr = new int[10]; }", NewArraySize10)); |
| 5862 |
EXPECT_TRUE( |
| 5863 |
notMatches("void foo() { int *Ptr = new int[20]; }", NewArraySize10)); |
| 5864 |
} |
| 5865 |
|
| 5866 |
TEST(CXXNewExpr, PlacementArgs) { |
| 5867 |
StatementMatcher IsPlacementNew = cxxNewExpr(hasAnyPlacementArg(anything())); |
| 5868 |
|
| 5869 |
EXPECT_TRUE(matches(R"( |
| 5870 |
void* operator new(decltype(sizeof(void*)), void*); |
| 5871 |
int *foo(void* Storage) { |
| 5872 |
return new (Storage) int; |
| 5873 |
})", |
| 5874 |
IsPlacementNew)); |
| 5875 |
|
| 5876 |
EXPECT_TRUE(matches(R"( |
| 5877 |
void* operator new(decltype(sizeof(void*)), void*, unsigned); |
| 5878 |
int *foo(void* Storage) { |
| 5879 |
return new (Storage, 16) int; |
| 5880 |
})", |
| 5881 |
cxxNewExpr(hasPlacementArg( |
| 5882 |
1, ignoringImpCasts(integerLiteral(equals(16))))))); |
| 5883 |
|
| 5884 |
EXPECT_TRUE(notMatches(R"( |
| 5885 |
void* operator new(decltype(sizeof(void*)), void*); |
| 5886 |
int *foo(void* Storage) { |
| 5887 |
return new int; |
| 5888 |
})", |
| 5889 |
IsPlacementNew)); |
| 5890 |
} |
| 5891 |
|
| 5892 |
TEST(HasUnqualifiedLoc, BindsToConstIntVarDecl) { |
| 5893 |
EXPECT_TRUE(matches( |
| 5894 |
"const int x = 0;", |
| 5895 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5896 |
hasUnqualifiedLoc(loc(asString("int")))))))); |
| 5897 |
} |
| 5898 |
|
| 5899 |
TEST(HasUnqualifiedLoc, BindsToVolatileIntVarDecl) { |
| 5900 |
EXPECT_TRUE(matches( |
| 5901 |
"volatile int x = 0;", |
| 5902 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5903 |
hasUnqualifiedLoc(loc(asString("int")))))))); |
| 5904 |
} |
| 5905 |
|
| 5906 |
TEST(HasUnqualifiedLoc, BindsToConstVolatileIntVarDecl) { |
| 5907 |
EXPECT_TRUE(matches( |
| 5908 |
"const volatile int x = 0;", |
| 5909 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5910 |
hasUnqualifiedLoc(loc(asString("int")))))))); |
| 5911 |
} |
| 5912 |
|
| 5913 |
TEST(HasUnqualifiedLoc, BindsToConstPointerVarDecl) { |
| 5914 |
auto matcher = varDecl( |
| 5915 |
hasName("x"), |
| 5916 |
hasTypeLoc(qualifiedTypeLoc(hasUnqualifiedLoc(pointerTypeLoc())))); |
| 5917 |
EXPECT_TRUE(matches("int* const x = 0;", matcher)); |
| 5918 |
EXPECT_TRUE(notMatches("int const x = 0;", matcher)); |
| 5919 |
} |
| 5920 |
|
| 5921 |
TEST(HasUnqualifiedLoc, BindsToPointerToConstVolatileIntVarDecl) { |
| 5922 |
EXPECT_TRUE( |
| 5923 |
matches("const volatile int* x = 0;", |
| 5924 |
varDecl(hasName("x"), |
| 5925 |
hasTypeLoc(pointerTypeLoc(hasPointeeLoc(qualifiedTypeLoc( |
| 5926 |
hasUnqualifiedLoc(loc(asString("int")))))))))); |
| 5927 |
} |
| 5928 |
|
| 5929 |
TEST(HasUnqualifiedLoc, BindsToConstIntFunctionDecl) { |
| 5930 |
EXPECT_TRUE( |
| 5931 |
matches("const int f() { return 5; }", |
| 5932 |
functionDecl(hasName("f"), |
| 5933 |
hasReturnTypeLoc(qualifiedTypeLoc( |
| 5934 |
hasUnqualifiedLoc(loc(asString("int")))))))); |
| 5935 |
} |
| 5936 |
|
| 5937 |
TEST(HasUnqualifiedLoc, FloatBindsToConstFloatVarDecl) { |
| 5938 |
EXPECT_TRUE(matches( |
| 5939 |
"const float x = 0;", |
| 5940 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5941 |
hasUnqualifiedLoc(loc(asString("float")))))))); |
| 5942 |
} |
| 5943 |
|
| 5944 |
TEST(HasUnqualifiedLoc, FloatDoesNotBindToIntVarDecl) { |
| 5945 |
EXPECT_TRUE(notMatches( |
| 5946 |
"int x = 0;", |
| 5947 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5948 |
hasUnqualifiedLoc(loc(asString("float")))))))); |
| 5949 |
} |
| 5950 |
|
| 5951 |
TEST(HasUnqualifiedLoc, FloatDoesNotBindToConstIntVarDecl) { |
| 5952 |
EXPECT_TRUE(notMatches( |
| 5953 |
"const int x = 0;", |
| 5954 |
varDecl(hasName("x"), hasTypeLoc(qualifiedTypeLoc( |
| 5955 |
hasUnqualifiedLoc(loc(asString("float")))))))); |
| 5956 |
} |
| 5957 |
|
| 5958 |
TEST(HasReturnTypeLoc, BindsToIntReturnTypeLoc) { |
| 5959 |
EXPECT_TRUE(matches( |
| 5960 |
"int f() { return 5; }", |
| 5961 |
functionDecl(hasName("f"), hasReturnTypeLoc(loc(asString("int")))))); |
| 5962 |
} |
| 5963 |
|
| 5964 |
TEST(HasReturnTypeLoc, BindsToFloatReturnTypeLoc) { |
| 5965 |
EXPECT_TRUE(matches( |
| 5966 |
"float f() { return 5.0; }", |
| 5967 |
functionDecl(hasName("f"), hasReturnTypeLoc(loc(asString("float")))))); |
| 5968 |
} |
| 5969 |
|
| 5970 |
TEST(HasReturnTypeLoc, BindsToVoidReturnTypeLoc) { |
| 5971 |
EXPECT_TRUE(matches( |
| 5972 |
"void f() {}", |
| 5973 |
functionDecl(hasName("f"), hasReturnTypeLoc(loc(asString("void")))))); |
| 5974 |
} |
| 5975 |
|
| 5976 |
TEST(HasReturnTypeLoc, FloatDoesNotBindToIntReturnTypeLoc) { |
| 5977 |
EXPECT_TRUE(notMatches( |
| 5978 |
"int f() { return 5; }", |
| 5979 |
functionDecl(hasName("f"), hasReturnTypeLoc(loc(asString("float")))))); |
| 5980 |
} |
| 5981 |
|
| 5982 |
TEST(HasReturnTypeLoc, IntDoesNotBindToFloatReturnTypeLoc) { |
| 5983 |
EXPECT_TRUE(notMatches( |
| 5984 |
"float f() { return 5.0; }", |
| 5985 |
functionDecl(hasName("f"), hasReturnTypeLoc(loc(asString("int")))))); |
| 5986 |
} |
| 5987 |
|
| 5988 |
TEST(HasPointeeLoc, BindsToAnyPointeeTypeLoc) { |
| 5989 |
auto matcher = varDecl(hasName("x"), |
| 5990 |
hasTypeLoc(pointerTypeLoc(hasPointeeLoc(typeLoc())))); |
| 5991 |
EXPECT_TRUE(matches("int* x;", matcher)); |
| 5992 |
EXPECT_TRUE(matches("float* x;", matcher)); |
| 5993 |
EXPECT_TRUE(matches("char* x;", matcher)); |
| 5994 |
EXPECT_TRUE(matches("void* x;", matcher)); |
| 5995 |
} |
| 5996 |
|
| 5997 |
TEST(HasPointeeLoc, DoesNotBindToTypeLocWithoutPointee) { |
| 5998 |
auto matcher = varDecl(hasName("x"), |
| 5999 |
hasTypeLoc(pointerTypeLoc(hasPointeeLoc(typeLoc())))); |
| 6000 |
EXPECT_TRUE(notMatches("int x;", matcher)); |
| 6001 |
EXPECT_TRUE(notMatches("float x;", matcher)); |
| 6002 |
EXPECT_TRUE(notMatches("char x;", matcher)); |
| 6003 |
} |
| 6004 |
|
| 6005 |
TEST(HasPointeeLoc, BindsToTypeLocPointingToInt) { |
| 6006 |
EXPECT_TRUE( |
| 6007 |
matches("int* x;", pointerTypeLoc(hasPointeeLoc(loc(asString("int")))))); |
| 6008 |
} |
| 6009 |
|
| 6010 |
TEST(HasPointeeLoc, BindsToTypeLocPointingToIntPointer) { |
| 6011 |
EXPECT_TRUE(matches("int** x;", |
| 6012 |
pointerTypeLoc(hasPointeeLoc(loc(asString("int *")))))); |
| 6013 |
} |
| 6014 |
|
| 6015 |
TEST(HasPointeeLoc, BindsToTypeLocPointingToTypeLocPointingToInt) { |
| 6016 |
EXPECT_TRUE(matches("int** x;", pointerTypeLoc(hasPointeeLoc(pointerTypeLoc( |
| 6017 |
hasPointeeLoc(loc(asString("int")))))))); |
| 6018 |
} |
| 6019 |
|
| 6020 |
TEST(HasPointeeLoc, BindsToTypeLocPointingToFloat) { |
| 6021 |
EXPECT_TRUE(matches("float* x;", |
| 6022 |
pointerTypeLoc(hasPointeeLoc(loc(asString("float")))))); |
| 6023 |
} |
| 6024 |
|
| 6025 |
TEST(HasPointeeLoc, IntPointeeDoesNotBindToTypeLocPointingToFloat) { |
| 6026 |
EXPECT_TRUE(notMatches("float* x;", |
| 6027 |
pointerTypeLoc(hasPointeeLoc(loc(asString("int")))))); |
| 6028 |
} |
| 6029 |
|
| 6030 |
TEST(HasPointeeLoc, FloatPointeeDoesNotBindToTypeLocPointingToInt) { |
| 6031 |
EXPECT_TRUE(notMatches( |
| 6032 |
"int* x;", pointerTypeLoc(hasPointeeLoc(loc(asString("float")))))); |
| 6033 |
} |
| 6034 |
|
| 6035 |
TEST(HasReferentLoc, BindsToAnyReferentTypeLoc) { |
| 6036 |
auto matcher = varDecl( |
| 6037 |
hasName("r"), hasTypeLoc(referenceTypeLoc(hasReferentLoc(typeLoc())))); |
| 6038 |
EXPECT_TRUE(matches("int rr = 3; int& r = rr;", matcher)); |
| 6039 |
EXPECT_TRUE(matches("int rr = 3; auto& r = rr;", matcher)); |
| 6040 |
EXPECT_TRUE(matches("int rr = 3; const int& r = rr;", matcher)); |
| 6041 |
EXPECT_TRUE(matches("float rr = 3.0; float& r = rr;", matcher)); |
| 6042 |
EXPECT_TRUE(matches("char rr = 'a'; char& r = rr;", matcher)); |
| 6043 |
} |
| 6044 |
|
| 6045 |
TEST(HasReferentLoc, DoesNotBindToTypeLocWithoutReferent) { |
| 6046 |
auto matcher = varDecl( |
| 6047 |
hasName("r"), hasTypeLoc(referenceTypeLoc(hasReferentLoc(typeLoc())))); |
| 6048 |
EXPECT_TRUE(notMatches("int r;", matcher)); |
| 6049 |
EXPECT_TRUE(notMatches("int r = 3;", matcher)); |
| 6050 |
EXPECT_TRUE(notMatches("const int r = 3;", matcher)); |
| 6051 |
EXPECT_TRUE(notMatches("int* r;", matcher)); |
| 6052 |
EXPECT_TRUE(notMatches("float r;", matcher)); |
| 6053 |
EXPECT_TRUE(notMatches("char r;", matcher)); |
| 6054 |
} |
| 6055 |
|
| 6056 |
TEST(HasReferentLoc, BindsToAnyRvalueReference) { |
| 6057 |
auto matcher = varDecl( |
| 6058 |
hasName("r"), hasTypeLoc(referenceTypeLoc(hasReferentLoc(typeLoc())))); |
| 6059 |
EXPECT_TRUE(matches("int&& r = 3;", matcher)); |
| 6060 |
EXPECT_TRUE(matches("auto&& r = 3;", matcher)); |
| 6061 |
EXPECT_TRUE(matches("float&& r = 3.0;", matcher)); |
| 6062 |
} |
| 6063 |
|
| 6064 |
TEST(HasReferentLoc, BindsToIntReferenceTypeLoc) { |
| 6065 |
EXPECT_TRUE(matches("int rr = 3; int& r = rr;", |
| 6066 |
referenceTypeLoc(hasReferentLoc(loc(asString("int")))))); |
| 6067 |
} |
| 6068 |
|
| 6069 |
TEST(HasReferentLoc, BindsToIntRvalueReferenceTypeLoc) { |
| 6070 |
EXPECT_TRUE(matches("int&& r = 3;", |
| 6071 |
referenceTypeLoc(hasReferentLoc(loc(asString("int")))))); |
| 6072 |
} |
| 6073 |
|
| 6074 |
TEST(HasReferentLoc, BindsToFloatReferenceTypeLoc) { |
| 6075 |
EXPECT_TRUE( |
| 6076 |
matches("float rr = 3.0; float& r = rr;", |
| 6077 |
referenceTypeLoc(hasReferentLoc(loc(asString("float")))))); |
| 6078 |
} |
| 6079 |
|
| 6080 |
TEST(HasReferentLoc, BindsToParameterWithIntReferenceTypeLoc) { |
| 6081 |
EXPECT_TRUE(matches( |
| 6082 |
"int f(int& r) { return r; }", |
| 6083 |
parmVarDecl(hasName("r"), hasTypeLoc(referenceTypeLoc( |
| 6084 |
hasReferentLoc(loc(asString("int")))))))); |
| 6085 |
} |
| 6086 |
|
| 6087 |
TEST(HasReferentLoc, IntReferenceDoesNotBindToFloatReferenceTypeLoc) { |
| 6088 |
EXPECT_TRUE( |
| 6089 |
notMatches("float rr = 3.0; float& r = rr;", |
| 6090 |
referenceTypeLoc(hasReferentLoc(loc(asString("int")))))); |
| 6091 |
} |
| 6092 |
|
| 6093 |
TEST(HasReferentLoc, FloatReferenceDoesNotBindToIntReferenceTypeLoc) { |
| 6094 |
EXPECT_TRUE( |
| 6095 |
notMatches("int rr = 3; int& r = rr;", |
| 6096 |
referenceTypeLoc(hasReferentLoc(loc(asString("float")))))); |
| 6097 |
} |
| 6098 |
|
| 6099 |
TEST(HasReferentLoc, DoesNotBindToParameterWithoutIntReferenceTypeLoc) { |
| 6100 |
EXPECT_TRUE(notMatches( |
| 6101 |
"int f(int r) { return r; }", |
| 6102 |
parmVarDecl(hasName("r"), hasTypeLoc(referenceTypeLoc( |
| 6103 |
hasReferentLoc(loc(asString("int")))))))); |
| 6104 |
} |
| 6105 |
|
| 6106 |
TEST(HasAnyTemplateArgumentLoc, BindsToSpecializationWithIntArgument) { |
| 6107 |
EXPECT_TRUE(matches( |
| 6108 |
"template<typename T> class A {}; A<int> a;", |
| 6109 |
varDecl(hasName("a"), |
| 6110 |
hasTypeLoc(elaboratedTypeLoc(hasNamedTypeLoc( |
| 6111 |
templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc( |
| 6112 |
hasTypeLoc(loc(asString("int"))))))))))); |
| 6113 |
} |
| 6114 |
|
| 6115 |
TEST(HasAnyTemplateArgumentLoc, BindsToSpecializationWithDoubleArgument) { |
| 6116 |
EXPECT_TRUE(matches( |
| 6117 |
"template<typename T> class A {}; A<double> a;", |
| 6118 |
varDecl(hasName("a"), |
| 6119 |
hasTypeLoc(elaboratedTypeLoc(hasNamedTypeLoc( |
| 6120 |
templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc( |
| 6121 |
hasTypeLoc(loc(asString("double"))))))))))); |
| 6122 |
} |
| 6123 |
|
| 6124 |
TEST(HasAnyTemplateArgumentLoc, BindsToExplicitSpecializationWithIntArgument) { |
| 6125 |
EXPECT_TRUE(matches( |
| 6126 |
"template<typename T> class A {}; template<> class A<int> {};", |
| 6127 |
classTemplateSpecializationDecl( |
| 6128 |
hasName("A"), |
| 6129 |
hasTypeLoc(templateSpecializationTypeLoc( |
| 6130 |
hasAnyTemplateArgumentLoc(hasTypeLoc(loc(asString("int"))))))))); |
| 6131 |
} |
| 6132 |
|
| 6133 |
TEST(HasAnyTemplateArgumentLoc, |
| 6134 |
BindsToExplicitSpecializationWithDoubleArgument) { |
| 6135 |
EXPECT_TRUE(matches( |
| 6136 |
"template<typename T> class A {}; template<> class A<double> {};", |
| 6137 |
classTemplateSpecializationDecl( |
| 6138 |
hasName("A"), |
| 6139 |
hasTypeLoc(templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc( |
| 6140 |
hasTypeLoc(loc(asString("double"))))))))); |
| 6141 |
} |
| 6142 |
|
| 6143 |
TEST(HasAnyTemplateArgumentLoc, BindsToSpecializationWithMultipleArguments) { |
| 6144 |
auto code = R"( |
| 6145 |
template<typename T, typename U> class A {}; |
| 6146 |
template<> class A<double, int> {}; |
| 6147 |
)"; |
| 6148 |
EXPECT_TRUE( |
| 6149 |
matches(code, classTemplateSpecializationDecl( |
| 6150 |
hasName("A"), hasTypeLoc(templateSpecializationTypeLoc( |
| 6151 |
hasAnyTemplateArgumentLoc(hasTypeLoc( |
| 6152 |
loc(asString("double"))))))))); |
| 6153 |
EXPECT_TRUE(matches( |
| 6154 |
code, |
| 6155 |
classTemplateSpecializationDecl( |
| 6156 |
hasName("A"), |
| 6157 |
hasTypeLoc(templateSpecializationTypeLoc( |
| 6158 |
hasAnyTemplateArgumentLoc(hasTypeLoc(loc(asString("int"))))))))); |
| 6159 |
} |
| 6160 |
|
| 6161 |
TEST(HasAnyTemplateArgumentLoc, DoesNotBindToSpecializationWithIntArgument) { |
| 6162 |
EXPECT_TRUE(notMatches( |
| 6163 |
"template<typename T> class A {}; A<int> a;", |
| 6164 |
classTemplateSpecializationDecl( |
| 6165 |
hasName("A"), |
| 6166 |
hasTypeLoc(templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc( |
| 6167 |
hasTypeLoc(loc(asString("double"))))))))); |
| 6168 |
} |
| 6169 |
|
| 6170 |
TEST(HasAnyTemplateArgumentLoc, |
| 6171 |
DoesNotBindToExplicitSpecializationWithIntArgument) { |
| 6172 |
EXPECT_TRUE(notMatches( |
| 6173 |
"template<typename T> class A {}; template<> class A<int> {};", |
| 6174 |
classTemplateSpecializationDecl( |
| 6175 |
hasName("A"), |
| 6176 |
hasTypeLoc(templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc( |
| 6177 |
hasTypeLoc(loc(asString("double"))))))))); |
| 6178 |
} |
| 6179 |
|
| 6180 |
TEST(HasTemplateArgumentLoc, BindsToSpecializationWithIntArgument) { |
| 6181 |
EXPECT_TRUE( |
| 6182 |
matches("template<typename T> class A {}; A<int> a;", |
| 6183 |
varDecl(hasName("a"), |
| 6184 |
hasTypeLoc(elaboratedTypeLoc(hasNamedTypeLoc( |
| 6185 |
templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6186 |
0, hasTypeLoc(loc(asString("int"))))))))))); |
| 6187 |
} |
| 6188 |
|
| 6189 |
TEST(HasTemplateArgumentLoc, BindsToSpecializationWithDoubleArgument) { |
| 6190 |
EXPECT_TRUE( |
| 6191 |
matches("template<typename T> class A {}; A<double> a;", |
| 6192 |
varDecl(hasName("a"), |
| 6193 |
hasTypeLoc(elaboratedTypeLoc(hasNamedTypeLoc( |
| 6194 |
templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6195 |
0, hasTypeLoc(loc(asString("double"))))))))))); |
| 6196 |
} |
| 6197 |
|
| 6198 |
TEST(HasTemplateArgumentLoc, BindsToExplicitSpecializationWithIntArgument) { |
| 6199 |
EXPECT_TRUE(matches( |
| 6200 |
"template<typename T> class A {}; template<> class A<int> {};", |
| 6201 |
classTemplateSpecializationDecl( |
| 6202 |
hasName("A"), |
| 6203 |
hasTypeLoc(templateSpecializationTypeLoc( |
| 6204 |
hasTemplateArgumentLoc(0, hasTypeLoc(loc(asString("int"))))))))); |
| 6205 |
} |
| 6206 |
|
| 6207 |
TEST(HasTemplateArgumentLoc, BindsToExplicitSpecializationWithDoubleArgument) { |
| 6208 |
EXPECT_TRUE(matches( |
| 6209 |
"template<typename T> class A {}; template<> class A<double> {};", |
| 6210 |
classTemplateSpecializationDecl( |
| 6211 |
hasName("A"), |
| 6212 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6213 |
0, hasTypeLoc(loc(asString("double"))))))))); |
| 6214 |
} |
| 6215 |
|
| 6216 |
TEST(HasTemplateArgumentLoc, BindsToSpecializationWithMultipleArguments) { |
| 6217 |
auto code = R"( |
| 6218 |
template<typename T, typename U> class A {}; |
| 6219 |
template<> class A<double, int> {}; |
| 6220 |
)"; |
| 6221 |
EXPECT_TRUE(matches( |
| 6222 |
code, classTemplateSpecializationDecl( |
| 6223 |
hasName("A"), |
| 6224 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6225 |
0, hasTypeLoc(loc(asString("double"))))))))); |
| 6226 |
EXPECT_TRUE(matches( |
| 6227 |
code, classTemplateSpecializationDecl( |
| 6228 |
hasName("A"), |
| 6229 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6230 |
1, hasTypeLoc(loc(asString("int"))))))))); |
| 6231 |
} |
| 6232 |
|
| 6233 |
TEST(HasTemplateArgumentLoc, DoesNotBindToSpecializationWithIntArgument) { |
| 6234 |
EXPECT_TRUE(notMatches( |
| 6235 |
"template<typename T> class A {}; A<int> a;", |
| 6236 |
classTemplateSpecializationDecl( |
| 6237 |
hasName("A"), |
| 6238 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6239 |
0, hasTypeLoc(loc(asString("double"))))))))); |
| 6240 |
} |
| 6241 |
|
| 6242 |
TEST(HasTemplateArgumentLoc, |
| 6243 |
DoesNotBindToExplicitSpecializationWithIntArgument) { |
| 6244 |
EXPECT_TRUE(notMatches( |
| 6245 |
"template<typename T> class A {}; template<> class A<int> {};", |
| 6246 |
classTemplateSpecializationDecl( |
| 6247 |
hasName("A"), |
| 6248 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6249 |
0, hasTypeLoc(loc(asString("double"))))))))); |
| 6250 |
} |
| 6251 |
|
| 6252 |
TEST(HasTemplateArgumentLoc, |
| 6253 |
DoesNotBindToSpecializationWithMisplacedArguments) { |
| 6254 |
auto code = R"( |
| 6255 |
template<typename T, typename U> class A {}; |
| 6256 |
template<> class A<double, int> {}; |
| 6257 |
)"; |
| 6258 |
EXPECT_TRUE(notMatches( |
| 6259 |
code, classTemplateSpecializationDecl( |
| 6260 |
hasName("A"), |
| 6261 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6262 |
1, hasTypeLoc(loc(asString("double"))))))))); |
| 6263 |
EXPECT_TRUE(notMatches( |
| 6264 |
code, classTemplateSpecializationDecl( |
| 6265 |
hasName("A"), |
| 6266 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6267 |
0, hasTypeLoc(loc(asString("int"))))))))); |
| 6268 |
} |
| 6269 |
|
| 6270 |
TEST(HasTemplateArgumentLoc, DoesNotBindWithBadIndex) { |
| 6271 |
auto code = R"( |
| 6272 |
template<typename T, typename U> class A {}; |
| 6273 |
template<> class A<double, int> {}; |
| 6274 |
)"; |
| 6275 |
EXPECT_TRUE(notMatches( |
| 6276 |
code, classTemplateSpecializationDecl( |
| 6277 |
hasName("A"), |
| 6278 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6279 |
-1, hasTypeLoc(loc(asString("double"))))))))); |
| 6280 |
EXPECT_TRUE(notMatches( |
| 6281 |
code, classTemplateSpecializationDecl( |
| 6282 |
hasName("A"), |
| 6283 |
hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc( |
| 6284 |
100, hasTypeLoc(loc(asString("int"))))))))); |
| 6285 |
} |
| 6286 |
|
| 6287 |
TEST(HasTemplateArgumentLoc, BindsToDeclRefExprWithIntArgument) { |
| 6288 |
EXPECT_TRUE(matches(R"( |
| 6289 |
template<typename T> T f(T t) { return t; } |
| 6290 |
int g() { int i = f<int>(3); return i; } |
| 6291 |
)", |
| 6292 |
declRefExpr(to(functionDecl(hasName("f"))), |
| 6293 |
hasTemplateArgumentLoc( |
| 6294 |
0, hasTypeLoc(loc(asString("int"))))))); |
| 6295 |
} |
| 6296 |
|
| 6297 |
TEST(HasTemplateArgumentLoc, BindsToDeclRefExprWithDoubleArgument) { |
| 6298 |
EXPECT_TRUE(matches( |
| 6299 |
R"( |
| 6300 |
template<typename T> T f(T t) { return t; } |
| 6301 |
double g() { double i = f<double>(3.0); return i; } |
| 6302 |
)", |
| 6303 |
declRefExpr( |
| 6304 |
to(functionDecl(hasName("f"))), |
| 6305 |
hasTemplateArgumentLoc(0, hasTypeLoc(loc(asString("double"))))))); |
| 6306 |
} |
| 6307 |
|
| 6308 |
TEST(HasTemplateArgumentLoc, DoesNotBindToDeclRefExprWithDoubleArgument) { |
| 6309 |
EXPECT_TRUE(notMatches( |
| 6310 |
R"( |
| 6311 |
template<typename T> T f(T t) { return t; } |
| 6312 |
double g() { double i = f<double>(3.0); return i; } |
| 6313 |
)", |
| 6314 |
declRefExpr( |
| 6315 |
to(functionDecl(hasName("f"))), |
| 6316 |
hasTemplateArgumentLoc(0, hasTypeLoc(loc(asString("int"))))))); |
| 6317 |
} |
| 6318 |
|
| 6319 |
TEST(HasNamedTypeLoc, BindsToElaboratedObjectDeclaration) { |
| 6320 |
EXPECT_TRUE(matches( |
| 6321 |
R"( |
| 6322 |
template <typename T> |
| 6323 |
class C {}; |
| 6324 |
class C<int> c; |
| 6325 |
)", |
| 6326 |
varDecl(hasName("c"), |
| 6327 |
hasTypeLoc(elaboratedTypeLoc( |
| 6328 |
hasNamedTypeLoc(templateSpecializationTypeLoc( |
| 6329 |
hasAnyTemplateArgumentLoc(templateArgumentLoc())))))))); |
| 6330 |
} |
| 6331 |
|
| 6332 |
TEST(HasNamedTypeLoc, DoesNotBindToNonElaboratedObjectDeclaration) { |
| 6333 |
EXPECT_TRUE(matches( |
| 6334 |
R"( |
| 6335 |
template <typename T> |
| 6336 |
class C {}; |
| 6337 |
C<int> c; |
| 6338 |
)", |
| 6339 |
varDecl(hasName("c"), |
| 6340 |
hasTypeLoc(elaboratedTypeLoc( |
| 6341 |
hasNamedTypeLoc(templateSpecializationTypeLoc( |
| 6342 |
hasAnyTemplateArgumentLoc(templateArgumentLoc())))))))); |
| 6343 |
} |
| 6344 |
|
| 6345 |
} // namespace ast_matchers |
| 6346 |
} // namespace clang |