Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 1 | //===- unittest/AST/RecursiveASTMatcherTest.cpp ---------------------------===// |
| 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 | |
| 155 | TEST(RecursiveASTVisitor, VisitsBaseClassDeclarations) { |
| 156 | TypeLocVisitor Visitor; |
| 157 | Visitor.ExpectMatch("class X", 1, 30); |
| 158 | EXPECT_TRUE(Visitor.runOver("class X {}; class Y : public X {};")); |
| 159 | } |
| 160 | |
Manuel Klimek | 9f99d06 | 2012-04-23 16:40:40 +0000 | [diff] [blame] | 161 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfForwardDeclaredClass) { |
| 162 | TypeLocVisitor Visitor; |
| 163 | Visitor.ExpectMatch("class X", 3, 18); |
| 164 | EXPECT_TRUE(Visitor.runOver( |
| 165 | "class Y;\n" |
| 166 | "class X {};\n" |
| 167 | "class Y : public X {};")); |
| 168 | } |
| 169 | |
| 170 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersWithIncompleteInnerClass) { |
| 171 | TypeLocVisitor Visitor; |
| 172 | Visitor.ExpectMatch("class X", 2, 18); |
| 173 | EXPECT_TRUE(Visitor.runOver( |
| 174 | "class X {};\n" |
| 175 | "class Y : public X { class Z; };")); |
| 176 | } |
| 177 | |
| 178 | TEST(RecursiveASTVisitor, VisitsCXXBaseSpecifiersOfSelfReferentialType) { |
| 179 | TypeLocVisitor Visitor; |
| 180 | Visitor.ExpectMatch("X<class Y>", 2, 18); |
| 181 | EXPECT_TRUE(Visitor.runOver( |
| 182 | "template<typename T> class X {};\n" |
| 183 | "class Y : public X<Y> {};")); |
| 184 | } |
| 185 | |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 186 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArguments) { |
| 187 | DeclRefExprVisitor Visitor; |
| 188 | Visitor.ExpectMatch("x", 2, 3); |
| 189 | EXPECT_TRUE(Visitor.runOver( |
| 190 | "void x(); template <void (*T)()> class X {};\nX<x> y;")); |
| 191 | } |
| 192 | |
| 193 | TEST(RecursiveASTVisitor, VisitsCallExpr) { |
| 194 | DeclRefExprVisitor Visitor; |
| 195 | Visitor.ExpectMatch("x", 1, 22); |
| 196 | EXPECT_TRUE(Visitor.runOver( |
| 197 | "void x(); void y() { x(); }")); |
| 198 | } |
| 199 | |
| 200 | TEST(RecursiveASTVisitor, VisitsCallInTemplateInstantiation) { |
| 201 | CXXMemberCallVisitor Visitor; |
| 202 | Visitor.ExpectMatch("Y::x", 3, 3); |
| 203 | EXPECT_TRUE(Visitor.runOver( |
| 204 | "struct Y { void x(); };\n" |
| 205 | "template<typename T> void y(T t) {\n" |
| 206 | " t.x();\n" |
| 207 | "}\n" |
| 208 | "void foo() { y<Y>(Y()); }")); |
| 209 | } |
| 210 | |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame^] | 211 | TEST(RecursiveASTVisitor, VisitsCallInNestedFunctionTemplateInstantiation) { |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 212 | CXXMemberCallVisitor Visitor; |
| 213 | Visitor.ExpectMatch("Y::x", 4, 5); |
| 214 | EXPECT_TRUE(Visitor.runOver( |
| 215 | "struct Y { void x(); };\n" |
| 216 | "template<typename T> struct Z {\n" |
| 217 | " template<typename U> static void f() {\n" |
| 218 | " T().x();\n" |
| 219 | " }\n" |
| 220 | "};\n" |
| 221 | "void foo() { Z<Y>::f<int>(); }")); |
| 222 | } |
Richard Smith | 5482dc3 | 2012-04-24 20:39:49 +0000 | [diff] [blame^] | 223 | |
| 224 | TEST(RecursiveASTVisitor, VisitsCallInNestedClassTemplateInstantiation) { |
| 225 | CXXMemberCallVisitor Visitor; |
| 226 | Visitor.ExpectMatch("A::x", 5, 7); |
| 227 | EXPECT_TRUE(Visitor.runOver( |
| 228 | "template <typename T1> struct X {\n" |
| 229 | " template <typename T2> struct Y {\n" |
| 230 | " void f() {\n" |
| 231 | " T2 y;\n" |
| 232 | " y.x();\n" |
| 233 | " }\n" |
| 234 | " };\n" |
| 235 | "};\n" |
| 236 | "struct A { void x(); };\n" |
| 237 | "int main() {\n" |
| 238 | " (new X<A>::Y<A>())->f();\n" |
| 239 | "}")); |
| 240 | } |
Manuel Klimek | fad7f85 | 2012-04-19 08:48:53 +0000 | [diff] [blame] | 241 | |
| 242 | /* FIXME: According to Richard Smith this is a bug in the AST. |
| 243 | TEST(RecursiveASTVisitor, VisitsBaseClassTemplateArgumentsInInstantiation) { |
| 244 | DeclRefExprVisitor Visitor; |
| 245 | Visitor.ExpectMatch("x", 3, 43); |
| 246 | EXPECT_TRUE(Visitor.runOver( |
| 247 | "template <typename T> void x();\n" |
| 248 | "template <void (*T)()> class X {};\n" |
| 249 | "template <typename T> class Y : public X< x<T> > {};\n" |
| 250 | "Y<int> y;")); |
| 251 | } |
| 252 | */ |
| 253 | |
| 254 | } // end namespace clang |