blob: 2e9a791c3039e19e32adea0507cfa5df7a380091 [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!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000164 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000165 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000166 setCompilerInstance(&CI);
167
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000168 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000169 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000170 if (!BeginInvocation(CI))
171 goto failure;
172
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000173 // AST files follow a very different path, since they share objects via the
174 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000175 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000176 assert(!usesPreprocessorOnly() &&
177 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000178 assert(hasASTFileSupport() &&
179 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000180
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000181 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000182 std::string Error;
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000183 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000184 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000185 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000186 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000187
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000188 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000189
190 // Set the shared objects, these are reset when we finish processing the
191 // file, otherwise the CompilerInstance will happily destroy them.
192 CI.setFileManager(&AST->getFileManager());
193 CI.setSourceManager(&AST->getSourceManager());
194 CI.setPreprocessor(&AST->getPreprocessor());
195 CI.setASTContext(&AST->getASTContext());
196
197 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000198 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000199 goto failure;
200
201 /// Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000202 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000203 if (!CI.hasASTConsumer())
204 goto failure;
205
206 return true;
207 }
208
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000209 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000210 if (!CI.hasFileManager())
211 CI.createFileManager();
212 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000213 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000214
215 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000216 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000217 assert(hasIRSupport() &&
218 "This action does not have IR file support!");
219
220 // Inform the diagnostic client we are processing a source file.
221 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000222 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000223
224 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000225 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000226 goto failure;
227
228 return true;
229 }
230
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000231 // If the implicit PCH include is actually a directory, rather than
232 // a single file, search for a suitable PCH file in that directory.
233 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
234 FileManager &FileMgr = CI.getFileManager();
235 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
236 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
237 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
238 llvm::error_code EC;
239 SmallString<128> DirNative;
240 llvm::sys::path::native(PCHDir->getName(), DirNative);
241 bool Found = false;
242 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
243 Dir != DirEnd && !EC; Dir.increment(EC)) {
244 // Check whether this is an acceptable AST file.
245 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
246 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000247 CI.getTargetOpts(),
248 CI.getPreprocessorOpts())) {
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000249 for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I) {
250 if (PPOpts.Includes[I] == PPOpts.ImplicitPCHInclude) {
251 PPOpts.Includes[I] = Dir->path();
252 PPOpts.ImplicitPCHInclude = Dir->path();
253 Found = true;
254 break;
255 }
256 }
257
258 assert(Found && "Implicit PCH include not in includes list?");
259 break;
260 }
261 }
262
263 if (!Found) {
264 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
265 return true;
266 }
267 }
268 }
269
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000270 // Set up the preprocessor.
Daniel Dunbar20560482010-06-07 23:23:50 +0000271 CI.createPreprocessor();
272
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000273 // Inform the diagnostic client we are processing a source file.
274 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
275 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000276 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000277
278 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000279 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000280 goto failure;
281
282 /// Create the AST context and consumer unless this is a preprocessor only
283 /// action.
284 if (!usesPreprocessorOnly()) {
285 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000286
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000287 OwningPtr<ASTConsumer> Consumer(
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000288 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000289 if (!Consumer)
290 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000291
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000292 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000293 CI.getPreprocessor().setPPMutationListener(
294 Consumer->GetPPMutationListener());
295
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000296 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
297 // Convert headers to PCH and chain them.
Dylan Noblesmith6f42b622012-02-05 02:12:40 +0000298 OwningPtr<ExternalASTSource> source;
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000299 source.reset(ChainedIncludesSource::create(CI));
300 if (!source)
301 goto failure;
302 CI.getASTContext().setExternalSource(source);
303
304 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
305 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000306 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000307 ASTDeserializationListener *DeserialListener =
308 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000309 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
310 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000311 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
312 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
313 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
314 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000315 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000316 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000317 CI.getPreprocessorOpts().DisablePCHValidation,
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()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000373 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000374 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000375 }
376
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000377 if (CI.hasFrontendTimer()) {
378 llvm::TimeRegion Timer(CI.getFrontendTimer());
379 ExecuteAction();
380 }
381 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000382
383 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000384}
385
386void FrontendAction::EndSourceFile() {
387 CompilerInstance &CI = getCompilerInstance();
388
Douglas Gregor92b97f22011-02-09 18:47:31 +0000389 // Inform the diagnostic client we are done with this source file.
390 CI.getDiagnosticClient().EndSourceFile();
391
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000392 // Finalize the action.
393 EndSourceFileAction();
394
395 // Release the consumer and the AST, in that order since the consumer may
396 // perform actions in its destructor which require the context.
397 //
398 // FIXME: There is more per-file stuff we could just drop here?
399 if (CI.getFrontendOpts().DisableFree) {
400 CI.takeASTConsumer();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000401 if (!isCurrentFileAST()) {
402 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000403 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000404 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000405 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000406 if (!isCurrentFileAST()) {
407 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000408 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000409 }
410 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000411 }
412
Daniel Dunbardbd82092010-03-23 05:09:10 +0000413 // Inform the preprocessor we are done.
414 if (CI.hasPreprocessor())
415 CI.getPreprocessor().EndSourceFile();
416
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000417 if (CI.getFrontendOpts().ShowStats) {
418 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
419 CI.getPreprocessor().PrintStats();
420 CI.getPreprocessor().getIdentifierTable().PrintStats();
421 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
422 CI.getSourceManager().PrintStats();
423 llvm::errs() << "\n";
424 }
425
426 // Cleanup the output streams, and erase the output files if we encountered
427 // an error.
Argyrios Kyrtzidisbe3aab62010-11-18 21:47:07 +0000428 CI.clearOutputFiles(/*EraseFiles=*/CI.getDiagnostics().hasErrorOccurred());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000429
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000430 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000431 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000432 CI.resetAndLeakASTContext();
433 CI.resetAndLeakPreprocessor();
434 CI.resetAndLeakSourceManager();
435 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000436 }
437
438 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000439 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000440}
441
442//===----------------------------------------------------------------------===//
443// Utility Actions
444//===----------------------------------------------------------------------===//
445
446void ASTFrontendAction::ExecuteAction() {
447 CompilerInstance &CI = getCompilerInstance();
448
449 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
450 // here so the source manager would be initialized.
451 if (hasCodeCompletionSupport() &&
452 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
453 CI.createCodeCompletionConsumer();
454
455 // Use a code completion consumer?
456 CodeCompleteConsumer *CompletionConsumer = 0;
457 if (CI.hasCodeCompletionConsumer())
458 CompletionConsumer = &CI.getCodeCompletionConsumer();
459
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000460 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000461 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000462
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000463 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
464 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000465}
466
David Blaikie99ba9e32011-12-20 02:48:34 +0000467void PluginASTAction::anchor() { }
468
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000469ASTConsumer *
470PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000471 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000472 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000473}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000474
475ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000476 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000477 return WrappedAction->CreateASTConsumer(CI, InFile);
478}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000479bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
480 return WrappedAction->BeginInvocation(CI);
481}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000482bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000483 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000484 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000485 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000486 return WrappedAction->BeginSourceFileAction(CI, Filename);
487}
488void WrapperFrontendAction::ExecuteAction() {
489 WrappedAction->ExecuteAction();
490}
491void WrapperFrontendAction::EndSourceFileAction() {
492 WrappedAction->EndSourceFileAction();
493}
494
495bool WrapperFrontendAction::usesPreprocessorOnly() const {
496 return WrappedAction->usesPreprocessorOnly();
497}
Douglas Gregor467dc882011-08-25 22:30:56 +0000498TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
499 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000500}
501bool WrapperFrontendAction::hasPCHSupport() const {
502 return WrappedAction->hasPCHSupport();
503}
504bool WrapperFrontendAction::hasASTFileSupport() const {
505 return WrappedAction->hasASTFileSupport();
506}
507bool WrapperFrontendAction::hasIRSupport() const {
508 return WrappedAction->hasIRSupport();
509}
510bool WrapperFrontendAction::hasCodeCompletionSupport() const {
511 return WrappedAction->hasCodeCompletionSupport();
512}
513
514WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
515 : WrappedAction(WrappedAction) {}
516