blob: c274ba71768cef353bef3c542de67a60fb30a87b [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
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000035namespace {
36
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000037class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000038 ASTDeserializationListener *Previous;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070039 bool DeletePrevious;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000040
41public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000042 explicit DelegatingDeserializationListener(
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043 ASTDeserializationListener *Previous, bool DeletePrevious)
44 : Previous(Previous), DeletePrevious(DeletePrevious) {}
45 virtual ~DelegatingDeserializationListener() {
46 if (DeletePrevious)
47 delete Previous;
48 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000049
Stephen Hines651f13c2014-04-23 16:59:28 -070050 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000051 if (Previous)
52 Previous->ReaderInitialized(Reader);
53 }
Stephen Hines651f13c2014-04-23 16:59:28 -070054 void IdentifierRead(serialization::IdentID ID,
55 IdentifierInfo *II) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000056 if (Previous)
57 Previous->IdentifierRead(ID, II);
58 }
Stephen Hines651f13c2014-04-23 16:59:28 -070059 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000060 if (Previous)
61 Previous->TypeRead(Idx, T);
62 }
Stephen Hines651f13c2014-04-23 16:59:28 -070063 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000064 if (Previous)
65 Previous->DeclRead(ID, D);
66 }
Stephen Hines651f13c2014-04-23 16:59:28 -070067 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000068 if (Previous)
69 Previous->SelectorRead(ID, Sel);
70 }
Stephen Hines651f13c2014-04-23 16:59:28 -070071 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
72 MacroDefinition *MD) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000073 if (Previous)
74 Previous->MacroDefinitionRead(PPID, MD);
75 }
76};
77
78/// \brief Dumps deserialized declarations.
79class DeserializedDeclsDumper : public DelegatingDeserializationListener {
80public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070081 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
82 bool DeletePrevious)
83 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000084
Stephen Hines651f13c2014-04-23 16:59:28 -070085 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000086 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
87 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000088 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000089 llvm::outs() << "\n";
90
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000091 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000092 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000093};
94
David Blaikiee3f34112012-05-29 17:05:42 +000095/// \brief Checks deserialized declarations and emits error if a name
96/// matches one given in command-line using -error-on-deserialized-decl.
97class DeserializedDeclsChecker : public DelegatingDeserializationListener {
98 ASTContext &Ctx;
99 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000100
David Blaikiee3f34112012-05-29 17:05:42 +0000101public:
102 DeserializedDeclsChecker(ASTContext &Ctx,
103 const std::set<std::string> &NamesToCheck,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700104 ASTDeserializationListener *Previous,
105 bool DeletePrevious)
106 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
107 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000108
Stephen Hines651f13c2014-04-23 16:59:28 -0700109 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikiee3f34112012-05-29 17:05:42 +0000110 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
111 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
112 unsigned DiagID
113 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
114 "%0 was deserialized");
115 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
116 << ND->getNameAsString();
117 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000118
David Blaikiee3f34112012-05-29 17:05:42 +0000119 DelegatingDeserializationListener::DeclRead(ID, D);
120 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000121};
122
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000123} // end anonymous namespace
124
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700125FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000126
127FrontendAction::~FrontendAction() {}
128
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000129void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
130 ASTUnit *AST) {
131 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000132 CurrentASTUnit.reset(AST);
133}
134
Nico Weber5aa74af2011-01-25 20:34:14 +0000135ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000136 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000137 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
138 if (!Consumer)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700139 return nullptr;
Nico Weber5aa74af2011-01-25 20:34:14 +0000140
141 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
142 return Consumer;
143
144 // Make sure the non-plugin consumer is first, so that plugins can't
145 // modifiy the AST.
146 std::vector<ASTConsumer*> Consumers(1, Consumer);
147
148 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
149 i != e; ++i) {
150 // This is O(|plugins| * |add_plugins|), but since both numbers are
151 // way below 50 in practice, that's ok.
152 for (FrontendPluginRegistry::iterator
153 it = FrontendPluginRegistry::begin(),
154 ie = FrontendPluginRegistry::end();
155 it != ie; ++it) {
156 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700157 std::unique_ptr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000158 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000159 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000160 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
161 }
162 }
163 }
164
165 return new MultiplexConsumer(Consumers);
166}
167
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000168bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000169 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000170 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000171 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000172 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000173 setCompilerInstance(&CI);
174
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000175 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000176 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000177 if (!BeginInvocation(CI))
178 goto failure;
179
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000180 // AST files follow a very different path, since they share objects via the
181 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000182 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000183 assert(!usesPreprocessorOnly() &&
184 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000185 assert(hasASTFileSupport() &&
186 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000187
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000188 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Stephen Hines651f13c2014-04-23 16:59:28 -0700189
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000190 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000191 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000192 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000193 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000194
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000195 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000196
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000197 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700198 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000199 HasBegunSourceFile = true;
200
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000201 // Set the shared objects, these are reset when we finish processing the
202 // file, otherwise the CompilerInstance will happily destroy them.
203 CI.setFileManager(&AST->getFileManager());
204 CI.setSourceManager(&AST->getSourceManager());
205 CI.setPreprocessor(&AST->getPreprocessor());
206 CI.setASTContext(&AST->getASTContext());
207
208 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000209 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000210 goto failure;
211
James Dennett18f43a62013-01-23 00:45:44 +0000212 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000213 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000214 if (!CI.hasASTConsumer())
215 goto failure;
216
217 return true;
218 }
219
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700220 if (!CI.hasVirtualFileSystem()) {
221 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
222 createVFSFromCompilerInvocation(CI.getInvocation(),
223 CI.getDiagnostics()))
224 CI.setVirtualFileSystem(VFS);
225 else
226 goto failure;
Stephen Hines651f13c2014-04-23 16:59:28 -0700227 }
228
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000229 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000230 if (!CI.hasFileManager())
231 CI.createFileManager();
232 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000233 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000234
235 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000236 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000237 assert(hasIRSupport() &&
238 "This action does not have IR file support!");
239
240 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700241 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000242 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000243
244 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000245 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000246 goto failure;
247
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700248 // Initialize the main file entry.
249 if (!CI.InitializeSourceManager(CurrentInput))
250 goto failure;
251
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000252 return true;
253 }
254
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000255 // If the implicit PCH include is actually a directory, rather than
256 // a single file, search for a suitable PCH file in that directory.
257 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
258 FileManager &FileMgr = CI.getFileManager();
259 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
260 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
261 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700262 std::error_code EC;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000263 SmallString<128> DirNative;
264 llvm::sys::path::native(PCHDir->getName(), DirNative);
265 bool Found = false;
266 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
267 Dir != DirEnd && !EC; Dir.increment(EC)) {
268 // Check whether this is an acceptable AST file.
269 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
270 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000271 CI.getTargetOpts(),
272 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000273 PPOpts.ImplicitPCHInclude = Dir->path();
274 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000275 break;
276 }
277 }
278
279 if (!Found) {
280 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
281 return true;
282 }
283 }
284 }
285
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000286 // Set up the preprocessor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700287 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbar20560482010-06-07 23:23:50 +0000288
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000289 // Inform the diagnostic client we are processing a source file.
290 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
291 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000292 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000293
294 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000295 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000296 goto failure;
297
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700298 // Initialize the main file entry. It is important that this occurs after
299 // BeginSourceFileAction, which may change CurrentInput during module builds.
300 if (!CI.InitializeSourceManager(CurrentInput))
301 goto failure;
302
James Dennett18f43a62013-01-23 00:45:44 +0000303 // Create the AST context and consumer unless this is a preprocessor only
304 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000305 if (!usesPreprocessorOnly()) {
306 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000307
Stephen Hines651f13c2014-04-23 16:59:28 -0700308 std::unique_ptr<ASTConsumer> Consumer(
309 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000310 if (!Consumer)
311 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000312
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000313 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000314
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000315 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
316 // Convert headers to PCH and chain them.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700317 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
318 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000319 if (!source)
320 goto failure;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700321 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000322 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000323 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
324 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000325 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000326 ASTDeserializationListener *DeserialListener =
327 Consumer->GetASTDeserializationListener();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700328 bool DeleteDeserialListener = false;
329 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
330 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
331 DeleteDeserialListener);
332 DeleteDeserialListener = true;
333 }
334 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
335 DeserialListener = new DeserializedDeclsChecker(
336 CI.getASTContext(),
337 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
338 DeserialListener, DeleteDeserialListener);
339 DeleteDeserialListener = true;
340 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000341 CI.createPCHExternalASTSource(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700342 CI.getPreprocessorOpts().ImplicitPCHInclude,
343 CI.getPreprocessorOpts().DisablePCHValidation,
344 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
345 DeleteDeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000346 if (!CI.getASTContext().getExternalSource())
347 goto failure;
348 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000349
Stephen Hines651f13c2014-04-23 16:59:28 -0700350 CI.setASTConsumer(Consumer.release());
Sebastian Redl77f46032010-07-09 21:00:24 +0000351 if (!CI.hasASTConsumer())
352 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000353 }
354
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000355 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000356 // source.
357 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
358 Preprocessor &PP = CI.getPreprocessor();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700359
360 // If modules are enabled, create the module manager before creating
361 // any builtins, so that all declarations know that they might be
362 // extended by an external source.
363 if (CI.getLangOpts().Modules)
364 CI.createModuleManager();
365
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000366 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000367 PP.getLangOpts());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700368 } else {
369 // FIXME: If this is a problem, recover from it by creating a multiplex
370 // source.
371 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
372 "modules enabled but created an external source that "
373 "doesn't support modules");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000374 }
375
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000376 // If there is a layout overrides file, attach an external AST source that
377 // provides the layouts from that file.
378 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
379 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700380 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000381 Override(new LayoutOverrideSource(
382 CI.getFrontendOpts().OverrideRecordLayoutsFile));
383 CI.getASTContext().setExternalSource(Override);
384 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700385
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000386 return true;
387
388 // If we failed, reset state since the client will not end up calling the
389 // matching EndSourceFile().
390 failure:
391 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700392 CI.setASTContext(nullptr);
393 CI.setPreprocessor(nullptr);
394 CI.setSourceManager(nullptr);
395 CI.setFileManager(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000396 }
397
Jordan Roseaf6cf432012-08-10 01:06:08 +0000398 if (HasBegunSourceFile)
399 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000400 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000401 setCurrentInput(FrontendInputFile());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700402 setCompilerInstance(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000403 return false;
404}
405
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000406bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000407 CompilerInstance &CI = getCompilerInstance();
408
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000409 if (CI.hasFrontendTimer()) {
410 llvm::TimeRegion Timer(CI.getFrontendTimer());
411 ExecuteAction();
412 }
413 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000414
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000415 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000416 // there were any module-build failures.
417 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
418 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000419 GlobalModuleIndex::writeIndex(
420 CI.getFileManager(),
421 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
422 }
423
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000424 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000425}
426
427void FrontendAction::EndSourceFile() {
428 CompilerInstance &CI = getCompilerInstance();
429
Douglas Gregor92b97f22011-02-09 18:47:31 +0000430 // Inform the diagnostic client we are done with this source file.
431 CI.getDiagnosticClient().EndSourceFile();
432
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000433 // Finalize the action.
434 EndSourceFileAction();
435
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700436 // Sema references the ast consumer, so reset sema first.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000437 //
438 // FIXME: There is more per-file stuff we could just drop here?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700439 bool DisableFree = CI.getFrontendOpts().DisableFree;
440 if (DisableFree) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000441 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700442 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000443 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000444 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700445 BuryPointer(CI.takeASTConsumer());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000446 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000447 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700448 CI.setSema(nullptr);
449 CI.setASTContext(nullptr);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000450 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700451 CI.setASTConsumer(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000452 }
453
Daniel Dunbardbd82092010-03-23 05:09:10 +0000454 // Inform the preprocessor we are done.
455 if (CI.hasPreprocessor())
456 CI.getPreprocessor().EndSourceFile();
457
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000458 if (CI.getFrontendOpts().ShowStats) {
459 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
460 CI.getPreprocessor().PrintStats();
461 CI.getPreprocessor().getIdentifierTable().PrintStats();
462 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
463 CI.getSourceManager().PrintStats();
464 llvm::errs() << "\n";
465 }
466
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000467 // Cleanup the output streams, and erase the output files if instructed by the
468 // FrontendAction.
469 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000470
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700471 // FIXME: Only do this if DisableFree is set.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000472 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700473 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000474 CI.resetAndLeakASTContext();
475 CI.resetAndLeakPreprocessor();
476 CI.resetAndLeakSourceManager();
477 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000478 }
479
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700480 setCompilerInstance(nullptr);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000481 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000482}
483
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000484bool FrontendAction::shouldEraseOutputFiles() {
485 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
486}
487
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000488//===----------------------------------------------------------------------===//
489// Utility Actions
490//===----------------------------------------------------------------------===//
491
492void ASTFrontendAction::ExecuteAction() {
493 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola0046ce52013-07-28 13:23:37 +0000494 if (!CI.hasPreprocessor())
495 return;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000496
497 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
498 // here so the source manager would be initialized.
499 if (hasCodeCompletionSupport() &&
500 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
501 CI.createCodeCompletionConsumer();
502
503 // Use a code completion consumer?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700504 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000505 if (CI.hasCodeCompletionConsumer())
506 CompletionConsumer = &CI.getCodeCompletionConsumer();
507
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000508 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000509 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000510
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000511 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
512 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000513}
514
David Blaikie99ba9e32011-12-20 02:48:34 +0000515void PluginASTAction::anchor() { }
516
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000517ASTConsumer *
518PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000519 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000520 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000521}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000522
523ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000524 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000525 return WrappedAction->CreateASTConsumer(CI, InFile);
526}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000527bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
528 return WrappedAction->BeginInvocation(CI);
529}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000530bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000531 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000532 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000533 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000534 return WrappedAction->BeginSourceFileAction(CI, Filename);
535}
536void WrapperFrontendAction::ExecuteAction() {
537 WrappedAction->ExecuteAction();
538}
539void WrapperFrontendAction::EndSourceFileAction() {
540 WrappedAction->EndSourceFileAction();
541}
542
543bool WrapperFrontendAction::usesPreprocessorOnly() const {
544 return WrappedAction->usesPreprocessorOnly();
545}
Douglas Gregor467dc882011-08-25 22:30:56 +0000546TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
547 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000548}
549bool WrapperFrontendAction::hasPCHSupport() const {
550 return WrappedAction->hasPCHSupport();
551}
552bool WrapperFrontendAction::hasASTFileSupport() const {
553 return WrappedAction->hasASTFileSupport();
554}
555bool WrapperFrontendAction::hasIRSupport() const {
556 return WrappedAction->hasIRSupport();
557}
558bool WrapperFrontendAction::hasCodeCompletionSupport() const {
559 return WrappedAction->hasCodeCompletionSupport();
560}
561
562WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
563 : WrappedAction(WrappedAction) {}
564