blob: c8e41def8e3e62d0c85adbfe049b83899510af9a [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());
Douglas Gregora8235d62012-10-09 23:05:51 +0000250 CI.getPreprocessor().setPPMutationListener(
251 Consumer->GetPPMutationListener());
252
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000253 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
254 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000255 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000256 source.reset(ChainedIncludesSource::create(CI));
257 if (!source)
258 goto failure;
259 CI.getASTContext().setExternalSource(source);
260
261 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
262 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000263 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000264 ASTDeserializationListener *DeserialListener =
265 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000266 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
267 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000268 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
269 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
270 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
271 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000272 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000273 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000274 CI.getPreprocessorOpts().DisablePCHValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000275 CI.getPreprocessorOpts().DisableStatCache,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000276 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000277 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000278 if (!CI.getASTContext().getExternalSource())
279 goto failure;
280 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000281
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000282 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000283 if (!CI.hasASTConsumer())
284 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000285 }
286
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000287 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000288 // source.
289 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
290 Preprocessor &PP = CI.getPreprocessor();
291 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000292 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000293 }
294
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000295 // If there is a layout overrides file, attach an external AST source that
296 // provides the layouts from that file.
297 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
298 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000299 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000300 Override(new LayoutOverrideSource(
301 CI.getFrontendOpts().OverrideRecordLayoutsFile));
302 CI.getASTContext().setExternalSource(Override);
303 }
304
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000305 return true;
306
307 // If we failed, reset state since the client will not end up calling the
308 // matching EndSourceFile().
309 failure:
310 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000311 CI.setASTContext(0);
312 CI.setPreprocessor(0);
313 CI.setSourceManager(0);
314 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000315 }
316
Jordan Roseaf6cf432012-08-10 01:06:08 +0000317 if (HasBegunSourceFile)
318 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000319 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000320 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000321 setCompilerInstance(0);
322 return false;
323}
324
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000325bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000326 CompilerInstance &CI = getCompilerInstance();
327
328 // Initialize the main file entry. This needs to be delayed until after PCH
329 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000330 if (!isCurrentFileAST()) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000331 if (!CI.InitializeSourceManager(getCurrentFile(),
332 getCurrentInput().IsSystem
333 ? SrcMgr::C_System
334 : SrcMgr::C_User))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000335 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000336 }
337
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000338 if (CI.hasFrontendTimer()) {
339 llvm::TimeRegion Timer(CI.getFrontendTimer());
340 ExecuteAction();
341 }
342 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000343
344 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000345}
346
347void FrontendAction::EndSourceFile() {
348 CompilerInstance &CI = getCompilerInstance();
349
Douglas Gregor92b97f22011-02-09 18:47:31 +0000350 // Inform the diagnostic client we are done with this source file.
351 CI.getDiagnosticClient().EndSourceFile();
352
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000353 // Finalize the action.
354 EndSourceFileAction();
355
356 // Release the consumer and the AST, in that order since the consumer may
357 // perform actions in its destructor which require the context.
358 //
359 // FIXME: There is more per-file stuff we could just drop here?
360 if (CI.getFrontendOpts().DisableFree) {
361 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000362 if (!isCurrentFileAST()) {
363 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000364 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000365 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000366 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000367 if (!isCurrentFileAST()) {
368 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000369 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000370 }
371 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000372 }
373
Daniel Dunbardbd82092010-03-23 05:09:10 +0000374 // Inform the preprocessor we are done.
375 if (CI.hasPreprocessor())
376 CI.getPreprocessor().EndSourceFile();
377
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000378 if (CI.getFrontendOpts().ShowStats) {
379 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
380 CI.getPreprocessor().PrintStats();
381 CI.getPreprocessor().getIdentifierTable().PrintStats();
382 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
383 CI.getSourceManager().PrintStats();
384 llvm::errs() << "\n";
385 }
386
387 // Cleanup the output streams, and erase the output files if we encountered
388 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000389 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000390
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000391 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000392 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000393 CI.resetAndLeakASTContext();
394 CI.resetAndLeakPreprocessor();
395 CI.resetAndLeakSourceManager();
396 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000397 }
398
399 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000400 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000401}
402
403//===----------------------------------------------------------------------===//
404// Utility Actions
405//===----------------------------------------------------------------------===//
406
407void ASTFrontendAction::ExecuteAction() {
408 CompilerInstance &CI = getCompilerInstance();
409
410 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
411 // here so the source manager would be initialized.
412 if (hasCodeCompletionSupport() &&
413 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
414 CI.createCodeCompletionConsumer();
415
416 // Use a code completion consumer?
417 CodeCompleteConsumer *CompletionConsumer = 0;
418 if (CI.hasCodeCompletionConsumer())
419 CompletionConsumer = &CI.getCodeCompletionConsumer();
420
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000421 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000422 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000423
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000424 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
425 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000426}
427
David Blaikie99ba9e32011-12-20 02:48:34 +0000428void PluginASTAction::anchor() { }
429
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000430ASTConsumer *
431PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000432 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000433 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000434}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000435
436ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000437 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000438 return WrappedAction->CreateASTConsumer(CI, InFile);
439}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000440bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
441 return WrappedAction->BeginInvocation(CI);
442}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000443bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000444 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000445 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000446 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000447 return WrappedAction->BeginSourceFileAction(CI, Filename);
448}
449void WrapperFrontendAction::ExecuteAction() {
450 WrappedAction->ExecuteAction();
451}
452void WrapperFrontendAction::EndSourceFileAction() {
453 WrappedAction->EndSourceFileAction();
454}
455
456bool WrapperFrontendAction::usesPreprocessorOnly() const {
457 return WrappedAction->usesPreprocessorOnly();
458}
Douglas Gregor467dc882011-08-25 22:30:56 +0000459TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
460 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000461}
462bool WrapperFrontendAction::hasPCHSupport() const {
463 return WrappedAction->hasPCHSupport();
464}
465bool WrapperFrontendAction::hasASTFileSupport() const {
466 return WrappedAction->hasASTFileSupport();
467}
468bool WrapperFrontendAction::hasIRSupport() const {
469 return WrappedAction->hasIRSupport();
470}
471bool WrapperFrontendAction::hasCodeCompletionSupport() const {
472 return WrappedAction->hasCodeCompletionSupport();
473}
474
475WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
476 : WrappedAction(WrappedAction) {}
477