blob: b7b93a9178d0ad39a52144d4081c74a1f72b80de [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/ErrorHandling.h"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000027#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000030#include "llvm/Support/system_error.h"
31#include "llvm/Support/Timer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000032using namespace clang;
33
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000034namespace {
35
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000036class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000037 ASTDeserializationListener *Previous;
38
39public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000040 explicit DelegatingDeserializationListener(
41 ASTDeserializationListener *Previous)
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000042 : Previous(Previous) { }
43
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000044 virtual void ReaderInitialized(ASTReader *Reader) {
45 if (Previous)
46 Previous->ReaderInitialized(Reader);
47 }
48 virtual void IdentifierRead(serialization::IdentID ID,
49 IdentifierInfo *II) {
50 if (Previous)
51 Previous->IdentifierRead(ID, II);
52 }
53 virtual void TypeRead(serialization::TypeIdx Idx, QualType T) {
54 if (Previous)
55 Previous->TypeRead(Idx, T);
56 }
57 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
58 if (Previous)
59 Previous->DeclRead(ID, D);
60 }
61 virtual void SelectorRead(serialization::SelectorID ID, Selector Sel) {
62 if (Previous)
63 Previous->SelectorRead(ID, Sel);
64 }
65 virtual void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
66 MacroDefinition *MD) {
67 if (Previous)
68 Previous->MacroDefinitionRead(PPID, MD);
69 }
70};
71
72/// \brief Dumps deserialized declarations.
73class DeserializedDeclsDumper : public DelegatingDeserializationListener {
74public:
75 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
76 : DelegatingDeserializationListener(Previous) { }
77
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000078 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
79 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
80 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000081 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000082 llvm::outs() << "\n";
83
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000084 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000085 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000086};
87
David Blaikiee3f34112012-05-29 17:05:42 +000088/// \brief Checks deserialized declarations and emits error if a name
89/// matches one given in command-line using -error-on-deserialized-decl.
90class DeserializedDeclsChecker : public DelegatingDeserializationListener {
91 ASTContext &Ctx;
92 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000093
David Blaikiee3f34112012-05-29 17:05:42 +000094public:
95 DeserializedDeclsChecker(ASTContext &Ctx,
96 const std::set<std::string> &NamesToCheck,
97 ASTDeserializationListener *Previous)
98 : DelegatingDeserializationListener(Previous),
99 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000100
David Blaikiee3f34112012-05-29 17:05:42 +0000101 virtual void DeclRead(serialization::DeclID ID, const Decl *D) {
102 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
103 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
104 unsigned DiagID
105 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
106 "%0 was deserialized");
107 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
108 << ND->getNameAsString();
109 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000110
David Blaikiee3f34112012-05-29 17:05:42 +0000111 DelegatingDeserializationListener::DeclRead(ID, D);
112 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000113};
114
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000115} // end anonymous namespace
116
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000117FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000118
119FrontendAction::~FrontendAction() {}
120
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000121void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
122 ASTUnit *AST) {
123 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000124 CurrentASTUnit.reset(AST);
125}
126
Nico Weber5aa74af2011-01-25 20:34:14 +0000127ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000129 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
130 if (!Consumer)
131 return 0;
132
133 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
134 return Consumer;
135
136 // Make sure the non-plugin consumer is first, so that plugins can't
137 // modifiy the AST.
138 std::vector<ASTConsumer*> Consumers(1, Consumer);
139
140 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
141 i != e; ++i) {
142 // This is O(|plugins| * |add_plugins|), but since both numbers are
143 // way below 50 in practice, that's ok.
144 for (FrontendPluginRegistry::iterator
145 it = FrontendPluginRegistry::begin(),
146 ie = FrontendPluginRegistry::end();
147 it != ie; ++it) {
148 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000149 OwningPtr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000150 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000151 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000152 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
153 }
154 }
155 }
156
157 return new MultiplexConsumer(Consumers);
158}
159
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000160
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000161bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000162 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000163 assert(!Instance && "Already processing a source file!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000164 assert(!Input.File.empty() && "Unexpected empty filename!");
165 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000166 setCompilerInstance(&CI);
167
Jordan Roseaf6cf432012-08-10 01:06:08 +0000168 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000169 if (!BeginInvocation(CI))
170 goto failure;
171
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000172 // AST files follow a very different path, since they share objects via the
173 // AST unit.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000174 if (Input.Kind == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000175 assert(!usesPreprocessorOnly() &&
176 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000177 assert(hasASTFileSupport() &&
178 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000179
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000180 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000181 std::string Error;
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000182 ASTUnit *AST = ASTUnit::LoadFromASTFile(Input.File, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000183 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000184 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000185 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000186
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000187 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000188
189 // Set the shared objects, these are reset when we finish processing the
190 // file, otherwise the CompilerInstance will happily destroy them.
191 CI.setFileManager(&AST->getFileManager());
192 CI.setSourceManager(&AST->getSourceManager());
193 CI.setPreprocessor(&AST->getPreprocessor());
194 CI.setASTContext(&AST->getASTContext());
195
196 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000197 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000198 goto failure;
199
200 /// Create the AST consumer.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000201 CI.setASTConsumer(CreateWrappedASTConsumer(CI, Input.File));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000202 if (!CI.hasASTConsumer())
203 goto failure;
204
205 return true;
206 }
207
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000208 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000209 if (!CI.hasFileManager())
210 CI.createFileManager();
211 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000212 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000213
214 // IR files bypass the rest of initialization.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000215 if (Input.Kind == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000216 assert(hasIRSupport() &&
217 "This action does not have IR file support!");
218
219 // Inform the diagnostic client we are processing a source file.
220 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000221 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000222
223 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000224 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000225 goto failure;
226
227 return true;
228 }
229
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000230 // If the implicit PCH include is actually a directory, rather than
231 // a single file, search for a suitable PCH file in that directory.
232 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
233 FileManager &FileMgr = CI.getFileManager();
234 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
235 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
236 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
237 llvm::error_code EC;
238 SmallString<128> DirNative;
239 llvm::sys::path::native(PCHDir->getName(), DirNative);
240 bool Found = false;
241 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
242 Dir != DirEnd && !EC; Dir.increment(EC)) {
243 // Check whether this is an acceptable AST file.
244 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
245 CI.getLangOpts(),
246 CI.getTargetOpts())) {
247 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
248 if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
249 PPOpts.Includes[I] = Dir->path();
250 PPOpts.ImplicitPCHInclude = Dir->path();
251 Found = true;
252 break;
253 }
254 }
255
256 assert(Found && "Implicit PCH include not in includes list?");
257 break;
258 }
259 }
260
261 if (!Found) {
262 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
263 return true;
264 }
265 }
266 }
267
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000268 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000269 CI.createPreprocessor();
270
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000271 // Inform the diagnostic client we are processing a source file.
272 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
273 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000274 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000275
276 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000277 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000278 goto failure;
279
280 /// Create the AST context and consumer unless this is a preprocessor only
281 /// action.
282 if (!usesPreprocessorOnly()) {
283 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000284
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000285 OwningPtr<ASTConsumer> Consumer(
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000286 CreateWrappedASTConsumer(CI, Input.File));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000287 if (!Consumer)
288 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000289
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000290 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000291 CI.getPreprocessor().setPPMutationListener(
292 Consumer->GetPPMutationListener());
293
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000294 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
295 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000296 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000297 source.reset(ChainedIncludesSource::create(CI));
298 if (!source)
299 goto failure;
300 CI.getASTContext().setExternalSource(source);
301
302 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
303 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000304 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000305 ASTDeserializationListener *DeserialListener =
306 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000307 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
308 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000309 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
310 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
311 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
312 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000313 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000314 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000315 CI.getPreprocessorOpts().DisablePCHValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000316 CI.getPreprocessorOpts().DisableStatCache,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000317 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000318 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000319 if (!CI.getASTContext().getExternalSource())
320 goto failure;
321 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000322
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000323 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000324 if (!CI.hasASTConsumer())
325 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000326 }
327
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000328 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000329 // source.
330 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
331 Preprocessor &PP = CI.getPreprocessor();
332 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000333 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000334 }
335
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000336 // If there is a layout overrides file, attach an external AST source that
337 // provides the layouts from that file.
338 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
339 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000340 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000341 Override(new LayoutOverrideSource(
342 CI.getFrontendOpts().OverrideRecordLayoutsFile));
343 CI.getASTContext().setExternalSource(Override);
344 }
345
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000346 return true;
347
348 // If we failed, reset state since the client will not end up calling the
349 // matching EndSourceFile().
350 failure:
351 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000352 CI.setASTContext(0);
353 CI.setPreprocessor(0);
354 CI.setSourceManager(0);
355 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000356 }
357
Jordan Roseaf6cf432012-08-10 01:06:08 +0000358 if (HasBegunSourceFile)
359 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000360 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000361 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000362 setCompilerInstance(0);
363 return false;
364}
365
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000366bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000367 CompilerInstance &CI = getCompilerInstance();
368
369 // Initialize the main file entry. This needs to be delayed until after PCH
370 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000371 if (!isCurrentFileAST()) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000372 if (!CI.InitializeSourceManager(getCurrentFile(),
373 getCurrentInput().IsSystem
374 ? SrcMgr::C_System
375 : SrcMgr::C_User))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000376 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000377 }
378
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000379 if (CI.hasFrontendTimer()) {
380 llvm::TimeRegion Timer(CI.getFrontendTimer());
381 ExecuteAction();
382 }
383 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000384
385 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000386}
387
388void FrontendAction::EndSourceFile() {
389 CompilerInstance &CI = getCompilerInstance();
390
Douglas Gregor92b97f22011-02-09 18:47:31 +0000391 // Inform the diagnostic client we are done with this source file.
392 CI.getDiagnosticClient().EndSourceFile();
393
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000394 // Finalize the action.
395 EndSourceFileAction();
396
397 // Release the consumer and the AST, in that order since the consumer may
398 // perform actions in its destructor which require the context.
399 //
400 // FIXME: There is more per-file stuff we could just drop here?
401 if (CI.getFrontendOpts().DisableFree) {
402 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000403 if (!isCurrentFileAST()) {
404 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000405 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000406 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000407 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000408 if (!isCurrentFileAST()) {
409 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000410 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000411 }
412 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000413 }
414
Daniel Dunbardbd82092010-03-23 05:09:10 +0000415 // Inform the preprocessor we are done.
416 if (CI.hasPreprocessor())
417 CI.getPreprocessor().EndSourceFile();
418
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000419 if (CI.getFrontendOpts().ShowStats) {
420 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
421 CI.getPreprocessor().PrintStats();
422 CI.getPreprocessor().getIdentifierTable().PrintStats();
423 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
424 CI.getSourceManager().PrintStats();
425 llvm::errs() << "\n";
426 }
427
428 // Cleanup the output streams, and erase the output files if we encountered
429 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000430 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000431
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000432 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000433 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000434 CI.resetAndLeakASTContext();
435 CI.resetAndLeakPreprocessor();
436 CI.resetAndLeakSourceManager();
437 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000438 }
439
440 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000441 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000442}
443
444//===----------------------------------------------------------------------===//
445// Utility Actions
446//===----------------------------------------------------------------------===//
447
448void ASTFrontendAction::ExecuteAction() {
449 CompilerInstance &CI = getCompilerInstance();
450
451 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
452 // here so the source manager would be initialized.
453 if (hasCodeCompletionSupport() &&
454 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
455 CI.createCodeCompletionConsumer();
456
457 // Use a code completion consumer?
458 CodeCompleteConsumer *CompletionConsumer = 0;
459 if (CI.hasCodeCompletionConsumer())
460 CompletionConsumer = &CI.getCodeCompletionConsumer();
461
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000462 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000463 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000464
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000465 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
466 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000467}
468
David Blaikie99ba9e32011-12-20 02:48:34 +0000469void PluginASTAction::anchor() { }
470
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000471ASTConsumer *
472PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000473 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000474 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000475}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000476
477ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000478 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000479 return WrappedAction->CreateASTConsumer(CI, InFile);
480}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000481bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
482 return WrappedAction->BeginInvocation(CI);
483}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000484bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000485 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000486 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000487 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000488 return WrappedAction->BeginSourceFileAction(CI, Filename);
489}
490void WrapperFrontendAction::ExecuteAction() {
491 WrappedAction->ExecuteAction();
492}
493void WrapperFrontendAction::EndSourceFileAction() {
494 WrappedAction->EndSourceFileAction();
495}
496
497bool WrapperFrontendAction::usesPreprocessorOnly() const {
498 return WrappedAction->usesPreprocessorOnly();
499}
Douglas Gregor467dc882011-08-25 22:30:56 +0000500TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
501 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000502}
503bool WrapperFrontendAction::hasPCHSupport() const {
504 return WrappedAction->hasPCHSupport();
505}
506bool WrapperFrontendAction::hasASTFileSupport() const {
507 return WrappedAction->hasASTFileSupport();
508}
509bool WrapperFrontendAction::hasIRSupport() const {
510 return WrappedAction->hasIRSupport();
511}
512bool WrapperFrontendAction::hasCodeCompletionSupport() const {
513 return WrappedAction->hasCodeCompletionSupport();
514}
515
516WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
517 : WrappedAction(WrappedAction) {}
518