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