blob: d2ece7e732d64f92db6891ee6cb4e92dd2353f1f [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/Frontend/ASTUnit.h"
Chandler Carruth71088d12011-12-09 01:55:54 +000015#include "clang/Frontend/ChainedIncludesSource.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000018#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregor453dbcb2012-01-26 07:55:45 +000019#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber5aa74af2011-01-25 20:34:14 +000020#include "clang/Frontend/MultiplexConsumer.h"
Stephen Hines651f13c2014-04-23 16:59:28 -070021#include "clang/Frontend/Utils.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000022#include "clang/Lex/HeaderSearch.h"
23#include "clang/Lex/Preprocessor.h"
John McCall19510852010-08-20 18:27:03 +000024#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000025#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +000026#include "clang/Serialization/ASTReader.h"
Douglas Gregora6b00fc2013-01-23 22:38:11 +000027#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000028#include "llvm/Support/ErrorHandling.h"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000029#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000031#include "llvm/Support/Timer.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregor27ffa6c2012-10-23 06:18:24 +000033#include "llvm/Support/system_error.h"
Daniel Dunbar4ee24092009-11-14 10:42:35 +000034using namespace clang;
35
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000036namespace {
37
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000038class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000039 ASTDeserializationListener *Previous;
40
41public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000042 explicit DelegatingDeserializationListener(
43 ASTDeserializationListener *Previous)
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000044 : Previous(Previous) { }
45
Stephen Hines651f13c2014-04-23 16:59:28 -070046 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000047 if (Previous)
48 Previous->ReaderInitialized(Reader);
49 }
Stephen Hines651f13c2014-04-23 16:59:28 -070050 void IdentifierRead(serialization::IdentID ID,
51 IdentifierInfo *II) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000052 if (Previous)
53 Previous->IdentifierRead(ID, II);
54 }
Stephen Hines651f13c2014-04-23 16:59:28 -070055 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000056 if (Previous)
57 Previous->TypeRead(Idx, T);
58 }
Stephen Hines651f13c2014-04-23 16:59:28 -070059 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000060 if (Previous)
61 Previous->DeclRead(ID, D);
62 }
Stephen Hines651f13c2014-04-23 16:59:28 -070063 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000064 if (Previous)
65 Previous->SelectorRead(ID, Sel);
66 }
Stephen Hines651f13c2014-04-23 16:59:28 -070067 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
68 MacroDefinition *MD) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000069 if (Previous)
70 Previous->MacroDefinitionRead(PPID, MD);
71 }
72};
73
74/// \brief Dumps deserialized declarations.
75class DeserializedDeclsDumper : public DelegatingDeserializationListener {
76public:
77 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous)
78 : DelegatingDeserializationListener(Previous) { }
79
Stephen Hines651f13c2014-04-23 16:59:28 -070080 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000081 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
82 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000083 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000084 llvm::outs() << "\n";
85
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000086 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000087 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000088};
89
David Blaikiee3f34112012-05-29 17:05:42 +000090/// \brief Checks deserialized declarations and emits error if a name
91/// matches one given in command-line using -error-on-deserialized-decl.
92class DeserializedDeclsChecker : public DelegatingDeserializationListener {
93 ASTContext &Ctx;
94 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +000095
David Blaikiee3f34112012-05-29 17:05:42 +000096public:
97 DeserializedDeclsChecker(ASTContext &Ctx,
98 const std::set<std::string> &NamesToCheck,
99 ASTDeserializationListener *Previous)
100 : DelegatingDeserializationListener(Previous),
101 Ctx(Ctx), NamesToCheck(NamesToCheck) { }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000102
Stephen Hines651f13c2014-04-23 16:59:28 -0700103 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikiee3f34112012-05-29 17:05:42 +0000104 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
105 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
106 unsigned DiagID
107 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
108 "%0 was deserialized");
109 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
110 << ND->getNameAsString();
111 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000112
David Blaikiee3f34112012-05-29 17:05:42 +0000113 DelegatingDeserializationListener::DeclRead(ID, D);
114 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000115};
116
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000117} // end anonymous namespace
118
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000119FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000120
121FrontendAction::~FrontendAction() {}
122
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000123void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
124 ASTUnit *AST) {
125 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000126 CurrentASTUnit.reset(AST);
127}
128
Nico Weber5aa74af2011-01-25 20:34:14 +0000129ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000131 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
132 if (!Consumer)
133 return 0;
134
135 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
136 return Consumer;
137
138 // Make sure the non-plugin consumer is first, so that plugins can't
139 // modifiy the AST.
140 std::vector<ASTConsumer*> Consumers(1, Consumer);
141
142 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
143 i != e; ++i) {
144 // This is O(|plugins| * |add_plugins|), but since both numbers are
145 // way below 50 in practice, that's ok.
146 for (FrontendPluginRegistry::iterator
147 it = FrontendPluginRegistry::begin(),
148 ie = FrontendPluginRegistry::end();
149 it != ie; ++it) {
150 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700151 std::unique_ptr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000152 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000153 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000154 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
155 }
156 }
157 }
158
159 return new MultiplexConsumer(Consumers);
160}
161
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000162bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000163 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000164 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000165 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000166 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000167 setCompilerInstance(&CI);
168
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000169 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000170 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000171 if (!BeginInvocation(CI))
172 goto failure;
173
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000174 // AST files follow a very different path, since they share objects via the
175 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000176 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000177 assert(!usesPreprocessorOnly() &&
178 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000179 assert(hasASTFileSupport() &&
180 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000181
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000182 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Stephen Hines651f13c2014-04-23 16:59:28 -0700183
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000184 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000185 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000186 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000187 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000188
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000189 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000190
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000191 // Inform the diagnostic client we are processing a source file.
192 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
193 HasBegunSourceFile = true;
194
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000195 // Set the shared objects, these are reset when we finish processing the
196 // file, otherwise the CompilerInstance will happily destroy them.
197 CI.setFileManager(&AST->getFileManager());
198 CI.setSourceManager(&AST->getSourceManager());
199 CI.setPreprocessor(&AST->getPreprocessor());
200 CI.setASTContext(&AST->getASTContext());
201
202 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000203 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000204 goto failure;
205
James Dennett18f43a62013-01-23 00:45:44 +0000206 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000207 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000208 if (!CI.hasASTConsumer())
209 goto failure;
210
211 return true;
212 }
213
Stephen Hines651f13c2014-04-23 16:59:28 -0700214 if (!CI.getHeaderSearchOpts().VFSOverlayFiles.empty()) {
215 IntrusiveRefCntPtr<vfs::OverlayFileSystem>
216 Overlay(new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
217 // earlier vfs files are on the bottom
218 const std::vector<std::string> &Files =
219 CI.getHeaderSearchOpts().VFSOverlayFiles;
220 for (std::vector<std::string>::const_iterator I = Files.begin(),
221 E = Files.end();
222 I != E; ++I) {
223 std::unique_ptr<llvm::MemoryBuffer> Buffer;
224 if (llvm::errc::success != llvm::MemoryBuffer::getFile(*I, Buffer)) {
225 CI.getDiagnostics().Report(diag::err_missing_vfs_overlay_file) << *I;
226 goto failure;
227 }
228
229 IntrusiveRefCntPtr<vfs::FileSystem> FS =
230 vfs::getVFSFromYAML(Buffer.release(), /*DiagHandler*/ 0);
231 if (!FS.getPtr()) {
232 CI.getDiagnostics().Report(diag::err_invalid_vfs_overlay) << *I;
233 goto failure;
234 }
235 Overlay->pushOverlay(FS);
236 }
237 CI.setVirtualFileSystem(Overlay);
238 }
239
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000240 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000241 if (!CI.hasFileManager())
242 CI.createFileManager();
243 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000244 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000245
246 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000247 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000248 assert(hasIRSupport() &&
249 "This action does not have IR file support!");
250
251 // Inform the diagnostic client we are processing a source file.
252 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000253 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000254
255 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000256 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000257 goto failure;
258
259 return true;
260 }
261
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000262 // If the implicit PCH include is actually a directory, rather than
263 // a single file, search for a suitable PCH file in that directory.
264 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
265 FileManager &FileMgr = CI.getFileManager();
266 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
267 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
268 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
269 llvm::error_code EC;
270 SmallString<128> DirNative;
271 llvm::sys::path::native(PCHDir->getName(), DirNative);
272 bool Found = false;
273 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
274 Dir != DirEnd && !EC; Dir.increment(EC)) {
275 // Check whether this is an acceptable AST file.
276 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
277 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000278 CI.getTargetOpts(),
279 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000280 PPOpts.ImplicitPCHInclude = Dir->path();
281 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000282 break;
283 }
284 }
285
286 if (!Found) {
287 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
288 return true;
289 }
290 }
291 }
292
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000293 // Set up the preprocessor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700294 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbar20560482010-06-07 23:23:50 +0000295
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000296 // Inform the diagnostic client we are processing a source file.
297 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
298 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000299 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000300
301 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000302 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000303 goto failure;
304
James Dennett18f43a62013-01-23 00:45:44 +0000305 // Create the AST context and consumer unless this is a preprocessor only
306 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000307 if (!usesPreprocessorOnly()) {
308 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000309
Stephen Hines651f13c2014-04-23 16:59:28 -0700310 std::unique_ptr<ASTConsumer> Consumer(
311 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000312 if (!Consumer)
313 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000314
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000315 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000316
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000317 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
318 // Convert headers to PCH and chain them.
Stephen Hines651f13c2014-04-23 16:59:28 -0700319 IntrusiveRefCntPtr<ChainedIncludesSource> source;
320 source = ChainedIncludesSource::create(CI);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000321 if (!source)
322 goto failure;
Stephen Hines651f13c2014-04-23 16:59:28 -0700323 CI.setModuleManager(static_cast<ASTReader*>(&source->getFinalReader()));
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000324 CI.getASTContext().setExternalSource(source);
325
326 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
327 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000328 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000329 ASTDeserializationListener *DeserialListener =
330 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000331 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
332 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000333 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
334 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
335 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
336 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000337 CI.createPCHExternalASTSource(
Douglas Gregorfae3b2f2010-07-27 00:27:13 +0000338 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000339 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidisbef35c92012-03-07 01:51:17 +0000340 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000341 DeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000342 if (!CI.getASTContext().getExternalSource())
343 goto failure;
344 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000345
Stephen Hines651f13c2014-04-23 16:59:28 -0700346 CI.setASTConsumer(Consumer.release());
Sebastian Redl77f46032010-07-09 21:00:24 +0000347 if (!CI.hasASTConsumer())
348 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000349 }
350
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000351 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000352 // source.
353 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
354 Preprocessor &PP = CI.getPreprocessor();
355 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000356 PP.getLangOpts());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000357 }
358
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000359 // If there is a layout overrides file, attach an external AST source that
360 // provides the layouts from that file.
361 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
362 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700363 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000364 Override(new LayoutOverrideSource(
365 CI.getFrontendOpts().OverrideRecordLayoutsFile));
366 CI.getASTContext().setExternalSource(Override);
367 }
368
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000369 return true;
370
371 // If we failed, reset state since the client will not end up calling the
372 // matching EndSourceFile().
373 failure:
374 if (isCurrentFileAST()) {
Ted Kremenek4f327862011-03-21 18:40:17 +0000375 CI.setASTContext(0);
376 CI.setPreprocessor(0);
377 CI.setSourceManager(0);
378 CI.setFileManager(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000379 }
380
Jordan Roseaf6cf432012-08-10 01:06:08 +0000381 if (HasBegunSourceFile)
382 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000383 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000384 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000385 setCompilerInstance(0);
386 return false;
387}
388
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000389bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000390 CompilerInstance &CI = getCompilerInstance();
391
392 // Initialize the main file entry. This needs to be delayed until after PCH
393 // has loaded.
Argyrios Kyrtzidisb8c879a2012-01-05 21:36:25 +0000394 if (!isCurrentFileAST()) {
Argyrios Kyrtzidis8e1fbbc2012-11-09 19:40:33 +0000395 if (!CI.InitializeSourceManager(getCurrentInput()))
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000396 return false;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000397 }
398
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000399 if (CI.hasFrontendTimer()) {
400 llvm::TimeRegion Timer(CI.getFrontendTimer());
401 ExecuteAction();
402 }
403 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000404
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000405 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000406 // there were any module-build failures.
407 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
408 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000409 GlobalModuleIndex::writeIndex(
410 CI.getFileManager(),
411 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
412 }
413
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000414 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000415}
416
417void FrontendAction::EndSourceFile() {
418 CompilerInstance &CI = getCompilerInstance();
419
Douglas Gregor92b97f22011-02-09 18:47:31 +0000420 // Inform the diagnostic client we are done with this source file.
421 CI.getDiagnosticClient().EndSourceFile();
422
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000423 // Finalize the action.
424 EndSourceFileAction();
425
426 // Release the consumer and the AST, in that order since the consumer may
427 // perform actions in its destructor which require the context.
428 //
429 // FIXME: There is more per-file stuff we could just drop here?
430 if (CI.getFrontendOpts().DisableFree) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700431 BuryPointer(CI.takeASTConsumer());
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000432 if (!isCurrentFileAST()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700433 BuryPointer(CI.takeSema());
Ted Kremenek4f327862011-03-21 18:40:17 +0000434 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000435 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000436 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000437 if (!isCurrentFileAST()) {
438 CI.setSema(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000439 CI.setASTContext(0);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000440 }
441 CI.setASTConsumer(0);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000442 }
443
Daniel Dunbardbd82092010-03-23 05:09:10 +0000444 // Inform the preprocessor we are done.
445 if (CI.hasPreprocessor())
446 CI.getPreprocessor().EndSourceFile();
447
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000448 if (CI.getFrontendOpts().ShowStats) {
449 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
450 CI.getPreprocessor().PrintStats();
451 CI.getPreprocessor().getIdentifierTable().PrintStats();
452 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
453 CI.getSourceManager().PrintStats();
454 llvm::errs() << "\n";
455 }
456
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000457 // Cleanup the output streams, and erase the output files if instructed by the
458 // FrontendAction.
459 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000460
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000461 if (isCurrentFileAST()) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000462 CI.takeSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000463 CI.resetAndLeakASTContext();
464 CI.resetAndLeakPreprocessor();
465 CI.resetAndLeakSourceManager();
466 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000467 }
468
469 setCompilerInstance(0);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000470 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000471}
472
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000473bool FrontendAction::shouldEraseOutputFiles() {
474 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
475}
476
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000477//===----------------------------------------------------------------------===//
478// Utility Actions
479//===----------------------------------------------------------------------===//
480
481void ASTFrontendAction::ExecuteAction() {
482 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola0046ce52013-07-28 13:23:37 +0000483 if (!CI.hasPreprocessor())
484 return;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000485
486 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
487 // here so the source manager would be initialized.
488 if (hasCodeCompletionSupport() &&
489 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
490 CI.createCodeCompletionConsumer();
491
492 // Use a code completion consumer?
493 CodeCompleteConsumer *CompletionConsumer = 0;
494 if (CI.hasCodeCompletionConsumer())
495 CompletionConsumer = &CI.getCodeCompletionConsumer();
496
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000497 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000498 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000499
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000500 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
501 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000502}
503
David Blaikie99ba9e32011-12-20 02:48:34 +0000504void PluginASTAction::anchor() { }
505
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000506ASTConsumer *
507PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000508 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000509 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000510}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000511
512ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000513 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000514 return WrappedAction->CreateASTConsumer(CI, InFile);
515}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000516bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
517 return WrappedAction->BeginInvocation(CI);
518}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000519bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000520 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000521 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000522 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000523 return WrappedAction->BeginSourceFileAction(CI, Filename);
524}
525void WrapperFrontendAction::ExecuteAction() {
526 WrappedAction->ExecuteAction();
527}
528void WrapperFrontendAction::EndSourceFileAction() {
529 WrappedAction->EndSourceFileAction();
530}
531
532bool WrapperFrontendAction::usesPreprocessorOnly() const {
533 return WrappedAction->usesPreprocessorOnly();
534}
Douglas Gregor467dc882011-08-25 22:30:56 +0000535TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
536 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000537}
538bool WrapperFrontendAction::hasPCHSupport() const {
539 return WrappedAction->hasPCHSupport();
540}
541bool WrapperFrontendAction::hasASTFileSupport() const {
542 return WrappedAction->hasASTFileSupport();
543}
544bool WrapperFrontendAction::hasIRSupport() const {
545 return WrappedAction->hasIRSupport();
546}
547bool WrapperFrontendAction::hasCodeCompletionSupport() const {
548 return WrappedAction->hasCodeCompletionSupport();
549}
550
551WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
552 : WrappedAction(WrappedAction) {}
553