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 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 297 | TEST(ClassTemplate, DoesNotMatchClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 298 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 299 | EXPECT_TRUE(notMatches("class X;", ClassX)); |
| 300 | EXPECT_TRUE(notMatches("class X {};", ClassX)); |
| 301 | } |
| 302 | |
| 303 | TEST(ClassTemplate, MatchesClassTemplate) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 304 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 305 | EXPECT_TRUE(matches("template<typename T> class X {};", ClassX)); |
| 306 | EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX)); |
| 307 | } |
| 308 | |
| 309 | TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) { |
| 310 | EXPECT_TRUE(notMatches("template<typename T> class X { };" |
| 311 | "template<> class X<int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 312 | classTemplateDecl(hasName("X"), |
| 313 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) { |
| 317 | EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };" |
| 318 | "template<typename T> class X<T, int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 319 | classTemplateDecl(hasName("X"), |
| 320 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 323 | TEST(AllOf, AllOverloadsWork) { |
| 324 | const char Program[] = |
| 325 | "struct T { }; int f(int, T*); void g(int x) { T t; f(x, &t); }"; |
| 326 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 327 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 328 | hasArgument(0, declRefExpr(to(varDecl()))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 329 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 330 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 331 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 332 | hasArgument(1, hasType(pointsTo( |
| 333 | recordDecl(hasName("T"))))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 336 | TEST(DeclarationMatcher, MatchAnyOf) { |
| 337 | DeclarationMatcher YOrZDerivedFromX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 338 | recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 339 | EXPECT_TRUE( |
| 340 | matches("class X {}; class Z : public X {};", YOrZDerivedFromX)); |
| 341 | EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX)); |
| 342 | EXPECT_TRUE( |
| 343 | notMatches("class X {}; class W : public X {};", YOrZDerivedFromX)); |
| 344 | EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX)); |
| 345 | |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 346 | DeclarationMatcher XOrYOrZOrU = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 347 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"))); |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 348 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrU)); |
| 349 | EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU)); |
| 350 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 351 | DeclarationMatcher XOrYOrZOrUOrV = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 352 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"), |
| 353 | hasName("V"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 354 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV)); |
| 355 | EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV)); |
| 356 | EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV)); |
| 357 | EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); |
| 358 | EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); |
| 359 | EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); |
| 360 | } |
| 361 | |
| 362 | TEST(DeclarationMatcher, MatchHas) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 363 | DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 364 | EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); |
| 365 | EXPECT_TRUE(matches("class X {};", HasClassX)); |
| 366 | |
| 367 | DeclarationMatcher YHasClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 368 | recordDecl(hasName("Y"), has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 369 | EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX)); |
| 370 | EXPECT_TRUE(notMatches("class X {};", YHasClassX)); |
| 371 | EXPECT_TRUE( |
| 372 | notMatches("class Y { class Z { class X {}; }; };", YHasClassX)); |
| 373 | } |
| 374 | |
| 375 | TEST(DeclarationMatcher, MatchHasRecursiveAllOf) { |
| 376 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 377 | recordDecl( |
| 378 | has(recordDecl( |
| 379 | has(recordDecl(hasName("X"))), |
| 380 | has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 381 | hasName("Z"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 382 | has(recordDecl( |
| 383 | has(recordDecl(hasName("A"))), |
| 384 | has(recordDecl(hasName("B"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 385 | hasName("C"))), |
| 386 | hasName("F")); |
| 387 | |
| 388 | EXPECT_TRUE(matches( |
| 389 | "class F {" |
| 390 | " class Z {" |
| 391 | " class X {};" |
| 392 | " class Y {};" |
| 393 | " };" |
| 394 | " class C {" |
| 395 | " class A {};" |
| 396 | " class B {};" |
| 397 | " };" |
| 398 | "};", Recursive)); |
| 399 | |
| 400 | EXPECT_TRUE(matches( |
| 401 | "class F {" |
| 402 | " class Z {" |
| 403 | " class A {};" |
| 404 | " class X {};" |
| 405 | " class Y {};" |
| 406 | " };" |
| 407 | " class C {" |
| 408 | " class X {};" |
| 409 | " class A {};" |
| 410 | " class B {};" |
| 411 | " };" |
| 412 | "};", Recursive)); |
| 413 | |
| 414 | EXPECT_TRUE(matches( |
| 415 | "class O1 {" |
| 416 | " class O2 {" |
| 417 | " class F {" |
| 418 | " class Z {" |
| 419 | " class A {};" |
| 420 | " class X {};" |
| 421 | " class Y {};" |
| 422 | " };" |
| 423 | " class C {" |
| 424 | " class X {};" |
| 425 | " class A {};" |
| 426 | " class B {};" |
| 427 | " };" |
| 428 | " };" |
| 429 | " };" |
| 430 | "};", Recursive)); |
| 431 | } |
| 432 | |
| 433 | TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) { |
| 434 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 435 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 436 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 437 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 438 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 439 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 440 | hasName("X"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 441 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 442 | hasName("Y"))), |
| 443 | hasName("Z")))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 444 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 445 | anyOf( |
| 446 | hasName("C"), |
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 | hasName("A"))), |
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("B")))))), |
| 451 | hasName("F"))); |
| 452 | |
| 453 | EXPECT_TRUE(matches("class F {};", Recursive)); |
| 454 | EXPECT_TRUE(matches("class Z {};", Recursive)); |
| 455 | EXPECT_TRUE(matches("class C {};", Recursive)); |
| 456 | EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive)); |
| 457 | EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive)); |
| 458 | EXPECT_TRUE( |
| 459 | matches("class O1 { class O2 {" |
| 460 | " class M { class N { class B {}; }; }; " |
| 461 | "}; };", Recursive)); |
| 462 | } |
| 463 | |
| 464 | TEST(DeclarationMatcher, MatchNot) { |
| 465 | DeclarationMatcher NotClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 466 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 467 | isDerivedFrom("Y"), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 468 | unless(hasName("X"))); |
| 469 | EXPECT_TRUE(notMatches("", NotClassX)); |
| 470 | EXPECT_TRUE(notMatches("class Y {};", NotClassX)); |
| 471 | EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX)); |
| 472 | EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX)); |
| 473 | EXPECT_TRUE( |
| 474 | notMatches("class Y {}; class Z {}; class X : public Y {};", |
| 475 | NotClassX)); |
| 476 | |
| 477 | DeclarationMatcher ClassXHasNotClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 478 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 479 | hasName("X"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 480 | has(recordDecl(hasName("Z"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 481 | unless( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 482 | has(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 483 | EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY)); |
| 484 | EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };", |
| 485 | ClassXHasNotClassY)); |
| 486 | } |
| 487 | |
| 488 | TEST(DeclarationMatcher, HasDescendant) { |
| 489 | DeclarationMatcher ZDescendantClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 490 | recordDecl( |
| 491 | hasDescendant(recordDecl(hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 492 | hasName("Z")); |
| 493 | EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX)); |
| 494 | EXPECT_TRUE( |
| 495 | matches("class Z { class Y { class X {}; }; };", ZDescendantClassX)); |
| 496 | EXPECT_TRUE( |
| 497 | matches("class Z { class A { class Y { class X {}; }; }; };", |
| 498 | ZDescendantClassX)); |
| 499 | EXPECT_TRUE( |
| 500 | matches("class Z { class A { class B { class Y { class X {}; }; }; }; };", |
| 501 | ZDescendantClassX)); |
| 502 | EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX)); |
| 503 | |
| 504 | DeclarationMatcher ZDescendantClassXHasClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 505 | recordDecl( |
| 506 | hasDescendant(recordDecl(has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 507 | hasName("X"))), |
| 508 | hasName("Z")); |
| 509 | EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };", |
| 510 | ZDescendantClassXHasClassY)); |
| 511 | EXPECT_TRUE( |
| 512 | matches("class Z { class A { class B { class X { class Y {}; }; }; }; };", |
| 513 | ZDescendantClassXHasClassY)); |
| 514 | EXPECT_TRUE(notMatches( |
| 515 | "class Z {" |
| 516 | " class A {" |
| 517 | " class B {" |
| 518 | " class X {" |
| 519 | " class C {" |
| 520 | " class Y {};" |
| 521 | " };" |
| 522 | " };" |
| 523 | " }; " |
| 524 | " };" |
| 525 | "};", ZDescendantClassXHasClassY)); |
| 526 | |
| 527 | DeclarationMatcher ZDescendantClassXDescendantClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 528 | recordDecl( |
| 529 | hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))), |
| 530 | hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 531 | hasName("Z")); |
| 532 | EXPECT_TRUE( |
| 533 | matches("class Z { class A { class X { class B { class Y {}; }; }; }; };", |
| 534 | ZDescendantClassXDescendantClassY)); |
| 535 | EXPECT_TRUE(matches( |
| 536 | "class Z {" |
| 537 | " class A {" |
| 538 | " class X {" |
| 539 | " class B {" |
| 540 | " class Y {};" |
| 541 | " };" |
| 542 | " class Y {};" |
| 543 | " };" |
| 544 | " };" |
| 545 | "};", ZDescendantClassXDescendantClassY)); |
| 546 | } |
| 547 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 548 | TEST(Enum, DoesNotMatchClasses) { |
| 549 | EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X")))); |
| 550 | } |
| 551 | |
| 552 | TEST(Enum, MatchesEnums) { |
| 553 | EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X")))); |
| 554 | } |
| 555 | |
| 556 | TEST(EnumConstant, Matches) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 557 | DeclarationMatcher Matcher = enumConstantDecl(hasName("A")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 558 | EXPECT_TRUE(matches("enum X{ A };", Matcher)); |
| 559 | EXPECT_TRUE(notMatches("enum X{ B };", Matcher)); |
| 560 | EXPECT_TRUE(notMatches("enum X {};", Matcher)); |
| 561 | } |
| 562 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 563 | TEST(StatementMatcher, Has) { |
| 564 | StatementMatcher HasVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 565 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 566 | has(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 567 | |
| 568 | EXPECT_TRUE(matches( |
| 569 | "class X; X *x(int); void c() { int i; x(i); }", HasVariableI)); |
| 570 | EXPECT_TRUE(notMatches( |
| 571 | "class X; X *x(int); void c() { int i; x(42); }", HasVariableI)); |
| 572 | } |
| 573 | |
| 574 | TEST(StatementMatcher, HasDescendant) { |
| 575 | StatementMatcher HasDescendantVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 576 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 577 | hasDescendant(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 578 | |
| 579 | EXPECT_TRUE(matches( |
| 580 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }", |
| 581 | HasDescendantVariableI)); |
| 582 | EXPECT_TRUE(notMatches( |
| 583 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }", |
| 584 | HasDescendantVariableI)); |
| 585 | } |
| 586 | |
| 587 | TEST(TypeMatcher, MatchesClassType) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 588 | TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 589 | |
| 590 | EXPECT_TRUE(matches("class A { public: A *a; };", TypeA)); |
| 591 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 592 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 593 | TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 594 | |
| 595 | EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };", |
| 596 | TypeDerivedFromA)); |
| 597 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 598 | |
| 599 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 600 | recordDecl(hasName("A"), has(recordDecl(hasName("B"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 601 | |
| 602 | EXPECT_TRUE( |
| 603 | matches("class A { public: A *a; class B {}; };", TypeAHasClassB)); |
| 604 | } |
| 605 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 606 | // Implements a run method that returns whether BoundNodes contains a |
| 607 | // Decl bound to Id that can be dynamically cast to T. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 608 | // Optionally checks that the check succeeded a specific number of times. |
| 609 | template <typename T> |
| 610 | class VerifyIdIsBoundToDecl : public BoundNodesCallback { |
| 611 | public: |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 612 | // Create an object that checks that a node of type \c T was bound to \c Id. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 613 | // Does not check for a certain number of matches. |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 614 | explicit VerifyIdIsBoundToDecl(llvm::StringRef Id) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 615 | : Id(Id), ExpectedCount(-1), Count(0) {} |
| 616 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 617 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 618 | // Checks that there were exactly \c ExpectedCount matches. |
| 619 | VerifyIdIsBoundToDecl(llvm::StringRef Id, int ExpectedCount) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 620 | : Id(Id), ExpectedCount(ExpectedCount), Count(0) {} |
| 621 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 622 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 623 | // Checks that there was exactly one match with the name \c ExpectedDeclName. |
| 624 | // Note that \c T must be a NamedDecl for this to work. |
| 625 | VerifyIdIsBoundToDecl(llvm::StringRef Id, llvm::StringRef ExpectedDeclName) |
| 626 | : Id(Id), ExpectedCount(1), Count(0), ExpectedDeclName(ExpectedDeclName) {} |
| 627 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 628 | ~VerifyIdIsBoundToDecl() { |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 629 | if (ExpectedCount != -1) |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 630 | EXPECT_EQ(ExpectedCount, Count); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 631 | if (!ExpectedDeclName.empty()) |
| 632 | EXPECT_EQ(ExpectedDeclName, DeclName); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | virtual bool run(const BoundNodes *Nodes) { |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 636 | if (const Decl *Node = Nodes->getDeclAs<T>(Id)) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 637 | ++Count; |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 638 | if (const NamedDecl *Named = llvm::dyn_cast<NamedDecl>(Node)) { |
| 639 | DeclName = Named->getNameAsString(); |
| 640 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 641 | return true; |
| 642 | } |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | private: |
| 647 | const std::string Id; |
| 648 | const int ExpectedCount; |
| 649 | int Count; |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 650 | const std::string ExpectedDeclName; |
| 651 | std::string DeclName; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 652 | }; |
| 653 | template <typename T> |
| 654 | class VerifyIdIsBoundToStmt : public BoundNodesCallback { |
| 655 | public: |
| 656 | explicit VerifyIdIsBoundToStmt(const std::string &Id) : Id(Id) {} |
| 657 | virtual bool run(const BoundNodes *Nodes) { |
| 658 | const T *Node = Nodes->getStmtAs<T>(Id); |
| 659 | return Node != NULL; |
| 660 | } |
| 661 | private: |
| 662 | const std::string Id; |
| 663 | }; |
| 664 | |
| 665 | TEST(Matcher, BindMatchedNodes) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 666 | DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 667 | |
| 668 | EXPECT_TRUE(matchAndVerifyResultTrue("class X {};", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 669 | ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 670 | |
| 671 | EXPECT_TRUE(matchAndVerifyResultFalse("class X {};", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 672 | ClassX, new VerifyIdIsBoundToDecl<CXXRecordDecl>("other-id"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 673 | |
| 674 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 675 | recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 676 | |
| 677 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };", |
| 678 | TypeAHasClassB, |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 679 | new VerifyIdIsBoundToDecl<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 680 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 681 | StatementMatcher MethodX = |
| 682 | callExpr(callee(methodDecl(hasName("x")))).bind("x"); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 683 | |
| 684 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };", |
| 685 | MethodX, |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 686 | new VerifyIdIsBoundToStmt<CXXMemberCallExpr>("x"))); |
| 687 | } |
| 688 | |
| 689 | TEST(Matcher, BindTheSameNameInAlternatives) { |
| 690 | StatementMatcher matcher = anyOf( |
| 691 | binaryOperator(hasOperatorName("+"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 692 | hasLHS(expr().bind("x")), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 693 | hasRHS(integerLiteral(equals(0)))), |
| 694 | binaryOperator(hasOperatorName("+"), |
| 695 | hasLHS(integerLiteral(equals(0))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 696 | hasRHS(expr().bind("x")))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 697 | |
| 698 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 699 | // The first branch of the matcher binds x to 0 but then fails. |
| 700 | // The second branch binds x to f() and succeeds. |
| 701 | "int f() { return 0 + f(); }", |
| 702 | matcher, |
| 703 | new VerifyIdIsBoundToStmt<CallExpr>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 706 | TEST(Matcher, BindsIDForMemoizedResults) { |
| 707 | // Using the same matcher in two match expressions will make memoization |
| 708 | // kick in. |
| 709 | DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x"); |
| 710 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 711 | "class A { class B { class X {}; }; };", |
| 712 | DeclarationMatcher(anyOf( |
| 713 | recordDecl(hasName("A"), hasDescendant(ClassX)), |
| 714 | recordDecl(hasName("B"), hasDescendant(ClassX)))), |
| 715 | new VerifyIdIsBoundToDecl<Decl>("x", 2))); |
| 716 | } |
| 717 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 718 | TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 719 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 720 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 721 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 722 | EXPECT_TRUE( |
| 723 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 724 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 725 | EXPECT_TRUE( |
| 726 | matches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 727 | expr(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 731 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 732 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 733 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 734 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 735 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 736 | EXPECT_TRUE( |
| 737 | matches("class X {}; void y() { X *x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 738 | varDecl(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 742 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 743 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 744 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 745 | EXPECT_TRUE( |
| 746 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 747 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 751 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 752 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 753 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 754 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 755 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | TEST(Matcher, Call) { |
| 759 | // FIXME: Do we want to overload Call() to directly take |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 760 | // Matcher<Decl>, too? |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 761 | StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 762 | |
| 763 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 764 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 765 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 766 | StatementMatcher MethodOnY = |
| 767 | memberCallExpr(on(hasType(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 768 | |
| 769 | EXPECT_TRUE( |
| 770 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 771 | MethodOnY)); |
| 772 | EXPECT_TRUE( |
| 773 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 774 | MethodOnY)); |
| 775 | EXPECT_TRUE( |
| 776 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 777 | MethodOnY)); |
| 778 | EXPECT_TRUE( |
| 779 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 780 | MethodOnY)); |
| 781 | EXPECT_TRUE( |
| 782 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 783 | MethodOnY)); |
| 784 | |
| 785 | StatementMatcher MethodOnYPointer = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 786 | memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 787 | |
| 788 | EXPECT_TRUE( |
| 789 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 790 | MethodOnYPointer)); |
| 791 | EXPECT_TRUE( |
| 792 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 793 | MethodOnYPointer)); |
| 794 | EXPECT_TRUE( |
| 795 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 796 | MethodOnYPointer)); |
| 797 | EXPECT_TRUE( |
| 798 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 799 | MethodOnYPointer)); |
| 800 | EXPECT_TRUE( |
| 801 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 802 | MethodOnYPointer)); |
| 803 | } |
| 804 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 805 | TEST(HasType, MatchesAsString) { |
| 806 | EXPECT_TRUE( |
| 807 | matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 808 | memberCallExpr(on(hasType(asString("class Y *")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 809 | EXPECT_TRUE(matches("class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 810 | methodDecl(hasParameter(0, hasType(asString("int")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 811 | EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 812 | fieldDecl(hasType(asString("ns::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 813 | EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 814 | fieldDecl(hasType(asString("struct <anonymous>::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 817 | TEST(Matcher, OverloadedOperatorCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 818 | StatementMatcher OpCall = operatorCallExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 819 | // Unary operator |
| 820 | EXPECT_TRUE(matches("class Y { }; " |
| 821 | "bool operator!(Y x) { return false; }; " |
| 822 | "Y y; bool c = !y;", OpCall)); |
| 823 | // No match -- special operators like "new", "delete" |
| 824 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
| 825 | // we need to figure out include paths in the test. |
| 826 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
| 827 | // "class Y { }; " |
| 828 | // "void *operator new(size_t size) { return 0; } " |
| 829 | // "Y *y = new Y;", OpCall)); |
| 830 | EXPECT_TRUE(notMatches("class Y { }; " |
| 831 | "void operator delete(void *p) { } " |
| 832 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 833 | // Binary operator |
| 834 | EXPECT_TRUE(matches("class Y { }; " |
| 835 | "bool operator&&(Y x, Y y) { return true; }; " |
| 836 | "Y a; Y b; bool c = a && b;", |
| 837 | OpCall)); |
| 838 | // No match -- normal operator, not an overloaded one. |
| 839 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 840 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 841 | } |
| 842 | |
| 843 | TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { |
| 844 | StatementMatcher OpCallAndAnd = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 845 | operatorCallExpr(hasOverloadedOperatorName("&&")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 846 | EXPECT_TRUE(matches("class Y { }; " |
| 847 | "bool operator&&(Y x, Y y) { return true; }; " |
| 848 | "Y a; Y b; bool c = a && b;", OpCallAndAnd)); |
| 849 | StatementMatcher OpCallLessLess = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 850 | operatorCallExpr(hasOverloadedOperatorName("<<")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 851 | EXPECT_TRUE(notMatches("class Y { }; " |
| 852 | "bool operator&&(Y x, Y y) { return true; }; " |
| 853 | "Y a; Y b; bool c = a && b;", |
| 854 | OpCallLessLess)); |
| 855 | } |
| 856 | |
| 857 | TEST(Matcher, ThisPointerType) { |
Manuel Klimek | 9f17408 | 2012-07-24 13:37:29 +0000 | [diff] [blame] | 858 | StatementMatcher MethodOnY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 859 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 860 | |
| 861 | EXPECT_TRUE( |
| 862 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 863 | MethodOnY)); |
| 864 | EXPECT_TRUE( |
| 865 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 866 | MethodOnY)); |
| 867 | EXPECT_TRUE( |
| 868 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 869 | MethodOnY)); |
| 870 | EXPECT_TRUE( |
| 871 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 872 | MethodOnY)); |
| 873 | EXPECT_TRUE( |
| 874 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 875 | MethodOnY)); |
| 876 | |
| 877 | EXPECT_TRUE(matches( |
| 878 | "class Y {" |
| 879 | " public: virtual void x();" |
| 880 | "};" |
| 881 | "class X : public Y {" |
| 882 | " public: virtual void x();" |
| 883 | "};" |
| 884 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 885 | } |
| 886 | |
| 887 | TEST(Matcher, VariableUsage) { |
| 888 | StatementMatcher Reference = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 889 | declRefExpr(to( |
| 890 | varDecl(hasInitializer( |
| 891 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 892 | |
| 893 | EXPECT_TRUE(matches( |
| 894 | "class Y {" |
| 895 | " public:" |
| 896 | " bool x() const;" |
| 897 | "};" |
| 898 | "void z(const Y &y) {" |
| 899 | " bool b = y.x();" |
| 900 | " if (b) {}" |
| 901 | "}", Reference)); |
| 902 | |
| 903 | EXPECT_TRUE(notMatches( |
| 904 | "class Y {" |
| 905 | " public:" |
| 906 | " bool x() const;" |
| 907 | "};" |
| 908 | "void z(const Y &y) {" |
| 909 | " bool b = y.x();" |
| 910 | "}", Reference)); |
| 911 | } |
| 912 | |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 913 | TEST(Matcher, FindsVarDeclInFuncitonParameter) { |
| 914 | EXPECT_TRUE(matches( |
| 915 | "void f(int i) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 916 | varDecl(hasName("i")))); |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 919 | TEST(Matcher, CalledVariable) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 920 | StatementMatcher CallOnVariableY = expr( |
| 921 | memberCallExpr(on(declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 922 | |
| 923 | EXPECT_TRUE(matches( |
| 924 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 925 | EXPECT_TRUE(matches( |
| 926 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 927 | EXPECT_TRUE(matches( |
| 928 | "class Y { public: void x(); };" |
| 929 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 930 | EXPECT_TRUE(matches( |
| 931 | "class Y { public: void x(); };" |
| 932 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 933 | EXPECT_TRUE(notMatches( |
| 934 | "class Y { public: void x(); };" |
| 935 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 936 | CallOnVariableY)); |
| 937 | } |
| 938 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 939 | TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) { |
| 940 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 941 | unaryExprOrTypeTraitExpr())); |
| 942 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 943 | alignOfExpr(anything()))); |
| 944 | // FIXME: Uncomment once alignof is enabled. |
| 945 | // EXPECT_TRUE(matches("void x() { int a = alignof(a); }", |
| 946 | // unaryExprOrTypeTraitExpr())); |
| 947 | // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }", |
| 948 | // sizeOfExpr())); |
| 949 | } |
| 950 | |
| 951 | TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) { |
| 952 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 953 | hasArgumentOfType(asString("int"))))); |
| 954 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 955 | hasArgumentOfType(asString("float"))))); |
| 956 | EXPECT_TRUE(matches( |
| 957 | "struct A {}; void x() { A a; int b = sizeof(a); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 958 | sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 959 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 960 | hasArgumentOfType(hasDeclaration(recordDecl(hasName("string"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 963 | TEST(MemberExpression, DoesNotMatchClasses) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 964 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 968 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | TEST(MemberExpression, MatchesVariable) { |
| 972 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 973 | matches("class Y { void x() { this->y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 974 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 975 | matches("class Y { void x() { y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 976 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 977 | matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | TEST(MemberExpression, MatchesStaticVariable) { |
| 981 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 982 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 983 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 984 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 985 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 986 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 989 | TEST(IsInteger, MatchesIntegers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 990 | EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger())))); |
| 991 | EXPECT_TRUE(matches( |
| 992 | "long long i = 0; void f(long long) { }; void g() {f(i);}", |
| 993 | callExpr(hasArgument(0, declRefExpr( |
| 994 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | TEST(IsInteger, ReportsNoFalsePositives) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 998 | EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 999 | 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] | 1000 | callExpr(hasArgument(0, declRefExpr( |
| 1001 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1004 | TEST(IsArrow, MatchesMemberVariablesViaArrow) { |
| 1005 | EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1006 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1007 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1008 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1009 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1010 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) { |
| 1014 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1015 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1016 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1017 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1018 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1019 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | TEST(IsArrow, MatchesMemberCallsViaArrow) { |
| 1023 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1024 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1025 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1026 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1027 | EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1028 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | TEST(Callee, MatchesDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1032 | StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1033 | |
| 1034 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 1035 | EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 1036 | } |
| 1037 | |
| 1038 | TEST(Callee, MatchesMemberExpressions) { |
| 1039 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1040 | callExpr(callee(memberExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1041 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1042 | notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | TEST(Function, MatchesFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1046 | StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1047 | |
| 1048 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 1049 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 1050 | |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1051 | #if !defined(_MSC_VER) |
| 1052 | // FIXME: Make this work for MSVC. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1053 | // Dependent contexts, but a non-dependent call. |
| 1054 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 1055 | CallFunctionF)); |
| 1056 | EXPECT_TRUE( |
| 1057 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 1058 | CallFunctionF)); |
Manuel Klimek | e265c87 | 2012-07-10 14:21:30 +0000 | [diff] [blame] | 1059 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1060 | |
| 1061 | // Depedent calls don't match. |
| 1062 | EXPECT_TRUE( |
| 1063 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 1064 | CallFunctionF)); |
| 1065 | EXPECT_TRUE( |
| 1066 | notMatches("void f(int);" |
| 1067 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 1068 | CallFunctionF)); |
| 1069 | } |
| 1070 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1071 | TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) { |
| 1072 | EXPECT_TRUE( |
| 1073 | matches("template <typename T> void f(T t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1074 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) { |
| 1078 | EXPECT_TRUE( |
| 1079 | notMatches("void f(double d); void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1080 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) { |
| 1084 | EXPECT_TRUE( |
| 1085 | notMatches("void g(); template <typename T> void f(T t) {}" |
| 1086 | "template <> void f(int t) { g(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1087 | functionTemplateDecl(hasName("f"), |
| 1088 | hasDescendant(declRefExpr(to( |
| 1089 | functionDecl(hasName("g")))))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1092 | TEST(Matcher, Argument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1093 | StatementMatcher CallArgumentY = expr(callExpr( |
| 1094 | hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1095 | |
| 1096 | EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 1097 | EXPECT_TRUE( |
| 1098 | matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 1099 | EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 1100 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1101 | StatementMatcher WrongIndex = expr(callExpr( |
| 1102 | hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1103 | EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 1104 | } |
| 1105 | |
| 1106 | TEST(Matcher, AnyArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1107 | StatementMatcher CallArgumentY = expr(callExpr( |
| 1108 | hasAnyArgument(declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1109 | EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 1110 | EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 1111 | EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 1112 | } |
| 1113 | |
| 1114 | TEST(Matcher, ArgumentCount) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1115 | StatementMatcher Call1Arg = expr(callExpr(argumentCountIs(1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1116 | |
| 1117 | EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 1118 | EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 1119 | EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 1120 | } |
| 1121 | |
| 1122 | TEST(Matcher, References) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1123 | DeclarationMatcher ReferenceClassX = varDecl( |
| 1124 | hasType(references(recordDecl(hasName("X"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1125 | EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }", |
| 1126 | ReferenceClassX)); |
| 1127 | EXPECT_TRUE( |
| 1128 | matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
| 1129 | EXPECT_TRUE( |
| 1130 | notMatches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 1131 | EXPECT_TRUE( |
| 1132 | notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 1133 | } |
| 1134 | |
| 1135 | TEST(HasParameter, CallsInnerMatcher) { |
| 1136 | EXPECT_TRUE(matches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1137 | methodDecl(hasParameter(0, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1138 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1139 | methodDecl(hasParameter(0, hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 1143 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1144 | methodDecl(hasParameter(42, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 1148 | EXPECT_TRUE(matches("class X { void x(X x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1149 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1150 | EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1151 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1152 | EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1153 | methodDecl(hasParameter(0, |
| 1154 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1155 | EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1156 | methodDecl(hasParameter(0, |
| 1157 | hasType(references(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 1161 | 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] | 1162 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1163 | 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] | 1164 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1167 | TEST(Returns, MatchesReturnTypes) { |
| 1168 | EXPECT_TRUE(matches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1169 | functionDecl(returns(asString("int"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1170 | EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1171 | functionDecl(returns(asString("float"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1172 | EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1173 | functionDecl(returns(hasDeclaration( |
| 1174 | recordDecl(hasName("Y"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1177 | TEST(IsExternC, MatchesExternCFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1178 | EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC()))); |
| 1179 | EXPECT_TRUE(matches("extern \"C\" { void f() {} }", |
| 1180 | functionDecl(isExternC()))); |
| 1181 | EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC()))); |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1184 | TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 1185 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1186 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 1190 | EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1191 | methodDecl(hasAnyParameter(hasType(pointsTo( |
| 1192 | recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | TEST(HasName, MatchesParameterVariableDeclartions) { |
| 1196 | EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1197 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1198 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1199 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1202 | TEST(Matcher, MatchesClassTemplateSpecialization) { |
| 1203 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
| 1204 | "template<> struct A<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1205 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1206 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1207 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1208 | EXPECT_TRUE(notMatches("template<typename T> struct A {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1209 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | TEST(Matcher, MatchesTypeTemplateArgument) { |
| 1213 | EXPECT_TRUE(matches( |
| 1214 | "template<typename T> struct B {};" |
| 1215 | "B<int> b;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1216 | classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1217 | asString("int")))))); |
| 1218 | } |
| 1219 | |
| 1220 | TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { |
| 1221 | EXPECT_TRUE(matches( |
| 1222 | "struct B { int next; };" |
| 1223 | "template<int(B::*next_ptr)> struct A {};" |
| 1224 | "A<&B::next> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1225 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1226 | refersToDeclaration(fieldDecl(hasName("next"))))))); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | TEST(Matcher, MatchesSpecificArgument) { |
| 1230 | EXPECT_TRUE(matches( |
| 1231 | "template<typename T, typename U> class A {};" |
| 1232 | "A<bool, int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1233 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1234 | 1, refersToType(asString("int")))))); |
| 1235 | EXPECT_TRUE(notMatches( |
| 1236 | "template<typename T, typename U> class A {};" |
| 1237 | "A<int, bool> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1238 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1239 | 1, refersToType(asString("int")))))); |
| 1240 | } |
| 1241 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1242 | TEST(Matcher, ConstructorCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1243 | StatementMatcher Constructor = expr(constructExpr()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1244 | |
| 1245 | EXPECT_TRUE( |
| 1246 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 1247 | EXPECT_TRUE( |
| 1248 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 1249 | Constructor)); |
| 1250 | EXPECT_TRUE( |
| 1251 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1252 | Constructor)); |
| 1253 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 1254 | } |
| 1255 | |
| 1256 | TEST(Matcher, ConstructorArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1257 | StatementMatcher Constructor = expr(constructExpr( |
| 1258 | hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1259 | |
| 1260 | EXPECT_TRUE( |
| 1261 | matches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1262 | Constructor)); |
| 1263 | EXPECT_TRUE( |
| 1264 | matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 1265 | Constructor)); |
| 1266 | EXPECT_TRUE( |
| 1267 | matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 1268 | Constructor)); |
| 1269 | EXPECT_TRUE( |
| 1270 | notMatches("class X { public: X(int); }; void x() { int z; X x(z); }", |
| 1271 | Constructor)); |
| 1272 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1273 | StatementMatcher WrongIndex = expr(constructExpr( |
| 1274 | hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1275 | EXPECT_TRUE( |
| 1276 | notMatches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1277 | WrongIndex)); |
| 1278 | } |
| 1279 | |
| 1280 | TEST(Matcher, ConstructorArgumentCount) { |
| 1281 | StatementMatcher Constructor1Arg = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1282 | expr(constructExpr(argumentCountIs(1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1283 | |
| 1284 | EXPECT_TRUE( |
| 1285 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1286 | Constructor1Arg)); |
| 1287 | EXPECT_TRUE( |
| 1288 | matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 1289 | Constructor1Arg)); |
| 1290 | EXPECT_TRUE( |
| 1291 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1292 | Constructor1Arg)); |
| 1293 | EXPECT_TRUE( |
| 1294 | notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 1295 | Constructor1Arg)); |
| 1296 | } |
| 1297 | |
| 1298 | TEST(Matcher, BindTemporaryExpression) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1299 | StatementMatcher TempExpression = expr(bindTemporaryExpr()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1300 | |
| 1301 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 1302 | |
| 1303 | EXPECT_TRUE( |
| 1304 | matches(ClassString + |
| 1305 | "string GetStringByValue();" |
| 1306 | "void FunctionTakesString(string s);" |
| 1307 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1308 | TempExpression)); |
| 1309 | |
| 1310 | EXPECT_TRUE( |
| 1311 | notMatches(ClassString + |
| 1312 | "string* GetStringPointer(); " |
| 1313 | "void FunctionTakesStringPtr(string* s);" |
| 1314 | "void run() {" |
| 1315 | " string* s = GetStringPointer();" |
| 1316 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1317 | " FunctionTakesStringPtr(s);" |
| 1318 | "}", |
| 1319 | TempExpression)); |
| 1320 | |
| 1321 | EXPECT_TRUE( |
| 1322 | notMatches("class no_dtor {};" |
| 1323 | "no_dtor GetObjByValue();" |
| 1324 | "void ConsumeObj(no_dtor param);" |
| 1325 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 1326 | TempExpression)); |
| 1327 | } |
| 1328 | |
Sam Panzer | e16acd3 | 2012-08-24 22:04:44 +0000 | [diff] [blame] | 1329 | TEST(MaterializeTemporaryExpr, MatchesTemporary) { |
| 1330 | std::string ClassString = |
| 1331 | "class string { public: string(); int length(); }; "; |
| 1332 | |
| 1333 | EXPECT_TRUE( |
| 1334 | matches(ClassString + |
| 1335 | "string GetStringByValue();" |
| 1336 | "void FunctionTakesString(string s);" |
| 1337 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1338 | materializeTemporaryExpr())); |
| 1339 | |
| 1340 | EXPECT_TRUE( |
| 1341 | notMatches(ClassString + |
| 1342 | "string* GetStringPointer(); " |
| 1343 | "void FunctionTakesStringPtr(string* s);" |
| 1344 | "void run() {" |
| 1345 | " string* s = GetStringPointer();" |
| 1346 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1347 | " FunctionTakesStringPtr(s);" |
| 1348 | "}", |
| 1349 | materializeTemporaryExpr())); |
| 1350 | |
| 1351 | EXPECT_TRUE( |
| 1352 | notMatches(ClassString + |
| 1353 | "string GetStringByValue();" |
| 1354 | "void run() { int k = GetStringByValue().length(); }", |
| 1355 | materializeTemporaryExpr())); |
| 1356 | |
| 1357 | EXPECT_TRUE( |
| 1358 | notMatches(ClassString + |
| 1359 | "string GetStringByValue();" |
| 1360 | "void run() { GetStringByValue(); }", |
| 1361 | materializeTemporaryExpr())); |
| 1362 | } |
| 1363 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1364 | TEST(ConstructorDeclaration, SimpleCase) { |
| 1365 | EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1366 | constructorDecl(ofClass(hasName("Foo"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1367 | EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1368 | constructorDecl(ofClass(hasName("Bar"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
| 1371 | TEST(ConstructorDeclaration, IsImplicit) { |
| 1372 | // This one doesn't match because the constructor is not added by the |
| 1373 | // compiler (it is not needed). |
| 1374 | EXPECT_TRUE(notMatches("class Foo { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1375 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1376 | // The compiler added the implicit default constructor. |
| 1377 | EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1378 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1379 | EXPECT_TRUE(matches("class Foo { Foo(){} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1380 | constructorDecl(unless(isImplicit())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1383 | TEST(DestructorDeclaration, MatchesVirtualDestructor) { |
| 1384 | EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1385 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1389 | EXPECT_TRUE(notMatches("class Foo {};", |
| 1390 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1393 | TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1394 | EXPECT_TRUE(notMatches( |
| 1395 | "class Foo { Foo() { } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1396 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1397 | EXPECT_TRUE(matches( |
| 1398 | "class Foo {" |
| 1399 | " Foo() : foo_() { }" |
| 1400 | " int foo_;" |
| 1401 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1402 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | TEST(HasAnyConstructorInitializer, ForField) { |
| 1406 | static const char Code[] = |
| 1407 | "class Baz { };" |
| 1408 | "class Foo {" |
| 1409 | " Foo() : foo_() { }" |
| 1410 | " Baz foo_;" |
| 1411 | " Baz bar_;" |
| 1412 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1413 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1414 | forField(hasType(recordDecl(hasName("Baz")))))))); |
| 1415 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1416 | forField(hasName("foo_")))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1417 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1418 | forField(hasType(recordDecl(hasName("Bar")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1422 | static const char Code[] = |
| 1423 | "class Foo {" |
| 1424 | " Foo() : foo_(0) { }" |
| 1425 | " int foo_;" |
| 1426 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1427 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1428 | withInitializer(integerLiteral(equals(0))))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1429 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1430 | withInitializer(integerLiteral(equals(1))))))); |
| 1431 | } |
| 1432 | |
| 1433 | TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1434 | static const char Code[] = |
| 1435 | "struct Bar { Bar(){} };" |
| 1436 | "class Foo {" |
| 1437 | " Foo() : foo_() { }" |
| 1438 | " Bar foo_;" |
| 1439 | " Bar bar_;" |
| 1440 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1441 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1442 | allOf(forField(hasName("foo_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1443 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1444 | allOf(forField(hasName("bar_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1445 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1446 | allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1447 | } |
| 1448 | |
| 1449 | TEST(Matcher, NewExpression) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1450 | StatementMatcher New = expr(newExpr()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1451 | |
| 1452 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 1453 | EXPECT_TRUE( |
| 1454 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 1455 | EXPECT_TRUE( |
| 1456 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1457 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 1458 | } |
| 1459 | |
| 1460 | TEST(Matcher, NewExpressionArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1461 | StatementMatcher New = expr(constructExpr( |
| 1462 | hasArgument(0, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1463 | |
| 1464 | EXPECT_TRUE( |
| 1465 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1466 | New)); |
| 1467 | EXPECT_TRUE( |
| 1468 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1469 | New)); |
| 1470 | EXPECT_TRUE( |
| 1471 | notMatches("class X { public: X(int); }; void x() { int z; new X(z); }", |
| 1472 | New)); |
| 1473 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1474 | StatementMatcher WrongIndex = expr(constructExpr( |
| 1475 | hasArgument(42, declRefExpr(to(varDecl(hasName("y"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1476 | EXPECT_TRUE( |
| 1477 | notMatches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1478 | WrongIndex)); |
| 1479 | } |
| 1480 | |
| 1481 | TEST(Matcher, NewExpressionArgumentCount) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1482 | StatementMatcher New = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1483 | |
| 1484 | EXPECT_TRUE( |
| 1485 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1486 | EXPECT_TRUE( |
| 1487 | notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }", |
| 1488 | New)); |
| 1489 | } |
| 1490 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1491 | TEST(Matcher, DeleteExpression) { |
| 1492 | EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1493 | deleteExpr())); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1496 | TEST(Matcher, DefaultArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1497 | StatementMatcher Arg = defaultArgExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1498 | |
| 1499 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 1500 | EXPECT_TRUE( |
| 1501 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 1502 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 1503 | } |
| 1504 | |
| 1505 | TEST(Matcher, StringLiterals) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1506 | StatementMatcher Literal = expr(stringLiteral()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1507 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 1508 | // wide string |
| 1509 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 1510 | // with escaped characters |
| 1511 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 1512 | // no matching -- though the data type is the same, there is no string literal |
| 1513 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 1514 | } |
| 1515 | |
| 1516 | TEST(Matcher, CharacterLiterals) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1517 | StatementMatcher CharLiteral = expr(characterLiteral()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1518 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 1519 | // wide character |
| 1520 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 1521 | // wide character, Hex encoded, NOT MATCHED! |
| 1522 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 1523 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 1524 | } |
| 1525 | |
| 1526 | TEST(Matcher, IntegerLiterals) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1527 | StatementMatcher HasIntLiteral = expr(integerLiteral()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1528 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 1529 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 1530 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 1531 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 1532 | |
| 1533 | // Non-matching cases (character literals, float and double) |
| 1534 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 1535 | HasIntLiteral)); // this is actually a character |
| 1536 | // literal cast to int |
| 1537 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 1538 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 1539 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 1540 | } |
| 1541 | |
| 1542 | TEST(Matcher, Conditions) { |
| 1543 | StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true)))); |
| 1544 | |
| 1545 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 1546 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 1547 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 1548 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 1549 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 1550 | } |
| 1551 | |
| 1552 | TEST(MatchBinaryOperator, HasOperatorName) { |
| 1553 | StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1554 | |
| 1555 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1556 | EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1557 | } |
| 1558 | |
| 1559 | TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1560 | StatementMatcher OperatorTrueFalse = |
| 1561 | binaryOperator(hasLHS(boolLiteral(equals(true))), |
| 1562 | hasRHS(boolLiteral(equals(false)))); |
| 1563 | |
| 1564 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1565 | EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1566 | EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1567 | } |
| 1568 | |
| 1569 | TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1570 | StatementMatcher HasOperand = |
| 1571 | binaryOperator(hasEitherOperand(boolLiteral(equals(false)))); |
| 1572 | |
| 1573 | EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 1574 | EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 1575 | EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 1576 | } |
| 1577 | |
| 1578 | TEST(Matcher, BinaryOperatorTypes) { |
| 1579 | // Integration test that verifies the AST provides all binary operators in |
| 1580 | // a way we expect. |
| 1581 | // FIXME: Operator ',' |
| 1582 | EXPECT_TRUE( |
| 1583 | matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 1584 | EXPECT_TRUE( |
| 1585 | matches("bool b; bool c = (b = true);", |
| 1586 | binaryOperator(hasOperatorName("=")))); |
| 1587 | EXPECT_TRUE( |
| 1588 | matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 1589 | EXPECT_TRUE( |
| 1590 | matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 1591 | EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 1592 | EXPECT_TRUE( |
| 1593 | matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 1594 | EXPECT_TRUE( |
| 1595 | matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 1596 | EXPECT_TRUE( |
| 1597 | matches("int i = 1; int j = (i <<= 2);", |
| 1598 | binaryOperator(hasOperatorName("<<=")))); |
| 1599 | EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 1600 | EXPECT_TRUE( |
| 1601 | matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 1602 | EXPECT_TRUE( |
| 1603 | matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 1604 | EXPECT_TRUE( |
| 1605 | matches("int i = 1; int j = (i >>= 2);", |
| 1606 | binaryOperator(hasOperatorName(">>=")))); |
| 1607 | EXPECT_TRUE( |
| 1608 | matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 1609 | EXPECT_TRUE( |
| 1610 | matches("int i = 42; int j = (i ^= 42);", |
| 1611 | binaryOperator(hasOperatorName("^=")))); |
| 1612 | EXPECT_TRUE( |
| 1613 | matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 1614 | EXPECT_TRUE( |
| 1615 | matches("int i = 42; int j = (i %= 42);", |
| 1616 | binaryOperator(hasOperatorName("%=")))); |
| 1617 | EXPECT_TRUE( |
| 1618 | matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 1619 | EXPECT_TRUE( |
| 1620 | matches("bool b = true && false;", |
| 1621 | binaryOperator(hasOperatorName("&&")))); |
| 1622 | EXPECT_TRUE( |
| 1623 | matches("bool b = true; bool c = (b &= false);", |
| 1624 | binaryOperator(hasOperatorName("&=")))); |
| 1625 | EXPECT_TRUE( |
| 1626 | matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 1627 | EXPECT_TRUE( |
| 1628 | matches("bool b = true || false;", |
| 1629 | binaryOperator(hasOperatorName("||")))); |
| 1630 | EXPECT_TRUE( |
| 1631 | matches("bool b = true; bool c = (b |= false);", |
| 1632 | binaryOperator(hasOperatorName("|=")))); |
| 1633 | EXPECT_TRUE( |
| 1634 | matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 1635 | EXPECT_TRUE( |
| 1636 | matches("int i = 42; int j = (i *= 23);", |
| 1637 | binaryOperator(hasOperatorName("*=")))); |
| 1638 | EXPECT_TRUE( |
| 1639 | matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 1640 | EXPECT_TRUE( |
| 1641 | matches("int i = 42; int j = (i /= 23);", |
| 1642 | binaryOperator(hasOperatorName("/=")))); |
| 1643 | EXPECT_TRUE( |
| 1644 | matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 1645 | EXPECT_TRUE( |
| 1646 | matches("int i = 42; int j = (i += 23);", |
| 1647 | binaryOperator(hasOperatorName("+=")))); |
| 1648 | EXPECT_TRUE( |
| 1649 | matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 1650 | EXPECT_TRUE( |
| 1651 | matches("int i = 42; int j = (i -= 23);", |
| 1652 | binaryOperator(hasOperatorName("-=")))); |
| 1653 | EXPECT_TRUE( |
| 1654 | matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 1655 | binaryOperator(hasOperatorName("->*")))); |
| 1656 | EXPECT_TRUE( |
| 1657 | matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 1658 | binaryOperator(hasOperatorName(".*")))); |
| 1659 | |
| 1660 | // Member expressions as operators are not supported in matches. |
| 1661 | EXPECT_TRUE( |
| 1662 | notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 1663 | binaryOperator(hasOperatorName("->")))); |
| 1664 | |
| 1665 | // Initializer assignments are not represented as operator equals. |
| 1666 | EXPECT_TRUE( |
| 1667 | notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 1668 | |
| 1669 | // Array indexing is not represented as operator. |
| 1670 | EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 1671 | |
| 1672 | // Overloaded operators do not match at all. |
| 1673 | EXPECT_TRUE(notMatches( |
| 1674 | "struct A { bool operator&&(const A &a) const { return false; } };" |
| 1675 | "void x() { A a, b; a && b; }", |
| 1676 | binaryOperator())); |
| 1677 | } |
| 1678 | |
| 1679 | TEST(MatchUnaryOperator, HasOperatorName) { |
| 1680 | StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 1681 | |
| 1682 | EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 1683 | EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 1684 | } |
| 1685 | |
| 1686 | TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 1687 | StatementMatcher OperatorOnFalse = |
| 1688 | unaryOperator(hasUnaryOperand(boolLiteral(equals(false)))); |
| 1689 | |
| 1690 | EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 1691 | EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 1692 | } |
| 1693 | |
| 1694 | TEST(Matcher, UnaryOperatorTypes) { |
| 1695 | // Integration test that verifies the AST provides all unary operators in |
| 1696 | // a way we expect. |
| 1697 | EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 1698 | EXPECT_TRUE( |
| 1699 | matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 1700 | EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 1701 | EXPECT_TRUE( |
| 1702 | matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 1703 | EXPECT_TRUE( |
| 1704 | matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 1705 | EXPECT_TRUE( |
| 1706 | matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 1707 | EXPECT_TRUE( |
| 1708 | matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 1709 | EXPECT_TRUE( |
| 1710 | matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 1711 | EXPECT_TRUE( |
| 1712 | matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 1713 | EXPECT_TRUE( |
| 1714 | matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 1715 | |
| 1716 | // We don't match conversion operators. |
| 1717 | EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 1718 | |
| 1719 | // Function calls are not represented as operator. |
| 1720 | EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 1721 | |
| 1722 | // Overloaded operators do not match at all. |
| 1723 | // FIXME: We probably want to add that. |
| 1724 | EXPECT_TRUE(notMatches( |
| 1725 | "struct A { bool operator!() const { return false; } };" |
| 1726 | "void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 1727 | } |
| 1728 | |
| 1729 | TEST(Matcher, ConditionalOperator) { |
| 1730 | StatementMatcher Conditional = conditionalOperator( |
| 1731 | hasCondition(boolLiteral(equals(true))), |
| 1732 | hasTrueExpression(boolLiteral(equals(false)))); |
| 1733 | |
| 1734 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 1735 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 1736 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 1737 | |
| 1738 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 1739 | hasFalseExpression(boolLiteral(equals(false)))); |
| 1740 | |
| 1741 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 1742 | EXPECT_TRUE( |
| 1743 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 1744 | } |
| 1745 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1746 | TEST(ArraySubscriptMatchers, ArraySubscripts) { |
| 1747 | EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }", |
| 1748 | arraySubscriptExpr())); |
| 1749 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }", |
| 1750 | arraySubscriptExpr())); |
| 1751 | } |
| 1752 | |
| 1753 | TEST(ArraySubscriptMatchers, ArrayIndex) { |
| 1754 | EXPECT_TRUE(matches( |
| 1755 | "int i[2]; void f() { i[1] = 1; }", |
| 1756 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1757 | EXPECT_TRUE(matches( |
| 1758 | "int i[2]; void f() { 1[i] = 1; }", |
| 1759 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 1760 | EXPECT_TRUE(notMatches( |
| 1761 | "int i[2]; void f() { i[1] = 1; }", |
| 1762 | arraySubscriptExpr(hasIndex(integerLiteral(equals(0)))))); |
| 1763 | } |
| 1764 | |
| 1765 | TEST(ArraySubscriptMatchers, MatchesArrayBase) { |
| 1766 | EXPECT_TRUE(matches( |
| 1767 | "int i[2]; void f() { i[1] = 2; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1768 | arraySubscriptExpr(hasBase(implicitCastExpr( |
| 1769 | hasSourceExpression(declRefExpr())))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1772 | TEST(Matcher, HasNameSupportsNamespaces) { |
| 1773 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1774 | recordDecl(hasName("a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1775 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1776 | recordDecl(hasName("::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1777 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1778 | recordDecl(hasName("b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1779 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1780 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1781 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1782 | recordDecl(hasName("c::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1783 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1784 | recordDecl(hasName("a::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1785 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1786 | recordDecl(hasName("a::b::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1787 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1788 | recordDecl(hasName("::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1789 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1790 | recordDecl(hasName("::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1791 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1792 | recordDecl(hasName("z::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1793 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1794 | recordDecl(hasName("a+b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1795 | EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1796 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | TEST(Matcher, HasNameSupportsOuterClasses) { |
| 1800 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1801 | matches("class A { class B { class C; }; };", |
| 1802 | recordDecl(hasName("A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1803 | EXPECT_TRUE( |
| 1804 | matches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1805 | recordDecl(hasName("::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1806 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1807 | matches("class A { class B { class C; }; };", |
| 1808 | recordDecl(hasName("B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1809 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1810 | matches("class A { class B { class C; }; };", |
| 1811 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1812 | EXPECT_TRUE( |
| 1813 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1814 | recordDecl(hasName("c::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1815 | EXPECT_TRUE( |
| 1816 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1817 | recordDecl(hasName("A::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1818 | EXPECT_TRUE( |
| 1819 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1820 | recordDecl(hasName("A::B::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1821 | EXPECT_TRUE( |
| 1822 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1823 | recordDecl(hasName("::C")))); |
| 1824 | EXPECT_TRUE( |
| 1825 | notMatches("class A { class B { class C; }; };", |
| 1826 | recordDecl(hasName("::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1827 | EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1828 | recordDecl(hasName("z::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1829 | EXPECT_TRUE( |
| 1830 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1831 | recordDecl(hasName("A+B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | TEST(Matcher, IsDefinition) { |
| 1835 | DeclarationMatcher DefinitionOfClassA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1836 | recordDecl(hasName("A"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1837 | EXPECT_TRUE(matches("class A {};", DefinitionOfClassA)); |
| 1838 | EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA)); |
| 1839 | |
| 1840 | DeclarationMatcher DefinitionOfVariableA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1841 | varDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1842 | EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 1843 | EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 1844 | |
| 1845 | DeclarationMatcher DefinitionOfMethodA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1846 | methodDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1847 | EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 1848 | EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 1849 | } |
| 1850 | |
| 1851 | TEST(Matcher, OfClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1852 | StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1853 | ofClass(hasName("X"))))); |
| 1854 | |
| 1855 | EXPECT_TRUE( |
| 1856 | matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 1857 | EXPECT_TRUE( |
| 1858 | matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 1859 | Constructor)); |
| 1860 | EXPECT_TRUE( |
| 1861 | notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 1862 | Constructor)); |
| 1863 | } |
| 1864 | |
| 1865 | TEST(Matcher, VisitsTemplateInstantiations) { |
| 1866 | EXPECT_TRUE(matches( |
| 1867 | "class A { public: void x(); };" |
| 1868 | "template <typename T> class B { public: void y() { T t; t.x(); } };" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1869 | "void f() { B<A> b; b.y(); }", |
| 1870 | callExpr(callee(methodDecl(hasName("x")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1871 | |
| 1872 | EXPECT_TRUE(matches( |
| 1873 | "class A { public: void x(); };" |
| 1874 | "class C {" |
| 1875 | " public:" |
| 1876 | " template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 1877 | "};" |
| 1878 | "void f() {" |
| 1879 | " C::B<A> b; b.y();" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1880 | "}", |
| 1881 | recordDecl(hasName("C"), |
| 1882 | hasDescendant(callExpr(callee(methodDecl(hasName("x")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1885 | TEST(Matcher, HandlesNullQualTypes) { |
| 1886 | // FIXME: Add a Type matcher so we can replace uses of this |
| 1887 | // variable with Type(True()) |
| 1888 | const TypeMatcher AnyType = anything(); |
| 1889 | |
| 1890 | // We don't really care whether this matcher succeeds; we're testing that |
| 1891 | // it completes without crashing. |
| 1892 | EXPECT_TRUE(matches( |
| 1893 | "struct A { };" |
| 1894 | "template <typename T>" |
| 1895 | "void f(T t) {" |
| 1896 | " T local_t(t /* this becomes a null QualType in the AST */);" |
| 1897 | "}" |
| 1898 | "void g() {" |
| 1899 | " f(0);" |
| 1900 | "}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1901 | expr(hasType(TypeMatcher( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1902 | anyOf( |
| 1903 | TypeMatcher(hasDeclaration(anything())), |
| 1904 | pointsTo(AnyType), |
| 1905 | references(AnyType) |
| 1906 | // Other QualType matchers should go here. |
| 1907 | )))))); |
| 1908 | } |
| 1909 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1910 | // For testing AST_MATCHER_P(). |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1911 | AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1912 | // Make sure all special variables are used: node, match_finder, |
| 1913 | // bound_nodes_builder, and the parameter named 'AMatcher'. |
| 1914 | return AMatcher.matches(Node, Finder, Builder); |
| 1915 | } |
| 1916 | |
| 1917 | TEST(AstMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1918 | DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1919 | |
| 1920 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1921 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1922 | |
| 1923 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1924 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1925 | |
| 1926 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1927 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | AST_POLYMORPHIC_MATCHER_P( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1931 | polymorphicHas, internal::Matcher<Decl>, AMatcher) { |
| 1932 | TOOLING_COMPILE_ASSERT((llvm::is_same<NodeType, Decl>::value) || |
| 1933 | (llvm::is_same<NodeType, Stmt>::value), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1934 | assert_node_type_is_accessible); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1935 | return Finder->matchesChildOf( |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 1936 | Node, AMatcher, Builder, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1937 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
| 1938 | ASTMatchFinder::BK_First); |
| 1939 | } |
| 1940 | |
| 1941 | TEST(AstPolymorphicMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1942 | DeclarationMatcher HasClassB = |
| 1943 | polymorphicHas(recordDecl(hasName("B")).bind("b")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1944 | |
| 1945 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1946 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1947 | |
| 1948 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1949 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1950 | |
| 1951 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1952 | HasClassB, new VerifyIdIsBoundToDecl<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1953 | |
| 1954 | StatementMatcher StatementHasClassB = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1955 | polymorphicHas(recordDecl(hasName("B"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1956 | |
| 1957 | EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB)); |
| 1958 | } |
| 1959 | |
| 1960 | TEST(For, FindsForLoops) { |
| 1961 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 1962 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
| 1963 | } |
| 1964 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1965 | TEST(For, ForLoopInternals) { |
| 1966 | EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", |
| 1967 | forStmt(hasCondition(anything())))); |
| 1968 | EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }", |
| 1969 | forStmt(hasLoopInit(anything())))); |
| 1970 | } |
| 1971 | |
| 1972 | TEST(For, NegativeForLoopInternals) { |
| 1973 | EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1974 | forStmt(hasCondition(expr())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1975 | EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }", |
| 1976 | forStmt(hasLoopInit(anything())))); |
| 1977 | } |
| 1978 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1979 | TEST(For, ReportsNoFalsePositives) { |
| 1980 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 1981 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 1982 | } |
| 1983 | |
| 1984 | TEST(CompoundStatement, HandlesSimpleCases) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1985 | EXPECT_TRUE(notMatches("void f();", compoundStmt())); |
| 1986 | EXPECT_TRUE(matches("void f() {}", compoundStmt())); |
| 1987 | EXPECT_TRUE(matches("void f() {{}}", compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
| 1990 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 1991 | // It's not a compound statement just because there's "{}" in the source |
| 1992 | // text. This is an AST search, not grep. |
| 1993 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1994 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1995 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1996 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1999 | TEST(HasBody, FindsBodyOfForWhileDoLoops) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2000 | EXPECT_TRUE(matches("void f() { for(;;) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2001 | forStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2002 | EXPECT_TRUE(notMatches("void f() { for(;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2003 | forStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2004 | EXPECT_TRUE(matches("void f() { while(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2005 | whileStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2006 | EXPECT_TRUE(matches("void f() { do {} while(true); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2007 | doStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 2011 | // The simplest case: every compound statement is in a function |
| 2012 | // definition, and the function body itself must be a compound |
| 2013 | // statement. |
| 2014 | EXPECT_TRUE(matches("void f() { for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2015 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | TEST(HasAnySubstatement, IsNotRecursive) { |
| 2019 | // It's really "has any immediate substatement". |
| 2020 | EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2021 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 2025 | EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2026 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 2030 | EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2031 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) { |
| 2035 | EXPECT_TRUE(matches("void f() { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2036 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2037 | EXPECT_TRUE(notMatches("void f() {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2038 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
| 2041 | TEST(StatementCountIs, AppearsToMatchOnlyOneCount) { |
| 2042 | EXPECT_TRUE(matches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2043 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2044 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2045 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2046 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2047 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | TEST(StatementCountIs, WorksWithMultipleStatements) { |
| 2051 | EXPECT_TRUE(matches("void f() { 1; 2; 3; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2052 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | TEST(StatementCountIs, WorksWithNestedCompoundStatements) { |
| 2056 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2057 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2058 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2059 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2060 | EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2061 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2062 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2063 | compoundStmt(statementCountIs(4)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | TEST(Member, WorksInSimplestCase) { |
| 2067 | EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2068 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | TEST(Member, DoesNotMatchTheBaseExpression) { |
| 2072 | // Don't pick out the wrong part of the member expression, this should |
| 2073 | // be checking the member (name) only. |
| 2074 | EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2075 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | TEST(Member, MatchesInMemberFunctionCall) { |
| 2079 | EXPECT_TRUE(matches("void f() {" |
| 2080 | " struct { void first() {}; } s;" |
| 2081 | " s.first();" |
| 2082 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2083 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2086 | TEST(Member, MatchesMemberAllocationFunction) { |
Dmitri Gribenko | 02ed37f | 2012-08-18 00:41:04 +0000 | [diff] [blame] | 2087 | EXPECT_TRUE(matches("namespace std { typedef typeof(sizeof(int)) size_t; }" |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2088 | "class X { void *operator new(std::size_t); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2089 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2090 | |
| 2091 | EXPECT_TRUE(matches("class X { void operator delete(void*); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2092 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2093 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2094 | EXPECT_TRUE(matches( |
| 2095 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2096 | "class X { void operator delete[](void*, std::size_t); };", |
| 2097 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2100 | TEST(HasObjectExpression, DoesNotMatchMember) { |
| 2101 | EXPECT_TRUE(notMatches( |
| 2102 | "class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2103 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | TEST(HasObjectExpression, MatchesBaseOfVariable) { |
| 2107 | EXPECT_TRUE(matches( |
| 2108 | "struct X { int m; }; void f(X x) { x.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2109 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2110 | EXPECT_TRUE(matches( |
| 2111 | "struct X { int m; }; void f(X* x) { x->m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2112 | memberExpr(hasObjectExpression( |
| 2113 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | TEST(HasObjectExpression, |
| 2117 | MatchesObjectExpressionOfImplicitlyFormedMemberExpression) { |
| 2118 | EXPECT_TRUE(matches( |
| 2119 | "class X {}; struct S { X m; void f() { this->m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2120 | memberExpr(hasObjectExpression( |
| 2121 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2122 | EXPECT_TRUE(matches( |
| 2123 | "class X {}; struct S { X m; void f() { m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2124 | memberExpr(hasObjectExpression( |
| 2125 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | TEST(Field, DoesNotMatchNonFieldMembers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2129 | EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m")))); |
| 2130 | EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m")))); |
| 2131 | EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m")))); |
| 2132 | EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | TEST(Field, MatchesField) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2136 | EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | TEST(IsConstQualified, MatchesConstInt) { |
| 2140 | EXPECT_TRUE(matches("const int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2141 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | TEST(IsConstQualified, MatchesConstPointer) { |
| 2145 | EXPECT_TRUE(matches("int i = 42; int* const p(&i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2146 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | TEST(IsConstQualified, MatchesThroughTypedef) { |
| 2150 | EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2151 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2152 | EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2153 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | TEST(IsConstQualified, DoesNotMatchInappropriately) { |
| 2157 | EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2158 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2159 | EXPECT_TRUE(notMatches("int const* p;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2160 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2163 | TEST(CastExpression, MatchesExplicitCasts) { |
| 2164 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2165 | expr(castExpr()))); |
| 2166 | EXPECT_TRUE(matches("void *p = (void *)(&p);", expr(castExpr()))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2167 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2168 | expr(castExpr()))); |
| 2169 | EXPECT_TRUE(matches("char c = char(0);", expr(castExpr()))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2170 | } |
| 2171 | TEST(CastExpression, MatchesImplicitCasts) { |
| 2172 | // This test creates an implicit cast from int to char. |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2173 | EXPECT_TRUE(matches("char c = 0;", expr(castExpr()))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2174 | // This test creates an implicit cast from lvalue to rvalue. |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2175 | EXPECT_TRUE(matches("char c = 0, d = c;", expr(castExpr()))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | TEST(CastExpression, DoesNotMatchNonCasts) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2179 | EXPECT_TRUE(notMatches("char c = '0';", expr(castExpr()))); |
| 2180 | EXPECT_TRUE(notMatches("char c, &q = c;", expr(castExpr()))); |
| 2181 | EXPECT_TRUE(notMatches("int i = (0);", expr(castExpr()))); |
| 2182 | EXPECT_TRUE(notMatches("int i = 0;", expr(castExpr()))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2185 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 2186 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2187 | expr(reinterpretCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
| 2191 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2192 | expr(reinterpretCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2193 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2194 | expr(reinterpretCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2195 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2196 | expr(reinterpretCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2197 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2198 | "B b;" |
| 2199 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2200 | expr(reinterpretCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | TEST(FunctionalCast, MatchesSimpleCase) { |
| 2204 | std::string foo_class = "class Foo { public: Foo(char*); };"; |
| 2205 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2206 | expr(functionalCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
| 2209 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
| 2210 | std::string FooClass = "class Foo { public: Foo(char*); };"; |
| 2211 | EXPECT_TRUE( |
| 2212 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2213 | expr(functionalCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2214 | EXPECT_TRUE( |
| 2215 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2216 | expr(functionalCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | TEST(DynamicCast, MatchesSimpleCase) { |
| 2220 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2221 | "B b;" |
| 2222 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2223 | expr(dynamicCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | TEST(StaticCast, MatchesSimpleCase) { |
| 2227 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2228 | expr(staticCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
| 2231 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
| 2232 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2233 | expr(staticCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2234 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2235 | expr(staticCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2236 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2237 | expr(staticCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2238 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2239 | "B b;" |
| 2240 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2241 | expr(staticCastExpr()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
| 2244 | TEST(HasDestinationType, MatchesSimpleCase) { |
| 2245 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2246 | expr(staticCastExpr(hasDestinationType( |
| 2247 | pointsTo(TypeMatcher(anything()))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2250 | TEST(HasImplicitDestinationType, MatchesSimpleCase) { |
| 2251 | // This test creates an implicit const cast. |
| 2252 | EXPECT_TRUE(matches("int x; const int i = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2253 | expr(implicitCastExpr( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2254 | hasImplicitDestinationType(isInteger()))))); |
| 2255 | // This test creates an implicit array-to-pointer cast. |
| 2256 | EXPECT_TRUE(matches("int arr[3]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2257 | expr(implicitCastExpr(hasImplicitDestinationType( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2258 | pointsTo(TypeMatcher(anything()))))))); |
| 2259 | } |
| 2260 | |
| 2261 | TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) { |
| 2262 | // This test creates an implicit cast from int to char. |
| 2263 | EXPECT_TRUE(notMatches("char c = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2264 | expr(implicitCastExpr(hasImplicitDestinationType( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2265 | unless(anything())))))); |
| 2266 | // This test creates an implicit array-to-pointer cast. |
| 2267 | EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2268 | expr(implicitCastExpr(hasImplicitDestinationType( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2269 | unless(anything())))))); |
| 2270 | } |
| 2271 | |
| 2272 | TEST(ImplicitCast, MatchesSimpleCase) { |
| 2273 | // This test creates an implicit const cast. |
| 2274 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2275 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2276 | // This test creates an implicit cast from int to char. |
| 2277 | EXPECT_TRUE(matches("char c = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2278 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2279 | // This test creates an implicit array-to-pointer cast. |
| 2280 | EXPECT_TRUE(matches("int arr[6]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2281 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | TEST(ImplicitCast, DoesNotMatchIncorrectly) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2285 | // This test verifies that implicitCastExpr() matches exactly when implicit casts |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2286 | // are present, and that it ignores explicit and paren casts. |
| 2287 | |
| 2288 | // These two test cases have no casts. |
| 2289 | EXPECT_TRUE(notMatches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2290 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2291 | EXPECT_TRUE(notMatches("int x = 0, &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2292 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2293 | |
| 2294 | EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2295 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2296 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2297 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2298 | |
| 2299 | EXPECT_TRUE(notMatches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2300 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | TEST(IgnoringImpCasts, MatchesImpCasts) { |
| 2304 | // This test checks that ignoringImpCasts matches when implicit casts are |
| 2305 | // present and its inner matcher alone does not match. |
| 2306 | // Note that this test creates an implicit const cast. |
| 2307 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2308 | varDecl(hasInitializer(ignoringImpCasts( |
| 2309 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2310 | // This test creates an implict cast from int to char. |
| 2311 | EXPECT_TRUE(matches("char x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2312 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2313 | integerLiteral(equals(0))))))); |
| 2314 | } |
| 2315 | |
| 2316 | TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) { |
| 2317 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2318 | // matcher does not match. |
| 2319 | // Note that the first test creates an implicit const cast. |
| 2320 | EXPECT_TRUE(notMatches("int x; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2321 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2322 | unless(anything())))))); |
| 2323 | EXPECT_TRUE(notMatches("int x; int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2324 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2325 | unless(anything())))))); |
| 2326 | |
| 2327 | // These tests verify that ignoringImplictCasts does not look through explicit |
| 2328 | // casts or parentheses. |
| 2329 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2330 | varDecl(hasInitializer(ignoringImpCasts( |
| 2331 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2332 | EXPECT_TRUE(notMatches("int i = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2333 | varDecl(hasInitializer(ignoringImpCasts( |
| 2334 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2335 | EXPECT_TRUE(notMatches("float i = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2336 | varDecl(hasInitializer(ignoringImpCasts( |
| 2337 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2338 | EXPECT_TRUE(notMatches("float i = float(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2339 | varDecl(hasInitializer(ignoringImpCasts( |
| 2340 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | TEST(IgnoringImpCasts, MatchesWithoutImpCasts) { |
| 2344 | // This test verifies that expressions that do not have implicit casts |
| 2345 | // still match the inner matcher. |
| 2346 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2347 | varDecl(hasInitializer(ignoringImpCasts( |
| 2348 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2349 | } |
| 2350 | |
| 2351 | TEST(IgnoringParenCasts, MatchesParenCasts) { |
| 2352 | // This test checks that ignoringParenCasts matches when parentheses and/or |
| 2353 | // casts are present and its inner matcher alone does not match. |
| 2354 | EXPECT_TRUE(matches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2355 | varDecl(hasInitializer(ignoringParenCasts( |
| 2356 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2357 | EXPECT_TRUE(matches("int x = (((((0)))));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2358 | varDecl(hasInitializer(ignoringParenCasts( |
| 2359 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2360 | |
| 2361 | // This test creates an implict cast from int to char in addition to the |
| 2362 | // parentheses. |
| 2363 | EXPECT_TRUE(matches("char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2364 | varDecl(hasInitializer(ignoringParenCasts( |
| 2365 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2366 | |
| 2367 | EXPECT_TRUE(matches("char x = (char)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2368 | varDecl(hasInitializer(ignoringParenCasts( |
| 2369 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2370 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2371 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2372 | integerLiteral(equals(0))))))); |
| 2373 | } |
| 2374 | |
| 2375 | TEST(IgnoringParenCasts, MatchesWithoutParenCasts) { |
| 2376 | // This test verifies that expressions that do not have any casts still match. |
| 2377 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2378 | varDecl(hasInitializer(ignoringParenCasts( |
| 2379 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) { |
| 2383 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2384 | // matcher does not match. |
| 2385 | EXPECT_TRUE(notMatches("int x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2386 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2387 | unless(anything())))))); |
| 2388 | |
| 2389 | // This test creates an implicit cast from int to char in addition to the |
| 2390 | // parentheses. |
| 2391 | EXPECT_TRUE(notMatches("char x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2392 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2393 | unless(anything())))))); |
| 2394 | |
| 2395 | EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2396 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2397 | unless(anything())))))); |
| 2398 | } |
| 2399 | |
| 2400 | TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) { |
| 2401 | // This test checks that ignoringParenAndImpCasts matches when |
| 2402 | // parentheses and/or implicit casts are present and its inner matcher alone |
| 2403 | // does not match. |
| 2404 | // Note that this test creates an implicit const cast. |
| 2405 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2406 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2407 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2408 | // This test creates an implicit cast from int to char. |
| 2409 | EXPECT_TRUE(matches("const char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2410 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2411 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) { |
| 2415 | // This test verifies that expressions that do not have parentheses or |
| 2416 | // implicit casts still match. |
| 2417 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2418 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2419 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2420 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2421 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2422 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) { |
| 2426 | // These tests verify that ignoringParenImpCasts does not match if |
| 2427 | // the inner matcher does not match. |
| 2428 | // This test creates an implicit cast. |
| 2429 | EXPECT_TRUE(notMatches("char c = ((3));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2430 | varDecl(hasInitializer(ignoringParenImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2431 | unless(anything())))))); |
| 2432 | // These tests verify that ignoringParenAndImplictCasts does not look |
| 2433 | // through explicit casts. |
| 2434 | EXPECT_TRUE(notMatches("float y = (float(0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2435 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2436 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2437 | EXPECT_TRUE(notMatches("float y = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2438 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2439 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2440 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2441 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2442 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2445 | TEST(HasSourceExpression, MatchesImplicitCasts) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2446 | EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 2447 | "void r() {string a_string; URL url = a_string; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2448 | expr(implicitCastExpr( |
| 2449 | hasSourceExpression(constructExpr()))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2452 | TEST(HasSourceExpression, MatchesExplicitCasts) { |
| 2453 | EXPECT_TRUE(matches("float x = static_cast<float>(42);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2454 | expr(explicitCastExpr( |
| 2455 | hasSourceExpression(hasDescendant( |
| 2456 | expr(integerLiteral()))))))); |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2459 | TEST(Statement, DoesNotMatchDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2460 | EXPECT_TRUE(notMatches("class X {};", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
| 2463 | TEST(Statement, MatchesCompoundStatments) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2464 | EXPECT_TRUE(matches("void x() {}", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2468 | EXPECT_TRUE(notMatches("void x() {}", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2472 | EXPECT_TRUE(matches("void x() { int a; }", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2475 | TEST(InitListExpression, MatchesInitListExpression) { |
| 2476 | EXPECT_TRUE(matches("int a[] = { 1, 2 };", |
| 2477 | initListExpr(hasType(asString("int [2]"))))); |
| 2478 | EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2479 | initListExpr(hasType(recordDecl(hasName("B")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
| 2482 | TEST(UsingDeclaration, MatchesUsingDeclarations) { |
| 2483 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;", |
| 2484 | usingDecl())); |
| 2485 | } |
| 2486 | |
| 2487 | TEST(UsingDeclaration, MatchesShadowUsingDelcarations) { |
| 2488 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;", |
| 2489 | usingDecl(hasAnyUsingShadowDecl(hasName("a"))))); |
| 2490 | } |
| 2491 | |
| 2492 | TEST(UsingDeclaration, MatchesSpecificTarget) { |
| 2493 | EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;", |
| 2494 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2495 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2496 | EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;", |
| 2497 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2498 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | TEST(UsingDeclaration, ThroughUsingDeclaration) { |
| 2502 | EXPECT_TRUE(matches( |
| 2503 | "namespace a { void f(); } using a::f; void g() { f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2504 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2505 | EXPECT_TRUE(notMatches( |
| 2506 | "namespace a { void f(); } using a::f; void g() { a::f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2507 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2510 | TEST(SingleDecl, IsSingleDecl) { |
| 2511 | StatementMatcher SingleDeclStmt = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2512 | declStmt(hasSingleDecl(varDecl(hasInitializer(anything())))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2513 | EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt)); |
| 2514 | EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt)); |
| 2515 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 2516 | SingleDeclStmt)); |
| 2517 | } |
| 2518 | |
| 2519 | TEST(DeclStmt, ContainsDeclaration) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2520 | DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything())); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2521 | |
| 2522 | EXPECT_TRUE(matches("void f() {int a = 4;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2523 | declStmt(containsDeclaration(0, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2524 | EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2525 | declStmt(containsDeclaration(0, MatchesInit), |
| 2526 | containsDeclaration(1, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2527 | unsigned WrongIndex = 42; |
| 2528 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2529 | declStmt(containsDeclaration(WrongIndex, |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2530 | MatchesInit)))); |
| 2531 | } |
| 2532 | |
| 2533 | TEST(DeclCount, DeclCountIsCorrect) { |
| 2534 | EXPECT_TRUE(matches("void f() {int i,j;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2535 | declStmt(declCountIs(2)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2536 | EXPECT_TRUE(notMatches("void f() {int i,j; int k;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2537 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2538 | EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2539 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2542 | TEST(While, MatchesWhileLoops) { |
| 2543 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 2544 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 2545 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 2546 | } |
| 2547 | |
| 2548 | TEST(Do, MatchesDoLoops) { |
| 2549 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 2550 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 2551 | } |
| 2552 | |
| 2553 | TEST(Do, DoesNotMatchWhileLoops) { |
| 2554 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 2555 | } |
| 2556 | |
| 2557 | TEST(SwitchCase, MatchesCase) { |
| 2558 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 2559 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 2560 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 2561 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 2562 | } |
| 2563 | |
| 2564 | TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 2565 | EXPECT_TRUE(notMatches( |
| 2566 | "void x() { if(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2567 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2568 | EXPECT_TRUE(notMatches( |
| 2569 | "void x() { int x; if((x = 42)) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2570 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 2574 | EXPECT_TRUE(matches( |
| 2575 | "void x() { if(int* a = 0) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2576 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | TEST(ForEach, BindsOneNode) { |
| 2580 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2581 | recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2582 | new VerifyIdIsBoundToDecl<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | TEST(ForEach, BindsMultipleNodes) { |
| 2586 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2587 | recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2588 | new VerifyIdIsBoundToDecl<FieldDecl>("f", 3))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | TEST(ForEach, BindsRecursiveCombinations) { |
| 2592 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2593 | "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] | 2594 | recordDecl(hasName("C"), |
| 2595 | forEach(recordDecl(forEach(fieldDecl().bind("f"))))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2596 | new VerifyIdIsBoundToDecl<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | TEST(ForEachDescendant, BindsOneNode) { |
| 2600 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2601 | recordDecl(hasName("C"), |
| 2602 | forEachDescendant(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2603 | new VerifyIdIsBoundToDecl<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | TEST(ForEachDescendant, BindsMultipleNodes) { |
| 2607 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2608 | "class C { class D { int x; int y; }; " |
| 2609 | " class E { class F { int y; int z; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2610 | recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2611 | new VerifyIdIsBoundToDecl<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 2615 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2616 | "class C { class D { " |
| 2617 | " class E { class F { class G { int y; int z; }; }; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2618 | recordDecl(hasName("C"), forEachDescendant(recordDecl( |
| 2619 | forEachDescendant(fieldDecl().bind("f"))))), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2620 | new VerifyIdIsBoundToDecl<FieldDecl>("f", 8))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | |
| 2624 | TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) { |
| 2625 | // Make sure that we can both match the class by name (::X) and by the type |
| 2626 | // the template was instantiated with (via a field). |
| 2627 | |
| 2628 | EXPECT_TRUE(matches( |
| 2629 | "template <typename T> class X {}; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2630 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2631 | |
| 2632 | EXPECT_TRUE(matches( |
| 2633 | "template <typename T> class X { T t; }; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2634 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 2635 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) { |
| 2639 | EXPECT_TRUE(matches( |
| 2640 | "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] | 2641 | functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2642 | isTemplateInstantiation()))); |
| 2643 | } |
| 2644 | |
| 2645 | TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) { |
| 2646 | EXPECT_TRUE(matches( |
| 2647 | "template <typename T> class X { T t; }; class A {};" |
| 2648 | "template class X<A>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2649 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 2650 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | TEST(IsTemplateInstantiation, |
| 2654 | MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 2655 | EXPECT_TRUE(matches( |
| 2656 | "template <typename T> class X {};" |
| 2657 | "template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2658 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
| 2661 | TEST(IsTemplateInstantiation, |
| 2662 | MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 2663 | EXPECT_TRUE(matches( |
| 2664 | "class A {};" |
| 2665 | "class X {" |
| 2666 | " template <typename U> class Y { U u; };" |
| 2667 | " Y<A> y;" |
| 2668 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2669 | recordDecl(hasName("::X::Y"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 2673 | // FIXME: Figure out whether this makes sense. It doesn't affect the |
| 2674 | // normal use case as long as the uppermost instantiation always is marked |
| 2675 | // as template instantiation, but it might be confusing as a predicate. |
| 2676 | EXPECT_TRUE(matches( |
| 2677 | "class A {};" |
| 2678 | "template <typename T> class X {" |
| 2679 | " template <typename U> class Y { U u; };" |
| 2680 | " Y<T> y;" |
| 2681 | "}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2682 | recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) { |
| 2686 | EXPECT_TRUE(notMatches( |
| 2687 | "template <typename T> class X {}; class A {};" |
| 2688 | "template <> class X<A> {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2689 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) { |
| 2693 | EXPECT_TRUE(notMatches( |
| 2694 | "class A {}; class Y { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2695 | recordDecl(isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2698 | TEST(IsExplicitTemplateSpecialization, |
| 2699 | DoesNotMatchPrimaryTemplate) { |
| 2700 | EXPECT_TRUE(notMatches( |
| 2701 | "template <typename T> class X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2702 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2703 | EXPECT_TRUE(notMatches( |
| 2704 | "template <typename T> void f(T t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2705 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | TEST(IsExplicitTemplateSpecialization, |
| 2709 | DoesNotMatchExplicitTemplateInstantiations) { |
| 2710 | EXPECT_TRUE(notMatches( |
| 2711 | "template <typename T> class X {};" |
| 2712 | "template class X<int>; extern template class X<long>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2713 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2714 | EXPECT_TRUE(notMatches( |
| 2715 | "template <typename T> void f(T t) {}" |
| 2716 | "template void f(int t); extern template void f(long t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2717 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | TEST(IsExplicitTemplateSpecialization, |
| 2721 | DoesNotMatchImplicitTemplateInstantiations) { |
| 2722 | EXPECT_TRUE(notMatches( |
| 2723 | "template <typename T> class X {}; X<int> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2724 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2725 | EXPECT_TRUE(notMatches( |
| 2726 | "template <typename T> void f(T t); void g() { f(10); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2727 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | TEST(IsExplicitTemplateSpecialization, |
| 2731 | MatchesExplicitTemplateSpecializations) { |
| 2732 | EXPECT_TRUE(matches( |
| 2733 | "template <typename T> class X {};" |
| 2734 | "template<> class X<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2735 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2736 | EXPECT_TRUE(matches( |
| 2737 | "template <typename T> void f(T t) {}" |
| 2738 | "template<> void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2739 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 2742 | TEST(HasAncenstor, MatchesDeclarationAncestors) { |
| 2743 | EXPECT_TRUE(matches( |
| 2744 | "class A { class B { class C {}; }; };", |
| 2745 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A")))))); |
| 2746 | } |
| 2747 | |
| 2748 | TEST(HasAncenstor, FailsIfNoAncestorMatches) { |
| 2749 | EXPECT_TRUE(notMatches( |
| 2750 | "class A { class B { class C {}; }; };", |
| 2751 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X")))))); |
| 2752 | } |
| 2753 | |
| 2754 | TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) { |
| 2755 | EXPECT_TRUE(matches( |
| 2756 | "class A { class B { void f() { C c; } class C {}; }; };", |
| 2757 | varDecl(hasName("c"), hasType(recordDecl(hasName("C"), |
| 2758 | hasAncestor(recordDecl(hasName("A")))))))); |
| 2759 | } |
| 2760 | |
| 2761 | TEST(HasAncenstor, MatchesStatementAncestors) { |
| 2762 | EXPECT_TRUE(matches( |
| 2763 | "void f() { if (true) { while (false) { 42; } } }", |
| 2764 | expr(integerLiteral(equals(42), hasAncestor(ifStmt()))))); |
| 2765 | } |
| 2766 | |
| 2767 | TEST(HasAncestor, DrillsThroughDifferentHierarchies) { |
| 2768 | EXPECT_TRUE(matches( |
| 2769 | "void f() { if (true) { int x = 42; } }", |
| 2770 | expr(integerLiteral( |
| 2771 | equals(42), hasAncestor(functionDecl(hasName("f"))))))); |
| 2772 | } |
| 2773 | |
| 2774 | TEST(HasAncestor, BindsRecursiveCombinations) { |
| 2775 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2776 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 2777 | fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))), |
| 2778 | new VerifyIdIsBoundToDecl<CXXRecordDecl>("r", 1))); |
| 2779 | } |
| 2780 | |
| 2781 | TEST(HasAncestor, BindsCombinationsWithHasDescendant) { |
| 2782 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 2783 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 2784 | fieldDecl(hasAncestor( |
| 2785 | decl( |
| 2786 | hasDescendant(recordDecl(isDefinition(), |
| 2787 | hasAncestor(recordDecl()))) |
| 2788 | ).bind("d") |
| 2789 | )), |
| 2790 | new VerifyIdIsBoundToDecl<CXXRecordDecl>("d", "E"))); |
| 2791 | } |
| 2792 | |
| 2793 | TEST(HasAncestor, MatchesInTemplateInstantiations) { |
| 2794 | EXPECT_TRUE(matches( |
| 2795 | "template <typename T> struct A { struct B { struct C { T t; }; }; }; " |
| 2796 | "A<int>::B::C a;", |
| 2797 | fieldDecl(hasType(asString("int")), |
| 2798 | hasAncestor(recordDecl(hasName("A")))))); |
| 2799 | } |
| 2800 | |
| 2801 | TEST(HasAncestor, MatchesInImplicitCode) { |
| 2802 | EXPECT_TRUE(matches( |
| 2803 | "struct X {}; struct A { A() {} X x; };", |
| 2804 | constructorDecl( |
| 2805 | hasAnyConstructorInitializer(withInitializer(expr( |
| 2806 | hasAncestor(recordDecl(hasName("A"))))))))); |
| 2807 | } |
| 2808 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2809 | } // end namespace ast_matchers |
| 2810 | } // end namespace clang |