blob: 5a15847aea0cbbdd698d23a4c29fc46b4c30b6a1 [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))
79 llvm::outs() << " - " << ND->getNameAsString();
80 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]) {
147 llvm::OwningPtr<PluginASTAction> P(it->instantiate());
148 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
David Blaikied6471f72011-09-25 23:23:43 +0000176 llvm::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
Nico Weber5aa74af2011-01-25 20:34:14 +0000241 llvm::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.
250 llvm::OwningPtr<ExternalASTSource> source;
251 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 Kyrtzidisb9728582010-10-14 20:14:18 +0000271 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000272 if (!CI.getASTContext().getExternalSource())
273 goto failure;
274 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000275
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000276 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000277 if (!CI.hasASTConsumer())
278 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000279 }
280
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000281 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000282 // source.
283 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
284 Preprocessor &PP = CI.getPreprocessor();
285 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
Fariborz Jahanian67aba812010-11-30 17:35:24 +0000286 PP.getLangOptions());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000287 }
288
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000289 // If there is a layout overrides file, attach an external AST source that
290 // provides the layouts from that file.
291 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
292 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
293 llvm::OwningPtr<ExternalASTSource>
294 Override(new LayoutOverrideSource(
295 CI.getFrontendOpts().OverrideRecordLayoutsFile));
296 CI.getASTContext().setExternalSource(Override);
297 }
298
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000299 return true;
300
301 // If we failed, reset state since the client will not end up calling the
302 // matching EndSourceFile().
303 failure:
304 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000305 CI.setASTContext(0);
306 CI.setPreprocessor(0);
307 CI.setSourceManager(0);
308 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000309 }
310
311 CI.getDiagnosticClient().EndSourceFile();
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000312 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000313 setCompilerInstance(0);
314 return false;
315}
316
317void FrontendAction::Execute() {
318 CompilerInstance &CI = getCompilerInstance();
319
320 // Initialize the main file entry. This needs to be delayed until after PCH
321 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000322 if (!isCurrentFileAST()) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000323 if (!CI.InitializeSourceManager(getCurrentFile()))
324 return;
325 }
326
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000327 if (CI.hasFrontendTimer()) {
328 llvm::TimeRegion Timer(CI.getFrontendTimer());
329 ExecuteAction();
330 }
331 else ExecuteAction();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000332}
333
334void FrontendAction::EndSourceFile() {
335 CompilerInstance &CI = getCompilerInstance();
336
Douglas Gregor92b97f22011-02-09 18:47:31 +0000337 // Inform the diagnostic client we are done with this source file.
338 CI.getDiagnosticClient().EndSourceFile();
339
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000340 // Finalize the action.
341 EndSourceFileAction();
342
343 // Release the consumer and the AST, in that order since the consumer may
344 // perform actions in its destructor which require the context.
345 //
346 // FIXME: There is more per-file stuff we could just drop here?
347 if (CI.getFrontendOpts().DisableFree) {
348 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000349 if (!isCurrentFileAST()) {
350 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000351 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000352 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000353 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000354 if (!isCurrentFileAST()) {
355 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000356 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000357 }
358 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000359 }
360
Daniel Dunbardbd82092010-03-23 05:09:10 +0000361 // Inform the preprocessor we are done.
362 if (CI.hasPreprocessor())
363 CI.getPreprocessor().EndSourceFile();
364
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000365 if (CI.getFrontendOpts().ShowStats) {
366 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
367 CI.getPreprocessor().PrintStats();
368 CI.getPreprocessor().getIdentifierTable().PrintStats();
369 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
370 CI.getSourceManager().PrintStats();
371 llvm::errs() << "\n";
372 }
373
374 // Cleanup the output streams, and erase the output files if we encountered
375 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000376 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000377
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000378 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000379 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000380 CI.resetAndLeakASTContext();
381 CI.resetAndLeakPreprocessor();
382 CI.resetAndLeakSourceManager();
383 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000384 }
385
386 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000387 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000388}
389
390//===----------------------------------------------------------------------===//
391// Utility Actions
392//===----------------------------------------------------------------------===//
393
394void ASTFrontendAction::ExecuteAction() {
395 CompilerInstance &CI = getCompilerInstance();
396
397 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
398 // here so the source manager would be initialized.
399 if (hasCodeCompletionSupport() &&
400 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
401 CI.createCodeCompletionConsumer();
402
403 // Use a code completion consumer?
404 CodeCompleteConsumer *CompletionConsumer = 0;
405 if (CI.hasCodeCompletionConsumer())
406 CompletionConsumer = &CI.getCodeCompletionConsumer();
407
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000408 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000409 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000410
411 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000412}
413
David Blaikie99ba9e32011-12-20 02:48:34 +0000414void PluginASTAction::anchor() { }
415
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000416ASTConsumer *
417PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000418 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000419 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000420}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000421
422ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000423 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000424 return WrappedAction->CreateASTConsumer(CI, InFile);
425}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000426bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
427 return WrappedAction->BeginInvocation(CI);
428}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000429bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000430 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000431 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000432 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000433 return WrappedAction->BeginSourceFileAction(CI, Filename);
434}
435void WrapperFrontendAction::ExecuteAction() {
436 WrappedAction->ExecuteAction();
437}
438void WrapperFrontendAction::EndSourceFileAction() {
439 WrappedAction->EndSourceFileAction();
440}
441
442bool WrapperFrontendAction::usesPreprocessorOnly() const {
443 return WrappedAction->usesPreprocessorOnly();
444}
Douglas Gregor467dc882011-08-25 22:30:56 +0000445TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
446 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000447}
448bool WrapperFrontendAction::hasPCHSupport() const {
449 return WrappedAction->hasPCHSupport();
450}
451bool WrapperFrontendAction::hasASTFileSupport() const {
452 return WrappedAction->hasASTFileSupport();
453}
454bool WrapperFrontendAction::hasIRSupport() const {
455 return WrappedAction->hasIRSupport();
456}
457bool WrapperFrontendAction::hasCodeCompletionSupport() const {
458 return WrappedAction->hasCodeCompletionSupport();
459}
460
461WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
462 : WrappedAction(WrappedAction) {}
463