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