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