blob: bdd22bd1810f0e46a191de4773361022fbe43420 [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 Blaikie6beb6aa2014-08-10 19:56:51 +000041 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
42 StringRef InFile) {
43 return llvm::make_unique<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(
Rafael Espindolad87f8d72014-08-27 20:03:29 +000068 "test.cc",
69 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Douglas Gregord6a58282012-01-20 16:33:00 +000070 invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
71 IK_CXX));
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000072 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
73 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
74 CompilerInstance compiler;
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000075 compiler.setInvocation(invocation);
Sean Silvaf1b49e22013-01-20 01:58:28 +000076 compiler.createDiagnostics();
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000077
78 TestASTFrontendAction test_action;
79 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Alp Toker56b5cc92013-12-15 10:36:26 +000080 ASSERT_EQ(2U, test_action.decl_names.size());
81 EXPECT_EQ("main", test_action.decl_names[0]);
82 EXPECT_EQ("x", test_action.decl_names[1]);
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +000083}
84
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000085TEST(ASTFrontendAction, IncrementalParsing) {
86 CompilerInvocation *invocation = new CompilerInvocation;
87 invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29 +000088 "test.cc",
89 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +000090 invocation->getFrontendOpts().Inputs.push_back(FrontendInputFile("test.cc",
91 IK_CXX));
92 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
93 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
94 CompilerInstance compiler;
95 compiler.setInvocation(invocation);
96 compiler.createDiagnostics();
97
98 TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
99 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Alp Toker56b5cc92013-12-15 10:36:26 +0000100 ASSERT_EQ(2U, test_action.decl_names.size());
101 EXPECT_EQ("main", test_action.decl_names[0]);
102 EXPECT_EQ("x", test_action.decl_names[1]);
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18 +0000103}
104
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000105struct TestPPCallbacks : public PPCallbacks {
106 TestPPCallbacks() : SeenEnd(false) {}
107
108 void EndOfMainFile() override { SeenEnd = true; }
109
110 bool SeenEnd;
111};
112
113class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
Craig Topperb8a70532014-09-10 04:53:53 +0000114 std::unique_ptr<TestPPCallbacks> Callbacks;
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000115
116public:
Craig Topperb8a70532014-09-10 04:53:53 +0000117 TestPPCallbacksFrontendAction(std::unique_ptr<TestPPCallbacks> C)
118 : Callbacks(std::move(C)), SeenEnd(false) {}
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000119
120 void ExecuteAction() override {
121 Preprocessor &PP = getCompilerInstance().getPreprocessor();
Craig Topperb8a70532014-09-10 04:53:53 +0000122 PP.addPPCallbacks(std::move(Callbacks));
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000123 PP.EnterMainSourceFile();
124 }
125 void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
126
127 bool SeenEnd;
128};
129
130TEST(PreprocessorFrontendAction, EndSourceFile) {
131 CompilerInvocation *Invocation = new CompilerInvocation;
132 Invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000133 "test.cc",
134 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000135 Invocation->getFrontendOpts().Inputs.push_back(
136 FrontendInputFile("test.cc", IK_CXX));
137 Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
138 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
139 CompilerInstance Compiler;
140 Compiler.setInvocation(Invocation);
141 Compiler.createDiagnostics();
142
Craig Topperb8a70532014-09-10 04:53:53 +0000143 std::unique_ptr<TestPPCallbacks> Callbacks(new TestPPCallbacks);
144 TestPPCallbacksFrontendAction TestAction(std::move(Callbacks));
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000145 ASSERT_FALSE(Callbacks->SeenEnd);
146 ASSERT_FALSE(TestAction.SeenEnd);
147 ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
148 // Check that EndOfMainFile was called before EndSourceFileAction.
149 ASSERT_TRUE(TestAction.SeenEnd);
150}
151
Jeffrey Yasskinc7da9932011-02-03 04:51:52 +0000152} // anonymous namespace