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