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