blob: 44843eed1379c1bdd7d22429abb17d90b8937013 [file] [log] [blame]
Jeffrey Yasskinc7da9932011-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 Carruth320d9662012-12-04 09:45:34 +000010#include "clang/Frontend/FrontendAction.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000011#include "clang/AST/ASTConsumer.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000012#include "clang/AST/ASTContext.h"
Chandler Carruthfa0b3bb2012-12-04 09:53:37 +000013#include "clang/AST/RecursiveASTVisitor.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/CompilerInvocation.h"
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000016#include "clang/Lex/Preprocessor.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000017#include "llvm/ADT/Triple.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000018#include "llvm/Support/MemoryBuffer.h"
Jeffrey Yasskinc7da9932011-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:
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000028 TestASTFrontendAction(bool enableIncrementalProcessing = false)
29 : EnableIncrementalProcessing(enableIncrementalProcessing) { }
30
31 bool EnableIncrementalProcessing;
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000032 std::vector<std::string> decl_names;
33
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000034 virtual bool BeginSourceFileAction(CompilerInstance &ci, StringRef filename) {
35 if (EnableIncrementalProcessing)
36 ci.getPreprocessor().enableIncrementalProcessing();
37
38 return ASTFrontendAction::BeginSourceFileAction(ci, filename);
39 }
40
David Blaikie62a56f32014-07-17 22:34:12 +000041 virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI,
42 StringRef InFile) {
43 return new Visitor(decl_names);
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000044 }
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 Gregord6a58282012-01-20 16:33:00 +000069 invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
70 IK_CXX));
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000071 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
72 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
73 CompilerInstance compiler;
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000074 compiler.setInvocation(invocation);
Sean Silvaf1b49e22013-01-20 01:58:28 +000075 compiler.createDiagnostics();
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000076
77 TestASTFrontendAction test_action;
78 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Alp Toker56b5cc92013-12-15 10:36:26 +000079 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]);
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000082}
83
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000084TEST(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));
Alp Toker56b5cc92013-12-15 10:36:26 +000098 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]);
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +0000101}
102
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000103struct TestPPCallbacks : public PPCallbacks {
104 TestPPCallbacks() : SeenEnd(false) {}
105
106 void EndOfMainFile() override { SeenEnd = true; }
107
108 bool SeenEnd;
109};
110
111class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
112 TestPPCallbacks *Callbacks;
113
114public:
115 TestPPCallbacksFrontendAction(TestPPCallbacks *C)
116 : Callbacks(C), SeenEnd(false) {}
117
118 void ExecuteAction() override {
119 Preprocessor &PP = getCompilerInstance().getPreprocessor();
120 PP.addPPCallbacks(Callbacks);
121 PP.EnterMainSourceFile();
122 }
123 void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
124
125 bool SeenEnd;
126};
127
128TEST(PreprocessorFrontendAction, EndSourceFile) {
129 CompilerInvocation *Invocation = new CompilerInvocation;
130 Invocation->getPreprocessorOpts().addRemappedFile(
131 "test.cc", MemoryBuffer::getMemBuffer("int main() { float x; }"));
132 Invocation->getFrontendOpts().Inputs.push_back(
133 FrontendInputFile("test.cc", IK_CXX));
134 Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
135 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
136 CompilerInstance Compiler;
137 Compiler.setInvocation(Invocation);
138 Compiler.createDiagnostics();
139
140 TestPPCallbacks *Callbacks = new TestPPCallbacks;
141 TestPPCallbacksFrontendAction TestAction(Callbacks);
142 ASSERT_FALSE(Callbacks->SeenEnd);
143 ASSERT_FALSE(TestAction.SeenEnd);
144 ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
145 // Check that EndOfMainFile was called before EndSourceFileAction.
146 ASSERT_TRUE(TestAction.SeenEnd);
147}
148
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +0000149} // anonymous namespace