blob: 6d31c315b4b177caaeabf1b8c762004ad0649f7f [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"
Chandler Carruth71088d12011-12-09 01:55:54 +000015#include "clang/Frontend/ChainedIncludesSource.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000018#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregor453dbcb2012-01-26 07:55:45 +000019#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000020#include "clang/Frontend/MultiplexConsumer.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"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000032#include "llvm/Support/system_error.h"
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;
39
40public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000041 explicit DelegatingDeserializationListener(
42 ASTDeserializationListener *Previous)
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000043 : Previous(Previous) { }
44
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000045 virtual void ReaderInitialized(ASTReader *Reader) {
46 if (Previous)
47 Previous->ReaderInitialized(Reader);
48 }
49 virtual void IdentifierRead(serialization::IdentID ID,
50 IdentifierInfo *II) {
51 if (Previous)
52 Previous->IdentifierRead(ID, II);
53 }
54 virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
55 if (Previous)
56 Previous->TypeRead(Idx, T);
57 }
58 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
59 if (Previous)
60 Previous->DeclRead(ID, D);
61 }
62 virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
63 if (Previous)
64 Previous->SelectorRead(ID, Sel);
65 }
66 virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
67 MacroDefinition *MD) {
68 if (Previous)
69 Previous->MacroDefinitionRead(PPID, MD);
70 }
71};
72
73/// \brief Dumps deserialized declarations.
74class DeserializedDeclsDumper : public DelegatingDeserializationListener {
75public:
76 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
77 : DelegatingDeserializationListener(Previous) { }
78
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000079 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
80 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
81 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000082 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000083 llvm::outs() << "\n";
84
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000085 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000086 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000087};
88
David Blaikiee3f34112012-05-29 17:05:42 +000089/// \brief Checks deserialized declarations and emits error if a name
90/// matches one given in command-line using -error-on-deserialized-decl.
91class DeserializedDeclsChecker : public DelegatingDeserializationListener {
92 ASTContext &Ctx;
93 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000094
David Blaikiee3f34112012-05-29 17:05:42 +000095public:
96 DeserializedDeclsChecker(ASTContext &Ctx,
97 const std::set<std::string> &NamesToCheck,
98 ASTDeserializationListener *Previous)
99 : DelegatingDeserializationListener(Previous),
100 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000101
David Blaikiee3f34112012-05-29 17:05:42 +0000102 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
103 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
104 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
105 unsigned DiagID
106 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
107 "%0 was deserialized");
108 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
109 << ND->getNameAsString();
110 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000111
David Blaikiee3f34112012-05-29 17:05:42 +0000112 DelegatingDeserializationListener::DeclRead(ID, D);
113 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000114};
115
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000116} // end anonymous namespace
117
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000118FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000119
120FrontendAction::~FrontendAction() {}
121
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000122void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
123 ASTUnit *AST) {
124 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000125 CurrentASTUnit.reset(AST);
126}
127
Nico Weber5aa74af2011-01-25 20:34:14 +0000128ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000130 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
131 if (!Consumer)
132 return 0;
133
134 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
135 return Consumer;
136
137 // Make sure the non-plugin consumer is first, so that plugins can't
138 // modifiy the AST.
139 std::vector<ASTConsumer*> Consumers(1, Consumer);
140
141 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
142 i != e; ++i) {
143 // This is O(|plugins| * |add_plugins|), but since both numbers are
144 // way below 50 in practice, that's ok.
145 for (FrontendPluginRegistry::iterator
146 it = FrontendPluginRegistry::begin(),
147 ie = FrontendPluginRegistry::end();
148 it != ie; ++it) {
149 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000150 OwningPtr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000151 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000152 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000153 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
154 }
155 }
156 }
157
158 return new MultiplexConsumer(Consumers);
159}
160
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000161
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000162bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000163 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000164 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000165 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000166 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000167 setCompilerInstance(&CI);
168
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000169 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000170 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000171 if (!BeginInvocation(CI))
172 goto failure;
173
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000174 // AST files follow a very different path, since they share objects via the
175 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000176 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000177 assert(!usesPreprocessorOnly() &&
178 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000179 assert(hasASTFileSupport() &&
180 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000181
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000182 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000183 std::string Error;
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000184 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000185 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000186 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000187 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000188
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000189 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000190
191 // Set the shared objects, these are reset when we finish processing the
192 // file, otherwise the CompilerInstance will happily destroy them.
193 CI.setFileManager(&AST->getFileManager());
194 CI.setSourceManager(&AST->getSourceManager());
195 CI.setPreprocessor(&AST->getPreprocessor());
196 CI.setASTContext(&AST->getASTContext());
197
198 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000199 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000200 goto failure;
201
James Dennett18f43a62013-01-23 00:45:44 +0000202 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000203 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000204 if (!CI.hasASTConsumer())
205 goto failure;
206
207 return true;
208 }
209
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000210 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000211 if (!CI.hasFileManager())
212 CI.createFileManager();
213 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000214 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000215
216 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000217 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000218 assert(hasIRSupport() &&
219 "This action does not have IR file support!");
220
221 // Inform the diagnostic client we are processing a source file.
222 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000223 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000224
225 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000226 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000227 goto failure;
228
229 return true;
230 }
231
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000232 // If the implicit PCH include is actually a directory, rather than
233 // a single file, search for a suitable PCH file in that directory.
234 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
235 FileManager &FileMgr = CI.getFileManager();
236 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
237 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
238 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
239 llvm::error_code EC;
240 SmallString<128> DirNative;
241 llvm::sys::path::native(PCHDir->getName(), DirNative);
242 bool Found = false;
243 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
244 Dir != DirEnd && !EC; Dir.increment(EC)) {
245 // Check whether this is an acceptable AST file.
246 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
247 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000248 CI.getTargetOpts(),
249 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000250 PPOpts.ImplicitPCHInclude = Dir->path();
251 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000252 break;
253 }
254 }
255
256 if (!Found) {
257 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
258 return true;
259 }
260 }
261 }
262
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000263 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000264 CI.createPreprocessor();
265
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000266 // Inform the diagnostic client we are processing a source file.
267 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
268 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000269 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000270
271 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000272 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000273 goto failure;
274
James Dennett18f43a62013-01-23 00:45:44 +0000275 // Create the AST context and consumer unless this is a preprocessor only
276 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000277 if (!usesPreprocessorOnly()) {
278 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000279
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000280 OwningPtr<ASTConsumer> Consumer(
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000281 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000282 if (!Consumer)
283 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000284
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000285 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000286 CI.getPreprocessor().setPPMutationListener(
287 Consumer->GetPPMutationListener());
288
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000289 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
290 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000291 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000292 source.reset(ChainedIncludesSource::create(CI));
293 if (!source)
294 goto failure;
295 CI.getASTContext().setExternalSource(source);
296
297 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
298 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000299 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000300 ASTDeserializationListener *DeserialListener =
301 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000302 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
303 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000304 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
305 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
306 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
307 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000308 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000309 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000310 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000311 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000312 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000313 if (!CI.getASTContext().getExternalSource())
314 goto failure;
315 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000316
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000317 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000318 if (!CI.hasASTConsumer())
319 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000320 }
321
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000322 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000323 // source.
324 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
325 Preprocessor &PP = CI.getPreprocessor();
326 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000327 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000328 }
329
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000330 // If there is a layout overrides file, attach an external AST source that
331 // provides the layouts from that file.
332 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
333 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000334 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000335 Override(new LayoutOverrideSource(
336 CI.getFrontendOpts().OverrideRecordLayoutsFile));
337 CI.getASTContext().setExternalSource(Override);
338 }
339
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000340 return true;
341
342 // If we failed, reset state since the client will not end up calling the
343 // matching EndSourceFile().
344 failure:
345 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000346 CI.setASTContext(0);
347 CI.setPreprocessor(0);
348 CI.setSourceManager(0);
349 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000350 }
351
Jordan Roseaf6cf432012-08-10 01:06:08 +0000352 if (HasBegunSourceFile)
353 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000354 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000355 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000356 setCompilerInstance(0);
357 return false;
358}
359
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000360bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000361 CompilerInstance &CI = getCompilerInstance();
362
363 // Initialize the main file entry. This needs to be delayed until after PCH
364 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000365 if (!isCurrentFileAST()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000366 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000367 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000368 }
369
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000370 if (CI.hasFrontendTimer()) {
371 llvm::TimeRegion Timer(CI.getFrontendTimer());
372 ExecuteAction();
373 }
374 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000375
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000376 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000377 // there were any module-build failures.
378 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
379 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000380 GlobalModuleIndex::writeIndex(
381 CI.getFileManager(),
382 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
383 }
384
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000385 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000386}
387
388void FrontendAction::EndSourceFile() {
389 CompilerInstance &CI = getCompilerInstance();
390
Douglas Gregor92b97f22011-02-09 18:47:31 +0000391 // Inform the diagnostic client we are done with this source file.
392 CI.getDiagnosticClient().EndSourceFile();
393
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000394 // Finalize the action.
395 EndSourceFileAction();
396
397 // Release the consumer and the AST, in that order since the consumer may
398 // perform actions in its destructor which require the context.
399 //
400 // FIXME: There is more per-file stuff we could just drop here?
401 if (CI.getFrontendOpts().DisableFree) {
402 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000403 if (!isCurrentFileAST()) {
404 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000405 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000406 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000407 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000408 if (!isCurrentFileAST()) {
409 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000410 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000411 }
412 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000413 }
414
Daniel Dunbardbd82092010-03-23 05:09:10 +0000415 // Inform the preprocessor we are done.
416 if (CI.hasPreprocessor())
417 CI.getPreprocessor().EndSourceFile();
418
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000419 if (CI.getFrontendOpts().ShowStats) {
420 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
421 CI.getPreprocessor().PrintStats();
422 CI.getPreprocessor().getIdentifierTable().PrintStats();
423 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
424 CI.getSourceManager().PrintStats();
425 llvm::errs() << "\n";
426 }
427
428 // Cleanup the output streams, and erase the output files if we encountered
429 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000430 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000431
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000432 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000433 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000434 CI.resetAndLeakASTContext();
435 CI.resetAndLeakPreprocessor();
436 CI.resetAndLeakSourceManager();
437 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000438 }
439
440 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000441 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000442}
443
444//===----------------------------------------------------------------------===//
445// Utility Actions
446//===----------------------------------------------------------------------===//
447
448void ASTFrontendAction::ExecuteAction() {
449 CompilerInstance &CI = getCompilerInstance();
450
451 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
452 // here so the source manager would be initialized.
453 if (hasCodeCompletionSupport() &&
454 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
455 CI.createCodeCompletionConsumer();
456
457 // Use a code completion consumer?
458 CodeCompleteConsumer *CompletionConsumer = 0;
459 if (CI.hasCodeCompletionConsumer())
460 CompletionConsumer = &CI.getCodeCompletionConsumer();
461
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000462 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000463 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000464
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000465 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
466 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000467}
468
David Blaikie99ba9e32011-12-20 02:48:34 +0000469void PluginASTAction::anchor() { }
470
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000471ASTConsumer *
472PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000473 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000474 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000475}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000476
477ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000479 return WrappedAction->CreateASTConsumer(CI, InFile);
480}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000481bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
482 return WrappedAction->BeginInvocation(CI);
483}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000484bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000485 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000486 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000487 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000488 return WrappedAction->BeginSourceFileAction(CI, Filename);
489}
490void WrapperFrontendAction::ExecuteAction() {
491 WrappedAction->ExecuteAction();
492}
493void WrapperFrontendAction::EndSourceFileAction() {
494 WrappedAction->EndSourceFileAction();
495}
496
497bool WrapperFrontendAction::usesPreprocessorOnly() const {
498 return WrappedAction->usesPreprocessorOnly();
499}
Douglas Gregor467dc882011-08-25 22:30:56 +0000500TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
501 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000502}
503bool WrapperFrontendAction::hasPCHSupport() const {
504 return WrappedAction->hasPCHSupport();
505}
506bool WrapperFrontendAction::hasASTFileSupport() const {
507 return WrappedAction->hasASTFileSupport();
508}
509bool WrapperFrontendAction::hasIRSupport() const {
510 return WrappedAction->hasIRSupport();
511}
512bool WrapperFrontendAction::hasCodeCompletionSupport() const {
513 return WrappedAction->hasCodeCompletionSupport();
514}
515
516WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
517 : WrappedAction(WrappedAction) {}
518