blob: 97dacfb440bd039da61998fbbb274e7753bce251 [file] [log] [blame]
Jeffrey Yasskinc7da9932011-02-03 04:51:521//===- unittests/Frontend/FrontendActionTest.cpp - FrontendAction tests ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jeffrey Yasskinc7da9932011-02-03 04:51:526//
7//===----------------------------------------------------------------------===//
8
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:049#include "clang/Frontend/FrontendAction.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:5210#include "clang/AST/ASTConsumer.h"
Chandler Carruth320d9662012-12-04 09:45:3411#include "clang/AST/ASTContext.h"
Chandler Carruthfa0b3bb2012-12-04 09:53:3712#include "clang/AST/RecursiveASTVisitor.h"
Rainer Orth09d890d2019-08-05 13:59:2613#include "clang/Basic/LangStandard.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:5214#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/CompilerInvocation.h"
Benjamin Kramer7de99692016-11-16 18:15:2616#include "clang/Frontend/FrontendActions.h"
Argyrios Kyrtzidis336fcd92013-11-24 02:12:1817#include "clang/Lex/Preprocessor.h"
Mehdi Amini9670f842016-07-18 19:02:1118#include "clang/Lex/PreprocessorOptions.h"
Reid Kleckner89bd8d62014-10-22 17:50:1919#include "clang/Sema/Sema.h"
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:0420#include "clang/Serialization/InMemoryModuleCache.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:5221#include "llvm/ADT/Triple.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:5222#include "llvm/Support/MemoryBuffer.h"
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:0423#include "llvm/Support/ToolOutputFile.h"
Jeffrey Yasskinc7da9932011-02-03 04:51:5224#include "gtest/gtest.h"
25
26using namespace llvm;
27using namespace clang;
28
29namespace {
30
31class TestASTFrontendAction : public ASTFrontendAction {
32public:
Reid Kleckner89bd8d62014-10-22 17:50:1933 TestASTFrontendAction(bool enableIncrementalProcessing = false,
34 bool actOnEndOfTranslationUnit = false)
35 : EnableIncrementalProcessing(enableIncrementalProcessing),
36 ActOnEndOfTranslationUnit(actOnEndOfTranslationUnit) { }
Argyrios Kyrtzidis336fcd92013-11-24 02:12:1837
38 bool EnableIncrementalProcessing;
Reid Kleckner89bd8d62014-10-22 17:50:1939 bool ActOnEndOfTranslationUnit;
Jeffrey Yasskinc7da9932011-02-03 04:51:5240 std::vector<std::string> decl_names;
41
Richard Smithd9259c22017-06-09 01:36:1042 bool BeginSourceFileAction(CompilerInstance &ci) override {
Argyrios Kyrtzidis336fcd92013-11-24 02:12:1843 if (EnableIncrementalProcessing)
44 ci.getPreprocessor().enableIncrementalProcessing();
45
Richard Smithd9259c22017-06-09 01:36:1046 return ASTFrontendAction::BeginSourceFileAction(ci);
Argyrios Kyrtzidis336fcd92013-11-24 02:12:1847 }
48
Alexander Kornienko34eb2072015-04-11 02:00:2349 std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
50 StringRef InFile) override {
Jonas Devlieghere2b3d49b2019-08-14 23:04:1851 return std::make_unique<Visitor>(CI, ActOnEndOfTranslationUnit,
Reid Kleckner89bd8d62014-10-22 17:50:1952 decl_names);
Jeffrey Yasskinc7da9932011-02-03 04:51:5253 }
54
55private:
56 class Visitor : public ASTConsumer, public RecursiveASTVisitor<Visitor> {
57 public:
Reid Kleckner89bd8d62014-10-22 17:50:1958 Visitor(CompilerInstance &CI, bool ActOnEndOfTranslationUnit,
59 std::vector<std::string> &decl_names) :
60 CI(CI), ActOnEndOfTranslationUnit(ActOnEndOfTranslationUnit),
61 decl_names_(decl_names) {}
Jeffrey Yasskinc7da9932011-02-03 04:51:5262
Alexander Kornienko34eb2072015-04-11 02:00:2363 void HandleTranslationUnit(ASTContext &context) override {
Reid Kleckner89bd8d62014-10-22 17:50:1964 if (ActOnEndOfTranslationUnit) {
65 CI.getSema().ActOnEndOfTranslationUnit();
66 }
Jeffrey Yasskinc7da9932011-02-03 04:51:5267 TraverseDecl(context.getTranslationUnitDecl());
68 }
69
70 virtual bool VisitNamedDecl(NamedDecl *Decl) {
71 decl_names_.push_back(Decl->getQualifiedNameAsString());
72 return true;
73 }
74
75 private:
Reid Kleckner89bd8d62014-10-22 17:50:1976 CompilerInstance &CI;
77 bool ActOnEndOfTranslationUnit;
Jeffrey Yasskinc7da9932011-02-03 04:51:5278 std::vector<std::string> &decl_names_;
79 };
80};
81
82TEST(ASTFrontendAction, Sanity) {
David Blaikieea4395e2017-01-06 19:49:0183 auto invocation = std::make_shared<CompilerInvocation>();
Jeffrey Yasskinc7da9932011-02-03 04:51:5284 invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:2985 "test.cc",
86 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Richard Smith40c0efa2017-04-26 18:57:4087 invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:2688 FrontendInputFile("test.cc", Language::CXX));
Jeffrey Yasskinc7da9932011-02-03 04:51:5289 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
90 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
91 CompilerInstance compiler;
David Blaikieea4395e2017-01-06 19:49:0192 compiler.setInvocation(std::move(invocation));
Sean Silvaf1b49e22013-01-20 01:58:2893 compiler.createDiagnostics();
Jeffrey Yasskinc7da9932011-02-03 04:51:5294
95 TestASTFrontendAction test_action;
96 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Alp Toker56b5cc92013-12-15 10:36:2697 ASSERT_EQ(2U, test_action.decl_names.size());
98 EXPECT_EQ("main", test_action.decl_names[0]);
99 EXPECT_EQ("x", test_action.decl_names[1]);
Jeffrey Yasskinc7da9932011-02-03 04:51:52100}
101
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18102TEST(ASTFrontendAction, IncrementalParsing) {
David Blaikieea4395e2017-01-06 19:49:01103 auto invocation = std::make_shared<CompilerInvocation>();
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18104 invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29105 "test.cc",
106 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Richard Smith40c0efa2017-04-26 18:57:40107 invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:26108 FrontendInputFile("test.cc", Language::CXX));
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18109 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
110 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
111 CompilerInstance compiler;
David Blaikieea4395e2017-01-06 19:49:01112 compiler.setInvocation(std::move(invocation));
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18113 compiler.createDiagnostics();
114
115 TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true);
116 ASSERT_TRUE(compiler.ExecuteAction(test_action));
Alp Toker56b5cc92013-12-15 10:36:26117 ASSERT_EQ(2U, test_action.decl_names.size());
118 EXPECT_EQ("main", test_action.decl_names[0]);
119 EXPECT_EQ("x", test_action.decl_names[1]);
Argyrios Kyrtzidis336fcd92013-11-24 02:12:18120}
121
Reid Kleckner89bd8d62014-10-22 17:50:19122TEST(ASTFrontendAction, LateTemplateIncrementalParsing) {
David Blaikieea4395e2017-01-06 19:49:01123 auto invocation = std::make_shared<CompilerInvocation>();
Reid Kleckner89bd8d62014-10-22 17:50:19124 invocation->getLangOpts()->CPlusPlus = true;
125 invocation->getLangOpts()->DelayedTemplateParsing = true;
126 invocation->getPreprocessorOpts().addRemappedFile(
127 "test.cc", MemoryBuffer::getMemBuffer(
128 "template<typename T> struct A { A(T); T data; };\n"
129 "template<typename T> struct B: public A<T> {\n"
130 " B();\n"
131 " B(B const& b): A<T>(b.data) {}\n"
132 "};\n"
133 "B<char> c() { return B<char>(); }\n").release());
Richard Smith40c0efa2017-04-26 18:57:40134 invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:26135 FrontendInputFile("test.cc", Language::CXX));
Reid Kleckner89bd8d62014-10-22 17:50:19136 invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
137 invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
138 CompilerInstance compiler;
David Blaikieea4395e2017-01-06 19:49:01139 compiler.setInvocation(std::move(invocation));
Reid Kleckner89bd8d62014-10-22 17:50:19140 compiler.createDiagnostics();
141
142 TestASTFrontendAction test_action(/*enableIncrementalProcessing=*/true,
143 /*actOnEndOfTranslationUnit=*/true);
144 ASSERT_TRUE(compiler.ExecuteAction(test_action));
145 ASSERT_EQ(13U, test_action.decl_names.size());
146 EXPECT_EQ("A", test_action.decl_names[0]);
147 EXPECT_EQ("c", test_action.decl_names[12]);
148}
149
Benjamin Kramer88d99e42014-08-07 20:51:16150struct TestPPCallbacks : public PPCallbacks {
151 TestPPCallbacks() : SeenEnd(false) {}
152
153 void EndOfMainFile() override { SeenEnd = true; }
154
155 bool SeenEnd;
156};
157
158class TestPPCallbacksFrontendAction : public PreprocessorFrontendAction {
Benjamin Kramera2406fa2014-09-10 09:35:49159 TestPPCallbacks *Callbacks;
Benjamin Kramer88d99e42014-08-07 20:51:16160
161public:
Benjamin Kramera2406fa2014-09-10 09:35:49162 TestPPCallbacksFrontendAction(TestPPCallbacks *C)
163 : Callbacks(C), SeenEnd(false) {}
Benjamin Kramer88d99e42014-08-07 20:51:16164
165 void ExecuteAction() override {
166 Preprocessor &PP = getCompilerInstance().getPreprocessor();
Benjamin Kramera2406fa2014-09-10 09:35:49167 PP.addPPCallbacks(std::unique_ptr<TestPPCallbacks>(Callbacks));
Benjamin Kramer88d99e42014-08-07 20:51:16168 PP.EnterMainSourceFile();
169 }
170 void EndSourceFileAction() override { SeenEnd = Callbacks->SeenEnd; }
171
172 bool SeenEnd;
173};
174
175TEST(PreprocessorFrontendAction, EndSourceFile) {
David Blaikieea4395e2017-01-06 19:49:01176 auto Invocation = std::make_shared<CompilerInvocation>();
Benjamin Kramer88d99e42014-08-07 20:51:16177 Invocation->getPreprocessorOpts().addRemappedFile(
Rafael Espindolad87f8d72014-08-27 20:03:29178 "test.cc",
179 MemoryBuffer::getMemBuffer("int main() { float x; }").release());
Benjamin Kramer88d99e42014-08-07 20:51:16180 Invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:26181 FrontendInputFile("test.cc", Language::CXX));
Benjamin Kramer88d99e42014-08-07 20:51:16182 Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
183 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
184 CompilerInstance Compiler;
David Blaikieea4395e2017-01-06 19:49:01185 Compiler.setInvocation(std::move(Invocation));
Benjamin Kramer88d99e42014-08-07 20:51:16186 Compiler.createDiagnostics();
187
Benjamin Kramera2406fa2014-09-10 09:35:49188 TestPPCallbacks *Callbacks = new TestPPCallbacks;
189 TestPPCallbacksFrontendAction TestAction(Callbacks);
Benjamin Kramer88d99e42014-08-07 20:51:16190 ASSERT_FALSE(Callbacks->SeenEnd);
191 ASSERT_FALSE(TestAction.SeenEnd);
192 ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
193 // Check that EndOfMainFile was called before EndSourceFileAction.
194 ASSERT_TRUE(TestAction.SeenEnd);
195}
196
Benjamin Kramer7de99692016-11-16 18:15:26197class TypoExternalSemaSource : public ExternalSemaSource {
198 CompilerInstance &CI;
199
200public:
201 TypoExternalSemaSource(CompilerInstance &CI) : CI(CI) {}
202
203 TypoCorrection CorrectTypo(const DeclarationNameInfo &Typo, int LookupKind,
204 Scope *S, CXXScopeSpec *SS,
205 CorrectionCandidateCallback &CCC,
206 DeclContext *MemberContext, bool EnteringContext,
207 const ObjCObjectPointerType *OPT) override {
208 // Generate a fake typo correction with one attached note.
209 ASTContext &Ctx = CI.getASTContext();
210 TypoCorrection TC(DeclarationName(&Ctx.Idents.get("moo")));
211 unsigned DiagID = Ctx.getDiagnostics().getCustomDiagID(
212 DiagnosticsEngine::Note, "This is a note");
213 TC.addExtraDiagnostic(PartialDiagnostic(DiagID, Ctx.getDiagAllocator()));
214 return TC;
215 }
216};
217
218struct TypoDiagnosticConsumer : public DiagnosticConsumer {
219 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
220 const Diagnostic &Info) override {
221 // Capture errors and notes. There should be one of each.
222 if (DiagLevel == DiagnosticsEngine::Error) {
223 assert(Error.empty());
224 Info.FormatDiagnostic(Error);
225 } else {
226 assert(Note.empty());
227 Info.FormatDiagnostic(Note);
228 }
229 }
230 SmallString<32> Error;
231 SmallString<32> Note;
232};
233
234TEST(ASTFrontendAction, ExternalSemaSource) {
David Blaikieea4395e2017-01-06 19:49:01235 auto Invocation = std::make_shared<CompilerInvocation>();
Benjamin Kramer7de99692016-11-16 18:15:26236 Invocation->getLangOpts()->CPlusPlus = true;
237 Invocation->getPreprocessorOpts().addRemappedFile(
238 "test.cc", MemoryBuffer::getMemBuffer("void fooo();\n"
239 "int main() { foo(); }")
240 .release());
241 Invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:26242 FrontendInputFile("test.cc", Language::CXX));
Benjamin Kramer7de99692016-11-16 18:15:26243 Invocation->getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
244 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
245 CompilerInstance Compiler;
David Blaikieea4395e2017-01-06 19:49:01246 Compiler.setInvocation(std::move(Invocation));
Benjamin Kramer7de99692016-11-16 18:15:26247 auto *TDC = new TypoDiagnosticConsumer;
248 Compiler.createDiagnostics(TDC, /*ShouldOwnClient=*/true);
249 Compiler.setExternalSemaSource(new TypoExternalSemaSource(Compiler));
250
251 SyntaxOnlyAction TestAction;
252 ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
253 // There should be one error correcting to 'moo' and a note attached to it.
254 EXPECT_EQ("use of undeclared identifier 'foo'; did you mean 'moo'?",
Jonas Devlieghere509e21a2020-01-30 05:27:46255 std::string(TDC->Error));
256 EXPECT_EQ("This is a note", std::string(TDC->Note));
Benjamin Kramer7de99692016-11-16 18:15:26257}
258
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04259TEST(GeneratePCHFrontendAction, CacheGeneratedPCH) {
260 // Create a temporary file for writing out the PCH that will be cleaned up.
261 int PCHFD;
262 llvm::SmallString<128> PCHFilename;
263 ASSERT_FALSE(
264 llvm::sys::fs::createTemporaryFile("test.h", "pch", PCHFD, PCHFilename));
265 llvm::ToolOutputFile PCHFile(PCHFilename, PCHFD);
266
267 for (bool ShouldCache : {false, true}) {
268 auto Invocation = std::make_shared<CompilerInvocation>();
269 Invocation->getLangOpts()->CacheGeneratedPCH = ShouldCache;
270 Invocation->getPreprocessorOpts().addRemappedFile(
271 "test.h",
272 MemoryBuffer::getMemBuffer("int foo(void) { return 1; }\n").release());
273 Invocation->getFrontendOpts().Inputs.push_back(
Rainer Orth09d890d2019-08-05 13:59:26274 FrontendInputFile("test.h", Language::C));
Benjamin Krameradcd0262020-01-28 19:23:46275 Invocation->getFrontendOpts().OutputFile =
276 std::string(StringRef(PCHFilename));
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04277 Invocation->getFrontendOpts().ProgramAction = frontend::GeneratePCH;
278 Invocation->getTargetOpts().Triple = "x86_64-apple-darwin19.0.0";
279 CompilerInstance Compiler;
280 Compiler.setInvocation(std::move(Invocation));
281 Compiler.createDiagnostics();
282
283 GeneratePCHAction TestAction;
284 ASSERT_TRUE(Compiler.ExecuteAction(TestAction));
285
286 // Check whether the PCH was cached.
287 if (ShouldCache)
Volodymyr Sapsai83f4c3a2020-01-17 01:12:41288 EXPECT_TRUE(Compiler.getModuleCache().isPCMFinal(PCHFilename));
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04289 else
Volodymyr Sapsai83f4c3a2020-01-17 01:12:41290 EXPECT_EQ(nullptr, Compiler.getModuleCache().lookupPCM(PCHFilename));
Duncan P. N. Exon Smith70d759b2019-03-12 18:38:04291 }
292}
293
Jeffrey Yasskinc7da9932011-02-03 04:51:52294} // anonymous namespace