Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/ASTMatchersTest.cpp - AST matcher unit tests ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ASTMatchersTest.h" |
| 11 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 13 | #include "clang/Tooling/Tooling.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | namespace clang { |
| 17 | namespace ast_matchers { |
| 18 | |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame^] | 19 | #if GTEST_HAS_DEATH_TEST |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 20 | TEST(HasNameDeathTest, DiesOnEmptyName) { |
| 21 | ASSERT_DEBUG_DEATH({ |
| 22 | DeclarationMatcher HasEmptyName = record(hasName("")); |
| 23 | EXPECT_TRUE(notMatches("class X {};", HasEmptyName)); |
| 24 | }, ""); |
| 25 | } |
| 26 | |
| 27 | TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) { |
| 28 | ASSERT_DEBUG_DEATH({ |
| 29 | DeclarationMatcher IsDerivedFromEmpty = record(isDerivedFrom("")); |
| 30 | EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty)); |
| 31 | }, ""); |
| 32 | } |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame^] | 33 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 34 | |
| 35 | TEST(NameableDeclaration, MatchesVariousDecls) { |
| 36 | DeclarationMatcher NamedX = nameableDeclaration(hasName("X")); |
| 37 | EXPECT_TRUE(matches("typedef int X;", NamedX)); |
| 38 | EXPECT_TRUE(matches("int X;", NamedX)); |
| 39 | EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX)); |
| 40 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX)); |
| 41 | EXPECT_TRUE(matches("void foo() { int X; }", NamedX)); |
| 42 | EXPECT_TRUE(matches("namespace X { }", NamedX)); |
| 43 | |
| 44 | EXPECT_TRUE(notMatches("#define X 1", NamedX)); |
| 45 | } |
| 46 | |
| 47 | TEST(DeclarationMatcher, MatchClass) { |
| 48 | DeclarationMatcher ClassMatcher(record()); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 49 | #if !defined(_MSC_VER) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 50 | EXPECT_FALSE(matches("", ClassMatcher)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 51 | #else |
| 52 | // Matches class type_info. |
| 53 | EXPECT_TRUE(matches("", ClassMatcher)); |
| 54 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 55 | |
| 56 | DeclarationMatcher ClassX = record(record(hasName("X"))); |
| 57 | EXPECT_TRUE(matches("class X;", ClassX)); |
| 58 | EXPECT_TRUE(matches("class X {};", ClassX)); |
| 59 | EXPECT_TRUE(matches("template<class T> class X {};", ClassX)); |
| 60 | EXPECT_TRUE(notMatches("", ClassX)); |
| 61 | } |
| 62 | |
| 63 | TEST(DeclarationMatcher, ClassIsDerived) { |
| 64 | DeclarationMatcher IsDerivedFromX = record(isDerivedFrom("X")); |
| 65 | |
| 66 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX)); |
| 67 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX)); |
| 68 | EXPECT_TRUE(matches("class X {};", IsDerivedFromX)); |
| 69 | EXPECT_TRUE(matches("class X;", IsDerivedFromX)); |
| 70 | EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX)); |
| 71 | EXPECT_TRUE(notMatches("", IsDerivedFromX)); |
| 72 | |
| 73 | DeclarationMatcher ZIsDerivedFromX = |
| 74 | record(hasName("Z"), isDerivedFrom("X")); |
| 75 | EXPECT_TRUE( |
| 76 | matches("class X {}; class Y : public X {}; class Z : public Y {};", |
| 77 | ZIsDerivedFromX)); |
| 78 | EXPECT_TRUE( |
| 79 | matches("class X {};" |
| 80 | "template<class T> class Y : public X {};" |
| 81 | "class Z : public Y<int> {};", ZIsDerivedFromX)); |
| 82 | EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};", |
| 83 | ZIsDerivedFromX)); |
| 84 | EXPECT_TRUE( |
| 85 | matches("template<class T> class X {}; " |
| 86 | "template<class T> class Z : public X<T> {};", |
| 87 | ZIsDerivedFromX)); |
| 88 | EXPECT_TRUE( |
| 89 | matches("template<class T, class U=T> class X {}; " |
| 90 | "template<class T> class Z : public X<T> {};", |
| 91 | ZIsDerivedFromX)); |
| 92 | EXPECT_TRUE( |
| 93 | notMatches("template<class X> class A { class Z : public X {}; };", |
| 94 | ZIsDerivedFromX)); |
| 95 | EXPECT_TRUE( |
| 96 | matches("template<class X> class A { public: class Z : public X {}; }; " |
| 97 | "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX)); |
| 98 | EXPECT_TRUE( |
| 99 | matches("template <class T> class X {}; " |
| 100 | "template<class Y> class A { class Z : public X<Y> {}; };", |
| 101 | ZIsDerivedFromX)); |
| 102 | EXPECT_TRUE( |
| 103 | notMatches("template<template<class T> class X> class A { " |
| 104 | " class Z : public X<int> {}; };", ZIsDerivedFromX)); |
| 105 | EXPECT_TRUE( |
| 106 | matches("template<template<class T> class X> class A { " |
| 107 | " public: class Z : public X<int> {}; }; " |
| 108 | "template<class T> class X {}; void y() { A<X>::Z z; }", |
| 109 | ZIsDerivedFromX)); |
| 110 | EXPECT_TRUE( |
| 111 | notMatches("template<class X> class A { class Z : public X::D {}; };", |
| 112 | ZIsDerivedFromX)); |
| 113 | EXPECT_TRUE( |
| 114 | matches("template<class X> class A { public: " |
| 115 | " class Z : public X::D {}; }; " |
| 116 | "class Y { public: class X {}; typedef X D; }; " |
| 117 | "void y() { A<Y>::Z z; }", ZIsDerivedFromX)); |
| 118 | EXPECT_TRUE( |
| 119 | matches("class X {}; typedef X Y; class Z : public Y {};", |
| 120 | ZIsDerivedFromX)); |
| 121 | EXPECT_TRUE( |
| 122 | matches("template<class T> class Y { typedef typename T::U X; " |
| 123 | " class Z : public X {}; };", ZIsDerivedFromX)); |
| 124 | EXPECT_TRUE(matches("class X {}; class Z : public ::X {};", |
| 125 | ZIsDerivedFromX)); |
| 126 | EXPECT_TRUE( |
| 127 | notMatches("template<class T> class X {}; " |
| 128 | "template<class T> class A { class Z : public X<T>::D {}; };", |
| 129 | ZIsDerivedFromX)); |
| 130 | EXPECT_TRUE( |
| 131 | matches("template<class T> class X { public: typedef X<T> D; }; " |
| 132 | "template<class T> class A { public: " |
| 133 | " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }", |
| 134 | ZIsDerivedFromX)); |
| 135 | EXPECT_TRUE( |
| 136 | notMatches("template<class X> class A { class Z : public X::D::E {}; };", |
| 137 | ZIsDerivedFromX)); |
| 138 | EXPECT_TRUE( |
| 139 | matches("class X {}; typedef X V; typedef V W; class Z : public W {};", |
| 140 | ZIsDerivedFromX)); |
| 141 | EXPECT_TRUE( |
| 142 | matches("class X {}; class Y : public X {}; " |
| 143 | "typedef Y V; typedef V W; class Z : public W {};", |
| 144 | ZIsDerivedFromX)); |
| 145 | EXPECT_TRUE( |
| 146 | matches("template<class T, class U> class X {}; " |
| 147 | "template<class T> class A { class Z : public X<T, int> {}; };", |
| 148 | ZIsDerivedFromX)); |
| 149 | EXPECT_TRUE( |
| 150 | notMatches("template<class X> class D { typedef X A; typedef A B; " |
| 151 | " typedef B C; class Z : public C {}; };", |
| 152 | ZIsDerivedFromX)); |
| 153 | EXPECT_TRUE( |
| 154 | matches("class X {}; typedef X A; typedef A B; " |
| 155 | "class Z : public B {};", ZIsDerivedFromX)); |
| 156 | EXPECT_TRUE( |
| 157 | matches("class X {}; typedef X A; typedef A B; typedef B C; " |
| 158 | "class Z : public C {};", ZIsDerivedFromX)); |
| 159 | EXPECT_TRUE( |
| 160 | matches("class U {}; typedef U X; typedef X V; " |
| 161 | "class Z : public V {};", ZIsDerivedFromX)); |
| 162 | EXPECT_TRUE( |
| 163 | matches("class Base {}; typedef Base X; " |
| 164 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 165 | EXPECT_TRUE( |
| 166 | matches("class Base {}; typedef Base Base2; typedef Base2 X; " |
| 167 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 168 | EXPECT_TRUE( |
| 169 | notMatches("class Base {}; class Base2 {}; typedef Base2 X; " |
| 170 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 171 | EXPECT_TRUE( |
| 172 | matches("class A {}; typedef A X; typedef A Y; " |
| 173 | "class Z : public Y {};", ZIsDerivedFromX)); |
| 174 | EXPECT_TRUE( |
| 175 | notMatches("template <typename T> class Z;" |
| 176 | "template <> class Z<void> {};" |
| 177 | "template <typename T> class Z : public Z<void> {};", |
| 178 | IsDerivedFromX)); |
| 179 | EXPECT_TRUE( |
| 180 | matches("template <typename T> class X;" |
| 181 | "template <> class X<void> {};" |
| 182 | "template <typename T> class X : public X<void> {};", |
| 183 | IsDerivedFromX)); |
| 184 | EXPECT_TRUE(matches( |
| 185 | "class X {};" |
| 186 | "template <typename T> class Z;" |
| 187 | "template <> class Z<void> {};" |
| 188 | "template <typename T> class Z : public Z<void>, public X {};", |
| 189 | ZIsDerivedFromX)); |
| 190 | |
| 191 | // FIXME: Once we have better matchers for template type matching, |
| 192 | // get rid of the Variable(...) matching and match the right template |
| 193 | // declarations directly. |
| 194 | const char *RecursiveTemplateOneParameter = |
| 195 | "class Base1 {}; class Base2 {};" |
| 196 | "template <typename T> class Z;" |
| 197 | "template <> class Z<void> : public Base1 {};" |
| 198 | "template <> class Z<int> : public Base2 {};" |
| 199 | "template <> class Z<float> : public Z<void> {};" |
| 200 | "template <> class Z<double> : public Z<int> {};" |
| 201 | "template <typename T> class Z : public Z<float>, public Z<double> {};" |
| 202 | "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }"; |
| 203 | EXPECT_TRUE(matches( |
| 204 | RecursiveTemplateOneParameter, |
| 205 | variable(hasName("z_float"), |
| 206 | hasInitializer(hasType(record(isDerivedFrom("Base1"))))))); |
| 207 | EXPECT_TRUE(notMatches( |
| 208 | RecursiveTemplateOneParameter, |
| 209 | variable( |
| 210 | hasName("z_float"), |
| 211 | hasInitializer(hasType(record(isDerivedFrom("Base2"))))))); |
| 212 | EXPECT_TRUE(matches( |
| 213 | RecursiveTemplateOneParameter, |
| 214 | variable( |
| 215 | hasName("z_char"), |
| 216 | hasInitializer(hasType(record(isDerivedFrom("Base1"), |
| 217 | isDerivedFrom("Base2"))))))); |
| 218 | |
| 219 | const char *RecursiveTemplateTwoParameters = |
| 220 | "class Base1 {}; class Base2 {};" |
| 221 | "template <typename T1, typename T2> class Z;" |
| 222 | "template <typename T> class Z<void, T> : public Base1 {};" |
| 223 | "template <typename T> class Z<int, T> : public Base2 {};" |
| 224 | "template <typename T> class Z<float, T> : public Z<void, T> {};" |
| 225 | "template <typename T> class Z<double, T> : public Z<int, T> {};" |
| 226 | "template <typename T1, typename T2> class Z : " |
| 227 | " public Z<float, T2>, public Z<double, T2> {};" |
| 228 | "void f() { Z<float, void> z_float; Z<double, void> z_double; " |
| 229 | " Z<char, void> z_char; }"; |
| 230 | EXPECT_TRUE(matches( |
| 231 | RecursiveTemplateTwoParameters, |
| 232 | variable( |
| 233 | hasName("z_float"), |
| 234 | hasInitializer(hasType(record(isDerivedFrom("Base1"))))))); |
| 235 | EXPECT_TRUE(notMatches( |
| 236 | RecursiveTemplateTwoParameters, |
| 237 | variable( |
| 238 | hasName("z_float"), |
| 239 | hasInitializer(hasType(record(isDerivedFrom("Base2"))))))); |
| 240 | EXPECT_TRUE(matches( |
| 241 | RecursiveTemplateTwoParameters, |
| 242 | variable( |
| 243 | hasName("z_char"), |
| 244 | hasInitializer(hasType(record(isDerivedFrom("Base1"), |
| 245 | isDerivedFrom("Base2"))))))); |
| 246 | } |
| 247 | |
| 248 | TEST(DeclarationMatcher, MatchAnyOf) { |
| 249 | DeclarationMatcher YOrZDerivedFromX = |
| 250 | record(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z")))); |
| 251 | |
| 252 | EXPECT_TRUE( |
| 253 | matches("class X {}; class Z : public X {};", YOrZDerivedFromX)); |
| 254 | EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX)); |
| 255 | EXPECT_TRUE( |
| 256 | notMatches("class X {}; class W : public X {};", YOrZDerivedFromX)); |
| 257 | EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX)); |
| 258 | |
| 259 | DeclarationMatcher XOrYOrZOrUOrV = |
| 260 | record(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"), |
| 261 | hasName("V"))); |
| 262 | |
| 263 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV)); |
| 264 | EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV)); |
| 265 | EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV)); |
| 266 | EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); |
| 267 | EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); |
| 268 | EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); |
| 269 | } |
| 270 | |
| 271 | TEST(DeclarationMatcher, MatchHas) { |
| 272 | DeclarationMatcher HasClassX = record(has(record(hasName("X")))); |
| 273 | |
| 274 | EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); |
| 275 | EXPECT_TRUE(matches("class X {};", HasClassX)); |
| 276 | |
| 277 | DeclarationMatcher YHasClassX = |
| 278 | record(hasName("Y"), has(record(hasName("X")))); |
| 279 | EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX)); |
| 280 | EXPECT_TRUE(notMatches("class X {};", YHasClassX)); |
| 281 | EXPECT_TRUE( |
| 282 | notMatches("class Y { class Z { class X {}; }; };", YHasClassX)); |
| 283 | } |
| 284 | |
| 285 | TEST(DeclarationMatcher, MatchHasRecursiveAllOf) { |
| 286 | DeclarationMatcher Recursive = |
| 287 | record( |
| 288 | has(record( |
| 289 | has(record(hasName("X"))), |
| 290 | has(record(hasName("Y"))), |
| 291 | hasName("Z"))), |
| 292 | has(record( |
| 293 | has(record(hasName("A"))), |
| 294 | has(record(hasName("B"))), |
| 295 | hasName("C"))), |
| 296 | hasName("F")); |
| 297 | |
| 298 | EXPECT_TRUE(matches( |
| 299 | "class F {" |
| 300 | " class Z {" |
| 301 | " class X {};" |
| 302 | " class Y {};" |
| 303 | " };" |
| 304 | " class C {" |
| 305 | " class A {};" |
| 306 | " class B {};" |
| 307 | " };" |
| 308 | "};", Recursive)); |
| 309 | |
| 310 | EXPECT_TRUE(matches( |
| 311 | "class F {" |
| 312 | " class Z {" |
| 313 | " class A {};" |
| 314 | " class X {};" |
| 315 | " class Y {};" |
| 316 | " };" |
| 317 | " class C {" |
| 318 | " class X {};" |
| 319 | " class A {};" |
| 320 | " class B {};" |
| 321 | " };" |
| 322 | "};", Recursive)); |
| 323 | |
| 324 | EXPECT_TRUE(matches( |
| 325 | "class O1 {" |
| 326 | " class O2 {" |
| 327 | " class F {" |
| 328 | " class Z {" |
| 329 | " class A {};" |
| 330 | " class X {};" |
| 331 | " class Y {};" |
| 332 | " };" |
| 333 | " class C {" |
| 334 | " class X {};" |
| 335 | " class A {};" |
| 336 | " class B {};" |
| 337 | " };" |
| 338 | " };" |
| 339 | " };" |
| 340 | "};", Recursive)); |
| 341 | } |
| 342 | |
| 343 | TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) { |
| 344 | DeclarationMatcher Recursive = |
| 345 | record( |
| 346 | anyOf( |
| 347 | has(record( |
| 348 | anyOf( |
| 349 | has(record( |
| 350 | hasName("X"))), |
| 351 | has(record( |
| 352 | hasName("Y"))), |
| 353 | hasName("Z")))), |
| 354 | has(record( |
| 355 | anyOf( |
| 356 | hasName("C"), |
| 357 | has(record( |
| 358 | hasName("A"))), |
| 359 | has(record( |
| 360 | hasName("B")))))), |
| 361 | hasName("F"))); |
| 362 | |
| 363 | EXPECT_TRUE(matches("class F {};", Recursive)); |
| 364 | EXPECT_TRUE(matches("class Z {};", Recursive)); |
| 365 | EXPECT_TRUE(matches("class C {};", Recursive)); |
| 366 | EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive)); |
| 367 | EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive)); |
| 368 | EXPECT_TRUE( |
| 369 | matches("class O1 { class O2 {" |
| 370 | " class M { class N { class B {}; }; }; " |
| 371 | "}; };", Recursive)); |
| 372 | } |
| 373 | |
| 374 | TEST(DeclarationMatcher, MatchNot) { |
| 375 | DeclarationMatcher NotClassX = |
| 376 | record( |
| 377 | isDerivedFrom("Y"), |
| 378 | unless(hasName("Y")), |
| 379 | unless(hasName("X"))); |
| 380 | EXPECT_TRUE(notMatches("", NotClassX)); |
| 381 | EXPECT_TRUE(notMatches("class Y {};", NotClassX)); |
| 382 | EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX)); |
| 383 | EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX)); |
| 384 | EXPECT_TRUE( |
| 385 | notMatches("class Y {}; class Z {}; class X : public Y {};", |
| 386 | NotClassX)); |
| 387 | |
| 388 | DeclarationMatcher ClassXHasNotClassY = |
| 389 | record( |
| 390 | hasName("X"), |
| 391 | has(record(hasName("Z"))), |
| 392 | unless( |
| 393 | has(record(hasName("Y"))))); |
| 394 | EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY)); |
| 395 | EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };", |
| 396 | ClassXHasNotClassY)); |
| 397 | } |
| 398 | |
| 399 | TEST(DeclarationMatcher, HasDescendant) { |
| 400 | DeclarationMatcher ZDescendantClassX = |
| 401 | record( |
| 402 | hasDescendant(record(hasName("X"))), |
| 403 | hasName("Z")); |
| 404 | EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX)); |
| 405 | EXPECT_TRUE( |
| 406 | matches("class Z { class Y { class X {}; }; };", ZDescendantClassX)); |
| 407 | EXPECT_TRUE( |
| 408 | matches("class Z { class A { class Y { class X {}; }; }; };", |
| 409 | ZDescendantClassX)); |
| 410 | EXPECT_TRUE( |
| 411 | matches("class Z { class A { class B { class Y { class X {}; }; }; }; };", |
| 412 | ZDescendantClassX)); |
| 413 | EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX)); |
| 414 | |
| 415 | DeclarationMatcher ZDescendantClassXHasClassY = |
| 416 | record( |
| 417 | hasDescendant(record(has(record(hasName("Y"))), |
| 418 | hasName("X"))), |
| 419 | hasName("Z")); |
| 420 | EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };", |
| 421 | ZDescendantClassXHasClassY)); |
| 422 | EXPECT_TRUE( |
| 423 | matches("class Z { class A { class B { class X { class Y {}; }; }; }; };", |
| 424 | ZDescendantClassXHasClassY)); |
| 425 | EXPECT_TRUE(notMatches( |
| 426 | "class Z {" |
| 427 | " class A {" |
| 428 | " class B {" |
| 429 | " class X {" |
| 430 | " class C {" |
| 431 | " class Y {};" |
| 432 | " };" |
| 433 | " };" |
| 434 | " }; " |
| 435 | " };" |
| 436 | "};", ZDescendantClassXHasClassY)); |
| 437 | |
| 438 | DeclarationMatcher ZDescendantClassXDescendantClassY = |
| 439 | record( |
| 440 | hasDescendant(record(hasDescendant(record(hasName("Y"))), |
| 441 | hasName("X"))), |
| 442 | hasName("Z")); |
| 443 | EXPECT_TRUE( |
| 444 | matches("class Z { class A { class X { class B { class Y {}; }; }; }; };", |
| 445 | ZDescendantClassXDescendantClassY)); |
| 446 | EXPECT_TRUE(matches( |
| 447 | "class Z {" |
| 448 | " class A {" |
| 449 | " class X {" |
| 450 | " class B {" |
| 451 | " class Y {};" |
| 452 | " };" |
| 453 | " class Y {};" |
| 454 | " };" |
| 455 | " };" |
| 456 | "};", ZDescendantClassXDescendantClassY)); |
| 457 | } |
| 458 | |
| 459 | TEST(StatementMatcher, Has) { |
| 460 | StatementMatcher HasVariableI = |
| 461 | expression( |
| 462 | hasType(pointsTo(record(hasName("X")))), |
| 463 | has(declarationReference(to(variable(hasName("i")))))); |
| 464 | |
| 465 | EXPECT_TRUE(matches( |
| 466 | "class X; X *x(int); void c() { int i; x(i); }", HasVariableI)); |
| 467 | EXPECT_TRUE(notMatches( |
| 468 | "class X; X *x(int); void c() { int i; x(42); }", HasVariableI)); |
| 469 | } |
| 470 | |
| 471 | TEST(StatementMatcher, HasDescendant) { |
| 472 | StatementMatcher HasDescendantVariableI = |
| 473 | expression( |
| 474 | hasType(pointsTo(record(hasName("X")))), |
| 475 | hasDescendant(declarationReference(to(variable(hasName("i")))))); |
| 476 | |
| 477 | EXPECT_TRUE(matches( |
| 478 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }", |
| 479 | HasDescendantVariableI)); |
| 480 | EXPECT_TRUE(notMatches( |
| 481 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }", |
| 482 | HasDescendantVariableI)); |
| 483 | } |
| 484 | |
| 485 | TEST(TypeMatcher, MatchesClassType) { |
| 486 | TypeMatcher TypeA = hasDeclaration(record(hasName("A"))); |
| 487 | |
| 488 | EXPECT_TRUE(matches("class A { public: A *a; };", TypeA)); |
| 489 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 490 | |
| 491 | TypeMatcher TypeDerivedFromA = hasDeclaration(record(isDerivedFrom("A"))); |
| 492 | |
| 493 | EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };", |
| 494 | TypeDerivedFromA)); |
| 495 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 496 | |
| 497 | TypeMatcher TypeAHasClassB = hasDeclaration( |
| 498 | record(hasName("A"), has(record(hasName("B"))))); |
| 499 | |
| 500 | EXPECT_TRUE( |
| 501 | matches("class A { public: A *a; class B {}; };", TypeAHasClassB)); |
| 502 | } |
| 503 | |
| 504 | // Returns from Run whether 'bound_nodes' contain a Decl bound to 'Id', which |
| 505 | // can be dynamically casted to T. |
| 506 | // Optionally checks that the check succeeded a specific number of times. |
| 507 | template <typename T> |
| 508 | class VerifyIdIsBoundToDecl : public BoundNodesCallback { |
| 509 | public: |
| 510 | // Create an object that checks that a node of type 'T' was bound to 'Id'. |
| 511 | // Does not check for a certain number of matches. |
| 512 | explicit VerifyIdIsBoundToDecl(const std::string& Id) |
| 513 | : Id(Id), ExpectedCount(-1), Count(0) {} |
| 514 | |
| 515 | // Create an object that checks that a node of type 'T' was bound to 'Id'. |
| 516 | // Checks that there were exactly 'ExpectedCount' matches. |
| 517 | explicit VerifyIdIsBoundToDecl(const std::string& Id, int ExpectedCount) |
| 518 | : Id(Id), ExpectedCount(ExpectedCount), Count(0) {} |
| 519 | |
| 520 | ~VerifyIdIsBoundToDecl() { |
| 521 | if (ExpectedCount != -1) { |
| 522 | EXPECT_EQ(ExpectedCount, Count); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | virtual bool run(const BoundNodes *Nodes) { |
| 527 | if (Nodes->getDeclAs<T>(Id) != NULL) { |
| 528 | ++Count; |
| 529 | return true; |
| 530 | } |
| 531 | return false; |
| 532 | } |
| 533 | |
| 534 | private: |
| 535 | const std::string Id; |
| 536 | const int ExpectedCount; |
| 537 | int Count; |
| 538 | }; |
| 539 | template <typename T> |
| 540 | class VerifyIdIsBoundToStmt : public BoundNodesCallback { |
| 541 | public: |
| 542 | explicit VerifyIdIsBoundToStmt(const std::string &Id) : Id(Id) {} |
| 543 | virtual bool run(const BoundNodes *Nodes) { |
| 544 | const T *Node = Nodes->getStmtAs<T>(Id); |
| 545 | return Node != NULL; |
| 546 | } |
| 547 | private: |
| 548 | const std::string Id; |
| 549 | }; |
| 550 | |
| 551 | TEST(Matcher, BindMatchedNodes) { |
| 552 | DeclarationMatcher ClassX = has(id("x", record(hasName("X")))); |
| 553 | |
| 554 | EXPECT_TRUE(matchAndVerifyResultTrue("class X {};", |
| 555 | ClassX, new VerifyIdIsBoundToDecl<clang::CXXRecordDecl>("x"))); |
| 556 | |
| 557 | EXPECT_TRUE(matchAndVerifyResultFalse("class X {};", |
| 558 | ClassX, new VerifyIdIsBoundToDecl<clang::CXXRecordDecl>("other-id"))); |
| 559 | |
| 560 | TypeMatcher TypeAHasClassB = hasDeclaration( |
| 561 | record(hasName("A"), has(id("b", record(hasName("B")))))); |
| 562 | |
| 563 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };", |
| 564 | TypeAHasClassB, |
| 565 | new VerifyIdIsBoundToDecl<clang::Decl>("b"))); |
| 566 | |
| 567 | StatementMatcher MethodX = id("x", call(callee(method(hasName("x"))))); |
| 568 | |
| 569 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };", |
| 570 | MethodX, |
| 571 | new VerifyIdIsBoundToStmt<clang::CXXMemberCallExpr>("x"))); |
| 572 | } |
| 573 | |
| 574 | TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
| 575 | TypeMatcher ClassX = hasDeclaration(record(hasName("X"))); |
| 576 | EXPECT_TRUE( |
| 577 | matches("class X {}; void y(X &x) { x; }", expression(hasType(ClassX)))); |
| 578 | EXPECT_TRUE( |
| 579 | notMatches("class X {}; void y(X *x) { x; }", |
| 580 | expression(hasType(ClassX)))); |
| 581 | EXPECT_TRUE( |
| 582 | matches("class X {}; void y(X *x) { x; }", |
| 583 | expression(hasType(pointsTo(ClassX))))); |
| 584 | } |
| 585 | |
| 586 | TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
| 587 | TypeMatcher ClassX = hasDeclaration(record(hasName("X"))); |
| 588 | EXPECT_TRUE( |
| 589 | matches("class X {}; void y() { X x; }", variable(hasType(ClassX)))); |
| 590 | EXPECT_TRUE( |
| 591 | notMatches("class X {}; void y() { X *x; }", variable(hasType(ClassX)))); |
| 592 | EXPECT_TRUE( |
| 593 | matches("class X {}; void y() { X *x; }", |
| 594 | variable(hasType(pointsTo(ClassX))))); |
| 595 | } |
| 596 | |
| 597 | TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
| 598 | DeclarationMatcher ClassX = record(hasName("X")); |
| 599 | EXPECT_TRUE( |
| 600 | matches("class X {}; void y(X &x) { x; }", expression(hasType(ClassX)))); |
| 601 | EXPECT_TRUE( |
| 602 | notMatches("class X {}; void y(X *x) { x; }", |
| 603 | expression(hasType(ClassX)))); |
| 604 | } |
| 605 | |
| 606 | TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
| 607 | DeclarationMatcher ClassX = record(hasName("X")); |
| 608 | EXPECT_TRUE( |
| 609 | matches("class X {}; void y() { X x; }", variable(hasType(ClassX)))); |
| 610 | EXPECT_TRUE( |
| 611 | notMatches("class X {}; void y() { X *x; }", variable(hasType(ClassX)))); |
| 612 | } |
| 613 | |
| 614 | TEST(Matcher, Call) { |
| 615 | // FIXME: Do we want to overload Call() to directly take |
| 616 | // Matcher<clang::Decl>, too? |
| 617 | StatementMatcher MethodX = call(hasDeclaration(method(hasName("x")))); |
| 618 | |
| 619 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 620 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 621 | |
| 622 | StatementMatcher MethodOnY = call(on(hasType(record(hasName("Y"))))); |
| 623 | |
| 624 | EXPECT_TRUE( |
| 625 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 626 | MethodOnY)); |
| 627 | EXPECT_TRUE( |
| 628 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 629 | MethodOnY)); |
| 630 | EXPECT_TRUE( |
| 631 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 632 | MethodOnY)); |
| 633 | EXPECT_TRUE( |
| 634 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 635 | MethodOnY)); |
| 636 | EXPECT_TRUE( |
| 637 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 638 | MethodOnY)); |
| 639 | |
| 640 | StatementMatcher MethodOnYPointer = |
| 641 | call(on(hasType(pointsTo(record(hasName("Y")))))); |
| 642 | |
| 643 | EXPECT_TRUE( |
| 644 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 645 | MethodOnYPointer)); |
| 646 | EXPECT_TRUE( |
| 647 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 648 | MethodOnYPointer)); |
| 649 | EXPECT_TRUE( |
| 650 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 651 | MethodOnYPointer)); |
| 652 | EXPECT_TRUE( |
| 653 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 654 | MethodOnYPointer)); |
| 655 | EXPECT_TRUE( |
| 656 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 657 | MethodOnYPointer)); |
| 658 | } |
| 659 | |
| 660 | TEST(Matcher, OverloadedOperatorCall) { |
| 661 | StatementMatcher OpCall = overloadedOperatorCall(); |
| 662 | // Unary operator |
| 663 | EXPECT_TRUE(matches("class Y { }; " |
| 664 | "bool operator!(Y x) { return false; }; " |
| 665 | "Y y; bool c = !y;", OpCall)); |
| 666 | // No match -- special operators like "new", "delete" |
| 667 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
| 668 | // we need to figure out include paths in the test. |
| 669 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
| 670 | // "class Y { }; " |
| 671 | // "void *operator new(size_t size) { return 0; } " |
| 672 | // "Y *y = new Y;", OpCall)); |
| 673 | EXPECT_TRUE(notMatches("class Y { }; " |
| 674 | "void operator delete(void *p) { } " |
| 675 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 676 | // Binary operator |
| 677 | EXPECT_TRUE(matches("class Y { }; " |
| 678 | "bool operator&&(Y x, Y y) { return true; }; " |
| 679 | "Y a; Y b; bool c = a && b;", |
| 680 | OpCall)); |
| 681 | // No match -- normal operator, not an overloaded one. |
| 682 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 683 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 684 | } |
| 685 | |
| 686 | TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { |
| 687 | StatementMatcher OpCallAndAnd = |
| 688 | overloadedOperatorCall(hasOverloadedOperatorName("&&")); |
| 689 | EXPECT_TRUE(matches("class Y { }; " |
| 690 | "bool operator&&(Y x, Y y) { return true; }; " |
| 691 | "Y a; Y b; bool c = a && b;", OpCallAndAnd)); |
| 692 | StatementMatcher OpCallLessLess = |
| 693 | overloadedOperatorCall(hasOverloadedOperatorName("<<")); |
| 694 | EXPECT_TRUE(notMatches("class Y { }; " |
| 695 | "bool operator&&(Y x, Y y) { return true; }; " |
| 696 | "Y a; Y b; bool c = a && b;", |
| 697 | OpCallLessLess)); |
| 698 | } |
| 699 | |
| 700 | TEST(Matcher, ThisPointerType) { |
| 701 | StatementMatcher MethodOnY = call(thisPointerType(record(hasName("Y")))); |
| 702 | |
| 703 | EXPECT_TRUE( |
| 704 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 705 | MethodOnY)); |
| 706 | EXPECT_TRUE( |
| 707 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 708 | MethodOnY)); |
| 709 | EXPECT_TRUE( |
| 710 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 711 | MethodOnY)); |
| 712 | EXPECT_TRUE( |
| 713 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 714 | MethodOnY)); |
| 715 | EXPECT_TRUE( |
| 716 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 717 | MethodOnY)); |
| 718 | |
| 719 | EXPECT_TRUE(matches( |
| 720 | "class Y {" |
| 721 | " public: virtual void x();" |
| 722 | "};" |
| 723 | "class X : public Y {" |
| 724 | " public: virtual void x();" |
| 725 | "};" |
| 726 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 727 | } |
| 728 | |
| 729 | TEST(Matcher, VariableUsage) { |
| 730 | StatementMatcher Reference = |
| 731 | declarationReference(to( |
| 732 | variable(hasInitializer( |
| 733 | call(thisPointerType(record(hasName("Y")))))))); |
| 734 | |
| 735 | EXPECT_TRUE(matches( |
| 736 | "class Y {" |
| 737 | " public:" |
| 738 | " bool x() const;" |
| 739 | "};" |
| 740 | "void z(const Y &y) {" |
| 741 | " bool b = y.x();" |
| 742 | " if (b) {}" |
| 743 | "}", Reference)); |
| 744 | |
| 745 | EXPECT_TRUE(notMatches( |
| 746 | "class Y {" |
| 747 | " public:" |
| 748 | " bool x() const;" |
| 749 | "};" |
| 750 | "void z(const Y &y) {" |
| 751 | " bool b = y.x();" |
| 752 | "}", Reference)); |
| 753 | } |
| 754 | |
| 755 | TEST(Matcher, CalledVariable) { |
| 756 | StatementMatcher CallOnVariableY = expression( |
| 757 | call(on(declarationReference(to(variable(hasName("y"))))))); |
| 758 | |
| 759 | EXPECT_TRUE(matches( |
| 760 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 761 | EXPECT_TRUE(matches( |
| 762 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 763 | EXPECT_TRUE(matches( |
| 764 | "class Y { public: void x(); };" |
| 765 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 766 | EXPECT_TRUE(matches( |
| 767 | "class Y { public: void x(); };" |
| 768 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 769 | EXPECT_TRUE(notMatches( |
| 770 | "class Y { public: void x(); };" |
| 771 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 772 | CallOnVariableY)); |
| 773 | } |
| 774 | |
| 775 | TEST(MemberExpression, DoesNotMatchClasses) { |
| 776 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpression())); |
| 777 | } |
| 778 | |
| 779 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
| 780 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpression())); |
| 781 | } |
| 782 | |
| 783 | TEST(MemberExpression, MatchesVariable) { |
| 784 | EXPECT_TRUE( |
| 785 | matches("class Y { void x() { this->y; } int y; };", memberExpression())); |
| 786 | EXPECT_TRUE( |
| 787 | matches("class Y { void x() { y; } int y; };", memberExpression())); |
| 788 | EXPECT_TRUE( |
| 789 | matches("class Y { void x() { Y y; y.y; } int y; };", |
| 790 | memberExpression())); |
| 791 | } |
| 792 | |
| 793 | TEST(MemberExpression, MatchesStaticVariable) { |
| 794 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
| 795 | memberExpression())); |
| 796 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
| 797 | memberExpression())); |
| 798 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
| 799 | memberExpression())); |
| 800 | } |
| 801 | |
| 802 | TEST(IsArrow, MatchesMemberVariablesViaArrow) { |
| 803 | EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
| 804 | memberExpression(isArrow()))); |
| 805 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };", |
| 806 | memberExpression(isArrow()))); |
| 807 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
| 808 | memberExpression(isArrow()))); |
| 809 | } |
| 810 | |
| 811 | TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) { |
| 812 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
| 813 | memberExpression(isArrow()))); |
| 814 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
| 815 | memberExpression(isArrow()))); |
| 816 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
| 817 | memberExpression(isArrow()))); |
| 818 | } |
| 819 | |
| 820 | TEST(IsArrow, MatchesMemberCallsViaArrow) { |
| 821 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
| 822 | memberExpression(isArrow()))); |
| 823 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", |
| 824 | memberExpression(isArrow()))); |
| 825 | EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
| 826 | memberExpression(isArrow()))); |
| 827 | } |
| 828 | |
| 829 | TEST(Callee, MatchesDeclarations) { |
| 830 | StatementMatcher CallMethodX = call(callee(method(hasName("x")))); |
| 831 | |
| 832 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 833 | EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 834 | } |
| 835 | |
| 836 | TEST(Callee, MatchesMemberExpressions) { |
| 837 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
| 838 | call(callee(memberExpression())))); |
| 839 | EXPECT_TRUE( |
| 840 | notMatches("class Y { void x() { this->x(); } };", call(callee(call())))); |
| 841 | } |
| 842 | |
| 843 | TEST(Function, MatchesFunctionDeclarations) { |
| 844 | StatementMatcher CallFunctionF = call(callee(function(hasName("f")))); |
| 845 | |
| 846 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 847 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 848 | |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 849 | #if !defined(_MSC_VER) |
| 850 | // FIXME: Make this work for MSVC. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 851 | // Dependent contexts, but a non-dependent call. |
| 852 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 853 | CallFunctionF)); |
| 854 | EXPECT_TRUE( |
| 855 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 856 | CallFunctionF)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 857 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 858 | |
| 859 | // Depedent calls don't match. |
| 860 | EXPECT_TRUE( |
| 861 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 862 | CallFunctionF)); |
| 863 | EXPECT_TRUE( |
| 864 | notMatches("void f(int);" |
| 865 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 866 | CallFunctionF)); |
| 867 | } |
| 868 | |
| 869 | TEST(Matcher, Argument) { |
| 870 | StatementMatcher CallArgumentY = expression(call( |
| 871 | hasArgument(0, declarationReference(to(variable(hasName("y"))))))); |
| 872 | |
| 873 | EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 874 | EXPECT_TRUE( |
| 875 | matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 876 | EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 877 | |
| 878 | StatementMatcher WrongIndex = expression(call( |
| 879 | hasArgument(42, declarationReference(to(variable(hasName("y"))))))); |
| 880 | EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 881 | } |
| 882 | |
| 883 | TEST(Matcher, AnyArgument) { |
| 884 | StatementMatcher CallArgumentY = expression(call( |
| 885 | hasAnyArgument(declarationReference(to(variable(hasName("y"))))))); |
| 886 | EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 887 | EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 888 | EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 889 | } |
| 890 | |
| 891 | TEST(Matcher, ArgumentCount) { |
| 892 | StatementMatcher Call1Arg = expression(call(argumentCountIs(1))); |
| 893 | |
| 894 | EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 895 | EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 896 | EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 897 | } |
| 898 | |
| 899 | TEST(Matcher, References) { |
| 900 | DeclarationMatcher ReferenceClassX = variable( |
| 901 | hasType(references(record(hasName("X"))))); |
| 902 | EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }", |
| 903 | ReferenceClassX)); |
| 904 | EXPECT_TRUE( |
| 905 | matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
| 906 | EXPECT_TRUE( |
| 907 | notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 908 | EXPECT_TRUE( |
| 909 | notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 910 | } |
| 911 | |
| 912 | TEST(HasParameter, CallsInnerMatcher) { |
| 913 | EXPECT_TRUE(matches("class X { void x(int) {} };", |
| 914 | method(hasParameter(0, variable())))); |
| 915 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
| 916 | method(hasParameter(0, hasName("x"))))); |
| 917 | } |
| 918 | |
| 919 | TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 920 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
| 921 | method(hasParameter(42, variable())))); |
| 922 | } |
| 923 | |
| 924 | TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 925 | EXPECT_TRUE(matches("class X { void x(X x) {} };", |
| 926 | method(hasParameter(0, hasType(record(hasName("X"))))))); |
| 927 | EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };", |
| 928 | method(hasParameter(0, hasType(record(hasName("X"))))))); |
| 929 | EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
| 930 | method(hasParameter(0, hasType(pointsTo(record(hasName("X")))))))); |
| 931 | EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
| 932 | method(hasParameter(0, hasType(references(record(hasName("X")))))))); |
| 933 | } |
| 934 | |
| 935 | TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 936 | EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };", |
| 937 | method(hasAnyParameter(hasType(record(hasName("X"))))))); |
| 938 | EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };", |
| 939 | method(hasAnyParameter(hasType(record(hasName("X"))))))); |
| 940 | } |
| 941 | |
| 942 | TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 943 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
| 944 | method(hasAnyParameter(hasType(record(hasName("X"))))))); |
| 945 | } |
| 946 | |
| 947 | TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 948 | EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
| 949 | method(hasAnyParameter(hasType(pointsTo(record(hasName("X")))))))); |
| 950 | } |
| 951 | |
| 952 | TEST(HasName, MatchesParameterVariableDeclartions) { |
| 953 | EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
| 954 | method(hasAnyParameter(hasName("x"))))); |
| 955 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
| 956 | method(hasAnyParameter(hasName("x"))))); |
| 957 | } |
| 958 | |
| 959 | TEST(Matcher, ConstructorCall) { |
| 960 | StatementMatcher Constructor = expression(constructorCall()); |
| 961 | |
| 962 | EXPECT_TRUE( |
| 963 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 964 | EXPECT_TRUE( |
| 965 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 966 | Constructor)); |
| 967 | EXPECT_TRUE( |
| 968 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 969 | Constructor)); |
| 970 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 971 | } |
| 972 | |
| 973 | TEST(Matcher, ConstructorArgument) { |
| 974 | StatementMatcher Constructor = expression(constructorCall( |
| 975 | hasArgument(0, declarationReference(to(variable(hasName("y"))))))); |
| 976 | |
| 977 | EXPECT_TRUE( |
| 978 | matches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 979 | Constructor)); |
| 980 | EXPECT_TRUE( |
| 981 | matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 982 | Constructor)); |
| 983 | EXPECT_TRUE( |
| 984 | matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 985 | Constructor)); |
| 986 | EXPECT_TRUE( |
| 987 | notMatches("class X { public: X(int); }; void x() { int z; X x(z); }", |
| 988 | Constructor)); |
| 989 | |
| 990 | StatementMatcher WrongIndex = expression(constructorCall( |
| 991 | hasArgument(42, declarationReference(to(variable(hasName("y"))))))); |
| 992 | EXPECT_TRUE( |
| 993 | notMatches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 994 | WrongIndex)); |
| 995 | } |
| 996 | |
| 997 | TEST(Matcher, ConstructorArgumentCount) { |
| 998 | StatementMatcher Constructor1Arg = |
| 999 | expression(constructorCall(argumentCountIs(1))); |
| 1000 | |
| 1001 | EXPECT_TRUE( |
| 1002 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1003 | Constructor1Arg)); |
| 1004 | EXPECT_TRUE( |
| 1005 | matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 1006 | Constructor1Arg)); |
| 1007 | EXPECT_TRUE( |
| 1008 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1009 | Constructor1Arg)); |
| 1010 | EXPECT_TRUE( |
| 1011 | notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 1012 | Constructor1Arg)); |
| 1013 | } |
| 1014 | |
| 1015 | TEST(Matcher, BindTemporaryExpression) { |
| 1016 | StatementMatcher TempExpression = expression(bindTemporaryExpression()); |
| 1017 | |
| 1018 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 1019 | |
| 1020 | EXPECT_TRUE( |
| 1021 | matches(ClassString + |
| 1022 | "string GetStringByValue();" |
| 1023 | "void FunctionTakesString(string s);" |
| 1024 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1025 | TempExpression)); |
| 1026 | |
| 1027 | EXPECT_TRUE( |
| 1028 | notMatches(ClassString + |
| 1029 | "string* GetStringPointer(); " |
| 1030 | "void FunctionTakesStringPtr(string* s);" |
| 1031 | "void run() {" |
| 1032 | " string* s = GetStringPointer();" |
| 1033 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1034 | " FunctionTakesStringPtr(s);" |
| 1035 | "}", |
| 1036 | TempExpression)); |
| 1037 | |
| 1038 | EXPECT_TRUE( |
| 1039 | notMatches("class no_dtor {};" |
| 1040 | "no_dtor GetObjByValue();" |
| 1041 | "void ConsumeObj(no_dtor param);" |
| 1042 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 1043 | TempExpression)); |
| 1044 | } |
| 1045 | |
| 1046 | TEST(ConstructorDeclaration, SimpleCase) { |
| 1047 | EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
| 1048 | constructor(ofClass(hasName("Foo"))))); |
| 1049 | EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
| 1050 | constructor(ofClass(hasName("Bar"))))); |
| 1051 | } |
| 1052 | |
| 1053 | TEST(ConstructorDeclaration, IsImplicit) { |
| 1054 | // This one doesn't match because the constructor is not added by the |
| 1055 | // compiler (it is not needed). |
| 1056 | EXPECT_TRUE(notMatches("class Foo { };", |
| 1057 | constructor(isImplicit()))); |
| 1058 | // The compiler added the implicit default constructor. |
| 1059 | EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
| 1060 | constructor(isImplicit()))); |
| 1061 | EXPECT_TRUE(matches("class Foo { Foo(){} };", |
| 1062 | constructor(unless(isImplicit())))); |
| 1063 | } |
| 1064 | |
| 1065 | TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1066 | EXPECT_TRUE(notMatches( |
| 1067 | "class Foo { Foo() { } };", |
| 1068 | constructor(hasAnyConstructorInitializer(anything())))); |
| 1069 | EXPECT_TRUE(matches( |
| 1070 | "class Foo {" |
| 1071 | " Foo() : foo_() { }" |
| 1072 | " int foo_;" |
| 1073 | "};", |
| 1074 | constructor(hasAnyConstructorInitializer(anything())))); |
| 1075 | } |
| 1076 | |
| 1077 | TEST(HasAnyConstructorInitializer, ForField) { |
| 1078 | static const char Code[] = |
| 1079 | "class Baz { };" |
| 1080 | "class Foo {" |
| 1081 | " Foo() : foo_() { }" |
| 1082 | " Baz foo_;" |
| 1083 | " Baz bar_;" |
| 1084 | "};"; |
| 1085 | EXPECT_TRUE(matches(Code, constructor(hasAnyConstructorInitializer( |
| 1086 | forField(hasType(record(hasName("Baz")))))))); |
| 1087 | EXPECT_TRUE(matches(Code, constructor(hasAnyConstructorInitializer( |
| 1088 | forField(hasName("foo_")))))); |
| 1089 | EXPECT_TRUE(notMatches(Code, constructor(hasAnyConstructorInitializer( |
| 1090 | forField(hasType(record(hasName("Bar")))))))); |
| 1091 | } |
| 1092 | |
| 1093 | TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1094 | static const char Code[] = |
| 1095 | "class Foo {" |
| 1096 | " Foo() : foo_(0) { }" |
| 1097 | " int foo_;" |
| 1098 | "};"; |
| 1099 | EXPECT_TRUE(matches(Code, constructor(hasAnyConstructorInitializer( |
| 1100 | withInitializer(integerLiteral(equals(0))))))); |
| 1101 | EXPECT_TRUE(notMatches(Code, constructor(hasAnyConstructorInitializer( |
| 1102 | withInitializer(integerLiteral(equals(1))))))); |
| 1103 | } |
| 1104 | |
| 1105 | TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1106 | static const char Code[] = |
| 1107 | "struct Bar { Bar(){} };" |
| 1108 | "class Foo {" |
| 1109 | " Foo() : foo_() { }" |
| 1110 | " Bar foo_;" |
| 1111 | " Bar bar_;" |
| 1112 | "};"; |
| 1113 | EXPECT_TRUE(matches(Code, constructor(hasAnyConstructorInitializer( |
| 1114 | allOf(forField(hasName("foo_")), isWritten()))))); |
| 1115 | EXPECT_TRUE(notMatches(Code, constructor(hasAnyConstructorInitializer( |
| 1116 | allOf(forField(hasName("bar_")), isWritten()))))); |
| 1117 | EXPECT_TRUE(matches(Code, constructor(hasAnyConstructorInitializer( |
| 1118 | allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1119 | } |
| 1120 | |
| 1121 | TEST(Matcher, NewExpression) { |
| 1122 | StatementMatcher New = expression(newExpression()); |
| 1123 | |
| 1124 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 1125 | EXPECT_TRUE( |
| 1126 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 1127 | EXPECT_TRUE( |
| 1128 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1129 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 1130 | } |
| 1131 | |
| 1132 | TEST(Matcher, NewExpressionArgument) { |
| 1133 | StatementMatcher New = expression(constructorCall( |
| 1134 | hasArgument( |
| 1135 | 0, declarationReference(to(variable(hasName("y"))))))); |
| 1136 | |
| 1137 | EXPECT_TRUE( |
| 1138 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1139 | New)); |
| 1140 | EXPECT_TRUE( |
| 1141 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1142 | New)); |
| 1143 | EXPECT_TRUE( |
| 1144 | notMatches("class X { public: X(int); }; void x() { int z; new X(z); }", |
| 1145 | New)); |
| 1146 | |
| 1147 | StatementMatcher WrongIndex = expression(constructorCall( |
| 1148 | hasArgument( |
| 1149 | 42, declarationReference(to(variable(hasName("y"))))))); |
| 1150 | EXPECT_TRUE( |
| 1151 | notMatches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1152 | WrongIndex)); |
| 1153 | } |
| 1154 | |
| 1155 | TEST(Matcher, NewExpressionArgumentCount) { |
| 1156 | StatementMatcher New = constructorCall(argumentCountIs(1)); |
| 1157 | |
| 1158 | EXPECT_TRUE( |
| 1159 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1160 | EXPECT_TRUE( |
| 1161 | notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }", |
| 1162 | New)); |
| 1163 | } |
| 1164 | |
| 1165 | TEST(Matcher, DefaultArgument) { |
| 1166 | StatementMatcher Arg = defaultArgument(); |
| 1167 | |
| 1168 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 1169 | EXPECT_TRUE( |
| 1170 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 1171 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 1172 | } |
| 1173 | |
| 1174 | TEST(Matcher, StringLiterals) { |
| 1175 | StatementMatcher Literal = expression(stringLiteral()); |
| 1176 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 1177 | // wide string |
| 1178 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 1179 | // with escaped characters |
| 1180 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 1181 | // no matching -- though the data type is the same, there is no string literal |
| 1182 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 1183 | } |
| 1184 | |
| 1185 | TEST(Matcher, CharacterLiterals) { |
| 1186 | StatementMatcher CharLiteral = expression(characterLiteral()); |
| 1187 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 1188 | // wide character |
| 1189 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 1190 | // wide character, Hex encoded, NOT MATCHED! |
| 1191 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 1192 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 1193 | } |
| 1194 | |
| 1195 | TEST(Matcher, IntegerLiterals) { |
| 1196 | StatementMatcher HasIntLiteral = expression(integerLiteral()); |
| 1197 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 1198 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 1199 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 1200 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 1201 | |
| 1202 | // Non-matching cases (character literals, float and double) |
| 1203 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 1204 | HasIntLiteral)); // this is actually a character |
| 1205 | // literal cast to int |
| 1206 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 1207 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 1208 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 1209 | } |
| 1210 | |
| 1211 | TEST(Matcher, Conditions) { |
| 1212 | StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true)))); |
| 1213 | |
| 1214 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 1215 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 1216 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 1217 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 1218 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 1219 | } |
| 1220 | |
| 1221 | TEST(MatchBinaryOperator, HasOperatorName) { |
| 1222 | StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1223 | |
| 1224 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1225 | EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1226 | } |
| 1227 | |
| 1228 | TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1229 | StatementMatcher OperatorTrueFalse = |
| 1230 | binaryOperator(hasLHS(boolLiteral(equals(true))), |
| 1231 | hasRHS(boolLiteral(equals(false)))); |
| 1232 | |
| 1233 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1234 | EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1235 | EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1236 | } |
| 1237 | |
| 1238 | TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1239 | StatementMatcher HasOperand = |
| 1240 | binaryOperator(hasEitherOperand(boolLiteral(equals(false)))); |
| 1241 | |
| 1242 | EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 1243 | EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 1244 | EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 1245 | } |
| 1246 | |
| 1247 | TEST(Matcher, BinaryOperatorTypes) { |
| 1248 | // Integration test that verifies the AST provides all binary operators in |
| 1249 | // a way we expect. |
| 1250 | // FIXME: Operator ',' |
| 1251 | EXPECT_TRUE( |
| 1252 | matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 1253 | EXPECT_TRUE( |
| 1254 | matches("bool b; bool c = (b = true);", |
| 1255 | binaryOperator(hasOperatorName("=")))); |
| 1256 | EXPECT_TRUE( |
| 1257 | matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 1258 | EXPECT_TRUE( |
| 1259 | matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 1260 | EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 1261 | EXPECT_TRUE( |
| 1262 | matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 1263 | EXPECT_TRUE( |
| 1264 | matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 1265 | EXPECT_TRUE( |
| 1266 | matches("int i = 1; int j = (i <<= 2);", |
| 1267 | binaryOperator(hasOperatorName("<<=")))); |
| 1268 | EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 1269 | EXPECT_TRUE( |
| 1270 | matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 1271 | EXPECT_TRUE( |
| 1272 | matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 1273 | EXPECT_TRUE( |
| 1274 | matches("int i = 1; int j = (i >>= 2);", |
| 1275 | binaryOperator(hasOperatorName(">>=")))); |
| 1276 | EXPECT_TRUE( |
| 1277 | matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 1278 | EXPECT_TRUE( |
| 1279 | matches("int i = 42; int j = (i ^= 42);", |
| 1280 | binaryOperator(hasOperatorName("^=")))); |
| 1281 | EXPECT_TRUE( |
| 1282 | matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 1283 | EXPECT_TRUE( |
| 1284 | matches("int i = 42; int j = (i %= 42);", |
| 1285 | binaryOperator(hasOperatorName("%=")))); |
| 1286 | EXPECT_TRUE( |
| 1287 | matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 1288 | EXPECT_TRUE( |
| 1289 | matches("bool b = true && false;", |
| 1290 | binaryOperator(hasOperatorName("&&")))); |
| 1291 | EXPECT_TRUE( |
| 1292 | matches("bool b = true; bool c = (b &= false);", |
| 1293 | binaryOperator(hasOperatorName("&=")))); |
| 1294 | EXPECT_TRUE( |
| 1295 | matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 1296 | EXPECT_TRUE( |
| 1297 | matches("bool b = true || false;", |
| 1298 | binaryOperator(hasOperatorName("||")))); |
| 1299 | EXPECT_TRUE( |
| 1300 | matches("bool b = true; bool c = (b |= false);", |
| 1301 | binaryOperator(hasOperatorName("|=")))); |
| 1302 | EXPECT_TRUE( |
| 1303 | matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 1304 | EXPECT_TRUE( |
| 1305 | matches("int i = 42; int j = (i *= 23);", |
| 1306 | binaryOperator(hasOperatorName("*=")))); |
| 1307 | EXPECT_TRUE( |
| 1308 | matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 1309 | EXPECT_TRUE( |
| 1310 | matches("int i = 42; int j = (i /= 23);", |
| 1311 | binaryOperator(hasOperatorName("/=")))); |
| 1312 | EXPECT_TRUE( |
| 1313 | matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 1314 | EXPECT_TRUE( |
| 1315 | matches("int i = 42; int j = (i += 23);", |
| 1316 | binaryOperator(hasOperatorName("+=")))); |
| 1317 | EXPECT_TRUE( |
| 1318 | matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 1319 | EXPECT_TRUE( |
| 1320 | matches("int i = 42; int j = (i -= 23);", |
| 1321 | binaryOperator(hasOperatorName("-=")))); |
| 1322 | EXPECT_TRUE( |
| 1323 | matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 1324 | binaryOperator(hasOperatorName("->*")))); |
| 1325 | EXPECT_TRUE( |
| 1326 | matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 1327 | binaryOperator(hasOperatorName(".*")))); |
| 1328 | |
| 1329 | // Member expressions as operators are not supported in matches. |
| 1330 | EXPECT_TRUE( |
| 1331 | notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 1332 | binaryOperator(hasOperatorName("->")))); |
| 1333 | |
| 1334 | // Initializer assignments are not represented as operator equals. |
| 1335 | EXPECT_TRUE( |
| 1336 | notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 1337 | |
| 1338 | // Array indexing is not represented as operator. |
| 1339 | EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 1340 | |
| 1341 | // Overloaded operators do not match at all. |
| 1342 | EXPECT_TRUE(notMatches( |
| 1343 | "struct A { bool operator&&(const A &a) const { return false; } };" |
| 1344 | "void x() { A a, b; a && b; }", |
| 1345 | binaryOperator())); |
| 1346 | } |
| 1347 | |
| 1348 | TEST(MatchUnaryOperator, HasOperatorName) { |
| 1349 | StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 1350 | |
| 1351 | EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 1352 | EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 1353 | } |
| 1354 | |
| 1355 | TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 1356 | StatementMatcher OperatorOnFalse = |
| 1357 | unaryOperator(hasUnaryOperand(boolLiteral(equals(false)))); |
| 1358 | |
| 1359 | EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 1360 | EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 1361 | } |
| 1362 | |
| 1363 | TEST(Matcher, UnaryOperatorTypes) { |
| 1364 | // Integration test that verifies the AST provides all unary operators in |
| 1365 | // a way we expect. |
| 1366 | EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 1367 | EXPECT_TRUE( |
| 1368 | matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 1369 | EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 1370 | EXPECT_TRUE( |
| 1371 | matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 1372 | EXPECT_TRUE( |
| 1373 | matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 1374 | EXPECT_TRUE( |
| 1375 | matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 1376 | EXPECT_TRUE( |
| 1377 | matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 1378 | EXPECT_TRUE( |
| 1379 | matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 1380 | EXPECT_TRUE( |
| 1381 | matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 1382 | EXPECT_TRUE( |
| 1383 | matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 1384 | |
| 1385 | // We don't match conversion operators. |
| 1386 | EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 1387 | |
| 1388 | // Function calls are not represented as operator. |
| 1389 | EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 1390 | |
| 1391 | // Overloaded operators do not match at all. |
| 1392 | // FIXME: We probably want to add that. |
| 1393 | EXPECT_TRUE(notMatches( |
| 1394 | "struct A { bool operator!() const { return false; } };" |
| 1395 | "void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 1396 | } |
| 1397 | |
| 1398 | TEST(Matcher, ConditionalOperator) { |
| 1399 | StatementMatcher Conditional = conditionalOperator( |
| 1400 | hasCondition(boolLiteral(equals(true))), |
| 1401 | hasTrueExpression(boolLiteral(equals(false)))); |
| 1402 | |
| 1403 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 1404 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 1405 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 1406 | |
| 1407 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 1408 | hasFalseExpression(boolLiteral(equals(false)))); |
| 1409 | |
| 1410 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 1411 | EXPECT_TRUE( |
| 1412 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 1413 | } |
| 1414 | |
| 1415 | TEST(Matcher, HasNameSupportsNamespaces) { |
| 1416 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 1417 | record(hasName("a::b::C")))); |
| 1418 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 1419 | record(hasName("::a::b::C")))); |
| 1420 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 1421 | record(hasName("b::C")))); |
| 1422 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
| 1423 | record(hasName("C")))); |
| 1424 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1425 | record(hasName("c::b::C")))); |
| 1426 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1427 | record(hasName("a::c::C")))); |
| 1428 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1429 | record(hasName("a::b::A")))); |
| 1430 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1431 | record(hasName("::C")))); |
| 1432 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1433 | record(hasName("::b::C")))); |
| 1434 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1435 | record(hasName("z::a::b::C")))); |
| 1436 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
| 1437 | record(hasName("a+b::C")))); |
| 1438 | EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
| 1439 | record(hasName("C")))); |
| 1440 | } |
| 1441 | |
| 1442 | TEST(Matcher, HasNameSupportsOuterClasses) { |
| 1443 | EXPECT_TRUE( |
| 1444 | matches("class A { class B { class C; }; };", record(hasName("A::B::C")))); |
| 1445 | EXPECT_TRUE( |
| 1446 | matches("class A { class B { class C; }; };", |
| 1447 | record(hasName("::A::B::C")))); |
| 1448 | EXPECT_TRUE( |
| 1449 | matches("class A { class B { class C; }; };", record(hasName("B::C")))); |
| 1450 | EXPECT_TRUE( |
| 1451 | matches("class A { class B { class C; }; };", record(hasName("C")))); |
| 1452 | EXPECT_TRUE( |
| 1453 | notMatches("class A { class B { class C; }; };", |
| 1454 | record(hasName("c::B::C")))); |
| 1455 | EXPECT_TRUE( |
| 1456 | notMatches("class A { class B { class C; }; };", |
| 1457 | record(hasName("A::c::C")))); |
| 1458 | EXPECT_TRUE( |
| 1459 | notMatches("class A { class B { class C; }; };", |
| 1460 | record(hasName("A::B::A")))); |
| 1461 | EXPECT_TRUE( |
| 1462 | notMatches("class A { class B { class C; }; };", record(hasName("::C")))); |
| 1463 | EXPECT_TRUE( |
| 1464 | notMatches("class A { class B { class C; }; };", |
| 1465 | record(hasName("::B::C")))); |
| 1466 | EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
| 1467 | record(hasName("z::A::B::C")))); |
| 1468 | EXPECT_TRUE( |
| 1469 | notMatches("class A { class B { class C; }; };", |
| 1470 | record(hasName("A+B::C")))); |
| 1471 | } |
| 1472 | |
| 1473 | TEST(Matcher, IsDefinition) { |
| 1474 | DeclarationMatcher DefinitionOfClassA = |
| 1475 | record(hasName("A"), isDefinition()); |
| 1476 | EXPECT_TRUE(matches("class A {};", DefinitionOfClassA)); |
| 1477 | EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA)); |
| 1478 | |
| 1479 | DeclarationMatcher DefinitionOfVariableA = |
| 1480 | variable(hasName("a"), isDefinition()); |
| 1481 | EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 1482 | EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 1483 | |
| 1484 | DeclarationMatcher DefinitionOfMethodA = |
| 1485 | method(hasName("a"), isDefinition()); |
| 1486 | EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 1487 | EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 1488 | } |
| 1489 | |
| 1490 | TEST(Matcher, OfClass) { |
| 1491 | StatementMatcher Constructor = constructorCall(hasDeclaration(method( |
| 1492 | ofClass(hasName("X"))))); |
| 1493 | |
| 1494 | EXPECT_TRUE( |
| 1495 | matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 1496 | EXPECT_TRUE( |
| 1497 | matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 1498 | Constructor)); |
| 1499 | EXPECT_TRUE( |
| 1500 | notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 1501 | Constructor)); |
| 1502 | } |
| 1503 | |
| 1504 | TEST(Matcher, VisitsTemplateInstantiations) { |
| 1505 | EXPECT_TRUE(matches( |
| 1506 | "class A { public: void x(); };" |
| 1507 | "template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 1508 | "void f() { B<A> b; b.y(); }", call(callee(method(hasName("x")))))); |
| 1509 | |
| 1510 | EXPECT_TRUE(matches( |
| 1511 | "class A { public: void x(); };" |
| 1512 | "class C {" |
| 1513 | " public:" |
| 1514 | " template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 1515 | "};" |
| 1516 | "void f() {" |
| 1517 | " C::B<A> b; b.y();" |
| 1518 | "}", record(hasName("C"), |
| 1519 | hasDescendant(call(callee(method(hasName("x")))))))); |
| 1520 | } |
| 1521 | |
| 1522 | // For testing AST_MATCHER_P(). |
| 1523 | AST_MATCHER_P(clang::Decl, just, internal::Matcher<clang::Decl>, AMatcher) { |
| 1524 | // Make sure all special variables are used: node, match_finder, |
| 1525 | // bound_nodes_builder, and the parameter named 'AMatcher'. |
| 1526 | return AMatcher.matches(Node, Finder, Builder); |
| 1527 | } |
| 1528 | |
| 1529 | TEST(AstMatcherPMacro, Works) { |
| 1530 | DeclarationMatcher HasClassB = just(has(id("b", record(hasName("B"))))); |
| 1531 | |
| 1532 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
| 1533 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("b"))); |
| 1534 | |
| 1535 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
| 1536 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("a"))); |
| 1537 | |
| 1538 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
| 1539 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("b"))); |
| 1540 | } |
| 1541 | |
| 1542 | AST_POLYMORPHIC_MATCHER_P( |
| 1543 | polymorphicHas, internal::Matcher<clang::Decl>, AMatcher) { |
| 1544 | TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, clang::Decl>::value) || |
| 1545 | (llvm::is_same<NodeType, clang::Stmt>::value), |
| 1546 | assert_node_type_is_accessible); |
| 1547 | internal::TypedBaseMatcher<clang::Decl> ChildMatcher(AMatcher); |
| 1548 | return Finder->matchesChildOf( |
| 1549 | Node, ChildMatcher, Builder, |
| 1550 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
| 1551 | ASTMatchFinder::BK_First); |
| 1552 | } |
| 1553 | |
| 1554 | TEST(AstPolymorphicMatcherPMacro, Works) { |
| 1555 | DeclarationMatcher HasClassB = polymorphicHas(id("b", record(hasName("B")))); |
| 1556 | |
| 1557 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
| 1558 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("b"))); |
| 1559 | |
| 1560 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
| 1561 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("a"))); |
| 1562 | |
| 1563 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
| 1564 | HasClassB, new VerifyIdIsBoundToDecl<clang::Decl>("b"))); |
| 1565 | |
| 1566 | StatementMatcher StatementHasClassB = |
| 1567 | polymorphicHas(record(hasName("B"))); |
| 1568 | |
| 1569 | EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB)); |
| 1570 | } |
| 1571 | |
| 1572 | TEST(For, FindsForLoops) { |
| 1573 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 1574 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
| 1575 | } |
| 1576 | |
| 1577 | TEST(For, ReportsNoFalsePositives) { |
| 1578 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 1579 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 1580 | } |
| 1581 | |
| 1582 | TEST(CompoundStatement, HandlesSimpleCases) { |
| 1583 | EXPECT_TRUE(notMatches("void f();", compoundStatement())); |
| 1584 | EXPECT_TRUE(matches("void f() {}", compoundStatement())); |
| 1585 | EXPECT_TRUE(matches("void f() {{}}", compoundStatement())); |
| 1586 | } |
| 1587 | |
| 1588 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 1589 | // It's not a compound statement just because there's "{}" in the source |
| 1590 | // text. This is an AST search, not grep. |
| 1591 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
| 1592 | compoundStatement())); |
| 1593 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
| 1594 | compoundStatement())); |
| 1595 | } |
| 1596 | |
| 1597 | TEST(HasBody, FindsBodyOfForLoop) { |
| 1598 | StatementMatcher HasCompoundStatementBody = |
| 1599 | forStmt(hasBody(compoundStatement())); |
| 1600 | EXPECT_TRUE(matches("void f() { for(;;) {} }", |
| 1601 | HasCompoundStatementBody)); |
| 1602 | EXPECT_TRUE(notMatches("void f() { for(;;); }", |
| 1603 | HasCompoundStatementBody)); |
| 1604 | } |
| 1605 | |
| 1606 | TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 1607 | // The simplest case: every compound statement is in a function |
| 1608 | // definition, and the function body itself must be a compound |
| 1609 | // statement. |
| 1610 | EXPECT_TRUE(matches("void f() { for (;;); }", |
| 1611 | compoundStatement(hasAnySubstatement(forStmt())))); |
| 1612 | } |
| 1613 | |
| 1614 | TEST(HasAnySubstatement, IsNotRecursive) { |
| 1615 | // It's really "has any immediate substatement". |
| 1616 | EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
| 1617 | compoundStatement(hasAnySubstatement(forStmt())))); |
| 1618 | } |
| 1619 | |
| 1620 | TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 1621 | EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
| 1622 | compoundStatement(hasAnySubstatement(forStmt())))); |
| 1623 | } |
| 1624 | |
| 1625 | TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 1626 | EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
| 1627 | compoundStatement(hasAnySubstatement(forStmt())))); |
| 1628 | } |
| 1629 | |
| 1630 | TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) { |
| 1631 | EXPECT_TRUE(matches("void f() { }", |
| 1632 | compoundStatement(statementCountIs(0)))); |
| 1633 | EXPECT_TRUE(notMatches("void f() {}", |
| 1634 | compoundStatement(statementCountIs(1)))); |
| 1635 | } |
| 1636 | |
| 1637 | TEST(StatementCountIs, AppearsToMatchOnlyOneCount) { |
| 1638 | EXPECT_TRUE(matches("void f() { 1; }", |
| 1639 | compoundStatement(statementCountIs(1)))); |
| 1640 | EXPECT_TRUE(notMatches("void f() { 1; }", |
| 1641 | compoundStatement(statementCountIs(0)))); |
| 1642 | EXPECT_TRUE(notMatches("void f() { 1; }", |
| 1643 | compoundStatement(statementCountIs(2)))); |
| 1644 | } |
| 1645 | |
| 1646 | TEST(StatementCountIs, WorksWithMultipleStatements) { |
| 1647 | EXPECT_TRUE(matches("void f() { 1; 2; 3; }", |
| 1648 | compoundStatement(statementCountIs(3)))); |
| 1649 | } |
| 1650 | |
| 1651 | TEST(StatementCountIs, WorksWithNestedCompoundStatements) { |
| 1652 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 1653 | compoundStatement(statementCountIs(1)))); |
| 1654 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 1655 | compoundStatement(statementCountIs(2)))); |
| 1656 | EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 1657 | compoundStatement(statementCountIs(3)))); |
| 1658 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
| 1659 | compoundStatement(statementCountIs(4)))); |
| 1660 | } |
| 1661 | |
| 1662 | TEST(Member, WorksInSimplestCase) { |
| 1663 | EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
| 1664 | memberExpression(member(hasName("first"))))); |
| 1665 | } |
| 1666 | |
| 1667 | TEST(Member, DoesNotMatchTheBaseExpression) { |
| 1668 | // Don't pick out the wrong part of the member expression, this should |
| 1669 | // be checking the member (name) only. |
| 1670 | EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
| 1671 | memberExpression(member(hasName("first"))))); |
| 1672 | } |
| 1673 | |
| 1674 | TEST(Member, MatchesInMemberFunctionCall) { |
| 1675 | EXPECT_TRUE(matches("void f() {" |
| 1676 | " struct { void first() {}; } s;" |
| 1677 | " s.first();" |
| 1678 | "};", |
| 1679 | memberExpression(member(hasName("first"))))); |
| 1680 | } |
| 1681 | |
| 1682 | TEST(HasObjectExpression, DoesNotMatchMember) { |
| 1683 | EXPECT_TRUE(notMatches( |
| 1684 | "class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
| 1685 | memberExpression(hasObjectExpression(hasType(record(hasName("X"))))))); |
| 1686 | } |
| 1687 | |
| 1688 | TEST(HasObjectExpression, MatchesBaseOfVariable) { |
| 1689 | EXPECT_TRUE(matches( |
| 1690 | "struct X { int m; }; void f(X x) { x.m; }", |
| 1691 | memberExpression(hasObjectExpression(hasType(record(hasName("X"))))))); |
| 1692 | EXPECT_TRUE(matches( |
| 1693 | "struct X { int m; }; void f(X* x) { x->m; }", |
| 1694 | memberExpression(hasObjectExpression( |
| 1695 | hasType(pointsTo(record(hasName("X")))))))); |
| 1696 | } |
| 1697 | |
| 1698 | TEST(HasObjectExpression, |
| 1699 | MatchesObjectExpressionOfImplicitlyFormedMemberExpression) { |
| 1700 | EXPECT_TRUE(matches( |
| 1701 | "class X {}; struct S { X m; void f() { this->m; } };", |
| 1702 | memberExpression(hasObjectExpression( |
| 1703 | hasType(pointsTo(record(hasName("S")))))))); |
| 1704 | EXPECT_TRUE(matches( |
| 1705 | "class X {}; struct S { X m; void f() { m; } };", |
| 1706 | memberExpression(hasObjectExpression( |
| 1707 | hasType(pointsTo(record(hasName("S")))))))); |
| 1708 | } |
| 1709 | |
| 1710 | TEST(Field, DoesNotMatchNonFieldMembers) { |
| 1711 | EXPECT_TRUE(notMatches("class X { void m(); };", field(hasName("m")))); |
| 1712 | EXPECT_TRUE(notMatches("class X { class m {}; };", field(hasName("m")))); |
| 1713 | EXPECT_TRUE(notMatches("class X { enum { m }; };", field(hasName("m")))); |
| 1714 | EXPECT_TRUE(notMatches("class X { enum m {}; };", field(hasName("m")))); |
| 1715 | } |
| 1716 | |
| 1717 | TEST(Field, MatchesField) { |
| 1718 | EXPECT_TRUE(matches("class X { int m; };", field(hasName("m")))); |
| 1719 | } |
| 1720 | |
| 1721 | TEST(IsConstQualified, MatchesConstInt) { |
| 1722 | EXPECT_TRUE(matches("const int i = 42;", |
| 1723 | variable(hasType(isConstQualified())))); |
| 1724 | } |
| 1725 | |
| 1726 | TEST(IsConstQualified, MatchesConstPointer) { |
| 1727 | EXPECT_TRUE(matches("int i = 42; int* const p(&i);", |
| 1728 | variable(hasType(isConstQualified())))); |
| 1729 | } |
| 1730 | |
| 1731 | TEST(IsConstQualified, MatchesThroughTypedef) { |
| 1732 | EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
| 1733 | variable(hasType(isConstQualified())))); |
| 1734 | EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);", |
| 1735 | variable(hasType(isConstQualified())))); |
| 1736 | } |
| 1737 | |
| 1738 | TEST(IsConstQualified, DoesNotMatchInappropriately) { |
| 1739 | EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
| 1740 | variable(hasType(isConstQualified())))); |
| 1741 | EXPECT_TRUE(notMatches("int const* p;", |
| 1742 | variable(hasType(isConstQualified())))); |
| 1743 | } |
| 1744 | |
| 1745 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 1746 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
| 1747 | expression(reinterpretCast()))); |
| 1748 | } |
| 1749 | |
| 1750 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
| 1751 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", |
| 1752 | expression(reinterpretCast()))); |
| 1753 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
| 1754 | expression(reinterpretCast()))); |
| 1755 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
| 1756 | expression(reinterpretCast()))); |
| 1757 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 1758 | "B b;" |
| 1759 | "D* p = dynamic_cast<D*>(&b);", |
| 1760 | expression(reinterpretCast()))); |
| 1761 | } |
| 1762 | |
| 1763 | TEST(FunctionalCast, MatchesSimpleCase) { |
| 1764 | std::string foo_class = "class Foo { public: Foo(char*); };"; |
| 1765 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
| 1766 | expression(functionalCast()))); |
| 1767 | } |
| 1768 | |
| 1769 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
| 1770 | std::string FooClass = "class Foo { public: Foo(char*); };"; |
| 1771 | EXPECT_TRUE( |
| 1772 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
| 1773 | expression(functionalCast()))); |
| 1774 | EXPECT_TRUE( |
| 1775 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
| 1776 | expression(functionalCast()))); |
| 1777 | } |
| 1778 | |
| 1779 | TEST(DynamicCast, MatchesSimpleCase) { |
| 1780 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 1781 | "B b;" |
| 1782 | "D* p = dynamic_cast<D*>(&b);", |
| 1783 | expression(dynamicCast()))); |
| 1784 | } |
| 1785 | |
| 1786 | TEST(StaticCast, MatchesSimpleCase) { |
| 1787 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
| 1788 | expression(staticCast()))); |
| 1789 | } |
| 1790 | |
| 1791 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
| 1792 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", |
| 1793 | expression(staticCast()))); |
| 1794 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
| 1795 | expression(staticCast()))); |
| 1796 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
| 1797 | expression(staticCast()))); |
| 1798 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 1799 | "B b;" |
| 1800 | "D* p = dynamic_cast<D*>(&b);", |
| 1801 | expression(staticCast()))); |
| 1802 | } |
| 1803 | |
| 1804 | TEST(HasDestinationType, MatchesSimpleCase) { |
| 1805 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
| 1806 | expression( |
| 1807 | staticCast(hasDestinationType( |
| 1808 | pointsTo(TypeMatcher(anything()))))))); |
| 1809 | } |
| 1810 | |
| 1811 | TEST(HasSourceExpression, MatchesSimpleCase) { |
| 1812 | EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 1813 | "void r() {string a_string; URL url = a_string; }", |
| 1814 | expression(implicitCast( |
| 1815 | hasSourceExpression(constructorCall()))))); |
| 1816 | } |
| 1817 | |
| 1818 | TEST(Statement, DoesNotMatchDeclarations) { |
| 1819 | EXPECT_TRUE(notMatches("class X {};", statement())); |
| 1820 | } |
| 1821 | |
| 1822 | TEST(Statement, MatchesCompoundStatments) { |
| 1823 | EXPECT_TRUE(matches("void x() {}", statement())); |
| 1824 | } |
| 1825 | |
| 1826 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
| 1827 | EXPECT_TRUE(notMatches("void x() {}", declarationStatement())); |
| 1828 | } |
| 1829 | |
| 1830 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
| 1831 | EXPECT_TRUE(matches("void x() { int a; }", declarationStatement())); |
| 1832 | } |
| 1833 | |
| 1834 | TEST(While, MatchesWhileLoops) { |
| 1835 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 1836 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 1837 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 1838 | } |
| 1839 | |
| 1840 | TEST(Do, MatchesDoLoops) { |
| 1841 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 1842 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 1843 | } |
| 1844 | |
| 1845 | TEST(Do, DoesNotMatchWhileLoops) { |
| 1846 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 1847 | } |
| 1848 | |
| 1849 | TEST(SwitchCase, MatchesCase) { |
| 1850 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 1851 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 1852 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 1853 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 1854 | } |
| 1855 | |
| 1856 | TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 1857 | EXPECT_TRUE(notMatches( |
| 1858 | "void x() { if(true) {} }", |
| 1859 | ifStmt(hasConditionVariableStatement(declarationStatement())))); |
| 1860 | EXPECT_TRUE(notMatches( |
| 1861 | "void x() { int x; if((x = 42)) {} }", |
| 1862 | ifStmt(hasConditionVariableStatement(declarationStatement())))); |
| 1863 | } |
| 1864 | |
| 1865 | TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 1866 | EXPECT_TRUE(matches( |
| 1867 | "void x() { if(int* a = 0) {} }", |
| 1868 | ifStmt(hasConditionVariableStatement(declarationStatement())))); |
| 1869 | } |
| 1870 | |
| 1871 | TEST(ForEach, BindsOneNode) { |
| 1872 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
| 1873 | record(hasName("C"), forEach(id("x", field(hasName("x"))))), |
| 1874 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("x", 1))); |
| 1875 | } |
| 1876 | |
| 1877 | TEST(ForEach, BindsMultipleNodes) { |
| 1878 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
| 1879 | record(hasName("C"), forEach(id("f", field()))), |
| 1880 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("f", 3))); |
| 1881 | } |
| 1882 | |
| 1883 | TEST(ForEach, BindsRecursiveCombinations) { |
| 1884 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1885 | "class C { class D { int x; int y; }; class E { int y; int z; }; };", |
| 1886 | record(hasName("C"), forEach(record(forEach(id("f", field()))))), |
| 1887 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("f", 4))); |
| 1888 | } |
| 1889 | |
| 1890 | TEST(ForEachDescendant, BindsOneNode) { |
| 1891 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
| 1892 | record(hasName("C"), forEachDescendant(id("x", field(hasName("x"))))), |
| 1893 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("x", 1))); |
| 1894 | } |
| 1895 | |
| 1896 | TEST(ForEachDescendant, BindsMultipleNodes) { |
| 1897 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1898 | "class C { class D { int x; int y; }; " |
| 1899 | " class E { class F { int y; int z; }; }; };", |
| 1900 | record(hasName("C"), forEachDescendant(id("f", field()))), |
| 1901 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("f", 4))); |
| 1902 | } |
| 1903 | |
| 1904 | TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 1905 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1906 | "class C { class D { " |
| 1907 | " class E { class F { class G { int y; int z; }; }; }; }; };", |
| 1908 | record(hasName("C"), forEachDescendant(record( |
| 1909 | forEachDescendant(id("f", field()))))), |
| 1910 | new VerifyIdIsBoundToDecl<clang::FieldDecl>("f", 8))); |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) { |
| 1915 | // Make sure that we can both match the class by name (::X) and by the type |
| 1916 | // the template was instantiated with (via a field). |
| 1917 | |
| 1918 | EXPECT_TRUE(matches( |
| 1919 | "template <typename T> class X {}; class A {}; X<A> x;", |
| 1920 | record(hasName("::X"), isTemplateInstantiation()))); |
| 1921 | |
| 1922 | EXPECT_TRUE(matches( |
| 1923 | "template <typename T> class X { T t; }; class A {}; X<A> x;", |
| 1924 | record(isTemplateInstantiation(), hasDescendant( |
| 1925 | field(hasType(record(hasName("A")))))))); |
| 1926 | } |
| 1927 | |
| 1928 | TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) { |
| 1929 | EXPECT_TRUE(matches( |
| 1930 | "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }", |
| 1931 | function(hasParameter(0, hasType(record(hasName("A")))), |
| 1932 | isTemplateInstantiation()))); |
| 1933 | } |
| 1934 | |
| 1935 | TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) { |
| 1936 | EXPECT_TRUE(matches( |
| 1937 | "template <typename T> class X { T t; }; class A {};" |
| 1938 | "template class X<A>;", |
| 1939 | record(isTemplateInstantiation(), hasDescendant( |
| 1940 | field(hasType(record(hasName("A")))))))); |
| 1941 | } |
| 1942 | |
| 1943 | TEST(IsTemplateInstantiation, |
| 1944 | MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 1945 | EXPECT_TRUE(matches( |
| 1946 | "template <typename T> class X {};" |
| 1947 | "template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
| 1948 | record(hasName("::X"), isTemplateInstantiation()))); |
| 1949 | } |
| 1950 | |
| 1951 | TEST(IsTemplateInstantiation, |
| 1952 | MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 1953 | EXPECT_TRUE(matches( |
| 1954 | "class A {};" |
| 1955 | "class X {" |
| 1956 | " template <typename U> class Y { U u; };" |
| 1957 | " Y<A> y;" |
| 1958 | "};", |
| 1959 | record(hasName("::X::Y"), isTemplateInstantiation()))); |
| 1960 | } |
| 1961 | |
| 1962 | TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 1963 | // FIXME: Figure out whether this makes sense. It doesn't affect the |
| 1964 | // normal use case as long as the uppermost instantiation always is marked |
| 1965 | // as template instantiation, but it might be confusing as a predicate. |
| 1966 | EXPECT_TRUE(matches( |
| 1967 | "class A {};" |
| 1968 | "template <typename T> class X {" |
| 1969 | " template <typename U> class Y { U u; };" |
| 1970 | " Y<T> y;" |
| 1971 | "}; X<A> x;", |
| 1972 | record(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
| 1973 | } |
| 1974 | |
| 1975 | TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) { |
| 1976 | EXPECT_TRUE(notMatches( |
| 1977 | "template <typename T> class X {}; class A {};" |
| 1978 | "template <> class X<A> {}; X<A> x;", |
| 1979 | record(hasName("::X"), isTemplateInstantiation()))); |
| 1980 | } |
| 1981 | |
| 1982 | TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) { |
| 1983 | EXPECT_TRUE(notMatches( |
| 1984 | "class A {}; class Y { A a; };", |
| 1985 | record(isTemplateInstantiation()))); |
| 1986 | } |
| 1987 | |
| 1988 | } // end namespace ast_matchers |
| 1989 | } // end namespace clang |