blob: da4bdfabc067276a200063a1cee0a792a797fa7b [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
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000086 /// \brief Checks deserialized declarations and emits error if a name
87 /// matches one given in command-line using -error-on-deserialized-decl.
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000088 class DeserializedDeclsChecker : public DelegatingDeserializationListener {
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000089 ASTContext &Ctx;
90 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000091
92 public:
93 DeserializedDeclsChecker(ASTContext &Ctx,
94 const std::set<std::string> &NamesToCheck,
95 ASTDeserializationListener *Previous)
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000096 : DelegatingDeserializationListener(Previous),
97 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000098
99 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
David Blaikied6471f72011-09-25 23:23:43 +0000103 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000104 "%0 was deserialized");
105 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
106 << ND->getNameAsString();
107 }
108
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +0000109 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000110 }
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
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000165 if (!BeginInvocation(CI))
166 goto failure;
167
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000168 // AST files follow a very different path, since they share objects via the
169 // AST unit.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000170 if (Input.Kind == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000171 assert(!usesPreprocessorOnly() &&
172 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000173 assert(hasASTFileSupport() &&
174 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000175
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000176 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000177 std::string Error;
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000178 ASTUnit *AST = ASTUnit::LoadFromASTFile(Input.File, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000179 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000180 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000181 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000182
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000183 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000184
185 // Set the shared objects, these are reset when we finish processing the
186 // file, otherwise the CompilerInstance will happily destroy them.
187 CI.setFileManager(&AST->getFileManager());
188 CI.setSourceManager(&AST->getSourceManager());
189 CI.setPreprocessor(&AST->getPreprocessor());
190 CI.setASTContext(&AST->getASTContext());
191
192 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000193 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000194 goto failure;
195
196 /// Create the AST consumer.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000197 CI.setASTConsumer(CreateWrappedASTConsumer(CI, Input.File));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000198 if (!CI.hasASTConsumer())
199 goto failure;
200
201 return true;
202 }
203
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000204 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000205 if (!CI.hasFileManager())
206 CI.createFileManager();
207 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000208 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000209
210 // IR files bypass the rest of initialization.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000211 if (Input.Kind == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000212 assert(hasIRSupport() &&
213 "This action does not have IR file support!");
214
215 // Inform the diagnostic client we are processing a source file.
216 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
217
218 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000219 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000220 goto failure;
221
222 return true;
223 }
224
225 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000226 CI.createPreprocessor();
227
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000228 // Inform the diagnostic client we are processing a source file.
229 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
230 &CI.getPreprocessor());
231
232 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000233 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000234 goto failure;
235
236 /// Create the AST context and consumer unless this is a preprocessor only
237 /// action.
238 if (!usesPreprocessorOnly()) {
239 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000240
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000241 OwningPtr<ASTConsumer> Consumer(
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000242 CreateWrappedASTConsumer(CI, Input.File));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000243 if (!Consumer)
244 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000245
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000246 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
247
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000248 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
249 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000250 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000251 source.reset(ChainedIncludesSource::create(CI));
252 if (!source)
253 goto failure;
254 CI.getASTContext().setExternalSource(source);
255
256 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
257 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000258 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000259 ASTDeserializationListener *DeserialListener =
260 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000261 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
262 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000263 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
264 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
265 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
266 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000267 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000268 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000269 CI.getPreprocessorOpts().DisablePCHValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000270 CI.getPreprocessorOpts().DisableStatCache,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000271 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000272 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000273 if (!CI.getASTContext().getExternalSource())
274 goto failure;
275 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000276
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000277 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000278 if (!CI.hasASTConsumer())
279 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000280 }
281
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000282 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000283 // source.
284 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
285 Preprocessor &PP = CI.getPreprocessor();
286 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000287 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000288 }
289
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000290 // If there is a layout overrides file, attach an external AST source that
291 // provides the layouts from that file.
292 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
293 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000294 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000295 Override(new LayoutOverrideSource(
296 CI.getFrontendOpts().OverrideRecordLayoutsFile));
297 CI.getASTContext().setExternalSource(Override);
298 }
299
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000300 return true;
301
302 // If we failed, reset state since the client will not end up calling the
303 // matching EndSourceFile().
304 failure:
305 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000306 CI.setASTContext(0);
307 CI.setPreprocessor(0);
308 CI.setSourceManager(0);
309 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000310 }
311
312 CI.getDiagnosticClient().EndSourceFile();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000313 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000314 setCompilerInstance(0);
315 return false;
316}
317
318void FrontendAction::Execute() {
319 CompilerInstance &CI = getCompilerInstance();
320
321 // Initialize the main file entry. This needs to be delayed until after PCH
322 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000323 if (!isCurrentFileAST()) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000324 if (!CI.InitializeSourceManager(getCurrentFile(),
325 getCurrentInput().IsSystem
326 ? SrcMgr::C_System
327 : SrcMgr::C_User))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000328 return;
329 }
330
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000331 if (CI.hasFrontendTimer()) {
332 llvm::TimeRegion Timer(CI.getFrontendTimer());
333 ExecuteAction();
334 }
335 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000336}
337
338void FrontendAction::EndSourceFile() {
339 CompilerInstance &CI = getCompilerInstance();
340
Douglas Gregor92b97f22011-02-09 18:47:31 +0000341 // Inform the diagnostic client we are done with this source file.
342 CI.getDiagnosticClient().EndSourceFile();
343
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000344 // Finalize the action.
345 EndSourceFileAction();
346
347 // Release the consumer and the AST, in that order since the consumer may
348 // perform actions in its destructor which require the context.
349 //
350 // FIXME: There is more per-file stuff we could just drop here?
351 if (CI.getFrontendOpts().DisableFree) {
352 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000353 if (!isCurrentFileAST()) {
354 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000355 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000356 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000357 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000358 if (!isCurrentFileAST()) {
359 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000360 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000361 }
362 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000363 }
364
Daniel Dunbardbd82092010-03-23 05:09:10 +0000365 // Inform the preprocessor we are done.
366 if (CI.hasPreprocessor())
367 CI.getPreprocessor().EndSourceFile();
368
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000369 if (CI.getFrontendOpts().ShowStats) {
370 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
371 CI.getPreprocessor().PrintStats();
372 CI.getPreprocessor().getIdentifierTable().PrintStats();
373 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
374 CI.getSourceManager().PrintStats();
375 llvm::errs() << "\n";
376 }
377
378 // Cleanup the output streams, and erase the output files if we encountered
379 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000380 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000381
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000382 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000383 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000384 CI.resetAndLeakASTContext();
385 CI.resetAndLeakPreprocessor();
386 CI.resetAndLeakSourceManager();
387 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000388 }
389
390 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000391 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000392}
393
394//===----------------------------------------------------------------------===//
395// Utility Actions
396//===----------------------------------------------------------------------===//
397
398void ASTFrontendAction::ExecuteAction() {
399 CompilerInstance &CI = getCompilerInstance();
400
401 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
402 // here so the source manager would be initialized.
403 if (hasCodeCompletionSupport() &&
404 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
405 CI.createCodeCompletionConsumer();
406
407 // Use a code completion consumer?
408 CodeCompleteConsumer *CompletionConsumer = 0;
409 if (CI.hasCodeCompletionConsumer())
410 CompletionConsumer = &CI.getCodeCompletionConsumer();
411
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000412 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000413 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000414
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000415 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
416 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000417}
418
David Blaikie99ba9e32011-12-20 02:48:34 +0000419void PluginASTAction::anchor() { }
420
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000421ASTConsumer *
422PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000423 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000424 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000425}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000426
427ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000428 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000429 return WrappedAction->CreateASTConsumer(CI, InFile);
430}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000431bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
432 return WrappedAction->BeginInvocation(CI);
433}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000434bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000435 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000436 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000437 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000438 return WrappedAction->BeginSourceFileAction(CI, Filename);
439}
440void WrapperFrontendAction::ExecuteAction() {
441 WrappedAction->ExecuteAction();
442}
443void WrapperFrontendAction::EndSourceFileAction() {
444 WrappedAction->EndSourceFileAction();
445}
446
447bool WrapperFrontendAction::usesPreprocessorOnly() const {
448 return WrappedAction->usesPreprocessorOnly();
449}
Douglas Gregor467dc882011-08-25 22:30:56 +0000450TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
451 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000452}
453bool WrapperFrontendAction::hasPCHSupport() const {
454 return WrappedAction->hasPCHSupport();
455}
456bool WrapperFrontendAction::hasASTFileSupport() const {
457 return WrappedAction->hasASTFileSupport();
458}
459bool WrapperFrontendAction::hasIRSupport() const {
460 return WrappedAction->hasIRSupport();
461}
462bool WrapperFrontendAction::hasCodeCompletionSupport() const {
463 return WrappedAction->hasCodeCompletionSupport();
464}
465
466WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
467 : WrappedAction(WrappedAction) {}
468