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