Home | History | Annotate | Download | only in Tooling
      1 //===- unittest/Tooling/ToolingTest.cpp - Tooling unit tests --------------===//
      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/DeclCXX.h"
     12 #include "clang/AST/DeclGroup.h"
     13 #include "clang/Frontend/FrontendAction.h"
     14 #include "clang/Frontend/FrontendActions.h"
     15 #include "clang/Tooling/CompilationDatabase.h"
     16 #include "clang/Tooling/Tooling.h"
     17 #include "gtest/gtest.h"
     18 
     19 namespace clang {
     20 namespace tooling {
     21 
     22 namespace {
     23 /// Takes an ast consumer and returns it from CreateASTConsumer. This only
     24 /// works with single translation unit compilations.
     25 class TestAction : public clang::ASTFrontendAction {
     26  public:
     27   /// Takes ownership of TestConsumer.
     28   explicit TestAction(clang::ASTConsumer *TestConsumer)
     29       : TestConsumer(TestConsumer) {}
     30 
     31  protected:
     32   virtual clang::ASTConsumer* CreateASTConsumer(
     33       clang::CompilerInstance& compiler, StringRef dummy) {
     34     /// TestConsumer will be deleted by the framework calling us.
     35     return TestConsumer;
     36   }
     37 
     38  private:
     39   clang::ASTConsumer * const TestConsumer;
     40 };
     41 
     42 class FindTopLevelDeclConsumer : public clang::ASTConsumer {
     43  public:
     44   explicit FindTopLevelDeclConsumer(bool *FoundTopLevelDecl)
     45       : FoundTopLevelDecl(FoundTopLevelDecl) {}
     46   virtual bool HandleTopLevelDecl(clang::DeclGroupRef DeclGroup) {
     47     *FoundTopLevelDecl = true;
     48     return true;
     49   }
     50  private:
     51   bool * const FoundTopLevelDecl;
     52 };
     53 } // end namespace
     54 
     55 TEST(runToolOnCode, FindsTopLevelDeclOnEmptyCode) {
     56   bool FoundTopLevelDecl = false;
     57   EXPECT_TRUE(runToolOnCode(
     58       new TestAction(new FindTopLevelDeclConsumer(&FoundTopLevelDecl)), ""));
     59   EXPECT_TRUE(FoundTopLevelDecl);
     60 }
     61 
     62 namespace {
     63 class FindClassDeclXConsumer : public clang::ASTConsumer {
     64  public:
     65   FindClassDeclXConsumer(bool *FoundClassDeclX)
     66       : FoundClassDeclX(FoundClassDeclX) {}
     67   virtual bool HandleTopLevelDecl(clang::DeclGroupRef GroupRef) {
     68     if (CXXRecordDecl* Record = dyn_cast<clang::CXXRecordDecl>(
     69             *GroupRef.begin())) {
     70       if (Record->getName() == "X") {
     71         *FoundClassDeclX = true;
     72       }
     73     }
     74     return true;
     75   }
     76  private:
     77   bool *FoundClassDeclX;
     78 };
     79 } // end namespace
     80 
     81 TEST(runToolOnCode, FindsClassDecl) {
     82   bool FoundClassDeclX = false;
     83   EXPECT_TRUE(runToolOnCode(new TestAction(
     84       new FindClassDeclXConsumer(&FoundClassDeclX)), "class X;"));
     85   EXPECT_TRUE(FoundClassDeclX);
     86 
     87   FoundClassDeclX = false;
     88   EXPECT_TRUE(runToolOnCode(new TestAction(
     89       new FindClassDeclXConsumer(&FoundClassDeclX)), "class Y;"));
     90   EXPECT_FALSE(FoundClassDeclX);
     91 }
     92 
     93 TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromType) {
     94   llvm::OwningPtr<FrontendActionFactory> Factory(
     95     newFrontendActionFactory<SyntaxOnlyAction>());
     96   llvm::OwningPtr<FrontendAction> Action(Factory->create());
     97   EXPECT_TRUE(Action.get() != NULL);
     98 }
     99 
    100 struct IndependentFrontendActionCreator {
    101   FrontendAction *newFrontendAction() { return new SyntaxOnlyAction; }
    102 };
    103 
    104 TEST(newFrontendActionFactory, CreatesFrontendActionFactoryFromFactoryType) {
    105   IndependentFrontendActionCreator Creator;
    106   llvm::OwningPtr<FrontendActionFactory> Factory(
    107     newFrontendActionFactory(&Creator));
    108   llvm::OwningPtr<FrontendAction> Action(Factory->create());
    109   EXPECT_TRUE(Action.get() != NULL);
    110 }
    111 
    112 } // end namespace tooling
    113 } // end namespace clang
    114