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