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