blob: fa1655db7904976c9872a6d7f209deb1fa09f381 [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(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000246 CI.getTargetOpts(),
247 CI.getPreprocessorOpts())) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000248 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
249 if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
250 PPOpts.Includes[I] = Dir->path();
251 PPOpts.ImplicitPCHInclude = Dir->path();
252 Found = true;
253 break;
254 }
255 }
256
257 assert(Found && "Implicit PCH include not in includes list?");
258 break;
259 }
260 }
261
262 if (!Found) {
263 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
264 return true;
265 }
266 }
267 }
268
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000269 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000270 CI.createPreprocessor();
271
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000272 // Inform the diagnostic client we are processing a source file.
273 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
274 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000275 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000276
277 // Initialize the action.
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000278 if (!BeginSourceFileAction(CI, Input.File))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000279 goto failure;
280
281 /// Create the AST context and consumer unless this is a preprocessor only
282 /// action.
283 if (!usesPreprocessorOnly()) {
284 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000285
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000286 OwningPtr<ASTConsumer> Consumer(
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000287 CreateWrappedASTConsumer(CI, Input.File));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000288 if (!Consumer)
289 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000290
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000291 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000292 CI.getPreprocessor().setPPMutationListener(
293 Consumer->GetPPMutationListener());
294
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000295 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
296 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000297 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000298 source.reset(ChainedIncludesSource::create(CI));
299 if (!source)
300 goto failure;
301 CI.getASTContext().setExternalSource(source);
302
303 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
304 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000305 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000306 ASTDeserializationListener *DeserialListener =
307 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000308 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
309 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000310 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
311 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
312 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
313 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000314 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000315 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000316 CI.getPreprocessorOpts().DisablePCHValidation,
Douglas Gregor8ef6c8c2011-02-05 19:42:43 +0000317 CI.getPreprocessorOpts().DisableStatCache,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000318 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000319 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000320 if (!CI.getASTContext().getExternalSource())
321 goto failure;
322 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000323
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000324 CI.setASTConsumer(Consumer.take());
Sebastian Redl77f46032010-07-09 21:00:24 +0000325 if (!CI.hasASTConsumer())
326 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000327 }
328
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000329 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000330 // source.
331 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
332 Preprocessor &PP = CI.getPreprocessor();
333 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000334 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000335 }
336
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000337 // If there is a layout overrides file, attach an external AST source that
338 // provides the layouts from that file.
339 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
340 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000341 OwningPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000342 Override(new LayoutOverrideSource(
343 CI.getFrontendOpts().OverrideRecordLayoutsFile));
344 CI.getASTContext().setExternalSource(Override);
345 }
346
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000347 return true;
348
349 // If we failed, reset state since the client will not end up calling the
350 // matching EndSourceFile().
351 failure:
352 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000353 CI.setASTContext(0);
354 CI.setPreprocessor(0);
355 CI.setSourceManager(0);
356 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000357 }
358
Jordan Roseaf6cf432012-08-10 01:06:08 +0000359 if (HasBegunSourceFile)
360 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000361 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000362 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000363 setCompilerInstance(0);
364 return false;
365}
366
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000367bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000368 CompilerInstance &CI = getCompilerInstance();
369
370 // Initialize the main file entry. This needs to be delayed until after PCH
371 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000372 if (!isCurrentFileAST()) {
Douglas Gregora1f1fad2012-01-27 19:52:33 +0000373 if (!CI.InitializeSourceManager(getCurrentFile(),
374 getCurrentInput().IsSystem
375 ? SrcMgr::C_System
376 : SrcMgr::C_User))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000377 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000378 }
379
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000380 if (CI.hasFrontendTimer()) {
381 llvm::TimeRegion Timer(CI.getFrontendTimer());
382 ExecuteAction();
383 }
384 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000385
386 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000387}
388
389void FrontendAction::EndSourceFile() {
390 CompilerInstance &CI = getCompilerInstance();
391
Douglas Gregor92b97f22011-02-09 18:47:31 +0000392 // Inform the diagnostic client we are done with this source file.
393 CI.getDiagnosticClient().EndSourceFile();
394
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000395 // Finalize the action.
396 EndSourceFileAction();
397
398 // Release the consumer and the AST, in that order since the consumer may
399 // perform actions in its destructor which require the context.
400 //
401 // FIXME: There is more per-file stuff we could just drop here?
402 if (CI.getFrontendOpts().DisableFree) {
403 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000404 if (!isCurrentFileAST()) {
405 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000406 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000407 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000408 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000409 if (!isCurrentFileAST()) {
410 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000411 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000412 }
413 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000414 }
415
Daniel Dunbardbd82092010-03-23 05:09:10 +0000416 // Inform the preprocessor we are done.
417 if (CI.hasPreprocessor())
418 CI.getPreprocessor().EndSourceFile();
419
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000420 if (CI.getFrontendOpts().ShowStats) {
421 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
422 CI.getPreprocessor().PrintStats();
423 CI.getPreprocessor().getIdentifierTable().PrintStats();
424 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
425 CI.getSourceManager().PrintStats();
426 llvm::errs() << "\n";
427 }
428
429 // Cleanup the output streams, and erase the output files if we encountered
430 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000431 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000432
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000433 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000434 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000435 CI.resetAndLeakASTContext();
436 CI.resetAndLeakPreprocessor();
437 CI.resetAndLeakSourceManager();
438 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000439 }
440
441 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000442 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000443}
444
445//===----------------------------------------------------------------------===//
446// Utility Actions
447//===----------------------------------------------------------------------===//
448
449void ASTFrontendAction::ExecuteAction() {
450 CompilerInstance &CI = getCompilerInstance();
451
452 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
453 // here so the source manager would be initialized.
454 if (hasCodeCompletionSupport() &&
455 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
456 CI.createCodeCompletionConsumer();
457
458 // Use a code completion consumer?
459 CodeCompleteConsumer *CompletionConsumer = 0;
460 if (CI.hasCodeCompletionConsumer())
461 CompletionConsumer = &CI.getCodeCompletionConsumer();
462
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000463 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000464 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000465
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000466 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
467 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000468}
469
David Blaikie99ba9e32011-12-20 02:48:34 +0000470void PluginASTAction::anchor() { }
471
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000472ASTConsumer *
473PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000474 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000475 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000476}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000477
478ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000479 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000480 return WrappedAction->CreateASTConsumer(CI, InFile);
481}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000482bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
483 return WrappedAction->BeginInvocation(CI);
484}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000485bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000486 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000487 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000488 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000489 return WrappedAction->BeginSourceFileAction(CI, Filename);
490}
491void WrapperFrontendAction::ExecuteAction() {
492 WrappedAction->ExecuteAction();
493}
494void WrapperFrontendAction::EndSourceFileAction() {
495 WrappedAction->EndSourceFileAction();
496}
497
498bool WrapperFrontendAction::usesPreprocessorOnly() const {
499 return WrappedAction->usesPreprocessorOnly();
500}
Douglas Gregor467dc882011-08-25 22:30:56 +0000501TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
502 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000503}
504bool WrapperFrontendAction::hasPCHSupport() const {
505 return WrappedAction->hasPCHSupport();
506}
507bool WrapperFrontendAction::hasASTFileSupport() const {
508 return WrappedAction->hasASTFileSupport();
509}
510bool WrapperFrontendAction::hasIRSupport() const {
511 return WrappedAction->hasIRSupport();
512}
513bool WrapperFrontendAction::hasCodeCompletionSupport() const {
514 return WrappedAction->hasCodeCompletionSupport();
515}
516
517WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
518 : WrappedAction(WrappedAction) {}
519