• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction 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/RecursiveASTVisitor.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Frontend/CompilerInstance.h"
13 #include "clang/Frontend/CompilerInvocation.h"
14 #include "clang/Frontend/FrontendAction.h"
15 
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 
19 #include "gtest/gtest.h"
20 
21 using namespace llvm;
22 using namespace clang;
23 
24 namespace {
25 
26 class TestASTFrontendAction : public ASTFrontendAction {
27 public:
28   std::vector<std::string> decl_names;
29 
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)30   virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
31                                          StringRef InFile) {
32     return new Visitor(decl_names);
33   }
34 
35 private:
36   class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
37   public:
Visitor(std::vector<std::string> & decl_names)38     Visitor(std::vector<std::string> &decl_names) : decl_names_(decl_names) {}
39 
HandleTranslationUnit(ASTContext & context)40     virtual void HandleTranslationUnit(ASTContext &context) {
41       TraverseDecl(context.getTranslationUnitDecl());
42     }
43 
VisitNamedDecl(NamedDecl * Decl)44     virtual bool VisitNamedDecl(NamedDecl *Decl) {
45       decl_names_.push_back(Decl->getQualifiedNameAsString());
46       return true;
47     }
48 
49   private:
50     std::vector<std::string> &decl_names_;
51   };
52 };
53 
TEST(ASTFrontendAction,Sanity)54 TEST(ASTFrontendAction, Sanity) {
55   CompilerInvocation *invocation = new CompilerInvocation;
56   invocation->getPreprocessorOpts().addRemappedFile(
57     "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
58   invocation->getFrontendOpts().Inputs.push_back(
59     std::make_pair(IK_CXX, "test.cc"));
60   invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
61   invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
62   CompilerInstance compiler;
63   compiler.setInvocation(invocation);
64   compiler.createDiagnostics(0, NULL);
65 
66   TestASTFrontendAction test_action;
67   ASSERT_TRUE(compiler.ExecuteAction(test_action));
68   ASSERT_EQ(3U, test_action.decl_names.size());
69   EXPECT_EQ("__builtin_va_list", test_action.decl_names[0]);
70   EXPECT_EQ("main", test_action.decl_names[1]);
71   EXPECT_EQ("x", test_action.decl_names[2]);
72 }
73 
74 } // anonymous namespace
75