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" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 15 | #include "llvm/ADT/Triple.h" |
| 16 | #include "llvm/Support/Host.h" |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
| 18 | |
| 19 | namespace clang { |
| 20 | namespace ast_matchers { |
| 21 | |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame] | 22 | #if GTEST_HAS_DEATH_TEST |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 23 | TEST(HasNameDeathTest, DiesOnEmptyName) { |
| 24 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 25 | DeclarationMatcher HasEmptyName = recordDecl(hasName("")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 26 | EXPECT_TRUE(notMatches("class X {};", HasEmptyName)); |
| 27 | }, ""); |
| 28 | } |
| 29 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 30 | TEST(HasNameDeathTest, DiesOnEmptyPattern) { |
| 31 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 32 | DeclarationMatcher HasEmptyName = recordDecl(matchesName("")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 33 | EXPECT_TRUE(notMatches("class X {};", HasEmptyName)); |
| 34 | }, ""); |
| 35 | } |
| 36 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 37 | TEST(IsDerivedFromDeathTest, DiesOnEmptyBaseName) { |
| 38 | ASSERT_DEBUG_DEATH({ |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 39 | DeclarationMatcher IsDerivedFromEmpty = recordDecl(isDerivedFrom("")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 40 | EXPECT_TRUE(notMatches("class X {};", IsDerivedFromEmpty)); |
| 41 | }, ""); |
| 42 | } |
Benjamin Kramer | 78a0ce4 | 2012-07-10 17:30:44 +0000 | [diff] [blame] | 43 | #endif |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 44 | |
Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 45 | TEST(Finder, DynamicOnlyAcceptsSomeMatchers) { |
| 46 | MatchFinder Finder; |
| 47 | EXPECT_TRUE(Finder.addDynamicMatcher(decl(), NULL)); |
| 48 | EXPECT_TRUE(Finder.addDynamicMatcher(callExpr(), NULL)); |
| 49 | EXPECT_TRUE(Finder.addDynamicMatcher(constantArrayType(hasSize(42)), NULL)); |
| 50 | |
| 51 | // Do not accept non-toplevel matchers. |
| 52 | EXPECT_FALSE(Finder.addDynamicMatcher(isArrow(), NULL)); |
| 53 | EXPECT_FALSE(Finder.addDynamicMatcher(hasSize(2), NULL)); |
| 54 | EXPECT_FALSE(Finder.addDynamicMatcher(hasName("x"), NULL)); |
| 55 | } |
| 56 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 57 | TEST(Decl, MatchesDeclarations) { |
| 58 | EXPECT_TRUE(notMatches("", decl(usingDecl()))); |
| 59 | EXPECT_TRUE(matches("namespace x { class X {}; } using x::X;", |
| 60 | decl(usingDecl()))); |
| 61 | } |
| 62 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 63 | TEST(NameableDeclaration, MatchesVariousDecls) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 64 | DeclarationMatcher NamedX = namedDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 65 | EXPECT_TRUE(matches("typedef int X;", NamedX)); |
| 66 | EXPECT_TRUE(matches("int X;", NamedX)); |
| 67 | EXPECT_TRUE(matches("class foo { virtual void X(); };", NamedX)); |
| 68 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", NamedX)); |
| 69 | EXPECT_TRUE(matches("void foo() { int X; }", NamedX)); |
| 70 | EXPECT_TRUE(matches("namespace X { }", NamedX)); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 71 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 72 | |
| 73 | EXPECT_TRUE(notMatches("#define X 1", NamedX)); |
| 74 | } |
| 75 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 76 | TEST(NameableDeclaration, REMatchesVariousDecls) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 77 | DeclarationMatcher NamedX = namedDecl(matchesName("::X")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 78 | EXPECT_TRUE(matches("typedef int Xa;", NamedX)); |
| 79 | EXPECT_TRUE(matches("int Xb;", NamedX)); |
| 80 | EXPECT_TRUE(matches("class foo { virtual void Xc(); };", NamedX)); |
| 81 | EXPECT_TRUE(matches("void foo() try { } catch(int Xdef) { }", NamedX)); |
| 82 | EXPECT_TRUE(matches("void foo() { int Xgh; }", NamedX)); |
| 83 | EXPECT_TRUE(matches("namespace Xij { }", NamedX)); |
| 84 | EXPECT_TRUE(matches("enum X { A, B, C };", NamedX)); |
| 85 | |
| 86 | EXPECT_TRUE(notMatches("#define Xkl 1", NamedX)); |
| 87 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 88 | DeclarationMatcher StartsWithNo = namedDecl(matchesName("::no")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 89 | EXPECT_TRUE(matches("int no_foo;", StartsWithNo)); |
| 90 | EXPECT_TRUE(matches("class foo { virtual void nobody(); };", StartsWithNo)); |
| 91 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 92 | DeclarationMatcher Abc = namedDecl(matchesName("a.*b.*c")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 93 | EXPECT_TRUE(matches("int abc;", Abc)); |
| 94 | EXPECT_TRUE(matches("int aFOObBARc;", Abc)); |
| 95 | EXPECT_TRUE(notMatches("int cab;", Abc)); |
| 96 | EXPECT_TRUE(matches("int cabc;", Abc)); |
Manuel Klimek | ec33b6f | 2012-12-10 07:08:53 +0000 | [diff] [blame] | 97 | |
| 98 | DeclarationMatcher StartsWithK = namedDecl(matchesName(":k[^:]*$")); |
| 99 | EXPECT_TRUE(matches("int k;", StartsWithK)); |
| 100 | EXPECT_TRUE(matches("int kAbc;", StartsWithK)); |
| 101 | EXPECT_TRUE(matches("namespace x { int kTest; }", StartsWithK)); |
| 102 | EXPECT_TRUE(matches("class C { int k; };", StartsWithK)); |
| 103 | EXPECT_TRUE(notMatches("class C { int ckc; };", StartsWithK)); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 106 | TEST(DeclarationMatcher, MatchClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 107 | DeclarationMatcher ClassMatcher(recordDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 108 | llvm::Triple Triple(llvm::sys::getDefaultTargetTriple()); |
| 109 | if (Triple.getOS() != llvm::Triple::Win32 || |
| 110 | Triple.getEnvironment() != llvm::Triple::MSVC) |
| 111 | EXPECT_FALSE(matches("", ClassMatcher)); |
| 112 | else |
| 113 | // Matches class type_info. |
| 114 | EXPECT_TRUE(matches("", ClassMatcher)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 115 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 116 | DeclarationMatcher ClassX = recordDecl(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 117 | EXPECT_TRUE(matches("class X;", ClassX)); |
| 118 | EXPECT_TRUE(matches("class X {};", ClassX)); |
| 119 | EXPECT_TRUE(matches("template<class T> class X {};", ClassX)); |
| 120 | EXPECT_TRUE(notMatches("", ClassX)); |
| 121 | } |
| 122 | |
| 123 | TEST(DeclarationMatcher, ClassIsDerived) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 124 | DeclarationMatcher IsDerivedFromX = recordDecl(isDerivedFrom("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 125 | |
| 126 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsDerivedFromX)); |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 127 | EXPECT_TRUE(notMatches("class X {};", IsDerivedFromX)); |
| 128 | EXPECT_TRUE(notMatches("class X;", IsDerivedFromX)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 129 | EXPECT_TRUE(notMatches("class Y;", IsDerivedFromX)); |
| 130 | EXPECT_TRUE(notMatches("", IsDerivedFromX)); |
| 131 | |
Daniel Jasper | 63d8872 | 2012-09-12 21:14:15 +0000 | [diff] [blame] | 132 | DeclarationMatcher IsAX = recordDecl(isSameOrDerivedFrom("X")); |
Daniel Jasper | 76dafa7 | 2012-09-07 12:48:17 +0000 | [diff] [blame] | 133 | |
| 134 | EXPECT_TRUE(matches("class X {}; class Y : public X {};", IsAX)); |
| 135 | EXPECT_TRUE(matches("class X {};", IsAX)); |
| 136 | EXPECT_TRUE(matches("class X;", IsAX)); |
| 137 | EXPECT_TRUE(notMatches("class Y;", IsAX)); |
| 138 | EXPECT_TRUE(notMatches("", IsAX)); |
| 139 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 140 | DeclarationMatcher ZIsDerivedFromX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 141 | recordDecl(hasName("Z"), isDerivedFrom("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 142 | EXPECT_TRUE( |
| 143 | matches("class X {}; class Y : public X {}; class Z : public Y {};", |
| 144 | ZIsDerivedFromX)); |
| 145 | EXPECT_TRUE( |
| 146 | matches("class X {};" |
| 147 | "template<class T> class Y : public X {};" |
| 148 | "class Z : public Y<int> {};", ZIsDerivedFromX)); |
| 149 | EXPECT_TRUE(matches("class X {}; template<class T> class Z : public X {};", |
| 150 | ZIsDerivedFromX)); |
| 151 | EXPECT_TRUE( |
| 152 | matches("template<class T> class X {}; " |
| 153 | "template<class T> class Z : public X<T> {};", |
| 154 | ZIsDerivedFromX)); |
| 155 | EXPECT_TRUE( |
| 156 | matches("template<class T, class U=T> class X {}; " |
| 157 | "template<class T> class Z : public X<T> {};", |
| 158 | ZIsDerivedFromX)); |
| 159 | EXPECT_TRUE( |
| 160 | notMatches("template<class X> class A { class Z : public X {}; };", |
| 161 | ZIsDerivedFromX)); |
| 162 | EXPECT_TRUE( |
| 163 | matches("template<class X> class A { public: class Z : public X {}; }; " |
| 164 | "class X{}; void y() { A<X>::Z z; }", ZIsDerivedFromX)); |
| 165 | EXPECT_TRUE( |
| 166 | matches("template <class T> class X {}; " |
| 167 | "template<class Y> class A { class Z : public X<Y> {}; };", |
| 168 | ZIsDerivedFromX)); |
| 169 | EXPECT_TRUE( |
| 170 | notMatches("template<template<class T> class X> class A { " |
| 171 | " class Z : public X<int> {}; };", ZIsDerivedFromX)); |
| 172 | EXPECT_TRUE( |
| 173 | matches("template<template<class T> class X> class A { " |
| 174 | " public: class Z : public X<int> {}; }; " |
| 175 | "template<class T> class X {}; void y() { A<X>::Z z; }", |
| 176 | ZIsDerivedFromX)); |
| 177 | EXPECT_TRUE( |
| 178 | notMatches("template<class X> class A { class Z : public X::D {}; };", |
| 179 | ZIsDerivedFromX)); |
| 180 | EXPECT_TRUE( |
| 181 | matches("template<class X> class A { public: " |
| 182 | " class Z : public X::D {}; }; " |
| 183 | "class Y { public: class X {}; typedef X D; }; " |
| 184 | "void y() { A<Y>::Z z; }", ZIsDerivedFromX)); |
| 185 | EXPECT_TRUE( |
| 186 | matches("class X {}; typedef X Y; class Z : public Y {};", |
| 187 | ZIsDerivedFromX)); |
| 188 | EXPECT_TRUE( |
| 189 | matches("template<class T> class Y { typedef typename T::U X; " |
| 190 | " class Z : public X {}; };", ZIsDerivedFromX)); |
| 191 | EXPECT_TRUE(matches("class X {}; class Z : public ::X {};", |
| 192 | ZIsDerivedFromX)); |
| 193 | EXPECT_TRUE( |
| 194 | notMatches("template<class T> class X {}; " |
| 195 | "template<class T> class A { class Z : public X<T>::D {}; };", |
| 196 | ZIsDerivedFromX)); |
| 197 | EXPECT_TRUE( |
| 198 | matches("template<class T> class X { public: typedef X<T> D; }; " |
| 199 | "template<class T> class A { public: " |
| 200 | " class Z : public X<T>::D {}; }; void y() { A<int>::Z z; }", |
| 201 | ZIsDerivedFromX)); |
| 202 | EXPECT_TRUE( |
| 203 | notMatches("template<class X> class A { class Z : public X::D::E {}; };", |
| 204 | ZIsDerivedFromX)); |
| 205 | EXPECT_TRUE( |
| 206 | matches("class X {}; typedef X V; typedef V W; class Z : public W {};", |
| 207 | ZIsDerivedFromX)); |
| 208 | EXPECT_TRUE( |
| 209 | matches("class X {}; class Y : public X {}; " |
| 210 | "typedef Y V; typedef V W; class Z : public W {};", |
| 211 | ZIsDerivedFromX)); |
| 212 | EXPECT_TRUE( |
| 213 | matches("template<class T, class U> class X {}; " |
| 214 | "template<class T> class A { class Z : public X<T, int> {}; };", |
| 215 | ZIsDerivedFromX)); |
| 216 | EXPECT_TRUE( |
| 217 | notMatches("template<class X> class D { typedef X A; typedef A B; " |
| 218 | " typedef B C; class Z : public C {}; };", |
| 219 | ZIsDerivedFromX)); |
| 220 | EXPECT_TRUE( |
| 221 | matches("class X {}; typedef X A; typedef A B; " |
| 222 | "class Z : public B {};", ZIsDerivedFromX)); |
| 223 | EXPECT_TRUE( |
| 224 | matches("class X {}; typedef X A; typedef A B; typedef B C; " |
| 225 | "class Z : public C {};", ZIsDerivedFromX)); |
| 226 | EXPECT_TRUE( |
| 227 | matches("class U {}; typedef U X; typedef X V; " |
| 228 | "class Z : public V {};", ZIsDerivedFromX)); |
| 229 | EXPECT_TRUE( |
| 230 | matches("class Base {}; typedef Base X; " |
| 231 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 232 | EXPECT_TRUE( |
| 233 | matches("class Base {}; typedef Base Base2; typedef Base2 X; " |
| 234 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 235 | EXPECT_TRUE( |
| 236 | notMatches("class Base {}; class Base2 {}; typedef Base2 X; " |
| 237 | "class Z : public Base {};", ZIsDerivedFromX)); |
| 238 | EXPECT_TRUE( |
| 239 | matches("class A {}; typedef A X; typedef A Y; " |
| 240 | "class Z : public Y {};", ZIsDerivedFromX)); |
| 241 | EXPECT_TRUE( |
| 242 | notMatches("template <typename T> class Z;" |
| 243 | "template <> class Z<void> {};" |
| 244 | "template <typename T> class Z : public Z<void> {};", |
| 245 | IsDerivedFromX)); |
| 246 | EXPECT_TRUE( |
| 247 | matches("template <typename T> class X;" |
| 248 | "template <> class X<void> {};" |
| 249 | "template <typename T> class X : public X<void> {};", |
| 250 | IsDerivedFromX)); |
| 251 | EXPECT_TRUE(matches( |
| 252 | "class X {};" |
| 253 | "template <typename T> class Z;" |
| 254 | "template <> class Z<void> {};" |
| 255 | "template <typename T> class Z : public Z<void>, public X {};", |
| 256 | ZIsDerivedFromX)); |
Manuel Klimek | 987c2f5 | 2012-12-04 13:40:29 +0000 | [diff] [blame] | 257 | EXPECT_TRUE( |
| 258 | notMatches("template<int> struct X;" |
| 259 | "template<int i> struct X : public X<i-1> {};", |
| 260 | recordDecl(isDerivedFrom(recordDecl(hasName("Some")))))); |
| 261 | EXPECT_TRUE(matches( |
| 262 | "struct A {};" |
| 263 | "template<int> struct X;" |
| 264 | "template<int i> struct X : public X<i-1> {};" |
| 265 | "template<> struct X<0> : public A {};" |
| 266 | "struct B : public X<42> {};", |
| 267 | recordDecl(hasName("B"), isDerivedFrom(recordDecl(hasName("A")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 268 | |
| 269 | // FIXME: Once we have better matchers for template type matching, |
| 270 | // get rid of the Variable(...) matching and match the right template |
| 271 | // declarations directly. |
| 272 | const char *RecursiveTemplateOneParameter = |
| 273 | "class Base1 {}; class Base2 {};" |
| 274 | "template <typename T> class Z;" |
| 275 | "template <> class Z<void> : public Base1 {};" |
| 276 | "template <> class Z<int> : public Base2 {};" |
| 277 | "template <> class Z<float> : public Z<void> {};" |
| 278 | "template <> class Z<double> : public Z<int> {};" |
| 279 | "template <typename T> class Z : public Z<float>, public Z<double> {};" |
| 280 | "void f() { Z<float> z_float; Z<double> z_double; Z<char> z_char; }"; |
| 281 | EXPECT_TRUE(matches( |
| 282 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 283 | varDecl(hasName("z_float"), |
| 284 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 285 | EXPECT_TRUE(notMatches( |
| 286 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 287 | varDecl(hasName("z_float"), |
| 288 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 289 | EXPECT_TRUE(matches( |
| 290 | RecursiveTemplateOneParameter, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 291 | varDecl(hasName("z_char"), |
| 292 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"), |
| 293 | isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 294 | |
| 295 | const char *RecursiveTemplateTwoParameters = |
| 296 | "class Base1 {}; class Base2 {};" |
| 297 | "template <typename T1, typename T2> class Z;" |
| 298 | "template <typename T> class Z<void, T> : public Base1 {};" |
| 299 | "template <typename T> class Z<int, T> : public Base2 {};" |
| 300 | "template <typename T> class Z<float, T> : public Z<void, T> {};" |
| 301 | "template <typename T> class Z<double, T> : public Z<int, T> {};" |
| 302 | "template <typename T1, typename T2> class Z : " |
| 303 | " public Z<float, T2>, public Z<double, T2> {};" |
| 304 | "void f() { Z<float, void> z_float; Z<double, void> z_double; " |
| 305 | " Z<char, void> z_char; }"; |
| 306 | EXPECT_TRUE(matches( |
| 307 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 308 | varDecl(hasName("z_float"), |
| 309 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 310 | EXPECT_TRUE(notMatches( |
| 311 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 312 | varDecl(hasName("z_float"), |
| 313 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base2"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 314 | EXPECT_TRUE(matches( |
| 315 | RecursiveTemplateTwoParameters, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 316 | varDecl(hasName("z_char"), |
| 317 | hasInitializer(hasType(recordDecl(isDerivedFrom("Base1"), |
| 318 | isDerivedFrom("Base2"))))))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 319 | EXPECT_TRUE(matches( |
| 320 | "namespace ns { class X {}; class Y : public X {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 321 | recordDecl(isDerivedFrom("::ns::X")))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 322 | EXPECT_TRUE(notMatches( |
| 323 | "class X {}; class Y : public X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 324 | recordDecl(isDerivedFrom("::ns::X")))); |
Daniel Jasper | 20b802d | 2012-07-17 07:39:27 +0000 | [diff] [blame] | 325 | |
| 326 | EXPECT_TRUE(matches( |
| 327 | "class X {}; class Y : public X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 328 | recordDecl(isDerivedFrom(recordDecl(hasName("X")).bind("test"))))); |
Manuel Klimek | b56da8c | 2013-08-02 21:24:09 +0000 | [diff] [blame] | 329 | |
| 330 | EXPECT_TRUE(matches( |
| 331 | "template<typename T> class X {};" |
| 332 | "template<typename T> using Z = X<T>;" |
| 333 | "template <typename T> class Y : Z<T> {};", |
| 334 | recordDecl(isDerivedFrom(namedDecl(hasName("X")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 337 | TEST(DeclarationMatcher, hasMethod) { |
| 338 | EXPECT_TRUE(matches("class A { void func(); };", |
| 339 | recordDecl(hasMethod(hasName("func"))))); |
| 340 | EXPECT_TRUE(notMatches("class A { void func(); };", |
| 341 | recordDecl(hasMethod(isPublic())))); |
| 342 | } |
| 343 | |
Daniel Jasper | 08f0c53 | 2012-09-18 14:17:42 +0000 | [diff] [blame] | 344 | TEST(DeclarationMatcher, ClassDerivedFromDependentTemplateSpecialization) { |
| 345 | EXPECT_TRUE(matches( |
| 346 | "template <typename T> struct A {" |
| 347 | " template <typename T2> struct F {};" |
| 348 | "};" |
| 349 | "template <typename T> struct B : A<T>::template F<T> {};" |
| 350 | "B<int> b;", |
| 351 | recordDecl(hasName("B"), isDerivedFrom(recordDecl())))); |
| 352 | } |
| 353 | |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 354 | TEST(DeclarationMatcher, hasDeclContext) { |
| 355 | EXPECT_TRUE(matches( |
| 356 | "namespace N {" |
| 357 | " namespace M {" |
| 358 | " class D {};" |
| 359 | " }" |
| 360 | "}", |
Daniel Jasper | abe9223 | 2013-04-08 16:44:05 +0000 | [diff] [blame] | 361 | recordDecl(hasDeclContext(namespaceDecl(hasName("M")))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 362 | EXPECT_TRUE(notMatches( |
| 363 | "namespace N {" |
| 364 | " namespace M {" |
| 365 | " class D {};" |
| 366 | " }" |
| 367 | "}", |
Daniel Jasper | abe9223 | 2013-04-08 16:44:05 +0000 | [diff] [blame] | 368 | recordDecl(hasDeclContext(namespaceDecl(hasName("N")))))); |
| 369 | |
| 370 | EXPECT_TRUE(matches("namespace {" |
| 371 | " namespace M {" |
| 372 | " class D {};" |
| 373 | " }" |
| 374 | "}", |
| 375 | recordDecl(hasDeclContext(namespaceDecl( |
| 376 | hasName("M"), hasDeclContext(namespaceDecl())))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 379 | TEST(ClassTemplate, DoesNotMatchClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 380 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 381 | EXPECT_TRUE(notMatches("class X;", ClassX)); |
| 382 | EXPECT_TRUE(notMatches("class X {};", ClassX)); |
| 383 | } |
| 384 | |
| 385 | TEST(ClassTemplate, MatchesClassTemplate) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 386 | DeclarationMatcher ClassX = classTemplateDecl(hasName("X")); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 387 | EXPECT_TRUE(matches("template<typename T> class X {};", ClassX)); |
| 388 | EXPECT_TRUE(matches("class Z { template<class T> class X {}; };", ClassX)); |
| 389 | } |
| 390 | |
| 391 | TEST(ClassTemplate, DoesNotMatchClassTemplateExplicitSpecialization) { |
| 392 | EXPECT_TRUE(notMatches("template<typename T> class X { };" |
| 393 | "template<> class X<int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 394 | classTemplateDecl(hasName("X"), |
| 395 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | TEST(ClassTemplate, DoesNotMatchClassTemplatePartialSpecialization) { |
| 399 | EXPECT_TRUE(notMatches("template<typename T, typename U> class X { };" |
| 400 | "template<typename T> class X<T, int> { int a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 401 | classTemplateDecl(hasName("X"), |
| 402 | hasDescendant(fieldDecl(hasName("a")))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 405 | TEST(AllOf, AllOverloadsWork) { |
| 406 | const char Program[] = |
Edwin Vane | 7f43fcf | 2013-02-12 13:55:40 +0000 | [diff] [blame] | 407 | "struct T { };" |
| 408 | "int f(int, T*, int, int);" |
| 409 | "void g(int x) { T t; f(x, &t, 3, 4); }"; |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 410 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 411 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 412 | hasArgument(0, declRefExpr(to(varDecl()))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 413 | EXPECT_TRUE(matches(Program, |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 414 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 415 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 416 | hasArgument(1, hasType(pointsTo( |
| 417 | recordDecl(hasName("T"))))))))); |
Edwin Vane | 7f43fcf | 2013-02-12 13:55:40 +0000 | [diff] [blame] | 418 | EXPECT_TRUE(matches(Program, |
| 419 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 420 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 421 | hasArgument(1, hasType(pointsTo( |
| 422 | recordDecl(hasName("T"))))), |
| 423 | hasArgument(2, integerLiteral(equals(3))))))); |
| 424 | EXPECT_TRUE(matches(Program, |
| 425 | callExpr(allOf(callee(functionDecl(hasName("f"))), |
| 426 | hasArgument(0, declRefExpr(to(varDecl()))), |
| 427 | hasArgument(1, hasType(pointsTo( |
| 428 | recordDecl(hasName("T"))))), |
| 429 | hasArgument(2, integerLiteral(equals(3))), |
| 430 | hasArgument(3, integerLiteral(equals(4))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 433 | TEST(DeclarationMatcher, MatchAnyOf) { |
| 434 | DeclarationMatcher YOrZDerivedFromX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 435 | recordDecl(anyOf(hasName("Y"), allOf(isDerivedFrom("X"), hasName("Z")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 436 | EXPECT_TRUE( |
| 437 | matches("class X {}; class Z : public X {};", YOrZDerivedFromX)); |
| 438 | EXPECT_TRUE(matches("class Y {};", YOrZDerivedFromX)); |
| 439 | EXPECT_TRUE( |
| 440 | notMatches("class X {}; class W : public X {};", YOrZDerivedFromX)); |
| 441 | EXPECT_TRUE(notMatches("class Z {};", YOrZDerivedFromX)); |
| 442 | |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 443 | DeclarationMatcher XOrYOrZOrU = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 444 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"))); |
Daniel Jasper | ff2fcb8 | 2012-07-15 19:57:12 +0000 | [diff] [blame] | 445 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrU)); |
| 446 | EXPECT_TRUE(notMatches("class V {};", XOrYOrZOrU)); |
| 447 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 448 | DeclarationMatcher XOrYOrZOrUOrV = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 449 | recordDecl(anyOf(hasName("X"), hasName("Y"), hasName("Z"), hasName("U"), |
| 450 | hasName("V"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 451 | EXPECT_TRUE(matches("class X {};", XOrYOrZOrUOrV)); |
| 452 | EXPECT_TRUE(matches("class Y {};", XOrYOrZOrUOrV)); |
| 453 | EXPECT_TRUE(matches("class Z {};", XOrYOrZOrUOrV)); |
| 454 | EXPECT_TRUE(matches("class U {};", XOrYOrZOrUOrV)); |
| 455 | EXPECT_TRUE(matches("class V {};", XOrYOrZOrUOrV)); |
| 456 | EXPECT_TRUE(notMatches("class A {};", XOrYOrZOrUOrV)); |
| 457 | } |
| 458 | |
| 459 | TEST(DeclarationMatcher, MatchHas) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 460 | DeclarationMatcher HasClassX = recordDecl(has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 461 | EXPECT_TRUE(matches("class Y { class X {}; };", HasClassX)); |
| 462 | EXPECT_TRUE(matches("class X {};", HasClassX)); |
| 463 | |
| 464 | DeclarationMatcher YHasClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 465 | recordDecl(hasName("Y"), has(recordDecl(hasName("X")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 466 | EXPECT_TRUE(matches("class Y { class X {}; };", YHasClassX)); |
| 467 | EXPECT_TRUE(notMatches("class X {};", YHasClassX)); |
| 468 | EXPECT_TRUE( |
| 469 | notMatches("class Y { class Z { class X {}; }; };", YHasClassX)); |
| 470 | } |
| 471 | |
| 472 | TEST(DeclarationMatcher, MatchHasRecursiveAllOf) { |
| 473 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 474 | recordDecl( |
| 475 | has(recordDecl( |
| 476 | has(recordDecl(hasName("X"))), |
| 477 | has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 478 | hasName("Z"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 479 | has(recordDecl( |
| 480 | has(recordDecl(hasName("A"))), |
| 481 | has(recordDecl(hasName("B"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 482 | hasName("C"))), |
| 483 | hasName("F")); |
| 484 | |
| 485 | EXPECT_TRUE(matches( |
| 486 | "class F {" |
| 487 | " class Z {" |
| 488 | " class X {};" |
| 489 | " class Y {};" |
| 490 | " };" |
| 491 | " class C {" |
| 492 | " class A {};" |
| 493 | " class B {};" |
| 494 | " };" |
| 495 | "};", Recursive)); |
| 496 | |
| 497 | EXPECT_TRUE(matches( |
| 498 | "class F {" |
| 499 | " class Z {" |
| 500 | " class A {};" |
| 501 | " class X {};" |
| 502 | " class Y {};" |
| 503 | " };" |
| 504 | " class C {" |
| 505 | " class X {};" |
| 506 | " class A {};" |
| 507 | " class B {};" |
| 508 | " };" |
| 509 | "};", Recursive)); |
| 510 | |
| 511 | EXPECT_TRUE(matches( |
| 512 | "class O1 {" |
| 513 | " class O2 {" |
| 514 | " class F {" |
| 515 | " class Z {" |
| 516 | " class A {};" |
| 517 | " class X {};" |
| 518 | " class Y {};" |
| 519 | " };" |
| 520 | " class C {" |
| 521 | " class X {};" |
| 522 | " class A {};" |
| 523 | " class B {};" |
| 524 | " };" |
| 525 | " };" |
| 526 | " };" |
| 527 | "};", Recursive)); |
| 528 | } |
| 529 | |
| 530 | TEST(DeclarationMatcher, MatchHasRecursiveAnyOf) { |
| 531 | DeclarationMatcher Recursive = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 532 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 533 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 534 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 535 | anyOf( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 536 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 537 | hasName("X"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 538 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 539 | hasName("Y"))), |
| 540 | hasName("Z")))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 541 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 542 | anyOf( |
| 543 | hasName("C"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 544 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 545 | hasName("A"))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 546 | has(recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 547 | hasName("B")))))), |
| 548 | hasName("F"))); |
| 549 | |
| 550 | EXPECT_TRUE(matches("class F {};", Recursive)); |
| 551 | EXPECT_TRUE(matches("class Z {};", Recursive)); |
| 552 | EXPECT_TRUE(matches("class C {};", Recursive)); |
| 553 | EXPECT_TRUE(matches("class M { class N { class X {}; }; };", Recursive)); |
| 554 | EXPECT_TRUE(matches("class M { class N { class B {}; }; };", Recursive)); |
| 555 | EXPECT_TRUE( |
| 556 | matches("class O1 { class O2 {" |
| 557 | " class M { class N { class B {}; }; }; " |
| 558 | "}; };", Recursive)); |
| 559 | } |
| 560 | |
| 561 | TEST(DeclarationMatcher, MatchNot) { |
| 562 | DeclarationMatcher NotClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 563 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 564 | isDerivedFrom("Y"), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 565 | unless(hasName("X"))); |
| 566 | EXPECT_TRUE(notMatches("", NotClassX)); |
| 567 | EXPECT_TRUE(notMatches("class Y {};", NotClassX)); |
| 568 | EXPECT_TRUE(matches("class Y {}; class Z : public Y {};", NotClassX)); |
| 569 | EXPECT_TRUE(notMatches("class Y {}; class X : public Y {};", NotClassX)); |
| 570 | EXPECT_TRUE( |
| 571 | notMatches("class Y {}; class Z {}; class X : public Y {};", |
| 572 | NotClassX)); |
| 573 | |
| 574 | DeclarationMatcher ClassXHasNotClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 575 | recordDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 576 | hasName("X"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 577 | has(recordDecl(hasName("Z"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 578 | unless( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 579 | has(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 580 | EXPECT_TRUE(matches("class X { class Z {}; };", ClassXHasNotClassY)); |
| 581 | EXPECT_TRUE(notMatches("class X { class Y {}; class Z {}; };", |
| 582 | ClassXHasNotClassY)); |
| 583 | } |
| 584 | |
| 585 | TEST(DeclarationMatcher, HasDescendant) { |
| 586 | DeclarationMatcher ZDescendantClassX = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 587 | recordDecl( |
| 588 | hasDescendant(recordDecl(hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 589 | hasName("Z")); |
| 590 | EXPECT_TRUE(matches("class Z { class X {}; };", ZDescendantClassX)); |
| 591 | EXPECT_TRUE( |
| 592 | matches("class Z { class Y { class X {}; }; };", ZDescendantClassX)); |
| 593 | EXPECT_TRUE( |
| 594 | matches("class Z { class A { class Y { class X {}; }; }; };", |
| 595 | ZDescendantClassX)); |
| 596 | EXPECT_TRUE( |
| 597 | matches("class Z { class A { class B { class Y { class X {}; }; }; }; };", |
| 598 | ZDescendantClassX)); |
| 599 | EXPECT_TRUE(notMatches("class Z {};", ZDescendantClassX)); |
| 600 | |
| 601 | DeclarationMatcher ZDescendantClassXHasClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 602 | recordDecl( |
| 603 | hasDescendant(recordDecl(has(recordDecl(hasName("Y"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 604 | hasName("X"))), |
| 605 | hasName("Z")); |
| 606 | EXPECT_TRUE(matches("class Z { class X { class Y {}; }; };", |
| 607 | ZDescendantClassXHasClassY)); |
| 608 | EXPECT_TRUE( |
| 609 | matches("class Z { class A { class B { class X { class Y {}; }; }; }; };", |
| 610 | ZDescendantClassXHasClassY)); |
| 611 | EXPECT_TRUE(notMatches( |
| 612 | "class Z {" |
| 613 | " class A {" |
| 614 | " class B {" |
| 615 | " class X {" |
| 616 | " class C {" |
| 617 | " class Y {};" |
| 618 | " };" |
| 619 | " };" |
| 620 | " }; " |
| 621 | " };" |
| 622 | "};", ZDescendantClassXHasClassY)); |
| 623 | |
| 624 | DeclarationMatcher ZDescendantClassXDescendantClassY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 625 | recordDecl( |
| 626 | hasDescendant(recordDecl(hasDescendant(recordDecl(hasName("Y"))), |
| 627 | hasName("X"))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 628 | hasName("Z")); |
| 629 | EXPECT_TRUE( |
| 630 | matches("class Z { class A { class X { class B { class Y {}; }; }; }; };", |
| 631 | ZDescendantClassXDescendantClassY)); |
| 632 | EXPECT_TRUE(matches( |
| 633 | "class Z {" |
| 634 | " class A {" |
| 635 | " class X {" |
| 636 | " class B {" |
| 637 | " class Y {};" |
| 638 | " };" |
| 639 | " class Y {};" |
| 640 | " };" |
| 641 | " };" |
| 642 | "};", ZDescendantClassXDescendantClassY)); |
| 643 | } |
| 644 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 645 | // Implements a run method that returns whether BoundNodes contains a |
| 646 | // Decl bound to Id that can be dynamically cast to T. |
| 647 | // Optionally checks that the check succeeded a specific number of times. |
| 648 | template <typename T> |
| 649 | class VerifyIdIsBoundTo : public BoundNodesCallback { |
| 650 | public: |
| 651 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 652 | // Does not check for a certain number of matches. |
| 653 | explicit VerifyIdIsBoundTo(llvm::StringRef Id) |
| 654 | : Id(Id), ExpectedCount(-1), Count(0) {} |
| 655 | |
| 656 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 657 | // Checks that there were exactly \c ExpectedCount matches. |
| 658 | VerifyIdIsBoundTo(llvm::StringRef Id, int ExpectedCount) |
| 659 | : Id(Id), ExpectedCount(ExpectedCount), Count(0) {} |
| 660 | |
| 661 | // Create an object that checks that a node of type \c T was bound to \c Id. |
| 662 | // Checks that there was exactly one match with the name \c ExpectedName. |
| 663 | // Note that \c T must be a NamedDecl for this to work. |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 664 | VerifyIdIsBoundTo(llvm::StringRef Id, llvm::StringRef ExpectedName, |
| 665 | int ExpectedCount = 1) |
| 666 | : Id(Id), ExpectedCount(ExpectedCount), Count(0), |
| 667 | ExpectedName(ExpectedName) {} |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 668 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 669 | void onEndOfTranslationUnit() override { |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 670 | if (ExpectedCount != -1) |
| 671 | EXPECT_EQ(ExpectedCount, Count); |
| 672 | if (!ExpectedName.empty()) |
| 673 | EXPECT_EQ(ExpectedName, Name); |
Peter Collingbourne | d2bd589 | 2013-11-07 22:30:32 +0000 | [diff] [blame] | 674 | Count = 0; |
| 675 | Name.clear(); |
| 676 | } |
| 677 | |
| 678 | ~VerifyIdIsBoundTo() { |
| 679 | EXPECT_EQ(0, Count); |
| 680 | EXPECT_EQ("", Name); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | virtual bool run(const BoundNodes *Nodes) { |
Peter Collingbourne | 3f0e040 | 2013-11-06 00:27:07 +0000 | [diff] [blame] | 684 | const BoundNodes::IDToNodeMap &M = Nodes->getMap(); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 685 | if (Nodes->getNodeAs<T>(Id)) { |
| 686 | ++Count; |
| 687 | if (const NamedDecl *Named = Nodes->getNodeAs<NamedDecl>(Id)) { |
| 688 | Name = Named->getNameAsString(); |
| 689 | } else if (const NestedNameSpecifier *NNS = |
| 690 | Nodes->getNodeAs<NestedNameSpecifier>(Id)) { |
| 691 | llvm::raw_string_ostream OS(Name); |
| 692 | NNS->print(OS, PrintingPolicy(LangOptions())); |
| 693 | } |
Peter Collingbourne | 3f0e040 | 2013-11-06 00:27:07 +0000 | [diff] [blame] | 694 | BoundNodes::IDToNodeMap::const_iterator I = M.find(Id); |
| 695 | EXPECT_NE(M.end(), I); |
| 696 | if (I != M.end()) |
| 697 | EXPECT_EQ(Nodes->getNodeAs<T>(Id), I->second.get<T>()); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 698 | return true; |
| 699 | } |
Peter Collingbourne | 3f0e040 | 2013-11-06 00:27:07 +0000 | [diff] [blame] | 700 | EXPECT_TRUE(M.count(Id) == 0 || M.find(Id)->second.template get<T>() == 0); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 701 | return false; |
| 702 | } |
| 703 | |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 704 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 705 | return run(Nodes); |
| 706 | } |
| 707 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 708 | private: |
| 709 | const std::string Id; |
| 710 | const int ExpectedCount; |
| 711 | int Count; |
| 712 | const std::string ExpectedName; |
| 713 | std::string Name; |
| 714 | }; |
| 715 | |
| 716 | TEST(HasDescendant, MatchesDescendantTypes) { |
| 717 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 718 | decl(hasDescendant(loc(builtinType()))))); |
| 719 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 720 | stmt(hasDescendant(builtinType())))); |
| 721 | |
| 722 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 723 | stmt(hasDescendant(loc(builtinType()))))); |
| 724 | EXPECT_TRUE(matches("void f() { int i = 3; }", |
| 725 | stmt(hasDescendant(qualType(builtinType()))))); |
| 726 | |
| 727 | EXPECT_TRUE(notMatches("void f() { float f = 2.0f; }", |
| 728 | stmt(hasDescendant(isInteger())))); |
| 729 | |
| 730 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 731 | "void f() { int a; float c; int d; int e; }", |
| 732 | functionDecl(forEachDescendant( |
| 733 | varDecl(hasDescendant(isInteger())).bind("x"))), |
| 734 | new VerifyIdIsBoundTo<Decl>("x", 3))); |
| 735 | } |
| 736 | |
| 737 | TEST(HasDescendant, MatchesDescendantsOfTypes) { |
| 738 | EXPECT_TRUE(matches("void f() { int*** i; }", |
| 739 | qualType(hasDescendant(builtinType())))); |
| 740 | EXPECT_TRUE(matches("void f() { int*** i; }", |
| 741 | qualType(hasDescendant( |
| 742 | pointerType(pointee(builtinType())))))); |
| 743 | EXPECT_TRUE(matches("void f() { int*** i; }", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 744 | typeLoc(hasDescendant(loc(builtinType()))))); |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 745 | |
| 746 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 747 | "void f() { int*** i; }", |
| 748 | qualType(asString("int ***"), forEachDescendant(pointerType().bind("x"))), |
| 749 | new VerifyIdIsBoundTo<Type>("x", 2))); |
| 750 | } |
| 751 | |
| 752 | TEST(Has, MatchesChildrenOfTypes) { |
| 753 | EXPECT_TRUE(matches("int i;", |
| 754 | varDecl(hasName("i"), has(isInteger())))); |
| 755 | EXPECT_TRUE(notMatches("int** i;", |
| 756 | varDecl(hasName("i"), has(isInteger())))); |
| 757 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 758 | "int (*f)(float, int);", |
| 759 | qualType(functionType(), forEach(qualType(isInteger()).bind("x"))), |
| 760 | new VerifyIdIsBoundTo<QualType>("x", 2))); |
| 761 | } |
| 762 | |
| 763 | TEST(Has, MatchesChildTypes) { |
| 764 | EXPECT_TRUE(matches( |
| 765 | "int* i;", |
| 766 | varDecl(hasName("i"), hasType(qualType(has(builtinType())))))); |
| 767 | EXPECT_TRUE(notMatches( |
| 768 | "int* i;", |
| 769 | varDecl(hasName("i"), hasType(qualType(has(pointerType())))))); |
| 770 | } |
| 771 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 772 | TEST(Enum, DoesNotMatchClasses) { |
| 773 | EXPECT_TRUE(notMatches("class X {};", enumDecl(hasName("X")))); |
| 774 | } |
| 775 | |
| 776 | TEST(Enum, MatchesEnums) { |
| 777 | EXPECT_TRUE(matches("enum X {};", enumDecl(hasName("X")))); |
| 778 | } |
| 779 | |
| 780 | TEST(EnumConstant, Matches) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 781 | DeclarationMatcher Matcher = enumConstantDecl(hasName("A")); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 782 | EXPECT_TRUE(matches("enum X{ A };", Matcher)); |
| 783 | EXPECT_TRUE(notMatches("enum X{ B };", Matcher)); |
| 784 | EXPECT_TRUE(notMatches("enum X {};", Matcher)); |
| 785 | } |
| 786 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 787 | TEST(StatementMatcher, Has) { |
| 788 | StatementMatcher HasVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 789 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 790 | has(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 791 | |
| 792 | EXPECT_TRUE(matches( |
| 793 | "class X; X *x(int); void c() { int i; x(i); }", HasVariableI)); |
| 794 | EXPECT_TRUE(notMatches( |
| 795 | "class X; X *x(int); void c() { int i; x(42); }", HasVariableI)); |
| 796 | } |
| 797 | |
| 798 | TEST(StatementMatcher, HasDescendant) { |
| 799 | StatementMatcher HasDescendantVariableI = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 800 | expr(hasType(pointsTo(recordDecl(hasName("X")))), |
| 801 | hasDescendant(declRefExpr(to(varDecl(hasName("i")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 802 | |
| 803 | EXPECT_TRUE(matches( |
| 804 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(i)); }", |
| 805 | HasDescendantVariableI)); |
| 806 | EXPECT_TRUE(notMatches( |
| 807 | "class X; X *x(bool); bool b(int); void c() { int i; x(b(42)); }", |
| 808 | HasDescendantVariableI)); |
| 809 | } |
| 810 | |
| 811 | TEST(TypeMatcher, MatchesClassType) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 812 | TypeMatcher TypeA = hasDeclaration(recordDecl(hasName("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 813 | |
| 814 | EXPECT_TRUE(matches("class A { public: A *a; };", TypeA)); |
| 815 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 816 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 817 | TypeMatcher TypeDerivedFromA = hasDeclaration(recordDecl(isDerivedFrom("A"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 818 | |
| 819 | EXPECT_TRUE(matches("class A {}; class B : public A { public: B *b; };", |
| 820 | TypeDerivedFromA)); |
| 821 | EXPECT_TRUE(notMatches("class A {};", TypeA)); |
| 822 | |
| 823 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 824 | recordDecl(hasName("A"), has(recordDecl(hasName("B"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 825 | |
| 826 | EXPECT_TRUE( |
| 827 | matches("class A { public: A *a; class B {}; };", TypeAHasClassB)); |
| 828 | } |
| 829 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 830 | TEST(Matcher, BindMatchedNodes) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 831 | DeclarationMatcher ClassX = has(recordDecl(hasName("::X")).bind("x")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 832 | |
| 833 | EXPECT_TRUE(matchAndVerifyResultTrue("class X {};", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 834 | ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 835 | |
| 836 | EXPECT_TRUE(matchAndVerifyResultFalse("class X {};", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 837 | ClassX, new VerifyIdIsBoundTo<CXXRecordDecl>("other-id"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 838 | |
| 839 | TypeMatcher TypeAHasClassB = hasDeclaration( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 840 | recordDecl(hasName("A"), has(recordDecl(hasName("B")).bind("b")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 841 | |
| 842 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { public: A *a; class B {}; };", |
| 843 | TypeAHasClassB, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 844 | new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 845 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 846 | StatementMatcher MethodX = |
| 847 | callExpr(callee(methodDecl(hasName("x")))).bind("x"); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 848 | |
| 849 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { void x() { x(); } };", |
| 850 | MethodX, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 851 | new VerifyIdIsBoundTo<CXXMemberCallExpr>("x"))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | TEST(Matcher, BindTheSameNameInAlternatives) { |
| 855 | StatementMatcher matcher = anyOf( |
| 856 | binaryOperator(hasOperatorName("+"), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 857 | hasLHS(expr().bind("x")), |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 858 | hasRHS(integerLiteral(equals(0)))), |
| 859 | binaryOperator(hasOperatorName("+"), |
| 860 | hasLHS(integerLiteral(equals(0))), |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 861 | hasRHS(expr().bind("x")))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 862 | |
| 863 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 864 | // The first branch of the matcher binds x to 0 but then fails. |
| 865 | // The second branch binds x to f() and succeeds. |
| 866 | "int f() { return 0 + f(); }", |
| 867 | matcher, |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 868 | new VerifyIdIsBoundTo<CallExpr>("x"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 871 | TEST(Matcher, BindsIDForMemoizedResults) { |
| 872 | // Using the same matcher in two match expressions will make memoization |
| 873 | // kick in. |
| 874 | DeclarationMatcher ClassX = recordDecl(hasName("X")).bind("x"); |
| 875 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 876 | "class A { class B { class X {}; }; };", |
| 877 | DeclarationMatcher(anyOf( |
| 878 | recordDecl(hasName("A"), hasDescendant(ClassX)), |
| 879 | recordDecl(hasName("B"), hasDescendant(ClassX)))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 880 | new VerifyIdIsBoundTo<Decl>("x", 2))); |
Manuel Klimek | 66341c5 | 2012-08-30 19:41:06 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Daniel Jasper | 189f2e4 | 2012-12-03 15:43:25 +0000 | [diff] [blame] | 883 | TEST(HasDeclaration, HasDeclarationOfEnumType) { |
| 884 | EXPECT_TRUE(matches("enum X {}; void y(X *x) { x; }", |
| 885 | expr(hasType(pointsTo( |
| 886 | qualType(hasDeclaration(enumDecl(hasName("X"))))))))); |
| 887 | } |
| 888 | |
Edwin Vane | b45083d | 2013-02-25 14:32:42 +0000 | [diff] [blame] | 889 | TEST(HasDeclaration, HasGetDeclTraitTest) { |
| 890 | EXPECT_TRUE(internal::has_getDecl<TypedefType>::value); |
| 891 | EXPECT_TRUE(internal::has_getDecl<RecordType>::value); |
| 892 | EXPECT_FALSE(internal::has_getDecl<TemplateSpecializationType>::value); |
| 893 | } |
| 894 | |
Edwin Vane | 5238060 | 2013-02-19 17:14:34 +0000 | [diff] [blame] | 895 | TEST(HasDeclaration, HasDeclarationOfTypeWithDecl) { |
| 896 | EXPECT_TRUE(matches("typedef int X; X a;", |
| 897 | varDecl(hasName("a"), |
| 898 | hasType(typedefType(hasDeclaration(decl())))))); |
| 899 | |
| 900 | // FIXME: Add tests for other types with getDecl() (e.g. RecordType) |
| 901 | } |
| 902 | |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 903 | TEST(HasDeclaration, HasDeclarationOfTemplateSpecializationType) { |
| 904 | EXPECT_TRUE(matches("template <typename T> class A {}; A<int> a;", |
| 905 | varDecl(hasType(templateSpecializationType( |
| 906 | hasDeclaration(namedDecl(hasName("A")))))))); |
| 907 | } |
| 908 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 909 | TEST(HasType, TakesQualTypeMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 910 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 911 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 912 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 913 | EXPECT_TRUE( |
| 914 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 915 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 916 | EXPECT_TRUE( |
| 917 | matches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 918 | expr(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | TEST(HasType, TakesQualTypeMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 922 | TypeMatcher ClassX = hasDeclaration(recordDecl(hasName("X"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 923 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 924 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 925 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 926 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 927 | EXPECT_TRUE( |
| 928 | matches("class X {}; void y() { X *x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 929 | varDecl(hasType(pointsTo(ClassX))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | TEST(HasType, TakesDeclMatcherAndMatchesExpr) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 933 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 934 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 935 | matches("class X {}; void y(X &x) { x; }", expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 936 | EXPECT_TRUE( |
| 937 | notMatches("class X {}; void y(X *x) { x; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 938 | expr(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | TEST(HasType, TakesDeclMatcherAndMatchesValueDecl) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 942 | DeclarationMatcher ClassX = recordDecl(hasName("X")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 943 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 944 | matches("class X {}; void y() { X x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 945 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 946 | notMatches("class X {}; void y() { X *x; }", varDecl(hasType(ClassX)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Manuel Klimek | 1a68afd | 2013-06-20 13:08:29 +0000 | [diff] [blame] | 949 | TEST(HasTypeLoc, MatchesDeclaratorDecls) { |
| 950 | EXPECT_TRUE(matches("int x;", |
| 951 | varDecl(hasName("x"), hasTypeLoc(loc(asString("int")))))); |
| 952 | |
| 953 | // Make sure we don't crash on implicit constructors. |
| 954 | EXPECT_TRUE(notMatches("class X {}; X x;", |
| 955 | declaratorDecl(hasTypeLoc(loc(asString("int")))))); |
| 956 | } |
| 957 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 958 | TEST(Matcher, Call) { |
| 959 | // FIXME: Do we want to overload Call() to directly take |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 960 | // Matcher<Decl>, too? |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 961 | StatementMatcher MethodX = callExpr(hasDeclaration(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 962 | |
| 963 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", MethodX)); |
| 964 | EXPECT_TRUE(notMatches("class Y { void x() {} };", MethodX)); |
| 965 | |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 966 | StatementMatcher MethodOnY = |
| 967 | memberCallExpr(on(hasType(recordDecl(hasName("Y"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 968 | |
| 969 | EXPECT_TRUE( |
| 970 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 971 | MethodOnY)); |
| 972 | EXPECT_TRUE( |
| 973 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 974 | MethodOnY)); |
| 975 | EXPECT_TRUE( |
| 976 | notMatches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 977 | MethodOnY)); |
| 978 | EXPECT_TRUE( |
| 979 | notMatches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 980 | MethodOnY)); |
| 981 | EXPECT_TRUE( |
| 982 | notMatches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 983 | MethodOnY)); |
| 984 | |
| 985 | StatementMatcher MethodOnYPointer = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 986 | memberCallExpr(on(hasType(pointsTo(recordDecl(hasName("Y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 987 | |
| 988 | EXPECT_TRUE( |
| 989 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 990 | MethodOnYPointer)); |
| 991 | EXPECT_TRUE( |
| 992 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 993 | MethodOnYPointer)); |
| 994 | EXPECT_TRUE( |
| 995 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 996 | MethodOnYPointer)); |
| 997 | EXPECT_TRUE( |
| 998 | notMatches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 999 | MethodOnYPointer)); |
| 1000 | EXPECT_TRUE( |
| 1001 | notMatches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 1002 | MethodOnYPointer)); |
| 1003 | } |
| 1004 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1005 | TEST(Matcher, Lambda) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1006 | EXPECT_TRUE(matches("auto f = [] (int i) { return i; };", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1007 | lambdaExpr())); |
| 1008 | } |
| 1009 | |
| 1010 | TEST(Matcher, ForRange) { |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 1011 | EXPECT_TRUE(matches("int as[] = { 1, 2, 3 };" |
| 1012 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1013 | forRangeStmt())); |
| 1014 | EXPECT_TRUE(notMatches("void f() { for (int i; i<5; ++i); }", |
| 1015 | forRangeStmt())); |
| 1016 | } |
| 1017 | |
| 1018 | TEST(Matcher, UserDefinedLiteral) { |
| 1019 | EXPECT_TRUE(matches("constexpr char operator \"\" _inc (const char i) {" |
| 1020 | " return i + 1;" |
| 1021 | "}" |
| 1022 | "char c = 'a'_inc;", |
| 1023 | userDefinedLiteral())); |
| 1024 | } |
| 1025 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 1026 | TEST(Matcher, FlowControl) { |
| 1027 | EXPECT_TRUE(matches("void f() { while(true) { break; } }", breakStmt())); |
| 1028 | EXPECT_TRUE(matches("void f() { while(true) { continue; } }", |
| 1029 | continueStmt())); |
| 1030 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", gotoStmt())); |
| 1031 | EXPECT_TRUE(matches("void f() { goto FOO; FOO: ;}", labelStmt())); |
| 1032 | EXPECT_TRUE(matches("void f() { return; }", returnStmt())); |
| 1033 | } |
| 1034 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1035 | TEST(HasType, MatchesAsString) { |
| 1036 | EXPECT_TRUE( |
| 1037 | matches("class Y { public: void x(); }; void z() {Y* y; y->x(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1038 | memberCallExpr(on(hasType(asString("class Y *")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1039 | EXPECT_TRUE(matches("class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1040 | methodDecl(hasParameter(0, hasType(asString("int")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1041 | EXPECT_TRUE(matches("namespace ns { struct A {}; } struct B { ns::A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1042 | fieldDecl(hasType(asString("ns::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1043 | EXPECT_TRUE(matches("namespace { struct A {}; } struct B { A a; };", |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1044 | fieldDecl(hasType(asString("struct (anonymous namespace)::A"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1047 | TEST(Matcher, OverloadedOperatorCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1048 | StatementMatcher OpCall = operatorCallExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1049 | // Unary operator |
| 1050 | EXPECT_TRUE(matches("class Y { }; " |
| 1051 | "bool operator!(Y x) { return false; }; " |
| 1052 | "Y y; bool c = !y;", OpCall)); |
| 1053 | // No match -- special operators like "new", "delete" |
| 1054 | // FIXME: operator new takes size_t, for which we need stddef.h, for which |
| 1055 | // we need to figure out include paths in the test. |
| 1056 | // EXPECT_TRUE(NotMatches("#include <stddef.h>\n" |
| 1057 | // "class Y { }; " |
| 1058 | // "void *operator new(size_t size) { return 0; } " |
| 1059 | // "Y *y = new Y;", OpCall)); |
| 1060 | EXPECT_TRUE(notMatches("class Y { }; " |
| 1061 | "void operator delete(void *p) { } " |
| 1062 | "void a() {Y *y = new Y; delete y;}", OpCall)); |
| 1063 | // Binary operator |
| 1064 | EXPECT_TRUE(matches("class Y { }; " |
| 1065 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1066 | "Y a; Y b; bool c = a && b;", |
| 1067 | OpCall)); |
| 1068 | // No match -- normal operator, not an overloaded one. |
| 1069 | EXPECT_TRUE(notMatches("bool x = true, y = true; bool t = x && y;", OpCall)); |
| 1070 | EXPECT_TRUE(notMatches("int t = 5 << 2;", OpCall)); |
| 1071 | } |
| 1072 | |
| 1073 | TEST(Matcher, HasOperatorNameForOverloadedOperatorCall) { |
| 1074 | StatementMatcher OpCallAndAnd = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1075 | operatorCallExpr(hasOverloadedOperatorName("&&")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1076 | EXPECT_TRUE(matches("class Y { }; " |
| 1077 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1078 | "Y a; Y b; bool c = a && b;", OpCallAndAnd)); |
| 1079 | StatementMatcher OpCallLessLess = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1080 | operatorCallExpr(hasOverloadedOperatorName("<<")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1081 | EXPECT_TRUE(notMatches("class Y { }; " |
| 1082 | "bool operator&&(Y x, Y y) { return true; }; " |
| 1083 | "Y a; Y b; bool c = a && b;", |
| 1084 | OpCallLessLess)); |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 1085 | DeclarationMatcher ClassWithOpStar = |
| 1086 | recordDecl(hasMethod(hasOverloadedOperatorName("*"))); |
| 1087 | EXPECT_TRUE(matches("class Y { int operator*(); };", |
| 1088 | ClassWithOpStar)); |
| 1089 | EXPECT_TRUE(notMatches("class Y { void myOperator(); };", |
| 1090 | ClassWithOpStar)) ; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Daniel Jasper | 278057f | 2012-11-15 03:29:05 +0000 | [diff] [blame] | 1093 | TEST(Matcher, NestedOverloadedOperatorCalls) { |
| 1094 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 1095 | "class Y { }; " |
| 1096 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1097 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1098 | operatorCallExpr(hasOverloadedOperatorName("&&")).bind("x"), |
| 1099 | new VerifyIdIsBoundTo<CXXOperatorCallExpr>("x", 2))); |
| 1100 | EXPECT_TRUE(matches( |
| 1101 | "class Y { }; " |
| 1102 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1103 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1104 | operatorCallExpr(hasParent(operatorCallExpr())))); |
| 1105 | EXPECT_TRUE(matches( |
| 1106 | "class Y { }; " |
| 1107 | "Y& operator&&(Y& x, Y& y) { return x; }; " |
| 1108 | "Y a; Y b; Y c; Y d = a && b && c;", |
| 1109 | operatorCallExpr(hasDescendant(operatorCallExpr())))); |
| 1110 | } |
| 1111 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1112 | TEST(Matcher, ThisPointerType) { |
Manuel Klimek | 9f17408 | 2012-07-24 13:37:29 +0000 | [diff] [blame] | 1113 | StatementMatcher MethodOnY = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1114 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1115 | |
| 1116 | EXPECT_TRUE( |
| 1117 | matches("class Y { public: void x(); }; void z() { Y y; y.x(); }", |
| 1118 | MethodOnY)); |
| 1119 | EXPECT_TRUE( |
| 1120 | matches("class Y { public: void x(); }; void z(Y &y) { y.x(); }", |
| 1121 | MethodOnY)); |
| 1122 | EXPECT_TRUE( |
| 1123 | matches("class Y { public: void x(); }; void z(Y *&y) { y->x(); }", |
| 1124 | MethodOnY)); |
| 1125 | EXPECT_TRUE( |
| 1126 | matches("class Y { public: void x(); }; void z(Y y[]) { y->x(); }", |
| 1127 | MethodOnY)); |
| 1128 | EXPECT_TRUE( |
| 1129 | matches("class Y { public: void x(); }; void z() { Y *y; y->x(); }", |
| 1130 | MethodOnY)); |
| 1131 | |
| 1132 | EXPECT_TRUE(matches( |
| 1133 | "class Y {" |
| 1134 | " public: virtual void x();" |
| 1135 | "};" |
| 1136 | "class X : public Y {" |
| 1137 | " public: virtual void x();" |
| 1138 | "};" |
| 1139 | "void z() { X *x; x->Y::x(); }", MethodOnY)); |
| 1140 | } |
| 1141 | |
| 1142 | TEST(Matcher, VariableUsage) { |
| 1143 | StatementMatcher Reference = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1144 | declRefExpr(to( |
| 1145 | varDecl(hasInitializer( |
| 1146 | memberCallExpr(thisPointerType(recordDecl(hasName("Y")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1147 | |
| 1148 | EXPECT_TRUE(matches( |
| 1149 | "class Y {" |
| 1150 | " public:" |
| 1151 | " bool x() const;" |
| 1152 | "};" |
| 1153 | "void z(const Y &y) {" |
| 1154 | " bool b = y.x();" |
| 1155 | " if (b) {}" |
| 1156 | "}", Reference)); |
| 1157 | |
| 1158 | EXPECT_TRUE(notMatches( |
| 1159 | "class Y {" |
| 1160 | " public:" |
| 1161 | " bool x() const;" |
| 1162 | "};" |
| 1163 | "void z(const Y &y) {" |
| 1164 | " bool b = y.x();" |
| 1165 | "}", Reference)); |
| 1166 | } |
| 1167 | |
Manuel Klimek | 8cb9bf5 | 2012-12-04 14:42:08 +0000 | [diff] [blame] | 1168 | TEST(Matcher, FindsVarDeclInFunctionParameter) { |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1169 | EXPECT_TRUE(matches( |
| 1170 | "void f(int i) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1171 | varDecl(hasName("i")))); |
Daniel Jasper | 9bd2809 | 2012-07-30 05:03:25 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1174 | TEST(Matcher, CalledVariable) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1175 | StatementMatcher CallOnVariableY = |
| 1176 | memberCallExpr(on(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1177 | |
| 1178 | EXPECT_TRUE(matches( |
| 1179 | "class Y { public: void x() { Y y; y.x(); } };", CallOnVariableY)); |
| 1180 | EXPECT_TRUE(matches( |
| 1181 | "class Y { public: void x() const { Y y; y.x(); } };", CallOnVariableY)); |
| 1182 | EXPECT_TRUE(matches( |
| 1183 | "class Y { public: void x(); };" |
| 1184 | "class X : public Y { void z() { X y; y.x(); } };", CallOnVariableY)); |
| 1185 | EXPECT_TRUE(matches( |
| 1186 | "class Y { public: void x(); };" |
| 1187 | "class X : public Y { void z() { X *y; y->x(); } };", CallOnVariableY)); |
| 1188 | EXPECT_TRUE(notMatches( |
| 1189 | "class Y { public: void x(); };" |
| 1190 | "class X : public Y { void z() { unsigned long y; ((X*)y)->x(); } };", |
| 1191 | CallOnVariableY)); |
| 1192 | } |
| 1193 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1194 | TEST(UnaryExprOrTypeTraitExpr, MatchesSizeOfAndAlignOf) { |
| 1195 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", |
| 1196 | unaryExprOrTypeTraitExpr())); |
| 1197 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", |
| 1198 | alignOfExpr(anything()))); |
| 1199 | // FIXME: Uncomment once alignof is enabled. |
| 1200 | // EXPECT_TRUE(matches("void x() { int a = alignof(a); }", |
| 1201 | // unaryExprOrTypeTraitExpr())); |
| 1202 | // EXPECT_TRUE(notMatches("void x() { int a = alignof(a); }", |
| 1203 | // sizeOfExpr())); |
| 1204 | } |
| 1205 | |
| 1206 | TEST(UnaryExpressionOrTypeTraitExpression, MatchesCorrectType) { |
| 1207 | EXPECT_TRUE(matches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1208 | hasArgumentOfType(asString("int"))))); |
| 1209 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
| 1210 | hasArgumentOfType(asString("float"))))); |
| 1211 | EXPECT_TRUE(matches( |
| 1212 | "struct A {}; void x() { A a; int b = sizeof(a); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1213 | sizeOfExpr(hasArgumentOfType(hasDeclaration(recordDecl(hasName("A"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1214 | EXPECT_TRUE(notMatches("void x() { int a = sizeof(a); }", sizeOfExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1215 | hasArgumentOfType(hasDeclaration(recordDecl(hasName("string"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1218 | TEST(MemberExpression, DoesNotMatchClasses) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1219 | EXPECT_TRUE(notMatches("class Y { void x() {} };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | TEST(MemberExpression, MatchesMemberFunctionCall) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1223 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | TEST(MemberExpression, MatchesVariable) { |
| 1227 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1228 | matches("class Y { void x() { this->y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1229 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1230 | matches("class Y { void x() { y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1231 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1232 | matches("class Y { void x() { Y y; y.y; } int y; };", memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | TEST(MemberExpression, MatchesStaticVariable) { |
| 1236 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1237 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1238 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1239 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1240 | EXPECT_TRUE(notMatches("class Y { void x() { Y::y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1241 | memberExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1244 | TEST(IsInteger, MatchesIntegers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1245 | EXPECT_TRUE(matches("int i = 0;", varDecl(hasType(isInteger())))); |
| 1246 | EXPECT_TRUE(matches( |
| 1247 | "long long i = 0; void f(long long) { }; void g() {f(i);}", |
| 1248 | callExpr(hasArgument(0, declRefExpr( |
| 1249 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | TEST(IsInteger, ReportsNoFalsePositives) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1253 | EXPECT_TRUE(notMatches("int *i;", varDecl(hasType(isInteger())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1254 | 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] | 1255 | callExpr(hasArgument(0, declRefExpr( |
| 1256 | to(varDecl(hasType(isInteger())))))))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1259 | TEST(IsArrow, MatchesMemberVariablesViaArrow) { |
| 1260 | EXPECT_TRUE(matches("class Y { void x() { this->y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1261 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1262 | EXPECT_TRUE(matches("class Y { void x() { y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1263 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1264 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1265 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | TEST(IsArrow, MatchesStaticMemberVariablesViaArrow) { |
| 1269 | EXPECT_TRUE(matches("class Y { void x() { this->y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1270 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1271 | EXPECT_TRUE(notMatches("class Y { void x() { y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1272 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1273 | EXPECT_TRUE(notMatches("class Y { void x() { (*this).y; } static int y; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1274 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | TEST(IsArrow, MatchesMemberCallsViaArrow) { |
| 1278 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1279 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1280 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1281 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1282 | EXPECT_TRUE(notMatches("class Y { void x() { Y y; y.x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1283 | memberExpr(isArrow()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | TEST(Callee, MatchesDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1287 | StatementMatcher CallMethodX = callExpr(callee(methodDecl(hasName("x")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1288 | |
| 1289 | EXPECT_TRUE(matches("class Y { void x() { x(); } };", CallMethodX)); |
| 1290 | EXPECT_TRUE(notMatches("class Y { void x() {} };", CallMethodX)); |
| 1291 | } |
| 1292 | |
| 1293 | TEST(Callee, MatchesMemberExpressions) { |
| 1294 | EXPECT_TRUE(matches("class Y { void x() { this->x(); } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1295 | callExpr(callee(memberExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1296 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1297 | notMatches("class Y { void x() { this->x(); } };", callExpr(callee(callExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | TEST(Function, MatchesFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1301 | StatementMatcher CallFunctionF = callExpr(callee(functionDecl(hasName("f")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1302 | |
| 1303 | EXPECT_TRUE(matches("void f() { f(); }", CallFunctionF)); |
| 1304 | EXPECT_TRUE(notMatches("void f() { }", CallFunctionF)); |
| 1305 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1306 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() != |
| 1307 | llvm::Triple::Win32) { |
| 1308 | // FIXME: Make this work for MSVC. |
| 1309 | // Dependent contexts, but a non-dependent call. |
| 1310 | EXPECT_TRUE(matches("void f(); template <int N> void g() { f(); }", |
| 1311 | CallFunctionF)); |
| 1312 | EXPECT_TRUE( |
| 1313 | matches("void f(); template <int N> struct S { void g() { f(); } };", |
| 1314 | CallFunctionF)); |
| 1315 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1316 | |
| 1317 | // Depedent calls don't match. |
| 1318 | EXPECT_TRUE( |
| 1319 | notMatches("void f(int); template <typename T> void g(T t) { f(t); }", |
| 1320 | CallFunctionF)); |
| 1321 | EXPECT_TRUE( |
| 1322 | notMatches("void f(int);" |
| 1323 | "template <typename T> struct S { void g(T t) { f(t); } };", |
| 1324 | CallFunctionF)); |
| 1325 | } |
| 1326 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1327 | TEST(FunctionTemplate, MatchesFunctionTemplateDeclarations) { |
| 1328 | EXPECT_TRUE( |
| 1329 | matches("template <typename T> void f(T t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1330 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | TEST(FunctionTemplate, DoesNotMatchFunctionDeclarations) { |
| 1334 | EXPECT_TRUE( |
| 1335 | notMatches("void f(double d); void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1336 | functionTemplateDecl(hasName("f")))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | TEST(FunctionTemplate, DoesNotMatchFunctionTemplateSpecializations) { |
| 1340 | EXPECT_TRUE( |
| 1341 | notMatches("void g(); template <typename T> void f(T t) {}" |
| 1342 | "template <> void f(int t) { g(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1343 | functionTemplateDecl(hasName("f"), |
| 1344 | hasDescendant(declRefExpr(to( |
| 1345 | functionDecl(hasName("g")))))))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1348 | TEST(Matcher, Argument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1349 | StatementMatcher CallArgumentY = callExpr( |
| 1350 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1351 | |
| 1352 | EXPECT_TRUE(matches("void x(int) { int y; x(y); }", CallArgumentY)); |
| 1353 | EXPECT_TRUE( |
| 1354 | matches("class X { void x(int) { int y; x(y); } };", CallArgumentY)); |
| 1355 | EXPECT_TRUE(notMatches("void x(int) { int z; x(z); }", CallArgumentY)); |
| 1356 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1357 | StatementMatcher WrongIndex = callExpr( |
| 1358 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1359 | EXPECT_TRUE(notMatches("void x(int) { int y; x(y); }", WrongIndex)); |
| 1360 | } |
| 1361 | |
| 1362 | TEST(Matcher, AnyArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1363 | StatementMatcher CallArgumentY = callExpr( |
| 1364 | hasAnyArgument(declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1365 | EXPECT_TRUE(matches("void x(int, int) { int y; x(1, y); }", CallArgumentY)); |
| 1366 | EXPECT_TRUE(matches("void x(int, int) { int y; x(y, 42); }", CallArgumentY)); |
| 1367 | EXPECT_TRUE(notMatches("void x(int, int) { x(1, 2); }", CallArgumentY)); |
| 1368 | } |
| 1369 | |
| 1370 | TEST(Matcher, ArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1371 | StatementMatcher Call1Arg = callExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1372 | |
| 1373 | EXPECT_TRUE(matches("void x(int) { x(0); }", Call1Arg)); |
| 1374 | EXPECT_TRUE(matches("class X { void x(int) { x(0); } };", Call1Arg)); |
| 1375 | EXPECT_TRUE(notMatches("void x(int, int) { x(0, 0); }", Call1Arg)); |
| 1376 | } |
| 1377 | |
Daniel Jasper | 36e29d6 | 2012-12-04 11:54:27 +0000 | [diff] [blame] | 1378 | TEST(Matcher, ParameterCount) { |
| 1379 | DeclarationMatcher Function1Arg = functionDecl(parameterCountIs(1)); |
| 1380 | EXPECT_TRUE(matches("void f(int i) {}", Function1Arg)); |
| 1381 | EXPECT_TRUE(matches("class X { void f(int i) {} };", Function1Arg)); |
| 1382 | EXPECT_TRUE(notMatches("void f() {}", Function1Arg)); |
| 1383 | EXPECT_TRUE(notMatches("void f(int i, int j, int k) {}", Function1Arg)); |
| 1384 | } |
| 1385 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1386 | TEST(Matcher, References) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1387 | DeclarationMatcher ReferenceClassX = varDecl( |
| 1388 | hasType(references(recordDecl(hasName("X"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1389 | EXPECT_TRUE(matches("class X {}; void y(X y) { X &x = y; }", |
| 1390 | ReferenceClassX)); |
| 1391 | EXPECT_TRUE( |
| 1392 | matches("class X {}; void y(X y) { const X &x = y; }", ReferenceClassX)); |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 1393 | // The match here is on the implicit copy constructor code for |
| 1394 | // class X, not on code 'X x = y'. |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1395 | EXPECT_TRUE( |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 1396 | matches("class X {}; void y(X y) { X x = y; }", ReferenceClassX)); |
| 1397 | EXPECT_TRUE( |
| 1398 | notMatches("class X {}; extern X x;", ReferenceClassX)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1399 | EXPECT_TRUE( |
| 1400 | notMatches("class X {}; void y(X *y) { X *&x = y; }", ReferenceClassX)); |
| 1401 | } |
| 1402 | |
Edwin Vane | 6a19a97 | 2013-03-06 17:02:57 +0000 | [diff] [blame] | 1403 | TEST(QualType, hasCanonicalType) { |
| 1404 | EXPECT_TRUE(notMatches("typedef int &int_ref;" |
| 1405 | "int a;" |
| 1406 | "int_ref b = a;", |
| 1407 | varDecl(hasType(qualType(referenceType()))))); |
| 1408 | EXPECT_TRUE( |
| 1409 | matches("typedef int &int_ref;" |
| 1410 | "int a;" |
| 1411 | "int_ref b = a;", |
| 1412 | varDecl(hasType(qualType(hasCanonicalType(referenceType())))))); |
| 1413 | } |
| 1414 | |
Edwin Vane | 7b69cd0 | 2013-04-02 18:15:55 +0000 | [diff] [blame] | 1415 | TEST(QualType, hasLocalQualifiers) { |
| 1416 | EXPECT_TRUE(notMatches("typedef const int const_int; const_int i = 1;", |
| 1417 | varDecl(hasType(hasLocalQualifiers())))); |
| 1418 | EXPECT_TRUE(matches("int *const j = nullptr;", |
| 1419 | varDecl(hasType(hasLocalQualifiers())))); |
| 1420 | EXPECT_TRUE(matches("int *volatile k;", |
| 1421 | varDecl(hasType(hasLocalQualifiers())))); |
| 1422 | EXPECT_TRUE(notMatches("int m;", |
| 1423 | varDecl(hasType(hasLocalQualifiers())))); |
| 1424 | } |
| 1425 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1426 | TEST(HasParameter, CallsInnerMatcher) { |
| 1427 | EXPECT_TRUE(matches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1428 | methodDecl(hasParameter(0, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1429 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1430 | methodDecl(hasParameter(0, hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | TEST(HasParameter, DoesNotMatchIfIndexOutOfBounds) { |
| 1434 | EXPECT_TRUE(notMatches("class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1435 | methodDecl(hasParameter(42, varDecl())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | TEST(HasType, MatchesParameterVariableTypesStrictly) { |
| 1439 | EXPECT_TRUE(matches("class X { void x(X x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1440 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1441 | EXPECT_TRUE(notMatches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1442 | methodDecl(hasParameter(0, hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1443 | EXPECT_TRUE(matches("class X { void x(const X *x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1444 | methodDecl(hasParameter(0, |
| 1445 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1446 | EXPECT_TRUE(matches("class X { void x(const X &x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1447 | methodDecl(hasParameter(0, |
| 1448 | hasType(references(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | TEST(HasAnyParameter, MatchesIndependentlyOfPosition) { |
| 1452 | 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] | 1453 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1454 | 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] | 1455 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1458 | TEST(Returns, MatchesReturnTypes) { |
| 1459 | EXPECT_TRUE(matches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1460 | functionDecl(returns(asString("int"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1461 | EXPECT_TRUE(notMatches("class Y { int f() { return 1; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1462 | functionDecl(returns(asString("float"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1463 | EXPECT_TRUE(matches("class Y { Y getMe() { return *this; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1464 | functionDecl(returns(hasDeclaration( |
| 1465 | recordDecl(hasName("Y"))))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1468 | TEST(IsExternC, MatchesExternCFunctionDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1469 | EXPECT_TRUE(matches("extern \"C\" void f() {}", functionDecl(isExternC()))); |
| 1470 | EXPECT_TRUE(matches("extern \"C\" { void f() {} }", |
| 1471 | functionDecl(isExternC()))); |
| 1472 | EXPECT_TRUE(notMatches("void f() {}", functionDecl(isExternC()))); |
Daniel Jasper | 8cc7efa | 2012-08-15 18:52:19 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1475 | TEST(HasAnyParameter, DoesntMatchIfInnerMatcherDoesntMatch) { |
| 1476 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1477 | methodDecl(hasAnyParameter(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | TEST(HasAnyParameter, DoesNotMatchThisPointer) { |
| 1481 | EXPECT_TRUE(notMatches("class Y {}; class X { void x() {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1482 | methodDecl(hasAnyParameter(hasType(pointsTo( |
| 1483 | recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1486 | TEST(HasName, MatchesParameterVariableDeclarations) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1487 | EXPECT_TRUE(matches("class Y {}; class X { void x(int x) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1488 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1489 | EXPECT_TRUE(notMatches("class Y {}; class X { void x(int) {} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1490 | methodDecl(hasAnyParameter(hasName("x"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1493 | TEST(Matcher, MatchesClassTemplateSpecialization) { |
| 1494 | EXPECT_TRUE(matches("template<typename T> struct A {};" |
| 1495 | "template<> struct A<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1496 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1497 | EXPECT_TRUE(matches("template<typename T> struct A {}; A<int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1498 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1499 | EXPECT_TRUE(notMatches("template<typename T> struct A {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1500 | classTemplateSpecializationDecl())); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Manuel Klimek | 1a68afd | 2013-06-20 13:08:29 +0000 | [diff] [blame] | 1503 | TEST(DeclaratorDecl, MatchesDeclaratorDecls) { |
| 1504 | EXPECT_TRUE(matches("int x;", declaratorDecl())); |
| 1505 | EXPECT_TRUE(notMatches("class A {};", declaratorDecl())); |
| 1506 | } |
| 1507 | |
| 1508 | TEST(ParmVarDecl, MatchesParmVars) { |
| 1509 | EXPECT_TRUE(matches("void f(int x);", parmVarDecl())); |
| 1510 | EXPECT_TRUE(notMatches("void f();", parmVarDecl())); |
| 1511 | } |
| 1512 | |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1513 | TEST(Matcher, MatchesTypeTemplateArgument) { |
| 1514 | EXPECT_TRUE(matches( |
| 1515 | "template<typename T> struct B {};" |
| 1516 | "B<int> b;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1517 | classTemplateSpecializationDecl(hasAnyTemplateArgument(refersToType( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1518 | asString("int")))))); |
| 1519 | } |
| 1520 | |
| 1521 | TEST(Matcher, MatchesDeclarationReferenceTemplateArgument) { |
| 1522 | EXPECT_TRUE(matches( |
| 1523 | "struct B { int next; };" |
| 1524 | "template<int(B::*next_ptr)> struct A {};" |
| 1525 | "A<&B::next> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1526 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1527 | refersToDeclaration(fieldDecl(hasName("next"))))))); |
Daniel Jasper | aaa8e45 | 2012-09-29 15:55:18 +0000 | [diff] [blame] | 1528 | |
| 1529 | EXPECT_TRUE(notMatches( |
| 1530 | "template <typename T> struct A {};" |
| 1531 | "A<int> a;", |
| 1532 | classTemplateSpecializationDecl(hasAnyTemplateArgument( |
| 1533 | refersToDeclaration(decl()))))); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1534 | |
| 1535 | EXPECT_TRUE(matches( |
| 1536 | "struct B { int next; };" |
| 1537 | "template<int(B::*next_ptr)> struct A {};" |
| 1538 | "A<&B::next> a;", |
| 1539 | templateSpecializationType(hasAnyTemplateArgument(isExpr( |
| 1540 | hasDescendant(declRefExpr(to(fieldDecl(hasName("next")))))))))); |
| 1541 | |
| 1542 | EXPECT_TRUE(notMatches( |
| 1543 | "template <typename T> struct A {};" |
| 1544 | "A<int> a;", |
| 1545 | templateSpecializationType(hasAnyTemplateArgument( |
| 1546 | refersToDeclaration(decl()))))); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | TEST(Matcher, MatchesSpecificArgument) { |
| 1550 | EXPECT_TRUE(matches( |
| 1551 | "template<typename T, typename U> class A {};" |
| 1552 | "A<bool, int> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1553 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1554 | 1, refersToType(asString("int")))))); |
| 1555 | EXPECT_TRUE(notMatches( |
| 1556 | "template<typename T, typename U> class A {};" |
| 1557 | "A<int, bool> a;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1558 | classTemplateSpecializationDecl(hasTemplateArgument( |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1559 | 1, refersToType(asString("int")))))); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1560 | |
| 1561 | EXPECT_TRUE(matches( |
| 1562 | "template<typename T, typename U> class A {};" |
| 1563 | "A<bool, int> a;", |
| 1564 | templateSpecializationType(hasTemplateArgument( |
| 1565 | 1, refersToType(asString("int")))))); |
| 1566 | EXPECT_TRUE(notMatches( |
| 1567 | "template<typename T, typename U> class A {};" |
| 1568 | "A<int, bool> a;", |
| 1569 | templateSpecializationType(hasTemplateArgument( |
| 1570 | 1, refersToType(asString("int")))))); |
Daniel Jasper | 371f939 | 2012-08-01 08:40:24 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Daniel Jasper | f3197e9 | 2013-02-25 12:02:08 +0000 | [diff] [blame] | 1573 | TEST(Matcher, MatchesAccessSpecDecls) { |
| 1574 | EXPECT_TRUE(matches("class C { public: int i; };", accessSpecDecl())); |
| 1575 | EXPECT_TRUE( |
| 1576 | matches("class C { public: int i; };", accessSpecDecl(isPublic()))); |
| 1577 | EXPECT_TRUE( |
| 1578 | notMatches("class C { public: int i; };", accessSpecDecl(isProtected()))); |
| 1579 | EXPECT_TRUE( |
| 1580 | notMatches("class C { public: int i; };", accessSpecDecl(isPrivate()))); |
| 1581 | |
| 1582 | EXPECT_TRUE(notMatches("class C { int i; };", accessSpecDecl())); |
| 1583 | } |
| 1584 | |
Edwin Vane | 5771a2f | 2013-04-09 20:46:36 +0000 | [diff] [blame] | 1585 | TEST(Matcher, MatchesVirtualMethod) { |
| 1586 | EXPECT_TRUE(matches("class X { virtual int f(); };", |
| 1587 | methodDecl(isVirtual(), hasName("::X::f")))); |
| 1588 | EXPECT_TRUE(notMatches("class X { int f(); };", |
| 1589 | methodDecl(isVirtual()))); |
| 1590 | } |
| 1591 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1592 | TEST(Matcher, MatchesPureMethod) { |
| 1593 | EXPECT_TRUE(matches("class X { virtual int f() = 0; };", |
| 1594 | methodDecl(isPure(), hasName("::X::f")))); |
| 1595 | EXPECT_TRUE(notMatches("class X { int f(); };", |
| 1596 | methodDecl(isPure()))); |
| 1597 | } |
| 1598 | |
Edwin Vane | 32a6ebc | 2013-05-09 17:00:17 +0000 | [diff] [blame] | 1599 | TEST(Matcher, MatchesConstMethod) { |
| 1600 | EXPECT_TRUE(matches("struct A { void foo() const; };", |
| 1601 | methodDecl(isConst()))); |
| 1602 | EXPECT_TRUE(notMatches("struct A { void foo(); };", |
| 1603 | methodDecl(isConst()))); |
| 1604 | } |
| 1605 | |
Edwin Vane | 5771a2f | 2013-04-09 20:46:36 +0000 | [diff] [blame] | 1606 | TEST(Matcher, MatchesOverridingMethod) { |
| 1607 | EXPECT_TRUE(matches("class X { virtual int f(); }; " |
| 1608 | "class Y : public X { int f(); };", |
| 1609 | methodDecl(isOverride(), hasName("::Y::f")))); |
| 1610 | EXPECT_TRUE(notMatches("class X { virtual int f(); }; " |
| 1611 | "class Y : public X { int f(); };", |
| 1612 | methodDecl(isOverride(), hasName("::X::f")))); |
| 1613 | EXPECT_TRUE(notMatches("class X { int f(); }; " |
| 1614 | "class Y : public X { int f(); };", |
| 1615 | methodDecl(isOverride()))); |
| 1616 | EXPECT_TRUE(notMatches("class X { int f(); int f(int); }; ", |
| 1617 | methodDecl(isOverride()))); |
| 1618 | } |
| 1619 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1620 | TEST(Matcher, ConstructorCall) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1621 | StatementMatcher Constructor = constructExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1622 | |
| 1623 | EXPECT_TRUE( |
| 1624 | matches("class X { public: X(); }; void x() { X x; }", Constructor)); |
| 1625 | EXPECT_TRUE( |
| 1626 | matches("class X { public: X(); }; void x() { X x = X(); }", |
| 1627 | Constructor)); |
| 1628 | EXPECT_TRUE( |
| 1629 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1630 | Constructor)); |
| 1631 | EXPECT_TRUE(matches("class X {}; void x(int) { X x; }", Constructor)); |
| 1632 | } |
| 1633 | |
| 1634 | TEST(Matcher, ConstructorArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1635 | StatementMatcher Constructor = constructExpr( |
| 1636 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1637 | |
| 1638 | EXPECT_TRUE( |
| 1639 | matches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1640 | Constructor)); |
| 1641 | EXPECT_TRUE( |
| 1642 | matches("class X { public: X(int); }; void x() { int y; X x = X(y); }", |
| 1643 | Constructor)); |
| 1644 | EXPECT_TRUE( |
| 1645 | matches("class X { public: X(int); }; void x() { int y; X x = y; }", |
| 1646 | Constructor)); |
| 1647 | EXPECT_TRUE( |
| 1648 | notMatches("class X { public: X(int); }; void x() { int z; X x(z); }", |
| 1649 | Constructor)); |
| 1650 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1651 | StatementMatcher WrongIndex = constructExpr( |
| 1652 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1653 | EXPECT_TRUE( |
| 1654 | notMatches("class X { public: X(int); }; void x() { int y; X x(y); }", |
| 1655 | WrongIndex)); |
| 1656 | } |
| 1657 | |
| 1658 | TEST(Matcher, ConstructorArgumentCount) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1659 | StatementMatcher Constructor1Arg = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1660 | |
| 1661 | EXPECT_TRUE( |
| 1662 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1663 | Constructor1Arg)); |
| 1664 | EXPECT_TRUE( |
| 1665 | matches("class X { public: X(int); }; void x() { X x = X(0); }", |
| 1666 | Constructor1Arg)); |
| 1667 | EXPECT_TRUE( |
| 1668 | matches("class X { public: X(int); }; void x() { X x = 0; }", |
| 1669 | Constructor1Arg)); |
| 1670 | EXPECT_TRUE( |
| 1671 | notMatches("class X { public: X(int, int); }; void x() { X x(0, 0); }", |
| 1672 | Constructor1Arg)); |
| 1673 | } |
| 1674 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1675 | TEST(Matcher, ConstructorListInitialization) { |
| 1676 | StatementMatcher ConstructorListInit = constructExpr(isListInitialization()); |
| 1677 | |
| 1678 | EXPECT_TRUE( |
| 1679 | matches("class X { public: X(int); }; void x() { X x{0}; }", |
| 1680 | ConstructorListInit)); |
| 1681 | EXPECT_FALSE( |
| 1682 | matches("class X { public: X(int); }; void x() { X x(0); }", |
| 1683 | ConstructorListInit)); |
| 1684 | } |
| 1685 | |
Manuel Klimek | 70b9db9 | 2012-10-23 10:40:50 +0000 | [diff] [blame] | 1686 | TEST(Matcher,ThisExpr) { |
| 1687 | EXPECT_TRUE( |
| 1688 | matches("struct X { int a; int f () { return a; } };", thisExpr())); |
| 1689 | EXPECT_TRUE( |
| 1690 | notMatches("struct X { int f () { int a; return a; } };", thisExpr())); |
| 1691 | } |
| 1692 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1693 | TEST(Matcher, BindTemporaryExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1694 | StatementMatcher TempExpression = bindTemporaryExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1695 | |
| 1696 | std::string ClassString = "class string { public: string(); ~string(); }; "; |
| 1697 | |
| 1698 | EXPECT_TRUE( |
| 1699 | matches(ClassString + |
| 1700 | "string GetStringByValue();" |
| 1701 | "void FunctionTakesString(string s);" |
| 1702 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1703 | TempExpression)); |
| 1704 | |
| 1705 | EXPECT_TRUE( |
| 1706 | notMatches(ClassString + |
| 1707 | "string* GetStringPointer(); " |
| 1708 | "void FunctionTakesStringPtr(string* s);" |
| 1709 | "void run() {" |
| 1710 | " string* s = GetStringPointer();" |
| 1711 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1712 | " FunctionTakesStringPtr(s);" |
| 1713 | "}", |
| 1714 | TempExpression)); |
| 1715 | |
| 1716 | EXPECT_TRUE( |
| 1717 | notMatches("class no_dtor {};" |
| 1718 | "no_dtor GetObjByValue();" |
| 1719 | "void ConsumeObj(no_dtor param);" |
| 1720 | "void run() { ConsumeObj(GetObjByValue()); }", |
| 1721 | TempExpression)); |
| 1722 | } |
| 1723 | |
Sam Panzer | e16acd3 | 2012-08-24 22:04:44 +0000 | [diff] [blame] | 1724 | TEST(MaterializeTemporaryExpr, MatchesTemporary) { |
| 1725 | std::string ClassString = |
| 1726 | "class string { public: string(); int length(); }; "; |
| 1727 | |
| 1728 | EXPECT_TRUE( |
| 1729 | matches(ClassString + |
| 1730 | "string GetStringByValue();" |
| 1731 | "void FunctionTakesString(string s);" |
| 1732 | "void run() { FunctionTakesString(GetStringByValue()); }", |
| 1733 | materializeTemporaryExpr())); |
| 1734 | |
| 1735 | EXPECT_TRUE( |
| 1736 | notMatches(ClassString + |
| 1737 | "string* GetStringPointer(); " |
| 1738 | "void FunctionTakesStringPtr(string* s);" |
| 1739 | "void run() {" |
| 1740 | " string* s = GetStringPointer();" |
| 1741 | " FunctionTakesStringPtr(GetStringPointer());" |
| 1742 | " FunctionTakesStringPtr(s);" |
| 1743 | "}", |
| 1744 | materializeTemporaryExpr())); |
| 1745 | |
| 1746 | EXPECT_TRUE( |
| 1747 | notMatches(ClassString + |
| 1748 | "string GetStringByValue();" |
| 1749 | "void run() { int k = GetStringByValue().length(); }", |
| 1750 | materializeTemporaryExpr())); |
| 1751 | |
| 1752 | EXPECT_TRUE( |
| 1753 | notMatches(ClassString + |
| 1754 | "string GetStringByValue();" |
| 1755 | "void run() { GetStringByValue(); }", |
| 1756 | materializeTemporaryExpr())); |
| 1757 | } |
| 1758 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1759 | TEST(ConstructorDeclaration, SimpleCase) { |
| 1760 | EXPECT_TRUE(matches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1761 | constructorDecl(ofClass(hasName("Foo"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1762 | EXPECT_TRUE(notMatches("class Foo { Foo(int i); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1763 | constructorDecl(ofClass(hasName("Bar"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | TEST(ConstructorDeclaration, IsImplicit) { |
| 1767 | // This one doesn't match because the constructor is not added by the |
| 1768 | // compiler (it is not needed). |
| 1769 | EXPECT_TRUE(notMatches("class Foo { };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1770 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1771 | // The compiler added the implicit default constructor. |
| 1772 | EXPECT_TRUE(matches("class Foo { }; Foo* f = new Foo();", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1773 | constructorDecl(isImplicit()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1774 | EXPECT_TRUE(matches("class Foo { Foo(){} };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1775 | constructorDecl(unless(isImplicit())))); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1776 | // The compiler added an implicit assignment operator. |
| 1777 | EXPECT_TRUE(matches("struct A { int x; } a = {0}, b = a; void f() { a = b; }", |
| 1778 | methodDecl(isImplicit(), hasName("operator=")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1781 | TEST(DestructorDeclaration, MatchesVirtualDestructor) { |
| 1782 | EXPECT_TRUE(matches("class Foo { virtual ~Foo(); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1783 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | TEST(DestructorDeclaration, DoesNotMatchImplicitDestructor) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1787 | EXPECT_TRUE(notMatches("class Foo {};", |
| 1788 | destructorDecl(ofClass(hasName("Foo"))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1791 | TEST(HasAnyConstructorInitializer, SimpleCase) { |
| 1792 | EXPECT_TRUE(notMatches( |
| 1793 | "class Foo { Foo() { } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1794 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1795 | EXPECT_TRUE(matches( |
| 1796 | "class Foo {" |
| 1797 | " Foo() : foo_() { }" |
| 1798 | " int foo_;" |
| 1799 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1800 | constructorDecl(hasAnyConstructorInitializer(anything())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | TEST(HasAnyConstructorInitializer, ForField) { |
| 1804 | static const char Code[] = |
| 1805 | "class Baz { };" |
| 1806 | "class Foo {" |
| 1807 | " Foo() : foo_() { }" |
| 1808 | " Baz foo_;" |
| 1809 | " Baz bar_;" |
| 1810 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1811 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1812 | forField(hasType(recordDecl(hasName("Baz")))))))); |
| 1813 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1814 | forField(hasName("foo_")))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1815 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
| 1816 | forField(hasType(recordDecl(hasName("Bar")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | TEST(HasAnyConstructorInitializer, WithInitializer) { |
| 1820 | static const char Code[] = |
| 1821 | "class Foo {" |
| 1822 | " Foo() : foo_(0) { }" |
| 1823 | " int foo_;" |
| 1824 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1825 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1826 | withInitializer(integerLiteral(equals(0))))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1827 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1828 | withInitializer(integerLiteral(equals(1))))))); |
| 1829 | } |
| 1830 | |
| 1831 | TEST(HasAnyConstructorInitializer, IsWritten) { |
| 1832 | static const char Code[] = |
| 1833 | "struct Bar { Bar(){} };" |
| 1834 | "class Foo {" |
| 1835 | " Foo() : foo_() { }" |
| 1836 | " Bar foo_;" |
| 1837 | " Bar bar_;" |
| 1838 | "};"; |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1839 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1840 | allOf(forField(hasName("foo_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1841 | EXPECT_TRUE(notMatches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1842 | allOf(forField(hasName("bar_")), isWritten()))))); |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1843 | EXPECT_TRUE(matches(Code, constructorDecl(hasAnyConstructorInitializer( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1844 | allOf(forField(hasName("bar_")), unless(isWritten())))))); |
| 1845 | } |
| 1846 | |
| 1847 | TEST(Matcher, NewExpression) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1848 | StatementMatcher New = newExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1849 | |
| 1850 | EXPECT_TRUE(matches("class X { public: X(); }; void x() { new X; }", New)); |
| 1851 | EXPECT_TRUE( |
| 1852 | matches("class X { public: X(); }; void x() { new X(); }", New)); |
| 1853 | EXPECT_TRUE( |
| 1854 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1855 | EXPECT_TRUE(matches("class X {}; void x(int) { new X; }", New)); |
| 1856 | } |
| 1857 | |
| 1858 | TEST(Matcher, NewExpressionArgument) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1859 | StatementMatcher New = constructExpr( |
| 1860 | hasArgument(0, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1861 | |
| 1862 | EXPECT_TRUE( |
| 1863 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1864 | New)); |
| 1865 | EXPECT_TRUE( |
| 1866 | matches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1867 | New)); |
| 1868 | EXPECT_TRUE( |
| 1869 | notMatches("class X { public: X(int); }; void x() { int z; new X(z); }", |
| 1870 | New)); |
| 1871 | |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1872 | StatementMatcher WrongIndex = constructExpr( |
| 1873 | hasArgument(42, declRefExpr(to(varDecl(hasName("y")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1874 | EXPECT_TRUE( |
| 1875 | notMatches("class X { public: X(int); }; void x() { int y; new X(y); }", |
| 1876 | WrongIndex)); |
| 1877 | } |
| 1878 | |
| 1879 | TEST(Matcher, NewExpressionArgumentCount) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1880 | StatementMatcher New = constructExpr(argumentCountIs(1)); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1881 | |
| 1882 | EXPECT_TRUE( |
| 1883 | matches("class X { public: X(int); }; void x() { new X(0); }", New)); |
| 1884 | EXPECT_TRUE( |
| 1885 | notMatches("class X { public: X(int, int); }; void x() { new X(0, 0); }", |
| 1886 | New)); |
| 1887 | } |
| 1888 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1889 | TEST(Matcher, DeleteExpression) { |
| 1890 | EXPECT_TRUE(matches("struct A {}; void f(A* a) { delete a; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1891 | deleteExpr())); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 1892 | } |
| 1893 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1894 | TEST(Matcher, DefaultArgument) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 1895 | StatementMatcher Arg = defaultArgExpr(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1896 | |
| 1897 | EXPECT_TRUE(matches("void x(int, int = 0) { int y; x(y); }", Arg)); |
| 1898 | EXPECT_TRUE( |
| 1899 | matches("class X { void x(int, int = 0) { int y; x(y); } };", Arg)); |
| 1900 | EXPECT_TRUE(notMatches("void x(int, int = 0) { int y; x(y, 0); }", Arg)); |
| 1901 | } |
| 1902 | |
| 1903 | TEST(Matcher, StringLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1904 | StatementMatcher Literal = stringLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1905 | EXPECT_TRUE(matches("const char *s = \"string\";", Literal)); |
| 1906 | // wide string |
| 1907 | EXPECT_TRUE(matches("const wchar_t *s = L\"string\";", Literal)); |
| 1908 | // with escaped characters |
| 1909 | EXPECT_TRUE(matches("const char *s = \"\x05five\";", Literal)); |
| 1910 | // no matching -- though the data type is the same, there is no string literal |
| 1911 | EXPECT_TRUE(notMatches("const char s[1] = {'a'};", Literal)); |
| 1912 | } |
| 1913 | |
| 1914 | TEST(Matcher, CharacterLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1915 | StatementMatcher CharLiteral = characterLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1916 | EXPECT_TRUE(matches("const char c = 'c';", CharLiteral)); |
| 1917 | // wide character |
| 1918 | EXPECT_TRUE(matches("const char c = L'c';", CharLiteral)); |
| 1919 | // wide character, Hex encoded, NOT MATCHED! |
| 1920 | EXPECT_TRUE(notMatches("const wchar_t c = 0x2126;", CharLiteral)); |
| 1921 | EXPECT_TRUE(notMatches("const char c = 0x1;", CharLiteral)); |
| 1922 | } |
| 1923 | |
| 1924 | TEST(Matcher, IntegerLiterals) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 1925 | StatementMatcher HasIntLiteral = integerLiteral(); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1926 | EXPECT_TRUE(matches("int i = 10;", HasIntLiteral)); |
| 1927 | EXPECT_TRUE(matches("int i = 0x1AB;", HasIntLiteral)); |
| 1928 | EXPECT_TRUE(matches("int i = 10L;", HasIntLiteral)); |
| 1929 | EXPECT_TRUE(matches("int i = 10U;", HasIntLiteral)); |
| 1930 | |
| 1931 | // Non-matching cases (character literals, float and double) |
| 1932 | EXPECT_TRUE(notMatches("int i = L'a';", |
| 1933 | HasIntLiteral)); // this is actually a character |
| 1934 | // literal cast to int |
| 1935 | EXPECT_TRUE(notMatches("int i = 'a';", HasIntLiteral)); |
| 1936 | EXPECT_TRUE(notMatches("int i = 1e10;", HasIntLiteral)); |
| 1937 | EXPECT_TRUE(notMatches("int i = 10.0;", HasIntLiteral)); |
| 1938 | } |
| 1939 | |
Daniel Jasper | c5ae717 | 2013-07-26 18:52:58 +0000 | [diff] [blame] | 1940 | TEST(Matcher, FloatLiterals) { |
| 1941 | StatementMatcher HasFloatLiteral = floatLiteral(); |
| 1942 | EXPECT_TRUE(matches("float i = 10.0;", HasFloatLiteral)); |
| 1943 | EXPECT_TRUE(matches("float i = 10.0f;", HasFloatLiteral)); |
| 1944 | EXPECT_TRUE(matches("double i = 10.0;", HasFloatLiteral)); |
| 1945 | EXPECT_TRUE(matches("double i = 10.0L;", HasFloatLiteral)); |
| 1946 | EXPECT_TRUE(matches("double i = 1e10;", HasFloatLiteral)); |
| 1947 | |
| 1948 | EXPECT_TRUE(notMatches("float i = 10;", HasFloatLiteral)); |
| 1949 | } |
| 1950 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 1951 | TEST(Matcher, NullPtrLiteral) { |
| 1952 | EXPECT_TRUE(matches("int* i = nullptr;", nullPtrLiteralExpr())); |
| 1953 | } |
| 1954 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 1955 | TEST(Matcher, AsmStatement) { |
| 1956 | EXPECT_TRUE(matches("void foo() { __asm(\"mov al, 2\"); }", asmStmt())); |
| 1957 | } |
| 1958 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1959 | TEST(Matcher, Conditions) { |
| 1960 | StatementMatcher Condition = ifStmt(hasCondition(boolLiteral(equals(true)))); |
| 1961 | |
| 1962 | EXPECT_TRUE(matches("void x() { if (true) {} }", Condition)); |
| 1963 | EXPECT_TRUE(notMatches("void x() { if (false) {} }", Condition)); |
| 1964 | EXPECT_TRUE(notMatches("void x() { bool a = true; if (a) {} }", Condition)); |
| 1965 | EXPECT_TRUE(notMatches("void x() { if (true || false) {} }", Condition)); |
| 1966 | EXPECT_TRUE(notMatches("void x() { if (1) {} }", Condition)); |
| 1967 | } |
| 1968 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1969 | TEST(IfStmt, ChildTraversalMatchers) { |
| 1970 | EXPECT_TRUE(matches("void f() { if (false) true; else false; }", |
| 1971 | ifStmt(hasThen(boolLiteral(equals(true)))))); |
| 1972 | EXPECT_TRUE(notMatches("void f() { if (false) false; else true; }", |
| 1973 | ifStmt(hasThen(boolLiteral(equals(true)))))); |
| 1974 | EXPECT_TRUE(matches("void f() { if (false) false; else true; }", |
| 1975 | ifStmt(hasElse(boolLiteral(equals(true)))))); |
| 1976 | EXPECT_TRUE(notMatches("void f() { if (false) true; else false; }", |
| 1977 | ifStmt(hasElse(boolLiteral(equals(true)))))); |
| 1978 | } |
| 1979 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 1980 | TEST(MatchBinaryOperator, HasOperatorName) { |
| 1981 | StatementMatcher OperatorOr = binaryOperator(hasOperatorName("||")); |
| 1982 | |
| 1983 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorOr)); |
| 1984 | EXPECT_TRUE(notMatches("void x() { true && false; }", OperatorOr)); |
| 1985 | } |
| 1986 | |
| 1987 | TEST(MatchBinaryOperator, HasLHSAndHasRHS) { |
| 1988 | StatementMatcher OperatorTrueFalse = |
| 1989 | binaryOperator(hasLHS(boolLiteral(equals(true))), |
| 1990 | hasRHS(boolLiteral(equals(false)))); |
| 1991 | |
| 1992 | EXPECT_TRUE(matches("void x() { true || false; }", OperatorTrueFalse)); |
| 1993 | EXPECT_TRUE(matches("void x() { true && false; }", OperatorTrueFalse)); |
| 1994 | EXPECT_TRUE(notMatches("void x() { false || true; }", OperatorTrueFalse)); |
| 1995 | } |
| 1996 | |
| 1997 | TEST(MatchBinaryOperator, HasEitherOperand) { |
| 1998 | StatementMatcher HasOperand = |
| 1999 | binaryOperator(hasEitherOperand(boolLiteral(equals(false)))); |
| 2000 | |
| 2001 | EXPECT_TRUE(matches("void x() { true || false; }", HasOperand)); |
| 2002 | EXPECT_TRUE(matches("void x() { false && true; }", HasOperand)); |
| 2003 | EXPECT_TRUE(notMatches("void x() { true || true; }", HasOperand)); |
| 2004 | } |
| 2005 | |
| 2006 | TEST(Matcher, BinaryOperatorTypes) { |
| 2007 | // Integration test that verifies the AST provides all binary operators in |
| 2008 | // a way we expect. |
| 2009 | // FIXME: Operator ',' |
| 2010 | EXPECT_TRUE( |
| 2011 | matches("void x() { 3, 4; }", binaryOperator(hasOperatorName(",")))); |
| 2012 | EXPECT_TRUE( |
| 2013 | matches("bool b; bool c = (b = true);", |
| 2014 | binaryOperator(hasOperatorName("=")))); |
| 2015 | EXPECT_TRUE( |
| 2016 | matches("bool b = 1 != 2;", binaryOperator(hasOperatorName("!=")))); |
| 2017 | EXPECT_TRUE( |
| 2018 | matches("bool b = 1 == 2;", binaryOperator(hasOperatorName("==")))); |
| 2019 | EXPECT_TRUE(matches("bool b = 1 < 2;", binaryOperator(hasOperatorName("<")))); |
| 2020 | EXPECT_TRUE( |
| 2021 | matches("bool b = 1 <= 2;", binaryOperator(hasOperatorName("<=")))); |
| 2022 | EXPECT_TRUE( |
| 2023 | matches("int i = 1 << 2;", binaryOperator(hasOperatorName("<<")))); |
| 2024 | EXPECT_TRUE( |
| 2025 | matches("int i = 1; int j = (i <<= 2);", |
| 2026 | binaryOperator(hasOperatorName("<<=")))); |
| 2027 | EXPECT_TRUE(matches("bool b = 1 > 2;", binaryOperator(hasOperatorName(">")))); |
| 2028 | EXPECT_TRUE( |
| 2029 | matches("bool b = 1 >= 2;", binaryOperator(hasOperatorName(">=")))); |
| 2030 | EXPECT_TRUE( |
| 2031 | matches("int i = 1 >> 2;", binaryOperator(hasOperatorName(">>")))); |
| 2032 | EXPECT_TRUE( |
| 2033 | matches("int i = 1; int j = (i >>= 2);", |
| 2034 | binaryOperator(hasOperatorName(">>=")))); |
| 2035 | EXPECT_TRUE( |
| 2036 | matches("int i = 42 ^ 23;", binaryOperator(hasOperatorName("^")))); |
| 2037 | EXPECT_TRUE( |
| 2038 | matches("int i = 42; int j = (i ^= 42);", |
| 2039 | binaryOperator(hasOperatorName("^=")))); |
| 2040 | EXPECT_TRUE( |
| 2041 | matches("int i = 42 % 23;", binaryOperator(hasOperatorName("%")))); |
| 2042 | EXPECT_TRUE( |
| 2043 | matches("int i = 42; int j = (i %= 42);", |
| 2044 | binaryOperator(hasOperatorName("%=")))); |
| 2045 | EXPECT_TRUE( |
| 2046 | matches("bool b = 42 &23;", binaryOperator(hasOperatorName("&")))); |
| 2047 | EXPECT_TRUE( |
| 2048 | matches("bool b = true && false;", |
| 2049 | binaryOperator(hasOperatorName("&&")))); |
| 2050 | EXPECT_TRUE( |
| 2051 | matches("bool b = true; bool c = (b &= false);", |
| 2052 | binaryOperator(hasOperatorName("&=")))); |
| 2053 | EXPECT_TRUE( |
| 2054 | matches("bool b = 42 | 23;", binaryOperator(hasOperatorName("|")))); |
| 2055 | EXPECT_TRUE( |
| 2056 | matches("bool b = true || false;", |
| 2057 | binaryOperator(hasOperatorName("||")))); |
| 2058 | EXPECT_TRUE( |
| 2059 | matches("bool b = true; bool c = (b |= false);", |
| 2060 | binaryOperator(hasOperatorName("|=")))); |
| 2061 | EXPECT_TRUE( |
| 2062 | matches("int i = 42 *23;", binaryOperator(hasOperatorName("*")))); |
| 2063 | EXPECT_TRUE( |
| 2064 | matches("int i = 42; int j = (i *= 23);", |
| 2065 | binaryOperator(hasOperatorName("*=")))); |
| 2066 | EXPECT_TRUE( |
| 2067 | matches("int i = 42 / 23;", binaryOperator(hasOperatorName("/")))); |
| 2068 | EXPECT_TRUE( |
| 2069 | matches("int i = 42; int j = (i /= 23);", |
| 2070 | binaryOperator(hasOperatorName("/=")))); |
| 2071 | EXPECT_TRUE( |
| 2072 | matches("int i = 42 + 23;", binaryOperator(hasOperatorName("+")))); |
| 2073 | EXPECT_TRUE( |
| 2074 | matches("int i = 42; int j = (i += 23);", |
| 2075 | binaryOperator(hasOperatorName("+=")))); |
| 2076 | EXPECT_TRUE( |
| 2077 | matches("int i = 42 - 23;", binaryOperator(hasOperatorName("-")))); |
| 2078 | EXPECT_TRUE( |
| 2079 | matches("int i = 42; int j = (i -= 23);", |
| 2080 | binaryOperator(hasOperatorName("-=")))); |
| 2081 | EXPECT_TRUE( |
| 2082 | matches("struct A { void x() { void (A::*a)(); (this->*a)(); } };", |
| 2083 | binaryOperator(hasOperatorName("->*")))); |
| 2084 | EXPECT_TRUE( |
| 2085 | matches("struct A { void x() { void (A::*a)(); ((*this).*a)(); } };", |
| 2086 | binaryOperator(hasOperatorName(".*")))); |
| 2087 | |
| 2088 | // Member expressions as operators are not supported in matches. |
| 2089 | EXPECT_TRUE( |
| 2090 | notMatches("struct A { void x(A *a) { a->x(this); } };", |
| 2091 | binaryOperator(hasOperatorName("->")))); |
| 2092 | |
| 2093 | // Initializer assignments are not represented as operator equals. |
| 2094 | EXPECT_TRUE( |
| 2095 | notMatches("bool b = true;", binaryOperator(hasOperatorName("=")))); |
| 2096 | |
| 2097 | // Array indexing is not represented as operator. |
| 2098 | EXPECT_TRUE(notMatches("int a[42]; void x() { a[23]; }", unaryOperator())); |
| 2099 | |
| 2100 | // Overloaded operators do not match at all. |
| 2101 | EXPECT_TRUE(notMatches( |
| 2102 | "struct A { bool operator&&(const A &a) const { return false; } };" |
| 2103 | "void x() { A a, b; a && b; }", |
| 2104 | binaryOperator())); |
| 2105 | } |
| 2106 | |
| 2107 | TEST(MatchUnaryOperator, HasOperatorName) { |
| 2108 | StatementMatcher OperatorNot = unaryOperator(hasOperatorName("!")); |
| 2109 | |
| 2110 | EXPECT_TRUE(matches("void x() { !true; } ", OperatorNot)); |
| 2111 | EXPECT_TRUE(notMatches("void x() { true; } ", OperatorNot)); |
| 2112 | } |
| 2113 | |
| 2114 | TEST(MatchUnaryOperator, HasUnaryOperand) { |
| 2115 | StatementMatcher OperatorOnFalse = |
| 2116 | unaryOperator(hasUnaryOperand(boolLiteral(equals(false)))); |
| 2117 | |
| 2118 | EXPECT_TRUE(matches("void x() { !false; }", OperatorOnFalse)); |
| 2119 | EXPECT_TRUE(notMatches("void x() { !true; }", OperatorOnFalse)); |
| 2120 | } |
| 2121 | |
| 2122 | TEST(Matcher, UnaryOperatorTypes) { |
| 2123 | // Integration test that verifies the AST provides all unary operators in |
| 2124 | // a way we expect. |
| 2125 | EXPECT_TRUE(matches("bool b = !true;", unaryOperator(hasOperatorName("!")))); |
| 2126 | EXPECT_TRUE( |
| 2127 | matches("bool b; bool *p = &b;", unaryOperator(hasOperatorName("&")))); |
| 2128 | EXPECT_TRUE(matches("int i = ~ 1;", unaryOperator(hasOperatorName("~")))); |
| 2129 | EXPECT_TRUE( |
| 2130 | matches("bool *p; bool b = *p;", unaryOperator(hasOperatorName("*")))); |
| 2131 | EXPECT_TRUE( |
| 2132 | matches("int i; int j = +i;", unaryOperator(hasOperatorName("+")))); |
| 2133 | EXPECT_TRUE( |
| 2134 | matches("int i; int j = -i;", unaryOperator(hasOperatorName("-")))); |
| 2135 | EXPECT_TRUE( |
| 2136 | matches("int i; int j = ++i;", unaryOperator(hasOperatorName("++")))); |
| 2137 | EXPECT_TRUE( |
| 2138 | matches("int i; int j = i++;", unaryOperator(hasOperatorName("++")))); |
| 2139 | EXPECT_TRUE( |
| 2140 | matches("int i; int j = --i;", unaryOperator(hasOperatorName("--")))); |
| 2141 | EXPECT_TRUE( |
| 2142 | matches("int i; int j = i--;", unaryOperator(hasOperatorName("--")))); |
| 2143 | |
| 2144 | // We don't match conversion operators. |
| 2145 | EXPECT_TRUE(notMatches("int i; double d = (double)i;", unaryOperator())); |
| 2146 | |
| 2147 | // Function calls are not represented as operator. |
| 2148 | EXPECT_TRUE(notMatches("void f(); void x() { f(); }", unaryOperator())); |
| 2149 | |
| 2150 | // Overloaded operators do not match at all. |
| 2151 | // FIXME: We probably want to add that. |
| 2152 | EXPECT_TRUE(notMatches( |
| 2153 | "struct A { bool operator!() const { return false; } };" |
| 2154 | "void x() { A a; !a; }", unaryOperator(hasOperatorName("!")))); |
| 2155 | } |
| 2156 | |
| 2157 | TEST(Matcher, ConditionalOperator) { |
| 2158 | StatementMatcher Conditional = conditionalOperator( |
| 2159 | hasCondition(boolLiteral(equals(true))), |
| 2160 | hasTrueExpression(boolLiteral(equals(false)))); |
| 2161 | |
| 2162 | EXPECT_TRUE(matches("void x() { true ? false : true; }", Conditional)); |
| 2163 | EXPECT_TRUE(notMatches("void x() { false ? false : true; }", Conditional)); |
| 2164 | EXPECT_TRUE(notMatches("void x() { true ? true : false; }", Conditional)); |
| 2165 | |
| 2166 | StatementMatcher ConditionalFalse = conditionalOperator( |
| 2167 | hasFalseExpression(boolLiteral(equals(false)))); |
| 2168 | |
| 2169 | EXPECT_TRUE(matches("void x() { true ? true : false; }", ConditionalFalse)); |
| 2170 | EXPECT_TRUE( |
| 2171 | notMatches("void x() { true ? false : true; }", ConditionalFalse)); |
| 2172 | } |
| 2173 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2174 | TEST(ArraySubscriptMatchers, ArraySubscripts) { |
| 2175 | EXPECT_TRUE(matches("int i[2]; void f() { i[1] = 1; }", |
| 2176 | arraySubscriptExpr())); |
| 2177 | EXPECT_TRUE(notMatches("int i; void f() { i = 1; }", |
| 2178 | arraySubscriptExpr())); |
| 2179 | } |
| 2180 | |
| 2181 | TEST(ArraySubscriptMatchers, ArrayIndex) { |
| 2182 | EXPECT_TRUE(matches( |
| 2183 | "int i[2]; void f() { i[1] = 1; }", |
| 2184 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 2185 | EXPECT_TRUE(matches( |
| 2186 | "int i[2]; void f() { 1[i] = 1; }", |
| 2187 | arraySubscriptExpr(hasIndex(integerLiteral(equals(1)))))); |
| 2188 | EXPECT_TRUE(notMatches( |
| 2189 | "int i[2]; void f() { i[1] = 1; }", |
| 2190 | arraySubscriptExpr(hasIndex(integerLiteral(equals(0)))))); |
| 2191 | } |
| 2192 | |
| 2193 | TEST(ArraySubscriptMatchers, MatchesArrayBase) { |
| 2194 | EXPECT_TRUE(matches( |
| 2195 | "int i[2]; void f() { i[1] = 2; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2196 | arraySubscriptExpr(hasBase(implicitCastExpr( |
| 2197 | hasSourceExpression(declRefExpr())))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2200 | TEST(Matcher, HasNameSupportsNamespaces) { |
| 2201 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2202 | recordDecl(hasName("a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2203 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2204 | recordDecl(hasName("::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2205 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2206 | recordDecl(hasName("b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2207 | EXPECT_TRUE(matches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2208 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2209 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2210 | recordDecl(hasName("c::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2211 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2212 | recordDecl(hasName("a::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2213 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2214 | recordDecl(hasName("a::b::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2215 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2216 | recordDecl(hasName("::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2217 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2218 | recordDecl(hasName("::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2219 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2220 | recordDecl(hasName("z::a::b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2221 | EXPECT_TRUE(notMatches("namespace a { namespace b { class C; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2222 | recordDecl(hasName("a+b::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2223 | EXPECT_TRUE(notMatches("namespace a { namespace b { class AC; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2224 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | TEST(Matcher, HasNameSupportsOuterClasses) { |
| 2228 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2229 | matches("class A { class B { class C; }; };", |
| 2230 | recordDecl(hasName("A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2231 | EXPECT_TRUE( |
| 2232 | matches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2233 | recordDecl(hasName("::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2234 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2235 | matches("class A { class B { class C; }; };", |
| 2236 | recordDecl(hasName("B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2237 | EXPECT_TRUE( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2238 | matches("class A { class B { class C; }; };", |
| 2239 | recordDecl(hasName("C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2240 | EXPECT_TRUE( |
| 2241 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2242 | recordDecl(hasName("c::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2243 | EXPECT_TRUE( |
| 2244 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2245 | recordDecl(hasName("A::c::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2246 | EXPECT_TRUE( |
| 2247 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2248 | recordDecl(hasName("A::B::A")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2249 | EXPECT_TRUE( |
| 2250 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2251 | recordDecl(hasName("::C")))); |
| 2252 | EXPECT_TRUE( |
| 2253 | notMatches("class A { class B { class C; }; };", |
| 2254 | recordDecl(hasName("::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2255 | EXPECT_TRUE(notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2256 | recordDecl(hasName("z::A::B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2257 | EXPECT_TRUE( |
| 2258 | notMatches("class A { class B { class C; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2259 | recordDecl(hasName("A+B::C")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | TEST(Matcher, IsDefinition) { |
| 2263 | DeclarationMatcher DefinitionOfClassA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2264 | recordDecl(hasName("A"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2265 | EXPECT_TRUE(matches("class A {};", DefinitionOfClassA)); |
| 2266 | EXPECT_TRUE(notMatches("class A;", DefinitionOfClassA)); |
| 2267 | |
| 2268 | DeclarationMatcher DefinitionOfVariableA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2269 | varDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2270 | EXPECT_TRUE(matches("int a;", DefinitionOfVariableA)); |
| 2271 | EXPECT_TRUE(notMatches("extern int a;", DefinitionOfVariableA)); |
| 2272 | |
| 2273 | DeclarationMatcher DefinitionOfMethodA = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2274 | methodDecl(hasName("a"), isDefinition()); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2275 | EXPECT_TRUE(matches("class A { void a() {} };", DefinitionOfMethodA)); |
| 2276 | EXPECT_TRUE(notMatches("class A { void a(); };", DefinitionOfMethodA)); |
| 2277 | } |
| 2278 | |
| 2279 | TEST(Matcher, OfClass) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2280 | StatementMatcher Constructor = constructExpr(hasDeclaration(methodDecl( |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2281 | ofClass(hasName("X"))))); |
| 2282 | |
| 2283 | EXPECT_TRUE( |
| 2284 | matches("class X { public: X(); }; void x(int) { X x; }", Constructor)); |
| 2285 | EXPECT_TRUE( |
| 2286 | matches("class X { public: X(); }; void x(int) { X x = X(); }", |
| 2287 | Constructor)); |
| 2288 | EXPECT_TRUE( |
| 2289 | notMatches("class Y { public: Y(); }; void x(int) { Y y; }", |
| 2290 | Constructor)); |
| 2291 | } |
| 2292 | |
| 2293 | TEST(Matcher, VisitsTemplateInstantiations) { |
| 2294 | EXPECT_TRUE(matches( |
| 2295 | "class A { public: void x(); };" |
| 2296 | "template <typename T> class B { public: void y() { T t; t.x(); } };" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2297 | "void f() { B<A> b; b.y(); }", |
| 2298 | callExpr(callee(methodDecl(hasName("x")))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2299 | |
| 2300 | EXPECT_TRUE(matches( |
| 2301 | "class A { public: void x(); };" |
| 2302 | "class C {" |
| 2303 | " public:" |
| 2304 | " template <typename T> class B { public: void y() { T t; t.x(); } };" |
| 2305 | "};" |
| 2306 | "void f() {" |
| 2307 | " C::B<A> b; b.y();" |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2308 | "}", |
| 2309 | recordDecl(hasName("C"), |
| 2310 | hasDescendant(callExpr(callee(methodDecl(hasName("x")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2313 | TEST(Matcher, HandlesNullQualTypes) { |
| 2314 | // FIXME: Add a Type matcher so we can replace uses of this |
| 2315 | // variable with Type(True()) |
| 2316 | const TypeMatcher AnyType = anything(); |
| 2317 | |
| 2318 | // We don't really care whether this matcher succeeds; we're testing that |
| 2319 | // it completes without crashing. |
| 2320 | EXPECT_TRUE(matches( |
| 2321 | "struct A { };" |
| 2322 | "template <typename T>" |
| 2323 | "void f(T t) {" |
| 2324 | " T local_t(t /* this becomes a null QualType in the AST */);" |
| 2325 | "}" |
| 2326 | "void g() {" |
| 2327 | " f(0);" |
| 2328 | "}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2329 | expr(hasType(TypeMatcher( |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2330 | anyOf( |
| 2331 | TypeMatcher(hasDeclaration(anything())), |
| 2332 | pointsTo(AnyType), |
| 2333 | references(AnyType) |
| 2334 | // Other QualType matchers should go here. |
| 2335 | )))))); |
| 2336 | } |
| 2337 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2338 | // For testing AST_MATCHER_P(). |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2339 | AST_MATCHER_P(Decl, just, internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2340 | // Make sure all special variables are used: node, match_finder, |
| 2341 | // bound_nodes_builder, and the parameter named 'AMatcher'. |
| 2342 | return AMatcher.matches(Node, Finder, Builder); |
| 2343 | } |
| 2344 | |
| 2345 | TEST(AstMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2346 | DeclarationMatcher HasClassB = just(has(recordDecl(hasName("B")).bind("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2347 | |
| 2348 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2349 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2350 | |
| 2351 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2352 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2353 | |
| 2354 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2355 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | AST_POLYMORPHIC_MATCHER_P( |
Samuel Benzaquen | ef7eb02 | 2013-06-21 15:51:31 +0000 | [diff] [blame] | 2359 | polymorphicHas, |
| 2360 | AST_POLYMORPHIC_SUPPORTED_TYPES_2(Decl, Stmt), |
| 2361 | internal::Matcher<Decl>, AMatcher) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2362 | return Finder->matchesChildOf( |
Manuel Klimek | a78d0d6 | 2012-09-05 12:12:07 +0000 | [diff] [blame] | 2363 | Node, AMatcher, Builder, |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2364 | ASTMatchFinder::TK_IgnoreImplicitCastsAndParentheses, |
| 2365 | ASTMatchFinder::BK_First); |
| 2366 | } |
| 2367 | |
| 2368 | TEST(AstPolymorphicMatcherPMacro, Works) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2369 | DeclarationMatcher HasClassB = |
| 2370 | polymorphicHas(recordDecl(hasName("B")).bind("b")); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2371 | |
| 2372 | EXPECT_TRUE(matchAndVerifyResultTrue("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2373 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2374 | |
| 2375 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class B {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2376 | HasClassB, new VerifyIdIsBoundTo<Decl>("a"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2377 | |
| 2378 | EXPECT_TRUE(matchAndVerifyResultFalse("class A { class C {}; };", |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 2379 | HasClassB, new VerifyIdIsBoundTo<Decl>("b"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2380 | |
| 2381 | StatementMatcher StatementHasClassB = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2382 | polymorphicHas(recordDecl(hasName("B"))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2383 | |
| 2384 | EXPECT_TRUE(matches("void x() { class B {}; }", StatementHasClassB)); |
| 2385 | } |
| 2386 | |
| 2387 | TEST(For, FindsForLoops) { |
| 2388 | EXPECT_TRUE(matches("void f() { for(;;); }", forStmt())); |
| 2389 | EXPECT_TRUE(matches("void f() { if(true) for(;;); }", forStmt())); |
Daniel Jasper | 1a00fee | 2012-10-01 15:05:34 +0000 | [diff] [blame] | 2390 | EXPECT_TRUE(notMatches("int as[] = { 1, 2, 3 };" |
| 2391 | "void f() { for (auto &a : as); }", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2392 | forStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2393 | } |
| 2394 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2395 | TEST(For, ForLoopInternals) { |
| 2396 | EXPECT_TRUE(matches("void f(){ int i; for (; i < 3 ; ); }", |
| 2397 | forStmt(hasCondition(anything())))); |
| 2398 | EXPECT_TRUE(matches("void f() { for (int i = 0; ;); }", |
| 2399 | forStmt(hasLoopInit(anything())))); |
| 2400 | } |
| 2401 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2402 | TEST(For, ForRangeLoopInternals) { |
| 2403 | EXPECT_TRUE(matches("void f(){ int a[] {1, 2}; for (int i : a); }", |
| 2404 | forRangeStmt(hasLoopVariable(anything())))); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2405 | EXPECT_TRUE(matches( |
| 2406 | "void f(){ int a[] {1, 2}; for (int i : a); }", |
| 2407 | forRangeStmt(hasRangeInit(declRefExpr(to(varDecl(hasName("a")))))))); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2410 | TEST(For, NegativeForLoopInternals) { |
| 2411 | EXPECT_TRUE(notMatches("void f(){ for (int i = 0; ; ++i); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2412 | forStmt(hasCondition(expr())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2413 | EXPECT_TRUE(notMatches("void f() {int i; for (; i < 4; ++i) {} }", |
| 2414 | forStmt(hasLoopInit(anything())))); |
| 2415 | } |
| 2416 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2417 | TEST(For, ReportsNoFalsePositives) { |
| 2418 | EXPECT_TRUE(notMatches("void f() { ; }", forStmt())); |
| 2419 | EXPECT_TRUE(notMatches("void f() { if(true); }", forStmt())); |
| 2420 | } |
| 2421 | |
| 2422 | TEST(CompoundStatement, HandlesSimpleCases) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2423 | EXPECT_TRUE(notMatches("void f();", compoundStmt())); |
| 2424 | EXPECT_TRUE(matches("void f() {}", compoundStmt())); |
| 2425 | EXPECT_TRUE(matches("void f() {{}}", compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
| 2428 | TEST(CompoundStatement, DoesNotMatchEmptyStruct) { |
| 2429 | // It's not a compound statement just because there's "{}" in the source |
| 2430 | // text. This is an AST search, not grep. |
| 2431 | EXPECT_TRUE(notMatches("namespace n { struct S {}; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2432 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2433 | EXPECT_TRUE(matches("namespace n { struct S { void f() {{}} }; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2434 | compoundStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2437 | TEST(HasBody, FindsBodyOfForWhileDoLoops) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2438 | EXPECT_TRUE(matches("void f() { for(;;) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2439 | forStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2440 | EXPECT_TRUE(notMatches("void f() { for(;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2441 | forStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2442 | EXPECT_TRUE(matches("void f() { while(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2443 | whileStmt(hasBody(compoundStmt())))); |
Daniel Jasper | 6a12449 | 2012-07-12 08:50:38 +0000 | [diff] [blame] | 2444 | EXPECT_TRUE(matches("void f() { do {} while(true); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2445 | doStmt(hasBody(compoundStmt())))); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2446 | EXPECT_TRUE(matches("void f() { int p[2]; for (auto x : p) {} }", |
| 2447 | forRangeStmt(hasBody(compoundStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | TEST(HasAnySubstatement, MatchesForTopLevelCompoundStatement) { |
| 2451 | // The simplest case: every compound statement is in a function |
| 2452 | // definition, and the function body itself must be a compound |
| 2453 | // statement. |
| 2454 | EXPECT_TRUE(matches("void f() { for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2455 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | TEST(HasAnySubstatement, IsNotRecursive) { |
| 2459 | // It's really "has any immediate substatement". |
| 2460 | EXPECT_TRUE(notMatches("void f() { if (true) for (;;); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2461 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | TEST(HasAnySubstatement, MatchesInNestedCompoundStatements) { |
| 2465 | EXPECT_TRUE(matches("void f() { if (true) { for (;;); } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2466 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
| 2469 | TEST(HasAnySubstatement, FindsSubstatementBetweenOthers) { |
| 2470 | EXPECT_TRUE(matches("void f() { 1; 2; 3; for (;;); 4; 5; 6; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2471 | compoundStmt(hasAnySubstatement(forStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | TEST(StatementCountIs, FindsNoStatementsInAnEmptyCompoundStatement) { |
| 2475 | EXPECT_TRUE(matches("void f() { }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2476 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2477 | EXPECT_TRUE(notMatches("void f() {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2478 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | TEST(StatementCountIs, AppearsToMatchOnlyOneCount) { |
| 2482 | EXPECT_TRUE(matches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2483 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2484 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2485 | compoundStmt(statementCountIs(0)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2486 | EXPECT_TRUE(notMatches("void f() { 1; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2487 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | TEST(StatementCountIs, WorksWithMultipleStatements) { |
| 2491 | EXPECT_TRUE(matches("void f() { 1; 2; 3; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2492 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | TEST(StatementCountIs, WorksWithNestedCompoundStatements) { |
| 2496 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2497 | compoundStmt(statementCountIs(1)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2498 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2499 | compoundStmt(statementCountIs(2)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2500 | EXPECT_TRUE(notMatches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2501 | compoundStmt(statementCountIs(3)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2502 | EXPECT_TRUE(matches("void f() { { 1; } { 1; 2; 3; 4; } }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2503 | compoundStmt(statementCountIs(4)))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
| 2506 | TEST(Member, WorksInSimplestCase) { |
| 2507 | EXPECT_TRUE(matches("struct { int first; } s; int i(s.first);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2508 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | TEST(Member, DoesNotMatchTheBaseExpression) { |
| 2512 | // Don't pick out the wrong part of the member expression, this should |
| 2513 | // be checking the member (name) only. |
| 2514 | EXPECT_TRUE(notMatches("struct { int i; } first; int i(first.i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2515 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
| 2518 | TEST(Member, MatchesInMemberFunctionCall) { |
| 2519 | EXPECT_TRUE(matches("void f() {" |
| 2520 | " struct { void first() {}; } s;" |
| 2521 | " s.first();" |
| 2522 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2523 | memberExpr(member(hasName("first"))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Daniel Jasper | c711af2 | 2012-10-23 15:46:39 +0000 | [diff] [blame] | 2526 | TEST(Member, MatchesMember) { |
| 2527 | EXPECT_TRUE(matches( |
| 2528 | "struct A { int i; }; void f() { A a; a.i = 2; }", |
| 2529 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2530 | EXPECT_TRUE(notMatches( |
| 2531 | "struct A { float f; }; void f() { A a; a.f = 2.0f; }", |
| 2532 | memberExpr(hasDeclaration(fieldDecl(hasType(isInteger())))))); |
| 2533 | } |
| 2534 | |
Daniel Jasper | f3197e9 | 2013-02-25 12:02:08 +0000 | [diff] [blame] | 2535 | TEST(Member, UnderstandsAccess) { |
| 2536 | EXPECT_TRUE(matches( |
| 2537 | "struct A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2538 | EXPECT_TRUE(notMatches( |
| 2539 | "struct A { int i; };", fieldDecl(isProtected(), hasName("i")))); |
| 2540 | EXPECT_TRUE(notMatches( |
| 2541 | "struct A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2542 | |
| 2543 | EXPECT_TRUE(notMatches( |
| 2544 | "class A { int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2545 | EXPECT_TRUE(notMatches( |
| 2546 | "class A { int i; };", fieldDecl(isProtected(), hasName("i")))); |
| 2547 | EXPECT_TRUE(matches( |
| 2548 | "class A { int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2549 | |
| 2550 | EXPECT_TRUE(notMatches( |
| 2551 | "class A { protected: int i; };", fieldDecl(isPublic(), hasName("i")))); |
| 2552 | EXPECT_TRUE(matches("class A { protected: int i; };", |
| 2553 | fieldDecl(isProtected(), hasName("i")))); |
| 2554 | EXPECT_TRUE(notMatches( |
| 2555 | "class A { protected: int i; };", fieldDecl(isPrivate(), hasName("i")))); |
| 2556 | |
| 2557 | // Non-member decls have the AccessSpecifier AS_none and thus aren't matched. |
| 2558 | EXPECT_TRUE(notMatches("int i;", varDecl(isPublic(), hasName("i")))); |
| 2559 | EXPECT_TRUE(notMatches("int i;", varDecl(isProtected(), hasName("i")))); |
| 2560 | EXPECT_TRUE(notMatches("int i;", varDecl(isPrivate(), hasName("i")))); |
| 2561 | } |
| 2562 | |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2563 | TEST(Member, MatchesMemberAllocationFunction) { |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2564 | // Fails in C++11 mode |
| 2565 | EXPECT_TRUE(matchesConditionally( |
| 2566 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2567 | "class X { void *operator new(std::size_t); };", |
| 2568 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2569 | |
| 2570 | EXPECT_TRUE(matches("class X { void operator delete(void*); };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2571 | methodDecl(ofClass(hasName("X"))))); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2572 | |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2573 | // Fails in C++11 mode |
| 2574 | EXPECT_TRUE(matchesConditionally( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2575 | "namespace std { typedef typeof(sizeof(int)) size_t; }" |
| 2576 | "class X { void operator delete[](void*, std::size_t); };", |
Daniel Jasper | 31f7c08 | 2012-10-01 13:40:41 +0000 | [diff] [blame] | 2577 | methodDecl(ofClass(hasName("X"))), true, "-std=gnu++98")); |
Dmitri Gribenko | 671a045 | 2012-08-18 00:29:27 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2580 | TEST(HasObjectExpression, DoesNotMatchMember) { |
| 2581 | EXPECT_TRUE(notMatches( |
| 2582 | "class X {}; struct Z { X m; }; void f(Z z) { z.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2583 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | TEST(HasObjectExpression, MatchesBaseOfVariable) { |
| 2587 | EXPECT_TRUE(matches( |
| 2588 | "struct X { int m; }; void f(X x) { x.m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2589 | memberExpr(hasObjectExpression(hasType(recordDecl(hasName("X"))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2590 | EXPECT_TRUE(matches( |
| 2591 | "struct X { int m; }; void f(X* x) { x->m; }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2592 | memberExpr(hasObjectExpression( |
| 2593 | hasType(pointsTo(recordDecl(hasName("X")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | TEST(HasObjectExpression, |
| 2597 | MatchesObjectExpressionOfImplicitlyFormedMemberExpression) { |
| 2598 | EXPECT_TRUE(matches( |
| 2599 | "class X {}; struct S { X m; void f() { this->m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2600 | memberExpr(hasObjectExpression( |
| 2601 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2602 | EXPECT_TRUE(matches( |
| 2603 | "class X {}; struct S { X m; void f() { m; } };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2604 | memberExpr(hasObjectExpression( |
| 2605 | hasType(pointsTo(recordDecl(hasName("S")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2606 | } |
| 2607 | |
| 2608 | TEST(Field, DoesNotMatchNonFieldMembers) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2609 | EXPECT_TRUE(notMatches("class X { void m(); };", fieldDecl(hasName("m")))); |
| 2610 | EXPECT_TRUE(notMatches("class X { class m {}; };", fieldDecl(hasName("m")))); |
| 2611 | EXPECT_TRUE(notMatches("class X { enum { m }; };", fieldDecl(hasName("m")))); |
| 2612 | EXPECT_TRUE(notMatches("class X { enum m {}; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | TEST(Field, MatchesField) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2616 | EXPECT_TRUE(matches("class X { int m; };", fieldDecl(hasName("m")))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
| 2619 | TEST(IsConstQualified, MatchesConstInt) { |
| 2620 | EXPECT_TRUE(matches("const int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2621 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | TEST(IsConstQualified, MatchesConstPointer) { |
| 2625 | EXPECT_TRUE(matches("int i = 42; int* const p(&i);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2626 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | TEST(IsConstQualified, MatchesThroughTypedef) { |
| 2630 | EXPECT_TRUE(matches("typedef const int const_int; const_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2631 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2632 | EXPECT_TRUE(matches("typedef int* int_ptr; const int_ptr p(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2633 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | TEST(IsConstQualified, DoesNotMatchInappropriately) { |
| 2637 | EXPECT_TRUE(notMatches("typedef int nonconst_int; nonconst_int i = 42;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2638 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2639 | EXPECT_TRUE(notMatches("int const* p;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2640 | varDecl(hasType(isConstQualified())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2643 | TEST(CastExpression, MatchesExplicitCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2644 | EXPECT_TRUE(matches("char *p = reinterpret_cast<char *>(&p);",castExpr())); |
| 2645 | EXPECT_TRUE(matches("void *p = (void *)(&p);", castExpr())); |
| 2646 | EXPECT_TRUE(matches("char q, *p = const_cast<char *>(&q);", castExpr())); |
| 2647 | EXPECT_TRUE(matches("char c = char(0);", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2648 | } |
| 2649 | TEST(CastExpression, MatchesImplicitCasts) { |
| 2650 | // This test creates an implicit cast from int to char. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2651 | EXPECT_TRUE(matches("char c = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2652 | // This test creates an implicit cast from lvalue to rvalue. |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2653 | EXPECT_TRUE(matches("char c = 0, d = c;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | TEST(CastExpression, DoesNotMatchNonCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2657 | EXPECT_TRUE(notMatches("char c = '0';", castExpr())); |
| 2658 | EXPECT_TRUE(notMatches("char c, &q = c;", castExpr())); |
| 2659 | EXPECT_TRUE(notMatches("int i = (0);", castExpr())); |
| 2660 | EXPECT_TRUE(notMatches("int i = 0;", castExpr())); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2663 | TEST(ReinterpretCast, MatchesSimpleCase) { |
| 2664 | EXPECT_TRUE(matches("char* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2665 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| 2668 | TEST(ReinterpretCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2669 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2670 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2671 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2672 | EXPECT_TRUE(notMatches("void* p = static_cast<void*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2673 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2674 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2675 | "B b;" |
| 2676 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2677 | reinterpretCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | TEST(FunctionalCast, MatchesSimpleCase) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2681 | std::string foo_class = "class Foo { public: Foo(const char*); };"; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2682 | EXPECT_TRUE(matches(foo_class + "void r() { Foo f = Foo(\"hello world\"); }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2683 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | TEST(FunctionalCast, DoesNotMatchOtherCasts) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2687 | std::string FooClass = "class Foo { public: Foo(const char*); };"; |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2688 | EXPECT_TRUE( |
| 2689 | notMatches(FooClass + "void r() { Foo f = (Foo) \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2690 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2691 | EXPECT_TRUE( |
| 2692 | notMatches(FooClass + "void r() { Foo f = \"hello world\"; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2693 | functionalCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
| 2696 | TEST(DynamicCast, MatchesSimpleCase) { |
| 2697 | EXPECT_TRUE(matches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2698 | "B b;" |
| 2699 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2700 | dynamicCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | TEST(StaticCast, MatchesSimpleCase) { |
| 2704 | EXPECT_TRUE(matches("void* p(static_cast<void*>(&p));", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2705 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | TEST(StaticCast, DoesNotMatchOtherCasts) { |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2709 | EXPECT_TRUE(notMatches("char* p = (char*)(&p);", staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2710 | EXPECT_TRUE(notMatches("char q, *p = const_cast<char*>(&q);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2711 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2712 | EXPECT_TRUE(notMatches("void* p = reinterpret_cast<char*>(&p);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2713 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2714 | EXPECT_TRUE(notMatches("struct B { virtual ~B() {} }; struct D : B {};" |
| 2715 | "B b;" |
| 2716 | "D* p = dynamic_cast<D*>(&b);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2717 | staticCastExpr())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
Daniel Jasper | e6d2a96 | 2012-09-18 13:36:17 +0000 | [diff] [blame] | 2720 | TEST(CStyleCast, MatchesSimpleCase) { |
| 2721 | EXPECT_TRUE(matches("int i = (int) 2.2f;", cStyleCastExpr())); |
| 2722 | } |
| 2723 | |
| 2724 | TEST(CStyleCast, DoesNotMatchOtherCasts) { |
| 2725 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);" |
| 2726 | "char q, *r = const_cast<char*>(&q);" |
| 2727 | "void* s = reinterpret_cast<char*>(&s);" |
| 2728 | "struct B { virtual ~B() {} }; struct D : B {};" |
| 2729 | "B b;" |
| 2730 | "D* t = dynamic_cast<D*>(&b);", |
| 2731 | cStyleCastExpr())); |
| 2732 | } |
| 2733 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2734 | TEST(HasDestinationType, MatchesSimpleCase) { |
| 2735 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2736 | staticCastExpr(hasDestinationType( |
| 2737 | pointsTo(TypeMatcher(anything())))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2740 | TEST(HasImplicitDestinationType, MatchesSimpleCase) { |
| 2741 | // This test creates an implicit const cast. |
| 2742 | EXPECT_TRUE(matches("int x; const int i = x;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2743 | implicitCastExpr( |
| 2744 | hasImplicitDestinationType(isInteger())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2745 | // This test creates an implicit array-to-pointer cast. |
| 2746 | EXPECT_TRUE(matches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2747 | implicitCastExpr(hasImplicitDestinationType( |
| 2748 | pointsTo(TypeMatcher(anything())))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | TEST(HasImplicitDestinationType, DoesNotMatchIncorrectly) { |
| 2752 | // This test creates an implicit cast from int to char. |
| 2753 | EXPECT_TRUE(notMatches("char c = 0;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2754 | implicitCastExpr(hasImplicitDestinationType( |
| 2755 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2756 | // This test creates an implicit array-to-pointer cast. |
| 2757 | EXPECT_TRUE(notMatches("int arr[3]; int *p = arr;", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2758 | implicitCastExpr(hasImplicitDestinationType( |
| 2759 | unless(anything()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | TEST(ImplicitCast, MatchesSimpleCase) { |
| 2763 | // This test creates an implicit const cast. |
| 2764 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2765 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2766 | // This test creates an implicit cast from int to char. |
| 2767 | EXPECT_TRUE(matches("char c = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2768 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2769 | // This test creates an implicit array-to-pointer cast. |
| 2770 | EXPECT_TRUE(matches("int arr[6]; int *p = arr;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2771 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | TEST(ImplicitCast, DoesNotMatchIncorrectly) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2775 | // This test verifies that implicitCastExpr() matches exactly when implicit casts |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2776 | // are present, and that it ignores explicit and paren casts. |
| 2777 | |
| 2778 | // These two test cases have no casts. |
| 2779 | EXPECT_TRUE(notMatches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2780 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2781 | EXPECT_TRUE(notMatches("int x = 0, &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2782 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2783 | |
| 2784 | EXPECT_TRUE(notMatches("int x = 0; double d = (double) x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2785 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2786 | EXPECT_TRUE(notMatches("const int *p; int *q = const_cast<int *>(p);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2787 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2788 | |
| 2789 | EXPECT_TRUE(notMatches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2790 | varDecl(hasInitializer(implicitCastExpr())))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | TEST(IgnoringImpCasts, MatchesImpCasts) { |
| 2794 | // This test checks that ignoringImpCasts matches when implicit casts are |
| 2795 | // present and its inner matcher alone does not match. |
| 2796 | // Note that this test creates an implicit const cast. |
| 2797 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2798 | varDecl(hasInitializer(ignoringImpCasts( |
| 2799 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2800 | // This test creates an implict cast from int to char. |
| 2801 | EXPECT_TRUE(matches("char x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2802 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2803 | integerLiteral(equals(0))))))); |
| 2804 | } |
| 2805 | |
| 2806 | TEST(IgnoringImpCasts, DoesNotMatchIncorrectly) { |
| 2807 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2808 | // matcher does not match. |
| 2809 | // Note that the first test creates an implicit const cast. |
| 2810 | EXPECT_TRUE(notMatches("int x; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2811 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2812 | unless(anything())))))); |
| 2813 | EXPECT_TRUE(notMatches("int x; int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2814 | varDecl(hasInitializer(ignoringImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2815 | unless(anything())))))); |
| 2816 | |
| 2817 | // These tests verify that ignoringImplictCasts does not look through explicit |
| 2818 | // casts or parentheses. |
| 2819 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2820 | varDecl(hasInitializer(ignoringImpCasts( |
| 2821 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2822 | EXPECT_TRUE(notMatches("int i = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2823 | varDecl(hasInitializer(ignoringImpCasts( |
| 2824 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2825 | EXPECT_TRUE(notMatches("float i = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2826 | varDecl(hasInitializer(ignoringImpCasts( |
| 2827 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2828 | EXPECT_TRUE(notMatches("float i = float(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2829 | varDecl(hasInitializer(ignoringImpCasts( |
| 2830 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | TEST(IgnoringImpCasts, MatchesWithoutImpCasts) { |
| 2834 | // This test verifies that expressions that do not have implicit casts |
| 2835 | // still match the inner matcher. |
| 2836 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2837 | varDecl(hasInitializer(ignoringImpCasts( |
| 2838 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | TEST(IgnoringParenCasts, MatchesParenCasts) { |
| 2842 | // This test checks that ignoringParenCasts matches when parentheses and/or |
| 2843 | // casts are present and its inner matcher alone does not match. |
| 2844 | EXPECT_TRUE(matches("int x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2845 | varDecl(hasInitializer(ignoringParenCasts( |
| 2846 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2847 | EXPECT_TRUE(matches("int x = (((((0)))));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2848 | varDecl(hasInitializer(ignoringParenCasts( |
| 2849 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2850 | |
| 2851 | // This test creates an implict cast from int to char in addition to the |
| 2852 | // parentheses. |
| 2853 | EXPECT_TRUE(matches("char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2854 | varDecl(hasInitializer(ignoringParenCasts( |
| 2855 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2856 | |
| 2857 | EXPECT_TRUE(matches("char x = (char)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2858 | varDecl(hasInitializer(ignoringParenCasts( |
| 2859 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2860 | EXPECT_TRUE(matches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2861 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2862 | integerLiteral(equals(0))))))); |
| 2863 | } |
| 2864 | |
| 2865 | TEST(IgnoringParenCasts, MatchesWithoutParenCasts) { |
| 2866 | // This test verifies that expressions that do not have any casts still match. |
| 2867 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2868 | varDecl(hasInitializer(ignoringParenCasts( |
| 2869 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | TEST(IgnoringParenCasts, DoesNotMatchIncorrectly) { |
| 2873 | // These tests verify that ignoringImpCasts does not match if the inner |
| 2874 | // matcher does not match. |
| 2875 | EXPECT_TRUE(notMatches("int x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2876 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2877 | unless(anything())))))); |
| 2878 | |
| 2879 | // This test creates an implicit cast from int to char in addition to the |
| 2880 | // parentheses. |
| 2881 | EXPECT_TRUE(notMatches("char x = ((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2882 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2883 | unless(anything())))))); |
| 2884 | |
| 2885 | EXPECT_TRUE(notMatches("char *x = static_cast<char *>((0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2886 | varDecl(hasInitializer(ignoringParenCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2887 | unless(anything())))))); |
| 2888 | } |
| 2889 | |
| 2890 | TEST(IgnoringParenAndImpCasts, MatchesParenImpCasts) { |
| 2891 | // This test checks that ignoringParenAndImpCasts matches when |
| 2892 | // parentheses and/or implicit casts are present and its inner matcher alone |
| 2893 | // does not match. |
| 2894 | // Note that this test creates an implicit const cast. |
| 2895 | EXPECT_TRUE(matches("int x = 0; const int y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2896 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2897 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2898 | // This test creates an implicit cast from int to char. |
| 2899 | EXPECT_TRUE(matches("const char x = (0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2900 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2901 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | TEST(IgnoringParenAndImpCasts, MatchesWithoutParenImpCasts) { |
| 2905 | // This test verifies that expressions that do not have parentheses or |
| 2906 | // implicit casts still match. |
| 2907 | EXPECT_TRUE(matches("int x = 0; int &y = x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2908 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2909 | declRefExpr(to(varDecl(hasName("x"))))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2910 | EXPECT_TRUE(matches("int x = 0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2911 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2912 | integerLiteral(equals(0))))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | TEST(IgnoringParenAndImpCasts, DoesNotMatchIncorrectly) { |
| 2916 | // These tests verify that ignoringParenImpCasts does not match if |
| 2917 | // the inner matcher does not match. |
| 2918 | // This test creates an implicit cast. |
| 2919 | EXPECT_TRUE(notMatches("char c = ((3));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2920 | varDecl(hasInitializer(ignoringParenImpCasts( |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2921 | unless(anything())))))); |
| 2922 | // These tests verify that ignoringParenAndImplictCasts does not look |
| 2923 | // through explicit casts. |
| 2924 | EXPECT_TRUE(notMatches("float y = (float(0));", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2925 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2926 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2927 | EXPECT_TRUE(notMatches("float y = (float)0;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2928 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2929 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2930 | EXPECT_TRUE(notMatches("char* p = static_cast<char*>(0);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2931 | varDecl(hasInitializer(ignoringParenImpCasts( |
| 2932 | integerLiteral()))))); |
Sam Panzer | 089e5b3 | 2012-08-16 16:58:10 +0000 | [diff] [blame] | 2933 | } |
| 2934 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2935 | TEST(HasSourceExpression, MatchesImplicitCasts) { |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2936 | EXPECT_TRUE(matches("class string {}; class URL { public: URL(string s); };" |
| 2937 | "void r() {string a_string; URL url = a_string; }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2938 | implicitCastExpr( |
| 2939 | hasSourceExpression(constructExpr())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2942 | TEST(HasSourceExpression, MatchesExplicitCasts) { |
| 2943 | EXPECT_TRUE(matches("float x = static_cast<float>(42);", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2944 | explicitCastExpr( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2945 | hasSourceExpression(hasDescendant( |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 2946 | expr(integerLiteral())))))); |
Manuel Klimek | 715c956 | 2012-07-25 10:02:02 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2949 | TEST(Statement, DoesNotMatchDeclarations) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2950 | EXPECT_TRUE(notMatches("class X {};", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | TEST(Statement, MatchesCompoundStatments) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2954 | EXPECT_TRUE(matches("void x() {}", stmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | TEST(DeclarationStatement, DoesNotMatchCompoundStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2958 | EXPECT_TRUE(notMatches("void x() {}", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | TEST(DeclarationStatement, MatchesVariableDeclarationStatements) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2962 | EXPECT_TRUE(matches("void x() { int a; }", declStmt())); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2965 | TEST(ExprWithCleanups, MatchesExprWithCleanups) { |
| 2966 | EXPECT_TRUE(matches("struct Foo { ~Foo(); };" |
| 2967 | "const Foo f = Foo();", |
| 2968 | varDecl(hasInitializer(exprWithCleanups())))); |
| 2969 | EXPECT_FALSE(matches("struct Foo { };" |
| 2970 | "const Foo f = Foo();", |
| 2971 | varDecl(hasInitializer(exprWithCleanups())))); |
| 2972 | } |
| 2973 | |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2974 | TEST(InitListExpression, MatchesInitListExpression) { |
| 2975 | EXPECT_TRUE(matches("int a[] = { 1, 2 };", |
| 2976 | initListExpr(hasType(asString("int [2]"))))); |
| 2977 | EXPECT_TRUE(matches("struct B { int x, y; }; B b = { 5, 6 };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2978 | initListExpr(hasType(recordDecl(hasName("B")))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | TEST(UsingDeclaration, MatchesUsingDeclarations) { |
| 2982 | EXPECT_TRUE(matches("namespace X { int x; } using X::x;", |
| 2983 | usingDecl())); |
| 2984 | } |
| 2985 | |
| 2986 | TEST(UsingDeclaration, MatchesShadowUsingDelcarations) { |
| 2987 | EXPECT_TRUE(matches("namespace f { int a; } using f::a;", |
| 2988 | usingDecl(hasAnyUsingShadowDecl(hasName("a"))))); |
| 2989 | } |
| 2990 | |
| 2991 | TEST(UsingDeclaration, MatchesSpecificTarget) { |
| 2992 | EXPECT_TRUE(matches("namespace f { int a; void b(); } using f::b;", |
| 2993 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2994 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2995 | EXPECT_TRUE(notMatches("namespace f { int a; void b(); } using f::a;", |
| 2996 | usingDecl(hasAnyUsingShadowDecl( |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 2997 | hasTargetDecl(functionDecl()))))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | TEST(UsingDeclaration, ThroughUsingDeclaration) { |
| 3001 | EXPECT_TRUE(matches( |
| 3002 | "namespace a { void f(); } using a::f; void g() { f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3003 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 3004 | EXPECT_TRUE(notMatches( |
| 3005 | "namespace a { void f(); } using a::f; void g() { a::f(); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3006 | declRefExpr(throughUsingDecl(anything())))); |
Daniel Jasper | e0e6b9e | 2012-07-10 20:20:19 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3009 | TEST(SingleDecl, IsSingleDecl) { |
| 3010 | StatementMatcher SingleDeclStmt = |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3011 | declStmt(hasSingleDecl(varDecl(hasInitializer(anything())))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3012 | EXPECT_TRUE(matches("void f() {int a = 4;}", SingleDeclStmt)); |
| 3013 | EXPECT_TRUE(notMatches("void f() {int a;}", SingleDeclStmt)); |
| 3014 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
| 3015 | SingleDeclStmt)); |
| 3016 | } |
| 3017 | |
| 3018 | TEST(DeclStmt, ContainsDeclaration) { |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3019 | DeclarationMatcher MatchesInit = varDecl(hasInitializer(anything())); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3020 | |
| 3021 | EXPECT_TRUE(matches("void f() {int a = 4;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3022 | declStmt(containsDeclaration(0, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3023 | EXPECT_TRUE(matches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3024 | declStmt(containsDeclaration(0, MatchesInit), |
| 3025 | containsDeclaration(1, MatchesInit)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3026 | unsigned WrongIndex = 42; |
| 3027 | EXPECT_TRUE(notMatches("void f() {int a = 4, b = 3;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3028 | declStmt(containsDeclaration(WrongIndex, |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3029 | MatchesInit)))); |
| 3030 | } |
| 3031 | |
| 3032 | TEST(DeclCount, DeclCountIsCorrect) { |
| 3033 | EXPECT_TRUE(matches("void f() {int i,j;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3034 | declStmt(declCountIs(2)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3035 | EXPECT_TRUE(notMatches("void f() {int i,j; int k;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3036 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3037 | EXPECT_TRUE(notMatches("void f() {int i,j, k, l;}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3038 | declStmt(declCountIs(3)))); |
Sam Panzer | 425f41b | 2012-08-16 17:20:59 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3041 | TEST(While, MatchesWhileLoops) { |
| 3042 | EXPECT_TRUE(notMatches("void x() {}", whileStmt())); |
| 3043 | EXPECT_TRUE(matches("void x() { while(true); }", whileStmt())); |
| 3044 | EXPECT_TRUE(notMatches("void x() { do {} while(true); }", whileStmt())); |
| 3045 | } |
| 3046 | |
| 3047 | TEST(Do, MatchesDoLoops) { |
| 3048 | EXPECT_TRUE(matches("void x() { do {} while(true); }", doStmt())); |
| 3049 | EXPECT_TRUE(matches("void x() { do ; while(false); }", doStmt())); |
| 3050 | } |
| 3051 | |
| 3052 | TEST(Do, DoesNotMatchWhileLoops) { |
| 3053 | EXPECT_TRUE(notMatches("void x() { while(true) {} }", doStmt())); |
| 3054 | } |
| 3055 | |
| 3056 | TEST(SwitchCase, MatchesCase) { |
| 3057 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchCase())); |
| 3058 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchCase())); |
| 3059 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchCase())); |
| 3060 | EXPECT_TRUE(notMatches("void x() { switch(42) {} }", switchCase())); |
| 3061 | } |
| 3062 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 3063 | TEST(SwitchCase, MatchesSwitch) { |
| 3064 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", switchStmt())); |
| 3065 | EXPECT_TRUE(matches("void x() { switch(42) { default:; } }", switchStmt())); |
| 3066 | EXPECT_TRUE(matches("void x() { switch(42) default:; }", switchStmt())); |
| 3067 | EXPECT_TRUE(notMatches("void x() {}", switchStmt())); |
| 3068 | } |
| 3069 | |
Peter Collingbourne | acf0271 | 2013-05-10 11:52:02 +0000 | [diff] [blame] | 3070 | TEST(SwitchCase, MatchesEachCase) { |
| 3071 | EXPECT_TRUE(notMatches("void x() { switch(42); }", |
| 3072 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 3073 | EXPECT_TRUE(matches("void x() { switch(42) case 42:; }", |
| 3074 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 3075 | EXPECT_TRUE(matches("void x() { switch(42) { case 42:; } }", |
| 3076 | switchStmt(forEachSwitchCase(caseStmt())))); |
| 3077 | EXPECT_TRUE(notMatches( |
| 3078 | "void x() { if (1) switch(42) { case 42: switch (42) { default:; } } }", |
| 3079 | ifStmt(has(switchStmt(forEachSwitchCase(defaultStmt())))))); |
| 3080 | EXPECT_TRUE(matches("void x() { switch(42) { case 1+1: case 4:; } }", |
| 3081 | switchStmt(forEachSwitchCase( |
| 3082 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 3083 | EXPECT_TRUE(notMatches("void x() { switch(42) { case 1+1: case 2+2:; } }", |
| 3084 | switchStmt(forEachSwitchCase( |
| 3085 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 3086 | EXPECT_TRUE(notMatches("void x() { switch(42) { case 1 ... 2:; } }", |
| 3087 | switchStmt(forEachSwitchCase( |
| 3088 | caseStmt(hasCaseConstant(integerLiteral())))))); |
| 3089 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3090 | "void x() { switch (42) { case 1: case 2: case 3: default:; } }", |
| 3091 | switchStmt(forEachSwitchCase(caseStmt().bind("x"))), |
| 3092 | new VerifyIdIsBoundTo<CaseStmt>("x", 3))); |
| 3093 | } |
| 3094 | |
Manuel Klimek | 0696301 | 2013-07-19 11:50:54 +0000 | [diff] [blame] | 3095 | TEST(ForEachConstructorInitializer, MatchesInitializers) { |
| 3096 | EXPECT_TRUE(matches( |
| 3097 | "struct X { X() : i(42), j(42) {} int i, j; };", |
| 3098 | constructorDecl(forEachConstructorInitializer(ctorInitializer())))); |
| 3099 | } |
| 3100 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 3101 | TEST(ExceptionHandling, SimpleCases) { |
| 3102 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", catchStmt())); |
| 3103 | EXPECT_TRUE(matches("void foo() try { } catch(int X) { }", tryStmt())); |
| 3104 | EXPECT_TRUE(notMatches("void foo() try { } catch(int X) { }", throwExpr())); |
| 3105 | EXPECT_TRUE(matches("void foo() try { throw; } catch(int X) { }", |
| 3106 | throwExpr())); |
| 3107 | EXPECT_TRUE(matches("void foo() try { throw 5;} catch(int X) { }", |
| 3108 | throwExpr())); |
| 3109 | } |
| 3110 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3111 | TEST(HasConditionVariableStatement, DoesNotMatchCondition) { |
| 3112 | EXPECT_TRUE(notMatches( |
| 3113 | "void x() { if(true) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3114 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3115 | EXPECT_TRUE(notMatches( |
| 3116 | "void x() { int x; if((x = 42)) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3117 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | TEST(HasConditionVariableStatement, MatchesConditionVariables) { |
| 3121 | EXPECT_TRUE(matches( |
| 3122 | "void x() { if(int* a = 0) {} }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3123 | ifStmt(hasConditionVariableStatement(declStmt())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
| 3126 | TEST(ForEach, BindsOneNode) { |
| 3127 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3128 | recordDecl(hasName("C"), forEach(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3129 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3130 | } |
| 3131 | |
| 3132 | TEST(ForEach, BindsMultipleNodes) { |
| 3133 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { int x; int y; int z; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3134 | recordDecl(hasName("C"), forEach(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3135 | new VerifyIdIsBoundTo<FieldDecl>("f", 3))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | TEST(ForEach, BindsRecursiveCombinations) { |
| 3139 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3140 | "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] | 3141 | recordDecl(hasName("C"), |
| 3142 | forEach(recordDecl(forEach(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3143 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3144 | } |
| 3145 | |
| 3146 | TEST(ForEachDescendant, BindsOneNode) { |
| 3147 | EXPECT_TRUE(matchAndVerifyResultTrue("class C { class D { int x; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3148 | recordDecl(hasName("C"), |
| 3149 | forEachDescendant(fieldDecl(hasName("x")).bind("x"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3150 | new VerifyIdIsBoundTo<FieldDecl>("x", 1))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
Daniel Jasper | 5f684e9 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 3153 | TEST(ForEachDescendant, NestedForEachDescendant) { |
| 3154 | DeclarationMatcher m = recordDecl( |
| 3155 | isDefinition(), decl().bind("x"), hasName("C")); |
| 3156 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3157 | "class A { class B { class C {}; }; };", |
| 3158 | recordDecl(hasName("A"), anyOf(m, forEachDescendant(m))), |
| 3159 | new VerifyIdIsBoundTo<Decl>("x", "C"))); |
| 3160 | |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3161 | // Check that a partial match of 'm' that binds 'x' in the |
| 3162 | // first part of anyOf(m, anything()) will not overwrite the |
| 3163 | // binding created by the earlier binding in the hasDescendant. |
| 3164 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3165 | "class A { class B { class C {}; }; };", |
| 3166 | recordDecl(hasName("A"), allOf(hasDescendant(m), anyOf(m, anything()))), |
| 3167 | new VerifyIdIsBoundTo<Decl>("x", "C"))); |
Daniel Jasper | 5f684e9 | 2012-11-16 18:39:22 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3170 | TEST(ForEachDescendant, BindsMultipleNodes) { |
| 3171 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3172 | "class C { class D { int x; int y; }; " |
| 3173 | " class E { class F { int y; int z; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3174 | recordDecl(hasName("C"), forEachDescendant(fieldDecl().bind("f"))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3175 | new VerifyIdIsBoundTo<FieldDecl>("f", 4))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | TEST(ForEachDescendant, BindsRecursiveCombinations) { |
| 3179 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3180 | "class C { class D { " |
| 3181 | " class E { class F { class G { int y; int z; }; }; }; }; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3182 | recordDecl(hasName("C"), forEachDescendant(recordDecl( |
| 3183 | forEachDescendant(fieldDecl().bind("f"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3184 | new VerifyIdIsBoundTo<FieldDecl>("f", 8))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3187 | TEST(ForEachDescendant, BindsCombinations) { |
| 3188 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3189 | "void f() { if(true) {} if (true) {} while (true) {} if (true) {} while " |
| 3190 | "(true) {} }", |
| 3191 | compoundStmt(forEachDescendant(ifStmt().bind("if")), |
| 3192 | forEachDescendant(whileStmt().bind("while"))), |
| 3193 | new VerifyIdIsBoundTo<IfStmt>("if", 6))); |
| 3194 | } |
| 3195 | |
| 3196 | TEST(Has, DoesNotDeleteBindings) { |
| 3197 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3198 | "class X { int a; };", recordDecl(decl().bind("x"), has(fieldDecl())), |
| 3199 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3200 | } |
| 3201 | |
| 3202 | TEST(LoopingMatchers, DoNotOverwritePreviousMatchResultOnFailure) { |
| 3203 | // Those matchers cover all the cases where an inner matcher is called |
| 3204 | // and there is not a 1:1 relationship between the match of the outer |
| 3205 | // matcher and the match of the inner matcher. |
| 3206 | // The pattern to look for is: |
| 3207 | // ... return InnerMatcher.matches(...); ... |
| 3208 | // In which case no special handling is needed. |
| 3209 | // |
| 3210 | // On the other hand, if there are multiple alternative matches |
| 3211 | // (for example forEach*) or matches might be discarded (for example has*) |
| 3212 | // the implementation must make sure that the discarded matches do not |
| 3213 | // affect the bindings. |
| 3214 | // When new such matchers are added, add a test here that: |
| 3215 | // - matches a simple node, and binds it as the first thing in the matcher: |
| 3216 | // recordDecl(decl().bind("x"), hasName("X"))) |
| 3217 | // - uses the matcher under test afterwards in a way that not the first |
| 3218 | // alternative is matched; for anyOf, that means the first branch |
| 3219 | // would need to return false; for hasAncestor, it means that not |
| 3220 | // the direct parent matches the inner matcher. |
| 3221 | |
| 3222 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3223 | "class X { int y; };", |
| 3224 | recordDecl( |
| 3225 | recordDecl().bind("x"), hasName("::X"), |
| 3226 | anyOf(forEachDescendant(recordDecl(hasName("Y"))), anything())), |
| 3227 | new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1))); |
| 3228 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3229 | "class X {};", recordDecl(recordDecl().bind("x"), hasName("::X"), |
| 3230 | anyOf(unless(anything()), anything())), |
| 3231 | new VerifyIdIsBoundTo<CXXRecordDecl>("x", 1))); |
| 3232 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3233 | "template<typename T1, typename T2> class X {}; X<float, int> x;", |
| 3234 | classTemplateSpecializationDecl( |
| 3235 | decl().bind("x"), |
| 3236 | hasAnyTemplateArgument(refersToType(asString("int")))), |
| 3237 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3238 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3239 | "class X { void f(); void g(); };", |
| 3240 | recordDecl(decl().bind("x"), hasMethod(hasName("g"))), |
| 3241 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3242 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3243 | "class X { X() : a(1), b(2) {} double a; int b; };", |
| 3244 | recordDecl(decl().bind("x"), |
| 3245 | has(constructorDecl( |
| 3246 | hasAnyConstructorInitializer(forField(hasName("b")))))), |
| 3247 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3248 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3249 | "void x(int, int) { x(0, 42); }", |
| 3250 | callExpr(expr().bind("x"), hasAnyArgument(integerLiteral(equals(42)))), |
| 3251 | new VerifyIdIsBoundTo<Expr>("x", 1))); |
| 3252 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3253 | "void x(int, int y) {}", |
| 3254 | functionDecl(decl().bind("x"), hasAnyParameter(hasName("y"))), |
| 3255 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3256 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3257 | "void x() { return; if (true) {} }", |
| 3258 | functionDecl(decl().bind("x"), |
| 3259 | has(compoundStmt(hasAnySubstatement(ifStmt())))), |
| 3260 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3261 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3262 | "namespace X { void b(int); void b(); }" |
| 3263 | "using X::b;", |
| 3264 | usingDecl(decl().bind("x"), hasAnyUsingShadowDecl(hasTargetDecl( |
| 3265 | functionDecl(parameterCountIs(1))))), |
| 3266 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3267 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3268 | "class A{}; class B{}; class C : B, A {};", |
| 3269 | recordDecl(decl().bind("x"), isDerivedFrom("::A")), |
| 3270 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3271 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3272 | "class A{}; typedef A B; typedef A C; typedef A D;" |
| 3273 | "class E : A {};", |
| 3274 | recordDecl(decl().bind("x"), isDerivedFrom("C")), |
| 3275 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3276 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3277 | "class A { class B { void f() {} }; };", |
| 3278 | functionDecl(decl().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 3279 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
| 3280 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3281 | "template <typename T> struct A { struct B {" |
| 3282 | " void f() { if(true) {} }" |
| 3283 | "}; };" |
| 3284 | "void t() { A<int>::B b; b.f(); }", |
| 3285 | ifStmt(stmt().bind("x"), hasAncestor(recordDecl(hasName("::A")))), |
| 3286 | new VerifyIdIsBoundTo<Stmt>("x", 2))); |
| 3287 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3288 | "class A {};", |
| 3289 | recordDecl(hasName("::A"), decl().bind("x"), unless(hasName("fooble"))), |
| 3290 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
Manuel Klimek | 0696301 | 2013-07-19 11:50:54 +0000 | [diff] [blame] | 3291 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3292 | "class A { A() : s(), i(42) {} const char *s; int i; };", |
| 3293 | constructorDecl(hasName("::A::A"), decl().bind("x"), |
| 3294 | forEachConstructorInitializer(forField(hasName("i")))), |
| 3295 | new VerifyIdIsBoundTo<Decl>("x", 1))); |
Manuel Klimek | 054d049 | 2013-06-19 15:42:45 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
Daniel Jasper | 11c9877 | 2012-11-11 22:14:55 +0000 | [diff] [blame] | 3298 | TEST(ForEachDescendant, BindsCorrectNodes) { |
| 3299 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3300 | "class C { void f(); int i; };", |
| 3301 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 3302 | new VerifyIdIsBoundTo<FieldDecl>("decl", 1))); |
| 3303 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3304 | "class C { void f() {} int i; };", |
| 3305 | recordDecl(hasName("C"), forEachDescendant(decl().bind("decl"))), |
| 3306 | new VerifyIdIsBoundTo<FunctionDecl>("decl", 1))); |
| 3307 | } |
| 3308 | |
Manuel Klimek | 152ea0e | 2013-02-04 10:59:20 +0000 | [diff] [blame] | 3309 | TEST(FindAll, BindsNodeOnMatch) { |
| 3310 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3311 | "class A {};", |
| 3312 | recordDecl(hasName("::A"), findAll(recordDecl(hasName("::A")).bind("v"))), |
| 3313 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 1))); |
| 3314 | } |
| 3315 | |
| 3316 | TEST(FindAll, BindsDescendantNodeOnMatch) { |
| 3317 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3318 | "class A { int a; int b; };", |
| 3319 | recordDecl(hasName("::A"), findAll(fieldDecl().bind("v"))), |
| 3320 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 3321 | } |
| 3322 | |
| 3323 | TEST(FindAll, BindsNodeAndDescendantNodesOnOneMatch) { |
| 3324 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3325 | "class A { int a; int b; };", |
| 3326 | recordDecl(hasName("::A"), |
| 3327 | findAll(decl(anyOf(recordDecl(hasName("::A")).bind("v"), |
| 3328 | fieldDecl().bind("v"))))), |
| 3329 | new VerifyIdIsBoundTo<Decl>("v", 3))); |
| 3330 | |
| 3331 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3332 | "class A { class B {}; class C {}; };", |
| 3333 | recordDecl(hasName("::A"), findAll(recordDecl(isDefinition()).bind("v"))), |
| 3334 | new VerifyIdIsBoundTo<CXXRecordDecl>("v", 3))); |
| 3335 | } |
| 3336 | |
Manuel Klimek | 7387673 | 2013-02-04 09:42:38 +0000 | [diff] [blame] | 3337 | TEST(EachOf, TriggersForEachMatch) { |
| 3338 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3339 | "class A { int a; int b; };", |
| 3340 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3341 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3342 | new VerifyIdIsBoundTo<FieldDecl>("v", 2))); |
| 3343 | } |
| 3344 | |
| 3345 | TEST(EachOf, BehavesLikeAnyOfUnlessBothMatch) { |
| 3346 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3347 | "class A { int a; int c; };", |
| 3348 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3349 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3350 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 3351 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3352 | "class A { int c; int b; };", |
| 3353 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3354 | has(fieldDecl(hasName("b")).bind("v")))), |
| 3355 | new VerifyIdIsBoundTo<FieldDecl>("v", 1))); |
| 3356 | EXPECT_TRUE(notMatches( |
| 3357 | "class A { int c; int d; };", |
| 3358 | recordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")), |
| 3359 | has(fieldDecl(hasName("b")).bind("v")))))); |
| 3360 | } |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3361 | |
| 3362 | TEST(IsTemplateInstantiation, MatchesImplicitClassTemplateInstantiation) { |
| 3363 | // Make sure that we can both match the class by name (::X) and by the type |
| 3364 | // the template was instantiated with (via a field). |
| 3365 | |
| 3366 | EXPECT_TRUE(matches( |
| 3367 | "template <typename T> class X {}; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3368 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3369 | |
| 3370 | EXPECT_TRUE(matches( |
| 3371 | "template <typename T> class X { T t; }; class A {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3372 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 3373 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | TEST(IsTemplateInstantiation, MatchesImplicitFunctionTemplateInstantiation) { |
| 3377 | EXPECT_TRUE(matches( |
| 3378 | "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] | 3379 | functionDecl(hasParameter(0, hasType(recordDecl(hasName("A")))), |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3380 | isTemplateInstantiation()))); |
| 3381 | } |
| 3382 | |
| 3383 | TEST(IsTemplateInstantiation, MatchesExplicitClassTemplateInstantiation) { |
| 3384 | EXPECT_TRUE(matches( |
| 3385 | "template <typename T> class X { T t; }; class A {};" |
| 3386 | "template class X<A>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3387 | recordDecl(isTemplateInstantiation(), hasDescendant( |
| 3388 | fieldDecl(hasType(recordDecl(hasName("A")))))))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | TEST(IsTemplateInstantiation, |
| 3392 | MatchesInstantiationOfPartiallySpecializedClassTemplate) { |
| 3393 | EXPECT_TRUE(matches( |
| 3394 | "template <typename T> class X {};" |
| 3395 | "template <typename T> class X<T*> {}; class A {}; X<A*> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3396 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
| 3399 | TEST(IsTemplateInstantiation, |
| 3400 | MatchesInstantiationOfClassTemplateNestedInNonTemplate) { |
| 3401 | EXPECT_TRUE(matches( |
| 3402 | "class A {};" |
| 3403 | "class X {" |
| 3404 | " template <typename U> class Y { U u; };" |
| 3405 | " Y<A> y;" |
| 3406 | "};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3407 | recordDecl(hasName("::X::Y"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | TEST(IsTemplateInstantiation, DoesNotMatchInstantiationsInsideOfInstantiation) { |
| 3411 | // FIXME: Figure out whether this makes sense. It doesn't affect the |
| 3412 | // normal use case as long as the uppermost instantiation always is marked |
| 3413 | // as template instantiation, but it might be confusing as a predicate. |
| 3414 | EXPECT_TRUE(matches( |
| 3415 | "class A {};" |
| 3416 | "template <typename T> class X {" |
| 3417 | " template <typename U> class Y { U u; };" |
| 3418 | " Y<T> y;" |
| 3419 | "}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3420 | recordDecl(hasName("::X<A>::Y"), unless(isTemplateInstantiation())))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | TEST(IsTemplateInstantiation, DoesNotMatchExplicitClassTemplateSpecialization) { |
| 3424 | EXPECT_TRUE(notMatches( |
| 3425 | "template <typename T> class X {}; class A {};" |
| 3426 | "template <> class X<A> {}; X<A> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3427 | recordDecl(hasName("::X"), isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | TEST(IsTemplateInstantiation, DoesNotMatchNonTemplate) { |
| 3431 | EXPECT_TRUE(notMatches( |
| 3432 | "class A {}; class Y { A a; };", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3433 | recordDecl(isTemplateInstantiation()))); |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3436 | TEST(IsExplicitTemplateSpecialization, |
| 3437 | DoesNotMatchPrimaryTemplate) { |
| 3438 | EXPECT_TRUE(notMatches( |
| 3439 | "template <typename T> class X {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3440 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3441 | EXPECT_TRUE(notMatches( |
| 3442 | "template <typename T> void f(T t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3443 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | TEST(IsExplicitTemplateSpecialization, |
| 3447 | DoesNotMatchExplicitTemplateInstantiations) { |
| 3448 | EXPECT_TRUE(notMatches( |
| 3449 | "template <typename T> class X {};" |
| 3450 | "template class X<int>; extern template class X<long>;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3451 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3452 | EXPECT_TRUE(notMatches( |
| 3453 | "template <typename T> void f(T t) {}" |
| 3454 | "template void f(int t); extern template void f(long t);", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3455 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
| 3458 | TEST(IsExplicitTemplateSpecialization, |
| 3459 | DoesNotMatchImplicitTemplateInstantiations) { |
| 3460 | EXPECT_TRUE(notMatches( |
| 3461 | "template <typename T> class X {}; X<int> x;", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3462 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3463 | EXPECT_TRUE(notMatches( |
| 3464 | "template <typename T> void f(T t); void g() { f(10); }", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3465 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | TEST(IsExplicitTemplateSpecialization, |
| 3469 | MatchesExplicitTemplateSpecializations) { |
| 3470 | EXPECT_TRUE(matches( |
| 3471 | "template <typename T> class X {};" |
| 3472 | "template<> class X<int> {};", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3473 | recordDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3474 | EXPECT_TRUE(matches( |
| 3475 | "template <typename T> void f(T t) {}" |
| 3476 | "template<> void f(int t) {}", |
Daniel Jasper | 2dc75ed | 2012-08-24 05:12:34 +0000 | [diff] [blame] | 3477 | functionDecl(isExplicitTemplateSpecialization()))); |
Dmitri Gribenko | 8456ae6 | 2012-08-17 18:42:47 +0000 | [diff] [blame] | 3478 | } |
| 3479 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3480 | TEST(HasAncenstor, MatchesDeclarationAncestors) { |
| 3481 | EXPECT_TRUE(matches( |
| 3482 | "class A { class B { class C {}; }; };", |
| 3483 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("A")))))); |
| 3484 | } |
| 3485 | |
| 3486 | TEST(HasAncenstor, FailsIfNoAncestorMatches) { |
| 3487 | EXPECT_TRUE(notMatches( |
| 3488 | "class A { class B { class C {}; }; };", |
| 3489 | recordDecl(hasName("C"), hasAncestor(recordDecl(hasName("X")))))); |
| 3490 | } |
| 3491 | |
| 3492 | TEST(HasAncestor, MatchesDeclarationsThatGetVisitedLater) { |
| 3493 | EXPECT_TRUE(matches( |
| 3494 | "class A { class B { void f() { C c; } class C {}; }; };", |
| 3495 | varDecl(hasName("c"), hasType(recordDecl(hasName("C"), |
| 3496 | hasAncestor(recordDecl(hasName("A")))))))); |
| 3497 | } |
| 3498 | |
| 3499 | TEST(HasAncenstor, MatchesStatementAncestors) { |
| 3500 | EXPECT_TRUE(matches( |
| 3501 | "void f() { if (true) { while (false) { 42; } } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3502 | integerLiteral(equals(42), hasAncestor(ifStmt())))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | TEST(HasAncestor, DrillsThroughDifferentHierarchies) { |
| 3506 | EXPECT_TRUE(matches( |
| 3507 | "void f() { if (true) { int x = 42; } }", |
Daniel Jasper | 3680b4f | 2012-09-18 13:09:13 +0000 | [diff] [blame] | 3508 | integerLiteral(equals(42), hasAncestor(functionDecl(hasName("f")))))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | TEST(HasAncestor, BindsRecursiveCombinations) { |
| 3512 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3513 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3514 | fieldDecl(hasAncestor(recordDecl(hasAncestor(recordDecl().bind("r"))))), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3515 | new VerifyIdIsBoundTo<CXXRecordDecl>("r", 1))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | TEST(HasAncestor, BindsCombinationsWithHasDescendant) { |
| 3519 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3520 | "class C { class D { class E { class F { int y; }; }; }; };", |
| 3521 | fieldDecl(hasAncestor( |
| 3522 | decl( |
| 3523 | hasDescendant(recordDecl(isDefinition(), |
| 3524 | hasAncestor(recordDecl()))) |
| 3525 | ).bind("d") |
| 3526 | )), |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3527 | new VerifyIdIsBoundTo<CXXRecordDecl>("d", "E"))); |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 3530 | TEST(HasAncestor, MatchesClosestAncestor) { |
| 3531 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3532 | "template <typename T> struct C {" |
| 3533 | " void f(int) {" |
| 3534 | " struct I { void g(T) { int x; } } i; i.g(42);" |
| 3535 | " }" |
| 3536 | "};" |
| 3537 | "template struct C<int>;", |
| 3538 | varDecl(hasName("x"), |
| 3539 | hasAncestor(functionDecl(hasParameter( |
| 3540 | 0, varDecl(hasType(asString("int"))))).bind("f"))).bind("v"), |
| 3541 | new VerifyIdIsBoundTo<FunctionDecl>("f", "g", 2))); |
| 3542 | } |
| 3543 | |
Manuel Klimek | 579b120 | 2012-09-07 09:26:10 +0000 | [diff] [blame] | 3544 | TEST(HasAncestor, MatchesInTemplateInstantiations) { |
| 3545 | EXPECT_TRUE(matches( |
| 3546 | "template <typename T> struct A { struct B { struct C { T t; }; }; }; " |
| 3547 | "A<int>::B::C a;", |
| 3548 | fieldDecl(hasType(asString("int")), |
| 3549 | hasAncestor(recordDecl(hasName("A")))))); |
| 3550 | } |
| 3551 | |
| 3552 | TEST(HasAncestor, MatchesInImplicitCode) { |
| 3553 | EXPECT_TRUE(matches( |
| 3554 | "struct X {}; struct A { A() {} X x; };", |
| 3555 | constructorDecl( |
| 3556 | hasAnyConstructorInitializer(withInitializer(expr( |
| 3557 | hasAncestor(recordDecl(hasName("A"))))))))); |
| 3558 | } |
| 3559 | |
Daniel Jasper | c99a3ad | 2012-10-22 16:26:51 +0000 | [diff] [blame] | 3560 | TEST(HasParent, MatchesOnlyParent) { |
| 3561 | EXPECT_TRUE(matches( |
| 3562 | "void f() { if (true) { int x = 42; } }", |
| 3563 | compoundStmt(hasParent(ifStmt())))); |
| 3564 | EXPECT_TRUE(notMatches( |
| 3565 | "void f() { for (;;) { int x = 42; } }", |
| 3566 | compoundStmt(hasParent(ifStmt())))); |
| 3567 | EXPECT_TRUE(notMatches( |
| 3568 | "void f() { if (true) for (;;) { int x = 42; } }", |
| 3569 | compoundStmt(hasParent(ifStmt())))); |
| 3570 | } |
| 3571 | |
Manuel Klimek | 30ace37 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 3572 | TEST(HasAncestor, MatchesAllAncestors) { |
| 3573 | EXPECT_TRUE(matches( |
| 3574 | "template <typename T> struct C { static void f() { 42; } };" |
| 3575 | "void t() { C<int>::f(); }", |
| 3576 | integerLiteral( |
| 3577 | equals(42), |
| 3578 | allOf(hasAncestor(recordDecl(isTemplateInstantiation())), |
| 3579 | hasAncestor(recordDecl(unless(isTemplateInstantiation()))))))); |
| 3580 | } |
| 3581 | |
| 3582 | TEST(HasParent, MatchesAllParents) { |
| 3583 | EXPECT_TRUE(matches( |
| 3584 | "template <typename T> struct C { static void f() { 42; } };" |
| 3585 | "void t() { C<int>::f(); }", |
| 3586 | integerLiteral( |
| 3587 | equals(42), |
| 3588 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3589 | hasParent(recordDecl(isTemplateInstantiation()))))))))); |
| 3590 | EXPECT_TRUE(matches( |
| 3591 | "template <typename T> struct C { static void f() { 42; } };" |
| 3592 | "void t() { C<int>::f(); }", |
| 3593 | integerLiteral( |
| 3594 | equals(42), |
| 3595 | hasParent(compoundStmt(hasParent(functionDecl( |
| 3596 | hasParent(recordDecl(unless(isTemplateInstantiation())))))))))); |
| 3597 | EXPECT_TRUE(matches( |
| 3598 | "template <typename T> struct C { static void f() { 42; } };" |
| 3599 | "void t() { C<int>::f(); }", |
| 3600 | integerLiteral(equals(42), |
| 3601 | hasParent(compoundStmt(allOf( |
| 3602 | hasParent(functionDecl( |
| 3603 | hasParent(recordDecl(isTemplateInstantiation())))), |
| 3604 | hasParent(functionDecl(hasParent(recordDecl( |
| 3605 | unless(isTemplateInstantiation()))))))))))); |
Manuel Klimek | 374516c | 2013-03-14 16:33:21 +0000 | [diff] [blame] | 3606 | EXPECT_TRUE( |
| 3607 | notMatches("template <typename T> struct C { static void f() {} };" |
| 3608 | "void t() { C<int>::f(); }", |
| 3609 | compoundStmt(hasParent(recordDecl())))); |
Manuel Klimek | 30ace37 | 2012-12-06 14:42:48 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3612 | TEST(TypeMatching, MatchesTypes) { |
| 3613 | EXPECT_TRUE(matches("struct S {};", qualType().bind("loc"))); |
| 3614 | } |
| 3615 | |
| 3616 | TEST(TypeMatching, MatchesArrayTypes) { |
| 3617 | EXPECT_TRUE(matches("int a[] = {2,3};", arrayType())); |
| 3618 | EXPECT_TRUE(matches("int a[42];", arrayType())); |
| 3619 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", arrayType())); |
| 3620 | |
| 3621 | EXPECT_TRUE(notMatches("struct A {}; A a[7];", |
| 3622 | arrayType(hasElementType(builtinType())))); |
| 3623 | |
| 3624 | EXPECT_TRUE(matches( |
| 3625 | "int const a[] = { 2, 3 };", |
| 3626 | qualType(arrayType(hasElementType(builtinType()))))); |
| 3627 | EXPECT_TRUE(matches( |
| 3628 | "int const a[] = { 2, 3 };", |
| 3629 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3630 | EXPECT_TRUE(matches( |
| 3631 | "typedef const int T; T x[] = { 1, 2 };", |
| 3632 | qualType(isConstQualified(), arrayType()))); |
| 3633 | |
| 3634 | EXPECT_TRUE(notMatches( |
| 3635 | "int a[] = { 2, 3 };", |
| 3636 | qualType(isConstQualified(), arrayType(hasElementType(builtinType()))))); |
| 3637 | EXPECT_TRUE(notMatches( |
| 3638 | "int a[] = { 2, 3 };", |
| 3639 | qualType(arrayType(hasElementType(isConstQualified(), builtinType()))))); |
| 3640 | EXPECT_TRUE(notMatches( |
| 3641 | "int const a[] = { 2, 3 };", |
| 3642 | qualType(arrayType(hasElementType(builtinType())), |
| 3643 | unless(isConstQualified())))); |
| 3644 | |
| 3645 | EXPECT_TRUE(matches("int a[2];", |
| 3646 | constantArrayType(hasElementType(builtinType())))); |
| 3647 | EXPECT_TRUE(matches("const int a = 0;", qualType(isInteger()))); |
| 3648 | } |
| 3649 | |
| 3650 | TEST(TypeMatching, MatchesComplexTypes) { |
| 3651 | EXPECT_TRUE(matches("_Complex float f;", complexType())); |
| 3652 | EXPECT_TRUE(matches( |
| 3653 | "_Complex float f;", |
| 3654 | complexType(hasElementType(builtinType())))); |
| 3655 | EXPECT_TRUE(notMatches( |
| 3656 | "_Complex float f;", |
| 3657 | complexType(hasElementType(isInteger())))); |
| 3658 | } |
| 3659 | |
| 3660 | TEST(TypeMatching, MatchesConstantArrayTypes) { |
| 3661 | EXPECT_TRUE(matches("int a[2];", constantArrayType())); |
| 3662 | EXPECT_TRUE(notMatches( |
| 3663 | "void f() { int a[] = { 2, 3 }; int b[a[0]]; }", |
| 3664 | constantArrayType(hasElementType(builtinType())))); |
| 3665 | |
| 3666 | EXPECT_TRUE(matches("int a[42];", constantArrayType(hasSize(42)))); |
| 3667 | EXPECT_TRUE(matches("int b[2*21];", constantArrayType(hasSize(42)))); |
| 3668 | EXPECT_TRUE(notMatches("int c[41], d[43];", constantArrayType(hasSize(42)))); |
| 3669 | } |
| 3670 | |
| 3671 | TEST(TypeMatching, MatchesDependentSizedArrayTypes) { |
| 3672 | EXPECT_TRUE(matches( |
| 3673 | "template <typename T, int Size> class array { T data[Size]; };", |
| 3674 | dependentSizedArrayType())); |
| 3675 | EXPECT_TRUE(notMatches( |
| 3676 | "int a[42]; int b[] = { 2, 3 }; void f() { int c[b[0]]; }", |
| 3677 | dependentSizedArrayType())); |
| 3678 | } |
| 3679 | |
| 3680 | TEST(TypeMatching, MatchesIncompleteArrayType) { |
| 3681 | EXPECT_TRUE(matches("int a[] = { 2, 3 };", incompleteArrayType())); |
| 3682 | EXPECT_TRUE(matches("void f(int a[]) {}", incompleteArrayType())); |
| 3683 | |
| 3684 | EXPECT_TRUE(notMatches("int a[42]; void f() { int b[a[0]]; }", |
| 3685 | incompleteArrayType())); |
| 3686 | } |
| 3687 | |
| 3688 | TEST(TypeMatching, MatchesVariableArrayType) { |
| 3689 | EXPECT_TRUE(matches("void f(int b) { int a[b]; }", variableArrayType())); |
| 3690 | EXPECT_TRUE(notMatches("int a[] = {2, 3}; int b[42];", variableArrayType())); |
| 3691 | |
| 3692 | EXPECT_TRUE(matches( |
| 3693 | "void f(int b) { int a[b]; }", |
| 3694 | variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to( |
| 3695 | varDecl(hasName("b"))))))))); |
| 3696 | } |
| 3697 | |
| 3698 | TEST(TypeMatching, MatchesAtomicTypes) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3699 | if (llvm::Triple(llvm::sys::getDefaultTargetTriple()).getOS() != |
| 3700 | llvm::Triple::Win32) { |
| 3701 | // FIXME: Make this work for MSVC. |
| 3702 | EXPECT_TRUE(matches("_Atomic(int) i;", atomicType())); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3703 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3704 | EXPECT_TRUE(matches("_Atomic(int) i;", |
| 3705 | atomicType(hasValueType(isInteger())))); |
| 3706 | EXPECT_TRUE(notMatches("_Atomic(float) f;", |
| 3707 | atomicType(hasValueType(isInteger())))); |
| 3708 | } |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
| 3711 | TEST(TypeMatching, MatchesAutoTypes) { |
| 3712 | EXPECT_TRUE(matches("auto i = 2;", autoType())); |
| 3713 | EXPECT_TRUE(matches("int v[] = { 2, 3 }; void f() { for (int i : v) {} }", |
| 3714 | autoType())); |
| 3715 | |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3716 | // FIXME: Matching against the type-as-written can't work here, because the |
| 3717 | // type as written was not deduced. |
| 3718 | //EXPECT_TRUE(matches("auto a = 1;", |
| 3719 | // autoType(hasDeducedType(isInteger())))); |
| 3720 | //EXPECT_TRUE(notMatches("auto b = 2.0;", |
| 3721 | // autoType(hasDeducedType(isInteger())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
Daniel Jasper | a267cf6 | 2012-10-29 10:14:44 +0000 | [diff] [blame] | 3724 | TEST(TypeMatching, MatchesFunctionTypes) { |
| 3725 | EXPECT_TRUE(matches("int (*f)(int);", functionType())); |
| 3726 | EXPECT_TRUE(matches("void f(int i) {}", functionType())); |
| 3727 | } |
| 3728 | |
Edwin Vane | 88be2fd | 2013-04-01 18:33:34 +0000 | [diff] [blame] | 3729 | TEST(TypeMatching, MatchesParenType) { |
| 3730 | EXPECT_TRUE( |
| 3731 | matches("int (*array)[4];", varDecl(hasType(pointsTo(parenType()))))); |
| 3732 | EXPECT_TRUE(notMatches("int *array[4];", varDecl(hasType(parenType())))); |
| 3733 | |
| 3734 | EXPECT_TRUE(matches( |
| 3735 | "int (*ptr_to_func)(int);", |
| 3736 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 3737 | EXPECT_TRUE(notMatches( |
| 3738 | "int (*ptr_to_array)[4];", |
| 3739 | varDecl(hasType(pointsTo(parenType(innerType(functionType()))))))); |
| 3740 | } |
| 3741 | |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3742 | TEST(TypeMatching, PointerTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3743 | // FIXME: Reactive when these tests can be more specific (not matching |
| 3744 | // implicit code on certain platforms), likely when we have hasDescendant for |
| 3745 | // Types/TypeLocs. |
| 3746 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3747 | // "int* a;", |
| 3748 | // pointerTypeLoc(pointeeLoc(typeLoc().bind("loc"))), |
| 3749 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
| 3750 | //EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3751 | // "int* a;", |
| 3752 | // pointerTypeLoc().bind("loc"), |
| 3753 | // new VerifyIdIsBoundTo<TypeLoc>("loc", 1))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3754 | EXPECT_TRUE(matches( |
| 3755 | "int** a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3756 | loc(pointerType(pointee(qualType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3757 | EXPECT_TRUE(matches( |
| 3758 | "int** a;", |
| 3759 | loc(pointerType(pointee(pointerType()))))); |
| 3760 | EXPECT_TRUE(matches( |
| 3761 | "int* b; int* * const a = &b;", |
| 3762 | loc(qualType(isConstQualified(), pointerType())))); |
| 3763 | |
| 3764 | std::string Fragment = "struct A { int i; }; int A::* ptr = &A::i;"; |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3765 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3766 | hasType(blockPointerType())))); |
| 3767 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3768 | hasType(memberPointerType())))); |
| 3769 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3770 | hasType(pointerType())))); |
| 3771 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3772 | hasType(referenceType())))); |
Edwin Vane | f4b4804 | 2013-03-07 15:44:40 +0000 | [diff] [blame] | 3773 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3774 | hasType(lValueReferenceType())))); |
| 3775 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3776 | hasType(rValueReferenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3777 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3778 | Fragment = "int *ptr;"; |
| 3779 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3780 | hasType(blockPointerType())))); |
| 3781 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3782 | hasType(memberPointerType())))); |
| 3783 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ptr"), |
| 3784 | hasType(pointerType())))); |
| 3785 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ptr"), |
| 3786 | hasType(referenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3787 | |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3788 | Fragment = "int a; int &ref = a;"; |
| 3789 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3790 | hasType(blockPointerType())))); |
| 3791 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3792 | hasType(memberPointerType())))); |
| 3793 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3794 | hasType(pointerType())))); |
| 3795 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3796 | hasType(referenceType())))); |
Edwin Vane | f4b4804 | 2013-03-07 15:44:40 +0000 | [diff] [blame] | 3797 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3798 | hasType(lValueReferenceType())))); |
| 3799 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3800 | hasType(rValueReferenceType())))); |
| 3801 | |
| 3802 | Fragment = "int &&ref = 2;"; |
| 3803 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3804 | hasType(blockPointerType())))); |
| 3805 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3806 | hasType(memberPointerType())))); |
| 3807 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3808 | hasType(pointerType())))); |
| 3809 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3810 | hasType(referenceType())))); |
| 3811 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("ref"), |
| 3812 | hasType(lValueReferenceType())))); |
| 3813 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("ref"), |
| 3814 | hasType(rValueReferenceType())))); |
| 3815 | } |
| 3816 | |
| 3817 | TEST(TypeMatching, AutoRefTypes) { |
| 3818 | std::string Fragment = "auto a = 1;" |
| 3819 | "auto b = a;" |
| 3820 | "auto &c = a;" |
| 3821 | "auto &&d = c;" |
| 3822 | "auto &&e = 2;"; |
| 3823 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("a"), |
| 3824 | hasType(referenceType())))); |
| 3825 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("b"), |
| 3826 | hasType(referenceType())))); |
| 3827 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 3828 | hasType(referenceType())))); |
| 3829 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("c"), |
| 3830 | hasType(lValueReferenceType())))); |
| 3831 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("c"), |
| 3832 | hasType(rValueReferenceType())))); |
| 3833 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 3834 | hasType(referenceType())))); |
| 3835 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("d"), |
| 3836 | hasType(lValueReferenceType())))); |
| 3837 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("d"), |
| 3838 | hasType(rValueReferenceType())))); |
| 3839 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 3840 | hasType(referenceType())))); |
| 3841 | EXPECT_TRUE(notMatches(Fragment, varDecl(hasName("e"), |
| 3842 | hasType(lValueReferenceType())))); |
| 3843 | EXPECT_TRUE(matches(Fragment, varDecl(hasName("e"), |
| 3844 | hasType(rValueReferenceType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | TEST(TypeMatching, PointeeTypes) { |
| 3848 | EXPECT_TRUE(matches("int b; int &a = b;", |
| 3849 | referenceType(pointee(builtinType())))); |
| 3850 | EXPECT_TRUE(matches("int *a;", pointerType(pointee(builtinType())))); |
| 3851 | |
| 3852 | EXPECT_TRUE(matches("int *a;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3853 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3854 | |
| 3855 | EXPECT_TRUE(matches( |
| 3856 | "int const *A;", |
| 3857 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3858 | EXPECT_TRUE(notMatches( |
| 3859 | "int *A;", |
| 3860 | pointerType(pointee(isConstQualified(), builtinType())))); |
| 3861 | } |
| 3862 | |
| 3863 | TEST(TypeMatching, MatchesPointersToConstTypes) { |
| 3864 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
| 3865 | loc(pointerType()))); |
| 3866 | EXPECT_TRUE(matches("int b; int * const a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3867 | loc(pointerType()))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3868 | EXPECT_TRUE(matches( |
| 3869 | "int b; const int * a = &b;", |
David Blaikie | 5be093c | 2013-02-18 19:04:16 +0000 | [diff] [blame] | 3870 | loc(pointerType(pointee(builtinType()))))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3871 | EXPECT_TRUE(matches( |
| 3872 | "int b; const int * a = &b;", |
| 3873 | pointerType(pointee(builtinType())))); |
| 3874 | } |
| 3875 | |
| 3876 | TEST(TypeMatching, MatchesTypedefTypes) { |
Daniel Jasper | 1802daf | 2012-10-17 13:35:36 +0000 | [diff] [blame] | 3877 | EXPECT_TRUE(matches("typedef int X; X a;", varDecl(hasName("a"), |
| 3878 | hasType(typedefType())))); |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 3881 | TEST(TypeMatching, MatchesTemplateSpecializationType) { |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3882 | EXPECT_TRUE(matches("template <typename T> class A{}; A<int> a;", |
Edwin Vane | 3abf778 | 2013-02-25 14:49:29 +0000 | [diff] [blame] | 3883 | templateSpecializationType())); |
| 3884 | } |
| 3885 | |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3886 | TEST(TypeMatching, MatchesRecordType) { |
| 3887 | EXPECT_TRUE(matches("class C{}; C c;", recordType())); |
Manuel Klimek | 0cc798f | 2013-02-27 11:56:58 +0000 | [diff] [blame] | 3888 | EXPECT_TRUE(matches("struct S{}; S s;", |
| 3889 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
| 3890 | EXPECT_TRUE(notMatches("int i;", |
| 3891 | recordType(hasDeclaration(recordDecl(hasName("S")))))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | TEST(TypeMatching, MatchesElaboratedType) { |
| 3895 | EXPECT_TRUE(matches( |
| 3896 | "namespace N {" |
| 3897 | " namespace M {" |
| 3898 | " class D {};" |
| 3899 | " }" |
| 3900 | "}" |
| 3901 | "N::M::D d;", elaboratedType())); |
| 3902 | EXPECT_TRUE(matches("class C {} c;", elaboratedType())); |
| 3903 | EXPECT_TRUE(notMatches("class C {}; C c;", elaboratedType())); |
| 3904 | } |
| 3905 | |
| 3906 | TEST(ElaboratedTypeNarrowing, hasQualifier) { |
| 3907 | EXPECT_TRUE(matches( |
| 3908 | "namespace N {" |
| 3909 | " namespace M {" |
| 3910 | " class D {};" |
| 3911 | " }" |
| 3912 | "}" |
| 3913 | "N::M::D d;", |
| 3914 | elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
| 3915 | EXPECT_TRUE(notMatches( |
| 3916 | "namespace M {" |
| 3917 | " class D {};" |
| 3918 | "}" |
| 3919 | "M::D d;", |
| 3920 | elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))))); |
Edwin Vane | aec89ac | 2013-03-04 17:51:00 +0000 | [diff] [blame] | 3921 | EXPECT_TRUE(notMatches( |
| 3922 | "struct D {" |
| 3923 | "} d;", |
| 3924 | elaboratedType(hasQualifier(nestedNameSpecifier())))); |
Edwin Vane | 742d9e7 | 2013-02-25 20:43:32 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | TEST(ElaboratedTypeNarrowing, namesType) { |
| 3928 | EXPECT_TRUE(matches( |
| 3929 | "namespace N {" |
| 3930 | " namespace M {" |
| 3931 | " class D {};" |
| 3932 | " }" |
| 3933 | "}" |
| 3934 | "N::M::D d;", |
| 3935 | elaboratedType(elaboratedType(namesType(recordType( |
| 3936 | hasDeclaration(namedDecl(hasName("D"))))))))); |
| 3937 | EXPECT_TRUE(notMatches( |
| 3938 | "namespace M {" |
| 3939 | " class D {};" |
| 3940 | "}" |
| 3941 | "M::D d;", |
| 3942 | elaboratedType(elaboratedType(namesType(typedefType()))))); |
| 3943 | } |
| 3944 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3945 | TEST(NNS, MatchesNestedNameSpecifiers) { |
| 3946 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", |
| 3947 | nestedNameSpecifier())); |
| 3948 | EXPECT_TRUE(matches("template <typename T> class A { typename T::B b; };", |
| 3949 | nestedNameSpecifier())); |
| 3950 | EXPECT_TRUE(matches("struct A { void f(); }; void A::f() {}", |
| 3951 | nestedNameSpecifier())); |
| 3952 | |
| 3953 | EXPECT_TRUE(matches( |
| 3954 | "struct A { static void f() {} }; void g() { A::f(); }", |
| 3955 | nestedNameSpecifier())); |
| 3956 | EXPECT_TRUE(notMatches( |
| 3957 | "struct A { static void f() {} }; void g(A* a) { a->f(); }", |
| 3958 | nestedNameSpecifier())); |
| 3959 | } |
| 3960 | |
Daniel Jasper | b54b764 | 2012-09-20 14:12:57 +0000 | [diff] [blame] | 3961 | TEST(NullStatement, SimpleCases) { |
| 3962 | EXPECT_TRUE(matches("void f() {int i;;}", nullStmt())); |
| 3963 | EXPECT_TRUE(notMatches("void f() {int i;}", nullStmt())); |
| 3964 | } |
| 3965 | |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 3966 | TEST(NNS, MatchesTypes) { |
| 3967 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3968 | specifiesType(hasDeclaration(recordDecl(hasName("A"))))); |
| 3969 | EXPECT_TRUE(matches("struct A { struct B {}; }; A::B b;", Matcher)); |
| 3970 | EXPECT_TRUE(matches("struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 3971 | Matcher)); |
| 3972 | EXPECT_TRUE(notMatches("namespace A { struct B {}; } A::B b;", Matcher)); |
| 3973 | } |
| 3974 | |
| 3975 | TEST(NNS, MatchesNamespaceDecls) { |
| 3976 | NestedNameSpecifierMatcher Matcher = nestedNameSpecifier( |
| 3977 | specifiesNamespace(hasName("ns"))); |
| 3978 | EXPECT_TRUE(matches("namespace ns { struct A {}; } ns::A a;", Matcher)); |
| 3979 | EXPECT_TRUE(notMatches("namespace xx { struct A {}; } xx::A a;", Matcher)); |
| 3980 | EXPECT_TRUE(notMatches("struct ns { struct A {}; }; ns::A a;", Matcher)); |
| 3981 | } |
| 3982 | |
| 3983 | TEST(NNS, BindsNestedNameSpecifiers) { |
| 3984 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3985 | "namespace ns { struct E { struct B {}; }; } ns::E::B b;", |
| 3986 | nestedNameSpecifier(specifiesType(asString("struct ns::E"))).bind("nns"), |
| 3987 | new VerifyIdIsBoundTo<NestedNameSpecifier>("nns", "ns::struct E::"))); |
| 3988 | } |
| 3989 | |
| 3990 | TEST(NNS, BindsNestedNameSpecifierLocs) { |
| 3991 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 3992 | "namespace ns { struct B {}; } ns::B b;", |
| 3993 | loc(nestedNameSpecifier()).bind("loc"), |
| 3994 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("loc", 1))); |
| 3995 | } |
| 3996 | |
| 3997 | TEST(NNS, MatchesNestedNameSpecifierPrefixes) { |
| 3998 | EXPECT_TRUE(matches( |
| 3999 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
| 4000 | nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))))); |
| 4001 | EXPECT_TRUE(matches( |
| 4002 | "struct A { struct B { struct C {}; }; }; A::B::C c;", |
Daniel Jasper | ce62007 | 2012-10-17 08:52:59 +0000 | [diff] [blame] | 4003 | nestedNameSpecifierLoc(hasPrefix( |
| 4004 | specifiesTypeLoc(loc(qualType(asString("struct A")))))))); |
Daniel Jasper | a756443 | 2012-09-13 13:11:25 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Daniel Jasper | d1ce3c1 | 2012-10-30 15:42:00 +0000 | [diff] [blame] | 4007 | TEST(NNS, DescendantsOfNestedNameSpecifiers) { |
| 4008 | std::string Fragment = |
| 4009 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 4010 | "void f() { a::A::B::C c; }"; |
| 4011 | EXPECT_TRUE(matches( |
| 4012 | Fragment, |
| 4013 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 4014 | hasDescendant(nestedNameSpecifier( |
| 4015 | specifiesNamespace(hasName("a"))))))); |
| 4016 | EXPECT_TRUE(notMatches( |
| 4017 | Fragment, |
| 4018 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 4019 | has(nestedNameSpecifier( |
| 4020 | specifiesNamespace(hasName("a"))))))); |
| 4021 | EXPECT_TRUE(matches( |
| 4022 | Fragment, |
| 4023 | nestedNameSpecifier(specifiesType(asString("struct a::A")), |
| 4024 | has(nestedNameSpecifier( |
| 4025 | specifiesNamespace(hasName("a"))))))); |
| 4026 | |
| 4027 | // Not really useful because a NestedNameSpecifier can af at most one child, |
| 4028 | // but to complete the interface. |
| 4029 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4030 | Fragment, |
| 4031 | nestedNameSpecifier(specifiesType(asString("struct a::A::B")), |
| 4032 | forEach(nestedNameSpecifier().bind("x"))), |
| 4033 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 1))); |
| 4034 | } |
| 4035 | |
| 4036 | TEST(NNS, NestedNameSpecifiersAsDescendants) { |
| 4037 | std::string Fragment = |
| 4038 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 4039 | "void f() { a::A::B::C c; }"; |
| 4040 | EXPECT_TRUE(matches( |
| 4041 | Fragment, |
| 4042 | decl(hasDescendant(nestedNameSpecifier(specifiesType( |
| 4043 | asString("struct a::A"))))))); |
| 4044 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4045 | Fragment, |
| 4046 | functionDecl(hasName("f"), |
| 4047 | forEachDescendant(nestedNameSpecifier().bind("x"))), |
| 4048 | // Nested names: a, a::A and a::A::B. |
| 4049 | new VerifyIdIsBoundTo<NestedNameSpecifier>("x", 3))); |
| 4050 | } |
| 4051 | |
| 4052 | TEST(NNSLoc, DescendantsOfNestedNameSpecifierLocs) { |
| 4053 | std::string Fragment = |
| 4054 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 4055 | "void f() { a::A::B::C c; }"; |
| 4056 | EXPECT_TRUE(matches( |
| 4057 | Fragment, |
| 4058 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 4059 | hasDescendant(loc(nestedNameSpecifier( |
| 4060 | specifiesNamespace(hasName("a")))))))); |
| 4061 | EXPECT_TRUE(notMatches( |
| 4062 | Fragment, |
| 4063 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 4064 | has(loc(nestedNameSpecifier( |
| 4065 | specifiesNamespace(hasName("a")))))))); |
| 4066 | EXPECT_TRUE(matches( |
| 4067 | Fragment, |
| 4068 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A"))), |
| 4069 | has(loc(nestedNameSpecifier( |
| 4070 | specifiesNamespace(hasName("a")))))))); |
| 4071 | |
| 4072 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4073 | Fragment, |
| 4074 | nestedNameSpecifierLoc(loc(specifiesType(asString("struct a::A::B"))), |
| 4075 | forEach(nestedNameSpecifierLoc().bind("x"))), |
| 4076 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 1))); |
| 4077 | } |
| 4078 | |
| 4079 | TEST(NNSLoc, NestedNameSpecifierLocsAsDescendants) { |
| 4080 | std::string Fragment = |
| 4081 | "namespace a { struct A { struct B { struct C {}; }; }; };" |
| 4082 | "void f() { a::A::B::C c; }"; |
| 4083 | EXPECT_TRUE(matches( |
| 4084 | Fragment, |
| 4085 | decl(hasDescendant(loc(nestedNameSpecifier(specifiesType( |
| 4086 | asString("struct a::A")))))))); |
| 4087 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4088 | Fragment, |
| 4089 | functionDecl(hasName("f"), |
| 4090 | forEachDescendant(nestedNameSpecifierLoc().bind("x"))), |
| 4091 | // Nested names: a, a::A and a::A::B. |
| 4092 | new VerifyIdIsBoundTo<NestedNameSpecifierLoc>("x", 3))); |
| 4093 | } |
| 4094 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4095 | template <typename T> class VerifyMatchOnNode : public BoundNodesCallback { |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4096 | public: |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4097 | VerifyMatchOnNode(StringRef Id, const internal::Matcher<T> &InnerMatcher, |
| 4098 | StringRef InnerId) |
| 4099 | : Id(Id), InnerMatcher(InnerMatcher), InnerId(InnerId) { |
Daniel Jasper | 452abbc | 2012-10-29 10:48:25 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4102 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 4103 | |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4104 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 4105 | const T *Node = Nodes->getNodeAs<T>(Id); |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4106 | return selectFirst<const T>(InnerId, |
| 4107 | match(InnerMatcher, *Node, *Context)) != NULL; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4108 | } |
| 4109 | private: |
| 4110 | std::string Id; |
| 4111 | internal::Matcher<T> InnerMatcher; |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4112 | std::string InnerId; |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4113 | }; |
| 4114 | |
| 4115 | TEST(MatchFinder, CanMatchDeclarationsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4116 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4117 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4118 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4119 | "X", decl(hasDescendant(recordDecl(hasName("X::Y")).bind("Y"))), |
| 4120 | "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4121 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4122 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4123 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4124 | "X", decl(hasDescendant(recordDecl(hasName("X::Z")).bind("Z"))), |
| 4125 | "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | TEST(MatchFinder, CanMatchStatementsRecursively) { |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4129 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4130 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4131 | new VerifyMatchOnNode<clang::Stmt>( |
| 4132 | "if", stmt(hasDescendant(forStmt().bind("for"))), "for"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4133 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4134 | "void f() { if (1) { for (;;) { } } }", ifStmt().bind("if"), |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4135 | new VerifyMatchOnNode<clang::Stmt>( |
| 4136 | "if", stmt(hasDescendant(declStmt().bind("decl"))), "decl"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
| 4139 | TEST(MatchFinder, CanMatchSingleNodesRecursively) { |
| 4140 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4141 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4142 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4143 | "X", recordDecl(has(recordDecl(hasName("X::Y")).bind("Y"))), "Y"))); |
Manuel Klimek | 60969f5 | 2013-02-01 13:41:35 +0000 | [diff] [blame] | 4144 | EXPECT_TRUE(matchAndVerifyResultFalse( |
| 4145 | "class X { class Y {}; };", recordDecl(hasName("::X")).bind("X"), |
| 4146 | new VerifyMatchOnNode<clang::Decl>( |
Manuel Klimek | cf87bff | 2013-02-06 10:33:21 +0000 | [diff] [blame] | 4147 | "X", recordDecl(has(recordDecl(hasName("X::Z")).bind("Z"))), "Z"))); |
Manuel Klimek | 3e2aa99 | 2012-10-24 14:47:44 +0000 | [diff] [blame] | 4148 | } |
| 4149 | |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4150 | template <typename T> |
| 4151 | class VerifyAncestorHasChildIsEqual : public BoundNodesCallback { |
| 4152 | public: |
| 4153 | virtual bool run(const BoundNodes *Nodes) { return false; } |
| 4154 | |
| 4155 | virtual bool run(const BoundNodes *Nodes, ASTContext *Context) { |
| 4156 | const T *Node = Nodes->getNodeAs<T>(""); |
| 4157 | return verify(*Nodes, *Context, Node); |
| 4158 | } |
| 4159 | |
| 4160 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Stmt *Node) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4161 | // Use the original typed pointer to verify we can pass pointers to subtypes |
| 4162 | // to equalsNode. |
| 4163 | const T *TypedNode = cast<T>(Node); |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4164 | return selectFirst<const T>( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4165 | "", match(stmt(hasParent( |
| 4166 | stmt(has(stmt(equalsNode(TypedNode)))).bind(""))), |
| 4167 | *Node, Context)) != NULL; |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4168 | } |
| 4169 | bool verify(const BoundNodes &Nodes, ASTContext &Context, const Decl *Node) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4170 | // Use the original typed pointer to verify we can pass pointers to subtypes |
| 4171 | // to equalsNode. |
| 4172 | const T *TypedNode = cast<T>(Node); |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4173 | return selectFirst<const T>( |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4174 | "", match(decl(hasParent( |
| 4175 | decl(has(decl(equalsNode(TypedNode)))).bind(""))), |
| 4176 | *Node, Context)) != NULL; |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4177 | } |
| 4178 | }; |
| 4179 | |
| 4180 | TEST(IsEqualTo, MatchesNodesByIdentity) { |
| 4181 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4182 | "class X { class Y {}; };", recordDecl(hasName("::X::Y")).bind(""), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4183 | new VerifyAncestorHasChildIsEqual<CXXRecordDecl>())); |
| 4184 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4185 | "void f() { if (true) if(true) {} }", ifStmt().bind(""), |
| 4186 | new VerifyAncestorHasChildIsEqual<IfStmt>())); |
Manuel Klimek | fa37c5c | 2013-02-07 12:42:10 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
Manuel Klimek | e579328 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 4189 | class VerifyStartOfTranslationUnit : public MatchFinder::MatchCallback { |
| 4190 | public: |
| 4191 | VerifyStartOfTranslationUnit() : Called(false) {} |
| 4192 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 4193 | EXPECT_TRUE(Called); |
| 4194 | } |
| 4195 | virtual void onStartOfTranslationUnit() { |
| 4196 | Called = true; |
| 4197 | } |
| 4198 | bool Called; |
| 4199 | }; |
| 4200 | |
| 4201 | TEST(MatchFinder, InterceptsStartOfTranslationUnit) { |
| 4202 | MatchFinder Finder; |
| 4203 | VerifyStartOfTranslationUnit VerifyCallback; |
| 4204 | Finder.addMatcher(decl(), &VerifyCallback); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4205 | std::unique_ptr<FrontendActionFactory> Factory( |
| 4206 | newFrontendActionFactory(&Finder)); |
Manuel Klimek | e579328 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 4207 | ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); |
| 4208 | EXPECT_TRUE(VerifyCallback.Called); |
Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame] | 4209 | |
| 4210 | VerifyCallback.Called = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4211 | std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;")); |
Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame] | 4212 | ASSERT_TRUE(AST.get()); |
| 4213 | Finder.matchAST(AST->getASTContext()); |
| 4214 | EXPECT_TRUE(VerifyCallback.Called); |
Manuel Klimek | e579328 | 2012-11-02 01:31:03 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
Peter Collingbourne | 8f9e590 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 4217 | class VerifyEndOfTranslationUnit : public MatchFinder::MatchCallback { |
| 4218 | public: |
| 4219 | VerifyEndOfTranslationUnit() : Called(false) {} |
| 4220 | virtual void run(const MatchFinder::MatchResult &Result) { |
| 4221 | EXPECT_FALSE(Called); |
| 4222 | } |
| 4223 | virtual void onEndOfTranslationUnit() { |
| 4224 | Called = true; |
| 4225 | } |
| 4226 | bool Called; |
| 4227 | }; |
| 4228 | |
| 4229 | TEST(MatchFinder, InterceptsEndOfTranslationUnit) { |
| 4230 | MatchFinder Finder; |
| 4231 | VerifyEndOfTranslationUnit VerifyCallback; |
| 4232 | Finder.addMatcher(decl(), &VerifyCallback); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4233 | std::unique_ptr<FrontendActionFactory> Factory( |
| 4234 | newFrontendActionFactory(&Finder)); |
Peter Collingbourne | 8f9e590 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 4235 | ASSERT_TRUE(tooling::runToolOnCode(Factory->create(), "int x;")); |
| 4236 | EXPECT_TRUE(VerifyCallback.Called); |
Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame] | 4237 | |
| 4238 | VerifyCallback.Called = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4239 | std::unique_ptr<ASTUnit> AST(tooling::buildASTFromCode("int x;")); |
Peter Collingbourne | 51fcdf8 | 2013-11-07 22:30:36 +0000 | [diff] [blame] | 4240 | ASSERT_TRUE(AST.get()); |
| 4241 | Finder.matchAST(AST->getASTContext()); |
| 4242 | EXPECT_TRUE(VerifyCallback.Called); |
Peter Collingbourne | 8f9e590 | 2013-05-28 19:21:51 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Manuel Klimek | cf52ca6 | 2013-06-20 14:06:32 +0000 | [diff] [blame] | 4245 | TEST(EqualsBoundNodeMatcher, QualType) { |
| 4246 | EXPECT_TRUE(matches( |
| 4247 | "int i = 1;", varDecl(hasType(qualType().bind("type")), |
| 4248 | hasInitializer(ignoringParenImpCasts( |
| 4249 | hasType(qualType(equalsBoundNode("type")))))))); |
| 4250 | EXPECT_TRUE(notMatches("int i = 1.f;", |
| 4251 | varDecl(hasType(qualType().bind("type")), |
| 4252 | hasInitializer(ignoringParenImpCasts(hasType( |
| 4253 | qualType(equalsBoundNode("type")))))))); |
| 4254 | } |
| 4255 | |
| 4256 | TEST(EqualsBoundNodeMatcher, NonMatchingTypes) { |
| 4257 | EXPECT_TRUE(notMatches( |
| 4258 | "int i = 1;", varDecl(namedDecl(hasName("i")).bind("name"), |
| 4259 | hasInitializer(ignoringParenImpCasts( |
| 4260 | hasType(qualType(equalsBoundNode("type")))))))); |
| 4261 | } |
| 4262 | |
| 4263 | TEST(EqualsBoundNodeMatcher, Stmt) { |
| 4264 | EXPECT_TRUE( |
| 4265 | matches("void f() { if(true) {} }", |
| 4266 | stmt(allOf(ifStmt().bind("if"), |
| 4267 | hasParent(stmt(has(stmt(equalsBoundNode("if"))))))))); |
| 4268 | |
| 4269 | EXPECT_TRUE(notMatches( |
| 4270 | "void f() { if(true) { if (true) {} } }", |
| 4271 | stmt(allOf(ifStmt().bind("if"), has(stmt(equalsBoundNode("if"))))))); |
| 4272 | } |
| 4273 | |
| 4274 | TEST(EqualsBoundNodeMatcher, Decl) { |
| 4275 | EXPECT_TRUE(matches( |
| 4276 | "class X { class Y {}; };", |
| 4277 | decl(allOf(recordDecl(hasName("::X::Y")).bind("record"), |
| 4278 | hasParent(decl(has(decl(equalsBoundNode("record"))))))))); |
| 4279 | |
| 4280 | EXPECT_TRUE(notMatches("class X { class Y {}; };", |
| 4281 | decl(allOf(recordDecl(hasName("::X")).bind("record"), |
| 4282 | has(decl(equalsBoundNode("record"))))))); |
| 4283 | } |
| 4284 | |
| 4285 | TEST(EqualsBoundNodeMatcher, Type) { |
| 4286 | EXPECT_TRUE(matches( |
| 4287 | "class X { int a; int b; };", |
| 4288 | recordDecl( |
| 4289 | has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 4290 | has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 4291 | |
| 4292 | EXPECT_TRUE(notMatches( |
| 4293 | "class X { int a; double b; };", |
| 4294 | recordDecl( |
| 4295 | has(fieldDecl(hasName("a"), hasType(type().bind("t")))), |
| 4296 | has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t")))))))); |
| 4297 | } |
| 4298 | |
| 4299 | TEST(EqualsBoundNodeMatcher, UsingForEachDescendant) { |
Manuel Klimek | cf52ca6 | 2013-06-20 14:06:32 +0000 | [diff] [blame] | 4300 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4301 | "int f() {" |
| 4302 | " if (1) {" |
| 4303 | " int i = 9;" |
| 4304 | " }" |
| 4305 | " int j = 10;" |
| 4306 | " {" |
| 4307 | " float k = 9.0;" |
| 4308 | " }" |
| 4309 | " return 0;" |
| 4310 | "}", |
| 4311 | // Look for variable declarations within functions whose type is the same |
| 4312 | // as the function return type. |
| 4313 | functionDecl(returns(qualType().bind("type")), |
| 4314 | forEachDescendant(varDecl(hasType( |
| 4315 | qualType(equalsBoundNode("type")))).bind("decl"))), |
| 4316 | // Only i and j should match, not k. |
| 4317 | new VerifyIdIsBoundTo<VarDecl>("decl", 2))); |
| 4318 | } |
| 4319 | |
| 4320 | TEST(EqualsBoundNodeMatcher, FiltersMatchedCombinations) { |
| 4321 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4322 | "void f() {" |
| 4323 | " int x;" |
| 4324 | " double d;" |
| 4325 | " x = d + x - d + x;" |
| 4326 | "}", |
| 4327 | functionDecl( |
| 4328 | hasName("f"), forEachDescendant(varDecl().bind("d")), |
| 4329 | forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d")))))), |
| 4330 | new VerifyIdIsBoundTo<VarDecl>("d", 5))); |
| 4331 | } |
| 4332 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4333 | TEST(EqualsBoundNodeMatcher, UnlessDescendantsOfAncestorsMatch) { |
| 4334 | EXPECT_TRUE(matchAndVerifyResultTrue( |
| 4335 | "struct StringRef { int size() const; const char* data() const; };" |
| 4336 | "void f(StringRef v) {" |
| 4337 | " v.data();" |
| 4338 | "}", |
| 4339 | memberCallExpr( |
| 4340 | callee(methodDecl(hasName("data"))), |
| 4341 | on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef")))) |
| 4342 | .bind("var")))), |
| 4343 | unless(hasAncestor(stmt(hasDescendant(memberCallExpr( |
| 4344 | callee(methodDecl(anyOf(hasName("size"), hasName("length")))), |
| 4345 | on(declRefExpr(to(varDecl(equalsBoundNode("var"))))))))))) |
| 4346 | .bind("data"), |
| 4347 | new VerifyIdIsBoundTo<Expr>("data", 1))); |
| 4348 | |
| 4349 | EXPECT_FALSE(matches( |
| 4350 | "struct StringRef { int size() const; const char* data() const; };" |
| 4351 | "void f(StringRef v) {" |
| 4352 | " v.data();" |
| 4353 | " v.size();" |
| 4354 | "}", |
| 4355 | memberCallExpr( |
| 4356 | callee(methodDecl(hasName("data"))), |
| 4357 | on(declRefExpr(to(varDecl(hasType(recordDecl(hasName("StringRef")))) |
| 4358 | .bind("var")))), |
| 4359 | unless(hasAncestor(stmt(hasDescendant(memberCallExpr( |
| 4360 | callee(methodDecl(anyOf(hasName("size"), hasName("length")))), |
| 4361 | on(declRefExpr(to(varDecl(equalsBoundNode("var"))))))))))) |
| 4362 | .bind("data"))); |
| 4363 | } |
| 4364 | |
Manuel Klimek | 4da2166 | 2012-07-06 05:48:52 +0000 | [diff] [blame] | 4365 | } // end namespace ast_matchers |
| 4366 | } // end namespace clang |