blob: 6031ad2b361b637ab5ad142424461c7efbca9dca [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;
297 CI.getASTContext().setExternalSource(source);
298
299 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
300 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000301 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000302 ASTDeserializationListener *DeserialListener =
303 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000304 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
305 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000306 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
307 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
308 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
309 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000310 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000311 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000312 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000313 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000314 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000315 if (!CI.getASTContext().getExternalSource())
316 goto failure;
317 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000318
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000319 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000320 if (!CI.hasASTConsumer())
321 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000322 }
323
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000324 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000325 // source.
326 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
327 Preprocessor &PP = CI.getPreprocessor();
328 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000329 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000330 }
331
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000332 // If there is a layout overrides file, attach an external AST source that
333 // provides the layouts from that file.
334 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
335 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000336 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000337 Override(new LayoutOverrideSource(
338 CI.getFrontendOpts().OverrideRecordLayoutsFile));
339 CI.getASTContext().setExternalSource(Override);
340 }
341
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000342 return true;
343
344 // If we failed, reset state since the client will not end up calling the
345 // matching EndSourceFile().
346 failure:
347 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000348 CI.setASTContext(0);
349 CI.setPreprocessor(0);
350 CI.setSourceManager(0);
351 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000352 }
353
Jordan Roseaf6cf432012-08-10 01:06:08 +0000354 if (HasBegunSourceFile)
355 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000356 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000357 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000358 setCompilerInstance(0);
359 return false;
360}
361
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000362bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000363 CompilerInstance &CI = getCompilerInstance();
364
365 // Initialize the main file entry. This needs to be delayed until after PCH
366 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000367 if (!isCurrentFileAST()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000368 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000369 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000370 }
371
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000372 if (CI.hasFrontendTimer()) {
373 llvm::TimeRegion Timer(CI.getFrontendTimer());
374 ExecuteAction();
375 }
376 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000377
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000378 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000379 // there were any module-build failures.
380 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
381 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000382 GlobalModuleIndex::writeIndex(
383 CI.getFileManager(),
384 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
385 }
386
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000387 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000388}
389
390void FrontendAction::EndSourceFile() {
391 CompilerInstance &CI = getCompilerInstance();
392
Douglas Gregor92b97f22011-02-09 18:47:31 +0000393 // Inform the diagnostic client we are done with this source file.
394 CI.getDiagnosticClient().EndSourceFile();
395
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000396 // Finalize the action.
397 EndSourceFileAction();
398
399 // Release the consumer and the AST, in that order since the consumer may
400 // perform actions in its destructor which require the context.
401 //
402 // FIXME: There is more per-file stuff we could just drop here?
403 if (CI.getFrontendOpts().DisableFree) {
404 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000405 if (!isCurrentFileAST()) {
406 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000407 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000408 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000409 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000410 if (!isCurrentFileAST()) {
411 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000412 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000413 }
414 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000415 }
416
Daniel Dunbardbd82092010-03-23 05:09:10 +0000417 // Inform the preprocessor we are done.
418 if (CI.hasPreprocessor())
419 CI.getPreprocessor().EndSourceFile();
420
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000421 if (CI.getFrontendOpts().ShowStats) {
422 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
423 CI.getPreprocessor().PrintStats();
424 CI.getPreprocessor().getIdentifierTable().PrintStats();
425 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
426 CI.getSourceManager().PrintStats();
427 llvm::errs() << "\n";
428 }
429
430 // Cleanup the output streams, and erase the output files if we encountered
431 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000432 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000433
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000434 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000435 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000436 CI.resetAndLeakASTContext();
437 CI.resetAndLeakPreprocessor();
438 CI.resetAndLeakSourceManager();
439 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000440 }
441
442 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000443 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000444}
445
446//===----------------------------------------------------------------------===//
447// Utility Actions
448//===----------------------------------------------------------------------===//
449
450void ASTFrontendAction::ExecuteAction() {
451 CompilerInstance &CI = getCompilerInstance();
452
453 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
454 // here so the source manager would be initialized.
455 if (hasCodeCompletionSupport() &&
456 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
457 CI.createCodeCompletionConsumer();
458
459 // Use a code completion consumer?
460 CodeCompleteConsumer *CompletionConsumer = 0;
461 if (CI.hasCodeCompletionConsumer())
462 CompletionConsumer = &CI.getCodeCompletionConsumer();
463
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000464 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000465 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000466
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000467 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
468 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000469}
470
David Blaikie99ba9e32011-12-20 02:48:34 +0000471void PluginASTAction::anchor() { }
472
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000473ASTConsumer *
474PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000475 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000476 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000477}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000478
479ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000480 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000481 return WrappedAction->CreateASTConsumer(CI, InFile);
482}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000483bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
484 return WrappedAction->BeginInvocation(CI);
485}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000486bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000487 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000488 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000489 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000490 return WrappedAction->BeginSourceFileAction(CI, Filename);
491}
492void WrapperFrontendAction::ExecuteAction() {
493 WrappedAction->ExecuteAction();
494}
495void WrapperFrontendAction::EndSourceFileAction() {
496 WrappedAction->EndSourceFileAction();
497}
498
499bool WrapperFrontendAction::usesPreprocessorOnly() const {
500 return WrappedAction->usesPreprocessorOnly();
501}
Douglas Gregor467dc882011-08-25 22:30:56 +0000502TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
503 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000504}
505bool WrapperFrontendAction::hasPCHSupport() const {
506 return WrappedAction->hasPCHSupport();
507}
508bool WrapperFrontendAction::hasASTFileSupport() const {
509 return WrappedAction->hasASTFileSupport();
510}
511bool WrapperFrontendAction::hasIRSupport() const {
512 return WrappedAction->hasIRSupport();
513}
514bool WrapperFrontendAction::hasCodeCompletionSupport() const {
515 return WrappedAction->hasCodeCompletionSupport();
516}
517
518WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
519 : WrappedAction(WrappedAction) {}
520