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