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