blob: e39d00f6af3de2a18ea57e27feab1277cf2b320d [file] [log] [blame]
Jeffrey Yasskin7a178892011-02-03 04:51:52 +00001//===- 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
Chandler Carruth1050e8b2012-12-04 09:45:34 +000010#include "clang/Frontend/FrontendAction.h"
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000011#include "clang/AST/ASTConsumer.h"
Chandler Carruth1050e8b2012-12-04 09:45:34 +000012#include "clang/AST/ASTContext.h"
Chandler Carruth7cc315c2012-12-04 09:53:37 +000013#include "clang/AST/RecursiveASTVisitor.h"
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/CompilerInvocation.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070016#include "clang/Lex/Preprocessor.h"
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000017#include "llvm/ADT/Triple.h"
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000018#include "llvm/Support/MemoryBuffer.h"
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000019#include "gtest/gtest.h"
20
21using namespace llvm;
22using namespace clang;
23
24namespace {
25
26class TestASTFrontendAction : public ASTFrontendAction {
27public:
Stephen Hines651f13c2014-04-23 16:59:28 -070028 TestASTFrontendAction(bool enableIncrementalProcessing = false)
29 : EnableIncrementalProcessing(enableIncrementalProcessing) { }
30
31 bool EnableIncrementalProcessing;
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000032 std::vector<std::string> decl_names;
33
Stephen Hines651f13c2014-04-23 16:59:28 -070034 virtual bool BeginSourceFileAction(CompilerInstance &ci, StringRef filename) {
35 if (EnableIncrementalProcessing)
36 ci.getPreprocessor().enableIncrementalProcessing();
37
38 return ASTFrontendAction::BeginSourceFileAction(ci, filename);
39 }
40
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000041 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
42 StringRef InFile) {
43 return new Visitor(decl_names);
44 }
45
46private:
47 class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
48 public:
49 Visitor(std::vector<std::string> &decl_names) : decl_names_(decl_names) {}
50
51 virtual void HandleTranslationUnit(ASTContext &context) {
52 TraverseDecl(context.getTranslationUnitDecl());
53 }
54
55 virtual bool VisitNamedDecl(NamedDecl *Decl) {
56 decl_names_.push_back(Decl->getQualifiedNameAsString());
57 return true;
58 }
59
60 private:
61 std::vector<std::string> &decl_names_;
62 };
63};
64
65TEST(ASTFrontendAction, Sanity) {
66 CompilerInvocation *invocation = new CompilerInvocation;
67 invocation->getPreprocessorOpts().addRemappedFile(
68 "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
Douglas Gregoreabcf7e2012-01-20 16:33:00 +000069 invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
70 IK_CXX));
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000071 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
72 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
73 CompilerInstance compiler;
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000074 compiler.setInvocation(invocation);
Sean Silvad47afb92013-01-20 01:58:28 +000075 compiler.createDiagnostics();
Jeffrey Yasskin7a178892011-02-03 04:51:52 +000076
77 TestASTFrontendAction test_action;
78 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Stephen Hines651f13c2014-04-23 16:59:28 -070079 ASSERT_EQ(2U, test_action.decl_names.size());
80 EXPECT_EQ("main", test_action.decl_names[0]);
81 EXPECT_EQ("x", test_action.decl_names[1]);
82}
83
84TEST(ASTFrontendAction, IncrementalParsing) {
85 CompilerInvocation *invocation = new CompilerInvocation;
86 invocation->getPreprocessorOpts().addRemappedFile(
87 "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
88 invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
89 IK_CXX));
90 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
91 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
92 CompilerInstance compiler;
93 compiler.setInvocation(invocation);
94 compiler.createDiagnostics();
95
96 TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
97 ASSERT_TRUE(compiler.ExecuteAction(test_action));
98 ASSERT_EQ(2U, test_action.decl_names.size());
99 EXPECT_EQ("main", test_action.decl_names[0]);
100 EXPECT_EQ("x", test_action.decl_names[1]);
Jeffrey Yasskin7a178892011-02-03 04:51:52 +0000101}
102
103} // anonymous namespace