blob: d514d406d8b61beeebf3c39e095de578434b6cf8 [file] [log] [blame]
Daniel Dunbara0ff58d2009-11-14 10:42:35 +00001//===--- FrontendAction.cpp -----------------------------------------------===//
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
10#include "clang/Frontend/FrontendAction.h"
Sebastian Redl07a89a82010-07-30 00:29:29 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000012#include "clang/AST/ASTContext.h"
Nico Weber2992efa2011-01-25 20:34:14 +000013#include "clang/AST/DeclGroup.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber2992efa2011-01-25 20:34:14 +000017#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000018#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber2992efa2011-01-25 20:34:14 +000019#include "clang/Frontend/MultiplexConsumer.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000020#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000024#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turner0248f572011-08-05 22:17:03 +000025#include "clang/Serialization/ASTReader.h"
Douglas Gregor5e306b12013-01-23 22:38:11 +000026#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000027#include "llvm/Support/ErrorHandling.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/Timer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000031#include "llvm/Support/raw_ostream.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000032#include <system_error>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000033using namespace clang;
34
NAKAMURA Takumiad4c06c2014-07-11 15:06:24 +000035template class llvm::Registry<clang::PluginASTAction>;
36
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000037namespace {
38
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000039class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000040 ASTDeserializationListener *Previous;
Nico Weber824285e2014-05-08 04:26:47 +000041 bool DeletePrevious;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000042
43public:
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000044 explicit DelegatingDeserializationListener(
Nico Weber824285e2014-05-08 04:26:47 +000045 ASTDeserializationListener *Previous, bool DeletePrevious)
46 : Previous(Previous), DeletePrevious(DeletePrevious) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000047 ~DelegatingDeserializationListener() override {
Nico Weber824285e2014-05-08 04:26:47 +000048 if (DeletePrevious)
49 delete Previous;
50 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000051
Craig Topperafa7cb32014-03-13 06:07:04 +000052 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000053 if (Previous)
54 Previous->ReaderInitialized(Reader);
55 }
Craig Topperafa7cb32014-03-13 06:07:04 +000056 void IdentifierRead(serialization::IdentID ID,
57 IdentifierInfo *II) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000058 if (Previous)
59 Previous->IdentifierRead(ID, II);
60 }
Craig Topperafa7cb32014-03-13 06:07:04 +000061 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000062 if (Previous)
63 Previous->TypeRead(Idx, T);
64 }
Craig Topperafa7cb32014-03-13 06:07:04 +000065 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000066 if (Previous)
67 Previous->DeclRead(ID, D);
68 }
Craig Topperafa7cb32014-03-13 06:07:04 +000069 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000070 if (Previous)
71 Previous->SelectorRead(ID, Sel);
72 }
Craig Topperafa7cb32014-03-13 06:07:04 +000073 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
Richard Smith66a81862015-05-04 02:25:31 +000074 MacroDefinitionRecord *MD) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000075 if (Previous)
76 Previous->MacroDefinitionRead(PPID, MD);
77 }
78};
79
80/// \brief Dumps deserialized declarations.
81class DeserializedDeclsDumper : public DelegatingDeserializationListener {
82public:
Nico Weber824285e2014-05-08 04:26:47 +000083 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
84 bool DeletePrevious)
85 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000086
Craig Topperafa7cb32014-03-13 06:07:04 +000087 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000088 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
89 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramerdb0fc512012-02-07 11:57:57 +000090 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000091 llvm::outs() << "\n";
92
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000093 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000094 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000095};
96
David Blaikie48b81282012-05-29 17:05:42 +000097/// \brief Checks deserialized declarations and emits error if a name
98/// matches one given in command-line using -error-on-deserialized-decl.
99class DeserializedDeclsChecker : public DelegatingDeserializationListener {
100 ASTContext &Ctx;
101 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000102
David Blaikie48b81282012-05-29 17:05:42 +0000103public:
104 DeserializedDeclsChecker(ASTContext &Ctx,
105 const std::set<std::string> &NamesToCheck,
Nico Weber824285e2014-05-08 04:26:47 +0000106 ASTDeserializationListener *Previous,
107 bool DeletePrevious)
108 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
109 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000110
Craig Topperafa7cb32014-03-13 06:07:04 +0000111 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikie48b81282012-05-29 17:05:42 +0000112 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
113 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
114 unsigned DiagID
115 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
116 "%0 was deserialized");
117 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
118 << ND->getNameAsString();
119 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000120
David Blaikie48b81282012-05-29 17:05:42 +0000121 DelegatingDeserializationListener::DeclRead(ID, D);
122 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000123};
124
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000125} // end anonymous namespace
126
Craig Topper49a27902014-05-22 04:46:25 +0000127FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000128
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000129FrontendAction::~FrontendAction() {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000130
Douglas Gregor32fbe312012-01-20 16:28:04 +0000131void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
David Blaikief62d4e72014-08-10 17:03:42 +0000132 std::unique_ptr<ASTUnit> AST) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000133 this->CurrentInput = CurrentInput;
David Blaikief62d4e72014-08-10 17:03:42 +0000134 CurrentASTUnit = std::move(AST);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000135}
136
David Blaikie6beb6aa2014-08-10 19:56:51 +0000137std::unique_ptr<ASTConsumer>
138FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
139 StringRef InFile) {
140 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
Nico Weber2992efa2011-01-25 20:34:14 +0000141 if (!Consumer)
Craig Topper49a27902014-05-22 04:46:25 +0000142 return nullptr;
Nico Weber2992efa2011-01-25 20:34:14 +0000143
John Brawn6c789742016-03-15 12:51:40 +0000144 // If there are no registered plugins we don't need to wrap the consumer
145 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
Nico Weber2992efa2011-01-25 20:34:14 +0000146 return Consumer;
147
John Brawn6c789742016-03-15 12:51:40 +0000148 // Collect the list of plugins that go before the main action (in Consumers)
149 // or after it (in AfterConsumers)
David Blaikie6beb6aa2014-08-10 19:56:51 +0000150 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
John Brawn6c789742016-03-15 12:51:40 +0000151 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
152 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
153 ie = FrontendPluginRegistry::end();
154 it != ie; ++it) {
155 std::unique_ptr<PluginASTAction> P = it->instantiate();
156 PluginASTAction::ActionType ActionType = P->getActionType();
157 if (ActionType == PluginASTAction::Cmdline) {
158 // This is O(|plugins| * |add_plugins|), but since both numbers are
159 // way below 50 in practice, that's ok.
160 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
161 i != e; ++i) {
162 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
163 ActionType = PluginASTAction::AddAfterMainAction;
164 break;
165 }
166 }
Nico Weber2992efa2011-01-25 20:34:14 +0000167 }
John Brawn6c789742016-03-15 12:51:40 +0000168 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
169 ActionType == PluginASTAction::AddAfterMainAction) &&
170 P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
171 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
172 if (ActionType == PluginASTAction::AddBeforeMainAction) {
173 Consumers.push_back(std::move(PluginConsumer));
174 } else {
175 AfterConsumers.push_back(std::move(PluginConsumer));
176 }
177 }
178 }
179
180 // Add to Consumers the main consumer, then all the plugins that go after it
181 Consumers.push_back(std::move(Consumer));
182 for (auto &C : AfterConsumers) {
183 Consumers.push_back(std::move(C));
Nico Weber2992efa2011-01-25 20:34:14 +0000184 }
185
David Blaikie6beb6aa2014-08-10 19:56:51 +0000186 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Nico Weber2992efa2011-01-25 20:34:14 +0000187}
188
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000189bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000190 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000191 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000192 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000193 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000194 setCompilerInstance(&CI);
195
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000196 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000197 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000198 if (!BeginInvocation(CI))
199 goto failure;
200
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000201 // AST files follow a very different path, since they share objects via the
202 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000203 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000204 assert(!usesPreprocessorOnly() &&
205 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000206 assert(hasASTFileSupport() &&
207 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000208
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000209 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000210
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000211 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
212 InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(),
213 CI.getCodeGenOpts().DebugTypeExtRefs);
David Blaikief62d4e72014-08-10 17:03:42 +0000214
Daniel Dunbar59203002009-12-03 01:45:44 +0000215 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000216 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000217
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000218 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000219 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000220 HasBegunSourceFile = true;
221
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000222 // Set the shared objects, these are reset when we finish processing the
223 // file, otherwise the CompilerInstance will happily destroy them.
224 CI.setFileManager(&AST->getFileManager());
225 CI.setSourceManager(&AST->getSourceManager());
226 CI.setPreprocessor(&AST->getPreprocessor());
227 CI.setASTContext(&AST->getASTContext());
228
David Blaikief62d4e72014-08-10 17:03:42 +0000229 setCurrentInput(Input, std::move(AST));
230
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000231 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000232 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000233 goto failure;
234
James Dennettf8317672013-01-23 00:45:44 +0000235 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000236 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000237 if (!CI.hasASTConsumer())
238 goto failure;
239
240 return true;
241 }
242
Ben Langmuir8832c062014-04-15 18:16:25 +0000243 if (!CI.hasVirtualFileSystem()) {
244 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
245 createVFSFromCompilerInvocation(CI.getInvocation(),
246 CI.getDiagnostics()))
247 CI.setVirtualFileSystem(VFS);
248 else
249 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000250 }
251
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000252 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000253 if (!CI.hasFileManager())
254 CI.createFileManager();
255 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000256 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000257
258 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000259 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000260 assert(hasIRSupport() &&
261 "This action does not have IR file support!");
262
263 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000264 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseea762b02012-08-10 01:06:08 +0000265 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000266
267 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000268 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000269 goto failure;
270
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000271 // Initialize the main file entry.
272 if (!CI.InitializeSourceManager(CurrentInput))
273 goto failure;
274
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000275 return true;
276 }
277
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000278 // If the implicit PCH include is actually a directory, rather than
279 // a single file, search for a suitable PCH file in that directory.
280 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
281 FileManager &FileMgr = CI.getFileManager();
282 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
283 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000284 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000285 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Rafael Espindolac0809172014-06-12 14:02:15 +0000286 std::error_code EC;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000287 SmallString<128> DirNative;
288 llvm::sys::path::native(PCHDir->getName(), DirNative);
289 bool Found = false;
Yaron Keren92e1b622015-03-18 10:17:07 +0000290 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000291 Dir != DirEnd && !EC; Dir.increment(EC)) {
292 // Check whether this is an acceptable AST file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000293 if (ASTReader::isAcceptableASTFile(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000294 Dir->path(), FileMgr, CI.getPCHContainerReader(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000295 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
296 SpecificModuleCachePath)) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000297 PPOpts.ImplicitPCHInclude = Dir->path();
298 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000299 break;
300 }
301 }
302
303 if (!Found) {
304 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
Richard Smith81c72482015-09-04 21:44:32 +0000305 goto failure;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000306 }
307 }
308 }
309
Ted Kremenekeeccb302014-08-27 15:14:15 +0000310 // Set up the preprocessor if needed. When parsing model files the
311 // preprocessor of the original source is reused.
312 if (!isModelParsingAction())
313 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000314
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000315 // Inform the diagnostic client we are processing a source file.
316 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
317 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000318 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000319
320 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000321 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000322 goto failure;
323
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000324 // Initialize the main file entry. It is important that this occurs after
325 // BeginSourceFileAction, which may change CurrentInput during module builds.
326 if (!CI.InitializeSourceManager(CurrentInput))
327 goto failure;
328
James Dennettf8317672013-01-23 00:45:44 +0000329 // Create the AST context and consumer unless this is a preprocessor only
330 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000331 if (!usesPreprocessorOnly()) {
Ted Kremenekeeccb302014-08-27 15:14:15 +0000332 // Parsing a model file should reuse the existing ASTContext.
333 if (!isModelParsingAction())
334 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000335
David Blaikie6beb6aa2014-08-10 19:56:51 +0000336 std::unique_ptr<ASTConsumer> Consumer =
337 CreateWrappedASTConsumer(CI, InputFile);
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000338 if (!Consumer)
339 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000340
Ted Kremenekeeccb302014-08-27 15:14:15 +0000341 // FIXME: should not overwrite ASTMutationListener when parsing model files?
342 if (!isModelParsingAction())
343 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Richard Smithe842a472014-10-22 02:05:46 +0000344
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000345 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
346 // Convert headers to PCH and chain them.
Alp Toker9e0523d2014-07-07 11:07:10 +0000347 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
348 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000349 if (!source)
350 goto failure;
Alp Toker9e0523d2014-07-07 11:07:10 +0000351 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000352 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000353 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
354 // Use PCH.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000355 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregor925296b2011-07-19 16:10:42 +0000356 ASTDeserializationListener *DeserialListener =
357 Consumer->GetASTDeserializationListener();
Nico Weber824285e2014-05-08 04:26:47 +0000358 bool DeleteDeserialListener = false;
359 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
360 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
361 DeleteDeserialListener);
362 DeleteDeserialListener = true;
363 }
364 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
365 DeserialListener = new DeserializedDeclsChecker(
366 CI.getASTContext(),
367 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
368 DeserialListener, DeleteDeserialListener);
369 DeleteDeserialListener = true;
370 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000371 CI.createPCHExternalASTSource(
Nico Weber824285e2014-05-08 04:26:47 +0000372 CI.getPreprocessorOpts().ImplicitPCHInclude,
373 CI.getPreprocessorOpts().DisablePCHValidation,
374 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
375 DeleteDeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000376 if (!CI.getASTContext().getExternalSource())
377 goto failure;
378 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000379
David Blaikie6beb6aa2014-08-10 19:56:51 +0000380 CI.setASTConsumer(std::move(Consumer));
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000381 if (!CI.hasASTConsumer())
382 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000383 }
384
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000385 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000386 // source.
387 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
388 Preprocessor &PP = CI.getPreprocessor();
Richard Smith053f6c62014-05-16 23:01:30 +0000389
390 // If modules are enabled, create the module manager before creating
391 // any builtins, so that all declarations know that they might be
392 // extended by an external source.
393 if (CI.getLangOpts().Modules)
394 CI.createModuleManager();
395
Eric Christopher02d5d862015-08-06 01:01:12 +0000396 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000397 PP.getLangOpts());
Richard Smith053f6c62014-05-16 23:01:30 +0000398 } else {
399 // FIXME: If this is a problem, recover from it by creating a multiplex
400 // source.
401 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
402 "modules enabled but created an external source that "
403 "doesn't support modules");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000404 }
405
Richard Smithac425e92015-01-23 00:01:13 +0000406 // If we were asked to load any module map files, do so now.
407 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
408 if (auto *File = CI.getFileManager().getFile(Filename))
409 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
410 File, /*IsSystem*/false);
411 else
412 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
413 }
414
Richard Smithd4b230b2014-10-27 23:01:16 +0000415 // If we were asked to load any module files, do so now.
416 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
417 if (!CI.loadModuleFile(ModuleFile))
Richard Smithe842a472014-10-22 02:05:46 +0000418 goto failure;
Richard Smithe842a472014-10-22 02:05:46 +0000419
Douglas Gregore9fc3772012-01-26 07:55:45 +0000420 // If there is a layout overrides file, attach an external AST source that
421 // provides the layouts from that file.
422 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
423 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000424 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000425 Override(new LayoutOverrideSource(
426 CI.getFrontendOpts().OverrideRecordLayoutsFile));
427 CI.getASTContext().setExternalSource(Override);
428 }
Richard Smith053f6c62014-05-16 23:01:30 +0000429
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000430 return true;
431
432 // If we failed, reset state since the client will not end up calling the
433 // matching EndSourceFile().
434 failure:
435 if (isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000436 CI.setASTContext(nullptr);
437 CI.setPreprocessor(nullptr);
438 CI.setSourceManager(nullptr);
439 CI.setFileManager(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000440 }
441
Jordan Roseea762b02012-08-10 01:06:08 +0000442 if (HasBegunSourceFile)
443 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000444 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000445 setCurrentInput(FrontendInputFile());
Craig Topper49a27902014-05-22 04:46:25 +0000446 setCompilerInstance(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000447 return false;
448}
449
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000450bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000451 CompilerInstance &CI = getCompilerInstance();
452
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000453 if (CI.hasFrontendTimer()) {
454 llvm::TimeRegion Timer(CI.getFrontendTimer());
455 ExecuteAction();
456 }
457 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000458
Douglas Gregor5e306b12013-01-23 22:38:11 +0000459 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000460 // there were any module-build failures.
461 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
462 CI.hasPreprocessor()) {
Richard Smith3938f0c2015-08-15 00:34:15 +0000463 StringRef Cache =
464 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
465 if (!Cache.empty())
466 GlobalModuleIndex::writeIndex(CI.getFileManager(),
467 CI.getPCHContainerReader(), Cache);
Douglas Gregor5e306b12013-01-23 22:38:11 +0000468 }
469
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000470 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000471}
472
473void FrontendAction::EndSourceFile() {
474 CompilerInstance &CI = getCompilerInstance();
475
Douglas Gregor1388a892011-02-09 18:47:31 +0000476 // Inform the diagnostic client we are done with this source file.
477 CI.getDiagnosticClient().EndSourceFile();
478
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000479 // Inform the preprocessor we are done.
480 if (CI.hasPreprocessor())
481 CI.getPreprocessor().EndSourceFile();
482
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000483 // Finalize the action.
484 EndSourceFileAction();
485
Nico Weber7de358e2014-04-24 02:42:04 +0000486 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000487 //
488 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000489 bool DisableFree = CI.getFrontendOpts().DisableFree;
490 if (DisableFree) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000491 CI.resetAndLeakSema();
492 CI.resetAndLeakASTContext();
David Blaikie6beb6aa2014-08-10 19:56:51 +0000493 BuryPointer(CI.takeASTConsumer().get());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000494 } else {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000495 CI.setSema(nullptr);
496 CI.setASTContext(nullptr);
Craig Topper49a27902014-05-22 04:46:25 +0000497 CI.setASTConsumer(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000498 }
499
500 if (CI.getFrontendOpts().ShowStats) {
501 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
502 CI.getPreprocessor().PrintStats();
503 CI.getPreprocessor().getIdentifierTable().PrintStats();
504 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
505 CI.getSourceManager().PrintStats();
506 llvm::errs() << "\n";
507 }
508
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000509 // Cleanup the output streams, and erase the output files if instructed by the
510 // FrontendAction.
511 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000512
Nico Weber1f29ccf2014-04-24 03:31:27 +0000513 if (isCurrentFileAST()) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000514 if (DisableFree) {
515 CI.resetAndLeakPreprocessor();
516 CI.resetAndLeakSourceManager();
517 CI.resetAndLeakFileManager();
518 } else {
519 CI.setPreprocessor(nullptr);
520 CI.setSourceManager(nullptr);
521 CI.setFileManager(nullptr);
522 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000523 }
524
Craig Topper49a27902014-05-22 04:46:25 +0000525 setCompilerInstance(nullptr);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000526 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000527}
528
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000529bool FrontendAction::shouldEraseOutputFiles() {
530 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
531}
532
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000533//===----------------------------------------------------------------------===//
534// Utility Actions
535//===----------------------------------------------------------------------===//
536
537void ASTFrontendAction::ExecuteAction() {
538 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000539 if (!CI.hasPreprocessor())
540 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000541
542 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
543 // here so the source manager would be initialized.
544 if (hasCodeCompletionSupport() &&
545 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
546 CI.createCodeCompletionConsumer();
547
548 // Use a code completion consumer?
Craig Topper49a27902014-05-22 04:46:25 +0000549 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000550 if (CI.hasCodeCompletionConsumer())
551 CompletionConsumer = &CI.getCodeCompletionConsumer();
552
Douglas Gregor0e93f012010-08-12 23:31:19 +0000553 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000554 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000555
Erik Verbruggen6e922512012-04-12 10:11:59 +0000556 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
557 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000558}
559
David Blaikie68e081d2011-12-20 02:48:34 +0000560void PluginASTAction::anchor() { }
561
David Blaikie6beb6aa2014-08-10 19:56:51 +0000562std::unique_ptr<ASTConsumer>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000563PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000564 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000565 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000566}
Chandler Carruthb5703512011-06-16 16:17:05 +0000567
David Blaikie6beb6aa2014-08-10 19:56:51 +0000568std::unique_ptr<ASTConsumer>
569WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
570 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000571 return WrappedAction->CreateASTConsumer(CI, InFile);
572}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000573bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
574 return WrappedAction->BeginInvocation(CI);
575}
Chandler Carruthb5703512011-06-16 16:17:05 +0000576bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000577 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000578 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000579 WrappedAction->setCompilerInstance(&CI);
Argyrios Kyrtzidise89a1792016-02-16 05:39:33 +0000580 auto Ret = WrappedAction->BeginSourceFileAction(CI, Filename);
581 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
582 setCurrentInput(WrappedAction->getCurrentInput());
583 return Ret;
Chandler Carruthb5703512011-06-16 16:17:05 +0000584}
585void WrapperFrontendAction::ExecuteAction() {
586 WrappedAction->ExecuteAction();
587}
588void WrapperFrontendAction::EndSourceFileAction() {
589 WrappedAction->EndSourceFileAction();
590}
591
592bool WrapperFrontendAction::usesPreprocessorOnly() const {
593 return WrappedAction->usesPreprocessorOnly();
594}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000595TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
596 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000597}
598bool WrapperFrontendAction::hasPCHSupport() const {
599 return WrappedAction->hasPCHSupport();
600}
601bool WrapperFrontendAction::hasASTFileSupport() const {
602 return WrappedAction->hasASTFileSupport();
603}
604bool WrapperFrontendAction::hasIRSupport() const {
605 return WrappedAction->hasIRSupport();
606}
607bool WrapperFrontendAction::hasCodeCompletionSupport() const {
608 return WrappedAction->hasCodeCompletionSupport();
609}
610
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +0000611WrapperFrontendAction::WrapperFrontendAction(
612 std::unique_ptr<FrontendAction> WrappedAction)
613 : WrappedAction(std::move(WrappedAction)) {}
Chandler Carruthb5703512011-06-16 16:17:05 +0000614