blob: 05064fbd4f39b79567b5bccddfa0b89f3f4f5c9d [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())) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000250 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
251 if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
252 PPOpts.Includes[I] = Dir->path();
253 PPOpts.ImplicitPCHInclude = Dir->path();
254 Found = true;
255 break;
256 }
257 }
258
259 assert(Found && "Implicit PCH include not in includes list?");
260 break;
261 }
262 }
263
264 if (!Found) {
265 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
266 return true;
267 }
268 }
269 }
270
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000271 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000272 CI.createPreprocessor();
273
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000274 // Inform the diagnostic client we are processing a source file.
275 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
276 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000277 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000278
279 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000280 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000281 goto failure;
282
James Dennett18f43a62013-01-23 00:45:44 +0000283 // Create the AST context and consumer unless this is a preprocessor only
284 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000285 if (!usesPreprocessorOnly()) {
286 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000287
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000288 OwningPtr<ASTConsumer> Consumer(
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000289 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000290 if (!Consumer)
291 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000292
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000293 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000294 CI.getPreprocessor().setPPMutationListener(
295 Consumer->GetPPMutationListener());
296
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000297 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
298 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000299 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000300 source.reset(ChainedIncludesSource::create(CI));
301 if (!source)
302 goto failure;
303 CI.getASTContext().setExternalSource(source);
304
305 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
306 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000307 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000308 ASTDeserializationListener *DeserialListener =
309 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000310 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
311 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000312 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
313 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
314 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
315 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000316 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000317 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000318 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000319 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000320 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000321 if (!CI.getASTContext().getExternalSource())
322 goto failure;
323 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000324
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000325 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000326 if (!CI.hasASTConsumer())
327 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000328 }
329
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000330 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000331 // source.
332 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
333 Preprocessor &PP = CI.getPreprocessor();
334 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000335 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000336 }
337
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000338 // If there is a layout overrides file, attach an external AST source that
339 // provides the layouts from that file.
340 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
341 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000342 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000343 Override(new LayoutOverrideSource(
344 CI.getFrontendOpts().OverrideRecordLayoutsFile));
345 CI.getASTContext().setExternalSource(Override);
346 }
347
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000348 return true;
349
350 // If we failed, reset state since the client will not end up calling the
351 // matching EndSourceFile().
352 failure:
353 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000354 CI.setASTContext(0);
355 CI.setPreprocessor(0);
356 CI.setSourceManager(0);
357 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000358 }
359
Jordan Roseaf6cf432012-08-10 01:06:08 +0000360 if (HasBegunSourceFile)
361 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000362 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000363 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000364 setCompilerInstance(0);
365 return false;
366}
367
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000368bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000369 CompilerInstance &CI = getCompilerInstance();
370
371 // Initialize the main file entry. This needs to be delayed until after PCH
372 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000373 if (!isCurrentFileAST()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000374 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000375 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000376 }
377
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000378 if (CI.hasFrontendTimer()) {
379 llvm::TimeRegion Timer(CI.getFrontendTimer());
380 ExecuteAction();
381 }
382 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000383
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000384 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000385 // there were any module-build failures.
386 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
387 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000388 GlobalModuleIndex::writeIndex(
389 CI.getFileManager(),
390 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
391 }
392
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000393 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000394}
395
396void FrontendAction::EndSourceFile() {
397 CompilerInstance &CI = getCompilerInstance();
398
Douglas Gregor92b97f22011-02-09 18:47:31 +0000399 // Inform the diagnostic client we are done with this source file.
400 CI.getDiagnosticClient().EndSourceFile();
401
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000402 // Finalize the action.
403 EndSourceFileAction();
404
405 // Release the consumer and the AST, in that order since the consumer may
406 // perform actions in its destructor which require the context.
407 //
408 // FIXME: There is more per-file stuff we could just drop here?
409 if (CI.getFrontendOpts().DisableFree) {
410 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000411 if (!isCurrentFileAST()) {
412 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000413 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000414 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000415 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000416 if (!isCurrentFileAST()) {
417 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000418 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000419 }
420 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000421 }
422
Daniel Dunbardbd82092010-03-23 05:09:10 +0000423 // Inform the preprocessor we are done.
424 if (CI.hasPreprocessor())
425 CI.getPreprocessor().EndSourceFile();
426
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000427 if (CI.getFrontendOpts().ShowStats) {
428 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
429 CI.getPreprocessor().PrintStats();
430 CI.getPreprocessor().getIdentifierTable().PrintStats();
431 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
432 CI.getSourceManager().PrintStats();
433 llvm::errs() << "\n";
434 }
435
436 // Cleanup the output streams, and erase the output files if we encountered
437 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000438 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000439
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000440 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000441 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000442 CI.resetAndLeakASTContext();
443 CI.resetAndLeakPreprocessor();
444 CI.resetAndLeakSourceManager();
445 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000446 }
447
448 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000449 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000450}
451
452//===----------------------------------------------------------------------===//
453// Utility Actions
454//===----------------------------------------------------------------------===//
455
456void ASTFrontendAction::ExecuteAction() {
457 CompilerInstance &CI = getCompilerInstance();
458
459 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
460 // here so the source manager would be initialized.
461 if (hasCodeCompletionSupport() &&
462 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
463 CI.createCodeCompletionConsumer();
464
465 // Use a code completion consumer?
466 CodeCompleteConsumer *CompletionConsumer = 0;
467 if (CI.hasCodeCompletionConsumer())
468 CompletionConsumer = &CI.getCodeCompletionConsumer();
469
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000470 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000471 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000472
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000473 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
474 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000475}
476
David Blaikie99ba9e32011-12-20 02:48:34 +0000477void PluginASTAction::anchor() { }
478
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000479ASTConsumer *
480PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000481 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000482 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000483}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000484
485ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000487 return WrappedAction->CreateASTConsumer(CI, InFile);
488}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000489bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
490 return WrappedAction->BeginInvocation(CI);
491}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000492bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000493 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000494 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000495 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000496 return WrappedAction->BeginSourceFileAction(CI, Filename);
497}
498void WrapperFrontendAction::ExecuteAction() {
499 WrappedAction->ExecuteAction();
500}
501void WrapperFrontendAction::EndSourceFileAction() {
502 WrappedAction->EndSourceFileAction();
503}
504
505bool WrapperFrontendAction::usesPreprocessorOnly() const {
506 return WrappedAction->usesPreprocessorOnly();
507}
Douglas Gregor467dc882011-08-25 22:30:56 +0000508TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
509 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000510}
511bool WrapperFrontendAction::hasPCHSupport() const {
512 return WrappedAction->hasPCHSupport();
513}
514bool WrapperFrontendAction::hasASTFileSupport() const {
515 return WrappedAction->hasASTFileSupport();
516}
517bool WrapperFrontendAction::hasIRSupport() const {
518 return WrappedAction->hasIRSupport();
519}
520bool WrapperFrontendAction::hasCodeCompletionSupport() const {
521 return WrappedAction->hasCodeCompletionSupport();
522}
523
524WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
525 : WrappedAction(WrappedAction) {}
526