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 | |
| 10 | #include "clang/AST/ASTConsumer.h" |
| 11 | #include "clang/AST/RecursiveASTVisitor.h" |
| 12 | #include "clang/Frontend/FrontendAction.h" |
| 13 | #include "clang/Frontend/CompilerInstance.h" |
| 14 | #include "clang/Tooling/Tooling.h" |
| 15 | #include "gtest/gtest.h" |
| 16 | |
| 17 | namespace clang { |
| 18 | |
| 19 | /// \brief Base class for sipmle RecursiveASTVisitor based tests. |
| 20 | /// |
| 21 | /// This is a drop-in replacement for RecursiveASTVisitor itself, with the |
| 22 | /// additional capability of running it over a snippet of code. |
| 23 | /// |
| 24 | /// Visits template instantiations by default. |
| 25 | /// |
| 26 | /// FIXME: Put into a common location. |
| 27 | template <typename T> |
| 28 | class TestVisitor : public clang::RecursiveASTVisitor<T> { |
| 29 | public: |
| 30 | /// \brief Runs the current AST visitor over the given code. |
| 31 | bool runOver(StringRef Code) { |
| 32 | return tooling::runToolOnCode(new TestAction(this), Code); |
| 33 | } |
| 34 | |
| 35 | bool shouldVisitTemplateInstantiations() const { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | protected: |
| 40 | clang::ASTContext *Context; |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | class FindConsumer : public clang::ASTConsumer { |
| 44 | public: |
| 45 | FindConsumer(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 46 | |
| 47 | virtual void HandleTranslationUnit(clang::ASTContext &Context) { |
| 48 | Visitor->TraverseDecl(Context.getTranslationUnitDecl()); |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | TestVisitor *Visitor; |
| 53 | }; |
| 54 | |
| 55 | class TestAction : public clang::ASTFrontendAction { |
| 56 | public: |
| 57 | TestAction(TestVisitor *Visitor) : Visitor(Visitor) {} |
| 58 | |
| 59 | virtual clang::ASTConsumer* CreateASTConsumer( |
| 60 | clang::CompilerInstance& compiler, llvm::StringRef dummy) { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 61 | Visitor->Context = &compiler.getASTContext(); |
| 62 | /// TestConsumer will be deleted by the framework calling us. |
| 63 | return new FindConsumer(Visitor); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | TestVisitor *Visitor; |
| 68 | }; |
| 69 | }; |
| 70 | |
| 71 | /// \brief A RecursiveASTVisitor for testing the RecursiveASTVisitor itself. |
| 72 | /// |
| 73 | /// Allows simple creation of test visitors running matches on only a small |
| 74 | /// subset of the Visit* methods. |
| 75 | template <typename T> |
| 76 | class ExpectedLocationVisitor : public TestVisitor<T> { |
| 77 | public: |
| 78 | ExpectedLocationVisitor() |
| 79 | : ExpectedLine(0), ExpectedColumn(0), Found(false) {} |
| 80 | |
| 81 | ~ExpectedLocationVisitor() { |
| 82 | EXPECT_TRUE(Found) |
| 83 | << "Expected \"" << ExpectedMatch << "\" at " << ExpectedLine |
| 84 | << ":" << ExpectedColumn << PartialMatches; |
| 85 | } |
| 86 | |
| 87 | /// \brief Expect 'Match' to occur at the given 'Line' and 'Column'. |
| 88 | void ExpectMatch(Twine Match, unsigned Line, unsigned Column) { |
| 89 | ExpectedMatch = Match.str(); |
| 90 | ExpectedLine = Line; |
| 91 | ExpectedColumn = Column; |
| 92 | } |
| 93 | |
| 94 | protected: |
| 95 | /// \brief Convenience method to simplify writing test visitors. |
| 96 | /// |
| 97 | /// Sets 'Found' to true if 'Name' and 'Location' match the expected |
| 98 | /// values. If only a partial match is found, record the information |
| 99 | /// to produce nice error output when a test fails. |
| 100 | /// |
| 101 | /// Implementations are required to call this with appropriate values |
| 102 | /// for 'Name' during visitation. |
| 103 | void Match(StringRef Name, SourceLocation Location) { |
| 104 | FullSourceLoc FullLocation = this->Context->getFullLoc(Location); |
| 105 | if (Name == ExpectedMatch && |
| 106 | FullLocation.isValid() && |
| 107 | FullLocation.getSpellingLineNumber() == ExpectedLine && |
| 108 | FullLocation.getSpellingColumnNumber() == ExpectedColumn) { |
Manuel Klimek | 9f99d06 | 2012-04-23 16:40:40 +0000 | [diff] [blame] | 109 | EXPECT_TRUE(!Found); |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 110 | Found = true; |
| 111 | } else if (Name == ExpectedMatch || |
| 112 | (FullLocation.isValid() && |
| 113 | FullLocation.getSpellingLineNumber() == ExpectedLine && |
| 114 | FullLocation.getSpellingColumnNumber() == ExpectedColumn)) { |
| 115 | // If we did not match, record information about partial matches. |
| 116 | llvm::raw_string_ostream Stream(PartialMatches); |
| 117 | Stream << ", partial match: \"" << Name << "\" at "; |
Manuel Klimek | dab2894 | 2012-04-20 14:07:01 +0000 | [diff] [blame] | 118 | Location.print(Stream, this->Context->getSourceManager()); |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | std::string ExpectedMatch; |
| 123 | unsigned ExpectedLine; |
| 124 | unsigned ExpectedColumn; |
| 125 | std::string PartialMatches; |
| 126 | bool Found; |
| 127 | }; |
| 128 | |
| 129 | class TypeLocVisitor : public ExpectedLocationVisitor<TypeLocVisitor> { |
| 130 | public: |
| 131 | bool VisitTypeLoc(TypeLoc TypeLocation) { |
| 132 | Match(TypeLocation.getType().getAsString(), TypeLocation.getBeginLoc()); |
| 133 | return true; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | class DeclRefExprVisitor : public ExpectedLocationVisitor<DeclRefExprVisitor> { |
| 138 | public: |
| 139 | bool VisitDeclRefExpr(DeclRefExpr *Reference) { |
| 140 | Match(Reference->getNameInfo().getAsString(), Reference->getLocation()); |
| 141 | return true; |
| 142 | } |
| 143 | }; |
| 144 | |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 145 | class VarDeclVisitor : public ExpectedLocationVisitor<VarDeclVisitor> { |
| 146 | public: |
| 147 | bool VisitVarDecl(VarDecl *Variable) { |
| 148 | Match(Variable->getNameAsString(), Variable->getLocStart()); |
| 149 | return true; |
| 150 | } |
| 151 | }; |
| 152 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 153 | class CXXMemberCallVisitor |
| 154 | : public ExpectedLocationVisitor<CXXMemberCallVisitor> { |
| 155 | public: |
| 156 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) { |
| 157 | Match(Call->getMethodDecl()->getQualifiedNameAsString(), |
| 158 | Call->getLocStart()); |
| 159 | return true; |
| 160 | } |
| 161 | }; |
| 162 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 163 | class NamedDeclVisitor |
| 164 | : public ExpectedLocationVisitor<NamedDeclVisitor> { |
| 165 | public: |
| 166 | bool VisitNamedDecl(NamedDecl *Decl) { |
| 167 | std::string NameWithTemplateArgs; |
| 168 | Decl->getNameForDiagnostic(NameWithTemplateArgs, |
| 169 | Decl->getASTContext().getPrintingPolicy(), |
| 170 | true); |
| 171 | Match(NameWithTemplateArgs, Decl->getLocation()); |
| 172 | return true; |
| 173 | } |
| 174 | }; |
| 175 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 176 | class CXXOperatorCallExprTraverser |
| 177 | : public ExpectedLocationVisitor<CXXOperatorCallExprTraverser> { |
| 178 | public: |
| 179 | // Use Traverse, not Visit, to check that data recursion optimization isn't |
| 180 | // bypassing the call of this function. |
| 181 | bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
| 182 | Match(getOperatorSpelling(CE->getOperator()), CE->getExprLoc()); |
| 183 | return ExpectedLocationVisitor<CXXOperatorCallExprTraverser>:: |
| 184 | TraverseCXXOperatorCallExpr(CE); |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | class ParenExprVisitor : public ExpectedLocationVisitor<ParenExprVisitor> { |
| 189 | public: |
| 190 | bool VisitParenExpr(ParenExpr *Parens) { |
| 191 | Match("", Parens->getExprLoc()); |
| 192 | return true; |
| 193 | } |
| 194 | }; |
| 195 | |
Richard Smith | 6fada8e | 2012-05-30 23:55:51 +0000 | [diff] [blame] | 196 | class TemplateArgumentLocTraverser |
| 197 | : public ExpectedLocationVisitor<TemplateArgumentLocTraverser> { |
| 198 | public: |
| 199 | bool TraverseTemplateArgumentLoc(const TemplateArgumentLoc &ArgLoc) { |
| 200 | std::string ArgStr; |
| 201 | llvm::raw_string_ostream Stream(ArgStr); |
| 202 | const TemplateArgument &Arg = ArgLoc.getArgument(); |
| 203 | |
| 204 | Arg.print(Context->getPrintingPolicy(), Stream); |
| 205 | Match(Stream.str(), ArgLoc.getLocation()); |
| 206 | return ExpectedLocationVisitor<TemplateArgumentLocTraverser>:: |
| 207 | TraverseTemplateArgumentLoc(ArgLoc); |
| 208 | } |
| 209 | }; |
| 210 | |
| 211 | class CXXBoolLiteralExprVisitor |
| 212 | : public ExpectedLocationVisitor<CXXBoolLiteralExprVisitor> { |
| 213 | public: |
| 214 | bool VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *BE) { |
| 215 | if (BE->getValue()) |
| 216 | Match("true", BE->getLocation()); |
| 217 | else |
| 218 | Match("false", BE->getLocation()); |
| 219 | return true; |
| 220 | } |
| 221 | }; |
| 222 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 223 | TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) { |
| 224 | TypeLocVisitor Visitor; |
| 225 | Visitor.ExpectMatch("class X", 1, 30); |
| 226 | EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};")); |
| 227 | } |
| 228 | |
Manuel Klimek | 9f99d06 | 2012-04-23 16:40:40 +0000 | [diff] [blame] | 229 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) { |
| 230 | TypeLocVisitor Visitor; |
| 231 | Visitor.ExpectMatch("class X", 3, 18); |
| 232 | EXPECT_TRUE(Visitor.runOver( |
| 233 | "class Y;\n" |
| 234 | "class X {};\n" |
| 235 | "class Y : public X {};")); |
| 236 | } |
| 237 | |
| 238 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) { |
| 239 | TypeLocVisitor Visitor; |
| 240 | Visitor.ExpectMatch("class X", 2, 18); |
| 241 | EXPECT_TRUE(Visitor.runOver( |
| 242 | "class X {};\n" |
| 243 | "class Y : public X { class Z; };")); |
| 244 | } |
| 245 | |
| 246 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) { |
| 247 | TypeLocVisitor Visitor; |
| 248 | Visitor.ExpectMatch("X<class Y>", 2, 18); |
| 249 | EXPECT_TRUE(Visitor.runOver( |
| 250 | "template<typename T> class X {};\n" |
| 251 | "class Y : public X<Y> {};")); |
| 252 | } |
| 253 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 254 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) { |
| 255 | DeclRefExprVisitor Visitor; |
| 256 | Visitor.ExpectMatch("x", 2, 3); |
| 257 | EXPECT_TRUE(Visitor.runOver( |
| 258 | "void x(); template <void (*T)()> class X {};\nX<x> y;")); |
| 259 | } |
| 260 | |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 261 | TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtRange) { |
| 262 | DeclRefExprVisitor Visitor; |
| 263 | Visitor.ExpectMatch("x", 2, 25); |
| 264 | EXPECT_TRUE(Visitor.runOver( |
| 265 | "int x[5];\n" |
| 266 | "void f() { for (int i : x) {} }")); |
| 267 | } |
| 268 | |
| 269 | TEST(RecursiveASTVisitor, VisitsCXXForRangeStmtLoopVariable) { |
| 270 | VarDeclVisitor Visitor; |
| 271 | Visitor.ExpectMatch("i", 2, 17); |
| 272 | EXPECT_TRUE(Visitor.runOver( |
| 273 | "int x[5];\n" |
| 274 | "void f() { for (int i : x) {} }")); |
| 275 | } |
| 276 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 277 | TEST(RecursiveASTVisitor, VisitsCallExpr) { |
| 278 | DeclRefExprVisitor Visitor; |
| 279 | Visitor.ExpectMatch("x", 1, 22); |
| 280 | EXPECT_TRUE(Visitor.runOver( |
| 281 | "void x(); void y() { x(); }")); |
| 282 | } |
| 283 | |
| 284 | TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) { |
| 285 | CXXMemberCallVisitor Visitor; |
| 286 | Visitor.ExpectMatch("Y::x", 3, 3); |
| 287 | EXPECT_TRUE(Visitor.runOver( |
| 288 | "struct Y { void x(); };\n" |
| 289 | "template<typename T> void y(T t) {\n" |
| 290 | " t.x();\n" |
| 291 | "}\n" |
| 292 | "void foo() { y<Y>(Y()); }")); |
| 293 | } |
| 294 | |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 295 | TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 296 | CXXMemberCallVisitor Visitor; |
| 297 | Visitor.ExpectMatch("Y::x", 4, 5); |
| 298 | EXPECT_TRUE(Visitor.runOver( |
| 299 | "struct Y { void x(); };\n" |
| 300 | "template<typename T> struct Z {\n" |
| 301 | " template<typename U> static void f() {\n" |
| 302 | " T().x();\n" |
| 303 | " }\n" |
| 304 | "};\n" |
| 305 | "void foo() { Z<Y>::f<int>(); }")); |
| 306 | } |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 307 | |
| 308 | TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) { |
| 309 | CXXMemberCallVisitor Visitor; |
| 310 | Visitor.ExpectMatch("A::x", 5, 7); |
| 311 | EXPECT_TRUE(Visitor.runOver( |
| 312 | "template <typename T1> struct X {\n" |
| 313 | " template <typename T2> struct Y {\n" |
| 314 | " void f() {\n" |
| 315 | " T2 y;\n" |
| 316 | " y.x();\n" |
| 317 | " }\n" |
| 318 | " };\n" |
| 319 | "};\n" |
| 320 | "struct A { void x(); };\n" |
| 321 | "int main() {\n" |
| 322 | " (new X<A>::Y<A>())->f();\n" |
| 323 | "}")); |
| 324 | } |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 325 | |
| 326 | /* FIXME: According to Richard Smith this is a bug in the AST. |
| 327 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) { |
| 328 | DeclRefExprVisitor Visitor; |
| 329 | Visitor.ExpectMatch("x", 3, 43); |
| 330 | EXPECT_TRUE(Visitor.runOver( |
| 331 | "template <typename T> void x();\n" |
| 332 | "template <void (*T)()> class X {};\n" |
| 333 | "template <typename T> class Y : public X< x<T> > {};\n" |
| 334 | "Y<int> y;")); |
| 335 | } |
| 336 | */ |
| 337 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 338 | TEST(RecursiveASTVisitor, VisitsCallInPartialTemplateSpecialization) { |
| 339 | CXXMemberCallVisitor Visitor; |
| 340 | Visitor.ExpectMatch("A::x", 6, 20); |
| 341 | EXPECT_TRUE(Visitor.runOver( |
| 342 | "template <typename T1> struct X {\n" |
| 343 | " template <typename T2, bool B> struct Y { void g(); };\n" |
| 344 | "};\n" |
| 345 | "template <typename T1> template <typename T2>\n" |
| 346 | "struct X<T1>::Y<T2, true> {\n" |
| 347 | " void f() { T2 y; y.x(); }\n" |
| 348 | "};\n" |
| 349 | "struct A { void x(); };\n" |
| 350 | "int main() {\n" |
| 351 | " (new X<A>::Y<A, true>())->f();\n" |
| 352 | "}\n")); |
| 353 | } |
| 354 | |
Richard Smith | 06cd51a | 2012-05-09 23:51:36 +0000 | [diff] [blame] | 355 | TEST(RecursiveASTVisitor, VisitsExplicitTemplateSpecialization) { |
| 356 | CXXMemberCallVisitor Visitor; |
| 357 | Visitor.ExpectMatch("A::f", 4, 5); |
| 358 | EXPECT_TRUE(Visitor.runOver( |
| 359 | "struct A {\n" |
| 360 | " void f() const {}\n" |
| 361 | " template<class T> void g(const T& t) const {\n" |
| 362 | " t.f();\n" |
| 363 | " }\n" |
| 364 | "};\n" |
| 365 | "template void A::g(const A& a) const;\n")); |
| 366 | } |
| 367 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 368 | TEST(RecursiveASTVisitor, VisitsPartialTemplateSpecialization) { |
| 369 | // From cfe-commits/Week-of-Mon-20100830/033998.html |
Daniel Jasper | e966bea | 2012-05-30 04:30:08 +0000 | [diff] [blame] | 370 | // Contrary to the approach suggested in that email, we visit all |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 371 | // specializations when we visit the primary template. Visiting them when we |
| 372 | // visit the associated specialization is problematic for specializations of |
| 373 | // template members of class templates. |
| 374 | NamedDeclVisitor Visitor; |
| 375 | Visitor.ExpectMatch("A<bool>", 1, 26); |
| 376 | Visitor.ExpectMatch("A<char *>", 2, 26); |
| 377 | EXPECT_TRUE(Visitor.runOver( |
| 378 | "template <class T> class A {};\n" |
| 379 | "template <class T> class A<T*> {};\n" |
| 380 | "A<bool> ab;\n" |
| 381 | "A<char*> acp;\n")); |
| 382 | } |
| 383 | |
| 384 | TEST(RecursiveASTVisitor, VisitsUndefinedClassTemplateSpecialization) { |
| 385 | NamedDeclVisitor Visitor; |
| 386 | Visitor.ExpectMatch("A<int>", 1, 29); |
| 387 | EXPECT_TRUE(Visitor.runOver( |
| 388 | "template<typename T> struct A;\n" |
| 389 | "A<int> *p;\n")); |
| 390 | } |
| 391 | |
| 392 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedClassTemplateSpecialization) { |
| 393 | NamedDeclVisitor Visitor; |
| 394 | Visitor.ExpectMatch("A<int>::B<char>", 2, 31); |
| 395 | EXPECT_TRUE(Visitor.runOver( |
| 396 | "template<typename T> struct A {\n" |
| 397 | " template<typename U> struct B;\n" |
| 398 | "};\n" |
| 399 | "A<int>::B<char> *p;\n")); |
| 400 | } |
| 401 | |
| 402 | TEST(RecursiveASTVisitor, VisitsUndefinedFunctionTemplateSpecialization) { |
| 403 | NamedDeclVisitor Visitor; |
| 404 | Visitor.ExpectMatch("A<int>", 1, 26); |
| 405 | EXPECT_TRUE(Visitor.runOver( |
| 406 | "template<typename T> int A();\n" |
| 407 | "int k = A<int>();\n")); |
| 408 | } |
| 409 | |
| 410 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedFunctionTemplateSpecialization) { |
| 411 | NamedDeclVisitor Visitor; |
| 412 | Visitor.ExpectMatch("A<int>::B<char>", 2, 35); |
| 413 | EXPECT_TRUE(Visitor.runOver( |
| 414 | "template<typename T> struct A {\n" |
| 415 | " template<typename U> static int B();\n" |
| 416 | "};\n" |
| 417 | "int k = A<int>::B<char>();\n")); |
| 418 | } |
| 419 | |
| 420 | TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) { |
| 421 | // From cfe-commits/Week-of-Mon-20100830/033977.html |
| 422 | NamedDeclVisitor Visitor; |
| 423 | Visitor.ExpectMatch("vector_iterator<int>", 2, 7); |
| 424 | EXPECT_TRUE(Visitor.runOver( |
| 425 | "template<typename Container>\n" |
| 426 | "class vector_iterator {\n" |
| 427 | " template <typename C> friend class vector_iterator;\n" |
| 428 | "};\n" |
| 429 | "vector_iterator<int> it_int;\n")); |
| 430 | } |
| 431 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 432 | TEST(RecursiveASTVisitor, TraversesOverloadedOperator) { |
| 433 | CXXOperatorCallExprTraverser Visitor; |
| 434 | Visitor.ExpectMatch("()", 4, 9); |
| 435 | EXPECT_TRUE(Visitor.runOver( |
| 436 | "struct A {\n" |
| 437 | " int operator()();\n" |
| 438 | "} a;\n" |
| 439 | "int k = a();\n")); |
| 440 | } |
| 441 | |
| 442 | TEST(RecursiveASTVisitor, VisitsParensDuringDataRecursion) { |
| 443 | ParenExprVisitor Visitor; |
| 444 | Visitor.ExpectMatch("", 1, 9); |
| 445 | EXPECT_TRUE(Visitor.runOver("int k = (4) + 9;\n")); |
| 446 | } |
| 447 | |
Richard Smith | 6fada8e | 2012-05-30 23:55:51 +0000 | [diff] [blame] | 448 | TEST(RecursiveASTVisitor, VisitsClassTemplateNonTypeParmDefaultArgument) { |
| 449 | CXXBoolLiteralExprVisitor Visitor; |
| 450 | Visitor.ExpectMatch("true", 2, 19); |
| 451 | EXPECT_TRUE(Visitor.runOver( |
| 452 | "template<bool B> class X;\n" |
| 453 | "template<bool B = true> class Y;\n" |
| 454 | "template<bool B> class Y {};\n")); |
| 455 | } |
| 456 | |
| 457 | TEST(RecursiveASTVisitor, VisitsClassTemplateTypeParmDefaultArgument) { |
| 458 | TypeLocVisitor Visitor; |
| 459 | Visitor.ExpectMatch("class X", 2, 23); |
| 460 | EXPECT_TRUE(Visitor.runOver( |
| 461 | "class X;\n" |
| 462 | "template<typename T = X> class Y;\n" |
| 463 | "template<typename T> class Y {};\n")); |
| 464 | } |
| 465 | |
| 466 | TEST(RecursiveASTVisitor, VisitsClassTemplateTemplateParmDefaultArgument) { |
| 467 | TemplateArgumentLocTraverser Visitor; |
| 468 | Visitor.ExpectMatch("X", 2, 40); |
| 469 | EXPECT_TRUE(Visitor.runOver( |
| 470 | "template<typename T> class X;\n" |
| 471 | "template<template <typename> class T = X> class Y;\n" |
| 472 | "template<template <typename> class T> class Y {};\n")); |
| 473 | } |
| 474 | |
Richard Smith | c28a335 | 2012-06-05 16:18:26 +0000 | [diff] [blame] | 475 | // A visitor that visits implicit declarations and matches constructors. |
| 476 | class ImplicitCtorVisitor |
| 477 | : public ExpectedLocationVisitor<ImplicitCtorVisitor> { |
| 478 | public: |
Daniel Jasper | 52ec0c0 | 2012-06-13 07:12:33 +0000 | [diff] [blame] | 479 | bool shouldVisitImplicitCode() const { return true; } |
Richard Smith | c28a335 | 2012-06-05 16:18:26 +0000 | [diff] [blame] | 480 | |
| 481 | bool VisitCXXConstructorDecl(CXXConstructorDecl* Ctor) { |
| 482 | if (Ctor->isImplicit()) { // Was not written in source code |
| 483 | if (const CXXRecordDecl* Class = Ctor->getParent()) { |
| 484 | Match(Class->getName(), Ctor->getLocation()); |
| 485 | } |
| 486 | } |
| 487 | return true; |
| 488 | } |
| 489 | }; |
| 490 | |
| 491 | TEST(RecursiveASTVisitor, VisitsImplicitCopyConstructors) { |
| 492 | ImplicitCtorVisitor Visitor; |
| 493 | Visitor.ExpectMatch("Simple", 2, 8); |
| 494 | // Note: Clang lazily instantiates implicit declarations, so we need |
| 495 | // to use them in order to force them to appear in the AST. |
| 496 | EXPECT_TRUE(Visitor.runOver( |
| 497 | "struct WithCtor { WithCtor(); }; \n" |
| 498 | "struct Simple { Simple(); WithCtor w; }; \n" |
| 499 | "int main() { Simple s; Simple t(s); }\n")); |
| 500 | } |
| 501 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 502 | } // end namespace clang |