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