blob: dc4dd89371a1dc5a33a49c20613b31d862ce8f1c [file] [log] [blame]
Daniel Dunbara0ff58d2009-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 Redl07a89a82010-07-30 00:29:29 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000012#include "clang/AST/ASTContext.h"
Nico Weber2992efa2011-01-25 20:34:14 +000013#include "clang/AST/DeclGroup.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000014#include "clang/Frontend/ASTUnit.h"
Chandler Carruthf887db02011-12-09 01:55:54 +000015#include "clang/Frontend/ChainedIncludesSource.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber2992efa2011-01-25 20:34:14 +000018#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000019#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber2992efa2011-01-25 20:34:14 +000020#include "clang/Frontend/MultiplexConsumer.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Lex/HeaderSearch.h"
23#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000025#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turner0248f572011-08-05 22:17:03 +000026#include "clang/Serialization/ASTReader.h"
Douglas Gregor5e306b12013-01-23 22:38:11 +000027#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000028#include "llvm/Support/ErrorHandling.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000029#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/Support/Timer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000033#include "llvm/Support/system_error.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000034using namespace clang;
35
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000036namespace {
37
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000038class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000039 ASTDeserializationListener *Previous;
40
41public:
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000042 explicit DelegatingDeserializationListener(
43 ASTDeserializationListener *Previous)
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000044 : Previous(Previous) { }
45
Craig Topperafa7cb32014-03-13 06:07:04 +000046 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000047 if (Previous)
48 Previous->ReaderInitialized(Reader);
49 }
Craig Topperafa7cb32014-03-13 06:07:04 +000050 void IdentifierRead(serialization::IdentID ID,
51 IdentifierInfo *II) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000052 if (Previous)
53 Previous->IdentifierRead(ID, II);
54 }
Craig Topperafa7cb32014-03-13 06:07:04 +000055 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000056 if (Previous)
57 Previous->TypeRead(Idx, T);
58 }
Craig Topperafa7cb32014-03-13 06:07:04 +000059 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000060 if (Previous)
61 Previous->DeclRead(ID, D);
62 }
Craig Topperafa7cb32014-03-13 06:07:04 +000063 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000064 if (Previous)
65 Previous->SelectorRead(ID, Sel);
66 }
Craig Topperafa7cb32014-03-13 06:07:04 +000067 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
68 MacroDefinition *MD) override {
Argyrios Kyrtzidisb12986f2011-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
Craig Topperafa7cb32014-03-13 06:07:04 +000080 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000081 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
82 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramerdb0fc512012-02-07 11:57:57 +000083 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000084 llvm::outs() << "\n";
85
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000086 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000087 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000088};
89
David Blaikie48b81282012-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 Kyrtzidis0427be92010-10-14 20:14:25 +000095
David Blaikie48b81282012-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 Kyrtzidis0427be92010-10-14 20:14:25 +0000102
Craig Topperafa7cb32014-03-13 06:07:04 +0000103 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikie48b81282012-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 Kyrtzidis0427be92010-10-14 20:14:25 +0000112
David Blaikie48b81282012-05-29 17:05:42 +0000113 DelegatingDeserializationListener::DeclRead(ID, D);
114 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000115};
116
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000117} // end anonymous namespace
118
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000119FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000120
121FrontendAction::~FrontendAction() {}
122
Douglas Gregor32fbe312012-01-20 16:28:04 +0000123void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
124 ASTUnit *AST) {
125 this->CurrentInput = CurrentInput;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000126 CurrentASTUnit.reset(AST);
127}
128
Nico Weber2992efa2011-01-25 20:34:14 +0000129ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000130 StringRef InFile) {
Nico Weber2992efa2011-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]) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000151 std::unique_ptr<PluginASTAction> P(it->instantiate());
Nico Weber2992efa2011-01-25 20:34:14 +0000152 FrontendAction* c = P.get();
Nico Weber741bf9d2011-01-29 21:21:49 +0000153 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber2992efa2011-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 Dunbara0ff58d2009-11-14 10:42:35 +0000162bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000163 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000164 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000165 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000166 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000167 setCompilerInstance(&CI);
168
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000169 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000170 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000171 if (!BeginInvocation(CI))
172 goto failure;
173
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000174 // AST files follow a very different path, since they share objects via the
175 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000176 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000177 assert(!usesPreprocessorOnly() &&
178 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000179 assert(hasASTFileSupport() &&
180 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000181
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000182 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000183
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000184 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000185 CI.getFileSystemOpts());
Daniel Dunbar59203002009-12-03 01:45:44 +0000186 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000187 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000188
Douglas Gregor32fbe312012-01-20 16:28:04 +0000189 setCurrentInput(Input, AST);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000190
Argyrios Kyrtzidisc00f43a2013-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 Dunbara0ff58d2009-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 Kyrtzidis873c8582012-11-09 19:40:39 +0000203 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000204 goto failure;
205
James Dennettf8317672013-01-23 00:45:44 +0000206 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000207 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000208 if (!CI.hasASTConsumer())
209 goto failure;
210
211 return true;
212 }
213
Ben Langmuir801272a2014-02-25 18:23:47 +0000214 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) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000223 std::unique_ptr<llvm::MemoryBuffer> Buffer;
Ben Langmuir801272a2014-02-25 18:23:47 +0000224 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 =
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000230 vfs::getVFSFromYAML(Buffer.release(), /*DiagHandler*/ 0);
Ben Langmuir801272a2014-02-25 18:23:47 +0000231 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 Dunbar9507f9c2010-06-07 23:26:47 +0000240 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000241 if (!CI.hasFileManager())
242 CI.createFileManager();
243 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000244 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000245
246 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000247 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-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 Roseea762b02012-08-10 01:06:08 +0000253 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000254
255 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000256 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000257 goto failure;
258
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000259 // Initialize the main file entry.
260 if (!CI.InitializeSourceManager(CurrentInput))
261 goto failure;
262
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000263 return true;
264 }
265
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000266 // If the implicit PCH include is actually a directory, rather than
267 // a single file, search for a suitable PCH file in that directory.
268 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
269 FileManager &FileMgr = CI.getFileManager();
270 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
271 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
272 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
273 llvm::error_code EC;
274 SmallString<128> DirNative;
275 llvm::sys::path::native(PCHDir->getName(), DirNative);
276 bool Found = false;
277 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
278 Dir != DirEnd && !EC; Dir.increment(EC)) {
279 // Check whether this is an acceptable AST file.
280 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
281 CI.getLangOpts(),
Douglas Gregorb6368752012-10-24 23:41:50 +0000282 CI.getTargetOpts(),
283 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000284 PPOpts.ImplicitPCHInclude = Dir->path();
285 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000286 break;
287 }
288 }
289
290 if (!Found) {
291 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
292 return true;
293 }
294 }
295 }
296
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000297 // Set up the preprocessor.
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000298 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000299
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000300 // Inform the diagnostic client we are processing a source file.
301 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
302 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000303 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000304
305 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000306 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000307 goto failure;
308
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000309 // Initialize the main file entry. It is important that this occurs after
310 // BeginSourceFileAction, which may change CurrentInput during module builds.
311 if (!CI.InitializeSourceManager(CurrentInput))
312 goto failure;
313
James Dennettf8317672013-01-23 00:45:44 +0000314 // Create the AST context and consumer unless this is a preprocessor only
315 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000316 if (!usesPreprocessorOnly()) {
317 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000318
Ahmed Charlesb8984322014-03-07 20:03:18 +0000319 std::unique_ptr<ASTConsumer> Consumer(
320 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000321 if (!Consumer)
322 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000323
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000324 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000325
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000326 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
327 // Convert headers to PCH and chain them.
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000328 IntrusiveRefCntPtr<ChainedIncludesSource> source;
329 source = ChainedIncludesSource::create(CI);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000330 if (!source)
331 goto failure;
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000332 CI.setModuleManager(static_cast<ASTReader*>(&source->getFinalReader()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000333 CI.getASTContext().setExternalSource(source);
334
335 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
336 // Use PCH.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000337 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregor925296b2011-07-19 16:10:42 +0000338 ASTDeserializationListener *DeserialListener =
339 Consumer->GetASTDeserializationListener();
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000340 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls)
341 DeserialListener = new DeserializedDeclsDumper(DeserialListener);
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000342 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty())
343 DeserialListener = new DeserializedDeclsChecker(CI.getASTContext(),
344 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
345 DeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000346 CI.createPCHExternalASTSource(
Douglas Gregorce3a8292010-07-27 00:27:13 +0000347 CI.getPreprocessorOpts().ImplicitPCHInclude,
Sebastian Redl07a89a82010-07-30 00:29:29 +0000348 CI.getPreprocessorOpts().DisablePCHValidation,
Argyrios Kyrtzidis4a280ff2012-03-07 01:51:17 +0000349 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000350 DeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000351 if (!CI.getASTContext().getExternalSource())
352 goto failure;
353 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000354
Ahmed Charles9a16beb2014-03-07 19:33:25 +0000355 CI.setASTConsumer(Consumer.release());
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000356 if (!CI.hasASTConsumer())
357 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000358 }
359
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000360 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000361 // source.
362 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
363 Preprocessor &PP = CI.getPreprocessor();
364 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000365 PP.getLangOpts());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000366 }
367
Douglas Gregore9fc3772012-01-26 07:55:45 +0000368 // If there is a layout overrides file, attach an external AST source that
369 // provides the layouts from that file.
370 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
371 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000372 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000373 Override(new LayoutOverrideSource(
374 CI.getFrontendOpts().OverrideRecordLayoutsFile));
375 CI.getASTContext().setExternalSource(Override);
376 }
377
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000378 return true;
379
380 // If we failed, reset state since the client will not end up calling the
381 // matching EndSourceFile().
382 failure:
383 if (isCurrentFileAST()) {
Ted Kremenek5e14d392011-03-21 18:40:17 +0000384 CI.setASTContext(0);
385 CI.setPreprocessor(0);
386 CI.setSourceManager(0);
387 CI.setFileManager(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000388 }
389
Jordan Roseea762b02012-08-10 01:06:08 +0000390 if (HasBegunSourceFile)
391 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000392 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000393 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000394 setCompilerInstance(0);
395 return false;
396}
397
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000398bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000399 CompilerInstance &CI = getCompilerInstance();
400
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000401 if (CI.hasFrontendTimer()) {
402 llvm::TimeRegion Timer(CI.getFrontendTimer());
403 ExecuteAction();
404 }
405 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000406
Douglas Gregor5e306b12013-01-23 22:38:11 +0000407 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000408 // there were any module-build failures.
409 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
410 CI.hasPreprocessor()) {
Douglas Gregor5e306b12013-01-23 22:38:11 +0000411 GlobalModuleIndex::writeIndex(
412 CI.getFileManager(),
413 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
414 }
415
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000416 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000417}
418
419void FrontendAction::EndSourceFile() {
420 CompilerInstance &CI = getCompilerInstance();
421
Douglas Gregor1388a892011-02-09 18:47:31 +0000422 // Inform the diagnostic client we are done with this source file.
423 CI.getDiagnosticClient().EndSourceFile();
424
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000425 // Finalize the action.
426 EndSourceFileAction();
427
428 // Release the consumer and the AST, in that order since the consumer may
429 // perform actions in its destructor which require the context.
430 //
431 // FIXME: There is more per-file stuff we could just drop here?
432 if (CI.getFrontendOpts().DisableFree) {
Kostya Serebryanyce2c7262013-12-27 08:11:08 +0000433 BuryPointer(CI.takeASTConsumer());
Douglas Gregor0e93f012010-08-12 23:31:19 +0000434 if (!isCurrentFileAST()) {
Kostya Serebryanyce2c7262013-12-27 08:11:08 +0000435 BuryPointer(CI.takeSema());
Ted Kremenek5e14d392011-03-21 18:40:17 +0000436 CI.resetAndLeakASTContext();
Douglas Gregor0e93f012010-08-12 23:31:19 +0000437 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000438 } else {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000439 if (!isCurrentFileAST()) {
440 CI.setSema(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000441 CI.setASTContext(0);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000442 }
443 CI.setASTConsumer(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000444 }
445
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000446 // Inform the preprocessor we are done.
447 if (CI.hasPreprocessor())
448 CI.getPreprocessor().EndSourceFile();
449
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000450 if (CI.getFrontendOpts().ShowStats) {
451 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
452 CI.getPreprocessor().PrintStats();
453 CI.getPreprocessor().getIdentifierTable().PrintStats();
454 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
455 CI.getSourceManager().PrintStats();
456 llvm::errs() << "\n";
457 }
458
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000459 // Cleanup the output streams, and erase the output files if instructed by the
460 // FrontendAction.
461 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000462
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000463 if (isCurrentFileAST()) {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000464 CI.takeSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000465 CI.resetAndLeakASTContext();
466 CI.resetAndLeakPreprocessor();
467 CI.resetAndLeakSourceManager();
468 CI.resetAndLeakFileManager();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000469 }
470
471 setCompilerInstance(0);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000472 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000473}
474
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000475bool FrontendAction::shouldEraseOutputFiles() {
476 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
477}
478
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000479//===----------------------------------------------------------------------===//
480// Utility Actions
481//===----------------------------------------------------------------------===//
482
483void ASTFrontendAction::ExecuteAction() {
484 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000485 if (!CI.hasPreprocessor())
486 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000487
488 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
489 // here so the source manager would be initialized.
490 if (hasCodeCompletionSupport() &&
491 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
492 CI.createCodeCompletionConsumer();
493
494 // Use a code completion consumer?
495 CodeCompleteConsumer *CompletionConsumer = 0;
496 if (CI.hasCodeCompletionConsumer())
497 CompletionConsumer = &CI.getCodeCompletionConsumer();
498
Douglas Gregor0e93f012010-08-12 23:31:19 +0000499 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000500 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000501
Erik Verbruggen6e922512012-04-12 10:11:59 +0000502 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
503 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000504}
505
David Blaikie68e081d2011-12-20 02:48:34 +0000506void PluginASTAction::anchor() { }
507
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000508ASTConsumer *
509PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000510 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000511 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000512}
Chandler Carruthb5703512011-06-16 16:17:05 +0000513
514ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000515 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000516 return WrappedAction->CreateASTConsumer(CI, InFile);
517}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000518bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
519 return WrappedAction->BeginInvocation(CI);
520}
Chandler Carruthb5703512011-06-16 16:17:05 +0000521bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000522 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000523 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000524 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthb5703512011-06-16 16:17:05 +0000525 return WrappedAction->BeginSourceFileAction(CI, Filename);
526}
527void WrapperFrontendAction::ExecuteAction() {
528 WrappedAction->ExecuteAction();
529}
530void WrapperFrontendAction::EndSourceFileAction() {
531 WrappedAction->EndSourceFileAction();
532}
533
534bool WrapperFrontendAction::usesPreprocessorOnly() const {
535 return WrappedAction->usesPreprocessorOnly();
536}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000537TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
538 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000539}
540bool WrapperFrontendAction::hasPCHSupport() const {
541 return WrappedAction->hasPCHSupport();
542}
543bool WrapperFrontendAction::hasASTFileSupport() const {
544 return WrappedAction->hasASTFileSupport();
545}
546bool WrapperFrontendAction::hasIRSupport() const {
547 return WrappedAction->hasIRSupport();
548}
549bool WrapperFrontendAction::hasCodeCompletionSupport() const {
550 return WrappedAction->hasCodeCompletionSupport();
551}
552
553WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
554 : WrappedAction(WrappedAction) {}
555