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