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 | |
| 145 | class CXXMemberCallVisitor |
| 146 | : public ExpectedLocationVisitor<CXXMemberCallVisitor> { |
| 147 | public: |
| 148 | bool VisitCXXMemberCallExpr(CXXMemberCallExpr *Call) { |
| 149 | Match(Call->getMethodDecl()->getQualifiedNameAsString(), |
| 150 | Call->getLocStart()); |
| 151 | return true; |
| 152 | } |
| 153 | }; |
| 154 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 155 | class NamedDeclVisitor |
| 156 | : public ExpectedLocationVisitor<NamedDeclVisitor> { |
| 157 | public: |
| 158 | bool VisitNamedDecl(NamedDecl *Decl) { |
| 159 | std::string NameWithTemplateArgs; |
| 160 | Decl->getNameForDiagnostic(NameWithTemplateArgs, |
| 161 | Decl->getASTContext().getPrintingPolicy(), |
| 162 | true); |
| 163 | Match(NameWithTemplateArgs, Decl->getLocation()); |
| 164 | return true; |
| 165 | } |
| 166 | }; |
| 167 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 168 | class CXXOperatorCallExprTraverser |
| 169 | : public ExpectedLocationVisitor<CXXOperatorCallExprTraverser> { |
| 170 | public: |
| 171 | // Use Traverse, not Visit, to check that data recursion optimization isn't |
| 172 | // bypassing the call of this function. |
| 173 | bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *CE) { |
| 174 | Match(getOperatorSpelling(CE->getOperator()), CE->getExprLoc()); |
| 175 | return ExpectedLocationVisitor<CXXOperatorCallExprTraverser>:: |
| 176 | TraverseCXXOperatorCallExpr(CE); |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | class ParenExprVisitor : public ExpectedLocationVisitor<ParenExprVisitor> { |
| 181 | public: |
| 182 | bool VisitParenExpr(ParenExpr *Parens) { |
| 183 | Match("", Parens->getExprLoc()); |
| 184 | return true; |
| 185 | } |
| 186 | }; |
| 187 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 188 | TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) { |
| 189 | TypeLocVisitor Visitor; |
| 190 | Visitor.ExpectMatch("class X", 1, 30); |
| 191 | EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};")); |
| 192 | } |
| 193 | |
Manuel Klimek | 9f99d06 | 2012-04-23 16:40:40 +0000 | [diff] [blame] | 194 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) { |
| 195 | TypeLocVisitor Visitor; |
| 196 | Visitor.ExpectMatch("class X", 3, 18); |
| 197 | EXPECT_TRUE(Visitor.runOver( |
| 198 | "class Y;\n" |
| 199 | "class X {};\n" |
| 200 | "class Y : public X {};")); |
| 201 | } |
| 202 | |
| 203 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) { |
| 204 | TypeLocVisitor Visitor; |
| 205 | Visitor.ExpectMatch("class X", 2, 18); |
| 206 | EXPECT_TRUE(Visitor.runOver( |
| 207 | "class X {};\n" |
| 208 | "class Y : public X { class Z; };")); |
| 209 | } |
| 210 | |
| 211 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) { |
| 212 | TypeLocVisitor Visitor; |
| 213 | Visitor.ExpectMatch("X<class Y>", 2, 18); |
| 214 | EXPECT_TRUE(Visitor.runOver( |
| 215 | "template<typename T> class X {};\n" |
| 216 | "class Y : public X<Y> {};")); |
| 217 | } |
| 218 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 219 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) { |
| 220 | DeclRefExprVisitor Visitor; |
| 221 | Visitor.ExpectMatch("x", 2, 3); |
| 222 | EXPECT_TRUE(Visitor.runOver( |
| 223 | "void x(); template <void (*T)()> class X {};\nX<x> y;")); |
| 224 | } |
| 225 | |
| 226 | TEST(RecursiveASTVisitor, VisitsCallExpr) { |
| 227 | DeclRefExprVisitor Visitor; |
| 228 | Visitor.ExpectMatch("x", 1, 22); |
| 229 | EXPECT_TRUE(Visitor.runOver( |
| 230 | "void x(); void y() { x(); }")); |
| 231 | } |
| 232 | |
| 233 | TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) { |
| 234 | CXXMemberCallVisitor Visitor; |
| 235 | Visitor.ExpectMatch("Y::x", 3, 3); |
| 236 | EXPECT_TRUE(Visitor.runOver( |
| 237 | "struct Y { void x(); };\n" |
| 238 | "template<typename T> void y(T t) {\n" |
| 239 | " t.x();\n" |
| 240 | "}\n" |
| 241 | "void foo() { y<Y>(Y()); }")); |
| 242 | } |
| 243 | |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 244 | TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 245 | CXXMemberCallVisitor Visitor; |
| 246 | Visitor.ExpectMatch("Y::x", 4, 5); |
| 247 | EXPECT_TRUE(Visitor.runOver( |
| 248 | "struct Y { void x(); };\n" |
| 249 | "template<typename T> struct Z {\n" |
| 250 | " template<typename U> static void f() {\n" |
| 251 | " T().x();\n" |
| 252 | " }\n" |
| 253 | "};\n" |
| 254 | "void foo() { Z<Y>::f<int>(); }")); |
| 255 | } |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame] | 256 | |
| 257 | TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) { |
| 258 | CXXMemberCallVisitor Visitor; |
| 259 | Visitor.ExpectMatch("A::x", 5, 7); |
| 260 | EXPECT_TRUE(Visitor.runOver( |
| 261 | "template <typename T1> struct X {\n" |
| 262 | " template <typename T2> struct Y {\n" |
| 263 | " void f() {\n" |
| 264 | " T2 y;\n" |
| 265 | " y.x();\n" |
| 266 | " }\n" |
| 267 | " };\n" |
| 268 | "};\n" |
| 269 | "struct A { void x(); };\n" |
| 270 | "int main() {\n" |
| 271 | " (new X<A>::Y<A>())->f();\n" |
| 272 | "}")); |
| 273 | } |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 274 | |
| 275 | /* FIXME: According to Richard Smith this is a bug in the AST. |
| 276 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) { |
| 277 | DeclRefExprVisitor Visitor; |
| 278 | Visitor.ExpectMatch("x", 3, 43); |
| 279 | EXPECT_TRUE(Visitor.runOver( |
| 280 | "template <typename T> void x();\n" |
| 281 | "template <void (*T)()> class X {};\n" |
| 282 | "template <typename T> class Y : public X< x<T> > {};\n" |
| 283 | "Y<int> y;")); |
| 284 | } |
| 285 | */ |
| 286 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 287 | TEST(RecursiveASTVisitor, VisitsCallInPartialTemplateSpecialization) { |
| 288 | CXXMemberCallVisitor Visitor; |
| 289 | Visitor.ExpectMatch("A::x", 6, 20); |
| 290 | EXPECT_TRUE(Visitor.runOver( |
| 291 | "template <typename T1> struct X {\n" |
| 292 | " template <typename T2, bool B> struct Y { void g(); };\n" |
| 293 | "};\n" |
| 294 | "template <typename T1> template <typename T2>\n" |
| 295 | "struct X<T1>::Y<T2, true> {\n" |
| 296 | " void f() { T2 y; y.x(); }\n" |
| 297 | "};\n" |
| 298 | "struct A { void x(); };\n" |
| 299 | "int main() {\n" |
| 300 | " (new X<A>::Y<A, true>())->f();\n" |
| 301 | "}\n")); |
| 302 | } |
| 303 | |
Richard Smith | 06cd51a | 2012-05-09 23:51:36 +0000 | [diff] [blame] | 304 | TEST(RecursiveASTVisitor, VisitsExplicitTemplateSpecialization) { |
| 305 | CXXMemberCallVisitor Visitor; |
| 306 | Visitor.ExpectMatch("A::f", 4, 5); |
| 307 | EXPECT_TRUE(Visitor.runOver( |
| 308 | "struct A {\n" |
| 309 | " void f() const {}\n" |
| 310 | " template<class T> void g(const T& t) const {\n" |
| 311 | " t.f();\n" |
| 312 | " }\n" |
| 313 | "};\n" |
| 314 | "template void A::g(const A& a) const;\n")); |
| 315 | } |
| 316 | |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 317 | TEST(RecursiveASTVisitor, VisitsPartialTemplateSpecialization) { |
| 318 | // From cfe-commits/Week-of-Mon-20100830/033998.html |
Daniel Jasper | e966bea | 2012-05-30 04:30:08 +0000 | [diff] [blame^] | 319 | // Contrary to the approach suggested in that email, we visit all |
Richard Smith | a313b2f | 2012-04-25 22:57:25 +0000 | [diff] [blame] | 320 | // specializations when we visit the primary template. Visiting them when we |
| 321 | // visit the associated specialization is problematic for specializations of |
| 322 | // template members of class templates. |
| 323 | NamedDeclVisitor Visitor; |
| 324 | Visitor.ExpectMatch("A<bool>", 1, 26); |
| 325 | Visitor.ExpectMatch("A<char *>", 2, 26); |
| 326 | EXPECT_TRUE(Visitor.runOver( |
| 327 | "template <class T> class A {};\n" |
| 328 | "template <class T> class A<T*> {};\n" |
| 329 | "A<bool> ab;\n" |
| 330 | "A<char*> acp;\n")); |
| 331 | } |
| 332 | |
| 333 | TEST(RecursiveASTVisitor, VisitsUndefinedClassTemplateSpecialization) { |
| 334 | NamedDeclVisitor Visitor; |
| 335 | Visitor.ExpectMatch("A<int>", 1, 29); |
| 336 | EXPECT_TRUE(Visitor.runOver( |
| 337 | "template<typename T> struct A;\n" |
| 338 | "A<int> *p;\n")); |
| 339 | } |
| 340 | |
| 341 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedClassTemplateSpecialization) { |
| 342 | NamedDeclVisitor Visitor; |
| 343 | Visitor.ExpectMatch("A<int>::B<char>", 2, 31); |
| 344 | EXPECT_TRUE(Visitor.runOver( |
| 345 | "template<typename T> struct A {\n" |
| 346 | " template<typename U> struct B;\n" |
| 347 | "};\n" |
| 348 | "A<int>::B<char> *p;\n")); |
| 349 | } |
| 350 | |
| 351 | TEST(RecursiveASTVisitor, VisitsUndefinedFunctionTemplateSpecialization) { |
| 352 | NamedDeclVisitor Visitor; |
| 353 | Visitor.ExpectMatch("A<int>", 1, 26); |
| 354 | EXPECT_TRUE(Visitor.runOver( |
| 355 | "template<typename T> int A();\n" |
| 356 | "int k = A<int>();\n")); |
| 357 | } |
| 358 | |
| 359 | TEST(RecursiveASTVisitor, VisitsNestedUndefinedFunctionTemplateSpecialization) { |
| 360 | NamedDeclVisitor Visitor; |
| 361 | Visitor.ExpectMatch("A<int>::B<char>", 2, 35); |
| 362 | EXPECT_TRUE(Visitor.runOver( |
| 363 | "template<typename T> struct A {\n" |
| 364 | " template<typename U> static int B();\n" |
| 365 | "};\n" |
| 366 | "int k = A<int>::B<char>();\n")); |
| 367 | } |
| 368 | |
| 369 | TEST(RecursiveASTVisitor, NoRecursionInSelfFriend) { |
| 370 | // From cfe-commits/Week-of-Mon-20100830/033977.html |
| 371 | NamedDeclVisitor Visitor; |
| 372 | Visitor.ExpectMatch("vector_iterator<int>", 2, 7); |
| 373 | EXPECT_TRUE(Visitor.runOver( |
| 374 | "template<typename Container>\n" |
| 375 | "class vector_iterator {\n" |
| 376 | " template <typename C> friend class vector_iterator;\n" |
| 377 | "};\n" |
| 378 | "vector_iterator<int> it_int;\n")); |
| 379 | } |
| 380 | |
Richard Smith | c8c2228 | 2012-05-02 00:30:48 +0000 | [diff] [blame] | 381 | TEST(RecursiveASTVisitor, TraversesOverloadedOperator) { |
| 382 | CXXOperatorCallExprTraverser Visitor; |
| 383 | Visitor.ExpectMatch("()", 4, 9); |
| 384 | EXPECT_TRUE(Visitor.runOver( |
| 385 | "struct A {\n" |
| 386 | " int operator()();\n" |
| 387 | "} a;\n" |
| 388 | "int k = a();\n")); |
| 389 | } |
| 390 | |
| 391 | TEST(RecursiveASTVisitor, VisitsParensDuringDataRecursion) { |
| 392 | ParenExprVisitor Visitor; |
| 393 | Visitor.ExpectMatch("", 1, 9); |
| 394 | EXPECT_TRUE(Visitor.runOver("int k = (4) + 9;\n")); |
| 395 | } |
| 396 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 397 | } // end namespace clang |