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