blob: 075fe93d58416dc4ff6325d9582a330fd210f928 [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
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000191 // Inform the diagnostic client we are processing a source file.
192 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
193 HasBegunSourceFile = true;
194
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000195 // Set the shared objects, these are reset when we finish processing the
196 // file, otherwise the CompilerInstance will happily destroy them.
197 CI.setFileManager(&AST->getFileManager());
198 CI.setSourceManager(&AST->getSourceManager());
199 CI.setPreprocessor(&AST->getPreprocessor());
200 CI.setASTContext(&AST->getASTContext());
201
202 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000203 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000204 goto failure;
205
James Dennett18f43a62013-01-23 00:45:44 +0000206 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000207 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000208 if (!CI.hasASTConsumer())
209 goto failure;
210
211 return true;
212 }
213
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000214 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000215 if (!CI.hasFileManager())
216 CI.createFileManager();
217 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000218 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000219
220 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000221 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000222 assert(hasIRSupport() &&
223 "This action does not have IR file support!");
224
225 // Inform the diagnostic client we are processing a source file.
226 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000227 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000228
229 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000230 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000231 goto failure;
232
233 return true;
234 }
235
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000236 // If the implicit PCH include is actually a directory, rather than
237 // a single file, search for a suitable PCH file in that directory.
238 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
239 FileManager &FileMgr = CI.getFileManager();
240 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
241 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
242 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
243 llvm::error_code EC;
244 SmallString<128> DirNative;
245 llvm::sys::path::native(PCHDir->getName(), DirNative);
246 bool Found = false;
247 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
248 Dir != DirEnd && !EC; Dir.increment(EC)) {
249 // Check whether this is an acceptable AST file.
250 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
251 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000252 CI.getTargetOpts(),
253 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000254 PPOpts.ImplicitPCHInclude = Dir->path();
255 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000256 break;
257 }
258 }
259
260 if (!Found) {
261 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
262 return true;
263 }
264 }
265 }
266
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000267 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000268 CI.createPreprocessor();
269
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000270 // Inform the diagnostic client we are processing a source file.
271 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
272 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000273 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000274
275 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000276 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000277 goto failure;
278
James Dennett18f43a62013-01-23 00:45:44 +0000279 // Create the AST context and consumer unless this is a preprocessor only
280 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000281 if (!usesPreprocessorOnly()) {
282 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000283
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000284 OwningPtr<ASTConsumer> Consumer(
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000285 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000286 if (!Consumer)
287 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000288
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000289 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000290
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000291 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
292 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000293 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000294 source.reset(ChainedIncludesSource::create(CI));
295 if (!source)
296 goto failure;
Argyrios Kyrtzidiscbdbbd12013-04-26 21:33:27 +0000297 CI.setModuleManager(static_cast<ASTReader*>(
298 &static_cast<ChainedIncludesSource*>(source.get())->getFinalReader()));
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000299 CI.getASTContext().setExternalSource(source);
300
301 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
302 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000303 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000304 ASTDeserializationListener *DeserialListener =
305 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000306 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
307 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000308 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
309 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
310 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
311 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000312 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000313 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000314 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000315 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000316 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000317 if (!CI.getASTContext().getExternalSource())
318 goto failure;
319 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000320
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000321 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000322 if (!CI.hasASTConsumer())
323 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000324 }
325
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000326 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000327 // source.
328 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
329 Preprocessor &PP = CI.getPreprocessor();
330 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000331 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000332 }
333
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000334 // If there is a layout overrides file, attach an external AST source that
335 // provides the layouts from that file.
336 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
337 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000338 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000339 Override(new LayoutOverrideSource(
340 CI.getFrontendOpts().OverrideRecordLayoutsFile));
341 CI.getASTContext().setExternalSource(Override);
342 }
343
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000344 return true;
345
346 // If we failed, reset state since the client will not end up calling the
347 // matching EndSourceFile().
348 failure:
349 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000350 CI.setASTContext(0);
351 CI.setPreprocessor(0);
352 CI.setSourceManager(0);
353 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000354 }
355
Jordan Roseaf6cf432012-08-10 01:06:08 +0000356 if (HasBegunSourceFile)
357 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000358 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000359 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000360 setCompilerInstance(0);
361 return false;
362}
363
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000364bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000365 CompilerInstance &CI = getCompilerInstance();
366
367 // Initialize the main file entry. This needs to be delayed until after PCH
368 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000369 if (!isCurrentFileAST()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000370 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000371 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000372 }
373
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000374 if (CI.hasFrontendTimer()) {
375 llvm::TimeRegion Timer(CI.getFrontendTimer());
376 ExecuteAction();
377 }
378 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000379
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000380 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000381 // there were any module-build failures.
382 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
383 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000384 GlobalModuleIndex::writeIndex(
385 CI.getFileManager(),
386 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
387 }
388
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000389 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000390}
391
392void FrontendAction::EndSourceFile() {
393 CompilerInstance &CI = getCompilerInstance();
394
Douglas Gregor92b97f22011-02-09 18:47:31 +0000395 // Inform the diagnostic client we are done with this source file.
396 CI.getDiagnosticClient().EndSourceFile();
397
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000398 // Finalize the action.
399 EndSourceFileAction();
400
401 // Release the consumer and the AST, in that order since the consumer may
402 // perform actions in its destructor which require the context.
403 //
404 // FIXME: There is more per-file stuff we could just drop here?
405 if (CI.getFrontendOpts().DisableFree) {
406 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000407 if (!isCurrentFileAST()) {
408 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000409 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000410 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000411 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000412 if (!isCurrentFileAST()) {
413 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000414 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000415 }
416 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000417 }
418
Daniel Dunbardbd82092010-03-23 05:09:10 +0000419 // Inform the preprocessor we are done.
420 if (CI.hasPreprocessor())
421 CI.getPreprocessor().EndSourceFile();
422
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000423 if (CI.getFrontendOpts().ShowStats) {
424 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
425 CI.getPreprocessor().PrintStats();
426 CI.getPreprocessor().getIdentifierTable().PrintStats();
427 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
428 CI.getSourceManager().PrintStats();
429 llvm::errs() << "\n";
430 }
431
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000432 // Cleanup the output streams, and erase the output files if instructed by the
433 // FrontendAction.
434 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000435
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000436 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000437 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000438 CI.resetAndLeakASTContext();
439 CI.resetAndLeakPreprocessor();
440 CI.resetAndLeakSourceManager();
441 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000442 }
443
444 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000445 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000446}
447
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000448bool FrontendAction::shouldEraseOutputFiles() {
449 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
450}
451
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000452//===----------------------------------------------------------------------===//
453// Utility Actions
454//===----------------------------------------------------------------------===//
455
456void ASTFrontendAction::ExecuteAction() {
457 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola0046ce52013-07-28 13:23:37 +0000458 if (!CI.hasPreprocessor())
459 return;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000460
461 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
462 // here so the source manager would be initialized.
463 if (hasCodeCompletionSupport() &&
464 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
465 CI.createCodeCompletionConsumer();
466
467 // Use a code completion consumer?
468 CodeCompleteConsumer *CompletionConsumer = 0;
469 if (CI.hasCodeCompletionConsumer())
470 CompletionConsumer = &CI.getCodeCompletionConsumer();
471
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000472 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000473 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000474
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000475 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
476 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000477}
478
David Blaikie99ba9e32011-12-20 02:48:34 +0000479void PluginASTAction::anchor() { }
480
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000481ASTConsumer *
482PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000483 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000484 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000485}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000486
487ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000488 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000489 return WrappedAction->CreateASTConsumer(CI, InFile);
490}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000491bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
492 return WrappedAction->BeginInvocation(CI);
493}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000494bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000495 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000496 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000497 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000498 return WrappedAction->BeginSourceFileAction(CI, Filename);
499}
500void WrapperFrontendAction::ExecuteAction() {
501 WrappedAction->ExecuteAction();
502}
503void WrapperFrontendAction::EndSourceFileAction() {
504 WrappedAction->EndSourceFileAction();
505}
506
507bool WrapperFrontendAction::usesPreprocessorOnly() const {
508 return WrappedAction->usesPreprocessorOnly();
509}
Douglas Gregor467dc882011-08-25 22:30:56 +0000510TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
511 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000512}
513bool WrapperFrontendAction::hasPCHSupport() const {
514 return WrappedAction->hasPCHSupport();
515}
516bool WrapperFrontendAction::hasASTFileSupport() const {
517 return WrappedAction->hasASTFileSupport();
518}
519bool WrapperFrontendAction::hasIRSupport() const {
520 return WrappedAction->hasIRSupport();
521}
522bool WrapperFrontendAction::hasCodeCompletionSupport() const {
523 return WrappedAction->hasCodeCompletionSupport();
524}
525
526WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
527 : WrappedAction(WrappedAction) {}
528