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" |
Benjamin Kramer | 8b9ed71 | 2012-12-01 17:22:05 +0000 | [diff] [blame] | 11 | #include "clang/AST/PrettyPrinter.h" |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 12 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 13 | #include "clang/ASTMatchers/ASTMatchers.h" |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 14 | #include "clang/Tooling/Tooling.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace ast_matchers { |
| 19 | |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame] | 20 | #if GTEST_HAS_DEATH_TEST |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 21 | TEST(HasNameDeathTest, DiesOnEmptyName) { |
| 22 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 23 | DeclarationMatcher HasEmptyName = recordDecl(hasName("")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 24 | EXPECT_TRUE(notMatches("class X {};", HasEmptyName)); |
| 25 | }, ""); |
| 26 | } |
| 27 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 28 | TEST(HasNameDeathTest, DiesOnEmptyPattern) { |
| 29 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 30 | DeclarationMatcher HasEmptyName = recordDecl(matchesName("")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 31 | EXPECT_TRUE(notMatches("class X {};", HasEmptyName)); |
| 32 | }, ""); |
| 33 | } |
| 34 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 35 | TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) { |
| 36 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 37 | DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom("")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 38 | EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty)); |
| 39 | }, ""); |
| 40 | } |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame] | 41 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 42 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 43 | TEST(Decl, MatchesDeclarations) { |
| 44 | EXPECT_TRUE(notMatches("", decl(usingDecl()))); |
| 45 | EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;", |
| 46 | decl(usingDecl()))); |
| 47 | } |
| 48 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 49 | TEST(NameableDeclaration, MatchesVariousDecls) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 50 | DeclarationMatcher NamedX = namedDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 51 | EXPECT_TRUE(matches("typedef int X;", NamedX)); |
| 52 | EXPECT_TRUE(matches("int X;", NamedX)); |
| 53 | EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX)); |
| 54 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX)); |
| 55 | EXPECT_TRUE(matches("void foo() { int X; }", NamedX)); |
| 56 | EXPECT_TRUE(matches("namespace X { }", NamedX)); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 57 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 58 | |
| 59 | EXPECT_TRUE(notMatches("#define X 1", NamedX)); |
| 60 | } |
| 61 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 62 | TEST(NameableDeclaration, REMatchesVariousDecls) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 63 | DeclarationMatcher NamedX = namedDecl(matchesName("::X")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 64 | EXPECT_TRUE(matches("typedef int Xa;", NamedX)); |
| 65 | EXPECT_TRUE(matches("int Xb;", NamedX)); |
| 66 | EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX)); |
| 67 | EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX)); |
| 68 | EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX)); |
| 69 | EXPECT_TRUE(matches("namespace Xij { }", NamedX)); |
| 70 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
| 71 | |
| 72 | EXPECT_TRUE(notMatches("#define Xkl 1", NamedX)); |
| 73 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 74 | DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 75 | EXPECT_TRUE(matches("int no_foo;", StartsWithNo)); |
| 76 | EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo)); |
| 77 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 78 | DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 79 | EXPECT_TRUE(matches("int abc;", Abc)); |
| 80 | EXPECT_TRUE(matches("int aFOObBARc;", Abc)); |
| 81 | EXPECT_TRUE(notMatches("int cab;", Abc)); |
| 82 | EXPECT_TRUE(matches("int cabc;", Abc)); |
Manuel Klimek | ec33b6f | 2012-12-10 07:08:53 +0000 | [diff] [blame] | 83 | |
| 84 | DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$")); |
| 85 | EXPECT_TRUE(matches("int k;", StartsWithK)); |
| 86 | EXPECT_TRUE(matches("int kAbc;", StartsWithK)); |
| 87 | EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK)); |
| 88 | EXPECT_TRUE(matches("class C { int k; };", StartsWithK)); |
| 89 | EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK)); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 92 | TEST(DeclarationMatcher, MatchClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 93 | DeclarationMatcher ClassMatcher(recordDecl()); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 94 | #if !defined(_MSC_VER) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 95 | EXPECT_FALSE(matches("", ClassMatcher)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 96 | #else |
| 97 | // Matches class type_info. |
| 98 | EXPECT_TRUE(matches("", ClassMatcher)); |
| 99 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 100 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 101 | DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 102 | EXPECT_TRUE(matches("class X;", ClassX)); |
| 103 | EXPECT_TRUE(matches("class X {};", ClassX)); |
| 104 | EXPECT_TRUE(matches("template<class T> class X {};", ClassX)); |
| 105 | EXPECT_TRUE(notMatches("", ClassX)); |
| 106 | } |
| 107 | |
| 108 | TEST(DeclarationMatcher, ClassIsDerived) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 109 | DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 110 | |
| 111 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX)); |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 112 | EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX)); |
| 113 | EXPECT_TRUE(notMatches("class X;", IsDerivedFromX)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 114 | EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX)); |
| 115 | EXPECT_TRUE(notMatches("", IsDerivedFromX)); |
| 116 | |
Daniel Jasper | 63d8872 | 2012-09-12 21:14:15 +0000 | [diff] [blame] | 117 | DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X")); |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 118 | |
| 119 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX)); |
| 120 | EXPECT_TRUE(matches("class X {};", IsAX)); |
| 121 | EXPECT_TRUE(matches("class X;", IsAX)); |
| 122 | EXPECT_TRUE(notMatches("class Y;", IsAX)); |
| 123 | EXPECT_TRUE(notMatches("", IsAX)); |
| 124 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 125 | DeclarationMatcher ZIsDerivedFromX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 126 | recordDecl(hasName("Z"), isDerivedFrom("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 127 | EXPECT_TRUE( |
| 128 | matches("class X {}; class Y : public X {}; class Z : public Y {};", |
| 129 | ZIsDerivedFromX)); |
| 130 | EXPECT_TRUE( |
| 131 | matches("class X {};" |
| 132 | "template<class T> class Y : public X {};" |
| 133 | "class Z : public Y<int> {};", ZIsDerivedFromX)); |
| 134 | EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};", |
| 135 | ZIsDerivedFromX)); |
| 136 | EXPECT_TRUE( |
| 137 | matches("template<class T> class X {}; " |
| 138 | "template<class T> class Z : public X<T> {};", |
| 139 | ZIsDerivedFromX)); |
| 140 | EXPECT_TRUE( |
| 141 | matches("template<class T, class U=T> class X {}; " |
| 142 | "template<class T> class Z : public X<T> {};", |
| 143 | ZIsDerivedFromX)); |
| 144 | EXPECT_TRUE( |
| 145 | notMatches("template<class X> class A { class Z : public X {}; };", |
| 146 | ZIsDerivedFromX)); |
| 147 | EXPECT_TRUE( |
| 148 | matches("template<class X> class A { public: class Z : public X {}; }; " |
| 149 | "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX)); |
| 150 | EXPECT_TRUE( |
| 151 | matches("template <class T> class X {}; " |
| 152 | "template<class Y> class A { class Z : public X<Y> {}; };", |
| 153 | ZIsDerivedFromX)); |
| 154 | EXPECT_TRUE( |
| 155 | notMatches("template<template<class T> class X> class A { " |
| 156 | " class Z : public X<int> {}; };", ZIsDerivedFromX)); |
| 157 | EXPECT_TRUE( |
| 158 | matches("template<template<class T> class X> class A { " |
| 159 | " public: class Z : public X<int> {}; }; " |
| 160 | "template<class T> class X {}; void y() { A<X>::Z z; }", |
| 161 | ZIsDerivedFromX)); |
| 162 | EXPECT_TRUE( |
| 163 | notMatches("template<class X> class A { class Z : public X::D {}; };", |
| 164 | ZIsDerivedFromX)); |
| 165 | EXPECT_TRUE( |
| 166 | matches("template<class X> class A { public: " |
| 167 | " class Z : public X::D {}; }; " |
| 168 | "class Y { public: class X {}; typedef X D; }; " |
| 169 | "void y() { A<Y>::Z z; }", ZIsDerivedFromX)); |
| 170 | EXPECT_TRUE( |
| 171 | matches("class X {}; typedef X Y; class Z : public Y {};", |
| 172 | ZIsDerivedFromX)); |
| 173 | EXPECT_TRUE( |
| 174 | matches("template<class T> class Y { typedef typename T::U X; " |
| 175 | " class Z : public X {}; };", ZIsDerivedFromX)); |
| 176 | EXPECT_TRUE(matches("class X {}; class Z : public ::X {};", |
| 177 | ZIsDerivedFromX)); |
| 178 | EXPECT_TRUE( |
| 179 | notMatches("template<class T> class X {}; " |
| 180 | "template<class T> class A { class Z : public X<T>::D {}; };", |
| 181 | ZIsDerivedFromX)); |
| 182 | EXPECT_TRUE( |
| 183 | matches("template<class T> class X { public: typedef X<T> D; }; " |
| 184 | "template<class T> class A { public: " |
| 185 | " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }", |
| 186 | ZIsDerivedFromX)); |
| 187 | EXPECT_TRUE( |
| 188 | notMatches("template<class X> class A { class Z : public X::D::E {}; };", |
| 189 | ZIsDerivedFromX)); |
| 190 | EXPECT_TRUE( |
| 191 | matches("class X {}; typedef X V; typedef V W; class Z : public W {};", |
| 192 | ZIsDerivedFromX)); |
| 193 | EXPECT_TRUE( |
| 194 | matches("class X {}; class Y : public X {}; " |
| 195 | "typedef Y V; typedef V W; class Z : public W {};", |
| 196 | ZIsDerivedFromX)); |
| 197 | EXPECT_TRUE( |
| 198 | matches("template<class T, class U> class X {}; " |
| 199 | "template<class T> class A { class Z : public X<T, int> {}; };", |
| 200 | ZIsDerivedFromX)); |
| 201 | EXPECT_TRUE( |
| 202 | notMatches("template<class X> class D { typedef X A; typedef A B; " |
| 203 | " typedef B C; class Z : public C {}; };", |
| 204 | ZIsDerivedFromX)); |
| 205 | EXPECT_TRUE( |
| 206 | matches("class X {}; typedef X A; typedef A B; " |
| 207 | "class Z : public B {};", ZIsDerivedFromX)); |
| 208 | EXPECT_TRUE( |
| 209 | matches("class X {}; typedef X A; typedef A B; typedef B C; " |
| 210 | "class Z : public C {};", ZIsDerivedFromX)); |
| 211 | EXPECT_TRUE( |
| 212 | matches("class U {}; typedef U X; typedef X V; " |
| 213 | "class Z : public V {};", ZIsDerivedFromX)); |
| 214 | EXPECT_TRUE( |
| 215 | matches("class Base {}; typedef Base X; " |
| 216 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 217 | EXPECT_TRUE( |
| 218 | matches("class Base {}; typedef Base Base2; typedef Base2 X; " |
| 219 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 220 | EXPECT_TRUE( |
| 221 | notMatches("class Base {}; class Base2 {}; typedef Base2 X; " |
| 222 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 223 | EXPECT_TRUE( |
| 224 | matches("class A {}; typedef A X; typedef A Y; " |
| 225 | "class Z : public Y {};", ZIsDerivedFromX)); |
| 226 | EXPECT_TRUE( |
| 227 | notMatches("template <typename T> class Z;" |
| 228 | "template <> class Z<void> {};" |
| 229 | "template <typename T> class Z : public Z<void> {};", |
| 230 | IsDerivedFromX)); |
| 231 | EXPECT_TRUE( |
| 232 | matches("template <typename T> class X;" |
| 233 | "template <> class X<void> {};" |
| 234 | "template <typename T> class X : public X<void> {};", |
| 235 | IsDerivedFromX)); |
| 236 | EXPECT_TRUE(matches( |
| 237 | "class X {};" |
| 238 | "template <typename T> class Z;" |
| 239 | "template <> class Z<void> {};" |
| 240 | "template <typename T> class Z : public Z<void>, public X {};", |
| 241 | ZIsDerivedFromX)); |
Manuel Klimek | 987c2f5 | 2012-12-04 13:40:29 +0000 | [diff] [blame] | 242 | EXPECT_TRUE( |
| 243 | notMatches("template<int> struct X;" |
| 244 | "template<int i> struct X : public X<i-1> {};", |
| 245 | recordDecl(isDerivedFrom(recordDecl(hasName("Some")))))); |
| 246 | EXPECT_TRUE(matches( |
| 247 | "struct A {};" |
| 248 | "template<int> struct X;" |
| 249 | "template<int i> struct X : public X<i-1> {};" |
| 250 | "template<> struct X<0> : public A {};" |
| 251 | "struct B : public X<42> {};", |
| 252 | recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 253 | |
| 254 | // FIXME: Once we have better matchers for template type matching, |
| 255 | // get rid of the Variable(...) matching and match the right template |
| 256 | // declarations directly. |
| 257 | const char *RecursiveTemplateOneParameter = |
| 258 | "class Base1 {}; class Base2 {};" |
| 259 | "template <typename T> class Z;" |
| 260 | "template <> class Z<void> : public Base1 {};" |
| 261 | "template <> class Z<int> : public Base2 {};" |
| 262 | "template <> class Z<float> : public Z<void> {};" |
| 263 | "template <> class Z<double> : public Z<int> {};" |
| 264 | "template <typename T> class Z : public Z<float>, public Z<double> {};" |
| 265 | "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }"; |
| 266 | EXPECT_TRUE(matches( |
| 267 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 268 | varDecl(hasName("z_float"), |
| 269 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 270 | EXPECT_TRUE(notMatches( |
| 271 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 272 | varDecl(hasName("z_float"), |
| 273 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 274 | EXPECT_TRUE(matches( |
| 275 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 276 | varDecl(hasName("z_char"), |
| 277 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"), |
| 278 | isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 279 | |
| 280 | const char *RecursiveTemplateTwoParameters = |
| 281 | "class Base1 {}; class Base2 {};" |
| 282 | "template <typename T1, typename T2> class Z;" |
| 283 | "template <typename T> class Z<void, T> : public Base1 {};" |
| 284 | "template <typename T> class Z<int, T> : public Base2 {};" |
| 285 | "template <typename T> class Z<float, T> : public Z<void, T> {};" |
| 286 | "template <typename T> class Z<double, T> : public Z<int, T> {};" |
| 287 | "template <typename T1, typename T2> class Z : " |
| 288 | " public Z<float, T2>, public Z<double, T2> {};" |
| 289 | "void f() { Z<float, void> z_float; Z<double, void> z_double; " |
| 290 | " Z<char, void> z_char; }"; |
| 291 | EXPECT_TRUE(matches( |
| 292 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 293 | varDecl(hasName("z_float"), |
| 294 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 295 | EXPECT_TRUE(notMatches( |
| 296 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 297 | varDecl(hasName("z_float"), |
| 298 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 299 | EXPECT_TRUE(matches( |
| 300 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 301 | varDecl(hasName("z_char"), |
| 302 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"), |
| 303 | isDerivedFrom("Base2"))))))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 304 | EXPECT_TRUE(matches( |
| 305 | "namespace ns { class X {}; class Y : public X {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 306 | recordDecl(isDerivedFrom("::ns::X")))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 307 | EXPECT_TRUE(notMatches( |
| 308 | "class X {}; class Y : public X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 309 | recordDecl(isDerivedFrom("::ns::X")))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 310 | |
| 311 | EXPECT_TRUE(matches( |
| 312 | "class X {}; class Y : public X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 313 | recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test"))))); |
Manuel Klimek | b56da8c | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 314 | |
| 315 | EXPECT_TRUE(matches( |
| 316 | "template<typename T> class X {};" |
| 317 | "template<typename T> using Z = X<T>;" |
| 318 | "template <typename T> class Y : Z<T> {};", |
| 319 | recordDecl(isDerivedFrom(namedDecl(hasName("X")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 322 | TEST(DeclarationMatcher, hasMethod) { |
| 323 | EXPECT_TRUE(matches("class A { void func(); };", |
| 324 | recordDecl(hasMethod(hasName("func"))))); |
| 325 | EXPECT_TRUE(notMatches("class A { void func(); };", |
| 326 | recordDecl(hasMethod(isPublic())))); |
| 327 | } |
| 328 | |
Daniel Jasper | 08f0c53 | 2012-09-18 14:17:42 +0000 | [diff] [blame] | 329 | TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) { |
| 330 | EXPECT_TRUE(matches( |
| 331 | "template <typename T> struct A {" |
| 332 | " template <typename T2> struct F {};" |
| 333 | "};" |
| 334 | "template <typename T> struct B : A<T>::template F<T> {};" |
| 335 | "B<int> b;", |
| 336 | recordDecl(hasName("B"), isDerivedFrom(recordDecl())))); |
| 337 | } |
| 338 | |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 339 | TEST(DeclarationMatcher, hasDeclContext) { |
| 340 | EXPECT_TRUE(matches( |
| 341 | "namespace N {" |
| 342 | " namespace M {" |
| 343 | " class D {};" |
| 344 | " }" |
| 345 | "}", |
Daniel Jasper | abe9223 | 2013-04-08 16:44:05 +0000 | [diff] [blame] | 346 | recordDecl(hasDeclContext(namespaceDecl(hasName("M")))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 347 | EXPECT_TRUE(notMatches( |
| 348 | "namespace N {" |
| 349 | " namespace M {" |
| 350 | " class D {};" |
| 351 | " }" |
| 352 | "}", |
Daniel Jasper | abe9223 | 2013-04-08 16:44:05 +0000 | [diff] [blame] | 353 | recordDecl(hasDeclContext(namespaceDecl(hasName("N")))))); |
| 354 | |
| 355 | EXPECT_TRUE(matches("namespace {" |
| 356 | " namespace M {" |
| 357 | " class D {};" |
| 358 | " }" |
| 359 | "}", |
| 360 | recordDecl(hasDeclContext(namespaceDecl( |
| 361 | hasName("M"), hasDeclContext(namespaceDecl())))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 364 | TEST(ClassTemplate, DoesNotMatchClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 365 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 366 | EXPECT_TRUE(notMatches("class X;", ClassX)); |
| 367 | EXPECT_TRUE(notMatches("class X {};", ClassX)); |
| 368 | } |
| 369 | |
| 370 | TEST(ClassTemplate, MatchesClassTemplate) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 371 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 372 | EXPECT_TRUE(matches("template<typename T> class X {};", ClassX)); |
| 373 | EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX)); |
| 374 | } |
| 375 | |
| 376 | TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) { |
| 377 | EXPECT_TRUE(notMatches("template<typename T> class X { };" |
| 378 | "template<> class X<int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 379 | classTemplateDecl(hasName("X"), |
| 380 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) { |
| 384 | EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };" |
| 385 | "template<typename T> class X<T, int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 386 | classTemplateDecl(hasName("X"), |
| 387 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 390 | TEST(AllOf, AllOverloadsWork) { |
| 391 | const char Program[] = |
Edwin Vane | 7f43fcf | 2013-02-12 13:55:40 +0000 | [diff] [blame] | 392 | "struct T { };" |
| 393 | "int f(int, T*, int, int);" |
| 394 | "void g(int x) { T t; f(x, &t, 3, 4); }"; |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 395 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 396 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 397 | hasArgument(0, declRefExpr(to(varDecl()))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 398 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 399 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 400 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 401 | hasArgument(1, hasType(pointsTo( |
| 402 | recordDecl(hasName("T"))))))))); |
Edwin Vane | 7f43fcf | 2013-02-12 13:55:40 +0000 | [diff] [blame] | 403 | EXPECT_TRUE(matches(Program, |
| 404 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 405 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 406 | hasArgument(1, hasType(pointsTo( |
| 407 | recordDecl(hasName("T"))))), |
| 408 | hasArgument(2, integerLiteral(equals(3))))))); |
| 409 | EXPECT_TRUE(matches(Program, |
| 410 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 411 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 412 | hasArgument(1, hasType(pointsTo( |
| 413 | recordDecl(hasName("T"))))), |
| 414 | hasArgument(2, integerLiteral(equals(3))), |
| 415 | hasArgument(3, integerLiteral(equals(4))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 418 | TEST(DeclarationMatcher, MatchAnyOf) { |
| 419 | DeclarationMatcher YOrZDerivedFromX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 420 | recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 421 | EXPECT_TRUE( |
| 422 | matches("class X {}; class Z : public X {};", YOrZDerivedFromX)); |
| 423 | EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX)); |
| 424 | EXPECT_TRUE( |
| 425 | notMatches("class X {}; class W : public X {};", YOrZDerivedFromX)); |
| 426 | EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX)); |
| 427 | |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 428 | DeclarationMatcher XOrYOrZOrU = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 429 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"))); |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 430 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrU)); |
| 431 | EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU)); |
| 432 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 433 | DeclarationMatcher XOrYOrZOrUOrV = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 434 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"), |
| 435 | hasName("V"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 436 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV)); |
| 437 | EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV)); |
| 438 | EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV)); |
| 439 | EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); |
| 440 | EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); |
| 441 | EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); |
| 442 | } |
| 443 | |
| 444 | TEST(DeclarationMatcher, MatchHas) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 445 | DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 446 | EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); |
| 447 | EXPECT_TRUE(matches("class X {};", HasClassX)); |
| 448 | |
| 449 | DeclarationMatcher YHasClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 450 | recordDecl(hasName("Y"), has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 451 | EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX)); |
| 452 | EXPECT_TRUE(notMatches("class X {};", YHasClassX)); |
| 453 | EXPECT_TRUE( |
| 454 | notMatches("class Y { class Z { class X {}; }; };", YHasClassX)); |
| 455 | } |
| 456 | |
| 457 | TEST(DeclarationMatcher, MatchHasRecursiveAllOf) { |
| 458 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 459 | recordDecl( |
| 460 | has(recordDecl( |
| 461 | has(recordDecl(hasName("X"))), |
| 462 | has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 463 | hasName("Z"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 464 | has(recordDecl( |
| 465 | has(recordDecl(hasName("A"))), |
| 466 | has(recordDecl(hasName("B"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 467 | hasName("C"))), |
| 468 | hasName("F")); |
| 469 | |
| 470 | EXPECT_TRUE(matches( |
| 471 | "class F {" |
| 472 | " class Z {" |
| 473 | " class X {};" |
| 474 | " class Y {};" |
| 475 | " };" |
| 476 | " class C {" |
| 477 | " class A {};" |
| 478 | " class B {};" |
| 479 | " };" |
| 480 | "};", Recursive)); |
| 481 | |
| 482 | EXPECT_TRUE(matches( |
| 483 | "class F {" |
| 484 | " class Z {" |
| 485 | " class A {};" |
| 486 | " class X {};" |
| 487 | " class Y {};" |
| 488 | " };" |
| 489 | " class C {" |
| 490 | " class X {};" |
| 491 | " class A {};" |
| 492 | " class B {};" |
| 493 | " };" |
| 494 | "};", Recursive)); |
| 495 | |
| 496 | EXPECT_TRUE(matches( |
| 497 | "class O1 {" |
| 498 | " class O2 {" |
| 499 | " class F {" |
| 500 | " class Z {" |
| 501 | " class A {};" |
| 502 | " class X {};" |
| 503 | " class Y {};" |
| 504 | " };" |
| 505 | " class C {" |
| 506 | " class X {};" |
| 507 | " class A {};" |
| 508 | " class B {};" |
| 509 | " };" |
| 510 | " };" |
| 511 | " };" |
| 512 | "};", Recursive)); |
| 513 | } |
| 514 | |
| 515 | TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) { |
| 516 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 517 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 518 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 519 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 520 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 521 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 522 | hasName("X"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 523 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 524 | hasName("Y"))), |
| 525 | hasName("Z")))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 526 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 527 | anyOf( |
| 528 | hasName("C"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 529 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 530 | hasName("A"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 531 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 532 | hasName("B")))))), |
| 533 | hasName("F"))); |
| 534 | |
| 535 | EXPECT_TRUE(matches("class F {};", Recursive)); |
| 536 | EXPECT_TRUE(matches("class Z {};", Recursive)); |
| 537 | EXPECT_TRUE(matches("class C {};", Recursive)); |
| 538 | EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive)); |
| 539 | EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive)); |
| 540 | EXPECT_TRUE( |
| 541 | matches("class O1 { class O2 {" |
| 542 | " class M { class N { class B {}; }; }; " |
| 543 | "}; };", Recursive)); |
| 544 | } |
| 545 | |
| 546 | TEST(DeclarationMatcher, MatchNot) { |
| 547 | DeclarationMatcher NotClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 548 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 549 | isDerivedFrom("Y"), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 550 | unless(hasName("X"))); |
| 551 | EXPECT_TRUE(notMatches("", NotClassX)); |
| 552 | EXPECT_TRUE(notMatches("class Y {};", NotClassX)); |
| 553 | EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX)); |
| 554 | EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX)); |
| 555 | EXPECT_TRUE( |
| 556 | notMatches("class Y {}; class Z {}; class X : public Y {};", |
| 557 | NotClassX)); |
| 558 | |
| 559 | DeclarationMatcher ClassXHasNotClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 560 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 561 | hasName("X"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 562 | has(recordDecl(hasName("Z"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 563 | unless( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 564 | has(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 565 | EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY)); |
| 566 | EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };", |
| 567 | ClassXHasNotClassY)); |
| 568 | } |
| 569 | |
| 570 | TEST(DeclarationMatcher, HasDescendant) { |
| 571 | DeclarationMatcher ZDescendantClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 572 | recordDecl( |
| 573 | hasDescendant(recordDecl(hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 574 | hasName("Z")); |
| 575 | EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX)); |
| 576 | EXPECT_TRUE( |
| 577 | matches("class Z { class Y { class X {}; }; };", ZDescendantClassX)); |
| 578 | EXPECT_TRUE( |
| 579 | matches("class Z { class A { class Y { class X {}; }; }; };", |
| 580 | ZDescendantClassX)); |
| 581 | EXPECT_TRUE( |
| 582 | matches("class Z { class A { class B { class Y { class X {}; }; }; }; };", |
| 583 | ZDescendantClassX)); |
| 584 | EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX)); |
| 585 | |
| 586 | DeclarationMatcher ZDescendantClassXHasClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 587 | recordDecl( |
| 588 | hasDescendant(recordDecl(has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 589 | hasName("X"))), |
| 590 | hasName("Z")); |
| 591 | EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };", |
| 592 | ZDescendantClassXHasClassY)); |
| 593 | EXPECT_TRUE( |
| 594 | matches("class Z { class A { class B { class X { class Y {}; }; }; }; };", |
| 595 | ZDescendantClassXHasClassY)); |
| 596 | EXPECT_TRUE(notMatches( |
| 597 | "class Z {" |
| 598 | " class A {" |
| 599 | " class B {" |
| 600 | " class X {" |
| 601 | " class C {" |
| 602 | " class Y {};" |
| 603 | " };" |
| 604 | " };" |
| 605 | " }; " |
| 606 | " };" |
| 607 | "};", ZDescendantClassXHasClassY)); |
| 608 | |
| 609 | DeclarationMatcher ZDescendantClassXDescendantClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 610 | recordDecl( |
| 611 | hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))), |
| 612 | hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 613 | hasName("Z")); |
| 614 | EXPECT_TRUE( |
| 615 | matches("class Z { class A { class X { class B { class Y {}; }; }; }; };", |
| 616 | ZDescendantClassXDescendantClassY)); |
| 617 | EXPECT_TRUE(matches( |
| 618 | "class Z {" |
| 619 | " class A {" |
| 620 | " class X {" |
| 621 | " class B {" |
| 622 | " class Y {};" |
| 623 | " };" |
| 624 | " class Y {};" |
| 625 | " };" |
| 626 | " };" |
| 627 | "};", ZDescendantClassXDescendantClassY)); |
| 628 | } |
| 629 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 630 | // Implements a run method that returns whether BoundNodes contains a |
| 631 | // Decl bound to Id that can be dynamically cast to T. |
| 632 | // Optionally checks that the check succeeded a specific number of times. |
| 633 | template <typename T> |
| 634 | class VerifyIdIsBoundTo : public BoundNodesCallback { |
| 635 | public: |
| 636 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 637 | // Does not check for a certain number of matches. |
| 638 | explicit VerifyIdIsBoundTo(llvm::StringRef Id) |
| 639 | : Id(Id), ExpectedCount(-1), Count(0) {} |
| 640 | |
| 641 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 642 | // Checks that there were exactly \c ExpectedCount matches. |
| 643 | VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount) |
| 644 | : Id(Id), ExpectedCount(ExpectedCount), Count(0) {} |
| 645 | |
| 646 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 647 | // Checks that there was exactly one match with the name \c ExpectedName. |
| 648 | // Note that \c T must be a NamedDecl for this to work. |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 649 | VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName, |
| 650 | int ExpectedCount = 1) |
| 651 | : Id(Id), ExpectedCount(ExpectedCount), Count(0), |
| 652 | ExpectedName(ExpectedName) {} |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 653 | |
| 654 | ~VerifyIdIsBoundTo() { |
| 655 | if (ExpectedCount != -1) |
| 656 | EXPECT_EQ(ExpectedCount, Count); |
| 657 | if (!ExpectedName.empty()) |
| 658 | EXPECT_EQ(ExpectedName, Name); |
| 659 | } |
| 660 | |
| 661 | virtual bool run(const BoundNodes *Nodes) { |
| 662 | if (Nodes->getNodeAs<T>(Id)) { |
| 663 | ++Count; |
| 664 | if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) { |
| 665 | Name = Named->getNameAsString(); |
| 666 | } else if (const NestedNameSpecifier *NNS = |
| 667 | Nodes->getNodeAs<NestedNameSpecifier>(Id)) { |
| 668 | llvm::raw_string_ostream OS(Name); |
| 669 | NNS->print(OS, PrintingPolicy(LangOptions())); |
| 670 | } |
| 671 | return true; |
| 672 | } |
| 673 | return false; |
| 674 | } |
| 675 | |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 676 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 677 | return run(Nodes); |
| 678 | } |
| 679 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 680 | private: |
| 681 | const std::string Id; |
| 682 | const int ExpectedCount; |
| 683 | int Count; |
| 684 | const std::string ExpectedName; |
| 685 | std::string Name; |
| 686 | }; |
| 687 | |
| 688 | TEST(HasDescendant, MatchesDescendantTypes) { |
| 689 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 690 | decl(hasDescendant(loc(builtinType()))))); |
| 691 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 692 | stmt(hasDescendant(builtinType())))); |
| 693 | |
| 694 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 695 | stmt(hasDescendant(loc(builtinType()))))); |
| 696 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 697 | stmt(hasDescendant(qualType(builtinType()))))); |
| 698 | |
| 699 | EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }", |
| 700 | stmt(hasDescendant(isInteger())))); |
| 701 | |
| 702 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 703 | "void f() { int a; float c; int d; int e; }", |
| 704 | functionDecl(forEachDescendant( |
| 705 | varDecl(hasDescendant(isInteger())).bind("x"))), |
| 706 | new VerifyIdIsBoundTo<Decl>("x", 3))); |
| 707 | } |
| 708 | |
| 709 | TEST(HasDescendant, MatchesDescendantsOfTypes) { |
| 710 | EXPECT_TRUE(matches("void f() { int*** i; }", |
| 711 | qualType(hasDescendant(builtinType())))); |
| 712 | EXPECT_TRUE(matches("void f() { int*** i; }", |
| 713 | qualType(hasDescendant( |
| 714 | pointerType(pointee(builtinType())))))); |
| 715 | EXPECT_TRUE(matches("void f() { int*** i; }", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 716 | typeLoc(hasDescendant(loc(builtinType()))))); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 717 | |
| 718 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 719 | "void f() { int*** i; }", |
| 720 | qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))), |
| 721 | new VerifyIdIsBoundTo<Type>("x", 2))); |
| 722 | } |
| 723 | |
| 724 | TEST(Has, MatchesChildrenOfTypes) { |
| 725 | EXPECT_TRUE(matches("int i;", |
| 726 | varDecl(hasName("i"), has(isInteger())))); |
| 727 | EXPECT_TRUE(notMatches("int** i;", |
| 728 | varDecl(hasName("i"), has(isInteger())))); |
| 729 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 730 | "int (*f)(float, int);", |
| 731 | qualType(functionType(), forEach(qualType(isInteger()).bind("x"))), |
| 732 | new VerifyIdIsBoundTo<QualType>("x", 2))); |
| 733 | } |
| 734 | |
| 735 | TEST(Has, MatchesChildTypes) { |
| 736 | EXPECT_TRUE(matches( |
| 737 | "int* i;", |
| 738 | varDecl(hasName("i"), hasType(qualType(has(builtinType())))))); |
| 739 | EXPECT_TRUE(notMatches( |
| 740 | "int* i;", |
| 741 | varDecl(hasName("i"), hasType(qualType(has(pointerType())))))); |
| 742 | } |
| 743 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 744 | TEST(Enum, DoesNotMatchClasses) { |
| 745 | EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X")))); |
| 746 | } |
| 747 | |
| 748 | TEST(Enum, MatchesEnums) { |
| 749 | EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X")))); |
| 750 | } |
| 751 | |
| 752 | TEST(EnumConstant, Matches) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 753 | DeclarationMatcher Matcher = enumConstantDecl(hasName("A")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 754 | EXPECT_TRUE(matches("enum X{ A };", Matcher)); |
| 755 | EXPECT_TRUE(notMatches("enum X{ B };", Matcher)); |
| 756 | EXPECT_TRUE(notMatches("enum X {};", Matcher)); |
| 757 | } |
| 758 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 759 | TEST(StatementMatcher, Has) { |
| 760 | StatementMatcher HasVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 761 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 762 | has(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 763 | |
| 764 | EXPECT_TRUE(matches( |
| 765 | "class X; X *x(int); void c() { int i; x(i); }", HasVariableI)); |
| 766 | EXPECT_TRUE(notMatches( |
| 767 | "class X; X *x(int); void c() { int i; x(42); }", HasVariableI)); |
| 768 | } |
| 769 | |
| 770 | TEST(StatementMatcher, HasDescendant) { |
| 771 | StatementMatcher HasDescendantVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 772 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 773 | hasDescendant(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 774 | |
| 775 | EXPECT_TRUE(matches( |
| 776 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }", |
| 777 | HasDescendantVariableI)); |
| 778 | EXPECT_TRUE(notMatches( |
| 779 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }", |
| 780 | HasDescendantVariableI)); |
| 781 | } |
| 782 | |
| 783 | TEST(TypeMatcher, MatchesClassType) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 784 | TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 785 | |
| 786 | EXPECT_TRUE(matches("class A { public: A *a; };", TypeA)); |
| 787 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 788 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 789 | TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 790 | |
| 791 | EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };", |
| 792 | TypeDerivedFromA)); |
| 793 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 794 | |
| 795 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 796 | recordDecl(hasName("A"), has(recordDecl(hasName("B"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 797 | |
| 798 | EXPECT_TRUE( |
| 799 | matches("class A { public: A *a; class B {}; };", TypeAHasClassB)); |
| 800 | } |
| 801 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 802 | TEST(Matcher, BindMatchedNodes) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 803 | DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 804 | |
| 805 | EXPECT_TRUE(matchAndVerifyResultTrue("class X {};", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 806 | ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 807 | |
| 808 | EXPECT_TRUE(matchAndVerifyResultFalse("class X {};", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 809 | ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 810 | |
| 811 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 812 | recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 813 | |
| 814 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };", |
| 815 | TypeAHasClassB, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 816 | new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 817 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 818 | StatementMatcher MethodX = |
| 819 | callExpr(callee(methodDecl(hasName("x")))).bind("x"); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 820 | |
| 821 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };", |
| 822 | MethodX, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 823 | new VerifyIdIsBoundTo<CXXMemberCallExpr>("x"))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | TEST(Matcher, BindTheSameNameInAlternatives) { |
| 827 | StatementMatcher matcher = anyOf( |
| 828 | binaryOperator(hasOperatorName("+"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 829 | hasLHS(expr().bind("x")), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 830 | hasRHS(integerLiteral(equals(0)))), |
| 831 | binaryOperator(hasOperatorName("+"), |
| 832 | hasLHS(integerLiteral(equals(0))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 833 | hasRHS(expr().bind("x")))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 834 | |
| 835 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 836 | // The first branch of the matcher binds x to 0 but then fails. |
| 837 | // The second branch binds x to f() and succeeds. |
| 838 | "int f() { return 0 + f(); }", |
| 839 | matcher, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 840 | new VerifyIdIsBoundTo<CallExpr>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 843 | TEST(Matcher, BindsIDForMemoizedResults) { |
| 844 | // Using the same matcher in two match expressions will make memoization |
| 845 | // kick in. |
| 846 | DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x"); |
| 847 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 848 | "class A { class B { class X {}; }; };", |
| 849 | DeclarationMatcher(anyOf( |
| 850 | recordDecl(hasName("A"), hasDescendant(ClassX)), |
| 851 | recordDecl(hasName("B"), hasDescendant(ClassX)))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 852 | new VerifyIdIsBoundTo<Decl>("x", 2))); |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Daniel Jasper | 189f2e4 | 2012-12-03 15:43:25 +0000 | [diff] [blame] | 855 | TEST(HasDeclaration, HasDeclarationOfEnumType) { |
| 856 | EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }", |
| 857 | expr(hasType(pointsTo( |
| 858 | qualType(hasDeclaration(enumDecl(hasName("X"))))))))); |
| 859 | } |
| 860 | |
Edwin Vane | b45083d | 2013-02-25 14:32:42 +0000 | [diff] [blame] | 861 | TEST(HasDeclaration, HasGetDeclTraitTest) { |
| 862 | EXPECT_TRUE(internal::has_getDecl<TypedefType>::value); |
| 863 | EXPECT_TRUE(internal::has_getDecl<RecordType>::value); |
| 864 | EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value); |
| 865 | } |
| 866 | |
Edwin Vane | 5238060 | 2013-02-19 17:14:34 +0000 | [diff] [blame] | 867 | TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) { |
| 868 | EXPECT_TRUE(matches("typedef int X; X a;", |
| 869 | varDecl(hasName("a"), |
| 870 | hasType(typedefType(hasDeclaration(decl())))))); |
| 871 | |
| 872 | // FIXME: Add tests for other types with getDecl() (e.g. RecordType) |
| 873 | } |
| 874 | |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 875 | TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) { |
| 876 | EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;", |
| 877 | varDecl(hasType(templateSpecializationType( |
| 878 | hasDeclaration(namedDecl(hasName("A")))))))); |
| 879 | } |
| 880 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 881 | TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 882 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 883 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 884 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 885 | EXPECT_TRUE( |
| 886 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 887 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 888 | EXPECT_TRUE( |
| 889 | matches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 890 | expr(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 894 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 895 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 896 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 897 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 898 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 899 | EXPECT_TRUE( |
| 900 | matches("class X {}; void y() { X *x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 901 | varDecl(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 905 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 906 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 907 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 908 | EXPECT_TRUE( |
| 909 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 910 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 914 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 915 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 916 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 917 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 918 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Manuel Klimek | 1a68afd | 2013-06-20 13:08:29 +0000 | [diff] [blame] | 921 | TEST(HasTypeLoc, MatchesDeclaratorDecls) { |
| 922 | EXPECT_TRUE(matches("int x;", |
| 923 | varDecl(hasName("x"), hasTypeLoc(loc(asString("int")))))); |
| 924 | |
| 925 | // Make sure we don't crash on implicit constructors. |
| 926 | EXPECT_TRUE(notMatches("class X {}; X x;", |
| 927 | declaratorDecl(hasTypeLoc(loc(asString("int")))))); |
| 928 | } |
| 929 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 930 | TEST(Matcher, Call) { |
| 931 | // FIXME: Do we want to overload Call() to directly take |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 932 | // Matcher<Decl>, too? |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 933 | StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 934 | |
| 935 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 936 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 937 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 938 | StatementMatcher MethodOnY = |
| 939 | memberCallExpr(on(hasType(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 940 | |
| 941 | EXPECT_TRUE( |
| 942 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 943 | MethodOnY)); |
| 944 | EXPECT_TRUE( |
| 945 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 946 | MethodOnY)); |
| 947 | EXPECT_TRUE( |
| 948 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 949 | MethodOnY)); |
| 950 | EXPECT_TRUE( |
| 951 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 952 | MethodOnY)); |
| 953 | EXPECT_TRUE( |
| 954 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 955 | MethodOnY)); |
| 956 | |
| 957 | StatementMatcher MethodOnYPointer = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 958 | memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 959 | |
| 960 | EXPECT_TRUE( |
| 961 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 962 | MethodOnYPointer)); |
| 963 | EXPECT_TRUE( |
| 964 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 965 | MethodOnYPointer)); |
| 966 | EXPECT_TRUE( |
| 967 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 968 | MethodOnYPointer)); |
| 969 | EXPECT_TRUE( |
| 970 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 971 | MethodOnYPointer)); |
| 972 | EXPECT_TRUE( |
| 973 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 974 | MethodOnYPointer)); |
| 975 | } |
| 976 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 977 | TEST(Matcher, Lambda) { |
| 978 | EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };", |
| 979 | lambdaExpr())); |
| 980 | } |
| 981 | |
| 982 | TEST(Matcher, ForRange) { |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 983 | EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };" |
| 984 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 985 | forRangeStmt())); |
| 986 | EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }", |
| 987 | forRangeStmt())); |
| 988 | } |
| 989 | |
| 990 | TEST(Matcher, UserDefinedLiteral) { |
| 991 | EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {" |
| 992 | " return i + 1;" |
| 993 | "}" |
| 994 | "char c = 'a'_inc;", |
| 995 | userDefinedLiteral())); |
| 996 | } |
| 997 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 998 | TEST(Matcher, FlowControl) { |
| 999 | EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt())); |
| 1000 | EXPECT_TRUE(matches("void f() { while(true) { continue; } }", |
| 1001 | continueStmt())); |
| 1002 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt())); |
| 1003 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt())); |
| 1004 | EXPECT_TRUE(matches("void f() { return; }", returnStmt())); |
| 1005 | } |
| 1006 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1007 | TEST(HasType, MatchesAsString) { |
| 1008 | EXPECT_TRUE( |
| 1009 | matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1010 | memberCallExpr(on(hasType(asString("class Y *")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1011 | EXPECT_TRUE(matches("class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1012 | methodDecl(hasParameter(0, hasType(asString("int")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1013 | EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1014 | fieldDecl(hasType(asString("ns::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1015 | EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1016 | fieldDecl(hasType(asString("struct <anonymous>::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1019 | TEST(Matcher, OverloadedOperatorCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1020 | StatementMatcher OpCall = operatorCallExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1021 | // Unary operator |
| 1022 | EXPECT_TRUE(matches("class Y { }; " |
| 1023 | "bool operator!(Y x) { return false; }; " |
| 1024 | "Y y; bool c = !y;", OpCall)); |
| 1025 | // No match -- special operators like "new", "delete" |
| 1026 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
| 1027 | // we need to figure out include paths in the test. |
| 1028 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
| 1029 | // "class Y { }; " |
| 1030 | // "void *operator new(size_t size) { return 0; } " |
| 1031 | // "Y *y = new Y;", OpCall)); |
| 1032 | EXPECT_TRUE(notMatches("class Y { }; " |
| 1033 | "void operator delete(void *p) { } " |
| 1034 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 1035 | // Binary operator |
| 1036 | EXPECT_TRUE(matches("class Y { }; " |
| 1037 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1038 | "Y a; Y b; bool c = a && b;", |
| 1039 | OpCall)); |
| 1040 | // No match -- normal operator, not an overloaded one. |
| 1041 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 1042 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 1043 | } |
| 1044 | |
| 1045 | TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { |
| 1046 | StatementMatcher OpCallAndAnd = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1047 | operatorCallExpr(hasOverloadedOperatorName("&&")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1048 | EXPECT_TRUE(matches("class Y { }; " |
| 1049 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1050 | "Y a; Y b; bool c = a && b;", OpCallAndAnd)); |
| 1051 | StatementMatcher OpCallLessLess = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1052 | operatorCallExpr(hasOverloadedOperatorName("<<")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1053 | EXPECT_TRUE(notMatches("class Y { }; " |
| 1054 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1055 | "Y a; Y b; bool c = a && b;", |
| 1056 | OpCallLessLess)); |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 1057 | DeclarationMatcher ClassWithOpStar = |
| 1058 | recordDecl(hasMethod(hasOverloadedOperatorName("*"))); |
| 1059 | EXPECT_TRUE(matches("class Y { int operator*(); };", |
| 1060 | ClassWithOpStar)); |
| 1061 | EXPECT_TRUE(notMatches("class Y { void myOperator(); };", |
| 1062 | ClassWithOpStar)) ; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Daniel Jasper | 278057f | 2012-11-15 03:29:05 +0000 | [diff] [blame] | 1065 | TEST(Matcher, NestedOverloadedOperatorCalls) { |
| 1066 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1067 | "class Y { }; " |
| 1068 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1069 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1070 | operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"), |
| 1071 | new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2))); |
| 1072 | EXPECT_TRUE(matches( |
| 1073 | "class Y { }; " |
| 1074 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1075 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1076 | operatorCallExpr(hasParent(operatorCallExpr())))); |
| 1077 | EXPECT_TRUE(matches( |
| 1078 | "class Y { }; " |
| 1079 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1080 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1081 | operatorCallExpr(hasDescendant(operatorCallExpr())))); |
| 1082 | } |
| 1083 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1084 | TEST(Matcher, ThisPointerType) { |
Manuel Klimek | 9f17408 | 2012-07-24 13:37:29 +0000 | [diff] [blame] | 1085 | StatementMatcher MethodOnY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1086 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1087 | |
| 1088 | EXPECT_TRUE( |
| 1089 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 1090 | MethodOnY)); |
| 1091 | EXPECT_TRUE( |
| 1092 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 1093 | MethodOnY)); |
| 1094 | EXPECT_TRUE( |
| 1095 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 1096 | MethodOnY)); |
| 1097 | EXPECT_TRUE( |
| 1098 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 1099 | MethodOnY)); |
| 1100 | EXPECT_TRUE( |
| 1101 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 1102 | MethodOnY)); |
| 1103 | |
| 1104 | EXPECT_TRUE(matches( |
| 1105 | "class Y {" |
| 1106 | " public: virtual void x();" |
| 1107 | "};" |
| 1108 | "class X : public Y {" |
| 1109 | " public: virtual void x();" |
| 1110 | "};" |
| 1111 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 1112 | } |
| 1113 | |
| 1114 | TEST(Matcher, VariableUsage) { |
| 1115 | StatementMatcher Reference = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1116 | declRefExpr(to( |
| 1117 | varDecl(hasInitializer( |
| 1118 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1119 | |
| 1120 | EXPECT_TRUE(matches( |
| 1121 | "class Y {" |
| 1122 | " public:" |
| 1123 | " bool x() const;" |
| 1124 | "};" |
| 1125 | "void z(const Y &y) {" |
| 1126 | " bool b = y.x();" |
| 1127 | " if (b) {}" |
| 1128 | "}", Reference)); |
| 1129 | |
| 1130 | EXPECT_TRUE(notMatches( |
| 1131 | "class Y {" |
| 1132 | " public:" |
| 1133 | " bool x() const;" |
| 1134 | "};" |
| 1135 | "void z(const Y &y) {" |
| 1136 | " bool b = y.x();" |
| 1137 | "}", Reference)); |
| 1138 | } |
| 1139 | |
Manuel Klimek | 8cb9bf5 | 2012-12-04 14:42:08 +0000 | [diff] [blame] | 1140 | TEST(Matcher, FindsVarDeclInFunctionParameter) { |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1141 | EXPECT_TRUE(matches( |
| 1142 | "void f(int i) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1143 | varDecl(hasName("i")))); |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1146 | TEST(Matcher, CalledVariable) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1147 | StatementMatcher CallOnVariableY = |
| 1148 | memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1149 | |
| 1150 | EXPECT_TRUE(matches( |
| 1151 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 1152 | EXPECT_TRUE(matches( |
| 1153 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 1154 | EXPECT_TRUE(matches( |
| 1155 | "class Y { public: void x(); };" |
| 1156 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 1157 | EXPECT_TRUE(matches( |
| 1158 | "class Y { public: void x(); };" |
| 1159 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 1160 | EXPECT_TRUE(notMatches( |
| 1161 | "class Y { public: void x(); };" |
| 1162 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 1163 | CallOnVariableY)); |
| 1164 | } |
| 1165 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1166 | TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) { |
| 1167 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 1168 | unaryExprOrTypeTraitExpr())); |
| 1169 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 1170 | alignOfExpr(anything()))); |
| 1171 | // FIXME: Uncomment once alignof is enabled. |
| 1172 | // EXPECT_TRUE(matches("void x() { int a = alignof(a); }", |
| 1173 | // unaryExprOrTypeTraitExpr())); |
| 1174 | // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }", |
| 1175 | // sizeOfExpr())); |
| 1176 | } |
| 1177 | |
| 1178 | TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) { |
| 1179 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1180 | hasArgumentOfType(asString("int"))))); |
| 1181 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1182 | hasArgumentOfType(asString("float"))))); |
| 1183 | EXPECT_TRUE(matches( |
| 1184 | "struct A {}; void x() { A a; int b = sizeof(a); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1185 | sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1186 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1187 | hasArgumentOfType(hasDeclaration(recordDecl(hasName("string"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1190 | TEST(MemberExpression, DoesNotMatchClasses) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1191 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1195 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | TEST(MemberExpression, MatchesVariable) { |
| 1199 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1200 | matches("class Y { void x() { this->y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1201 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1202 | matches("class Y { void x() { y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1203 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1204 | matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | TEST(MemberExpression, MatchesStaticVariable) { |
| 1208 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1209 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1210 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1211 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1212 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1213 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1216 | TEST(IsInteger, MatchesIntegers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1217 | EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger())))); |
| 1218 | EXPECT_TRUE(matches( |
| 1219 | "long long i = 0; void f(long long) { }; void g() {f(i);}", |
| 1220 | callExpr(hasArgument(0, declRefExpr( |
| 1221 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | TEST(IsInteger, ReportsNoFalsePositives) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1225 | EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1226 | EXPECT_TRUE(notMatches("struct T {}; T t; void f(T *) { }; void g() {f(&t);}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1227 | callExpr(hasArgument(0, declRefExpr( |
| 1228 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1231 | TEST(IsArrow, MatchesMemberVariablesViaArrow) { |
| 1232 | EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1233 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1234 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1235 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1236 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1237 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) { |
| 1241 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1242 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1243 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1244 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1245 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1246 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | TEST(IsArrow, MatchesMemberCallsViaArrow) { |
| 1250 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1251 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1252 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1253 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1254 | EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1255 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | TEST(Callee, MatchesDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1259 | StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1260 | |
| 1261 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 1262 | EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 1263 | } |
| 1264 | |
| 1265 | TEST(Callee, MatchesMemberExpressions) { |
| 1266 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1267 | callExpr(callee(memberExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1268 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1269 | notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | TEST(Function, MatchesFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1273 | StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1274 | |
| 1275 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 1276 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 1277 | |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1278 | #if !defined(_MSC_VER) |
| 1279 | // FIXME: Make this work for MSVC. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1280 | // Dependent contexts, but a non-dependent call. |
| 1281 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 1282 | CallFunctionF)); |
| 1283 | EXPECT_TRUE( |
| 1284 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 1285 | CallFunctionF)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1286 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1287 | |
| 1288 | // Depedent calls don't match. |
| 1289 | EXPECT_TRUE( |
| 1290 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 1291 | CallFunctionF)); |
| 1292 | EXPECT_TRUE( |
| 1293 | notMatches("void f(int);" |
| 1294 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 1295 | CallFunctionF)); |
| 1296 | } |
| 1297 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1298 | TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) { |
| 1299 | EXPECT_TRUE( |
| 1300 | matches("template <typename T> void f(T t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1301 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) { |
| 1305 | EXPECT_TRUE( |
| 1306 | notMatches("void f(double d); void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1307 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) { |
| 1311 | EXPECT_TRUE( |
| 1312 | notMatches("void g(); template <typename T> void f(T t) {}" |
| 1313 | "template <> void f(int t) { g(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1314 | functionTemplateDecl(hasName("f"), |
| 1315 | hasDescendant(declRefExpr(to( |
| 1316 | functionDecl(hasName("g")))))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1319 | TEST(Matcher, Argument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1320 | StatementMatcher CallArgumentY = callExpr( |
| 1321 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1322 | |
| 1323 | EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 1324 | EXPECT_TRUE( |
| 1325 | matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 1326 | EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 1327 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1328 | StatementMatcher WrongIndex = callExpr( |
| 1329 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1330 | EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 1331 | } |
| 1332 | |
| 1333 | TEST(Matcher, AnyArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1334 | StatementMatcher CallArgumentY = callExpr( |
| 1335 | hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1336 | EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 1337 | EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 1338 | EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 1339 | } |
| 1340 | |
| 1341 | TEST(Matcher, ArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1342 | StatementMatcher Call1Arg = callExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1343 | |
| 1344 | EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 1345 | EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 1346 | EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 1347 | } |
| 1348 | |
Daniel Jasper | 36e29d6 | 2012-12-04 11:54:27 +0000 | [diff] [blame] | 1349 | TEST(Matcher, ParameterCount) { |
| 1350 | DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1)); |
| 1351 | EXPECT_TRUE(matches("void f(int i) {}", Function1Arg)); |
| 1352 | EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg)); |
| 1353 | EXPECT_TRUE(notMatches("void f() {}", Function1Arg)); |
| 1354 | EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg)); |
| 1355 | } |
| 1356 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1357 | TEST(Matcher, References) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1358 | DeclarationMatcher ReferenceClassX = varDecl( |
| 1359 | hasType(references(recordDecl(hasName("X"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1360 | EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }", |
| 1361 | ReferenceClassX)); |
| 1362 | EXPECT_TRUE( |
| 1363 | matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
| 1364 | EXPECT_TRUE( |
| 1365 | notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 1366 | EXPECT_TRUE( |
| 1367 | notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 1368 | } |
| 1369 | |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 1370 | TEST(QualType, hasCanonicalType) { |
| 1371 | EXPECT_TRUE(notMatches("typedef int &int_ref;" |
| 1372 | "int a;" |
| 1373 | "int_ref b = a;", |
| 1374 | varDecl(hasType(qualType(referenceType()))))); |
| 1375 | EXPECT_TRUE( |
| 1376 | matches("typedef int &int_ref;" |
| 1377 | "int a;" |
| 1378 | "int_ref b = a;", |
| 1379 | varDecl(hasType(qualType(hasCanonicalType(referenceType())))))); |
| 1380 | } |
| 1381 | |
Edwin Vane | 7b69cd0 | 2013-04-02 18:15:55 +0000 | [diff] [blame] | 1382 | TEST(QualType, hasLocalQualifiers) { |
| 1383 | EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;", |
| 1384 | varDecl(hasType(hasLocalQualifiers())))); |
| 1385 | EXPECT_TRUE(matches("int *const j = nullptr;", |
| 1386 | varDecl(hasType(hasLocalQualifiers())))); |
| 1387 | EXPECT_TRUE(matches("int *volatile k;", |
| 1388 | varDecl(hasType(hasLocalQualifiers())))); |
| 1389 | EXPECT_TRUE(notMatches("int m;", |
| 1390 | varDecl(hasType(hasLocalQualifiers())))); |
| 1391 | } |
| 1392 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1393 | TEST(HasParameter, CallsInnerMatcher) { |
| 1394 | EXPECT_TRUE(matches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1395 | methodDecl(hasParameter(0, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1396 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1397 | methodDecl(hasParameter(0, hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 1401 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1402 | methodDecl(hasParameter(42, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 1406 | EXPECT_TRUE(matches("class X { void x(X x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1407 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1408 | EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1409 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1410 | EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1411 | methodDecl(hasParameter(0, |
| 1412 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1413 | EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1414 | methodDecl(hasParameter(0, |
| 1415 | hasType(references(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
| 1418 | TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 1419 | EXPECT_TRUE(matches("class Y {}; class X { void x(X x, Y y) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1420 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1421 | EXPECT_TRUE(matches("class Y {}; class X { void x(Y y, X x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1422 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1425 | TEST(Returns, MatchesReturnTypes) { |
| 1426 | EXPECT_TRUE(matches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1427 | functionDecl(returns(asString("int"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1428 | EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1429 | functionDecl(returns(asString("float"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1430 | EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1431 | functionDecl(returns(hasDeclaration( |
| 1432 | recordDecl(hasName("Y"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1435 | TEST(IsExternC, MatchesExternCFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1436 | EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC()))); |
| 1437 | EXPECT_TRUE(matches("extern \"C\" { void f() {} }", |
| 1438 | functionDecl(isExternC()))); |
| 1439 | EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC()))); |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1442 | TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 1443 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1444 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 1448 | EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1449 | methodDecl(hasAnyParameter(hasType(pointsTo( |
| 1450 | recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | TEST(HasName, MatchesParameterVariableDeclartions) { |
| 1454 | EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1455 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1456 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1457 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1460 | TEST(Matcher, MatchesClassTemplateSpecialization) { |
| 1461 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
| 1462 | "template<> struct A<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1463 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1464 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1465 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1466 | EXPECT_TRUE(notMatches("template<typename T> struct A {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1467 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Manuel Klimek | 1a68afd | 2013-06-20 13:08:29 +0000 | [diff] [blame] | 1470 | TEST(DeclaratorDecl, MatchesDeclaratorDecls) { |
| 1471 | EXPECT_TRUE(matches("int x;", declaratorDecl())); |
| 1472 | EXPECT_TRUE(notMatches("class A {};", declaratorDecl())); |
| 1473 | } |
| 1474 | |
| 1475 | TEST(ParmVarDecl, MatchesParmVars) { |
| 1476 | EXPECT_TRUE(matches("void f(int x);", parmVarDecl())); |
| 1477 | EXPECT_TRUE(notMatches("void f();", parmVarDecl())); |
| 1478 | } |
| 1479 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1480 | TEST(Matcher, MatchesTypeTemplateArgument) { |
| 1481 | EXPECT_TRUE(matches( |
| 1482 | "template<typename T> struct B {};" |
| 1483 | "B<int> b;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1484 | classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1485 | asString("int")))))); |
| 1486 | } |
| 1487 | |
| 1488 | TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { |
| 1489 | EXPECT_TRUE(matches( |
| 1490 | "struct B { int next; };" |
| 1491 | "template<int(B::*next_ptr)> struct A {};" |
| 1492 | "A<&B::next> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1493 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1494 | refersToDeclaration(fieldDecl(hasName("next"))))))); |
Daniel Jasper | aaa8e45 | 2012-09-29 15:55:18 +0000 | [diff] [blame] | 1495 | |
| 1496 | EXPECT_TRUE(notMatches( |
| 1497 | "template <typename T> struct A {};" |
| 1498 | "A<int> a;", |
| 1499 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1500 | refersToDeclaration(decl()))))); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | TEST(Matcher, MatchesSpecificArgument) { |
| 1504 | EXPECT_TRUE(matches( |
| 1505 | "template<typename T, typename U> class A {};" |
| 1506 | "A<bool, int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1507 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1508 | 1, refersToType(asString("int")))))); |
| 1509 | EXPECT_TRUE(notMatches( |
| 1510 | "template<typename T, typename U> class A {};" |
| 1511 | "A<int, bool> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1512 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1513 | 1, refersToType(asString("int")))))); |
| 1514 | } |
| 1515 | |
Daniel Jasper | f3197e9 | 2013-02-25 12:02:08 +0000 | [diff] [blame] | 1516 | TEST(Matcher, MatchesAccessSpecDecls) { |
| 1517 | EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl())); |
| 1518 | EXPECT_TRUE( |
| 1519 | matches("class C { public: int i; };", accessSpecDecl(isPublic()))); |
| 1520 | EXPECT_TRUE( |
| 1521 | notMatches("class C { public: int i; };", accessSpecDecl(isProtected()))); |
| 1522 | EXPECT_TRUE( |
| 1523 | notMatches("class C { public: int i; };", accessSpecDecl(isPrivate()))); |
| 1524 | |
| 1525 | EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl())); |
| 1526 | } |
| 1527 | |
Edwin Vane | 5771a2f | 2013-04-09 20:46:36 +0000 | [diff] [blame] | 1528 | TEST(Matcher, MatchesVirtualMethod) { |
| 1529 | EXPECT_TRUE(matches("class X { virtual int f(); };", |
| 1530 | methodDecl(isVirtual(), hasName("::X::f")))); |
| 1531 | EXPECT_TRUE(notMatches("class X { int f(); };", |
| 1532 | methodDecl(isVirtual()))); |
| 1533 | } |
| 1534 | |
Edwin Vane | 32a6ebc | 2013-05-09 17:00:17 +0000 | [diff] [blame] | 1535 | TEST(Matcher, MatchesConstMethod) { |
| 1536 | EXPECT_TRUE(matches("struct A { void foo() const; };", |
| 1537 | methodDecl(isConst()))); |
| 1538 | EXPECT_TRUE(notMatches("struct A { void foo(); };", |
| 1539 | methodDecl(isConst()))); |
| 1540 | } |
| 1541 | |
Edwin Vane | 5771a2f | 2013-04-09 20:46:36 +0000 | [diff] [blame] | 1542 | TEST(Matcher, MatchesOverridingMethod) { |
| 1543 | EXPECT_TRUE(matches("class X { virtual int f(); }; " |
| 1544 | "class Y : public X { int f(); };", |
| 1545 | methodDecl(isOverride(), hasName("::Y::f")))); |
| 1546 | EXPECT_TRUE(notMatches("class X { virtual int f(); }; " |
| 1547 | "class Y : public X { int f(); };", |
| 1548 | methodDecl(isOverride(), hasName("::X::f")))); |
| 1549 | EXPECT_TRUE(notMatches("class X { int f(); }; " |
| 1550 | "class Y : public X { int f(); };", |
| 1551 | methodDecl(isOverride()))); |
| 1552 | EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ", |
| 1553 | methodDecl(isOverride()))); |
| 1554 | } |
| 1555 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1556 | TEST(Matcher, ConstructorCall) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1557 | StatementMatcher Constructor = constructExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1558 | |
| 1559 | EXPECT_TRUE( |
| 1560 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 1561 | EXPECT_TRUE( |
| 1562 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 1563 | Constructor)); |
| 1564 | EXPECT_TRUE( |
| 1565 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1566 | Constructor)); |
| 1567 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 1568 | } |
| 1569 | |
| 1570 | TEST(Matcher, ConstructorArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1571 | StatementMatcher Constructor = constructExpr( |
| 1572 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1573 | |
| 1574 | EXPECT_TRUE( |
| 1575 | matches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1576 | Constructor)); |
| 1577 | EXPECT_TRUE( |
| 1578 | matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 1579 | Constructor)); |
| 1580 | EXPECT_TRUE( |
| 1581 | matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 1582 | Constructor)); |
| 1583 | EXPECT_TRUE( |
| 1584 | notMatches("class X { public: X(int); }; void x() { int z; X x(z); }", |
| 1585 | Constructor)); |
| 1586 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1587 | StatementMatcher WrongIndex = constructExpr( |
| 1588 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1589 | EXPECT_TRUE( |
| 1590 | notMatches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1591 | WrongIndex)); |
| 1592 | } |
| 1593 | |
| 1594 | TEST(Matcher, ConstructorArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1595 | StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1596 | |
| 1597 | EXPECT_TRUE( |
| 1598 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1599 | Constructor1Arg)); |
| 1600 | EXPECT_TRUE( |
| 1601 | matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 1602 | Constructor1Arg)); |
| 1603 | EXPECT_TRUE( |
| 1604 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1605 | Constructor1Arg)); |
| 1606 | EXPECT_TRUE( |
| 1607 | notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 1608 | Constructor1Arg)); |
| 1609 | } |
| 1610 | |
Manuel Klimek | 70b9db9 | 2012-10-23 10:40:50 +0000 | [diff] [blame] | 1611 | TEST(Matcher,ThisExpr) { |
| 1612 | EXPECT_TRUE( |
| 1613 | matches("struct X { int a; int f () { return a; } };", thisExpr())); |
| 1614 | EXPECT_TRUE( |
| 1615 | notMatches("struct X { int f () { int a; return a; } };", thisExpr())); |
| 1616 | } |
| 1617 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1618 | TEST(Matcher, BindTemporaryExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1619 | StatementMatcher TempExpression = bindTemporaryExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1620 | |
| 1621 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 1622 | |
| 1623 | EXPECT_TRUE( |
| 1624 | matches(ClassString + |
| 1625 | "string GetStringByValue();" |
| 1626 | "void FunctionTakesString(string s);" |
| 1627 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1628 | TempExpression)); |
| 1629 | |
| 1630 | EXPECT_TRUE( |
| 1631 | notMatches(ClassString + |
| 1632 | "string* GetStringPointer(); " |
| 1633 | "void FunctionTakesStringPtr(string* s);" |
| 1634 | "void run() {" |
| 1635 | " string* s = GetStringPointer();" |
| 1636 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1637 | " FunctionTakesStringPtr(s);" |
| 1638 | "}", |
| 1639 | TempExpression)); |
| 1640 | |
| 1641 | EXPECT_TRUE( |
| 1642 | notMatches("class no_dtor {};" |
| 1643 | "no_dtor GetObjByValue();" |
| 1644 | "void ConsumeObj(no_dtor param);" |
| 1645 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 1646 | TempExpression)); |
| 1647 | } |
| 1648 | |
Sam Panzer | e16acd3 | 2012-08-24 22:04:44 +0000 | [diff] [blame] | 1649 | TEST(MaterializeTemporaryExpr, MatchesTemporary) { |
| 1650 | std::string ClassString = |
| 1651 | "class string { public: string(); int length(); }; "; |
| 1652 | |
| 1653 | EXPECT_TRUE( |
| 1654 | matches(ClassString + |
| 1655 | "string GetStringByValue();" |
| 1656 | "void FunctionTakesString(string s);" |
| 1657 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1658 | materializeTemporaryExpr())); |
| 1659 | |
| 1660 | EXPECT_TRUE( |
| 1661 | notMatches(ClassString + |
| 1662 | "string* GetStringPointer(); " |
| 1663 | "void FunctionTakesStringPtr(string* s);" |
| 1664 | "void run() {" |
| 1665 | " string* s = GetStringPointer();" |
| 1666 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1667 | " FunctionTakesStringPtr(s);" |
| 1668 | "}", |
| 1669 | materializeTemporaryExpr())); |
| 1670 | |
| 1671 | EXPECT_TRUE( |
| 1672 | notMatches(ClassString + |
| 1673 | "string GetStringByValue();" |
| 1674 | "void run() { int k = GetStringByValue().length(); }", |
| 1675 | materializeTemporaryExpr())); |
| 1676 | |
| 1677 | EXPECT_TRUE( |
| 1678 | notMatches(ClassString + |
| 1679 | "string GetStringByValue();" |
| 1680 | "void run() { GetStringByValue(); }", |
| 1681 | materializeTemporaryExpr())); |
| 1682 | } |
| 1683 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1684 | TEST(ConstructorDeclaration, SimpleCase) { |
| 1685 | EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1686 | constructorDecl(ofClass(hasName("Foo"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1687 | EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1688 | constructorDecl(ofClass(hasName("Bar"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | TEST(ConstructorDeclaration, IsImplicit) { |
| 1692 | // This one doesn't match because the constructor is not added by the |
| 1693 | // compiler (it is not needed). |
| 1694 | EXPECT_TRUE(notMatches("class Foo { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1695 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1696 | // The compiler added the implicit default constructor. |
| 1697 | EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1698 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1699 | EXPECT_TRUE(matches("class Foo { Foo(){} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1700 | constructorDecl(unless(isImplicit())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1703 | TEST(DestructorDeclaration, MatchesVirtualDestructor) { |
| 1704 | EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1705 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1709 | EXPECT_TRUE(notMatches("class Foo {};", |
| 1710 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1713 | TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1714 | EXPECT_TRUE(notMatches( |
| 1715 | "class Foo { Foo() { } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1716 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1717 | EXPECT_TRUE(matches( |
| 1718 | "class Foo {" |
| 1719 | " Foo() : foo_() { }" |
| 1720 | " int foo_;" |
| 1721 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1722 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
| 1725 | TEST(HasAnyConstructorInitializer, ForField) { |
| 1726 | static const char Code[] = |
| 1727 | "class Baz { };" |
| 1728 | "class Foo {" |
| 1729 | " Foo() : foo_() { }" |
| 1730 | " Baz foo_;" |
| 1731 | " Baz bar_;" |
| 1732 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1733 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1734 | forField(hasType(recordDecl(hasName("Baz")))))))); |
| 1735 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1736 | forField(hasName("foo_")))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1737 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1738 | forField(hasType(recordDecl(hasName("Bar")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1742 | static const char Code[] = |
| 1743 | "class Foo {" |
| 1744 | " Foo() : foo_(0) { }" |
| 1745 | " int foo_;" |
| 1746 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1747 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1748 | withInitializer(integerLiteral(equals(0))))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1749 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1750 | withInitializer(integerLiteral(equals(1))))))); |
| 1751 | } |
| 1752 | |
| 1753 | TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1754 | static const char Code[] = |
| 1755 | "struct Bar { Bar(){} };" |
| 1756 | "class Foo {" |
| 1757 | " Foo() : foo_() { }" |
| 1758 | " Bar foo_;" |
| 1759 | " Bar bar_;" |
| 1760 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1761 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1762 | allOf(forField(hasName("foo_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1763 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1764 | allOf(forField(hasName("bar_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1765 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1766 | allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1767 | } |
| 1768 | |
| 1769 | TEST(Matcher, NewExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1770 | StatementMatcher New = newExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1771 | |
| 1772 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 1773 | EXPECT_TRUE( |
| 1774 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 1775 | EXPECT_TRUE( |
| 1776 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1777 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 1778 | } |
| 1779 | |
| 1780 | TEST(Matcher, NewExpressionArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1781 | StatementMatcher New = constructExpr( |
| 1782 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1783 | |
| 1784 | EXPECT_TRUE( |
| 1785 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1786 | New)); |
| 1787 | EXPECT_TRUE( |
| 1788 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1789 | New)); |
| 1790 | EXPECT_TRUE( |
| 1791 | notMatches("class X { public: X(int); }; void x() { int z; new X(z); }", |
| 1792 | New)); |
| 1793 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1794 | StatementMatcher WrongIndex = constructExpr( |
| 1795 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1796 | EXPECT_TRUE( |
| 1797 | notMatches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1798 | WrongIndex)); |
| 1799 | } |
| 1800 | |
| 1801 | TEST(Matcher, NewExpressionArgumentCount) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1802 | StatementMatcher New = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1803 | |
| 1804 | EXPECT_TRUE( |
| 1805 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1806 | EXPECT_TRUE( |
| 1807 | notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }", |
| 1808 | New)); |
| 1809 | } |
| 1810 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1811 | TEST(Matcher, DeleteExpression) { |
| 1812 | EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1813 | deleteExpr())); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1816 | TEST(Matcher, DefaultArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1817 | StatementMatcher Arg = defaultArgExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1818 | |
| 1819 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 1820 | EXPECT_TRUE( |
| 1821 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 1822 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 1823 | } |
| 1824 | |
| 1825 | TEST(Matcher, StringLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1826 | StatementMatcher Literal = stringLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1827 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 1828 | // wide string |
| 1829 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 1830 | // with escaped characters |
| 1831 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 1832 | // no matching -- though the data type is the same, there is no string literal |
| 1833 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 1834 | } |
| 1835 | |
| 1836 | TEST(Matcher, CharacterLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1837 | StatementMatcher CharLiteral = characterLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1838 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 1839 | // wide character |
| 1840 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 1841 | // wide character, Hex encoded, NOT MATCHED! |
| 1842 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 1843 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 1844 | } |
| 1845 | |
| 1846 | TEST(Matcher, IntegerLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1847 | StatementMatcher HasIntLiteral = integerLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1848 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 1849 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 1850 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 1851 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 1852 | |
| 1853 | // Non-matching cases (character literals, float and double) |
| 1854 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 1855 | HasIntLiteral)); // this is actually a character |
| 1856 | // literal cast to int |
| 1857 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 1858 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 1859 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 1860 | } |
| 1861 | |
Daniel Jasper | c5ae717 | 2013-07-26 18:52:58 +0000 | [diff] [blame] | 1862 | TEST(Matcher, FloatLiterals) { |
| 1863 | StatementMatcher HasFloatLiteral = floatLiteral(); |
| 1864 | EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral)); |
| 1865 | EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral)); |
| 1866 | EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral)); |
| 1867 | EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral)); |
| 1868 | EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral)); |
| 1869 | |
| 1870 | EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral)); |
| 1871 | } |
| 1872 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1873 | TEST(Matcher, NullPtrLiteral) { |
| 1874 | EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr())); |
| 1875 | } |
| 1876 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 1877 | TEST(Matcher, AsmStatement) { |
| 1878 | EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt())); |
| 1879 | } |
| 1880 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1881 | TEST(Matcher, Conditions) { |
| 1882 | StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true)))); |
| 1883 | |
| 1884 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 1885 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 1886 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 1887 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 1888 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 1889 | } |
| 1890 | |
| 1891 | TEST(MatchBinaryOperator, HasOperatorName) { |
| 1892 | StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1893 | |
| 1894 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1895 | EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1896 | } |
| 1897 | |
| 1898 | TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1899 | StatementMatcher OperatorTrueFalse = |
| 1900 | binaryOperator(hasLHS(boolLiteral(equals(true))), |
| 1901 | hasRHS(boolLiteral(equals(false)))); |
| 1902 | |
| 1903 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1904 | EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1905 | EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1906 | } |
| 1907 | |
| 1908 | TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1909 | StatementMatcher HasOperand = |
| 1910 | binaryOperator(hasEitherOperand(boolLiteral(equals(false)))); |
| 1911 | |
| 1912 | EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 1913 | EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 1914 | EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 1915 | } |
| 1916 | |
| 1917 | TEST(Matcher, BinaryOperatorTypes) { |
| 1918 | // Integration test that verifies the AST provides all binary operators in |
| 1919 | // a way we expect. |
| 1920 | // FIXME: Operator ',' |
| 1921 | EXPECT_TRUE( |
| 1922 | matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 1923 | EXPECT_TRUE( |
| 1924 | matches("bool b; bool c = (b = true);", |
| 1925 | binaryOperator(hasOperatorName("=")))); |
| 1926 | EXPECT_TRUE( |
| 1927 | matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 1928 | EXPECT_TRUE( |
| 1929 | matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 1930 | EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 1931 | EXPECT_TRUE( |
| 1932 | matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 1933 | EXPECT_TRUE( |
| 1934 | matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 1935 | EXPECT_TRUE( |
| 1936 | matches("int i = 1; int j = (i <<= 2);", |
| 1937 | binaryOperator(hasOperatorName("<<=")))); |
| 1938 | EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 1939 | EXPECT_TRUE( |
| 1940 | matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 1941 | EXPECT_TRUE( |
| 1942 | matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 1943 | EXPECT_TRUE( |
| 1944 | matches("int i = 1; int j = (i >>= 2);", |
| 1945 | binaryOperator(hasOperatorName(">>=")))); |
| 1946 | EXPECT_TRUE( |
| 1947 | matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 1948 | EXPECT_TRUE( |
| 1949 | matches("int i = 42; int j = (i ^= 42);", |
| 1950 | binaryOperator(hasOperatorName("^=")))); |
| 1951 | EXPECT_TRUE( |
| 1952 | matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 1953 | EXPECT_TRUE( |
| 1954 | matches("int i = 42; int j = (i %= 42);", |
| 1955 | binaryOperator(hasOperatorName("%=")))); |
| 1956 | EXPECT_TRUE( |
| 1957 | matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 1958 | EXPECT_TRUE( |
| 1959 | matches("bool b = true && false;", |
| 1960 | binaryOperator(hasOperatorName("&&")))); |
| 1961 | EXPECT_TRUE( |
| 1962 | matches("bool b = true; bool c = (b &= false);", |
| 1963 | binaryOperator(hasOperatorName("&=")))); |
| 1964 | EXPECT_TRUE( |
| 1965 | matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 1966 | EXPECT_TRUE( |
| 1967 | matches("bool b = true || false;", |
| 1968 | binaryOperator(hasOperatorName("||")))); |
| 1969 | EXPECT_TRUE( |
| 1970 | matches("bool b = true; bool c = (b |= false);", |
| 1971 | binaryOperator(hasOperatorName("|=")))); |
| 1972 | EXPECT_TRUE( |
| 1973 | matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 1974 | EXPECT_TRUE( |
| 1975 | matches("int i = 42; int j = (i *= 23);", |
| 1976 | binaryOperator(hasOperatorName("*=")))); |
| 1977 | EXPECT_TRUE( |
| 1978 | matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 1979 | EXPECT_TRUE( |
| 1980 | matches("int i = 42; int j = (i /= 23);", |
| 1981 | binaryOperator(hasOperatorName("/=")))); |
| 1982 | EXPECT_TRUE( |
| 1983 | matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 1984 | EXPECT_TRUE( |
| 1985 | matches("int i = 42; int j = (i += 23);", |
| 1986 | binaryOperator(hasOperatorName("+=")))); |
| 1987 | EXPECT_TRUE( |
| 1988 | matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 1989 | EXPECT_TRUE( |
| 1990 | matches("int i = 42; int j = (i -= 23);", |
| 1991 | binaryOperator(hasOperatorName("-=")))); |
| 1992 | EXPECT_TRUE( |
| 1993 | matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 1994 | binaryOperator(hasOperatorName("->*")))); |
| 1995 | EXPECT_TRUE( |
| 1996 | matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 1997 | binaryOperator(hasOperatorName(".*")))); |
| 1998 | |
| 1999 | // Member expressions as operators are not supported in matches. |
| 2000 | EXPECT_TRUE( |
| 2001 | notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 2002 | binaryOperator(hasOperatorName("->")))); |
| 2003 | |
| 2004 | // Initializer assignments are not represented as operator equals. |
| 2005 | EXPECT_TRUE( |
| 2006 | notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 2007 | |
| 2008 | // Array indexing is not represented as operator. |
| 2009 | EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 2010 | |
| 2011 | // Overloaded operators do not match at all. |
| 2012 | EXPECT_TRUE(notMatches( |
| 2013 | "struct A { bool operator&&(const A &a) const { return false; } };" |
| 2014 | "void x() { A a, b; a && b; }", |
| 2015 | binaryOperator())); |
| 2016 | } |
| 2017 | |
| 2018 | TEST(MatchUnaryOperator, HasOperatorName) { |
| 2019 | StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 2020 | |
| 2021 | EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 2022 | EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 2023 | } |
| 2024 | |
| 2025 | TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 2026 | StatementMatcher OperatorOnFalse = |
| 2027 | unaryOperator(hasUnaryOperand(boolLiteral(equals(false)))); |
| 2028 | |
| 2029 | EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 2030 | EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 2031 | } |
| 2032 | |
| 2033 | TEST(Matcher, UnaryOperatorTypes) { |
| 2034 | // Integration test that verifies the AST provides all unary operators in |
| 2035 | // a way we expect. |
| 2036 | EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 2037 | EXPECT_TRUE( |
| 2038 | matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 2039 | EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 2040 | EXPECT_TRUE( |
| 2041 | matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 2042 | EXPECT_TRUE( |
| 2043 | matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 2044 | EXPECT_TRUE( |
| 2045 | matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 2046 | EXPECT_TRUE( |
| 2047 | matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 2048 | EXPECT_TRUE( |
| 2049 | matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 2050 | EXPECT_TRUE( |
| 2051 | matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 2052 | EXPECT_TRUE( |
| 2053 | matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 2054 | |
| 2055 | // We don't match conversion operators. |
| 2056 | EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 2057 | |
| 2058 | // Function calls are not represented as operator. |
| 2059 | EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 2060 | |
| 2061 | // Overloaded operators do not match at all. |
| 2062 | // FIXME: We probably want to add that. |
| 2063 | EXPECT_TRUE(notMatches( |
| 2064 | "struct A { bool operator!() const { return false; } };" |
| 2065 | "void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 2066 | } |
| 2067 | |
| 2068 | TEST(Matcher, ConditionalOperator) { |
| 2069 | StatementMatcher Conditional = conditionalOperator( |
| 2070 | hasCondition(boolLiteral(equals(true))), |
| 2071 | hasTrueExpression(boolLiteral(equals(false)))); |
| 2072 | |
| 2073 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 2074 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 2075 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 2076 | |
| 2077 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 2078 | hasFalseExpression(boolLiteral(equals(false)))); |
| 2079 | |
| 2080 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 2081 | EXPECT_TRUE( |
| 2082 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 2083 | } |
| 2084 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2085 | TEST(ArraySubscriptMatchers, ArraySubscripts) { |
| 2086 | EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }", |
| 2087 | arraySubscriptExpr())); |
| 2088 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }", |
| 2089 | arraySubscriptExpr())); |
| 2090 | } |
| 2091 | |
| 2092 | TEST(ArraySubscriptMatchers, ArrayIndex) { |
| 2093 | EXPECT_TRUE(matches( |
| 2094 | "int i[2]; void f() { i[1] = 1; }", |
| 2095 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 2096 | EXPECT_TRUE(matches( |
| 2097 | "int i[2]; void f() { 1[i] = 1; }", |
| 2098 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 2099 | EXPECT_TRUE(notMatches( |
| 2100 | "int i[2]; void f() { i[1] = 1; }", |
| 2101 | arraySubscriptExpr(hasIndex(integerLiteral(equals(0)))))); |
| 2102 | } |
| 2103 | |
| 2104 | TEST(ArraySubscriptMatchers, MatchesArrayBase) { |
| 2105 | EXPECT_TRUE(matches( |
| 2106 | "int i[2]; void f() { i[1] = 2; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2107 | arraySubscriptExpr(hasBase(implicitCastExpr( |
| 2108 | hasSourceExpression(declRefExpr())))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2111 | TEST(Matcher, HasNameSupportsNamespaces) { |
| 2112 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2113 | recordDecl(hasName("a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2114 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2115 | recordDecl(hasName("::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2116 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2117 | recordDecl(hasName("b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2118 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2119 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2120 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2121 | recordDecl(hasName("c::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2122 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2123 | recordDecl(hasName("a::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2124 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2125 | recordDecl(hasName("a::b::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2126 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2127 | recordDecl(hasName("::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2128 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2129 | recordDecl(hasName("::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2130 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2131 | recordDecl(hasName("z::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2132 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2133 | recordDecl(hasName("a+b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2134 | EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2135 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | TEST(Matcher, HasNameSupportsOuterClasses) { |
| 2139 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2140 | matches("class A { class B { class C; }; };", |
| 2141 | recordDecl(hasName("A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2142 | EXPECT_TRUE( |
| 2143 | matches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2144 | recordDecl(hasName("::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2145 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2146 | matches("class A { class B { class C; }; };", |
| 2147 | recordDecl(hasName("B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2148 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2149 | matches("class A { class B { class C; }; };", |
| 2150 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2151 | EXPECT_TRUE( |
| 2152 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2153 | recordDecl(hasName("c::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2154 | EXPECT_TRUE( |
| 2155 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2156 | recordDecl(hasName("A::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2157 | EXPECT_TRUE( |
| 2158 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2159 | recordDecl(hasName("A::B::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2160 | EXPECT_TRUE( |
| 2161 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2162 | recordDecl(hasName("::C")))); |
| 2163 | EXPECT_TRUE( |
| 2164 | notMatches("class A { class B { class C; }; };", |
| 2165 | recordDecl(hasName("::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2166 | EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2167 | recordDecl(hasName("z::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2168 | EXPECT_TRUE( |
| 2169 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2170 | recordDecl(hasName("A+B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
| 2173 | TEST(Matcher, IsDefinition) { |
| 2174 | DeclarationMatcher DefinitionOfClassA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2175 | recordDecl(hasName("A"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2176 | EXPECT_TRUE(matches("class A {};", DefinitionOfClassA)); |
| 2177 | EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA)); |
| 2178 | |
| 2179 | DeclarationMatcher DefinitionOfVariableA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2180 | varDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2181 | EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 2182 | EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 2183 | |
| 2184 | DeclarationMatcher DefinitionOfMethodA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2185 | methodDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2186 | EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 2187 | EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 2188 | } |
| 2189 | |
| 2190 | TEST(Matcher, OfClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2191 | StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2192 | ofClass(hasName("X"))))); |
| 2193 | |
| 2194 | EXPECT_TRUE( |
| 2195 | matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 2196 | EXPECT_TRUE( |
| 2197 | matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 2198 | Constructor)); |
| 2199 | EXPECT_TRUE( |
| 2200 | notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 2201 | Constructor)); |
| 2202 | } |
| 2203 | |
| 2204 | TEST(Matcher, VisitsTemplateInstantiations) { |
| 2205 | EXPECT_TRUE(matches( |
| 2206 | "class A { public: void x(); };" |
| 2207 | "template <typename T> class B { public: void y() { T t; t.x(); } };" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2208 | "void f() { B<A> b; b.y(); }", |
| 2209 | callExpr(callee(methodDecl(hasName("x")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2210 | |
| 2211 | EXPECT_TRUE(matches( |
| 2212 | "class A { public: void x(); };" |
| 2213 | "class C {" |
| 2214 | " public:" |
| 2215 | " template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 2216 | "};" |
| 2217 | "void f() {" |
| 2218 | " C::B<A> b; b.y();" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2219 | "}", |
| 2220 | recordDecl(hasName("C"), |
| 2221 | hasDescendant(callExpr(callee(methodDecl(hasName("x")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2224 | TEST(Matcher, HandlesNullQualTypes) { |
| 2225 | // FIXME: Add a Type matcher so we can replace uses of this |
| 2226 | // variable with Type(True()) |
| 2227 | const TypeMatcher AnyType = anything(); |
| 2228 | |
| 2229 | // We don't really care whether this matcher succeeds; we're testing that |
| 2230 | // it completes without crashing. |
| 2231 | EXPECT_TRUE(matches( |
| 2232 | "struct A { };" |
| 2233 | "template <typename T>" |
| 2234 | "void f(T t) {" |
| 2235 | " T local_t(t /* this becomes a null QualType in the AST */);" |
| 2236 | "}" |
| 2237 | "void g() {" |
| 2238 | " f(0);" |
| 2239 | "}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2240 | expr(hasType(TypeMatcher( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2241 | anyOf( |
| 2242 | TypeMatcher(hasDeclaration(anything())), |
| 2243 | pointsTo(AnyType), |
| 2244 | references(AnyType) |
| 2245 | // Other QualType matchers should go here. |
| 2246 | )))))); |
| 2247 | } |
| 2248 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2249 | // For testing AST_MATCHER_P(). |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2250 | AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2251 | // Make sure all special variables are used: node, match_finder, |
| 2252 | // bound_nodes_builder, and the parameter named 'AMatcher'. |
| 2253 | return AMatcher.matches(Node, Finder, Builder); |
| 2254 | } |
| 2255 | |
| 2256 | TEST(AstMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2257 | DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2258 | |
| 2259 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2260 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2261 | |
| 2262 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2263 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2264 | |
| 2265 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2266 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | AST_POLYMORPHIC_MATCHER_P( |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 2270 | polymorphicHas, |
| 2271 | AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt), |
| 2272 | internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2273 | return Finder->matchesChildOf( |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 2274 | Node, AMatcher, Builder, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2275 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
| 2276 | ASTMatchFinder::BK_First); |
| 2277 | } |
| 2278 | |
| 2279 | TEST(AstPolymorphicMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2280 | DeclarationMatcher HasClassB = |
| 2281 | polymorphicHas(recordDecl(hasName("B")).bind("b")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2282 | |
| 2283 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2284 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2285 | |
| 2286 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2287 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2288 | |
| 2289 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2290 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2291 | |
| 2292 | StatementMatcher StatementHasClassB = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2293 | polymorphicHas(recordDecl(hasName("B"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2294 | |
| 2295 | EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB)); |
| 2296 | } |
| 2297 | |
| 2298 | TEST(For, FindsForLoops) { |
| 2299 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 2300 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 2301 | EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };" |
| 2302 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2303 | forStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2306 | TEST(For, ForLoopInternals) { |
| 2307 | EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", |
| 2308 | forStmt(hasCondition(anything())))); |
| 2309 | EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }", |
| 2310 | forStmt(hasLoopInit(anything())))); |
| 2311 | } |
| 2312 | |
| 2313 | TEST(For, NegativeForLoopInternals) { |
| 2314 | EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2315 | forStmt(hasCondition(expr())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2316 | EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }", |
| 2317 | forStmt(hasLoopInit(anything())))); |
| 2318 | } |
| 2319 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2320 | TEST(For, ReportsNoFalsePositives) { |
| 2321 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 2322 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 2323 | } |
| 2324 | |
| 2325 | TEST(CompoundStatement, HandlesSimpleCases) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2326 | EXPECT_TRUE(notMatches("void f();", compoundStmt())); |
| 2327 | EXPECT_TRUE(matches("void f() {}", compoundStmt())); |
| 2328 | EXPECT_TRUE(matches("void f() {{}}", compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 2332 | // It's not a compound statement just because there's "{}" in the source |
| 2333 | // text. This is an AST search, not grep. |
| 2334 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2335 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2336 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2337 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2340 | TEST(HasBody, FindsBodyOfForWhileDoLoops) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2341 | EXPECT_TRUE(matches("void f() { for(;;) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2342 | forStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2343 | EXPECT_TRUE(notMatches("void f() { for(;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2344 | forStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2345 | EXPECT_TRUE(matches("void f() { while(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2346 | whileStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2347 | EXPECT_TRUE(matches("void f() { do {} while(true); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2348 | doStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 2352 | // The simplest case: every compound statement is in a function |
| 2353 | // definition, and the function body itself must be a compound |
| 2354 | // statement. |
| 2355 | EXPECT_TRUE(matches("void f() { for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2356 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | TEST(HasAnySubstatement, IsNotRecursive) { |
| 2360 | // It's really "has any immediate substatement". |
| 2361 | EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2362 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 2366 | EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2367 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
| 2370 | TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 2371 | EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2372 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) { |
| 2376 | EXPECT_TRUE(matches("void f() { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2377 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2378 | EXPECT_TRUE(notMatches("void f() {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2379 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | TEST(StatementCountIs, AppearsToMatchOnlyOneCount) { |
| 2383 | EXPECT_TRUE(matches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2384 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2385 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2386 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2387 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2388 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | TEST(StatementCountIs, WorksWithMultipleStatements) { |
| 2392 | EXPECT_TRUE(matches("void f() { 1; 2; 3; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2393 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | TEST(StatementCountIs, WorksWithNestedCompoundStatements) { |
| 2397 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2398 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2399 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2400 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2401 | EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2402 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2403 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2404 | compoundStmt(statementCountIs(4)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | TEST(Member, WorksInSimplestCase) { |
| 2408 | EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2409 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
| 2412 | TEST(Member, DoesNotMatchTheBaseExpression) { |
| 2413 | // Don't pick out the wrong part of the member expression, this should |
| 2414 | // be checking the member (name) only. |
| 2415 | EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2416 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | TEST(Member, MatchesInMemberFunctionCall) { |
| 2420 | EXPECT_TRUE(matches("void f() {" |
| 2421 | " struct { void first() {}; } s;" |
| 2422 | " s.first();" |
| 2423 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2424 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Daniel Jasper | c711af2 | 2012-10-23 15:46:39 +0000 | [diff] [blame] | 2427 | TEST(Member, MatchesMember) { |
| 2428 | EXPECT_TRUE(matches( |
| 2429 | "struct A { int i; }; void f() { A a; a.i = 2; }", |
| 2430 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2431 | EXPECT_TRUE(notMatches( |
| 2432 | "struct A { float f; }; void f() { A a; a.f = 2.0f; }", |
| 2433 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2434 | } |
| 2435 | |
Daniel Jasper | f3197e9 | 2013-02-25 12:02:08 +0000 | [diff] [blame] | 2436 | TEST(Member, UnderstandsAccess) { |
| 2437 | EXPECT_TRUE(matches( |
| 2438 | "struct A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2439 | EXPECT_TRUE(notMatches( |
| 2440 | "struct A { int i; };", fieldDecl(isProtected(), hasName("i")))); |
| 2441 | EXPECT_TRUE(notMatches( |
| 2442 | "struct A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2443 | |
| 2444 | EXPECT_TRUE(notMatches( |
| 2445 | "class A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2446 | EXPECT_TRUE(notMatches( |
| 2447 | "class A { int i; };", fieldDecl(isProtected(), hasName("i")))); |
| 2448 | EXPECT_TRUE(matches( |
| 2449 | "class A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2450 | |
| 2451 | EXPECT_TRUE(notMatches( |
| 2452 | "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2453 | EXPECT_TRUE(matches("class A { protected: int i; };", |
| 2454 | fieldDecl(isProtected(), hasName("i")))); |
| 2455 | EXPECT_TRUE(notMatches( |
| 2456 | "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2457 | |
| 2458 | // Non-member decls have the AccessSpecifier AS_none and thus aren't matched. |
| 2459 | EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i")))); |
| 2460 | EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i")))); |
| 2461 | EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i")))); |
| 2462 | } |
| 2463 | |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2464 | TEST(Member, MatchesMemberAllocationFunction) { |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2465 | // Fails in C++11 mode |
| 2466 | EXPECT_TRUE(matchesConditionally( |
| 2467 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2468 | "class X { void *operator new(std::size_t); };", |
| 2469 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2470 | |
| 2471 | EXPECT_TRUE(matches("class X { void operator delete(void*); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2472 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2473 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2474 | // Fails in C++11 mode |
| 2475 | EXPECT_TRUE(matchesConditionally( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2476 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2477 | "class X { void operator delete[](void*, std::size_t); };", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2478 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2481 | TEST(HasObjectExpression, DoesNotMatchMember) { |
| 2482 | EXPECT_TRUE(notMatches( |
| 2483 | "class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2484 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | TEST(HasObjectExpression, MatchesBaseOfVariable) { |
| 2488 | EXPECT_TRUE(matches( |
| 2489 | "struct X { int m; }; void f(X x) { x.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2490 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2491 | EXPECT_TRUE(matches( |
| 2492 | "struct X { int m; }; void f(X* x) { x->m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2493 | memberExpr(hasObjectExpression( |
| 2494 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | TEST(HasObjectExpression, |
| 2498 | MatchesObjectExpressionOfImplicitlyFormedMemberExpression) { |
| 2499 | EXPECT_TRUE(matches( |
| 2500 | "class X {}; struct S { X m; void f() { this->m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2501 | memberExpr(hasObjectExpression( |
| 2502 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2503 | EXPECT_TRUE(matches( |
| 2504 | "class X {}; struct S { X m; void f() { m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2505 | memberExpr(hasObjectExpression( |
| 2506 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
| 2509 | TEST(Field, DoesNotMatchNonFieldMembers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2510 | EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m")))); |
| 2511 | EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m")))); |
| 2512 | EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m")))); |
| 2513 | EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | TEST(Field, MatchesField) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2517 | EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | TEST(IsConstQualified, MatchesConstInt) { |
| 2521 | EXPECT_TRUE(matches("const int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2522 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | TEST(IsConstQualified, MatchesConstPointer) { |
| 2526 | EXPECT_TRUE(matches("int i = 42; int* const p(&i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2527 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | TEST(IsConstQualified, MatchesThroughTypedef) { |
| 2531 | EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2532 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2533 | EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2534 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | TEST(IsConstQualified, DoesNotMatchInappropriately) { |
| 2538 | EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2539 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2540 | EXPECT_TRUE(notMatches("int const* p;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2541 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2544 | TEST(CastExpression, MatchesExplicitCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2545 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr())); |
| 2546 | EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr())); |
| 2547 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr())); |
| 2548 | EXPECT_TRUE(matches("char c = char(0);", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2549 | } |
| 2550 | TEST(CastExpression, MatchesImplicitCasts) { |
| 2551 | // This test creates an implicit cast from int to char. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2552 | EXPECT_TRUE(matches("char c = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2553 | // This test creates an implicit cast from lvalue to rvalue. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2554 | EXPECT_TRUE(matches("char c = 0, d = c;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | TEST(CastExpression, DoesNotMatchNonCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2558 | EXPECT_TRUE(notMatches("char c = '0';", castExpr())); |
| 2559 | EXPECT_TRUE(notMatches("char c, &q = c;", castExpr())); |
| 2560 | EXPECT_TRUE(notMatches("int i = (0);", castExpr())); |
| 2561 | EXPECT_TRUE(notMatches("int i = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2564 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 2565 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2566 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2570 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2571 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2572 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2573 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2574 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2575 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2576 | "B b;" |
| 2577 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2578 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | TEST(FunctionalCast, MatchesSimpleCase) { |
| 2582 | std::string foo_class = "class Foo { public: Foo(char*); };"; |
| 2583 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2584 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
| 2588 | std::string FooClass = "class Foo { public: Foo(char*); };"; |
| 2589 | EXPECT_TRUE( |
| 2590 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2591 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2592 | EXPECT_TRUE( |
| 2593 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2594 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | TEST(DynamicCast, MatchesSimpleCase) { |
| 2598 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2599 | "B b;" |
| 2600 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2601 | dynamicCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | TEST(StaticCast, MatchesSimpleCase) { |
| 2605 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2606 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2610 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2611 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2612 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2613 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2614 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2615 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2616 | "B b;" |
| 2617 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2618 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
Daniel Jasper | e6d2a96 | 2012-09-18 13:36:17 +0000 | [diff] [blame] | 2621 | TEST(CStyleCast, MatchesSimpleCase) { |
| 2622 | EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr())); |
| 2623 | } |
| 2624 | |
| 2625 | TEST(CStyleCast, DoesNotMatchOtherCasts) { |
| 2626 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);" |
| 2627 | "char q, *r = const_cast<char*>(&q);" |
| 2628 | "void* s = reinterpret_cast<char*>(&s);" |
| 2629 | "struct B { virtual ~B() {} }; struct D : B {};" |
| 2630 | "B b;" |
| 2631 | "D* t = dynamic_cast<D*>(&b);", |
| 2632 | cStyleCastExpr())); |
| 2633 | } |
| 2634 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2635 | TEST(HasDestinationType, MatchesSimpleCase) { |
| 2636 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2637 | staticCastExpr(hasDestinationType( |
| 2638 | pointsTo(TypeMatcher(anything())))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2641 | TEST(HasImplicitDestinationType, MatchesSimpleCase) { |
| 2642 | // This test creates an implicit const cast. |
| 2643 | EXPECT_TRUE(matches("int x; const int i = x;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2644 | implicitCastExpr( |
| 2645 | hasImplicitDestinationType(isInteger())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2646 | // This test creates an implicit array-to-pointer cast. |
| 2647 | EXPECT_TRUE(matches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2648 | implicitCastExpr(hasImplicitDestinationType( |
| 2649 | pointsTo(TypeMatcher(anything())))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2650 | } |
| 2651 | |
| 2652 | TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) { |
| 2653 | // This test creates an implicit cast from int to char. |
| 2654 | EXPECT_TRUE(notMatches("char c = 0;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2655 | implicitCastExpr(hasImplicitDestinationType( |
| 2656 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2657 | // This test creates an implicit array-to-pointer cast. |
| 2658 | EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2659 | implicitCastExpr(hasImplicitDestinationType( |
| 2660 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | TEST(ImplicitCast, MatchesSimpleCase) { |
| 2664 | // This test creates an implicit const cast. |
| 2665 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2666 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2667 | // This test creates an implicit cast from int to char. |
| 2668 | EXPECT_TRUE(matches("char c = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2669 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2670 | // This test creates an implicit array-to-pointer cast. |
| 2671 | EXPECT_TRUE(matches("int arr[6]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2672 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | TEST(ImplicitCast, DoesNotMatchIncorrectly) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2676 | // This test verifies that implicitCastExpr() matches exactly when implicit casts |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2677 | // are present, and that it ignores explicit and paren casts. |
| 2678 | |
| 2679 | // These two test cases have no casts. |
| 2680 | EXPECT_TRUE(notMatches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2681 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2682 | EXPECT_TRUE(notMatches("int x = 0, &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2683 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2684 | |
| 2685 | EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2686 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2687 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2688 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2689 | |
| 2690 | EXPECT_TRUE(notMatches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2691 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| 2694 | TEST(IgnoringImpCasts, MatchesImpCasts) { |
| 2695 | // This test checks that ignoringImpCasts matches when implicit casts are |
| 2696 | // present and its inner matcher alone does not match. |
| 2697 | // Note that this test creates an implicit const cast. |
| 2698 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2699 | varDecl(hasInitializer(ignoringImpCasts( |
| 2700 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2701 | // This test creates an implict cast from int to char. |
| 2702 | EXPECT_TRUE(matches("char x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2703 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2704 | integerLiteral(equals(0))))))); |
| 2705 | } |
| 2706 | |
| 2707 | TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) { |
| 2708 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2709 | // matcher does not match. |
| 2710 | // Note that the first test creates an implicit const cast. |
| 2711 | EXPECT_TRUE(notMatches("int x; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2712 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2713 | unless(anything())))))); |
| 2714 | EXPECT_TRUE(notMatches("int x; int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2715 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2716 | unless(anything())))))); |
| 2717 | |
| 2718 | // These tests verify that ignoringImplictCasts does not look through explicit |
| 2719 | // casts or parentheses. |
| 2720 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2721 | varDecl(hasInitializer(ignoringImpCasts( |
| 2722 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2723 | EXPECT_TRUE(notMatches("int i = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2724 | varDecl(hasInitializer(ignoringImpCasts( |
| 2725 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2726 | EXPECT_TRUE(notMatches("float i = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2727 | varDecl(hasInitializer(ignoringImpCasts( |
| 2728 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2729 | EXPECT_TRUE(notMatches("float i = float(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2730 | varDecl(hasInitializer(ignoringImpCasts( |
| 2731 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | TEST(IgnoringImpCasts, MatchesWithoutImpCasts) { |
| 2735 | // This test verifies that expressions that do not have implicit casts |
| 2736 | // still match the inner matcher. |
| 2737 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2738 | varDecl(hasInitializer(ignoringImpCasts( |
| 2739 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | TEST(IgnoringParenCasts, MatchesParenCasts) { |
| 2743 | // This test checks that ignoringParenCasts matches when parentheses and/or |
| 2744 | // casts are present and its inner matcher alone does not match. |
| 2745 | EXPECT_TRUE(matches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2746 | varDecl(hasInitializer(ignoringParenCasts( |
| 2747 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2748 | EXPECT_TRUE(matches("int x = (((((0)))));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2749 | varDecl(hasInitializer(ignoringParenCasts( |
| 2750 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2751 | |
| 2752 | // This test creates an implict cast from int to char in addition to the |
| 2753 | // parentheses. |
| 2754 | EXPECT_TRUE(matches("char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2755 | varDecl(hasInitializer(ignoringParenCasts( |
| 2756 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2757 | |
| 2758 | EXPECT_TRUE(matches("char x = (char)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2759 | varDecl(hasInitializer(ignoringParenCasts( |
| 2760 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2761 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2762 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2763 | integerLiteral(equals(0))))))); |
| 2764 | } |
| 2765 | |
| 2766 | TEST(IgnoringParenCasts, MatchesWithoutParenCasts) { |
| 2767 | // This test verifies that expressions that do not have any casts still match. |
| 2768 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2769 | varDecl(hasInitializer(ignoringParenCasts( |
| 2770 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) { |
| 2774 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2775 | // matcher does not match. |
| 2776 | EXPECT_TRUE(notMatches("int x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2777 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2778 | unless(anything())))))); |
| 2779 | |
| 2780 | // This test creates an implicit cast from int to char in addition to the |
| 2781 | // parentheses. |
| 2782 | EXPECT_TRUE(notMatches("char x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2783 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2784 | unless(anything())))))); |
| 2785 | |
| 2786 | EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2787 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2788 | unless(anything())))))); |
| 2789 | } |
| 2790 | |
| 2791 | TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) { |
| 2792 | // This test checks that ignoringParenAndImpCasts matches when |
| 2793 | // parentheses and/or implicit casts are present and its inner matcher alone |
| 2794 | // does not match. |
| 2795 | // Note that this test creates an implicit const cast. |
| 2796 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2797 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2798 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2799 | // This test creates an implicit cast from int to char. |
| 2800 | EXPECT_TRUE(matches("const char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2801 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2802 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) { |
| 2806 | // This test verifies that expressions that do not have parentheses or |
| 2807 | // implicit casts still match. |
| 2808 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2809 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2810 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2811 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2812 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2813 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) { |
| 2817 | // These tests verify that ignoringParenImpCasts does not match if |
| 2818 | // the inner matcher does not match. |
| 2819 | // This test creates an implicit cast. |
| 2820 | EXPECT_TRUE(notMatches("char c = ((3));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2821 | varDecl(hasInitializer(ignoringParenImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2822 | unless(anything())))))); |
| 2823 | // These tests verify that ignoringParenAndImplictCasts does not look |
| 2824 | // through explicit casts. |
| 2825 | EXPECT_TRUE(notMatches("float y = (float(0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2826 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2827 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2828 | EXPECT_TRUE(notMatches("float y = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2829 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2830 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2831 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2832 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2833 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2836 | TEST(HasSourceExpression, MatchesImplicitCasts) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2837 | EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 2838 | "void r() {string a_string; URL url = a_string; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2839 | implicitCastExpr( |
| 2840 | hasSourceExpression(constructExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2843 | TEST(HasSourceExpression, MatchesExplicitCasts) { |
| 2844 | EXPECT_TRUE(matches("float x = static_cast<float>(42);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2845 | explicitCastExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2846 | hasSourceExpression(hasDescendant( |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2847 | expr(integerLiteral())))))); |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2850 | TEST(Statement, DoesNotMatchDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2851 | EXPECT_TRUE(notMatches("class X {};", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2852 | } |
| 2853 | |
| 2854 | TEST(Statement, MatchesCompoundStatments) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2855 | EXPECT_TRUE(matches("void x() {}", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2859 | EXPECT_TRUE(notMatches("void x() {}", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2860 | } |
| 2861 | |
| 2862 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2863 | EXPECT_TRUE(matches("void x() { int a; }", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2866 | TEST(InitListExpression, MatchesInitListExpression) { |
| 2867 | EXPECT_TRUE(matches("int a[] = { 1, 2 };", |
| 2868 | initListExpr(hasType(asString("int [2]"))))); |
| 2869 | EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2870 | initListExpr(hasType(recordDecl(hasName("B")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | TEST(UsingDeclaration, MatchesUsingDeclarations) { |
| 2874 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;", |
| 2875 | usingDecl())); |
| 2876 | } |
| 2877 | |
| 2878 | TEST(UsingDeclaration, MatchesShadowUsingDelcarations) { |
| 2879 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;", |
| 2880 | usingDecl(hasAnyUsingShadowDecl(hasName("a"))))); |
| 2881 | } |
| 2882 | |
| 2883 | TEST(UsingDeclaration, MatchesSpecificTarget) { |
| 2884 | EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;", |
| 2885 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2886 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2887 | EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;", |
| 2888 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2889 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | TEST(UsingDeclaration, ThroughUsingDeclaration) { |
| 2893 | EXPECT_TRUE(matches( |
| 2894 | "namespace a { void f(); } using a::f; void g() { f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2895 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2896 | EXPECT_TRUE(notMatches( |
| 2897 | "namespace a { void f(); } using a::f; void g() { a::f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2898 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2901 | TEST(SingleDecl, IsSingleDecl) { |
| 2902 | StatementMatcher SingleDeclStmt = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2903 | declStmt(hasSingleDecl(varDecl(hasInitializer(anything())))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2904 | EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt)); |
| 2905 | EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt)); |
| 2906 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 2907 | SingleDeclStmt)); |
| 2908 | } |
| 2909 | |
| 2910 | TEST(DeclStmt, ContainsDeclaration) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2911 | DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything())); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2912 | |
| 2913 | EXPECT_TRUE(matches("void f() {int a = 4;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2914 | declStmt(containsDeclaration(0, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2915 | EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2916 | declStmt(containsDeclaration(0, MatchesInit), |
| 2917 | containsDeclaration(1, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2918 | unsigned WrongIndex = 42; |
| 2919 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2920 | declStmt(containsDeclaration(WrongIndex, |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2921 | MatchesInit)))); |
| 2922 | } |
| 2923 | |
| 2924 | TEST(DeclCount, DeclCountIsCorrect) { |
| 2925 | EXPECT_TRUE(matches("void f() {int i,j;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2926 | declStmt(declCountIs(2)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2927 | EXPECT_TRUE(notMatches("void f() {int i,j; int k;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2928 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2929 | EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2930 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2933 | TEST(While, MatchesWhileLoops) { |
| 2934 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 2935 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 2936 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 2937 | } |
| 2938 | |
| 2939 | TEST(Do, MatchesDoLoops) { |
| 2940 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 2941 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 2942 | } |
| 2943 | |
| 2944 | TEST(Do, DoesNotMatchWhileLoops) { |
| 2945 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 2946 | } |
| 2947 | |
| 2948 | TEST(SwitchCase, MatchesCase) { |
| 2949 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 2950 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 2951 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 2952 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 2953 | } |
| 2954 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 2955 | TEST(SwitchCase, MatchesSwitch) { |
| 2956 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt())); |
| 2957 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt())); |
| 2958 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt())); |
| 2959 | EXPECT_TRUE(notMatches("void x() {}", switchStmt())); |
| 2960 | } |
| 2961 | |
Peter Collingbourne | acf0271 | 2013-05-10 11:52:02 +0000 | [diff] [blame] | 2962 | TEST(SwitchCase, MatchesEachCase) { |
| 2963 | EXPECT_TRUE(notMatches("void x() { switch(42); }", |
| 2964 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 2965 | EXPECT_TRUE(matches("void x() { switch(42) case 42:; }", |
| 2966 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 2967 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", |
| 2968 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 2969 | EXPECT_TRUE(notMatches( |
| 2970 | "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }", |
| 2971 | ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt())))))); |
| 2972 | EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }", |
| 2973 | switchStmt(forEachSwitchCase( |
| 2974 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 2975 | EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }", |
| 2976 | switchStmt(forEachSwitchCase( |
| 2977 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 2978 | EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }", |
| 2979 | switchStmt(forEachSwitchCase( |
| 2980 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 2981 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2982 | "void x() { switch (42) { case 1: case 2: case 3: default:; } }", |
| 2983 | switchStmt(forEachSwitchCase(caseStmt().bind("x"))), |
| 2984 | new VerifyIdIsBoundTo<CaseStmt>("x", 3))); |
| 2985 | } |
| 2986 | |
Manuel Klimek | 0696301 | 2013-07-19 11:50:54 +0000 | [diff] [blame] | 2987 | TEST(ForEachConstructorInitializer, MatchesInitializers) { |
| 2988 | EXPECT_TRUE(matches( |
| 2989 | "struct X { X() : i(42), j(42) {} int i, j; };", |
| 2990 | constructorDecl(forEachConstructorInitializer(ctorInitializer())))); |
| 2991 | } |
| 2992 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 2993 | TEST(ExceptionHandling, SimpleCases) { |
| 2994 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt())); |
| 2995 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt())); |
| 2996 | EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr())); |
| 2997 | EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }", |
| 2998 | throwExpr())); |
| 2999 | EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }", |
| 3000 | throwExpr())); |
| 3001 | } |
| 3002 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3003 | TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 3004 | EXPECT_TRUE(notMatches( |
| 3005 | "void x() { if(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3006 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3007 | EXPECT_TRUE(notMatches( |
| 3008 | "void x() { int x; if((x = 42)) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3009 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 3013 | EXPECT_TRUE(matches( |
| 3014 | "void x() { if(int* a = 0) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3015 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | TEST(ForEach, BindsOneNode) { |
| 3019 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3020 | recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3021 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | TEST(ForEach, BindsMultipleNodes) { |
| 3025 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3026 | recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3027 | new VerifyIdIsBoundTo<FieldDecl>("f", 3))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | TEST(ForEach, BindsRecursiveCombinations) { |
| 3031 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3032 | "class C { class D { int x; int y; }; class E { int y; int z; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3033 | recordDecl(hasName("C"), |
| 3034 | forEach(recordDecl(forEach(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3035 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | TEST(ForEachDescendant, BindsOneNode) { |
| 3039 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3040 | recordDecl(hasName("C"), |
| 3041 | forEachDescendant(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3042 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
Daniel Jasper | 5f684e9 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 3045 | TEST(ForEachDescendant, NestedForEachDescendant) { |
| 3046 | DeclarationMatcher m = recordDecl( |
| 3047 | isDefinition(), decl().bind("x"), hasName("C")); |
| 3048 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3049 | "class A { class B { class C {}; }; };", |
| 3050 | recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))), |
| 3051 | new VerifyIdIsBoundTo<Decl>("x", "C"))); |
| 3052 | |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3053 | // Check that a partial match of 'm' that binds 'x' in the |
| 3054 | // first part of anyOf(m, anything()) will not overwrite the |
| 3055 | // binding created by the earlier binding in the hasDescendant. |
| 3056 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3057 | "class A { class B { class C {}; }; };", |
| 3058 | recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))), |
| 3059 | new VerifyIdIsBoundTo<Decl>("x", "C"))); |
Daniel Jasper | 5f684e9 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3062 | TEST(ForEachDescendant, BindsMultipleNodes) { |
| 3063 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3064 | "class C { class D { int x; int y; }; " |
| 3065 | " class E { class F { int y; int z; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3066 | recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3067 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 3071 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3072 | "class C { class D { " |
| 3073 | " class E { class F { class G { int y; int z; }; }; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3074 | recordDecl(hasName("C"), forEachDescendant(recordDecl( |
| 3075 | forEachDescendant(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3076 | new VerifyIdIsBoundTo<FieldDecl>("f", 8))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3079 | TEST(ForEachDescendant, BindsCombinations) { |
| 3080 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3081 | "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while " |
| 3082 | "(true) {} }", |
| 3083 | compoundStmt(forEachDescendant(ifStmt().bind("if")), |
| 3084 | forEachDescendant(whileStmt().bind("while"))), |
| 3085 | new VerifyIdIsBoundTo<IfStmt>("if", 6))); |
| 3086 | } |
| 3087 | |
| 3088 | TEST(Has, DoesNotDeleteBindings) { |
| 3089 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3090 | "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())), |
| 3091 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3092 | } |
| 3093 | |
| 3094 | TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) { |
| 3095 | // Those matchers cover all the cases where an inner matcher is called |
| 3096 | // and there is not a 1:1 relationship between the match of the outer |
| 3097 | // matcher and the match of the inner matcher. |
| 3098 | // The pattern to look for is: |
| 3099 | // ... return InnerMatcher.matches(...); ... |
| 3100 | // In which case no special handling is needed. |
| 3101 | // |
| 3102 | // On the other hand, if there are multiple alternative matches |
| 3103 | // (for example forEach*) or matches might be discarded (for example has*) |
| 3104 | // the implementation must make sure that the discarded matches do not |
| 3105 | // affect the bindings. |
| 3106 | // When new such matchers are added, add a test here that: |
| 3107 | // - matches a simple node, and binds it as the first thing in the matcher: |
| 3108 | // recordDecl(decl().bind("x"), hasName("X"))) |
| 3109 | // - uses the matcher under test afterwards in a way that not the first |
| 3110 | // alternative is matched; for anyOf, that means the first branch |
| 3111 | // would need to return false; for hasAncestor, it means that not |
| 3112 | // the direct parent matches the inner matcher. |
| 3113 | |
| 3114 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3115 | "class X { int y; };", |
| 3116 | recordDecl( |
| 3117 | recordDecl().bind("x"), hasName("::X"), |
| 3118 | anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())), |
| 3119 | new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1))); |
| 3120 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3121 | "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"), |
| 3122 | anyOf(unless(anything()), anything())), |
| 3123 | new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1))); |
| 3124 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3125 | "template<typename T1, typename T2> class X {}; X<float, int> x;", |
| 3126 | classTemplateSpecializationDecl( |
| 3127 | decl().bind("x"), |
| 3128 | hasAnyTemplateArgument(refersToType(asString("int")))), |
| 3129 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3130 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3131 | "class X { void f(); void g(); };", |
| 3132 | recordDecl(decl().bind("x"), hasMethod(hasName("g"))), |
| 3133 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3134 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3135 | "class X { X() : a(1), b(2) {} double a; int b; };", |
| 3136 | recordDecl(decl().bind("x"), |
| 3137 | has(constructorDecl( |
| 3138 | hasAnyConstructorInitializer(forField(hasName("b")))))), |
| 3139 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3140 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3141 | "void x(int, int) { x(0, 42); }", |
| 3142 | callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))), |
| 3143 | new VerifyIdIsBoundTo<Expr>("x", 1))); |
| 3144 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3145 | "void x(int, int y) {}", |
| 3146 | functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))), |
| 3147 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3148 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3149 | "void x() { return; if (true) {} }", |
| 3150 | functionDecl(decl().bind("x"), |
| 3151 | has(compoundStmt(hasAnySubstatement(ifStmt())))), |
| 3152 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3153 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3154 | "namespace X { void b(int); void b(); }" |
| 3155 | "using X::b;", |
| 3156 | usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl( |
| 3157 | functionDecl(parameterCountIs(1))))), |
| 3158 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3159 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3160 | "class A{}; class B{}; class C : B, A {};", |
| 3161 | recordDecl(decl().bind("x"), isDerivedFrom("::A")), |
| 3162 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3163 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3164 | "class A{}; typedef A B; typedef A C; typedef A D;" |
| 3165 | "class E : A {};", |
| 3166 | recordDecl(decl().bind("x"), isDerivedFrom("C")), |
| 3167 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3168 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3169 | "class A { class B { void f() {} }; };", |
| 3170 | functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 3171 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3172 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3173 | "template <typename T> struct A { struct B {" |
| 3174 | " void f() { if(true) {} }" |
| 3175 | "}; };" |
| 3176 | "void t() { A<int>::B b; b.f(); }", |
| 3177 | ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 3178 | new VerifyIdIsBoundTo<Stmt>("x", 2))); |
| 3179 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3180 | "class A {};", |
| 3181 | recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))), |
| 3182 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
Manuel Klimek | 0696301 | 2013-07-19 11:50:54 +0000 | [diff] [blame] | 3183 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3184 | "class A { A() : s(), i(42) {} const char *s; int i; };", |
| 3185 | constructorDecl(hasName("::A::A"), decl().bind("x"), |
| 3186 | forEachConstructorInitializer(forField(hasName("i")))), |
| 3187 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
Daniel Jasper | 11c9877 | 2012-11-11 22:14:55 +0000 | [diff] [blame] | 3190 | TEST(ForEachDescendant, BindsCorrectNodes) { |
| 3191 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3192 | "class C { void f(); int i; };", |
| 3193 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 3194 | new VerifyIdIsBoundTo<FieldDecl>("decl", 1))); |
| 3195 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3196 | "class C { void f() {} int i; };", |
| 3197 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 3198 | new VerifyIdIsBoundTo<FunctionDecl>("decl", 1))); |
| 3199 | } |
| 3200 | |
Manuel Klimek | 152ea0e | 2013-02-04 10:59:20 +0000 | [diff] [blame] | 3201 | TEST(FindAll, BindsNodeOnMatch) { |
| 3202 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3203 | "class A {};", |
| 3204 | recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))), |
| 3205 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1))); |
| 3206 | } |
| 3207 | |
| 3208 | TEST(FindAll, BindsDescendantNodeOnMatch) { |
| 3209 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3210 | "class A { int a; int b; };", |
| 3211 | recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))), |
| 3212 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 3213 | } |
| 3214 | |
| 3215 | TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) { |
| 3216 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3217 | "class A { int a; int b; };", |
| 3218 | recordDecl(hasName("::A"), |
| 3219 | findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"), |
| 3220 | fieldDecl().bind("v"))))), |
| 3221 | new VerifyIdIsBoundTo<Decl>("v", 3))); |
| 3222 | |
| 3223 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3224 | "class A { class B {}; class C {}; };", |
| 3225 | recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))), |
| 3226 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3))); |
| 3227 | } |
| 3228 | |
Manuel Klimek | 7387673 | 2013-02-04 09:42:38 +0000 | [diff] [blame] | 3229 | TEST(EachOf, TriggersForEachMatch) { |
| 3230 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3231 | "class A { int a; int b; };", |
| 3232 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3233 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3234 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 3235 | } |
| 3236 | |
| 3237 | TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) { |
| 3238 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3239 | "class A { int a; int c; };", |
| 3240 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3241 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3242 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 3243 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3244 | "class A { int c; int b; };", |
| 3245 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3246 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3247 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 3248 | EXPECT_TRUE(notMatches( |
| 3249 | "class A { int c; int d; };", |
| 3250 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3251 | has(fieldDecl(hasName("b")).bind("v")))))); |
| 3252 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3253 | |
| 3254 | TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) { |
| 3255 | // Make sure that we can both match the class by name (::X) and by the type |
| 3256 | // the template was instantiated with (via a field). |
| 3257 | |
| 3258 | EXPECT_TRUE(matches( |
| 3259 | "template <typename T> class X {}; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3260 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3261 | |
| 3262 | EXPECT_TRUE(matches( |
| 3263 | "template <typename T> class X { T t; }; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3264 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 3265 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) { |
| 3269 | EXPECT_TRUE(matches( |
| 3270 | "template <typename T> void f(T t) {} class A {}; void g() { f(A()); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3271 | functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3272 | isTemplateInstantiation()))); |
| 3273 | } |
| 3274 | |
| 3275 | TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) { |
| 3276 | EXPECT_TRUE(matches( |
| 3277 | "template <typename T> class X { T t; }; class A {};" |
| 3278 | "template class X<A>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3279 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 3280 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | TEST(IsTemplateInstantiation, |
| 3284 | MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 3285 | EXPECT_TRUE(matches( |
| 3286 | "template <typename T> class X {};" |
| 3287 | "template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3288 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | TEST(IsTemplateInstantiation, |
| 3292 | MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 3293 | EXPECT_TRUE(matches( |
| 3294 | "class A {};" |
| 3295 | "class X {" |
| 3296 | " template <typename U> class Y { U u; };" |
| 3297 | " Y<A> y;" |
| 3298 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3299 | recordDecl(hasName("::X::Y"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 3303 | // FIXME: Figure out whether this makes sense. It doesn't affect the |
| 3304 | // normal use case as long as the uppermost instantiation always is marked |
| 3305 | // as template instantiation, but it might be confusing as a predicate. |
| 3306 | EXPECT_TRUE(matches( |
| 3307 | "class A {};" |
| 3308 | "template <typename T> class X {" |
| 3309 | " template <typename U> class Y { U u; };" |
| 3310 | " Y<T> y;" |
| 3311 | "}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3312 | recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3313 | } |
| 3314 | |
| 3315 | TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) { |
| 3316 | EXPECT_TRUE(notMatches( |
| 3317 | "template <typename T> class X {}; class A {};" |
| 3318 | "template <> class X<A> {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3319 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) { |
| 3323 | EXPECT_TRUE(notMatches( |
| 3324 | "class A {}; class Y { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3325 | recordDecl(isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3328 | TEST(IsExplicitTemplateSpecialization, |
| 3329 | DoesNotMatchPrimaryTemplate) { |
| 3330 | EXPECT_TRUE(notMatches( |
| 3331 | "template <typename T> class X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3332 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3333 | EXPECT_TRUE(notMatches( |
| 3334 | "template <typename T> void f(T t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3335 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | TEST(IsExplicitTemplateSpecialization, |
| 3339 | DoesNotMatchExplicitTemplateInstantiations) { |
| 3340 | EXPECT_TRUE(notMatches( |
| 3341 | "template <typename T> class X {};" |
| 3342 | "template class X<int>; extern template class X<long>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3343 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3344 | EXPECT_TRUE(notMatches( |
| 3345 | "template <typename T> void f(T t) {}" |
| 3346 | "template void f(int t); extern template void f(long t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3347 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | TEST(IsExplicitTemplateSpecialization, |
| 3351 | DoesNotMatchImplicitTemplateInstantiations) { |
| 3352 | EXPECT_TRUE(notMatches( |
| 3353 | "template <typename T> class X {}; X<int> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3354 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3355 | EXPECT_TRUE(notMatches( |
| 3356 | "template <typename T> void f(T t); void g() { f(10); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3357 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | TEST(IsExplicitTemplateSpecialization, |
| 3361 | MatchesExplicitTemplateSpecializations) { |
| 3362 | EXPECT_TRUE(matches( |
| 3363 | "template <typename T> class X {};" |
| 3364 | "template<> class X<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3365 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3366 | EXPECT_TRUE(matches( |
| 3367 | "template <typename T> void f(T t) {}" |
| 3368 | "template<> void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3369 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3372 | TEST(HasAncenstor, MatchesDeclarationAncestors) { |
| 3373 | EXPECT_TRUE(matches( |
| 3374 | "class A { class B { class C {}; }; };", |
| 3375 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A")))))); |
| 3376 | } |
| 3377 | |
| 3378 | TEST(HasAncenstor, FailsIfNoAncestorMatches) { |
| 3379 | EXPECT_TRUE(notMatches( |
| 3380 | "class A { class B { class C {}; }; };", |
| 3381 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X")))))); |
| 3382 | } |
| 3383 | |
| 3384 | TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) { |
| 3385 | EXPECT_TRUE(matches( |
| 3386 | "class A { class B { void f() { C c; } class C {}; }; };", |
| 3387 | varDecl(hasName("c"), hasType(recordDecl(hasName("C"), |
| 3388 | hasAncestor(recordDecl(hasName("A")))))))); |
| 3389 | } |
| 3390 | |
| 3391 | TEST(HasAncenstor, MatchesStatementAncestors) { |
| 3392 | EXPECT_TRUE(matches( |
| 3393 | "void f() { if (true) { while (false) { 42; } } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3394 | integerLiteral(equals(42), hasAncestor(ifStmt())))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
| 3397 | TEST(HasAncestor, DrillsThroughDifferentHierarchies) { |
| 3398 | EXPECT_TRUE(matches( |
| 3399 | "void f() { if (true) { int x = 42; } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3400 | integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f")))))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | TEST(HasAncestor, BindsRecursiveCombinations) { |
| 3404 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3405 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3406 | fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3407 | new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | TEST(HasAncestor, BindsCombinationsWithHasDescendant) { |
| 3411 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3412 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3413 | fieldDecl(hasAncestor( |
| 3414 | decl( |
| 3415 | hasDescendant(recordDecl(isDefinition(), |
| 3416 | hasAncestor(recordDecl()))) |
| 3417 | ).bind("d") |
| 3418 | )), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3419 | new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E"))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 3422 | TEST(HasAncestor, MatchesClosestAncestor) { |
| 3423 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3424 | "template <typename T> struct C {" |
| 3425 | " void f(int) {" |
| 3426 | " struct I { void g(T) { int x; } } i; i.g(42);" |
| 3427 | " }" |
| 3428 | "};" |
| 3429 | "template struct C<int>;", |
| 3430 | varDecl(hasName("x"), |
| 3431 | hasAncestor(functionDecl(hasParameter( |
| 3432 | 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"), |
| 3433 | new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2))); |
| 3434 | } |
| 3435 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3436 | TEST(HasAncestor, MatchesInTemplateInstantiations) { |
| 3437 | EXPECT_TRUE(matches( |
| 3438 | "template <typename T> struct A { struct B { struct C { T t; }; }; }; " |
| 3439 | "A<int>::B::C a;", |
| 3440 | fieldDecl(hasType(asString("int")), |
| 3441 | hasAncestor(recordDecl(hasName("A")))))); |
| 3442 | } |
| 3443 | |
| 3444 | TEST(HasAncestor, MatchesInImplicitCode) { |
| 3445 | EXPECT_TRUE(matches( |
| 3446 | "struct X {}; struct A { A() {} X x; };", |
| 3447 | constructorDecl( |
| 3448 | hasAnyConstructorInitializer(withInitializer(expr( |
| 3449 | hasAncestor(recordDecl(hasName("A"))))))))); |
| 3450 | } |
| 3451 | |
Daniel Jasper | c99a3ad | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 3452 | TEST(HasParent, MatchesOnlyParent) { |
| 3453 | EXPECT_TRUE(matches( |
| 3454 | "void f() { if (true) { int x = 42; } }", |
| 3455 | compoundStmt(hasParent(ifStmt())))); |
| 3456 | EXPECT_TRUE(notMatches( |
| 3457 | "void f() { for (;;) { int x = 42; } }", |
| 3458 | compoundStmt(hasParent(ifStmt())))); |
| 3459 | EXPECT_TRUE(notMatches( |
| 3460 | "void f() { if (true) for (;;) { int x = 42; } }", |
| 3461 | compoundStmt(hasParent(ifStmt())))); |
| 3462 | } |
| 3463 | |
Manuel Klimek | 30ace37 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 3464 | TEST(HasAncestor, MatchesAllAncestors) { |
| 3465 | EXPECT_TRUE(matches( |
| 3466 | "template <typename T> struct C { static void f() { 42; } };" |
| 3467 | "void t() { C<int>::f(); }", |
| 3468 | integerLiteral( |
| 3469 | equals(42), |
| 3470 | allOf(hasAncestor(recordDecl(isTemplateInstantiation())), |
| 3471 | hasAncestor(recordDecl(unless(isTemplateInstantiation()))))))); |
| 3472 | } |
| 3473 | |
| 3474 | TEST(HasParent, MatchesAllParents) { |
| 3475 | EXPECT_TRUE(matches( |
| 3476 | "template <typename T> struct C { static void f() { 42; } };" |
| 3477 | "void t() { C<int>::f(); }", |
| 3478 | integerLiteral( |
| 3479 | equals(42), |
| 3480 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3481 | hasParent(recordDecl(isTemplateInstantiation()))))))))); |
| 3482 | EXPECT_TRUE(matches( |
| 3483 | "template <typename T> struct C { static void f() { 42; } };" |
| 3484 | "void t() { C<int>::f(); }", |
| 3485 | integerLiteral( |
| 3486 | equals(42), |
| 3487 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3488 | hasParent(recordDecl(unless(isTemplateInstantiation())))))))))); |
| 3489 | EXPECT_TRUE(matches( |
| 3490 | "template <typename T> struct C { static void f() { 42; } };" |
| 3491 | "void t() { C<int>::f(); }", |
| 3492 | integerLiteral(equals(42), |
| 3493 | hasParent(compoundStmt(allOf( |
| 3494 | hasParent(functionDecl( |
| 3495 | hasParent(recordDecl(isTemplateInstantiation())))), |
| 3496 | hasParent(functionDecl(hasParent(recordDecl( |
| 3497 | unless(isTemplateInstantiation()))))))))))); |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 3498 | EXPECT_TRUE( |
| 3499 | notMatches("template <typename T> struct C { static void f() {} };" |
| 3500 | "void t() { C<int>::f(); }", |
| 3501 | compoundStmt(hasParent(recordDecl())))); |
Manuel Klimek | 30ace37 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3504 | TEST(TypeMatching, MatchesTypes) { |
| 3505 | EXPECT_TRUE(matches("struct S {};", qualType().bind("loc"))); |
| 3506 | } |
| 3507 | |
| 3508 | TEST(TypeMatching, MatchesArrayTypes) { |
| 3509 | EXPECT_TRUE(matches("int a[] = {2,3};", arrayType())); |
| 3510 | EXPECT_TRUE(matches("int a[42];", arrayType())); |
| 3511 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType())); |
| 3512 | |
| 3513 | EXPECT_TRUE(notMatches("struct A {}; A a[7];", |
| 3514 | arrayType(hasElementType(builtinType())))); |
| 3515 | |
| 3516 | EXPECT_TRUE(matches( |
| 3517 | "int const a[] = { 2, 3 };", |
| 3518 | qualType(arrayType(hasElementType(builtinType()))))); |
| 3519 | EXPECT_TRUE(matches( |
| 3520 | "int const a[] = { 2, 3 };", |
| 3521 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3522 | EXPECT_TRUE(matches( |
| 3523 | "typedef const int T; T x[] = { 1, 2 };", |
| 3524 | qualType(isConstQualified(), arrayType()))); |
| 3525 | |
| 3526 | EXPECT_TRUE(notMatches( |
| 3527 | "int a[] = { 2, 3 };", |
| 3528 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3529 | EXPECT_TRUE(notMatches( |
| 3530 | "int a[] = { 2, 3 };", |
| 3531 | qualType(arrayType(hasElementType(isConstQualified(), builtinType()))))); |
| 3532 | EXPECT_TRUE(notMatches( |
| 3533 | "int const a[] = { 2, 3 };", |
| 3534 | qualType(arrayType(hasElementType(builtinType())), |
| 3535 | unless(isConstQualified())))); |
| 3536 | |
| 3537 | EXPECT_TRUE(matches("int a[2];", |
| 3538 | constantArrayType(hasElementType(builtinType())))); |
| 3539 | EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger()))); |
| 3540 | } |
| 3541 | |
| 3542 | TEST(TypeMatching, MatchesComplexTypes) { |
| 3543 | EXPECT_TRUE(matches("_Complex float f;", complexType())); |
| 3544 | EXPECT_TRUE(matches( |
| 3545 | "_Complex float f;", |
| 3546 | complexType(hasElementType(builtinType())))); |
| 3547 | EXPECT_TRUE(notMatches( |
| 3548 | "_Complex float f;", |
| 3549 | complexType(hasElementType(isInteger())))); |
| 3550 | } |
| 3551 | |
| 3552 | TEST(TypeMatching, MatchesConstantArrayTypes) { |
| 3553 | EXPECT_TRUE(matches("int a[2];", constantArrayType())); |
| 3554 | EXPECT_TRUE(notMatches( |
| 3555 | "void f() { int a[] = { 2, 3 }; int b[a[0]]; }", |
| 3556 | constantArrayType(hasElementType(builtinType())))); |
| 3557 | |
| 3558 | EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42)))); |
| 3559 | EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42)))); |
| 3560 | EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42)))); |
| 3561 | } |
| 3562 | |
| 3563 | TEST(TypeMatching, MatchesDependentSizedArrayTypes) { |
| 3564 | EXPECT_TRUE(matches( |
| 3565 | "template <typename T, int Size> class array { T data[Size]; };", |
| 3566 | dependentSizedArrayType())); |
| 3567 | EXPECT_TRUE(notMatches( |
| 3568 | "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }", |
| 3569 | dependentSizedArrayType())); |
| 3570 | } |
| 3571 | |
| 3572 | TEST(TypeMatching, MatchesIncompleteArrayType) { |
| 3573 | EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType())); |
| 3574 | EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType())); |
| 3575 | |
| 3576 | EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }", |
| 3577 | incompleteArrayType())); |
| 3578 | } |
| 3579 | |
| 3580 | TEST(TypeMatching, MatchesVariableArrayType) { |
| 3581 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType())); |
| 3582 | EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType())); |
| 3583 | |
| 3584 | EXPECT_TRUE(matches( |
| 3585 | "void f(int b) { int a[b]; }", |
| 3586 | variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( |
| 3587 | varDecl(hasName("b"))))))))); |
| 3588 | } |
| 3589 | |
| 3590 | TEST(TypeMatching, MatchesAtomicTypes) { |
| 3591 | EXPECT_TRUE(matches("_Atomic(int) i;", atomicType())); |
| 3592 | |
| 3593 | EXPECT_TRUE(matches("_Atomic(int) i;", |
| 3594 | atomicType(hasValueType(isInteger())))); |
| 3595 | EXPECT_TRUE(notMatches("_Atomic(float) f;", |
| 3596 | atomicType(hasValueType(isInteger())))); |
| 3597 | } |
| 3598 | |
| 3599 | TEST(TypeMatching, MatchesAutoTypes) { |
| 3600 | EXPECT_TRUE(matches("auto i = 2;", autoType())); |
| 3601 | EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }", |
| 3602 | autoType())); |
| 3603 | |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3604 | // FIXME: Matching against the type-as-written can't work here, because the |
| 3605 | // type as written was not deduced. |
| 3606 | //EXPECT_TRUE(matches("auto a = 1;", |
| 3607 | // autoType(hasDeducedType(isInteger())))); |
| 3608 | //EXPECT_TRUE(notMatches("auto b = 2.0;", |
| 3609 | // autoType(hasDeducedType(isInteger())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 3612 | TEST(TypeMatching, MatchesFunctionTypes) { |
| 3613 | EXPECT_TRUE(matches("int (*f)(int);", functionType())); |
| 3614 | EXPECT_TRUE(matches("void f(int i) {}", functionType())); |
| 3615 | } |
| 3616 | |
Edwin Vane | 88be2fd | 2013-04-01 18:33:34 +0000 | [diff] [blame] | 3617 | TEST(TypeMatching, MatchesParenType) { |
| 3618 | EXPECT_TRUE( |
| 3619 | matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType()))))); |
| 3620 | EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType())))); |
| 3621 | |
| 3622 | EXPECT_TRUE(matches( |
| 3623 | "int (*ptr_to_func)(int);", |
| 3624 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 3625 | EXPECT_TRUE(notMatches( |
| 3626 | "int (*ptr_to_array)[4];", |
| 3627 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 3628 | } |
| 3629 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3630 | TEST(TypeMatching, PointerTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3631 | // FIXME: Reactive when these tests can be more specific (not matching |
| 3632 | // implicit code on certain platforms), likely when we have hasDescendant for |
| 3633 | // Types/TypeLocs. |
| 3634 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3635 | // "int* a;", |
| 3636 | // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))), |
| 3637 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
| 3638 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3639 | // "int* a;", |
| 3640 | // pointerTypeLoc().bind("loc"), |
| 3641 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3642 | EXPECT_TRUE(matches( |
| 3643 | "int** a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3644 | loc(pointerType(pointee(qualType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3645 | EXPECT_TRUE(matches( |
| 3646 | "int** a;", |
| 3647 | loc(pointerType(pointee(pointerType()))))); |
| 3648 | EXPECT_TRUE(matches( |
| 3649 | "int* b; int* * const a = &b;", |
| 3650 | loc(qualType(isConstQualified(), pointerType())))); |
| 3651 | |
| 3652 | std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3653 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3654 | hasType(blockPointerType())))); |
| 3655 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3656 | hasType(memberPointerType())))); |
| 3657 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3658 | hasType(pointerType())))); |
| 3659 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3660 | hasType(referenceType())))); |
Edwin Vane | f4b4804 | 2013-03-07 15:44:40 +0000 | [diff] [blame] | 3661 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3662 | hasType(lValueReferenceType())))); |
| 3663 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3664 | hasType(rValueReferenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3665 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3666 | Fragment = "int *ptr;"; |
| 3667 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3668 | hasType(blockPointerType())))); |
| 3669 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3670 | hasType(memberPointerType())))); |
| 3671 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3672 | hasType(pointerType())))); |
| 3673 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3674 | hasType(referenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3675 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3676 | Fragment = "int a; int &ref = a;"; |
| 3677 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3678 | hasType(blockPointerType())))); |
| 3679 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3680 | hasType(memberPointerType())))); |
| 3681 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3682 | hasType(pointerType())))); |
| 3683 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3684 | hasType(referenceType())))); |
Edwin Vane | f4b4804 | 2013-03-07 15:44:40 +0000 | [diff] [blame] | 3685 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3686 | hasType(lValueReferenceType())))); |
| 3687 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3688 | hasType(rValueReferenceType())))); |
| 3689 | |
| 3690 | Fragment = "int &&ref = 2;"; |
| 3691 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3692 | hasType(blockPointerType())))); |
| 3693 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3694 | hasType(memberPointerType())))); |
| 3695 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3696 | hasType(pointerType())))); |
| 3697 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3698 | hasType(referenceType())))); |
| 3699 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3700 | hasType(lValueReferenceType())))); |
| 3701 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3702 | hasType(rValueReferenceType())))); |
| 3703 | } |
| 3704 | |
| 3705 | TEST(TypeMatching, AutoRefTypes) { |
| 3706 | std::string Fragment = "auto a = 1;" |
| 3707 | "auto b = a;" |
| 3708 | "auto &c = a;" |
| 3709 | "auto &&d = c;" |
| 3710 | "auto &&e = 2;"; |
| 3711 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"), |
| 3712 | hasType(referenceType())))); |
| 3713 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"), |
| 3714 | hasType(referenceType())))); |
| 3715 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 3716 | hasType(referenceType())))); |
| 3717 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 3718 | hasType(lValueReferenceType())))); |
| 3719 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"), |
| 3720 | hasType(rValueReferenceType())))); |
| 3721 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 3722 | hasType(referenceType())))); |
| 3723 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 3724 | hasType(lValueReferenceType())))); |
| 3725 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"), |
| 3726 | hasType(rValueReferenceType())))); |
| 3727 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 3728 | hasType(referenceType())))); |
| 3729 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"), |
| 3730 | hasType(lValueReferenceType())))); |
| 3731 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 3732 | hasType(rValueReferenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | TEST(TypeMatching, PointeeTypes) { |
| 3736 | EXPECT_TRUE(matches("int b; int &a = b;", |
| 3737 | referenceType(pointee(builtinType())))); |
| 3738 | EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType())))); |
| 3739 | |
| 3740 | EXPECT_TRUE(matches("int *a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3741 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3742 | |
| 3743 | EXPECT_TRUE(matches( |
| 3744 | "int const *A;", |
| 3745 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3746 | EXPECT_TRUE(notMatches( |
| 3747 | "int *A;", |
| 3748 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3749 | } |
| 3750 | |
| 3751 | TEST(TypeMatching, MatchesPointersToConstTypes) { |
| 3752 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
| 3753 | loc(pointerType()))); |
| 3754 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3755 | loc(pointerType()))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3756 | EXPECT_TRUE(matches( |
| 3757 | "int b; const int * a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3758 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3759 | EXPECT_TRUE(matches( |
| 3760 | "int b; const int * a = &b;", |
| 3761 | pointerType(pointee(builtinType())))); |
| 3762 | } |
| 3763 | |
| 3764 | TEST(TypeMatching, MatchesTypedefTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3765 | EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"), |
| 3766 | hasType(typedefType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 3769 | TEST(TypeMatching, MatchesTemplateSpecializationType) { |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3770 | EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;", |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 3771 | templateSpecializationType())); |
| 3772 | } |
| 3773 | |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3774 | TEST(TypeMatching, MatchesRecordType) { |
| 3775 | EXPECT_TRUE(matches("class C{}; C c;", recordType())); |
Manuel Klimek | 0cc798f | 2013-02-27 11:56:58 +0000 | [diff] [blame] | 3776 | EXPECT_TRUE(matches("struct S{}; S s;", |
| 3777 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
| 3778 | EXPECT_TRUE(notMatches("int i;", |
| 3779 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
| 3782 | TEST(TypeMatching, MatchesElaboratedType) { |
| 3783 | EXPECT_TRUE(matches( |
| 3784 | "namespace N {" |
| 3785 | " namespace M {" |
| 3786 | " class D {};" |
| 3787 | " }" |
| 3788 | "}" |
| 3789 | "N::M::D d;", elaboratedType())); |
| 3790 | EXPECT_TRUE(matches("class C {} c;", elaboratedType())); |
| 3791 | EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType())); |
| 3792 | } |
| 3793 | |
| 3794 | TEST(ElaboratedTypeNarrowing, hasQualifier) { |
| 3795 | EXPECT_TRUE(matches( |
| 3796 | "namespace N {" |
| 3797 | " namespace M {" |
| 3798 | " class D {};" |
| 3799 | " }" |
| 3800 | "}" |
| 3801 | "N::M::D d;", |
| 3802 | elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
| 3803 | EXPECT_TRUE(notMatches( |
| 3804 | "namespace M {" |
| 3805 | " class D {};" |
| 3806 | "}" |
| 3807 | "M::D d;", |
| 3808 | elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
Edwin Vane | aec89ac | 2013-03-04 17:51:00 +0000 | [diff] [blame] | 3809 | EXPECT_TRUE(notMatches( |
| 3810 | "struct D {" |
| 3811 | "} d;", |
| 3812 | elaboratedType(hasQualifier(nestedNameSpecifier())))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | TEST(ElaboratedTypeNarrowing, namesType) { |
| 3816 | EXPECT_TRUE(matches( |
| 3817 | "namespace N {" |
| 3818 | " namespace M {" |
| 3819 | " class D {};" |
| 3820 | " }" |
| 3821 | "}" |
| 3822 | "N::M::D d;", |
| 3823 | elaboratedType(elaboratedType(namesType(recordType( |
| 3824 | hasDeclaration(namedDecl(hasName("D"))))))))); |
| 3825 | EXPECT_TRUE(notMatches( |
| 3826 | "namespace M {" |
| 3827 | " class D {};" |
| 3828 | "}" |
| 3829 | "M::D d;", |
| 3830 | elaboratedType(elaboratedType(namesType(typedefType()))))); |
| 3831 | } |
| 3832 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3833 | TEST(NNS, MatchesNestedNameSpecifiers) { |
| 3834 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", |
| 3835 | nestedNameSpecifier())); |
| 3836 | EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };", |
| 3837 | nestedNameSpecifier())); |
| 3838 | EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}", |
| 3839 | nestedNameSpecifier())); |
| 3840 | |
| 3841 | EXPECT_TRUE(matches( |
| 3842 | "struct A { static void f() {} }; void g() { A::f(); }", |
| 3843 | nestedNameSpecifier())); |
| 3844 | EXPECT_TRUE(notMatches( |
| 3845 | "struct A { static void f() {} }; void g(A* a) { a->f(); }", |
| 3846 | nestedNameSpecifier())); |
| 3847 | } |
| 3848 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 3849 | TEST(NullStatement, SimpleCases) { |
| 3850 | EXPECT_TRUE(matches("void f() {int i;;}", nullStmt())); |
| 3851 | EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt())); |
| 3852 | } |
| 3853 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3854 | TEST(NNS, MatchesTypes) { |
| 3855 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3856 | specifiesType(hasDeclaration(recordDecl(hasName("A"))))); |
| 3857 | EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher)); |
| 3858 | EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 3859 | Matcher)); |
| 3860 | EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher)); |
| 3861 | } |
| 3862 | |
| 3863 | TEST(NNS, MatchesNamespaceDecls) { |
| 3864 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3865 | specifiesNamespace(hasName("ns"))); |
| 3866 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher)); |
| 3867 | EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher)); |
| 3868 | EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher)); |
| 3869 | } |
| 3870 | |
| 3871 | TEST(NNS, BindsNestedNameSpecifiers) { |
| 3872 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3873 | "namespace ns { struct E { struct B {}; }; } ns::E::B b;", |
| 3874 | nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"), |
| 3875 | new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::"))); |
| 3876 | } |
| 3877 | |
| 3878 | TEST(NNS, BindsNestedNameSpecifierLocs) { |
| 3879 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3880 | "namespace ns { struct B {}; } ns::B b;", |
| 3881 | loc(nestedNameSpecifier()).bind("loc"), |
| 3882 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1))); |
| 3883 | } |
| 3884 | |
| 3885 | TEST(NNS, MatchesNestedNameSpecifierPrefixes) { |
| 3886 | EXPECT_TRUE(matches( |
| 3887 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 3888 | nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))))); |
| 3889 | EXPECT_TRUE(matches( |
| 3890 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3891 | nestedNameSpecifierLoc(hasPrefix( |
| 3892 | specifiesTypeLoc(loc(qualType(asString("struct A")))))))); |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Daniel Jasper | d1ce3c1 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 3895 | TEST(NNS, DescendantsOfNestedNameSpecifiers) { |
| 3896 | std::string Fragment = |
| 3897 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3898 | "void f() { a::A::B::C c; }"; |
| 3899 | EXPECT_TRUE(matches( |
| 3900 | Fragment, |
| 3901 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3902 | hasDescendant(nestedNameSpecifier( |
| 3903 | specifiesNamespace(hasName("a"))))))); |
| 3904 | EXPECT_TRUE(notMatches( |
| 3905 | Fragment, |
| 3906 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3907 | has(nestedNameSpecifier( |
| 3908 | specifiesNamespace(hasName("a"))))))); |
| 3909 | EXPECT_TRUE(matches( |
| 3910 | Fragment, |
| 3911 | nestedNameSpecifier(specifiesType(asString("struct a::A")), |
| 3912 | has(nestedNameSpecifier( |
| 3913 | specifiesNamespace(hasName("a"))))))); |
| 3914 | |
| 3915 | // Not really useful because a NestedNameSpecifier can af at most one child, |
| 3916 | // but to complete the interface. |
| 3917 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3918 | Fragment, |
| 3919 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3920 | forEach(nestedNameSpecifier().bind("x"))), |
| 3921 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1))); |
| 3922 | } |
| 3923 | |
| 3924 | TEST(NNS, NestedNameSpecifiersAsDescendants) { |
| 3925 | std::string Fragment = |
| 3926 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3927 | "void f() { a::A::B::C c; }"; |
| 3928 | EXPECT_TRUE(matches( |
| 3929 | Fragment, |
| 3930 | decl(hasDescendant(nestedNameSpecifier(specifiesType( |
| 3931 | asString("struct a::A"))))))); |
| 3932 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3933 | Fragment, |
| 3934 | functionDecl(hasName("f"), |
| 3935 | forEachDescendant(nestedNameSpecifier().bind("x"))), |
| 3936 | // Nested names: a, a::A and a::A::B. |
| 3937 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3))); |
| 3938 | } |
| 3939 | |
| 3940 | TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { |
| 3941 | std::string Fragment = |
| 3942 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3943 | "void f() { a::A::B::C c; }"; |
| 3944 | EXPECT_TRUE(matches( |
| 3945 | Fragment, |
| 3946 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3947 | hasDescendant(loc(nestedNameSpecifier( |
| 3948 | specifiesNamespace(hasName("a")))))))); |
| 3949 | EXPECT_TRUE(notMatches( |
| 3950 | Fragment, |
| 3951 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3952 | has(loc(nestedNameSpecifier( |
| 3953 | specifiesNamespace(hasName("a")))))))); |
| 3954 | EXPECT_TRUE(matches( |
| 3955 | Fragment, |
| 3956 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))), |
| 3957 | has(loc(nestedNameSpecifier( |
| 3958 | specifiesNamespace(hasName("a")))))))); |
| 3959 | |
| 3960 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3961 | Fragment, |
| 3962 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3963 | forEach(nestedNameSpecifierLoc().bind("x"))), |
| 3964 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1))); |
| 3965 | } |
| 3966 | |
| 3967 | TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { |
| 3968 | std::string Fragment = |
| 3969 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3970 | "void f() { a::A::B::C c; }"; |
| 3971 | EXPECT_TRUE(matches( |
| 3972 | Fragment, |
| 3973 | decl(hasDescendant(loc(nestedNameSpecifier(specifiesType( |
| 3974 | asString("struct a::A")))))))); |
| 3975 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3976 | Fragment, |
| 3977 | functionDecl(hasName("f"), |
| 3978 | forEachDescendant(nestedNameSpecifierLoc().bind("x"))), |
| 3979 | // Nested names: a, a::A and a::A::B. |
| 3980 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3))); |
| 3981 | } |
| 3982 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3983 | template <typename T> class VerifyMatchOnNode : public BoundNodesCallback { |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3984 | public: |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3985 | VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher, |
| 3986 | StringRef InnerId) |
| 3987 | : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) { |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3990 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 3991 | |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3992 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 3993 | const T *Node = Nodes->getNodeAs<T>(Id); |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3994 | return selectFirst<const T>(InnerId, |
| 3995 | match(InnerMatcher, *Node, *Context)) != NULL; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3996 | } |
| 3997 | private: |
| 3998 | std::string Id; |
| 3999 | internal::Matcher<T> InnerMatcher; |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4000 | std::string InnerId; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4001 | }; |
| 4002 | |
| 4003 | TEST(MatchFinder, CanMatchDeclarationsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4004 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4005 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4006 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4007 | "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))), |
| 4008 | "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4009 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4010 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4011 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4012 | "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))), |
| 4013 | "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | TEST(MatchFinder, CanMatchStatementsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4017 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4018 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4019 | new VerifyMatchOnNode<clang::Stmt>( |
| 4020 | "if", stmt(hasDescendant(forStmt().bind("for"))), "for"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4021 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4022 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4023 | new VerifyMatchOnNode<clang::Stmt>( |
| 4024 | "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | TEST(MatchFinder, CanMatchSingleNodesRecursively) { |
| 4028 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4029 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4030 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4031 | "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4032 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4033 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4034 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4035 | "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4036 | } |
| 4037 | |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4038 | template <typename T> |
| 4039 | class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { |
| 4040 | public: |
| 4041 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 4042 | |
| 4043 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 4044 | const T *Node = Nodes->getNodeAs<T>(""); |
| 4045 | return verify(*Nodes, *Context, Node); |
| 4046 | } |
| 4047 | |
| 4048 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { |
| 4049 | return selectFirst<const T>( |
| 4050 | "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))), |
| 4051 | *Node, Context)) != NULL; |
| 4052 | } |
| 4053 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { |
| 4054 | return selectFirst<const T>( |
| 4055 | "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))), |
| 4056 | *Node, Context)) != NULL; |
| 4057 | } |
| 4058 | }; |
| 4059 | |
| 4060 | TEST(IsEqualTo, MatchesNodesByIdentity) { |
| 4061 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4062 | "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""), |
| 4063 | new VerifyAncestorHasChildIsEqual<Decl>())); |
| 4064 | EXPECT_TRUE( |
| 4065 | matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""), |
| 4066 | new VerifyAncestorHasChildIsEqual<Stmt>())); |
| 4067 | } |
| 4068 | |
Manuel Klimek | e579328 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 4069 | class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback { |
| 4070 | public: |
| 4071 | VerifyStartOfTranslationUnit() : Called(false) {} |
| 4072 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 4073 | EXPECT_TRUE(Called); |
| 4074 | } |
| 4075 | virtual void onStartOfTranslationUnit() { |
| 4076 | Called = true; |
| 4077 | } |
| 4078 | bool Called; |
| 4079 | }; |
| 4080 | |
| 4081 | TEST(MatchFinder, InterceptsStartOfTranslationUnit) { |
| 4082 | MatchFinder Finder; |
| 4083 | VerifyStartOfTranslationUnit VerifyCallback; |
| 4084 | Finder.addMatcher(decl(), &VerifyCallback); |
| 4085 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| 4086 | ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); |
| 4087 | EXPECT_TRUE(VerifyCallback.Called); |
| 4088 | } |
| 4089 | |
Peter Collingbourne | 8f9e590 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 4090 | class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback { |
| 4091 | public: |
| 4092 | VerifyEndOfTranslationUnit() : Called(false) {} |
| 4093 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 4094 | EXPECT_FALSE(Called); |
| 4095 | } |
| 4096 | virtual void onEndOfTranslationUnit() { |
| 4097 | Called = true; |
| 4098 | } |
| 4099 | bool Called; |
| 4100 | }; |
| 4101 | |
| 4102 | TEST(MatchFinder, InterceptsEndOfTranslationUnit) { |
| 4103 | MatchFinder Finder; |
| 4104 | VerifyEndOfTranslationUnit VerifyCallback; |
| 4105 | Finder.addMatcher(decl(), &VerifyCallback); |
| 4106 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| 4107 | ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); |
| 4108 | EXPECT_TRUE(VerifyCallback.Called); |
| 4109 | } |
| 4110 | |
Manuel Klimek | cf52ca6 | 2013-06-20 14:06:32 +0000 | [diff] [blame] | 4111 | TEST(EqualsBoundNodeMatcher, QualType) { |
| 4112 | EXPECT_TRUE(matches( |
| 4113 | "int i = 1;", varDecl(hasType(qualType().bind("type")), |
| 4114 | hasInitializer(ignoringParenImpCasts( |
| 4115 | hasType(qualType(equalsBoundNode("type")))))))); |
| 4116 | EXPECT_TRUE(notMatches("int i = 1.f;", |
| 4117 | varDecl(hasType(qualType().bind("type")), |
| 4118 | hasInitializer(ignoringParenImpCasts(hasType( |
| 4119 | qualType(equalsBoundNode("type")))))))); |
| 4120 | } |
| 4121 | |
| 4122 | TEST(EqualsBoundNodeMatcher, NonMatchingTypes) { |
| 4123 | EXPECT_TRUE(notMatches( |
| 4124 | "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"), |
| 4125 | hasInitializer(ignoringParenImpCasts( |
| 4126 | hasType(qualType(equalsBoundNode("type")))))))); |
| 4127 | } |
| 4128 | |
| 4129 | TEST(EqualsBoundNodeMatcher, Stmt) { |
| 4130 | EXPECT_TRUE( |
| 4131 | matches("void f() { if(true) {} }", |
| 4132 | stmt(allOf(ifStmt().bind("if"), |
| 4133 | hasParent(stmt(has(stmt(equalsBoundNode("if"))))))))); |
| 4134 | |
| 4135 | EXPECT_TRUE(notMatches( |
| 4136 | "void f() { if(true) { if (true) {} } }", |
| 4137 | stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if"))))))); |
| 4138 | } |
| 4139 | |
| 4140 | TEST(EqualsBoundNodeMatcher, Decl) { |
| 4141 | EXPECT_TRUE(matches( |
| 4142 | "class X { class Y {}; };", |
| 4143 | decl(allOf(recordDecl(hasName("::X::Y")).bind("record"), |
| 4144 | hasParent(decl(has(decl(equalsBoundNode("record"))))))))); |
| 4145 | |
| 4146 | EXPECT_TRUE(notMatches("class X { class Y {}; };", |
| 4147 | decl(allOf(recordDecl(hasName("::X")).bind("record"), |
| 4148 | has(decl(equalsBoundNode("record"))))))); |
| 4149 | } |
| 4150 | |
| 4151 | TEST(EqualsBoundNodeMatcher, Type) { |
| 4152 | EXPECT_TRUE(matches( |
| 4153 | "class X { int a; int b; };", |
| 4154 | recordDecl( |
| 4155 | has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 4156 | has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 4157 | |
| 4158 | EXPECT_TRUE(notMatches( |
| 4159 | "class X { int a; double b; };", |
| 4160 | recordDecl( |
| 4161 | has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 4162 | has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 4163 | } |
| 4164 | |
| 4165 | TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) { |
| 4166 | |
| 4167 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4168 | "int f() {" |
| 4169 | " if (1) {" |
| 4170 | " int i = 9;" |
| 4171 | " }" |
| 4172 | " int j = 10;" |
| 4173 | " {" |
| 4174 | " float k = 9.0;" |
| 4175 | " }" |
| 4176 | " return 0;" |
| 4177 | "}", |
| 4178 | // Look for variable declarations within functions whose type is the same |
| 4179 | // as the function return type. |
| 4180 | functionDecl(returns(qualType().bind("type")), |
| 4181 | forEachDescendant(varDecl(hasType( |
| 4182 | qualType(equalsBoundNode("type")))).bind("decl"))), |
| 4183 | // Only i and j should match, not k. |
| 4184 | new VerifyIdIsBoundTo<VarDecl>("decl", 2))); |
| 4185 | } |
| 4186 | |
| 4187 | TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) { |
| 4188 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4189 | "void f() {" |
| 4190 | " int x;" |
| 4191 | " double d;" |
| 4192 | " x = d + x - d + x;" |
| 4193 | "}", |
| 4194 | functionDecl( |
| 4195 | hasName("f"), forEachDescendant(varDecl().bind("d")), |
| 4196 | forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))), |
| 4197 | new VerifyIdIsBoundTo<VarDecl>("d", 5))); |
| 4198 | } |
| 4199 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 4200 | } // end namespace ast_matchers |
| 4201 | } // end namespace clang |