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