1 //===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Support/MemoryBuffer.h"
19 #include "gtest/gtest.h"
22 using namespace clang
;
26 class TestASTFrontendAction
: public ASTFrontendAction
{
28 std::vector
<std::string
> decl_names
;
30 virtual ASTConsumer
*CreateASTConsumer(CompilerInstance
&CI
,
32 return new Visitor(decl_names
);
36 class Visitor
: public ASTConsumer
, public RecursiveASTVisitor
<Visitor
> {
38 Visitor(std::vector
<std::string
> &decl_names
) : decl_names_(decl_names
) {}
40 virtual void HandleTranslationUnit(ASTContext
&context
) {
41 TraverseDecl(context
.getTranslationUnitDecl());
44 virtual bool VisitNamedDecl(NamedDecl
*Decl
) {
45 decl_names_
.push_back(Decl
->getQualifiedNameAsString());
50 std::vector
<std::string
> &decl_names_
;
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
);
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]);
74 } // anonymous namespace