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