blob: c81c81aba4d2601ba259ca92aec48039ec914eac [file] [log] [blame]
Daniel Dunbar4ee24092009-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 Redlffaab3e2010-07-30 00:29:29 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000012#include "clang/AST/ASTContext.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000013#include "clang/AST/DeclGroup.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000014#include "clang/Frontend/ASTUnit.h"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000017#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregor453dbcb2012-01-26 07:55:45 +000018#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000019#include "clang/Frontend/MultiplexConsumer.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070020#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000021#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000023#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000024#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +000025#include "clang/Serialization/ASTReader.h"
Douglas Gregora6b00fc2013-01-23 22:38:11 +000026#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000027#include "llvm/Support/ErrorHandling.h"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000030#include "llvm/Support/Timer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000031#include "llvm/Support/raw_ostream.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070032#include <system_error>
Daniel Dunbar4ee24092009-11-14 10:42:35 +000033using namespace clang;
34
Stephen Hines176edba2014-12-01 14:53:08 -080035template class llvm::Registry<clang::PluginASTAction>;
36
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000037namespace {
38
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000039class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000040 ASTDeserializationListener *Previous;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070041 bool DeletePrevious;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000042
43public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000044 explicit DelegatingDeserializationListener(
Stephen Hines6bcf27b2014-05-29 04:14:42 -070045 ASTDeserializationListener *Previous, bool DeletePrevious)
46 : Previous(Previous), DeletePrevious(DeletePrevious) {}
47 virtual ~DelegatingDeserializationListener() {
48 if (DeletePrevious)
49 delete Previous;
50 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000051
Stephen Hines651f13c2014-04-23 16:59:28 -070052 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000053 if (Previous)
54 Previous->ReaderInitialized(Reader);
55 }
Stephen Hines651f13c2014-04-23 16:59:28 -070056 void IdentifierRead(serialization::IdentID ID,
57 IdentifierInfo *II) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000058 if (Previous)
59 Previous->IdentifierRead(ID, II);
60 }
Stephen Hines651f13c2014-04-23 16:59:28 -070061 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000062 if (Previous)
63 Previous->TypeRead(Idx, T);
64 }
Stephen Hines651f13c2014-04-23 16:59:28 -070065 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000066 if (Previous)
67 Previous->DeclRead(ID, D);
68 }
Stephen Hines651f13c2014-04-23 16:59:28 -070069 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000070 if (Previous)
71 Previous->SelectorRead(ID, Sel);
72 }
Stephen Hines651f13c2014-04-23 16:59:28 -070073 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
74 MacroDefinition *MD) override {
Argyrios Kyrtzidis407ef9a2011-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:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070083 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
84 bool DeletePrevious)
85 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000086
Stephen Hines651f13c2014-04-23 16:59:28 -070087 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000088 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
89 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000090 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000091 llvm::outs() << "\n";
92
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000093 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000094 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000095};
96
David Blaikiee3f34112012-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 Kyrtzidis3e785932010-10-14 20:14:25 +0000102
David Blaikiee3f34112012-05-29 17:05:42 +0000103public:
104 DeserializedDeclsChecker(ASTContext &Ctx,
105 const std::set<std::string> &NamesToCheck,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700106 ASTDeserializationListener *Previous,
107 bool DeletePrevious)
108 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
109 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000110
Stephen Hines651f13c2014-04-23 16:59:28 -0700111 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikiee3f34112012-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 Kyrtzidis3e785932010-10-14 20:14:25 +0000120
David Blaikiee3f34112012-05-29 17:05:42 +0000121 DelegatingDeserializationListener::DeclRead(ID, D);
122 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000123};
124
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000125} // end anonymous namespace
126
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700127FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000128
129FrontendAction::~FrontendAction() {}
130
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000131void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
Stephen Hines176edba2014-12-01 14:53:08 -0800132 std::unique_ptr<ASTUnit> AST) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000133 this->CurrentInput = CurrentInput;
Stephen Hines176edba2014-12-01 14:53:08 -0800134 CurrentASTUnit = std::move(AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000135}
136
Stephen Hines176edba2014-12-01 14:53:08 -0800137std::unique_ptr<ASTConsumer>
138FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
139 StringRef InFile) {
140 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
Nico Weber5aa74af2011-01-25 20:34:14 +0000141 if (!Consumer)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700142 return nullptr;
Nico Weber5aa74af2011-01-25 20:34:14 +0000143
144 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
145 return Consumer;
146
147 // Make sure the non-plugin consumer is first, so that plugins can't
148 // modifiy the AST.
Stephen Hines176edba2014-12-01 14:53:08 -0800149 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
150 Consumers.push_back(std::move(Consumer));
Nico Weber5aa74af2011-01-25 20:34:14 +0000151
152 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
153 i != e; ++i) {
154 // This is O(|plugins| * |add_plugins|), but since both numbers are
155 // way below 50 in practice, that's ok.
156 for (FrontendPluginRegistry::iterator
157 it = FrontendPluginRegistry::begin(),
158 ie = FrontendPluginRegistry::end();
159 it != ie; ++it) {
Stephen Hines176edba2014-12-01 14:53:08 -0800160 if (it->getName() != CI.getFrontendOpts().AddPluginActions[i])
161 continue;
162 std::unique_ptr<PluginASTAction> P = it->instantiate();
163 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
164 Consumers.push_back(P->CreateASTConsumer(CI, InFile));
Nico Weber5aa74af2011-01-25 20:34:14 +0000165 }
166 }
167
Stephen Hines176edba2014-12-01 14:53:08 -0800168 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Nico Weber5aa74af2011-01-25 20:34:14 +0000169}
170
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000171bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000172 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000173 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000174 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000175 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000176 setCompilerInstance(&CI);
177
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000178 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000179 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000180 if (!BeginInvocation(CI))
181 goto failure;
182
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000183 // AST files follow a very different path, since they share objects via the
184 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000185 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000186 assert(!usesPreprocessorOnly() &&
187 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000188 assert(hasASTFileSupport() &&
189 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000190
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000191 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Stephen Hines651f13c2014-04-23 16:59:28 -0700192
Stephen Hines176edba2014-12-01 14:53:08 -0800193 std::unique_ptr<ASTUnit> AST =
194 ASTUnit::LoadFromASTFile(InputFile, Diags, CI.getFileSystemOpts());
195
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000196 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000197 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000198
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000199 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700200 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000201 HasBegunSourceFile = true;
202
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000203 // Set the shared objects, these are reset when we finish processing the
204 // file, otherwise the CompilerInstance will happily destroy them.
205 CI.setFileManager(&AST->getFileManager());
206 CI.setSourceManager(&AST->getSourceManager());
207 CI.setPreprocessor(&AST->getPreprocessor());
208 CI.setASTContext(&AST->getASTContext());
209
Stephen Hines176edba2014-12-01 14:53:08 -0800210 setCurrentInput(Input, std::move(AST));
211
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000212 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000213 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000214 goto failure;
215
James Dennett18f43a62013-01-23 00:45:44 +0000216 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000217 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000218 if (!CI.hasASTConsumer())
219 goto failure;
220
221 return true;
222 }
223
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700224 if (!CI.hasVirtualFileSystem()) {
225 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
226 createVFSFromCompilerInvocation(CI.getInvocation(),
227 CI.getDiagnostics()))
228 CI.setVirtualFileSystem(VFS);
229 else
230 goto failure;
Stephen Hines651f13c2014-04-23 16:59:28 -0700231 }
232
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000233 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000234 if (!CI.hasFileManager())
235 CI.createFileManager();
236 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000237 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000238
239 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000240 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000241 assert(hasIRSupport() &&
242 "This action does not have IR file support!");
243
244 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700245 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000246 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000247
248 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000249 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000250 goto failure;
251
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700252 // Initialize the main file entry.
253 if (!CI.InitializeSourceManager(CurrentInput))
254 goto failure;
255
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000256 return true;
257 }
258
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000259 // If the implicit PCH include is actually a directory, rather than
260 // a single file, search for a suitable PCH file in that directory.
261 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
262 FileManager &FileMgr = CI.getFileManager();
263 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
264 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
265 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700266 std::error_code EC;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000267 SmallString<128> DirNative;
268 llvm::sys::path::native(PCHDir->getName(), DirNative);
269 bool Found = false;
270 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
271 Dir != DirEnd && !EC; Dir.increment(EC)) {
272 // Check whether this is an acceptable AST file.
273 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
274 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000275 CI.getTargetOpts(),
276 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000277 PPOpts.ImplicitPCHInclude = Dir->path();
278 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000279 break;
280 }
281 }
282
283 if (!Found) {
284 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
285 return true;
286 }
287 }
288 }
289
Stephen Hines176edba2014-12-01 14:53:08 -0800290 // Set up the preprocessor if needed. When parsing model files the
291 // preprocessor of the original source is reused.
292 if (!isModelParsingAction())
293 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbar20560482010-06-07 23:23:50 +0000294
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000295 // Inform the diagnostic client we are processing a source file.
296 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
297 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000298 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000299
300 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000301 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000302 goto failure;
303
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700304 // Initialize the main file entry. It is important that this occurs after
305 // BeginSourceFileAction, which may change CurrentInput during module builds.
306 if (!CI.InitializeSourceManager(CurrentInput))
307 goto failure;
308
James Dennett18f43a62013-01-23 00:45:44 +0000309 // Create the AST context and consumer unless this is a preprocessor only
310 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000311 if (!usesPreprocessorOnly()) {
Stephen Hines176edba2014-12-01 14:53:08 -0800312 // Parsing a model file should reuse the existing ASTContext.
313 if (!isModelParsingAction())
314 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000315
Stephen Hines176edba2014-12-01 14:53:08 -0800316 std::unique_ptr<ASTConsumer> Consumer =
317 CreateWrappedASTConsumer(CI, InputFile);
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000318 if (!Consumer)
319 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000320
Stephen Hines176edba2014-12-01 14:53:08 -0800321 // FIXME: should not overwrite ASTMutationListener when parsing model files?
322 if (!isModelParsingAction())
323 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
324
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000325 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
326 // Convert headers to PCH and chain them.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700327 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
328 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000329 if (!source)
330 goto failure;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700331 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000332 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000333 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
334 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000335 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000336 ASTDeserializationListener *DeserialListener =
337 Consumer->GetASTDeserializationListener();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700338 bool DeleteDeserialListener = false;
339 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
340 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
341 DeleteDeserialListener);
342 DeleteDeserialListener = true;
343 }
344 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
345 DeserialListener = new DeserializedDeclsChecker(
346 CI.getASTContext(),
347 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
348 DeserialListener, DeleteDeserialListener);
349 DeleteDeserialListener = true;
350 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000351 CI.createPCHExternalASTSource(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700352 CI.getPreprocessorOpts().ImplicitPCHInclude,
353 CI.getPreprocessorOpts().DisablePCHValidation,
354 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
355 DeleteDeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000356 if (!CI.getASTContext().getExternalSource())
357 goto failure;
358 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000359
Stephen Hines176edba2014-12-01 14:53:08 -0800360 CI.setASTConsumer(std::move(Consumer));
Sebastian Redl77f46032010-07-09 21:00:24 +0000361 if (!CI.hasASTConsumer())
362 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000363 }
364
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000365 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000366 // source.
367 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
368 Preprocessor &PP = CI.getPreprocessor();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700369
370 // If modules are enabled, create the module manager before creating
371 // any builtins, so that all declarations know that they might be
372 // extended by an external source.
373 if (CI.getLangOpts().Modules)
374 CI.createModuleManager();
375
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000376 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000377 PP.getLangOpts());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700378 } else {
379 // FIXME: If this is a problem, recover from it by creating a multiplex
380 // source.
381 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
382 "modules enabled but created an external source that "
383 "doesn't support modules");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000384 }
385
Stephen Hines176edba2014-12-01 14:53:08 -0800386 // If we were asked to load any module files, do so now.
387 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
388 if (!CI.loadModuleFile(ModuleFile))
389 goto failure;
390
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000391 // If there is a layout overrides file, attach an external AST source that
392 // provides the layouts from that file.
393 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
394 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700395 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000396 Override(new LayoutOverrideSource(
397 CI.getFrontendOpts().OverrideRecordLayoutsFile));
398 CI.getASTContext().setExternalSource(Override);
399 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700400
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000401 return true;
402
403 // If we failed, reset state since the client will not end up calling the
404 // matching EndSourceFile().
405 failure:
406 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700407 CI.setASTContext(nullptr);
408 CI.setPreprocessor(nullptr);
409 CI.setSourceManager(nullptr);
410 CI.setFileManager(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000411 }
412
Jordan Roseaf6cf432012-08-10 01:06:08 +0000413 if (HasBegunSourceFile)
414 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000415 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000416 setCurrentInput(FrontendInputFile());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700417 setCompilerInstance(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000418 return false;
419}
420
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000421bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000422 CompilerInstance &CI = getCompilerInstance();
423
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000424 if (CI.hasFrontendTimer()) {
425 llvm::TimeRegion Timer(CI.getFrontendTimer());
426 ExecuteAction();
427 }
428 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000429
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000430 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000431 // there were any module-build failures.
432 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
433 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000434 GlobalModuleIndex::writeIndex(
435 CI.getFileManager(),
436 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
437 }
438
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000439 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000440}
441
442void FrontendAction::EndSourceFile() {
443 CompilerInstance &CI = getCompilerInstance();
444
Douglas Gregor92b97f22011-02-09 18:47:31 +0000445 // Inform the diagnostic client we are done with this source file.
446 CI.getDiagnosticClient().EndSourceFile();
447
Stephen Hines176edba2014-12-01 14:53:08 -0800448 // Inform the preprocessor we are done.
449 if (CI.hasPreprocessor())
450 CI.getPreprocessor().EndSourceFile();
451
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000452 // Finalize the action.
453 EndSourceFileAction();
454
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700455 // Sema references the ast consumer, so reset sema first.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000456 //
457 // FIXME: There is more per-file stuff we could just drop here?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700458 bool DisableFree = CI.getFrontendOpts().DisableFree;
459 if (DisableFree) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000460 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700461 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000462 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000463 }
Stephen Hines176edba2014-12-01 14:53:08 -0800464 BuryPointer(CI.takeASTConsumer().get());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000465 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000466 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700467 CI.setSema(nullptr);
468 CI.setASTContext(nullptr);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000469 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700470 CI.setASTConsumer(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000471 }
472
473 if (CI.getFrontendOpts().ShowStats) {
474 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
475 CI.getPreprocessor().PrintStats();
476 CI.getPreprocessor().getIdentifierTable().PrintStats();
477 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
478 CI.getSourceManager().PrintStats();
479 llvm::errs() << "\n";
480 }
481
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000482 // Cleanup the output streams, and erase the output files if instructed by the
483 // FrontendAction.
484 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000485
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700486 // FIXME: Only do this if DisableFree is set.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000487 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700488 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000489 CI.resetAndLeakASTContext();
490 CI.resetAndLeakPreprocessor();
491 CI.resetAndLeakSourceManager();
492 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000493 }
494
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700495 setCompilerInstance(nullptr);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000496 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000497}
498
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000499bool FrontendAction::shouldEraseOutputFiles() {
500 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
501}
502
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000503//===----------------------------------------------------------------------===//
504// Utility Actions
505//===----------------------------------------------------------------------===//
506
507void ASTFrontendAction::ExecuteAction() {
508 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola0046ce52013-07-28 13:23:37 +0000509 if (!CI.hasPreprocessor())
510 return;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000511
512 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
513 // here so the source manager would be initialized.
514 if (hasCodeCompletionSupport() &&
515 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
516 CI.createCodeCompletionConsumer();
517
518 // Use a code completion consumer?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700519 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000520 if (CI.hasCodeCompletionConsumer())
521 CompletionConsumer = &CI.getCodeCompletionConsumer();
522
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000523 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000524 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000525
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000526 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
527 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000528}
529
David Blaikie99ba9e32011-12-20 02:48:34 +0000530void PluginASTAction::anchor() { }
531
Stephen Hines176edba2014-12-01 14:53:08 -0800532std::unique_ptr<ASTConsumer>
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000533PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000534 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000535 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000536}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000537
Stephen Hines176edba2014-12-01 14:53:08 -0800538std::unique_ptr<ASTConsumer>
539WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
540 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000541 return WrappedAction->CreateASTConsumer(CI, InFile);
542}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000543bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
544 return WrappedAction->BeginInvocation(CI);
545}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000546bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000547 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000548 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000549 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000550 return WrappedAction->BeginSourceFileAction(CI, Filename);
551}
552void WrapperFrontendAction::ExecuteAction() {
553 WrappedAction->ExecuteAction();
554}
555void WrapperFrontendAction::EndSourceFileAction() {
556 WrappedAction->EndSourceFileAction();
557}
558
559bool WrapperFrontendAction::usesPreprocessorOnly() const {
560 return WrappedAction->usesPreprocessorOnly();
561}
Douglas Gregor467dc882011-08-25 22:30:56 +0000562TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
563 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000564}
565bool WrapperFrontendAction::hasPCHSupport() const {
566 return WrappedAction->hasPCHSupport();
567}
568bool WrapperFrontendAction::hasASTFileSupport() const {
569 return WrappedAction->hasASTFileSupport();
570}
571bool WrapperFrontendAction::hasIRSupport() const {
572 return WrappedAction->hasIRSupport();
573}
574bool WrapperFrontendAction::hasCodeCompletionSupport() const {
575 return WrappedAction->hasCodeCompletionSupport();
576}
577
578WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
579 : WrappedAction(WrappedAction) {}
580