blob: a4321e720ed0460f9d190f374487b6e7f924f2d3 [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/Lex/HeaderSearch.h"
15#include "clang/Lex/Preprocessor.h"
16#include "clang/Frontend/ASTUnit.h"
Chandler Carruth71088d12011-12-09 01:55:54 +000017#include "clang/Frontend/ChainedIncludesSource.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000018#include "clang/Frontend/CompilerInstance.h"
19#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000020#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregor453dbcb2012-01-26 07:55:45 +000021#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000022#include "clang/Frontend/MultiplexConsumer.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"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000026#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/Timer.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace clang;
31
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000032namespace {
33
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000034class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000035 ASTDeserializationListener *Previous;
36
37public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000038 explicit DelegatingDeserializationListener(
39 ASTDeserializationListener *Previous)
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000040 : Previous(Previous) { }
41
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000042 virtual void ReaderInitialized(ASTReader *Reader) {
43 if (Previous)
44 Previous->ReaderInitialized(Reader);
45 }
46 virtual void IdentifierRead(serialization::IdentID ID,
47 IdentifierInfo *II) {
48 if (Previous)
49 Previous->IdentifierRead(ID, II);
50 }
51 virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
52 if (Previous)
53 Previous->TypeRead(Idx, T);
54 }
55 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
56 if (Previous)
57 Previous->DeclRead(ID, D);
58 }
59 virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
60 if (Previous)
61 Previous->SelectorRead(ID, Sel);
62 }
63 virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
64 MacroDefinition *MD) {
65 if (Previous)
66 Previous->MacroDefinitionRead(PPID, MD);
67 }
68};
69
70/// \brief Dumps deserialized declarations.
71class DeserializedDeclsDumper : public DelegatingDeserializationListener {
72public:
73 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
74 : DelegatingDeserializationListener(Previous) { }
75
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000076 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
77 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
78 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000079 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000080 llvm::outs() << "\n";
81
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000082 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000083 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000084};
85
David Blaikiee3f34112012-05-29 17:05:42 +000086/// \brief Checks deserialized declarations and emits error if a name
87/// matches one given in command-line using -error-on-deserialized-decl.
88class DeserializedDeclsChecker : public DelegatingDeserializationListener {
89 ASTContext &Ctx;
90 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000091
David Blaikiee3f34112012-05-29 17:05:42 +000092public:
93 DeserializedDeclsChecker(ASTContext &Ctx,
94 const std::set<std::string> &NamesToCheck,
95 ASTDeserializationListener *Previous)
96 : DelegatingDeserializationListener(Previous),
97 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000098
David Blaikiee3f34112012-05-29 17:05:42 +000099 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
100 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
101 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
102 unsigned DiagID
103 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
104 "%0 was deserialized");
105 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
106 << ND->getNameAsString();
107 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000108
David Blaikiee3f34112012-05-29 17:05:42 +0000109 DelegatingDeserializationListener::DeclRead(ID, D);
110 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000111};
112
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000113} // end anonymous namespace
114
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000115FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000116
117FrontendAction::~FrontendAction() {}
118
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000119void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
120 ASTUnit *AST) {
121 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000122 CurrentASTUnit.reset(AST);
123}
124
Nico Weber5aa74af2011-01-25 20:34:14 +0000125ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000126 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000127 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
128 if (!Consumer)
129 return 0;
130
131 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
132 return Consumer;
133
134 // Make sure the non-plugin consumer is first, so that plugins can't
135 // modifiy the AST.
136 std::vector<ASTConsumer*> Consumers(1, Consumer);
137
138 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
139 i != e; ++i) {
140 // This is O(|plugins| * |add_plugins|), but since both numbers are
141 // way below 50 in practice, that's ok.
142 for (FrontendPluginRegistry::iterator
143 it = FrontendPluginRegistry::begin(),
144 ie = FrontendPluginRegistry::end();
145 it != ie; ++it) {
146 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000147 OwningPtr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000148 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000149 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000150 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
151 }
152 }
153 }
154
155 return new MultiplexConsumer(Consumers);
156}
157
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000158bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000159 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000160 assert(!Instance && "Already processing a source file!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000161 assert(!Input.File.empty() && "Unexpected empty filename!");
162 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000163 setCompilerInstance(&CI);
164
Jordan Roseaf6cf432012-08-10 01:06:08 +0000165 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000166 if (!BeginInvocation(CI))
167 goto failure;
168
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000169 // AST files follow a very different path, since they share objects via the
170 // AST unit.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000171 if (Input.Kind == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000172 assert(!usesPreprocessorOnly() &&
173 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000174 assert(hasASTFileSupport() &&
175 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000176
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000177 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000178 std::string Error;
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000179 ASTUnit *AST = ASTUnit::LoadFromASTFile(Input.File, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000180 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000181 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000182 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000183
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000184 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000185
186 // Set the shared objects, these are reset when we finish processing the
187 // file, otherwise the CompilerInstance will happily destroy them.
188 CI.setFileManager(&AST->getFileManager());
189 CI.setSourceManager(&AST->getSourceManager());
190 CI.setPreprocessor(&AST->getPreprocessor());
191 CI.setASTContext(&AST->getASTContext());
192
193 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000194 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000195 goto failure;
196
197 /// Create the AST consumer.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000198 CI.setASTConsumer(CreateWrappedASTConsumer(CI, Input.File));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000199 if (!CI.hasASTConsumer())
200 goto failure;
201
202 return true;
203 }
204
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000205 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000206 if (!CI.hasFileManager())
207 CI.createFileManager();
208 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000209 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000210
211 // IR files bypass the rest of initialization.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000212 if (Input.Kind == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000213 assert(hasIRSupport() &&
214 "This action does not have IR file support!");
215
216 // Inform the diagnostic client we are processing a source file.
217 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000218 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000219
220 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000221 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000222 goto failure;
223
224 return true;
225 }
226
227 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000228 CI.createPreprocessor();
229
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000230 // Inform the diagnostic client we are processing a source file.
231 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
232 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000233 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000234
235 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000236 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000237 goto failure;
238
239 /// Create the AST context and consumer unless this is a preprocessor only
240 /// action.
241 if (!usesPreprocessorOnly()) {
242 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000243
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000244 OwningPtr<ASTConsumer> Consumer(
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000245 CreateWrappedASTConsumer(CI, Input.File));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000246 if (!Consumer)
247 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000248
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000249 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
250
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000251 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
252 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000253 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000254 source.reset(ChainedIncludesSource::create(CI));
255 if (!source)
256 goto failure;
257 CI.getASTContext().setExternalSource(source);
258
259 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
260 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000261 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000262 ASTDeserializationListener *DeserialListener =
263 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000264 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
265 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000266 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
267 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
268 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
269 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000270 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000271 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000272 CI.getPreprocessorOpts().DisablePCHValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000273 CI.getPreprocessorOpts().DisableStatCache,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000274 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000275 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000276 if (!CI.getASTContext().getExternalSource())
277 goto failure;
278 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000279
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000280 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000281 if (!CI.hasASTConsumer())
282 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000283 }
284
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000285 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000286 // source.
287 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
288 Preprocessor &PP = CI.getPreprocessor();
289 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000290 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000291 }
292
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000293 // If there is a layout overrides file, attach an external AST source that
294 // provides the layouts from that file.
295 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
296 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000297 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000298 Override(new LayoutOverrideSource(
299 CI.getFrontendOpts().OverrideRecordLayoutsFile));
300 CI.getASTContext().setExternalSource(Override);
301 }
302
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000303 return true;
304
305 // If we failed, reset state since the client will not end up calling the
306 // matching EndSourceFile().
307 failure:
308 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000309 CI.setASTContext(0);
310 CI.setPreprocessor(0);
311 CI.setSourceManager(0);
312 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000313 }
314
Jordan Roseaf6cf432012-08-10 01:06:08 +0000315 if (HasBegunSourceFile)
316 CI.getDiagnosticClient().EndSourceFile();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000317 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000318 setCompilerInstance(0);
319 return false;
320}
321
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000322bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000323 CompilerInstance &CI = getCompilerInstance();
324
325 // Initialize the main file entry. This needs to be delayed until after PCH
326 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000327 if (!isCurrentFileAST()) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000328 if (!CI.InitializeSourceManager(getCurrentFile(),
329 getCurrentInput().IsSystem
330 ? SrcMgr::C_System
331 : SrcMgr::C_User))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000332 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000333 }
334
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000335 if (CI.hasFrontendTimer()) {
336 llvm::TimeRegion Timer(CI.getFrontendTimer());
337 ExecuteAction();
338 }
339 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000340
341 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000342}
343
344void FrontendAction::EndSourceFile() {
345 CompilerInstance &CI = getCompilerInstance();
346
Douglas Gregor92b97f22011-02-09 18:47:31 +0000347 // Inform the diagnostic client we are done with this source file.
348 CI.getDiagnosticClient().EndSourceFile();
349
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000350 // Finalize the action.
351 EndSourceFileAction();
352
353 // Release the consumer and the AST, in that order since the consumer may
354 // perform actions in its destructor which require the context.
355 //
356 // FIXME: There is more per-file stuff we could just drop here?
357 if (CI.getFrontendOpts().DisableFree) {
358 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000359 if (!isCurrentFileAST()) {
360 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000361 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000362 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000363 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000364 if (!isCurrentFileAST()) {
365 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000366 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000367 }
368 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000369 }
370
Daniel Dunbardbd82092010-03-23 05:09:10 +0000371 // Inform the preprocessor we are done.
372 if (CI.hasPreprocessor())
373 CI.getPreprocessor().EndSourceFile();
374
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000375 if (CI.getFrontendOpts().ShowStats) {
376 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
377 CI.getPreprocessor().PrintStats();
378 CI.getPreprocessor().getIdentifierTable().PrintStats();
379 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
380 CI.getSourceManager().PrintStats();
381 llvm::errs() << "\n";
382 }
383
384 // Cleanup the output streams, and erase the output files if we encountered
385 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000386 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000387
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000388 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000389 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000390 CI.resetAndLeakASTContext();
391 CI.resetAndLeakPreprocessor();
392 CI.resetAndLeakSourceManager();
393 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000394 }
395
396 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000397 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000398}
399
400//===----------------------------------------------------------------------===//
401// Utility Actions
402//===----------------------------------------------------------------------===//
403
404void ASTFrontendAction::ExecuteAction() {
405 CompilerInstance &CI = getCompilerInstance();
406
407 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
408 // here so the source manager would be initialized.
409 if (hasCodeCompletionSupport() &&
410 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
411 CI.createCodeCompletionConsumer();
412
413 // Use a code completion consumer?
414 CodeCompleteConsumer *CompletionConsumer = 0;
415 if (CI.hasCodeCompletionConsumer())
416 CompletionConsumer = &CI.getCodeCompletionConsumer();
417
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000418 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000419 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000420
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000421 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
422 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000423}
424
David Blaikie99ba9e32011-12-20 02:48:34 +0000425void PluginASTAction::anchor() { }
426
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000427ASTConsumer *
428PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000429 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000430 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000431}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000432
433ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000434 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000435 return WrappedAction->CreateASTConsumer(CI, InFile);
436}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000437bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
438 return WrappedAction->BeginInvocation(CI);
439}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000440bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000441 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000442 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000443 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000444 return WrappedAction->BeginSourceFileAction(CI, Filename);
445}
446void WrapperFrontendAction::ExecuteAction() {
447 WrappedAction->ExecuteAction();
448}
449void WrapperFrontendAction::EndSourceFileAction() {
450 WrappedAction->EndSourceFileAction();
451}
452
453bool WrapperFrontendAction::usesPreprocessorOnly() const {
454 return WrappedAction->usesPreprocessorOnly();
455}
Douglas Gregor467dc882011-08-25 22:30:56 +0000456TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
457 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000458}
459bool WrapperFrontendAction::hasPCHSupport() const {
460 return WrappedAction->hasPCHSupport();
461}
462bool WrapperFrontendAction::hasASTFileSupport() const {
463 return WrappedAction->hasASTFileSupport();
464}
465bool WrapperFrontendAction::hasIRSupport() const {
466 return WrappedAction->hasIRSupport();
467}
468bool WrapperFrontendAction::hasCodeCompletionSupport() const {
469 return WrappedAction->hasCodeCompletionSupport();
470}
471
472WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
473 : WrappedAction(WrappedAction) {}
474