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