David Blaikie | e002631 | 2012-04-26 20:39:46 +0000 | [diff] [blame] | 1 | //===- unittest/Tooling/RecursiveASTVisitorTest.cpp -----------------------===// |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 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 | |
Richard Smith | bc9e558 | 2012-06-24 23:56:26 +0000 | [diff] [blame] | 10 | #include "TestVisitor.h" |
James Dennett | 49007d7 | 2013-07-10 18:29:15 +0000 | [diff] [blame] | 11 | #include <stack> |
| 12 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 13 | using namespace clang; |
| 14 | |
| 15 | namespace { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 16 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 17 | class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> { |
| 18 | public: |
| 19 | bool VisitTypeLoc(TypeLoc TypeLocation) { |
| 20 | Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc()); |
| 21 | return true; |
| 22 | } |
| 23 | }; |
| 24 | |
| 25 | class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> { |
| 26 | public: |
| 27 | bool VisitDeclRefExpr(DeclRefExpr *Reference) { |
| 28 | Match(Reference->getNameInfo().getAsString(), Reference->getLocation()); |
| 29 | return true; |
| 30 | } |
| 31 | }; |
| 32 | |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 33 | class VarDeclVisitor : public ExpectedLocationVisitor<VarDeclVisitor> { |
| 34 | public: |
| 35 | bool VisitVarDecl(VarDecl *Variable) { |
| 36 | Match(Variable->getNameAsString(), Variable->getLocStart()); |
| 37 | return true; |
| 38 | } |
| 39 | }; |
| 40 | |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 41 | class ParmVarDeclVisitorForImplicitCode : |
| 42 | public ExpectedLocationVisitor<ParmVarDeclVisitorForImplicitCode> { |
| 43 | public: |
| 44 | bool shouldVisitImplicitCode() const { return true; } |
| 45 | |
| 46 | bool VisitParmVarDecl(ParmVarDecl *ParamVar) { |
| 47 | Match(ParamVar->getNameAsString(), ParamVar->getLocStart()); |
| 48 | return true; |
| 49 | } |
| 50 | }; |
| 51 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 52 | class CXXMemberCallVisitor |
| 53 | : public ExpectedLocationVisitor<CXXMemberCallVisitor> { |
| 54 | public: |
| 55 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) { |
| 56 | Match(Call->getMethodDecl()->getQualifiedNameAsString(), |
| 57 | Call->getLocStart()); |
| 58 | return true; |
| 59 | } |
| 60 | }; |
| 61 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 62 | class NamedDeclVisitor |
| 63 | : public ExpectedLocationVisitor<NamedDeclVisitor> { |
| 64 | public: |
| 65 | bool VisitNamedDecl(NamedDecl *Decl) { |
| 66 | std::string NameWithTemplateArgs; |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 67 | llvm::raw_string_ostream OS(NameWithTemplateArgs); |
| 68 | Decl->getNameForDiagnostic(OS, |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 69 | Decl->getASTContext().getPrintingPolicy(), |
| 70 | true); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 71 | Match(OS.str(), Decl->getLocation()); |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | }; |
| 75 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 76 | class CXXOperatorCallExprTraverser |
| 77 | : public ExpectedLocationVisitor<CXXOperatorCallExprTraverser> { |
| 78 | public: |
| 79 | // Use Traverse, not Visit, to check that data recursion optimization isn't |
| 80 | // bypassing the call of this function. |
| 81 | bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
| 82 | Match(getOperatorSpelling(CE->getOperator()), CE->getExprLoc()); |
| 83 | return ExpectedLocationVisitor<CXXOperatorCallExprTraverser>:: |
| 84 | TraverseCXXOperatorCallExpr(CE); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | class ParenExprVisitor : public ExpectedLocationVisitor<ParenExprVisitor> { |
| 89 | public: |
| 90 | bool VisitParenExpr(ParenExpr *Parens) { |
| 91 | Match("", Parens->getExprLoc()); |
| 92 | return true; |
| 93 | } |
| 94 | }; |
| 95 | |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 96 | class LambdaExprVisitor : public ExpectedLocationVisitor<LambdaExprVisitor> { |
| 97 | public: |
| 98 | bool VisitLambdaExpr(LambdaExpr *Lambda) { |
James Dennett | 49007d7 | 2013-07-10 18:29:15 +0000 | [diff] [blame] | 99 | PendingBodies.push(Lambda); |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 100 | Match("", Lambda->getIntroducerRange().getBegin()); |
| 101 | return true; |
| 102 | } |
James Dennett | 49007d7 | 2013-07-10 18:29:15 +0000 | [diff] [blame] | 103 | /// For each call to VisitLambdaExpr, we expect a subsequent call (with |
| 104 | /// proper nesting) to TraverseLambdaBody. |
| 105 | bool TraverseLambdaBody(LambdaExpr *Lambda) { |
| 106 | EXPECT_FALSE(PendingBodies.empty()); |
| 107 | EXPECT_EQ(PendingBodies.top(), Lambda); |
| 108 | PendingBodies.pop(); |
| 109 | return TraverseStmt(Lambda->getBody()); |
| 110 | } |
| 111 | /// Determine whether TraverseLambdaBody has been called for every call to |
| 112 | /// VisitLambdaExpr. |
| 113 | bool allBodiesHaveBeenTraversed() const { |
| 114 | return PendingBodies.empty(); |
| 115 | } |
| 116 | private: |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 117 | std::stack<LambdaExpr *> PendingBodies; |
| 118 | }; |
| 119 | |
| 120 | // Matches the (optional) capture-default of a lambda-introducer. |
| 121 | class LambdaDefaultCaptureVisitor |
| 122 | : public ExpectedLocationVisitor<LambdaDefaultCaptureVisitor> { |
| 123 | public: |
| 124 | bool VisitLambdaExpr(LambdaExpr *Lambda) { |
| 125 | if (Lambda->getCaptureDefault() != LCD_None) { |
| 126 | Match("", Lambda->getCaptureDefaultLoc()); |
| 127 | } |
| 128 | return true; |
| 129 | } |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Richard Smith | 6fada8e | 2012-05-30 23:55:51 +0000 | [diff] [blame] | 132 | class TemplateArgumentLocTraverser |
| 133 | : public ExpectedLocationVisitor<TemplateArgumentLocTraverser> { |
| 134 | public: |
| 135 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 136 | std::string ArgStr; |
| 137 | llvm::raw_string_ostream Stream(ArgStr); |
| 138 | const TemplateArgument &Arg = ArgLoc.getArgument(); |
| 139 | |
| 140 | Arg.print(Context->getPrintingPolicy(), Stream); |
| 141 | Match(Stream.str(), ArgLoc.getLocation()); |
| 142 | return ExpectedLocationVisitor<TemplateArgumentLocTraverser>:: |
| 143 | TraverseTemplateArgumentLoc(ArgLoc); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | class CXXBoolLiteralExprVisitor |
| 148 | : public ExpectedLocationVisitor<CXXBoolLiteralExprVisitor> { |
| 149 | public: |
| 150 | bool VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *BE) { |
| 151 | if (BE->getValue()) |
| 152 | Match("true", BE->getLocation()); |
| 153 | else |
| 154 | Match("false", BE->getLocation()); |
| 155 | return true; |
| 156 | } |
| 157 | }; |
| 158 | |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 159 | // Test RAV visits parameter variable declaration of the implicit |
Michael Han | 0bb3ca3 | 2013-09-12 20:59:33 +0000 | [diff] [blame] | 160 | // copy assignment operator and implicit copy constructor. |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 161 | TEST(RecursiveASTVisitor, VisitsParmVarDeclForImplicitCode) { |
| 162 | ParmVarDeclVisitorForImplicitCode Visitor; |
Michael Han | 0bb3ca3 | 2013-09-12 20:59:33 +0000 | [diff] [blame] | 163 | // Match parameter variable name of implicit copy assignment operator and |
| 164 | // implicit copy constructor. |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 165 | // This parameter name does not have a valid IdentifierInfo, and shares |
| 166 | // same SourceLocation with its class declaration, so we match an empty name |
| 167 | // with the class' source location. |
| 168 | Visitor.ExpectMatch("", 1, 7); |
Michael Han | 0bb3ca3 | 2013-09-12 20:59:33 +0000 | [diff] [blame] | 169 | Visitor.ExpectMatch("", 3, 7); |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 170 | EXPECT_TRUE(Visitor.runOver( |
| 171 | "class X {};\n" |
Michael Han | 0bb3ca3 | 2013-09-12 20:59:33 +0000 | [diff] [blame] | 172 | "void foo(X a, X b) {a = b;}\n" |
| 173 | "class Y {};\n" |
| 174 | "void bar(Y a) {Y b = a;}")); |
Michael Han | 4b6730d | 2013-09-11 15:53:29 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 177 | TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) { |
| 178 | TypeLocVisitor Visitor; |
| 179 | Visitor.ExpectMatch("class X", 1, 30); |
| 180 | EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};")); |
| 181 | } |
| 182 | |
Manuel Klimek | 9f99d06 | 2012-04-23 16:40:40 +0000 | [diff] [blame] | 183 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) { |
| 184 | TypeLocVisitor Visitor; |
| 185 | Visitor.ExpectMatch("class X", 3, 18); |
| 186 | EXPECT_TRUE(Visitor.runOver( |
| 187 | "class Y;\n" |
| 188 | "class X {};\n" |
| 189 | "class Y : public X {};")); |
| 190 | } |
| 191 | |
| 192 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) { |
| 193 | TypeLocVisitor Visitor; |
| 194 | Visitor.ExpectMatch("class X", 2, 18); |
| 195 | EXPECT_TRUE(Visitor.runOver( |
| 196 | "class X {};\n" |
| 197 | "class Y : public X { class Z; };")); |
| 198 | } |
| 199 | |
| 200 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) { |
| 201 | TypeLocVisitor Visitor; |
| 202 | Visitor.ExpectMatch("X<class Y>", 2, 18); |
| 203 | EXPECT_TRUE(Visitor.runOver( |
| 204 | "template<typename T> class X {};\n" |
| 205 | "class Y : public X<Y> {};")); |
| 206 | } |
| 207 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 208 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) { |
| 209 | DeclRefExprVisitor Visitor; |
| 210 | Visitor.ExpectMatch("x", 2, 3); |
| 211 | EXPECT_TRUE(Visitor.runOver( |
| 212 | "void x(); template <void (*T)()> class X {};\nX<x> y;")); |
| 213 | } |
| 214 | |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 215 | TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtRange) { |
| 216 | DeclRefExprVisitor Visitor; |
| 217 | Visitor.ExpectMatch("x", 2, 25); |
Daniel Jasper | 1071ba9 | 2012-06-21 08:50:04 +0000 | [diff] [blame] | 218 | Visitor.ExpectMatch("x", 2, 30); |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 219 | EXPECT_TRUE(Visitor.runOver( |
| 220 | "int x[5];\n" |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 221 | "void f() { for (int i : x) { x[0] = 1; } }", |
| 222 | DeclRefExprVisitor::Lang_CXX11)); |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtLoopVariable) { |
| 226 | VarDeclVisitor Visitor; |
| 227 | Visitor.ExpectMatch("i", 2, 17); |
| 228 | EXPECT_TRUE(Visitor.runOver( |
| 229 | "int x[5];\n" |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 230 | "void f() { for (int i : x) {} }", |
| 231 | VarDeclVisitor::Lang_CXX11)); |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 234 | TEST(RecursiveASTVisitor, VisitsCallExpr) { |
| 235 | DeclRefExprVisitor Visitor; |
| 236 | Visitor.ExpectMatch("x", 1, 22); |
| 237 | EXPECT_TRUE(Visitor.runOver( |
| 238 | "void x(); void y() { x(); }")); |
| 239 | } |
| 240 | |
| 241 | TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) { |
| 242 | CXXMemberCallVisitor Visitor; |
| 243 | Visitor.ExpectMatch("Y::x", 3, 3); |
| 244 | EXPECT_TRUE(Visitor.runOver( |
| 245 | "struct Y { void x(); };\n" |
| 246 | "template<typename T> void y(T t) {\n" |
| 247 | " t.x();\n" |
| 248 | "}\n" |
| 249 | "void foo() { y<Y>(Y()); }")); |
| 250 | } |
| 251 | |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 252 | TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 253 | CXXMemberCallVisitor Visitor; |
| 254 | Visitor.ExpectMatch("Y::x", 4, 5); |
| 255 | EXPECT_TRUE(Visitor.runOver( |
| 256 | "struct Y { void x(); };\n" |
| 257 | "template<typename T> struct Z {\n" |
| 258 | " template<typename U> static void f() {\n" |
| 259 | " T().x();\n" |
| 260 | " }\n" |
| 261 | "};\n" |
| 262 | "void foo() { Z<Y>::f<int>(); }")); |
| 263 | } |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 264 | |
| 265 | TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) { |
| 266 | CXXMemberCallVisitor Visitor; |
| 267 | Visitor.ExpectMatch("A::x", 5, 7); |
| 268 | EXPECT_TRUE(Visitor.runOver( |
| 269 | "template <typename T1> struct X {\n" |
| 270 | " template <typename T2> struct Y {\n" |
| 271 | " void f() {\n" |
| 272 | " T2 y;\n" |
| 273 | " y.x();\n" |
| 274 | " }\n" |
| 275 | " };\n" |
| 276 | "};\n" |
| 277 | "struct A { void x(); };\n" |
| 278 | "int main() {\n" |
| 279 | " (new X<A>::Y<A>())->f();\n" |
| 280 | "}")); |
| 281 | } |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 282 | |
| 283 | /* FIXME: According to Richard Smith this is a bug in the AST. |
| 284 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) { |
| 285 | DeclRefExprVisitor Visitor; |
| 286 | Visitor.ExpectMatch("x", 3, 43); |
| 287 | EXPECT_TRUE(Visitor.runOver( |
| 288 | "template <typename T> void x();\n" |
| 289 | "template <void (*T)()> class X {};\n" |
| 290 | "template <typename T> class Y : public X< x<T> > {};\n" |
| 291 | "Y<int> y;")); |
| 292 | } |
| 293 | */ |
| 294 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 295 | TEST(RecursiveASTVisitor, VisitsCallInPartialTemplateSpecialization) { |
| 296 | CXXMemberCallVisitor Visitor; |
| 297 | Visitor.ExpectMatch("A::x", 6, 20); |
| 298 | EXPECT_TRUE(Visitor.runOver( |
| 299 | "template <typename T1> struct X {\n" |
| 300 | " template <typename T2, bool B> struct Y { void g(); };\n" |
| 301 | "};\n" |
| 302 | "template <typename T1> template <typename T2>\n" |
| 303 | "struct X<T1>::Y<T2, true> {\n" |
| 304 | " void f() { T2 y; y.x(); }\n" |
| 305 | "};\n" |
| 306 | "struct A { void x(); };\n" |
| 307 | "int main() {\n" |
| 308 | " (new X<A>::Y<A, true>())->f();\n" |
| 309 | "}\n")); |
| 310 | } |
| 311 | |
Richard Smith | 06cd51a | 2012-05-09 23:51:36 +0000 | [diff] [blame] | 312 | TEST(RecursiveASTVisitor, VisitsExplicitTemplateSpecialization) { |
| 313 | CXXMemberCallVisitor Visitor; |
| 314 | Visitor.ExpectMatch("A::f", 4, 5); |
| 315 | EXPECT_TRUE(Visitor.runOver( |
| 316 | "struct A {\n" |
| 317 | " void f() const {}\n" |
| 318 | " template<class T> void g(const T& t) const {\n" |
| 319 | " t.f();\n" |
| 320 | " }\n" |
| 321 | "};\n" |
| 322 | "template void A::g(const A& a) const;\n")); |
| 323 | } |
| 324 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 325 | TEST(RecursiveASTVisitor, VisitsPartialTemplateSpecialization) { |
| 326 | // From cfe-commits/Week-of-Mon-20100830/033998.html |
Daniel Jasper | e966bea | 2012-05-30 04:30:08 +0000 | [diff] [blame] | 327 | // Contrary to the approach suggested in that email, we visit all |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 328 | // specializations when we visit the primary template. Visiting them when we |
| 329 | // visit the associated specialization is problematic for specializations of |
| 330 | // template members of class templates. |
| 331 | NamedDeclVisitor Visitor; |
| 332 | Visitor.ExpectMatch("A<bool>", 1, 26); |
| 333 | Visitor.ExpectMatch("A<char *>", 2, 26); |
| 334 | EXPECT_TRUE(Visitor.runOver( |
| 335 | "template <class T> class A {};\n" |
| 336 | "template <class T> class A<T*> {};\n" |
| 337 | "A<bool> ab;\n" |
| 338 | "A<char*> acp;\n")); |
| 339 | } |
| 340 | |
| 341 | TEST(RecursiveASTVisitor, VisitsUndefinedClassTemplateSpecialization) { |
| 342 | NamedDeclVisitor Visitor; |
| 343 | Visitor.ExpectMatch("A<int>", 1, 29); |
| 344 | EXPECT_TRUE(Visitor.runOver( |
| 345 | "template<typename T> struct A;\n" |
| 346 | "A<int> *p;\n")); |
| 347 | } |
| 348 | |
| 349 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedClassTemplateSpecialization) { |
| 350 | NamedDeclVisitor Visitor; |
| 351 | Visitor.ExpectMatch("A<int>::B<char>", 2, 31); |
| 352 | EXPECT_TRUE(Visitor.runOver( |
| 353 | "template<typename T> struct A {\n" |
| 354 | " template<typename U> struct B;\n" |
| 355 | "};\n" |
| 356 | "A<int>::B<char> *p;\n")); |
| 357 | } |
| 358 | |
| 359 | TEST(RecursiveASTVisitor, VisitsUndefinedFunctionTemplateSpecialization) { |
| 360 | NamedDeclVisitor Visitor; |
| 361 | Visitor.ExpectMatch("A<int>", 1, 26); |
| 362 | EXPECT_TRUE(Visitor.runOver( |
| 363 | "template<typename T> int A();\n" |
| 364 | "int k = A<int>();\n")); |
| 365 | } |
| 366 | |
| 367 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedFunctionTemplateSpecialization) { |
| 368 | NamedDeclVisitor Visitor; |
| 369 | Visitor.ExpectMatch("A<int>::B<char>", 2, 35); |
| 370 | EXPECT_TRUE(Visitor.runOver( |
| 371 | "template<typename T> struct A {\n" |
| 372 | " template<typename U> static int B();\n" |
| 373 | "};\n" |
| 374 | "int k = A<int>::B<char>();\n")); |
| 375 | } |
| 376 | |
| 377 | TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) { |
| 378 | // From cfe-commits/Week-of-Mon-20100830/033977.html |
| 379 | NamedDeclVisitor Visitor; |
| 380 | Visitor.ExpectMatch("vector_iterator<int>", 2, 7); |
| 381 | EXPECT_TRUE(Visitor.runOver( |
| 382 | "template<typename Container>\n" |
| 383 | "class vector_iterator {\n" |
| 384 | " template <typename C> friend class vector_iterator;\n" |
| 385 | "};\n" |
| 386 | "vector_iterator<int> it_int;\n")); |
| 387 | } |
| 388 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 389 | TEST(RecursiveASTVisitor, TraversesOverloadedOperator) { |
| 390 | CXXOperatorCallExprTraverser Visitor; |
| 391 | Visitor.ExpectMatch("()", 4, 9); |
| 392 | EXPECT_TRUE(Visitor.runOver( |
| 393 | "struct A {\n" |
| 394 | " int operator()();\n" |
| 395 | "} a;\n" |
| 396 | "int k = a();\n")); |
| 397 | } |
| 398 | |
| 399 | TEST(RecursiveASTVisitor, VisitsParensDuringDataRecursion) { |
| 400 | ParenExprVisitor Visitor; |
| 401 | Visitor.ExpectMatch("", 1, 9); |
| 402 | EXPECT_TRUE(Visitor.runOver("int k = (4) + 9;\n")); |
| 403 | } |
| 404 | |
Richard Smith | 6fada8e | 2012-05-30 23:55:51 +0000 | [diff] [blame] | 405 | TEST(RecursiveASTVisitor, VisitsClassTemplateNonTypeParmDefaultArgument) { |
| 406 | CXXBoolLiteralExprVisitor Visitor; |
| 407 | Visitor.ExpectMatch("true", 2, 19); |
| 408 | EXPECT_TRUE(Visitor.runOver( |
| 409 | "template<bool B> class X;\n" |
| 410 | "template<bool B = true> class Y;\n" |
| 411 | "template<bool B> class Y {};\n")); |
| 412 | } |
| 413 | |
| 414 | TEST(RecursiveASTVisitor, VisitsClassTemplateTypeParmDefaultArgument) { |
| 415 | TypeLocVisitor Visitor; |
| 416 | Visitor.ExpectMatch("class X", 2, 23); |
| 417 | EXPECT_TRUE(Visitor.runOver( |
| 418 | "class X;\n" |
| 419 | "template<typename T = X> class Y;\n" |
| 420 | "template<typename T> class Y {};\n")); |
| 421 | } |
| 422 | |
| 423 | TEST(RecursiveASTVisitor, VisitsClassTemplateTemplateParmDefaultArgument) { |
| 424 | TemplateArgumentLocTraverser Visitor; |
| 425 | Visitor.ExpectMatch("X", 2, 40); |
| 426 | EXPECT_TRUE(Visitor.runOver( |
| 427 | "template<typename T> class X;\n" |
| 428 | "template<template <typename> class T = X> class Y;\n" |
| 429 | "template<template <typename> class T> class Y {};\n")); |
| 430 | } |
| 431 | |
Richard Smith | c28a335 | 2012-06-05 16:18:26 +0000 | [diff] [blame] | 432 | // A visitor that visits implicit declarations and matches constructors. |
| 433 | class ImplicitCtorVisitor |
| 434 | : public ExpectedLocationVisitor<ImplicitCtorVisitor> { |
| 435 | public: |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 436 | bool shouldVisitImplicitCode() const { return true; } |
Richard Smith | c28a335 | 2012-06-05 16:18:26 +0000 | [diff] [blame] | 437 | |
| 438 | bool VisitCXXConstructorDecl(CXXConstructorDecl* Ctor) { |
| 439 | if (Ctor->isImplicit()) { // Was not written in source code |
| 440 | if (const CXXRecordDecl* Class = Ctor->getParent()) { |
| 441 | Match(Class->getName(), Ctor->getLocation()); |
| 442 | } |
| 443 | } |
| 444 | return true; |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | TEST(RecursiveASTVisitor, VisitsImplicitCopyConstructors) { |
| 449 | ImplicitCtorVisitor Visitor; |
| 450 | Visitor.ExpectMatch("Simple", 2, 8); |
| 451 | // Note: Clang lazily instantiates implicit declarations, so we need |
| 452 | // to use them in order to force them to appear in the AST. |
| 453 | EXPECT_TRUE(Visitor.runOver( |
| 454 | "struct WithCtor { WithCtor(); }; \n" |
| 455 | "struct Simple { Simple(); WithCtor w; }; \n" |
| 456 | "int main() { Simple s; Simple t(s); }\n")); |
| 457 | } |
| 458 | |
James Dennett | 8268fe7 | 2012-08-24 06:59:51 +0000 | [diff] [blame] | 459 | /// \brief A visitor that optionally includes implicit code and matches |
| 460 | /// CXXConstructExpr. |
| 461 | /// |
| 462 | /// The name recorded for the match is the name of the class whose constructor |
| 463 | /// is invoked by the CXXConstructExpr, not the name of the class whose |
| 464 | /// constructor the CXXConstructExpr is contained in. |
| 465 | class ConstructExprVisitor |
| 466 | : public ExpectedLocationVisitor<ConstructExprVisitor> { |
| 467 | public: |
| 468 | ConstructExprVisitor() : ShouldVisitImplicitCode(false) {} |
| 469 | |
| 470 | bool shouldVisitImplicitCode() const { return ShouldVisitImplicitCode; } |
| 471 | |
| 472 | void setShouldVisitImplicitCode(bool NewValue) { |
| 473 | ShouldVisitImplicitCode = NewValue; |
| 474 | } |
| 475 | |
| 476 | bool VisitCXXConstructExpr(CXXConstructExpr* Expr) { |
| 477 | if (const CXXConstructorDecl* Ctor = Expr->getConstructor()) { |
| 478 | if (const CXXRecordDecl* Class = Ctor->getParent()) { |
| 479 | Match(Class->getName(), Expr->getLocation()); |
| 480 | } |
| 481 | } |
| 482 | return true; |
| 483 | } |
| 484 | |
| 485 | private: |
| 486 | bool ShouldVisitImplicitCode; |
| 487 | }; |
| 488 | |
| 489 | TEST(RecursiveASTVisitor, CanVisitImplicitMemberInitializations) { |
| 490 | ConstructExprVisitor Visitor; |
| 491 | Visitor.setShouldVisitImplicitCode(true); |
| 492 | Visitor.ExpectMatch("WithCtor", 2, 8); |
| 493 | // Simple has a constructor that implicitly initializes 'w'. Test |
| 494 | // that a visitor that visits implicit code visits that initialization. |
| 495 | // Note: Clang lazily instantiates implicit declarations, so we need |
| 496 | // to use them in order to force them to appear in the AST. |
| 497 | EXPECT_TRUE(Visitor.runOver( |
| 498 | "struct WithCtor { WithCtor(); }; \n" |
| 499 | "struct Simple { WithCtor w; }; \n" |
| 500 | "int main() { Simple s; }\n")); |
| 501 | } |
| 502 | |
| 503 | // The same as CanVisitImplicitMemberInitializations, but checking that the |
| 504 | // visits are omitted when the visitor does not include implicit code. |
| 505 | TEST(RecursiveASTVisitor, CanSkipImplicitMemberInitializations) { |
| 506 | ConstructExprVisitor Visitor; |
| 507 | Visitor.setShouldVisitImplicitCode(false); |
| 508 | Visitor.DisallowMatch("WithCtor", 2, 8); |
| 509 | // Simple has a constructor that implicitly initializes 'w'. Test |
| 510 | // that a visitor that skips implicit code skips that initialization. |
| 511 | // Note: Clang lazily instantiates implicit declarations, so we need |
| 512 | // to use them in order to force them to appear in the AST. |
| 513 | EXPECT_TRUE(Visitor.runOver( |
| 514 | "struct WithCtor { WithCtor(); }; \n" |
| 515 | "struct Simple { WithCtor w; }; \n" |
| 516 | "int main() { Simple s; }\n")); |
| 517 | } |
| 518 | |
Richard Smith | 0652c35 | 2012-08-17 03:20:55 +0000 | [diff] [blame] | 519 | TEST(RecursiveASTVisitor, VisitsExtension) { |
| 520 | DeclRefExprVisitor Visitor; |
| 521 | Visitor.ExpectMatch("s", 1, 24); |
| 522 | EXPECT_TRUE(Visitor.runOver( |
| 523 | "int s = __extension__ (s);\n")); |
| 524 | } |
| 525 | |
Richard Smith | 82b4550 | 2012-08-17 21:23:17 +0000 | [diff] [blame] | 526 | TEST(RecursiveASTVisitor, VisitsCompoundLiteralType) { |
| 527 | TypeLocVisitor Visitor; |
| 528 | Visitor.ExpectMatch("struct S", 1, 26); |
| 529 | EXPECT_TRUE(Visitor.runOver( |
| 530 | "int f() { return (struct S { int a; }){.a = 0}.a; }", |
| 531 | TypeLocVisitor::Lang_C)); |
| 532 | } |
| 533 | |
James Dennett | 89faf86 | 2013-06-30 03:13:35 +0000 | [diff] [blame] | 534 | TEST(RecursiveASTVisitor, VisitsLambdaExpr) { |
| 535 | LambdaExprVisitor Visitor; |
| 536 | Visitor.ExpectMatch("", 1, 12); |
| 537 | EXPECT_TRUE(Visitor.runOver("void f() { []{ return; }(); }", |
| 538 | LambdaExprVisitor::Lang_CXX11)); |
| 539 | } |
| 540 | |
James Dennett | 49007d7 | 2013-07-10 18:29:15 +0000 | [diff] [blame] | 541 | TEST(RecursiveASTVisitor, TraverseLambdaBodyCanBeOverridden) { |
| 542 | LambdaExprVisitor Visitor; |
| 543 | EXPECT_TRUE(Visitor.runOver("void f() { []{ return; }(); }", |
| 544 | LambdaExprVisitor::Lang_CXX11)); |
| 545 | EXPECT_TRUE(Visitor.allBodiesHaveBeenTraversed()); |
| 546 | } |
| 547 | |
James Dennett | f68af64 | 2013-08-09 23:08:25 +0000 | [diff] [blame] | 548 | TEST(RecursiveASTVisitor, HasCaptureDefaultLoc) { |
| 549 | LambdaDefaultCaptureVisitor Visitor; |
| 550 | Visitor.ExpectMatch("", 1, 20); |
| 551 | EXPECT_TRUE(Visitor.runOver("void f() { int a; [=]{a;}; }", |
| 552 | LambdaDefaultCaptureVisitor::Lang_CXX11)); |
| 553 | } |
| 554 | |
James Dennett | e3efec2 | 2013-09-05 17:46:21 +0000 | [diff] [blame] | 555 | // Checks for lambda classes that are not marked as implicitly-generated. |
| 556 | // (There should be none.) |
| 557 | class ClassVisitor : public ExpectedLocationVisitor<ClassVisitor> { |
| 558 | public: |
| 559 | ClassVisitor() : SawNonImplicitLambdaClass(false) {} |
| 560 | bool VisitCXXRecordDecl(CXXRecordDecl* record) { |
| 561 | if (record->isLambda() && !record->isImplicit()) |
| 562 | SawNonImplicitLambdaClass = true; |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | bool sawOnlyImplicitLambdaClasses() const { |
| 567 | return !SawNonImplicitLambdaClass; |
| 568 | } |
| 569 | |
| 570 | private: |
| 571 | bool SawNonImplicitLambdaClass; |
| 572 | }; |
| 573 | |
| 574 | TEST(RecursiveASTVisitor, LambdaClosureTypesAreImplicit) { |
| 575 | ClassVisitor Visitor; |
| 576 | EXPECT_TRUE(Visitor.runOver("auto lambda = []{};", |
| 577 | ClassVisitor::Lang_CXX11)); |
| 578 | EXPECT_TRUE(Visitor.sawOnlyImplicitLambdaClasses()); |
| 579 | } |
| 580 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 581 | |
| 582 | |
| 583 | // Check to ensure that attributes and expressions within them are being |
| 584 | // visited. |
| 585 | class AttrVisitor : public ExpectedLocationVisitor<AttrVisitor> { |
| 586 | public: |
| 587 | bool VisitMemberExpr(MemberExpr *ME) { |
| 588 | Match(ME->getMemberDecl()->getNameAsString(), ME->getLocStart()); |
| 589 | return true; |
| 590 | } |
| 591 | bool VisitAttr(Attr *A) { |
| 592 | Match("Attr", A->getLocation()); |
| 593 | return true; |
| 594 | } |
| 595 | bool VisitGuardedByAttr(GuardedByAttr *A) { |
| 596 | Match("guarded_by", A->getLocation()); |
| 597 | return true; |
| 598 | } |
| 599 | }; |
| 600 | |
| 601 | |
| 602 | TEST(RecursiveASTVisitor, AttributesAreVisited) { |
| 603 | AttrVisitor Visitor; |
| 604 | Visitor.ExpectMatch("Attr", 4, 24); |
| 605 | Visitor.ExpectMatch("guarded_by", 4, 24); |
| 606 | Visitor.ExpectMatch("mu1", 4, 35); |
| 607 | Visitor.ExpectMatch("Attr", 5, 29); |
| 608 | Visitor.ExpectMatch("mu1", 5, 54); |
| 609 | Visitor.ExpectMatch("mu2", 5, 59); |
| 610 | EXPECT_TRUE(Visitor.runOver( |
| 611 | "class Foo {\n" |
| 612 | " int mu1;\n" |
| 613 | " int mu2;\n" |
| 614 | " int a __attribute__((guarded_by(mu1)));\n" |
| 615 | " void bar() __attribute__((exclusive_locks_required(mu1, mu2)));\n" |
| 616 | "};\n")); |
| 617 | } |
| 618 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 619 | } // end anonymous namespace |