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 | 5238060 | 2013-02-19 17:14:34 +0000 | [diff] [blame^] | 821 | TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) { |
| 822 | EXPECT_TRUE(matches("typedef int X; X a;", |
| 823 | varDecl(hasName("a"), |
| 824 | hasType(typedefType(hasDeclaration(decl())))))); |
| 825 | |
| 826 | // FIXME: Add tests for other types with getDecl() (e.g. RecordType) |
| 827 | } |
| 828 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 829 | TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 830 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 831 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 832 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 833 | EXPECT_TRUE( |
| 834 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 835 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 836 | EXPECT_TRUE( |
| 837 | matches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 838 | expr(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
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; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 845 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 846 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 847 | EXPECT_TRUE( |
| 848 | matches("class X {}; void y() { X *x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 849 | varDecl(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 853 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 854 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 855 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 856 | EXPECT_TRUE( |
| 857 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 858 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 862 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 863 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 864 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 865 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 866 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | TEST(Matcher, Call) { |
| 870 | // FIXME: Do we want to overload Call() to directly take |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 871 | // Matcher<Decl>, too? |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 872 | StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 873 | |
| 874 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 875 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 876 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 877 | StatementMatcher MethodOnY = |
| 878 | memberCallExpr(on(hasType(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 879 | |
| 880 | EXPECT_TRUE( |
| 881 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 882 | MethodOnY)); |
| 883 | EXPECT_TRUE( |
| 884 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 885 | MethodOnY)); |
| 886 | EXPECT_TRUE( |
| 887 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 888 | MethodOnY)); |
| 889 | EXPECT_TRUE( |
| 890 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 891 | MethodOnY)); |
| 892 | EXPECT_TRUE( |
| 893 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 894 | MethodOnY)); |
| 895 | |
| 896 | StatementMatcher MethodOnYPointer = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 897 | memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 898 | |
| 899 | EXPECT_TRUE( |
| 900 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 901 | MethodOnYPointer)); |
| 902 | EXPECT_TRUE( |
| 903 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 904 | MethodOnYPointer)); |
| 905 | EXPECT_TRUE( |
| 906 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 907 | MethodOnYPointer)); |
| 908 | EXPECT_TRUE( |
| 909 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 910 | MethodOnYPointer)); |
| 911 | EXPECT_TRUE( |
| 912 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 913 | MethodOnYPointer)); |
| 914 | } |
| 915 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 916 | TEST(Matcher, Lambda) { |
| 917 | EXPECT_TRUE(matches("auto f = [&] (int i) { return i; };", |
| 918 | lambdaExpr())); |
| 919 | } |
| 920 | |
| 921 | TEST(Matcher, ForRange) { |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 922 | EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };" |
| 923 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 924 | forRangeStmt())); |
| 925 | EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }", |
| 926 | forRangeStmt())); |
| 927 | } |
| 928 | |
| 929 | TEST(Matcher, UserDefinedLiteral) { |
| 930 | EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {" |
| 931 | " return i + 1;" |
| 932 | "}" |
| 933 | "char c = 'a'_inc;", |
| 934 | userDefinedLiteral())); |
| 935 | } |
| 936 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 937 | TEST(Matcher, FlowControl) { |
| 938 | EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt())); |
| 939 | EXPECT_TRUE(matches("void f() { while(true) { continue; } }", |
| 940 | continueStmt())); |
| 941 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt())); |
| 942 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt())); |
| 943 | EXPECT_TRUE(matches("void f() { return; }", returnStmt())); |
| 944 | } |
| 945 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 946 | TEST(HasType, MatchesAsString) { |
| 947 | EXPECT_TRUE( |
| 948 | matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 949 | memberCallExpr(on(hasType(asString("class Y *")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 950 | EXPECT_TRUE(matches("class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 951 | methodDecl(hasParameter(0, hasType(asString("int")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 952 | EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 953 | fieldDecl(hasType(asString("ns::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 954 | EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 955 | fieldDecl(hasType(asString("struct <anonymous>::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 958 | TEST(Matcher, OverloadedOperatorCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 959 | StatementMatcher OpCall = operatorCallExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 960 | // Unary operator |
| 961 | EXPECT_TRUE(matches("class Y { }; " |
| 962 | "bool operator!(Y x) { return false; }; " |
| 963 | "Y y; bool c = !y;", OpCall)); |
| 964 | // No match -- special operators like "new", "delete" |
| 965 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
| 966 | // we need to figure out include paths in the test. |
| 967 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
| 968 | // "class Y { }; " |
| 969 | // "void *operator new(size_t size) { return 0; } " |
| 970 | // "Y *y = new Y;", OpCall)); |
| 971 | EXPECT_TRUE(notMatches("class Y { }; " |
| 972 | "void operator delete(void *p) { } " |
| 973 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 974 | // Binary operator |
| 975 | EXPECT_TRUE(matches("class Y { }; " |
| 976 | "bool operator&&(Y x, Y y) { return true; }; " |
| 977 | "Y a; Y b; bool c = a && b;", |
| 978 | OpCall)); |
| 979 | // No match -- normal operator, not an overloaded one. |
| 980 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 981 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 982 | } |
| 983 | |
| 984 | TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { |
| 985 | StatementMatcher OpCallAndAnd = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 986 | operatorCallExpr(hasOverloadedOperatorName("&&")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 987 | EXPECT_TRUE(matches("class Y { }; " |
| 988 | "bool operator&&(Y x, Y y) { return true; }; " |
| 989 | "Y a; Y b; bool c = a && b;", OpCallAndAnd)); |
| 990 | StatementMatcher OpCallLessLess = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 991 | operatorCallExpr(hasOverloadedOperatorName("<<")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 992 | EXPECT_TRUE(notMatches("class Y { }; " |
| 993 | "bool operator&&(Y x, Y y) { return true; }; " |
| 994 | "Y a; Y b; bool c = a && b;", |
| 995 | OpCallLessLess)); |
| 996 | } |
| 997 | |
Daniel Jasper | 278057f | 2012-11-15 03:29:05 +0000 | [diff] [blame] | 998 | TEST(Matcher, NestedOverloadedOperatorCalls) { |
| 999 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1000 | "class Y { }; " |
| 1001 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1002 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1003 | operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"), |
| 1004 | new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2))); |
| 1005 | EXPECT_TRUE(matches( |
| 1006 | "class Y { }; " |
| 1007 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1008 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1009 | operatorCallExpr(hasParent(operatorCallExpr())))); |
| 1010 | EXPECT_TRUE(matches( |
| 1011 | "class Y { }; " |
| 1012 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1013 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1014 | operatorCallExpr(hasDescendant(operatorCallExpr())))); |
| 1015 | } |
| 1016 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1017 | TEST(Matcher, ThisPointerType) { |
Manuel Klimek | 9f17408 | 2012-07-24 13:37:29 +0000 | [diff] [blame] | 1018 | StatementMatcher MethodOnY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1019 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1020 | |
| 1021 | EXPECT_TRUE( |
| 1022 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 1023 | MethodOnY)); |
| 1024 | EXPECT_TRUE( |
| 1025 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 1026 | MethodOnY)); |
| 1027 | EXPECT_TRUE( |
| 1028 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 1029 | MethodOnY)); |
| 1030 | EXPECT_TRUE( |
| 1031 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 1032 | MethodOnY)); |
| 1033 | EXPECT_TRUE( |
| 1034 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 1035 | MethodOnY)); |
| 1036 | |
| 1037 | EXPECT_TRUE(matches( |
| 1038 | "class Y {" |
| 1039 | " public: virtual void x();" |
| 1040 | "};" |
| 1041 | "class X : public Y {" |
| 1042 | " public: virtual void x();" |
| 1043 | "};" |
| 1044 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 1045 | } |
| 1046 | |
| 1047 | TEST(Matcher, VariableUsage) { |
| 1048 | StatementMatcher Reference = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1049 | declRefExpr(to( |
| 1050 | varDecl(hasInitializer( |
| 1051 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1052 | |
| 1053 | EXPECT_TRUE(matches( |
| 1054 | "class Y {" |
| 1055 | " public:" |
| 1056 | " bool x() const;" |
| 1057 | "};" |
| 1058 | "void z(const Y &y) {" |
| 1059 | " bool b = y.x();" |
| 1060 | " if (b) {}" |
| 1061 | "}", Reference)); |
| 1062 | |
| 1063 | EXPECT_TRUE(notMatches( |
| 1064 | "class Y {" |
| 1065 | " public:" |
| 1066 | " bool x() const;" |
| 1067 | "};" |
| 1068 | "void z(const Y &y) {" |
| 1069 | " bool b = y.x();" |
| 1070 | "}", Reference)); |
| 1071 | } |
| 1072 | |
Manuel Klimek | 8cb9bf5 | 2012-12-04 14:42:08 +0000 | [diff] [blame] | 1073 | TEST(Matcher, FindsVarDeclInFunctionParameter) { |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1074 | EXPECT_TRUE(matches( |
| 1075 | "void f(int i) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1076 | varDecl(hasName("i")))); |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1077 | } |
| 1078 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1079 | TEST(Matcher, CalledVariable) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1080 | StatementMatcher CallOnVariableY = |
| 1081 | memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1082 | |
| 1083 | EXPECT_TRUE(matches( |
| 1084 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 1085 | EXPECT_TRUE(matches( |
| 1086 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 1087 | EXPECT_TRUE(matches( |
| 1088 | "class Y { public: void x(); };" |
| 1089 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 1090 | EXPECT_TRUE(matches( |
| 1091 | "class Y { public: void x(); };" |
| 1092 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 1093 | EXPECT_TRUE(notMatches( |
| 1094 | "class Y { public: void x(); };" |
| 1095 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 1096 | CallOnVariableY)); |
| 1097 | } |
| 1098 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1099 | TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) { |
| 1100 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 1101 | unaryExprOrTypeTraitExpr())); |
| 1102 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 1103 | alignOfExpr(anything()))); |
| 1104 | // FIXME: Uncomment once alignof is enabled. |
| 1105 | // EXPECT_TRUE(matches("void x() { int a = alignof(a); }", |
| 1106 | // unaryExprOrTypeTraitExpr())); |
| 1107 | // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }", |
| 1108 | // sizeOfExpr())); |
| 1109 | } |
| 1110 | |
| 1111 | TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) { |
| 1112 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1113 | hasArgumentOfType(asString("int"))))); |
| 1114 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1115 | hasArgumentOfType(asString("float"))))); |
| 1116 | EXPECT_TRUE(matches( |
| 1117 | "struct A {}; void x() { A a; int b = sizeof(a); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1118 | sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1119 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1120 | hasArgumentOfType(hasDeclaration(recordDecl(hasName("string"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1123 | TEST(MemberExpression, DoesNotMatchClasses) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1124 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1128 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | TEST(MemberExpression, MatchesVariable) { |
| 1132 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1133 | matches("class Y { void x() { this->y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1134 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1135 | matches("class Y { void x() { y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1136 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1137 | matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | TEST(MemberExpression, MatchesStaticVariable) { |
| 1141 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1142 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1143 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1144 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1145 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1146 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1149 | TEST(IsInteger, MatchesIntegers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1150 | EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger())))); |
| 1151 | EXPECT_TRUE(matches( |
| 1152 | "long long i = 0; void f(long long) { }; void g() {f(i);}", |
| 1153 | callExpr(hasArgument(0, declRefExpr( |
| 1154 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | TEST(IsInteger, ReportsNoFalsePositives) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1158 | EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1159 | 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] | 1160 | callExpr(hasArgument(0, declRefExpr( |
| 1161 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1164 | TEST(IsArrow, MatchesMemberVariablesViaArrow) { |
| 1165 | EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1166 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1167 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1168 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1169 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1170 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) { |
| 1174 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1175 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1176 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1177 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1178 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1179 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | TEST(IsArrow, MatchesMemberCallsViaArrow) { |
| 1183 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1184 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1185 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1186 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1187 | EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1188 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | TEST(Callee, MatchesDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1192 | StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1193 | |
| 1194 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 1195 | EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 1196 | } |
| 1197 | |
| 1198 | TEST(Callee, MatchesMemberExpressions) { |
| 1199 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1200 | callExpr(callee(memberExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1201 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1202 | notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | TEST(Function, MatchesFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1206 | StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1207 | |
| 1208 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 1209 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 1210 | |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1211 | #if !defined(_MSC_VER) |
| 1212 | // FIXME: Make this work for MSVC. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1213 | // Dependent contexts, but a non-dependent call. |
| 1214 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 1215 | CallFunctionF)); |
| 1216 | EXPECT_TRUE( |
| 1217 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 1218 | CallFunctionF)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1219 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1220 | |
| 1221 | // Depedent calls don't match. |
| 1222 | EXPECT_TRUE( |
| 1223 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 1224 | CallFunctionF)); |
| 1225 | EXPECT_TRUE( |
| 1226 | notMatches("void f(int);" |
| 1227 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 1228 | CallFunctionF)); |
| 1229 | } |
| 1230 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1231 | TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) { |
| 1232 | EXPECT_TRUE( |
| 1233 | matches("template <typename T> void f(T t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1234 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) { |
| 1238 | EXPECT_TRUE( |
| 1239 | notMatches("void f(double d); void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1240 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) { |
| 1244 | EXPECT_TRUE( |
| 1245 | notMatches("void g(); template <typename T> void f(T t) {}" |
| 1246 | "template <> void f(int t) { g(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1247 | functionTemplateDecl(hasName("f"), |
| 1248 | hasDescendant(declRefExpr(to( |
| 1249 | functionDecl(hasName("g")))))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1252 | TEST(Matcher, Argument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1253 | StatementMatcher CallArgumentY = callExpr( |
| 1254 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1255 | |
| 1256 | EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 1257 | EXPECT_TRUE( |
| 1258 | matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 1259 | EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 1260 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1261 | StatementMatcher WrongIndex = callExpr( |
| 1262 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1263 | EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 1264 | } |
| 1265 | |
| 1266 | TEST(Matcher, AnyArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1267 | StatementMatcher CallArgumentY = callExpr( |
| 1268 | hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1269 | EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 1270 | EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 1271 | EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 1272 | } |
| 1273 | |
| 1274 | TEST(Matcher, ArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1275 | StatementMatcher Call1Arg = callExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1276 | |
| 1277 | EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 1278 | EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 1279 | EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 1280 | } |
| 1281 | |
Daniel Jasper | 36e29d6 | 2012-12-04 11:54:27 +0000 | [diff] [blame] | 1282 | TEST(Matcher, ParameterCount) { |
| 1283 | DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1)); |
| 1284 | EXPECT_TRUE(matches("void f(int i) {}", Function1Arg)); |
| 1285 | EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg)); |
| 1286 | EXPECT_TRUE(notMatches("void f() {}", Function1Arg)); |
| 1287 | EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg)); |
| 1288 | } |
| 1289 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1290 | TEST(Matcher, References) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1291 | DeclarationMatcher ReferenceClassX = varDecl( |
| 1292 | hasType(references(recordDecl(hasName("X"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1293 | EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }", |
| 1294 | ReferenceClassX)); |
| 1295 | EXPECT_TRUE( |
| 1296 | matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
| 1297 | EXPECT_TRUE( |
| 1298 | notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 1299 | EXPECT_TRUE( |
| 1300 | notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 1301 | } |
| 1302 | |
| 1303 | TEST(HasParameter, CallsInnerMatcher) { |
| 1304 | EXPECT_TRUE(matches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1305 | methodDecl(hasParameter(0, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1306 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1307 | methodDecl(hasParameter(0, hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 1311 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1312 | methodDecl(hasParameter(42, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 1316 | EXPECT_TRUE(matches("class X { void x(X x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1317 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1318 | EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1319 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1320 | EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1321 | methodDecl(hasParameter(0, |
| 1322 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1323 | EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1324 | methodDecl(hasParameter(0, |
| 1325 | hasType(references(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 1329 | 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] | 1330 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1331 | 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] | 1332 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1335 | TEST(Returns, MatchesReturnTypes) { |
| 1336 | EXPECT_TRUE(matches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1337 | functionDecl(returns(asString("int"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1338 | EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1339 | functionDecl(returns(asString("float"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1340 | EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1341 | functionDecl(returns(hasDeclaration( |
| 1342 | recordDecl(hasName("Y"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1345 | TEST(IsExternC, MatchesExternCFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1346 | EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC()))); |
| 1347 | EXPECT_TRUE(matches("extern \"C\" { void f() {} }", |
| 1348 | functionDecl(isExternC()))); |
| 1349 | EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC()))); |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1352 | TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 1353 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1354 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 1358 | EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1359 | methodDecl(hasAnyParameter(hasType(pointsTo( |
| 1360 | recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | TEST(HasName, MatchesParameterVariableDeclartions) { |
| 1364 | EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1365 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1366 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1367 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1370 | TEST(Matcher, MatchesClassTemplateSpecialization) { |
| 1371 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
| 1372 | "template<> struct A<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1373 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1374 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1375 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1376 | EXPECT_TRUE(notMatches("template<typename T> struct A {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1377 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | TEST(Matcher, MatchesTypeTemplateArgument) { |
| 1381 | EXPECT_TRUE(matches( |
| 1382 | "template<typename T> struct B {};" |
| 1383 | "B<int> b;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1384 | classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1385 | asString("int")))))); |
| 1386 | } |
| 1387 | |
| 1388 | TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { |
| 1389 | EXPECT_TRUE(matches( |
| 1390 | "struct B { int next; };" |
| 1391 | "template<int(B::*next_ptr)> struct A {};" |
| 1392 | "A<&B::next> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1393 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1394 | refersToDeclaration(fieldDecl(hasName("next"))))))); |
Daniel Jasper | aaa8e45 | 2012-09-29 15:55:18 +0000 | [diff] [blame] | 1395 | |
| 1396 | EXPECT_TRUE(notMatches( |
| 1397 | "template <typename T> struct A {};" |
| 1398 | "A<int> a;", |
| 1399 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1400 | refersToDeclaration(decl()))))); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | TEST(Matcher, MatchesSpecificArgument) { |
| 1404 | EXPECT_TRUE(matches( |
| 1405 | "template<typename T, typename U> class A {};" |
| 1406 | "A<bool, int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1407 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1408 | 1, refersToType(asString("int")))))); |
| 1409 | EXPECT_TRUE(notMatches( |
| 1410 | "template<typename T, typename U> class A {};" |
| 1411 | "A<int, bool> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1412 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1413 | 1, refersToType(asString("int")))))); |
| 1414 | } |
| 1415 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1416 | TEST(Matcher, ConstructorCall) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1417 | StatementMatcher Constructor = constructExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1418 | |
| 1419 | EXPECT_TRUE( |
| 1420 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 1421 | EXPECT_TRUE( |
| 1422 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 1423 | Constructor)); |
| 1424 | EXPECT_TRUE( |
| 1425 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1426 | Constructor)); |
| 1427 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 1428 | } |
| 1429 | |
| 1430 | TEST(Matcher, ConstructorArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1431 | StatementMatcher Constructor = constructExpr( |
| 1432 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1433 | |
| 1434 | EXPECT_TRUE( |
| 1435 | matches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1436 | Constructor)); |
| 1437 | EXPECT_TRUE( |
| 1438 | matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 1439 | Constructor)); |
| 1440 | EXPECT_TRUE( |
| 1441 | matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 1442 | Constructor)); |
| 1443 | EXPECT_TRUE( |
| 1444 | notMatches("class X { public: X(int); }; void x() { int z; X x(z); }", |
| 1445 | Constructor)); |
| 1446 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1447 | StatementMatcher WrongIndex = constructExpr( |
| 1448 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1449 | EXPECT_TRUE( |
| 1450 | notMatches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1451 | WrongIndex)); |
| 1452 | } |
| 1453 | |
| 1454 | TEST(Matcher, ConstructorArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1455 | StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1456 | |
| 1457 | EXPECT_TRUE( |
| 1458 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1459 | Constructor1Arg)); |
| 1460 | EXPECT_TRUE( |
| 1461 | matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 1462 | Constructor1Arg)); |
| 1463 | EXPECT_TRUE( |
| 1464 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1465 | Constructor1Arg)); |
| 1466 | EXPECT_TRUE( |
| 1467 | notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 1468 | Constructor1Arg)); |
| 1469 | } |
| 1470 | |
Manuel Klimek | 70b9db9 | 2012-10-23 10:40:50 +0000 | [diff] [blame] | 1471 | TEST(Matcher,ThisExpr) { |
| 1472 | EXPECT_TRUE( |
| 1473 | matches("struct X { int a; int f () { return a; } };", thisExpr())); |
| 1474 | EXPECT_TRUE( |
| 1475 | notMatches("struct X { int f () { int a; return a; } };", thisExpr())); |
| 1476 | } |
| 1477 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1478 | TEST(Matcher, BindTemporaryExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1479 | StatementMatcher TempExpression = bindTemporaryExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1480 | |
| 1481 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 1482 | |
| 1483 | EXPECT_TRUE( |
| 1484 | matches(ClassString + |
| 1485 | "string GetStringByValue();" |
| 1486 | "void FunctionTakesString(string s);" |
| 1487 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1488 | TempExpression)); |
| 1489 | |
| 1490 | EXPECT_TRUE( |
| 1491 | notMatches(ClassString + |
| 1492 | "string* GetStringPointer(); " |
| 1493 | "void FunctionTakesStringPtr(string* s);" |
| 1494 | "void run() {" |
| 1495 | " string* s = GetStringPointer();" |
| 1496 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1497 | " FunctionTakesStringPtr(s);" |
| 1498 | "}", |
| 1499 | TempExpression)); |
| 1500 | |
| 1501 | EXPECT_TRUE( |
| 1502 | notMatches("class no_dtor {};" |
| 1503 | "no_dtor GetObjByValue();" |
| 1504 | "void ConsumeObj(no_dtor param);" |
| 1505 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 1506 | TempExpression)); |
| 1507 | } |
| 1508 | |
Sam Panzer | e16acd3 | 2012-08-24 22:04:44 +0000 | [diff] [blame] | 1509 | TEST(MaterializeTemporaryExpr, MatchesTemporary) { |
| 1510 | std::string ClassString = |
| 1511 | "class string { public: string(); int length(); }; "; |
| 1512 | |
| 1513 | EXPECT_TRUE( |
| 1514 | matches(ClassString + |
| 1515 | "string GetStringByValue();" |
| 1516 | "void FunctionTakesString(string s);" |
| 1517 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1518 | materializeTemporaryExpr())); |
| 1519 | |
| 1520 | EXPECT_TRUE( |
| 1521 | notMatches(ClassString + |
| 1522 | "string* GetStringPointer(); " |
| 1523 | "void FunctionTakesStringPtr(string* s);" |
| 1524 | "void run() {" |
| 1525 | " string* s = GetStringPointer();" |
| 1526 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1527 | " FunctionTakesStringPtr(s);" |
| 1528 | "}", |
| 1529 | materializeTemporaryExpr())); |
| 1530 | |
| 1531 | EXPECT_TRUE( |
| 1532 | notMatches(ClassString + |
| 1533 | "string GetStringByValue();" |
| 1534 | "void run() { int k = GetStringByValue().length(); }", |
| 1535 | materializeTemporaryExpr())); |
| 1536 | |
| 1537 | EXPECT_TRUE( |
| 1538 | notMatches(ClassString + |
| 1539 | "string GetStringByValue();" |
| 1540 | "void run() { GetStringByValue(); }", |
| 1541 | materializeTemporaryExpr())); |
| 1542 | } |
| 1543 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1544 | TEST(ConstructorDeclaration, SimpleCase) { |
| 1545 | EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1546 | constructorDecl(ofClass(hasName("Foo"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1547 | EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1548 | constructorDecl(ofClass(hasName("Bar"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | TEST(ConstructorDeclaration, IsImplicit) { |
| 1552 | // This one doesn't match because the constructor is not added by the |
| 1553 | // compiler (it is not needed). |
| 1554 | EXPECT_TRUE(notMatches("class Foo { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1555 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1556 | // The compiler added the implicit default constructor. |
| 1557 | EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1558 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1559 | EXPECT_TRUE(matches("class Foo { Foo(){} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1560 | constructorDecl(unless(isImplicit())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1563 | TEST(DestructorDeclaration, MatchesVirtualDestructor) { |
| 1564 | EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1565 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1569 | EXPECT_TRUE(notMatches("class Foo {};", |
| 1570 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1573 | TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1574 | EXPECT_TRUE(notMatches( |
| 1575 | "class Foo { Foo() { } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1576 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1577 | EXPECT_TRUE(matches( |
| 1578 | "class Foo {" |
| 1579 | " Foo() : foo_() { }" |
| 1580 | " int foo_;" |
| 1581 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1582 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | TEST(HasAnyConstructorInitializer, ForField) { |
| 1586 | static const char Code[] = |
| 1587 | "class Baz { };" |
| 1588 | "class Foo {" |
| 1589 | " Foo() : foo_() { }" |
| 1590 | " Baz foo_;" |
| 1591 | " Baz bar_;" |
| 1592 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1593 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1594 | forField(hasType(recordDecl(hasName("Baz")))))))); |
| 1595 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1596 | forField(hasName("foo_")))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1597 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1598 | forField(hasType(recordDecl(hasName("Bar")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1602 | static const char Code[] = |
| 1603 | "class Foo {" |
| 1604 | " Foo() : foo_(0) { }" |
| 1605 | " int foo_;" |
| 1606 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1607 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1608 | withInitializer(integerLiteral(equals(0))))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1609 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1610 | withInitializer(integerLiteral(equals(1))))))); |
| 1611 | } |
| 1612 | |
| 1613 | TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1614 | static const char Code[] = |
| 1615 | "struct Bar { Bar(){} };" |
| 1616 | "class Foo {" |
| 1617 | " Foo() : foo_() { }" |
| 1618 | " Bar foo_;" |
| 1619 | " Bar bar_;" |
| 1620 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1621 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1622 | allOf(forField(hasName("foo_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1623 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1624 | allOf(forField(hasName("bar_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1625 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1626 | allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1627 | } |
| 1628 | |
| 1629 | TEST(Matcher, NewExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1630 | StatementMatcher New = newExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1631 | |
| 1632 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 1633 | EXPECT_TRUE( |
| 1634 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 1635 | EXPECT_TRUE( |
| 1636 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1637 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 1638 | } |
| 1639 | |
| 1640 | TEST(Matcher, NewExpressionArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1641 | StatementMatcher New = constructExpr( |
| 1642 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1643 | |
| 1644 | EXPECT_TRUE( |
| 1645 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1646 | New)); |
| 1647 | EXPECT_TRUE( |
| 1648 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1649 | New)); |
| 1650 | EXPECT_TRUE( |
| 1651 | notMatches("class X { public: X(int); }; void x() { int z; new X(z); }", |
| 1652 | New)); |
| 1653 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1654 | StatementMatcher WrongIndex = constructExpr( |
| 1655 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1656 | EXPECT_TRUE( |
| 1657 | notMatches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1658 | WrongIndex)); |
| 1659 | } |
| 1660 | |
| 1661 | TEST(Matcher, NewExpressionArgumentCount) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1662 | StatementMatcher New = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1663 | |
| 1664 | EXPECT_TRUE( |
| 1665 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1666 | EXPECT_TRUE( |
| 1667 | notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }", |
| 1668 | New)); |
| 1669 | } |
| 1670 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1671 | TEST(Matcher, DeleteExpression) { |
| 1672 | EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1673 | deleteExpr())); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1676 | TEST(Matcher, DefaultArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1677 | StatementMatcher Arg = defaultArgExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1678 | |
| 1679 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 1680 | EXPECT_TRUE( |
| 1681 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 1682 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 1683 | } |
| 1684 | |
| 1685 | TEST(Matcher, StringLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1686 | StatementMatcher Literal = stringLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1687 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 1688 | // wide string |
| 1689 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 1690 | // with escaped characters |
| 1691 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 1692 | // no matching -- though the data type is the same, there is no string literal |
| 1693 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 1694 | } |
| 1695 | |
| 1696 | TEST(Matcher, CharacterLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1697 | StatementMatcher CharLiteral = characterLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1698 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 1699 | // wide character |
| 1700 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 1701 | // wide character, Hex encoded, NOT MATCHED! |
| 1702 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 1703 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 1704 | } |
| 1705 | |
| 1706 | TEST(Matcher, IntegerLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1707 | StatementMatcher HasIntLiteral = integerLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1708 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 1709 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 1710 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 1711 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 1712 | |
| 1713 | // Non-matching cases (character literals, float and double) |
| 1714 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 1715 | HasIntLiteral)); // this is actually a character |
| 1716 | // literal cast to int |
| 1717 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 1718 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 1719 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 1720 | } |
| 1721 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1722 | TEST(Matcher, NullPtrLiteral) { |
| 1723 | EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr())); |
| 1724 | } |
| 1725 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 1726 | TEST(Matcher, AsmStatement) { |
| 1727 | EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt())); |
| 1728 | } |
| 1729 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1730 | TEST(Matcher, Conditions) { |
| 1731 | StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true)))); |
| 1732 | |
| 1733 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 1734 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 1735 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 1736 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 1737 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 1738 | } |
| 1739 | |
| 1740 | TEST(MatchBinaryOperator, HasOperatorName) { |
| 1741 | StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1742 | |
| 1743 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1744 | EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1745 | } |
| 1746 | |
| 1747 | TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1748 | StatementMatcher OperatorTrueFalse = |
| 1749 | binaryOperator(hasLHS(boolLiteral(equals(true))), |
| 1750 | hasRHS(boolLiteral(equals(false)))); |
| 1751 | |
| 1752 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1753 | EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1754 | EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1755 | } |
| 1756 | |
| 1757 | TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1758 | StatementMatcher HasOperand = |
| 1759 | binaryOperator(hasEitherOperand(boolLiteral(equals(false)))); |
| 1760 | |
| 1761 | EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 1762 | EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 1763 | EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 1764 | } |
| 1765 | |
| 1766 | TEST(Matcher, BinaryOperatorTypes) { |
| 1767 | // Integration test that verifies the AST provides all binary operators in |
| 1768 | // a way we expect. |
| 1769 | // FIXME: Operator ',' |
| 1770 | EXPECT_TRUE( |
| 1771 | matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 1772 | EXPECT_TRUE( |
| 1773 | matches("bool b; bool c = (b = true);", |
| 1774 | binaryOperator(hasOperatorName("=")))); |
| 1775 | EXPECT_TRUE( |
| 1776 | matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 1777 | EXPECT_TRUE( |
| 1778 | matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 1779 | EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 1780 | EXPECT_TRUE( |
| 1781 | matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 1782 | EXPECT_TRUE( |
| 1783 | matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 1784 | EXPECT_TRUE( |
| 1785 | matches("int i = 1; int j = (i <<= 2);", |
| 1786 | binaryOperator(hasOperatorName("<<=")))); |
| 1787 | EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 1788 | EXPECT_TRUE( |
| 1789 | matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 1790 | EXPECT_TRUE( |
| 1791 | matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 1792 | EXPECT_TRUE( |
| 1793 | matches("int i = 1; int j = (i >>= 2);", |
| 1794 | binaryOperator(hasOperatorName(">>=")))); |
| 1795 | EXPECT_TRUE( |
| 1796 | matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 1797 | EXPECT_TRUE( |
| 1798 | matches("int i = 42; int j = (i ^= 42);", |
| 1799 | binaryOperator(hasOperatorName("^=")))); |
| 1800 | EXPECT_TRUE( |
| 1801 | matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 1802 | EXPECT_TRUE( |
| 1803 | matches("int i = 42; int j = (i %= 42);", |
| 1804 | binaryOperator(hasOperatorName("%=")))); |
| 1805 | EXPECT_TRUE( |
| 1806 | matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 1807 | EXPECT_TRUE( |
| 1808 | matches("bool b = true && false;", |
| 1809 | binaryOperator(hasOperatorName("&&")))); |
| 1810 | EXPECT_TRUE( |
| 1811 | matches("bool b = true; bool c = (b &= false);", |
| 1812 | binaryOperator(hasOperatorName("&=")))); |
| 1813 | EXPECT_TRUE( |
| 1814 | matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 1815 | EXPECT_TRUE( |
| 1816 | matches("bool b = true || false;", |
| 1817 | binaryOperator(hasOperatorName("||")))); |
| 1818 | EXPECT_TRUE( |
| 1819 | matches("bool b = true; bool c = (b |= false);", |
| 1820 | binaryOperator(hasOperatorName("|=")))); |
| 1821 | EXPECT_TRUE( |
| 1822 | matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 1823 | EXPECT_TRUE( |
| 1824 | matches("int i = 42; int j = (i *= 23);", |
| 1825 | binaryOperator(hasOperatorName("*=")))); |
| 1826 | EXPECT_TRUE( |
| 1827 | matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 1828 | EXPECT_TRUE( |
| 1829 | matches("int i = 42; int j = (i /= 23);", |
| 1830 | binaryOperator(hasOperatorName("/=")))); |
| 1831 | EXPECT_TRUE( |
| 1832 | matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 1833 | EXPECT_TRUE( |
| 1834 | matches("int i = 42; int j = (i += 23);", |
| 1835 | binaryOperator(hasOperatorName("+=")))); |
| 1836 | EXPECT_TRUE( |
| 1837 | matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 1838 | EXPECT_TRUE( |
| 1839 | matches("int i = 42; int j = (i -= 23);", |
| 1840 | binaryOperator(hasOperatorName("-=")))); |
| 1841 | EXPECT_TRUE( |
| 1842 | matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 1843 | binaryOperator(hasOperatorName("->*")))); |
| 1844 | EXPECT_TRUE( |
| 1845 | matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 1846 | binaryOperator(hasOperatorName(".*")))); |
| 1847 | |
| 1848 | // Member expressions as operators are not supported in matches. |
| 1849 | EXPECT_TRUE( |
| 1850 | notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 1851 | binaryOperator(hasOperatorName("->")))); |
| 1852 | |
| 1853 | // Initializer assignments are not represented as operator equals. |
| 1854 | EXPECT_TRUE( |
| 1855 | notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 1856 | |
| 1857 | // Array indexing is not represented as operator. |
| 1858 | EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 1859 | |
| 1860 | // Overloaded operators do not match at all. |
| 1861 | EXPECT_TRUE(notMatches( |
| 1862 | "struct A { bool operator&&(const A &a) const { return false; } };" |
| 1863 | "void x() { A a, b; a && b; }", |
| 1864 | binaryOperator())); |
| 1865 | } |
| 1866 | |
| 1867 | TEST(MatchUnaryOperator, HasOperatorName) { |
| 1868 | StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 1869 | |
| 1870 | EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 1871 | EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 1872 | } |
| 1873 | |
| 1874 | TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 1875 | StatementMatcher OperatorOnFalse = |
| 1876 | unaryOperator(hasUnaryOperand(boolLiteral(equals(false)))); |
| 1877 | |
| 1878 | EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 1879 | EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 1880 | } |
| 1881 | |
| 1882 | TEST(Matcher, UnaryOperatorTypes) { |
| 1883 | // Integration test that verifies the AST provides all unary operators in |
| 1884 | // a way we expect. |
| 1885 | EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 1886 | EXPECT_TRUE( |
| 1887 | matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 1888 | EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 1889 | EXPECT_TRUE( |
| 1890 | matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 1891 | EXPECT_TRUE( |
| 1892 | matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 1893 | EXPECT_TRUE( |
| 1894 | matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 1895 | EXPECT_TRUE( |
| 1896 | matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 1897 | EXPECT_TRUE( |
| 1898 | matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 1899 | EXPECT_TRUE( |
| 1900 | matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 1901 | EXPECT_TRUE( |
| 1902 | matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 1903 | |
| 1904 | // We don't match conversion operators. |
| 1905 | EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 1906 | |
| 1907 | // Function calls are not represented as operator. |
| 1908 | EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 1909 | |
| 1910 | // Overloaded operators do not match at all. |
| 1911 | // FIXME: We probably want to add that. |
| 1912 | EXPECT_TRUE(notMatches( |
| 1913 | "struct A { bool operator!() const { return false; } };" |
| 1914 | "void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 1915 | } |
| 1916 | |
| 1917 | TEST(Matcher, ConditionalOperator) { |
| 1918 | StatementMatcher Conditional = conditionalOperator( |
| 1919 | hasCondition(boolLiteral(equals(true))), |
| 1920 | hasTrueExpression(boolLiteral(equals(false)))); |
| 1921 | |
| 1922 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 1923 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 1924 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 1925 | |
| 1926 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 1927 | hasFalseExpression(boolLiteral(equals(false)))); |
| 1928 | |
| 1929 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 1930 | EXPECT_TRUE( |
| 1931 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 1932 | } |
| 1933 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1934 | TEST(ArraySubscriptMatchers, ArraySubscripts) { |
| 1935 | EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }", |
| 1936 | arraySubscriptExpr())); |
| 1937 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }", |
| 1938 | arraySubscriptExpr())); |
| 1939 | } |
| 1940 | |
| 1941 | TEST(ArraySubscriptMatchers, ArrayIndex) { |
| 1942 | EXPECT_TRUE(matches( |
| 1943 | "int i[2]; void f() { i[1] = 1; }", |
| 1944 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1945 | EXPECT_TRUE(matches( |
| 1946 | "int i[2]; void f() { 1[i] = 1; }", |
| 1947 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1948 | EXPECT_TRUE(notMatches( |
| 1949 | "int i[2]; void f() { i[1] = 1; }", |
| 1950 | arraySubscriptExpr(hasIndex(integerLiteral(equals(0)))))); |
| 1951 | } |
| 1952 | |
| 1953 | TEST(ArraySubscriptMatchers, MatchesArrayBase) { |
| 1954 | EXPECT_TRUE(matches( |
| 1955 | "int i[2]; void f() { i[1] = 2; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1956 | arraySubscriptExpr(hasBase(implicitCastExpr( |
| 1957 | hasSourceExpression(declRefExpr())))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1960 | TEST(Matcher, HasNameSupportsNamespaces) { |
| 1961 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1962 | recordDecl(hasName("a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1963 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1964 | recordDecl(hasName("::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1965 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1966 | recordDecl(hasName("b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1967 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1968 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1969 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1970 | recordDecl(hasName("c::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1971 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1972 | recordDecl(hasName("a::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1973 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1974 | recordDecl(hasName("a::b::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1975 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1976 | recordDecl(hasName("::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1977 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1978 | recordDecl(hasName("::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1979 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1980 | recordDecl(hasName("z::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1981 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1982 | recordDecl(hasName("a+b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1983 | EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1984 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | TEST(Matcher, HasNameSupportsOuterClasses) { |
| 1988 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1989 | matches("class A { class B { class C; }; };", |
| 1990 | recordDecl(hasName("A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1991 | EXPECT_TRUE( |
| 1992 | matches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1993 | recordDecl(hasName("::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1994 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1995 | matches("class A { class B { class C; }; };", |
| 1996 | recordDecl(hasName("B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1997 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1998 | matches("class A { class B { class C; }; };", |
| 1999 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2000 | EXPECT_TRUE( |
| 2001 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2002 | recordDecl(hasName("c::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2003 | EXPECT_TRUE( |
| 2004 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2005 | recordDecl(hasName("A::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2006 | EXPECT_TRUE( |
| 2007 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2008 | recordDecl(hasName("A::B::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2009 | EXPECT_TRUE( |
| 2010 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2011 | recordDecl(hasName("::C")))); |
| 2012 | EXPECT_TRUE( |
| 2013 | notMatches("class A { class B { class C; }; };", |
| 2014 | recordDecl(hasName("::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2015 | EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2016 | recordDecl(hasName("z::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2017 | EXPECT_TRUE( |
| 2018 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2019 | recordDecl(hasName("A+B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | TEST(Matcher, IsDefinition) { |
| 2023 | DeclarationMatcher DefinitionOfClassA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2024 | recordDecl(hasName("A"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2025 | EXPECT_TRUE(matches("class A {};", DefinitionOfClassA)); |
| 2026 | EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA)); |
| 2027 | |
| 2028 | DeclarationMatcher DefinitionOfVariableA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2029 | varDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2030 | EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 2031 | EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 2032 | |
| 2033 | DeclarationMatcher DefinitionOfMethodA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2034 | methodDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2035 | EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 2036 | EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 2037 | } |
| 2038 | |
| 2039 | TEST(Matcher, OfClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2040 | StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2041 | ofClass(hasName("X"))))); |
| 2042 | |
| 2043 | EXPECT_TRUE( |
| 2044 | matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 2045 | EXPECT_TRUE( |
| 2046 | matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 2047 | Constructor)); |
| 2048 | EXPECT_TRUE( |
| 2049 | notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 2050 | Constructor)); |
| 2051 | } |
| 2052 | |
| 2053 | TEST(Matcher, VisitsTemplateInstantiations) { |
| 2054 | EXPECT_TRUE(matches( |
| 2055 | "class A { public: void x(); };" |
| 2056 | "template <typename T> class B { public: void y() { T t; t.x(); } };" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2057 | "void f() { B<A> b; b.y(); }", |
| 2058 | callExpr(callee(methodDecl(hasName("x")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2059 | |
| 2060 | EXPECT_TRUE(matches( |
| 2061 | "class A { public: void x(); };" |
| 2062 | "class C {" |
| 2063 | " public:" |
| 2064 | " template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 2065 | "};" |
| 2066 | "void f() {" |
| 2067 | " C::B<A> b; b.y();" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2068 | "}", |
| 2069 | recordDecl(hasName("C"), |
| 2070 | hasDescendant(callExpr(callee(methodDecl(hasName("x")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2073 | TEST(Matcher, HandlesNullQualTypes) { |
| 2074 | // FIXME: Add a Type matcher so we can replace uses of this |
| 2075 | // variable with Type(True()) |
| 2076 | const TypeMatcher AnyType = anything(); |
| 2077 | |
| 2078 | // We don't really care whether this matcher succeeds; we're testing that |
| 2079 | // it completes without crashing. |
| 2080 | EXPECT_TRUE(matches( |
| 2081 | "struct A { };" |
| 2082 | "template <typename T>" |
| 2083 | "void f(T t) {" |
| 2084 | " T local_t(t /* this becomes a null QualType in the AST */);" |
| 2085 | "}" |
| 2086 | "void g() {" |
| 2087 | " f(0);" |
| 2088 | "}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2089 | expr(hasType(TypeMatcher( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2090 | anyOf( |
| 2091 | TypeMatcher(hasDeclaration(anything())), |
| 2092 | pointsTo(AnyType), |
| 2093 | references(AnyType) |
| 2094 | // Other QualType matchers should go here. |
| 2095 | )))))); |
| 2096 | } |
| 2097 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2098 | // For testing AST_MATCHER_P(). |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2099 | AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2100 | // Make sure all special variables are used: node, match_finder, |
| 2101 | // bound_nodes_builder, and the parameter named 'AMatcher'. |
| 2102 | return AMatcher.matches(Node, Finder, Builder); |
| 2103 | } |
| 2104 | |
| 2105 | TEST(AstMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2106 | DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2107 | |
| 2108 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2109 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2110 | |
| 2111 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2112 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2113 | |
| 2114 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2115 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
| 2118 | AST_POLYMORPHIC_MATCHER_P( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2119 | polymorphicHas, internal::Matcher<Decl>, AMatcher) { |
| 2120 | TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) || |
| 2121 | (llvm::is_same<NodeType, Stmt>::value), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2122 | assert_node_type_is_accessible); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2123 | return Finder->matchesChildOf( |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 2124 | Node, AMatcher, Builder, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2125 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
| 2126 | ASTMatchFinder::BK_First); |
| 2127 | } |
| 2128 | |
| 2129 | TEST(AstPolymorphicMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2130 | DeclarationMatcher HasClassB = |
| 2131 | polymorphicHas(recordDecl(hasName("B")).bind("b")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2132 | |
| 2133 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2134 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2135 | |
| 2136 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2137 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2138 | |
| 2139 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2140 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2141 | |
| 2142 | StatementMatcher StatementHasClassB = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2143 | polymorphicHas(recordDecl(hasName("B"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2144 | |
| 2145 | EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB)); |
| 2146 | } |
| 2147 | |
| 2148 | TEST(For, FindsForLoops) { |
| 2149 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 2150 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 2151 | EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };" |
| 2152 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2153 | forStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2156 | TEST(For, ForLoopInternals) { |
| 2157 | EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", |
| 2158 | forStmt(hasCondition(anything())))); |
| 2159 | EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }", |
| 2160 | forStmt(hasLoopInit(anything())))); |
| 2161 | } |
| 2162 | |
| 2163 | TEST(For, NegativeForLoopInternals) { |
| 2164 | EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2165 | forStmt(hasCondition(expr())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2166 | EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }", |
| 2167 | forStmt(hasLoopInit(anything())))); |
| 2168 | } |
| 2169 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2170 | TEST(For, ReportsNoFalsePositives) { |
| 2171 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 2172 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 2173 | } |
| 2174 | |
| 2175 | TEST(CompoundStatement, HandlesSimpleCases) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2176 | EXPECT_TRUE(notMatches("void f();", compoundStmt())); |
| 2177 | EXPECT_TRUE(matches("void f() {}", compoundStmt())); |
| 2178 | EXPECT_TRUE(matches("void f() {{}}", compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 2182 | // It's not a compound statement just because there's "{}" in the source |
| 2183 | // text. This is an AST search, not grep. |
| 2184 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2185 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2186 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2187 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2190 | TEST(HasBody, FindsBodyOfForWhileDoLoops) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2191 | EXPECT_TRUE(matches("void f() { for(;;) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2192 | forStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2193 | EXPECT_TRUE(notMatches("void f() { for(;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2194 | forStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2195 | EXPECT_TRUE(matches("void f() { while(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2196 | whileStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2197 | EXPECT_TRUE(matches("void f() { do {} while(true); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2198 | doStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 2202 | // The simplest case: every compound statement is in a function |
| 2203 | // definition, and the function body itself must be a compound |
| 2204 | // statement. |
| 2205 | EXPECT_TRUE(matches("void f() { for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2206 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | TEST(HasAnySubstatement, IsNotRecursive) { |
| 2210 | // It's really "has any immediate substatement". |
| 2211 | EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2212 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
| 2215 | TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 2216 | EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2217 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 2221 | EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2222 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) { |
| 2226 | EXPECT_TRUE(matches("void f() { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2227 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2228 | EXPECT_TRUE(notMatches("void f() {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2229 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | TEST(StatementCountIs, AppearsToMatchOnlyOneCount) { |
| 2233 | EXPECT_TRUE(matches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2234 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2235 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2236 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2237 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2238 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | TEST(StatementCountIs, WorksWithMultipleStatements) { |
| 2242 | EXPECT_TRUE(matches("void f() { 1; 2; 3; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2243 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | TEST(StatementCountIs, WorksWithNestedCompoundStatements) { |
| 2247 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2248 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2249 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2250 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2251 | EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2252 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2253 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2254 | compoundStmt(statementCountIs(4)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | TEST(Member, WorksInSimplestCase) { |
| 2258 | EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2259 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | TEST(Member, DoesNotMatchTheBaseExpression) { |
| 2263 | // Don't pick out the wrong part of the member expression, this should |
| 2264 | // be checking the member (name) only. |
| 2265 | EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2266 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | TEST(Member, MatchesInMemberFunctionCall) { |
| 2270 | EXPECT_TRUE(matches("void f() {" |
| 2271 | " struct { void first() {}; } s;" |
| 2272 | " s.first();" |
| 2273 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2274 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Daniel Jasper | c711af2 | 2012-10-23 15:46:39 +0000 | [diff] [blame] | 2277 | TEST(Member, MatchesMember) { |
| 2278 | EXPECT_TRUE(matches( |
| 2279 | "struct A { int i; }; void f() { A a; a.i = 2; }", |
| 2280 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2281 | EXPECT_TRUE(notMatches( |
| 2282 | "struct A { float f; }; void f() { A a; a.f = 2.0f; }", |
| 2283 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2284 | } |
| 2285 | |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2286 | TEST(Member, MatchesMemberAllocationFunction) { |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2287 | // Fails in C++11 mode |
| 2288 | EXPECT_TRUE(matchesConditionally( |
| 2289 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2290 | "class X { void *operator new(std::size_t); };", |
| 2291 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2292 | |
| 2293 | EXPECT_TRUE(matches("class X { void operator delete(void*); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2294 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2295 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2296 | // Fails in C++11 mode |
| 2297 | EXPECT_TRUE(matchesConditionally( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2298 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2299 | "class X { void operator delete[](void*, std::size_t); };", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2300 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2303 | TEST(HasObjectExpression, DoesNotMatchMember) { |
| 2304 | EXPECT_TRUE(notMatches( |
| 2305 | "class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2306 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | TEST(HasObjectExpression, MatchesBaseOfVariable) { |
| 2310 | EXPECT_TRUE(matches( |
| 2311 | "struct X { int m; }; void f(X x) { x.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2312 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2313 | EXPECT_TRUE(matches( |
| 2314 | "struct X { int m; }; void f(X* x) { x->m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2315 | memberExpr(hasObjectExpression( |
| 2316 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | TEST(HasObjectExpression, |
| 2320 | MatchesObjectExpressionOfImplicitlyFormedMemberExpression) { |
| 2321 | EXPECT_TRUE(matches( |
| 2322 | "class X {}; struct S { X m; void f() { this->m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2323 | memberExpr(hasObjectExpression( |
| 2324 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2325 | EXPECT_TRUE(matches( |
| 2326 | "class X {}; struct S { X m; void f() { m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2327 | memberExpr(hasObjectExpression( |
| 2328 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | TEST(Field, DoesNotMatchNonFieldMembers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2332 | EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m")))); |
| 2333 | EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m")))); |
| 2334 | EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m")))); |
| 2335 | EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | TEST(Field, MatchesField) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2339 | EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | TEST(IsConstQualified, MatchesConstInt) { |
| 2343 | EXPECT_TRUE(matches("const int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2344 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | TEST(IsConstQualified, MatchesConstPointer) { |
| 2348 | EXPECT_TRUE(matches("int i = 42; int* const p(&i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2349 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2350 | } |
| 2351 | |
| 2352 | TEST(IsConstQualified, MatchesThroughTypedef) { |
| 2353 | EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2354 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2355 | EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2356 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | TEST(IsConstQualified, DoesNotMatchInappropriately) { |
| 2360 | EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2361 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2362 | EXPECT_TRUE(notMatches("int const* p;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2363 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2366 | TEST(CastExpression, MatchesExplicitCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2367 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr())); |
| 2368 | EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr())); |
| 2369 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr())); |
| 2370 | EXPECT_TRUE(matches("char c = char(0);", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2371 | } |
| 2372 | TEST(CastExpression, MatchesImplicitCasts) { |
| 2373 | // This test creates an implicit cast from int to char. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2374 | EXPECT_TRUE(matches("char c = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2375 | // This test creates an implicit cast from lvalue to rvalue. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2376 | EXPECT_TRUE(matches("char c = 0, d = c;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | TEST(CastExpression, DoesNotMatchNonCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2380 | EXPECT_TRUE(notMatches("char c = '0';", castExpr())); |
| 2381 | EXPECT_TRUE(notMatches("char c, &q = c;", castExpr())); |
| 2382 | EXPECT_TRUE(notMatches("int i = (0);", castExpr())); |
| 2383 | EXPECT_TRUE(notMatches("int i = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2384 | } |
| 2385 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2386 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 2387 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2388 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2392 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2393 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2394 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2395 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2396 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2397 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2398 | "B b;" |
| 2399 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2400 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | TEST(FunctionalCast, MatchesSimpleCase) { |
| 2404 | std::string foo_class = "class Foo { public: Foo(char*); };"; |
| 2405 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2406 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
| 2410 | std::string FooClass = "class Foo { public: Foo(char*); };"; |
| 2411 | EXPECT_TRUE( |
| 2412 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2413 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2414 | EXPECT_TRUE( |
| 2415 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2416 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2417 | } |
| 2418 | |
| 2419 | TEST(DynamicCast, MatchesSimpleCase) { |
| 2420 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2421 | "B b;" |
| 2422 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2423 | dynamicCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | TEST(StaticCast, MatchesSimpleCase) { |
| 2427 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2428 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2432 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2433 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2434 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2435 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2436 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2437 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2438 | "B b;" |
| 2439 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2440 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Daniel Jasper | e6d2a96 | 2012-09-18 13:36:17 +0000 | [diff] [blame] | 2443 | TEST(CStyleCast, MatchesSimpleCase) { |
| 2444 | EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr())); |
| 2445 | } |
| 2446 | |
| 2447 | TEST(CStyleCast, DoesNotMatchOtherCasts) { |
| 2448 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);" |
| 2449 | "char q, *r = const_cast<char*>(&q);" |
| 2450 | "void* s = reinterpret_cast<char*>(&s);" |
| 2451 | "struct B { virtual ~B() {} }; struct D : B {};" |
| 2452 | "B b;" |
| 2453 | "D* t = dynamic_cast<D*>(&b);", |
| 2454 | cStyleCastExpr())); |
| 2455 | } |
| 2456 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2457 | TEST(HasDestinationType, MatchesSimpleCase) { |
| 2458 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2459 | staticCastExpr(hasDestinationType( |
| 2460 | pointsTo(TypeMatcher(anything())))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2463 | TEST(HasImplicitDestinationType, MatchesSimpleCase) { |
| 2464 | // This test creates an implicit const cast. |
| 2465 | EXPECT_TRUE(matches("int x; const int i = x;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2466 | implicitCastExpr( |
| 2467 | hasImplicitDestinationType(isInteger())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2468 | // This test creates an implicit array-to-pointer cast. |
| 2469 | EXPECT_TRUE(matches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2470 | implicitCastExpr(hasImplicitDestinationType( |
| 2471 | pointsTo(TypeMatcher(anything())))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) { |
| 2475 | // This test creates an implicit cast from int to char. |
| 2476 | EXPECT_TRUE(notMatches("char c = 0;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2477 | implicitCastExpr(hasImplicitDestinationType( |
| 2478 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2479 | // This test creates an implicit array-to-pointer cast. |
| 2480 | EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2481 | implicitCastExpr(hasImplicitDestinationType( |
| 2482 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
| 2485 | TEST(ImplicitCast, MatchesSimpleCase) { |
| 2486 | // This test creates an implicit const cast. |
| 2487 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2488 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2489 | // This test creates an implicit cast from int to char. |
| 2490 | EXPECT_TRUE(matches("char c = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2491 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2492 | // This test creates an implicit array-to-pointer cast. |
| 2493 | EXPECT_TRUE(matches("int arr[6]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2494 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
| 2497 | TEST(ImplicitCast, DoesNotMatchIncorrectly) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2498 | // This test verifies that implicitCastExpr() matches exactly when implicit casts |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2499 | // are present, and that it ignores explicit and paren casts. |
| 2500 | |
| 2501 | // These two test cases have no casts. |
| 2502 | EXPECT_TRUE(notMatches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2503 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2504 | EXPECT_TRUE(notMatches("int x = 0, &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2505 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2506 | |
| 2507 | EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2508 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2509 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2510 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2511 | |
| 2512 | EXPECT_TRUE(notMatches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2513 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
| 2516 | TEST(IgnoringImpCasts, MatchesImpCasts) { |
| 2517 | // This test checks that ignoringImpCasts matches when implicit casts are |
| 2518 | // present and its inner matcher alone does not match. |
| 2519 | // Note that this test creates an implicit const cast. |
| 2520 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2521 | varDecl(hasInitializer(ignoringImpCasts( |
| 2522 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2523 | // This test creates an implict cast from int to char. |
| 2524 | EXPECT_TRUE(matches("char x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2525 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2526 | integerLiteral(equals(0))))))); |
| 2527 | } |
| 2528 | |
| 2529 | TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) { |
| 2530 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2531 | // matcher does not match. |
| 2532 | // Note that the first test creates an implicit const cast. |
| 2533 | EXPECT_TRUE(notMatches("int x; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2534 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2535 | unless(anything())))))); |
| 2536 | EXPECT_TRUE(notMatches("int x; int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2537 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2538 | unless(anything())))))); |
| 2539 | |
| 2540 | // These tests verify that ignoringImplictCasts does not look through explicit |
| 2541 | // casts or parentheses. |
| 2542 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2543 | varDecl(hasInitializer(ignoringImpCasts( |
| 2544 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2545 | EXPECT_TRUE(notMatches("int i = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2546 | varDecl(hasInitializer(ignoringImpCasts( |
| 2547 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2548 | EXPECT_TRUE(notMatches("float i = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2549 | varDecl(hasInitializer(ignoringImpCasts( |
| 2550 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2551 | EXPECT_TRUE(notMatches("float i = float(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2552 | varDecl(hasInitializer(ignoringImpCasts( |
| 2553 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | TEST(IgnoringImpCasts, MatchesWithoutImpCasts) { |
| 2557 | // This test verifies that expressions that do not have implicit casts |
| 2558 | // still match the inner matcher. |
| 2559 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2560 | varDecl(hasInitializer(ignoringImpCasts( |
| 2561 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | TEST(IgnoringParenCasts, MatchesParenCasts) { |
| 2565 | // This test checks that ignoringParenCasts matches when parentheses and/or |
| 2566 | // casts are present and its inner matcher alone does not match. |
| 2567 | EXPECT_TRUE(matches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2568 | varDecl(hasInitializer(ignoringParenCasts( |
| 2569 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2570 | EXPECT_TRUE(matches("int x = (((((0)))));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2571 | varDecl(hasInitializer(ignoringParenCasts( |
| 2572 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2573 | |
| 2574 | // This test creates an implict cast from int to char in addition to the |
| 2575 | // parentheses. |
| 2576 | EXPECT_TRUE(matches("char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2577 | varDecl(hasInitializer(ignoringParenCasts( |
| 2578 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2579 | |
| 2580 | EXPECT_TRUE(matches("char x = (char)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2581 | varDecl(hasInitializer(ignoringParenCasts( |
| 2582 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2583 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2584 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2585 | integerLiteral(equals(0))))))); |
| 2586 | } |
| 2587 | |
| 2588 | TEST(IgnoringParenCasts, MatchesWithoutParenCasts) { |
| 2589 | // This test verifies that expressions that do not have any casts still match. |
| 2590 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2591 | varDecl(hasInitializer(ignoringParenCasts( |
| 2592 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) { |
| 2596 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2597 | // matcher does not match. |
| 2598 | EXPECT_TRUE(notMatches("int x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2599 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2600 | unless(anything())))))); |
| 2601 | |
| 2602 | // This test creates an implicit cast from int to char in addition to the |
| 2603 | // parentheses. |
| 2604 | EXPECT_TRUE(notMatches("char x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2605 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2606 | unless(anything())))))); |
| 2607 | |
| 2608 | EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2609 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2610 | unless(anything())))))); |
| 2611 | } |
| 2612 | |
| 2613 | TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) { |
| 2614 | // This test checks that ignoringParenAndImpCasts matches when |
| 2615 | // parentheses and/or implicit casts are present and its inner matcher alone |
| 2616 | // does not match. |
| 2617 | // Note that this test creates an implicit const cast. |
| 2618 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2619 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2620 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2621 | // This test creates an implicit cast from int to char. |
| 2622 | EXPECT_TRUE(matches("const char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2623 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2624 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) { |
| 2628 | // This test verifies that expressions that do not have parentheses or |
| 2629 | // implicit casts still match. |
| 2630 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2631 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2632 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2633 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2634 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2635 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) { |
| 2639 | // These tests verify that ignoringParenImpCasts does not match if |
| 2640 | // the inner matcher does not match. |
| 2641 | // This test creates an implicit cast. |
| 2642 | EXPECT_TRUE(notMatches("char c = ((3));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2643 | varDecl(hasInitializer(ignoringParenImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2644 | unless(anything())))))); |
| 2645 | // These tests verify that ignoringParenAndImplictCasts does not look |
| 2646 | // through explicit casts. |
| 2647 | EXPECT_TRUE(notMatches("float y = (float(0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2648 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2649 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2650 | EXPECT_TRUE(notMatches("float y = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2651 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2652 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2653 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2654 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2655 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2658 | TEST(HasSourceExpression, MatchesImplicitCasts) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2659 | EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 2660 | "void r() {string a_string; URL url = a_string; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2661 | implicitCastExpr( |
| 2662 | hasSourceExpression(constructExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2665 | TEST(HasSourceExpression, MatchesExplicitCasts) { |
| 2666 | EXPECT_TRUE(matches("float x = static_cast<float>(42);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2667 | explicitCastExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2668 | hasSourceExpression(hasDescendant( |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2669 | expr(integerLiteral())))))); |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2672 | TEST(Statement, DoesNotMatchDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2673 | EXPECT_TRUE(notMatches("class X {};", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | TEST(Statement, MatchesCompoundStatments) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2677 | EXPECT_TRUE(matches("void x() {}", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2681 | EXPECT_TRUE(notMatches("void x() {}", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2685 | EXPECT_TRUE(matches("void x() { int a; }", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2688 | TEST(InitListExpression, MatchesInitListExpression) { |
| 2689 | EXPECT_TRUE(matches("int a[] = { 1, 2 };", |
| 2690 | initListExpr(hasType(asString("int [2]"))))); |
| 2691 | EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2692 | initListExpr(hasType(recordDecl(hasName("B")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | TEST(UsingDeclaration, MatchesUsingDeclarations) { |
| 2696 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;", |
| 2697 | usingDecl())); |
| 2698 | } |
| 2699 | |
| 2700 | TEST(UsingDeclaration, MatchesShadowUsingDelcarations) { |
| 2701 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;", |
| 2702 | usingDecl(hasAnyUsingShadowDecl(hasName("a"))))); |
| 2703 | } |
| 2704 | |
| 2705 | TEST(UsingDeclaration, MatchesSpecificTarget) { |
| 2706 | EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;", |
| 2707 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2708 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2709 | EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;", |
| 2710 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2711 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2712 | } |
| 2713 | |
| 2714 | TEST(UsingDeclaration, ThroughUsingDeclaration) { |
| 2715 | EXPECT_TRUE(matches( |
| 2716 | "namespace a { void f(); } using a::f; void g() { f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2717 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2718 | EXPECT_TRUE(notMatches( |
| 2719 | "namespace a { void f(); } using a::f; void g() { a::f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2720 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2723 | TEST(SingleDecl, IsSingleDecl) { |
| 2724 | StatementMatcher SingleDeclStmt = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2725 | declStmt(hasSingleDecl(varDecl(hasInitializer(anything())))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2726 | EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt)); |
| 2727 | EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt)); |
| 2728 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 2729 | SingleDeclStmt)); |
| 2730 | } |
| 2731 | |
| 2732 | TEST(DeclStmt, ContainsDeclaration) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2733 | DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything())); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2734 | |
| 2735 | EXPECT_TRUE(matches("void f() {int a = 4;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2736 | declStmt(containsDeclaration(0, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2737 | EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2738 | declStmt(containsDeclaration(0, MatchesInit), |
| 2739 | containsDeclaration(1, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2740 | unsigned WrongIndex = 42; |
| 2741 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2742 | declStmt(containsDeclaration(WrongIndex, |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2743 | MatchesInit)))); |
| 2744 | } |
| 2745 | |
| 2746 | TEST(DeclCount, DeclCountIsCorrect) { |
| 2747 | EXPECT_TRUE(matches("void f() {int i,j;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2748 | declStmt(declCountIs(2)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2749 | EXPECT_TRUE(notMatches("void f() {int i,j; int k;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2750 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2751 | EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2752 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2753 | } |
| 2754 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2755 | TEST(While, MatchesWhileLoops) { |
| 2756 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 2757 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 2758 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 2759 | } |
| 2760 | |
| 2761 | TEST(Do, MatchesDoLoops) { |
| 2762 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 2763 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 2764 | } |
| 2765 | |
| 2766 | TEST(Do, DoesNotMatchWhileLoops) { |
| 2767 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 2768 | } |
| 2769 | |
| 2770 | TEST(SwitchCase, MatchesCase) { |
| 2771 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 2772 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 2773 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 2774 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 2775 | } |
| 2776 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 2777 | TEST(SwitchCase, MatchesSwitch) { |
| 2778 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt())); |
| 2779 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt())); |
| 2780 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt())); |
| 2781 | EXPECT_TRUE(notMatches("void x() {}", switchStmt())); |
| 2782 | } |
| 2783 | |
| 2784 | TEST(ExceptionHandling, SimpleCases) { |
| 2785 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt())); |
| 2786 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt())); |
| 2787 | EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr())); |
| 2788 | EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }", |
| 2789 | throwExpr())); |
| 2790 | EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }", |
| 2791 | throwExpr())); |
| 2792 | } |
| 2793 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2794 | TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 2795 | EXPECT_TRUE(notMatches( |
| 2796 | "void x() { if(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2797 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2798 | EXPECT_TRUE(notMatches( |
| 2799 | "void x() { int x; if((x = 42)) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2800 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 2804 | EXPECT_TRUE(matches( |
| 2805 | "void x() { if(int* a = 0) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2806 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | TEST(ForEach, BindsOneNode) { |
| 2810 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2811 | recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2812 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | TEST(ForEach, BindsMultipleNodes) { |
| 2816 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2817 | recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2818 | new VerifyIdIsBoundTo<FieldDecl>("f", 3))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | TEST(ForEach, BindsRecursiveCombinations) { |
| 2822 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2823 | "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] | 2824 | recordDecl(hasName("C"), |
| 2825 | forEach(recordDecl(forEach(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2826 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
| 2829 | TEST(ForEachDescendant, BindsOneNode) { |
| 2830 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2831 | recordDecl(hasName("C"), |
| 2832 | forEachDescendant(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2833 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
Daniel Jasper | 5f684e9 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 2836 | TEST(ForEachDescendant, NestedForEachDescendant) { |
| 2837 | DeclarationMatcher m = recordDecl( |
| 2838 | isDefinition(), decl().bind("x"), hasName("C")); |
| 2839 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2840 | "class A { class B { class C {}; }; };", |
| 2841 | recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))), |
| 2842 | new VerifyIdIsBoundTo<Decl>("x", "C"))); |
| 2843 | |
| 2844 | // FIXME: This is not really a useful matcher, but the result is still |
| 2845 | // surprising (currently binds "A"). |
| 2846 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2847 | // "class A { class B { class C {}; }; };", |
| 2848 | // recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))), |
| 2849 | // new VerifyIdIsBoundTo<Decl>("x", "C"))); |
| 2850 | } |
| 2851 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2852 | TEST(ForEachDescendant, BindsMultipleNodes) { |
| 2853 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2854 | "class C { class D { int x; int y; }; " |
| 2855 | " class E { class F { int y; int z; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2856 | recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2857 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 2861 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2862 | "class C { class D { " |
| 2863 | " class E { class F { class G { int y; int z; }; }; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2864 | recordDecl(hasName("C"), forEachDescendant(recordDecl( |
| 2865 | forEachDescendant(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2866 | new VerifyIdIsBoundTo<FieldDecl>("f", 8))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
Daniel Jasper | 11c9877 | 2012-11-11 22:14:55 +0000 | [diff] [blame] | 2869 | TEST(ForEachDescendant, BindsCorrectNodes) { |
| 2870 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2871 | "class C { void f(); int i; };", |
| 2872 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 2873 | new VerifyIdIsBoundTo<FieldDecl>("decl", 1))); |
| 2874 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2875 | "class C { void f() {} int i; };", |
| 2876 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 2877 | new VerifyIdIsBoundTo<FunctionDecl>("decl", 1))); |
| 2878 | } |
| 2879 | |
Manuel Klimek | 152ea0e | 2013-02-04 10:59:20 +0000 | [diff] [blame] | 2880 | TEST(FindAll, BindsNodeOnMatch) { |
| 2881 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2882 | "class A {};", |
| 2883 | recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))), |
| 2884 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1))); |
| 2885 | } |
| 2886 | |
| 2887 | TEST(FindAll, BindsDescendantNodeOnMatch) { |
| 2888 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2889 | "class A { int a; int b; };", |
| 2890 | recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))), |
| 2891 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 2892 | } |
| 2893 | |
| 2894 | TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) { |
| 2895 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2896 | "class A { int a; int b; };", |
| 2897 | recordDecl(hasName("::A"), |
| 2898 | findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"), |
| 2899 | fieldDecl().bind("v"))))), |
| 2900 | new VerifyIdIsBoundTo<Decl>("v", 3))); |
| 2901 | |
| 2902 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2903 | "class A { class B {}; class C {}; };", |
| 2904 | recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))), |
| 2905 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3))); |
| 2906 | } |
| 2907 | |
Manuel Klimek | 7387673 | 2013-02-04 09:42:38 +0000 | [diff] [blame] | 2908 | TEST(EachOf, TriggersForEachMatch) { |
| 2909 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2910 | "class A { int a; int b; };", |
| 2911 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2912 | has(fieldDecl(hasName("b")).bind("v")))), |
| 2913 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 2914 | } |
| 2915 | |
| 2916 | TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) { |
| 2917 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2918 | "class A { int a; int c; };", |
| 2919 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2920 | has(fieldDecl(hasName("b")).bind("v")))), |
| 2921 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 2922 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2923 | "class A { int c; int b; };", |
| 2924 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2925 | has(fieldDecl(hasName("b")).bind("v")))), |
| 2926 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 2927 | EXPECT_TRUE(notMatches( |
| 2928 | "class A { int c; int d; };", |
| 2929 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 2930 | has(fieldDecl(hasName("b")).bind("v")))))); |
| 2931 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2932 | |
| 2933 | TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) { |
| 2934 | // Make sure that we can both match the class by name (::X) and by the type |
| 2935 | // the template was instantiated with (via a field). |
| 2936 | |
| 2937 | EXPECT_TRUE(matches( |
| 2938 | "template <typename T> class X {}; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2939 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2940 | |
| 2941 | EXPECT_TRUE(matches( |
| 2942 | "template <typename T> class X { T t; }; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2943 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 2944 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
| 2947 | TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) { |
| 2948 | EXPECT_TRUE(matches( |
| 2949 | "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] | 2950 | functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2951 | isTemplateInstantiation()))); |
| 2952 | } |
| 2953 | |
| 2954 | TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) { |
| 2955 | EXPECT_TRUE(matches( |
| 2956 | "template <typename T> class X { T t; }; class A {};" |
| 2957 | "template class X<A>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2958 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 2959 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | TEST(IsTemplateInstantiation, |
| 2963 | MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 2964 | EXPECT_TRUE(matches( |
| 2965 | "template <typename T> class X {};" |
| 2966 | "template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2967 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
| 2970 | TEST(IsTemplateInstantiation, |
| 2971 | MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 2972 | EXPECT_TRUE(matches( |
| 2973 | "class A {};" |
| 2974 | "class X {" |
| 2975 | " template <typename U> class Y { U u; };" |
| 2976 | " Y<A> y;" |
| 2977 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2978 | recordDecl(hasName("::X::Y"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 2982 | // FIXME: Figure out whether this makes sense. It doesn't affect the |
| 2983 | // normal use case as long as the uppermost instantiation always is marked |
| 2984 | // as template instantiation, but it might be confusing as a predicate. |
| 2985 | EXPECT_TRUE(matches( |
| 2986 | "class A {};" |
| 2987 | "template <typename T> class X {" |
| 2988 | " template <typename U> class Y { U u; };" |
| 2989 | " Y<T> y;" |
| 2990 | "}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2991 | recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) { |
| 2995 | EXPECT_TRUE(notMatches( |
| 2996 | "template <typename T> class X {}; class A {};" |
| 2997 | "template <> class X<A> {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2998 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) { |
| 3002 | EXPECT_TRUE(notMatches( |
| 3003 | "class A {}; class Y { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3004 | recordDecl(isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3007 | TEST(IsExplicitTemplateSpecialization, |
| 3008 | DoesNotMatchPrimaryTemplate) { |
| 3009 | EXPECT_TRUE(notMatches( |
| 3010 | "template <typename T> class X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3011 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3012 | EXPECT_TRUE(notMatches( |
| 3013 | "template <typename T> void f(T t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3014 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | TEST(IsExplicitTemplateSpecialization, |
| 3018 | DoesNotMatchExplicitTemplateInstantiations) { |
| 3019 | EXPECT_TRUE(notMatches( |
| 3020 | "template <typename T> class X {};" |
| 3021 | "template class X<int>; extern template class X<long>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3022 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3023 | EXPECT_TRUE(notMatches( |
| 3024 | "template <typename T> void f(T t) {}" |
| 3025 | "template void f(int t); extern template void f(long t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3026 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | TEST(IsExplicitTemplateSpecialization, |
| 3030 | DoesNotMatchImplicitTemplateInstantiations) { |
| 3031 | EXPECT_TRUE(notMatches( |
| 3032 | "template <typename T> class X {}; X<int> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3033 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3034 | EXPECT_TRUE(notMatches( |
| 3035 | "template <typename T> void f(T t); void g() { f(10); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3036 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | TEST(IsExplicitTemplateSpecialization, |
| 3040 | MatchesExplicitTemplateSpecializations) { |
| 3041 | EXPECT_TRUE(matches( |
| 3042 | "template <typename T> class X {};" |
| 3043 | "template<> class X<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3044 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3045 | EXPECT_TRUE(matches( |
| 3046 | "template <typename T> void f(T t) {}" |
| 3047 | "template<> void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3048 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3051 | TEST(HasAncenstor, MatchesDeclarationAncestors) { |
| 3052 | EXPECT_TRUE(matches( |
| 3053 | "class A { class B { class C {}; }; };", |
| 3054 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A")))))); |
| 3055 | } |
| 3056 | |
| 3057 | TEST(HasAncenstor, FailsIfNoAncestorMatches) { |
| 3058 | EXPECT_TRUE(notMatches( |
| 3059 | "class A { class B { class C {}; }; };", |
| 3060 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X")))))); |
| 3061 | } |
| 3062 | |
| 3063 | TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) { |
| 3064 | EXPECT_TRUE(matches( |
| 3065 | "class A { class B { void f() { C c; } class C {}; }; };", |
| 3066 | varDecl(hasName("c"), hasType(recordDecl(hasName("C"), |
| 3067 | hasAncestor(recordDecl(hasName("A")))))))); |
| 3068 | } |
| 3069 | |
| 3070 | TEST(HasAncenstor, MatchesStatementAncestors) { |
| 3071 | EXPECT_TRUE(matches( |
| 3072 | "void f() { if (true) { while (false) { 42; } } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3073 | integerLiteral(equals(42), hasAncestor(ifStmt())))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | TEST(HasAncestor, DrillsThroughDifferentHierarchies) { |
| 3077 | EXPECT_TRUE(matches( |
| 3078 | "void f() { if (true) { int x = 42; } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3079 | integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f")))))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3080 | } |
| 3081 | |
| 3082 | TEST(HasAncestor, BindsRecursiveCombinations) { |
| 3083 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3084 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3085 | fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3086 | new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3087 | } |
| 3088 | |
| 3089 | TEST(HasAncestor, BindsCombinationsWithHasDescendant) { |
| 3090 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3091 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3092 | fieldDecl(hasAncestor( |
| 3093 | decl( |
| 3094 | hasDescendant(recordDecl(isDefinition(), |
| 3095 | hasAncestor(recordDecl()))) |
| 3096 | ).bind("d") |
| 3097 | )), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3098 | new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E"))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | TEST(HasAncestor, MatchesInTemplateInstantiations) { |
| 3102 | EXPECT_TRUE(matches( |
| 3103 | "template <typename T> struct A { struct B { struct C { T t; }; }; }; " |
| 3104 | "A<int>::B::C a;", |
| 3105 | fieldDecl(hasType(asString("int")), |
| 3106 | hasAncestor(recordDecl(hasName("A")))))); |
| 3107 | } |
| 3108 | |
| 3109 | TEST(HasAncestor, MatchesInImplicitCode) { |
| 3110 | EXPECT_TRUE(matches( |
| 3111 | "struct X {}; struct A { A() {} X x; };", |
| 3112 | constructorDecl( |
| 3113 | hasAnyConstructorInitializer(withInitializer(expr( |
| 3114 | hasAncestor(recordDecl(hasName("A"))))))))); |
| 3115 | } |
| 3116 | |
Daniel Jasper | c99a3ad | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 3117 | TEST(HasParent, MatchesOnlyParent) { |
| 3118 | EXPECT_TRUE(matches( |
| 3119 | "void f() { if (true) { int x = 42; } }", |
| 3120 | compoundStmt(hasParent(ifStmt())))); |
| 3121 | EXPECT_TRUE(notMatches( |
| 3122 | "void f() { for (;;) { int x = 42; } }", |
| 3123 | compoundStmt(hasParent(ifStmt())))); |
| 3124 | EXPECT_TRUE(notMatches( |
| 3125 | "void f() { if (true) for (;;) { int x = 42; } }", |
| 3126 | compoundStmt(hasParent(ifStmt())))); |
| 3127 | } |
| 3128 | |
Manuel Klimek | 30ace37 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 3129 | TEST(HasAncestor, MatchesAllAncestors) { |
| 3130 | EXPECT_TRUE(matches( |
| 3131 | "template <typename T> struct C { static void f() { 42; } };" |
| 3132 | "void t() { C<int>::f(); }", |
| 3133 | integerLiteral( |
| 3134 | equals(42), |
| 3135 | allOf(hasAncestor(recordDecl(isTemplateInstantiation())), |
| 3136 | hasAncestor(recordDecl(unless(isTemplateInstantiation()))))))); |
| 3137 | } |
| 3138 | |
| 3139 | TEST(HasParent, MatchesAllParents) { |
| 3140 | EXPECT_TRUE(matches( |
| 3141 | "template <typename T> struct C { static void f() { 42; } };" |
| 3142 | "void t() { C<int>::f(); }", |
| 3143 | integerLiteral( |
| 3144 | equals(42), |
| 3145 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3146 | hasParent(recordDecl(isTemplateInstantiation()))))))))); |
| 3147 | EXPECT_TRUE(matches( |
| 3148 | "template <typename T> struct C { static void f() { 42; } };" |
| 3149 | "void t() { C<int>::f(); }", |
| 3150 | integerLiteral( |
| 3151 | equals(42), |
| 3152 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3153 | hasParent(recordDecl(unless(isTemplateInstantiation())))))))))); |
| 3154 | EXPECT_TRUE(matches( |
| 3155 | "template <typename T> struct C { static void f() { 42; } };" |
| 3156 | "void t() { C<int>::f(); }", |
| 3157 | integerLiteral(equals(42), |
| 3158 | hasParent(compoundStmt(allOf( |
| 3159 | hasParent(functionDecl( |
| 3160 | hasParent(recordDecl(isTemplateInstantiation())))), |
| 3161 | hasParent(functionDecl(hasParent(recordDecl( |
| 3162 | unless(isTemplateInstantiation()))))))))))); |
| 3163 | } |
| 3164 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3165 | TEST(TypeMatching, MatchesTypes) { |
| 3166 | EXPECT_TRUE(matches("struct S {};", qualType().bind("loc"))); |
| 3167 | } |
| 3168 | |
| 3169 | TEST(TypeMatching, MatchesArrayTypes) { |
| 3170 | EXPECT_TRUE(matches("int a[] = {2,3};", arrayType())); |
| 3171 | EXPECT_TRUE(matches("int a[42];", arrayType())); |
| 3172 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType())); |
| 3173 | |
| 3174 | EXPECT_TRUE(notMatches("struct A {}; A a[7];", |
| 3175 | arrayType(hasElementType(builtinType())))); |
| 3176 | |
| 3177 | EXPECT_TRUE(matches( |
| 3178 | "int const a[] = { 2, 3 };", |
| 3179 | qualType(arrayType(hasElementType(builtinType()))))); |
| 3180 | EXPECT_TRUE(matches( |
| 3181 | "int const a[] = { 2, 3 };", |
| 3182 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3183 | EXPECT_TRUE(matches( |
| 3184 | "typedef const int T; T x[] = { 1, 2 };", |
| 3185 | qualType(isConstQualified(), arrayType()))); |
| 3186 | |
| 3187 | EXPECT_TRUE(notMatches( |
| 3188 | "int a[] = { 2, 3 };", |
| 3189 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3190 | EXPECT_TRUE(notMatches( |
| 3191 | "int a[] = { 2, 3 };", |
| 3192 | qualType(arrayType(hasElementType(isConstQualified(), builtinType()))))); |
| 3193 | EXPECT_TRUE(notMatches( |
| 3194 | "int const a[] = { 2, 3 };", |
| 3195 | qualType(arrayType(hasElementType(builtinType())), |
| 3196 | unless(isConstQualified())))); |
| 3197 | |
| 3198 | EXPECT_TRUE(matches("int a[2];", |
| 3199 | constantArrayType(hasElementType(builtinType())))); |
| 3200 | EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger()))); |
| 3201 | } |
| 3202 | |
| 3203 | TEST(TypeMatching, MatchesComplexTypes) { |
| 3204 | EXPECT_TRUE(matches("_Complex float f;", complexType())); |
| 3205 | EXPECT_TRUE(matches( |
| 3206 | "_Complex float f;", |
| 3207 | complexType(hasElementType(builtinType())))); |
| 3208 | EXPECT_TRUE(notMatches( |
| 3209 | "_Complex float f;", |
| 3210 | complexType(hasElementType(isInteger())))); |
| 3211 | } |
| 3212 | |
| 3213 | TEST(TypeMatching, MatchesConstantArrayTypes) { |
| 3214 | EXPECT_TRUE(matches("int a[2];", constantArrayType())); |
| 3215 | EXPECT_TRUE(notMatches( |
| 3216 | "void f() { int a[] = { 2, 3 }; int b[a[0]]; }", |
| 3217 | constantArrayType(hasElementType(builtinType())))); |
| 3218 | |
| 3219 | EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42)))); |
| 3220 | EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42)))); |
| 3221 | EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42)))); |
| 3222 | } |
| 3223 | |
| 3224 | TEST(TypeMatching, MatchesDependentSizedArrayTypes) { |
| 3225 | EXPECT_TRUE(matches( |
| 3226 | "template <typename T, int Size> class array { T data[Size]; };", |
| 3227 | dependentSizedArrayType())); |
| 3228 | EXPECT_TRUE(notMatches( |
| 3229 | "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }", |
| 3230 | dependentSizedArrayType())); |
| 3231 | } |
| 3232 | |
| 3233 | TEST(TypeMatching, MatchesIncompleteArrayType) { |
| 3234 | EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType())); |
| 3235 | EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType())); |
| 3236 | |
| 3237 | EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }", |
| 3238 | incompleteArrayType())); |
| 3239 | } |
| 3240 | |
| 3241 | TEST(TypeMatching, MatchesVariableArrayType) { |
| 3242 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType())); |
| 3243 | EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType())); |
| 3244 | |
| 3245 | EXPECT_TRUE(matches( |
| 3246 | "void f(int b) { int a[b]; }", |
| 3247 | variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( |
| 3248 | varDecl(hasName("b"))))))))); |
| 3249 | } |
| 3250 | |
| 3251 | TEST(TypeMatching, MatchesAtomicTypes) { |
| 3252 | EXPECT_TRUE(matches("_Atomic(int) i;", atomicType())); |
| 3253 | |
| 3254 | EXPECT_TRUE(matches("_Atomic(int) i;", |
| 3255 | atomicType(hasValueType(isInteger())))); |
| 3256 | EXPECT_TRUE(notMatches("_Atomic(float) f;", |
| 3257 | atomicType(hasValueType(isInteger())))); |
| 3258 | } |
| 3259 | |
| 3260 | TEST(TypeMatching, MatchesAutoTypes) { |
| 3261 | EXPECT_TRUE(matches("auto i = 2;", autoType())); |
| 3262 | EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }", |
| 3263 | autoType())); |
| 3264 | |
| 3265 | EXPECT_TRUE(matches("auto a = 1;", |
| 3266 | autoType(hasDeducedType(isInteger())))); |
| 3267 | EXPECT_TRUE(notMatches("auto b = 2.0;", |
| 3268 | autoType(hasDeducedType(isInteger())))); |
| 3269 | } |
| 3270 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 3271 | TEST(TypeMatching, MatchesFunctionTypes) { |
| 3272 | EXPECT_TRUE(matches("int (*f)(int);", functionType())); |
| 3273 | EXPECT_TRUE(matches("void f(int i) {}", functionType())); |
| 3274 | } |
| 3275 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3276 | TEST(TypeMatching, PointerTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3277 | // FIXME: Reactive when these tests can be more specific (not matching |
| 3278 | // implicit code on certain platforms), likely when we have hasDescendant for |
| 3279 | // Types/TypeLocs. |
| 3280 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3281 | // "int* a;", |
| 3282 | // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))), |
| 3283 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
| 3284 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3285 | // "int* a;", |
| 3286 | // pointerTypeLoc().bind("loc"), |
| 3287 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3288 | EXPECT_TRUE(matches( |
| 3289 | "int** a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3290 | loc(pointerType(pointee(qualType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3291 | EXPECT_TRUE(matches( |
| 3292 | "int** a;", |
| 3293 | loc(pointerType(pointee(pointerType()))))); |
| 3294 | EXPECT_TRUE(matches( |
| 3295 | "int* b; int* * const a = &b;", |
| 3296 | loc(qualType(isConstQualified(), pointerType())))); |
| 3297 | |
| 3298 | std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3299 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3300 | hasType(blockPointerType())))); |
| 3301 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3302 | hasType(memberPointerType())))); |
| 3303 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3304 | hasType(pointerType())))); |
| 3305 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3306 | hasType(referenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3307 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3308 | Fragment = "int *ptr;"; |
| 3309 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3310 | hasType(blockPointerType())))); |
| 3311 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3312 | hasType(memberPointerType())))); |
| 3313 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3314 | hasType(pointerType())))); |
| 3315 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3316 | hasType(referenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3317 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3318 | Fragment = "int a; int &ref = a;"; |
| 3319 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3320 | hasType(blockPointerType())))); |
| 3321 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3322 | hasType(memberPointerType())))); |
| 3323 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3324 | hasType(pointerType())))); |
| 3325 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3326 | hasType(referenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | TEST(TypeMatching, PointeeTypes) { |
| 3330 | EXPECT_TRUE(matches("int b; int &a = b;", |
| 3331 | referenceType(pointee(builtinType())))); |
| 3332 | EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType())))); |
| 3333 | |
| 3334 | EXPECT_TRUE(matches("int *a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3335 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3336 | |
| 3337 | EXPECT_TRUE(matches( |
| 3338 | "int const *A;", |
| 3339 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3340 | EXPECT_TRUE(notMatches( |
| 3341 | "int *A;", |
| 3342 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3343 | } |
| 3344 | |
| 3345 | TEST(TypeMatching, MatchesPointersToConstTypes) { |
| 3346 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
| 3347 | loc(pointerType()))); |
| 3348 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3349 | loc(pointerType()))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3350 | EXPECT_TRUE(matches( |
| 3351 | "int b; const int * a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3352 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3353 | EXPECT_TRUE(matches( |
| 3354 | "int b; const int * a = &b;", |
| 3355 | pointerType(pointee(builtinType())))); |
| 3356 | } |
| 3357 | |
| 3358 | TEST(TypeMatching, MatchesTypedefTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3359 | EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"), |
| 3360 | hasType(typedefType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3361 | } |
| 3362 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3363 | TEST(NNS, MatchesNestedNameSpecifiers) { |
| 3364 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", |
| 3365 | nestedNameSpecifier())); |
| 3366 | EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };", |
| 3367 | nestedNameSpecifier())); |
| 3368 | EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}", |
| 3369 | nestedNameSpecifier())); |
| 3370 | |
| 3371 | EXPECT_TRUE(matches( |
| 3372 | "struct A { static void f() {} }; void g() { A::f(); }", |
| 3373 | nestedNameSpecifier())); |
| 3374 | EXPECT_TRUE(notMatches( |
| 3375 | "struct A { static void f() {} }; void g(A* a) { a->f(); }", |
| 3376 | nestedNameSpecifier())); |
| 3377 | } |
| 3378 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 3379 | TEST(NullStatement, SimpleCases) { |
| 3380 | EXPECT_TRUE(matches("void f() {int i;;}", nullStmt())); |
| 3381 | EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt())); |
| 3382 | } |
| 3383 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3384 | TEST(NNS, MatchesTypes) { |
| 3385 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3386 | specifiesType(hasDeclaration(recordDecl(hasName("A"))))); |
| 3387 | EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher)); |
| 3388 | EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 3389 | Matcher)); |
| 3390 | EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher)); |
| 3391 | } |
| 3392 | |
| 3393 | TEST(NNS, MatchesNamespaceDecls) { |
| 3394 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3395 | specifiesNamespace(hasName("ns"))); |
| 3396 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher)); |
| 3397 | EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher)); |
| 3398 | EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher)); |
| 3399 | } |
| 3400 | |
| 3401 | TEST(NNS, BindsNestedNameSpecifiers) { |
| 3402 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3403 | "namespace ns { struct E { struct B {}; }; } ns::E::B b;", |
| 3404 | nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"), |
| 3405 | new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::"))); |
| 3406 | } |
| 3407 | |
| 3408 | TEST(NNS, BindsNestedNameSpecifierLocs) { |
| 3409 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3410 | "namespace ns { struct B {}; } ns::B b;", |
| 3411 | loc(nestedNameSpecifier()).bind("loc"), |
| 3412 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1))); |
| 3413 | } |
| 3414 | |
| 3415 | TEST(NNS, MatchesNestedNameSpecifierPrefixes) { |
| 3416 | EXPECT_TRUE(matches( |
| 3417 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 3418 | nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))))); |
| 3419 | EXPECT_TRUE(matches( |
| 3420 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3421 | nestedNameSpecifierLoc(hasPrefix( |
| 3422 | specifiesTypeLoc(loc(qualType(asString("struct A")))))))); |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
Daniel Jasper | d1ce3c1 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 3425 | TEST(NNS, DescendantsOfNestedNameSpecifiers) { |
| 3426 | std::string Fragment = |
| 3427 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3428 | "void f() { a::A::B::C c; }"; |
| 3429 | EXPECT_TRUE(matches( |
| 3430 | Fragment, |
| 3431 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3432 | hasDescendant(nestedNameSpecifier( |
| 3433 | specifiesNamespace(hasName("a"))))))); |
| 3434 | EXPECT_TRUE(notMatches( |
| 3435 | Fragment, |
| 3436 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3437 | has(nestedNameSpecifier( |
| 3438 | specifiesNamespace(hasName("a"))))))); |
| 3439 | EXPECT_TRUE(matches( |
| 3440 | Fragment, |
| 3441 | nestedNameSpecifier(specifiesType(asString("struct a::A")), |
| 3442 | has(nestedNameSpecifier( |
| 3443 | specifiesNamespace(hasName("a"))))))); |
| 3444 | |
| 3445 | // Not really useful because a NestedNameSpecifier can af at most one child, |
| 3446 | // but to complete the interface. |
| 3447 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3448 | Fragment, |
| 3449 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 3450 | forEach(nestedNameSpecifier().bind("x"))), |
| 3451 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1))); |
| 3452 | } |
| 3453 | |
| 3454 | TEST(NNS, NestedNameSpecifiersAsDescendants) { |
| 3455 | std::string Fragment = |
| 3456 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3457 | "void f() { a::A::B::C c; }"; |
| 3458 | EXPECT_TRUE(matches( |
| 3459 | Fragment, |
| 3460 | decl(hasDescendant(nestedNameSpecifier(specifiesType( |
| 3461 | asString("struct a::A"))))))); |
| 3462 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3463 | Fragment, |
| 3464 | functionDecl(hasName("f"), |
| 3465 | forEachDescendant(nestedNameSpecifier().bind("x"))), |
| 3466 | // Nested names: a, a::A and a::A::B. |
| 3467 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3))); |
| 3468 | } |
| 3469 | |
| 3470 | TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { |
| 3471 | std::string Fragment = |
| 3472 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3473 | "void f() { a::A::B::C c; }"; |
| 3474 | EXPECT_TRUE(matches( |
| 3475 | Fragment, |
| 3476 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3477 | hasDescendant(loc(nestedNameSpecifier( |
| 3478 | specifiesNamespace(hasName("a")))))))); |
| 3479 | EXPECT_TRUE(notMatches( |
| 3480 | Fragment, |
| 3481 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3482 | has(loc(nestedNameSpecifier( |
| 3483 | specifiesNamespace(hasName("a")))))))); |
| 3484 | EXPECT_TRUE(matches( |
| 3485 | Fragment, |
| 3486 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))), |
| 3487 | has(loc(nestedNameSpecifier( |
| 3488 | specifiesNamespace(hasName("a")))))))); |
| 3489 | |
| 3490 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3491 | Fragment, |
| 3492 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 3493 | forEach(nestedNameSpecifierLoc().bind("x"))), |
| 3494 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1))); |
| 3495 | } |
| 3496 | |
| 3497 | TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { |
| 3498 | std::string Fragment = |
| 3499 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 3500 | "void f() { a::A::B::C c; }"; |
| 3501 | EXPECT_TRUE(matches( |
| 3502 | Fragment, |
| 3503 | decl(hasDescendant(loc(nestedNameSpecifier(specifiesType( |
| 3504 | asString("struct a::A")))))))); |
| 3505 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3506 | Fragment, |
| 3507 | functionDecl(hasName("f"), |
| 3508 | forEachDescendant(nestedNameSpecifierLoc().bind("x"))), |
| 3509 | // Nested names: a, a::A and a::A::B. |
| 3510 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3))); |
| 3511 | } |
| 3512 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3513 | template <typename T> class VerifyMatchOnNode : public BoundNodesCallback { |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3514 | public: |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3515 | VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher, |
| 3516 | StringRef InnerId) |
| 3517 | : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) { |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3520 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 3521 | |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3522 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 3523 | const T *Node = Nodes->getNodeAs<T>(Id); |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3524 | return selectFirst<const T>(InnerId, |
| 3525 | match(InnerMatcher, *Node, *Context)) != NULL; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3526 | } |
| 3527 | private: |
| 3528 | std::string Id; |
| 3529 | internal::Matcher<T> InnerMatcher; |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3530 | std::string InnerId; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3531 | }; |
| 3532 | |
| 3533 | TEST(MatchFinder, CanMatchDeclarationsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3534 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3535 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 3536 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3537 | "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))), |
| 3538 | "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3539 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 3540 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 3541 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3542 | "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))), |
| 3543 | "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | TEST(MatchFinder, CanMatchStatementsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3547 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3548 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3549 | new VerifyMatchOnNode<clang::Stmt>( |
| 3550 | "if", stmt(hasDescendant(forStmt().bind("for"))), "for"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3551 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 3552 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3553 | new VerifyMatchOnNode<clang::Stmt>( |
| 3554 | "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | TEST(MatchFinder, CanMatchSingleNodesRecursively) { |
| 3558 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3559 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 3560 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3561 | "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 3562 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 3563 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 3564 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 3565 | "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 3568 | template <typename T> |
| 3569 | class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { |
| 3570 | public: |
| 3571 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 3572 | |
| 3573 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 3574 | const T *Node = Nodes->getNodeAs<T>(""); |
| 3575 | return verify(*Nodes, *Context, Node); |
| 3576 | } |
| 3577 | |
| 3578 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { |
| 3579 | return selectFirst<const T>( |
| 3580 | "", match(stmt(hasParent(stmt(has(stmt(equalsNode(Node)))).bind(""))), |
| 3581 | *Node, Context)) != NULL; |
| 3582 | } |
| 3583 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { |
| 3584 | return selectFirst<const T>( |
| 3585 | "", match(decl(hasParent(decl(has(decl(equalsNode(Node)))).bind(""))), |
| 3586 | *Node, Context)) != NULL; |
| 3587 | } |
| 3588 | }; |
| 3589 | |
| 3590 | TEST(IsEqualTo, MatchesNodesByIdentity) { |
| 3591 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3592 | "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""), |
| 3593 | new VerifyAncestorHasChildIsEqual<Decl>())); |
| 3594 | EXPECT_TRUE( |
| 3595 | matchAndVerifyResultTrue("void f() { if(true) {} }", ifStmt().bind(""), |
| 3596 | new VerifyAncestorHasChildIsEqual<Stmt>())); |
| 3597 | } |
| 3598 | |
Manuel Klimek | e579328 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 3599 | class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback { |
| 3600 | public: |
| 3601 | VerifyStartOfTranslationUnit() : Called(false) {} |
| 3602 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 3603 | EXPECT_TRUE(Called); |
| 3604 | } |
| 3605 | virtual void onStartOfTranslationUnit() { |
| 3606 | Called = true; |
| 3607 | } |
| 3608 | bool Called; |
| 3609 | }; |
| 3610 | |
| 3611 | TEST(MatchFinder, InterceptsStartOfTranslationUnit) { |
| 3612 | MatchFinder Finder; |
| 3613 | VerifyStartOfTranslationUnit VerifyCallback; |
| 3614 | Finder.addMatcher(decl(), &VerifyCallback); |
| 3615 | OwningPtr<FrontendActionFactory> Factory(newFrontendActionFactory(&Finder)); |
| 3616 | ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); |
| 3617 | EXPECT_TRUE(VerifyCallback.Called); |
| 3618 | } |
| 3619 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3620 | } // end namespace ast_matchers |
| 3621 | } // end namespace clang |