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