blob: b08574907437d0a72eb0c8eb075e87572e93b7c0 [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;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070040 bool DeletePrevious;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000041
42public:
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000043 explicit DelegatingDeserializationListener(
Stephen Hines6bcf27b2014-05-29 04:14:42 -070044 ASTDeserializationListener *Previous, bool DeletePrevious)
45 : Previous(Previous), DeletePrevious(DeletePrevious) {}
46 virtual ~DelegatingDeserializationListener() {
47 if (DeletePrevious)
48 delete Previous;
49 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000050
Stephen Hines651f13c2014-04-23 16:59:28 -070051 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000052 if (Previous)
53 Previous->ReaderInitialized(Reader);
54 }
Stephen Hines651f13c2014-04-23 16:59:28 -070055 void IdentifierRead(serialization::IdentID ID,
56 IdentifierInfo *II) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000057 if (Previous)
58 Previous->IdentifierRead(ID, II);
59 }
Stephen Hines651f13c2014-04-23 16:59:28 -070060 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000061 if (Previous)
62 Previous->TypeRead(Idx, T);
63 }
Stephen Hines651f13c2014-04-23 16:59:28 -070064 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000065 if (Previous)
66 Previous->DeclRead(ID, D);
67 }
Stephen Hines651f13c2014-04-23 16:59:28 -070068 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000069 if (Previous)
70 Previous->SelectorRead(ID, Sel);
71 }
Stephen Hines651f13c2014-04-23 16:59:28 -070072 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
73 MacroDefinition *MD) override {
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000074 if (Previous)
75 Previous->MacroDefinitionRead(PPID, MD);
76 }
77};
78
79/// \brief Dumps deserialized declarations.
80class DeserializedDeclsDumper : public DelegatingDeserializationListener {
81public:
Stephen Hines6bcf27b2014-05-29 04:14:42 -070082 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
83 bool DeletePrevious)
84 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000085
Stephen Hines651f13c2014-04-23 16:59:28 -070086 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000087 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
88 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramera59d20b2012-02-07 11:57:57 +000089 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000090 llvm::outs() << "\n";
91
Argyrios Kyrtzidis407ef9a2011-10-28 22:54:31 +000092 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000093 }
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +000094};
95
David Blaikiee3f34112012-05-29 17:05:42 +000096/// \brief Checks deserialized declarations and emits error if a name
97/// matches one given in command-line using -error-on-deserialized-decl.
98class DeserializedDeclsChecker : public DelegatingDeserializationListener {
99 ASTContext &Ctx;
100 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000101
David Blaikiee3f34112012-05-29 17:05:42 +0000102public:
103 DeserializedDeclsChecker(ASTContext &Ctx,
104 const std::set<std::string> &NamesToCheck,
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700105 ASTDeserializationListener *Previous,
106 bool DeletePrevious)
107 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
108 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000109
Stephen Hines651f13c2014-04-23 16:59:28 -0700110 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikiee3f34112012-05-29 17:05:42 +0000111 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
112 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
113 unsigned DiagID
114 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
115 "%0 was deserialized");
116 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
117 << ND->getNameAsString();
118 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000119
David Blaikiee3f34112012-05-29 17:05:42 +0000120 DelegatingDeserializationListener::DeclRead(ID, D);
121 }
Argyrios Kyrtzidis3e785932010-10-14 20:14:25 +0000122};
123
Argyrios Kyrtzidisb9728582010-10-14 20:14:18 +0000124} // end anonymous namespace
125
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700126FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000127
128FrontendAction::~FrontendAction() {}
129
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000130void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
131 ASTUnit *AST) {
132 this->CurrentInput = CurrentInput;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000133 CurrentASTUnit.reset(AST);
134}
135
Nico Weber5aa74af2011-01-25 20:34:14 +0000136ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000137 StringRef InFile) {
Nico Weber5aa74af2011-01-25 20:34:14 +0000138 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
139 if (!Consumer)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700140 return nullptr;
Nico Weber5aa74af2011-01-25 20:34:14 +0000141
142 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
143 return Consumer;
144
145 // Make sure the non-plugin consumer is first, so that plugins can't
146 // modifiy the AST.
147 std::vector<ASTConsumer*> Consumers(1, Consumer);
148
149 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
150 i != e; ++i) {
151 // This is O(|plugins| * |add_plugins|), but since both numbers are
152 // way below 50 in practice, that's ok.
153 for (FrontendPluginRegistry::iterator
154 it = FrontendPluginRegistry::begin(),
155 ie = FrontendPluginRegistry::end();
156 it != ie; ++it) {
157 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700158 std::unique_ptr<PluginASTAction> P(it->instantiate());
Nico Weber5aa74af2011-01-25 20:34:14 +0000159 FrontendAction* c = P.get();
Nico Weberf25649c2011-01-29 21:21:49 +0000160 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber5aa74af2011-01-25 20:34:14 +0000161 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
162 }
163 }
164 }
165
166 return new MultiplexConsumer(Consumers);
167}
168
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000169bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000170 const FrontendInputFile &Input) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000171 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000172 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000173 setCurrentInput(Input);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000174 setCompilerInstance(&CI);
175
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000176 StringRef InputFile = Input.getFile();
Jordan Roseaf6cf432012-08-10 01:06:08 +0000177 bool HasBegunSourceFile = false;
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000178 if (!BeginInvocation(CI))
179 goto failure;
180
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000181 // AST files follow a very different path, since they share objects via the
182 // AST unit.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000183 if (Input.getKind() == IK_AST) {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000184 assert(!usesPreprocessorOnly() &&
185 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbareb58d832010-06-07 23:24:43 +0000186 assert(hasASTFileSupport() &&
187 "This action does not have AST file support!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000188
Dylan Noblesmithc93dc782012-02-20 14:00:23 +0000189 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Stephen Hines651f13c2014-04-23 16:59:28 -0700190
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000191 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000192 CI.getFileSystemOpts());
Daniel Dunbar5262fda2009-12-03 01:45:44 +0000193 if (!AST)
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000194 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000195
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000196 setCurrentInput(Input, AST);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000197
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000198 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700199 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidis62ba4ba2013-03-18 22:55:24 +0000200 HasBegunSourceFile = true;
201
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000202 // Set the shared objects, these are reset when we finish processing the
203 // file, otherwise the CompilerInstance will happily destroy them.
204 CI.setFileManager(&AST->getFileManager());
205 CI.setSourceManager(&AST->getSourceManager());
206 CI.setPreprocessor(&AST->getPreprocessor());
207 CI.setASTContext(&AST->getASTContext());
208
209 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000210 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000211 goto failure;
212
James Dennett18f43a62013-01-23 00:45:44 +0000213 // Create the AST consumer.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000214 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000215 if (!CI.hasASTConsumer())
216 goto failure;
217
218 return true;
219 }
220
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700221 if (!CI.hasVirtualFileSystem()) {
222 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
223 createVFSFromCompilerInvocation(CI.getInvocation(),
224 CI.getDiagnostics()))
225 CI.setVirtualFileSystem(VFS);
226 else
227 goto failure;
Stephen Hines651f13c2014-04-23 16:59:28 -0700228 }
229
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000230 // Set up the file and source managers, if needed.
Daniel Dunbar20560482010-06-07 23:23:50 +0000231 if (!CI.hasFileManager())
232 CI.createFileManager();
233 if (!CI.hasSourceManager())
Chris Lattner39b49bc2010-11-23 08:35:12 +0000234 CI.createSourceManager(CI.getFileManager());
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000235
236 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000237 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000238 assert(hasIRSupport() &&
239 "This action does not have IR file support!");
240
241 // Inform the diagnostic client we are processing a source file.
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700242 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseaf6cf432012-08-10 01:06:08 +0000243 HasBegunSourceFile = true;
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000244
245 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000246 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000247 goto failure;
248
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700249 // Initialize the main file entry.
250 if (!CI.InitializeSourceManager(CurrentInput))
251 goto failure;
252
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000253 return true;
254 }
255
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000256 // If the implicit PCH include is actually a directory, rather than
257 // a single file, search for a suitable PCH file in that directory.
258 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
259 FileManager &FileMgr = CI.getFileManager();
260 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
261 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
262 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
263 llvm::error_code EC;
264 SmallString<128> DirNative;
265 llvm::sys::path::native(PCHDir->getName(), DirNative);
266 bool Found = false;
267 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
268 Dir != DirEnd && !EC; Dir.increment(EC)) {
269 // Check whether this is an acceptable AST file.
270 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
271 CI.getLangOpts(),
Douglas Gregor4c0c7e82012-10-24 23:41:50 +0000272 CI.getTargetOpts(),
273 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis3ad86fd2013-02-05 16:36:52 +0000274 PPOpts.ImplicitPCHInclude = Dir->path();
275 Found = true;
Douglas Gregor27ffa6c2012-10-23 06:18:24 +0000276 break;
277 }
278 }
279
280 if (!Found) {
281 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
282 return true;
283 }
284 }
285 }
286
Daniel Dunbarfaddc3e2010-06-07 23:26:47 +0000287 // Set up the preprocessor.
Stephen Hines651f13c2014-04-23 16:59:28 -0700288 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbar20560482010-06-07 23:23:50 +0000289
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000290 // Inform the diagnostic client we are processing a source file.
291 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
292 &CI.getPreprocessor());
Jordan Roseaf6cf432012-08-10 01:06:08 +0000293 HasBegunSourceFile = true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000294
295 // Initialize the action.
Argyrios Kyrtzidis8616f9a2012-11-09 19:40:39 +0000296 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000297 goto failure;
298
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700299 // Initialize the main file entry. It is important that this occurs after
300 // BeginSourceFileAction, which may change CurrentInput during module builds.
301 if (!CI.InitializeSourceManager(CurrentInput))
302 goto failure;
303
James Dennett18f43a62013-01-23 00:45:44 +0000304 // Create the AST context and consumer unless this is a preprocessor only
305 // action.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000306 if (!usesPreprocessorOnly()) {
307 CI.createASTContext();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000308
Stephen Hines651f13c2014-04-23 16:59:28 -0700309 std::unique_ptr<ASTConsumer> Consumer(
310 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahaniand3057192010-10-29 19:49:13 +0000311 if (!Consumer)
312 goto failure;
Sebastian Redlffaab3e2010-07-30 00:29:29 +0000313
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +0000314 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregora8235d62012-10-09 23:05:51 +0000315
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000316 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
317 // Convert headers to PCH and chain them.
Stephen Hines651f13c2014-04-23 16:59:28 -0700318 IntrusiveRefCntPtr<ChainedIncludesSource> source;
319 source = ChainedIncludesSource::create(CI);
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000320 if (!source)
321 goto failure;
Stephen Hines651f13c2014-04-23 16:59:28 -0700322 CI.setModuleManager(static_cast<ASTReader*>(&source->getFinalReader()));
Argyrios Kyrtzidisb0f4b9a2011-03-09 17:21:42 +0000323 CI.getASTContext().setExternalSource(source);
324
325 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
326 // Use PCH.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000327 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000328 ASTDeserializationListener *DeserialListener =
329 Consumer->GetASTDeserializationListener();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700330 bool DeleteDeserialListener = false;
331 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
332 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
333 DeleteDeserialListener);
334 DeleteDeserialListener = true;
335 }
336 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
337 DeserialListener = new DeserializedDeclsChecker(
338 CI.getASTContext(),
339 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
340 DeserialListener, DeleteDeserialListener);
341 DeleteDeserialListener = true;
342 }
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000343 CI.createPCHExternalASTSource(
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700344 CI.getPreprocessorOpts().ImplicitPCHInclude,
345 CI.getPreprocessorOpts().DisablePCHValidation,
346 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
347 DeleteDeserialListener);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000348 if (!CI.getASTContext().getExternalSource())
349 goto failure;
350 }
Sebastian Redl77f46032010-07-09 21:00:24 +0000351
Stephen Hines651f13c2014-04-23 16:59:28 -0700352 CI.setASTConsumer(Consumer.release());
Sebastian Redl77f46032010-07-09 21:00:24 +0000353 if (!CI.hasASTConsumer())
354 goto failure;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000355 }
356
Jonathan D. Turnere735e2d2011-08-05 22:17:03 +0000357 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000358 // source.
359 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
360 Preprocessor &PP = CI.getPreprocessor();
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700361
362 // If modules are enabled, create the module manager before creating
363 // any builtins, so that all declarations know that they might be
364 // extended by an external source.
365 if (CI.getLangOpts().Modules)
366 CI.createModuleManager();
367
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000368 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikie4e4d0842012-03-11 07:00:24 +0000369 PP.getLangOpts());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700370 } else {
371 // FIXME: If this is a problem, recover from it by creating a multiplex
372 // source.
373 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
374 "modules enabled but created an external source that "
375 "doesn't support modules");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000376 }
377
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000378 // If there is a layout overrides file, attach an external AST source that
379 // provides the layouts from that file.
380 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
381 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700382 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregor453dbcb2012-01-26 07:55:45 +0000383 Override(new LayoutOverrideSource(
384 CI.getFrontendOpts().OverrideRecordLayoutsFile));
385 CI.getASTContext().setExternalSource(Override);
386 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700387
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000388 return true;
389
390 // If we failed, reset state since the client will not end up calling the
391 // matching EndSourceFile().
392 failure:
393 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700394 CI.setASTContext(nullptr);
395 CI.setPreprocessor(nullptr);
396 CI.setSourceManager(nullptr);
397 CI.setFileManager(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000398 }
399
Jordan Roseaf6cf432012-08-10 01:06:08 +0000400 if (HasBegunSourceFile)
401 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramerac447fc2012-10-14 19:21:21 +0000402 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000403 setCurrentInput(FrontendInputFile());
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700404 setCompilerInstance(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000405 return false;
406}
407
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000408bool FrontendAction::Execute() {
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000409 CompilerInstance &CI = getCompilerInstance();
410
Kovarththanan Rajaratnamf79bafa2009-11-29 09:57:35 +0000411 if (CI.hasFrontendTimer()) {
412 llvm::TimeRegion Timer(CI.getFrontendTimer());
413 ExecuteAction();
414 }
415 else ExecuteAction();
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000416
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000417 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorf575d6e2013-01-25 00:45:27 +0000418 // there were any module-build failures.
419 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
420 CI.hasPreprocessor()) {
Douglas Gregora6b00fc2013-01-23 22:38:11 +0000421 GlobalModuleIndex::writeIndex(
422 CI.getFileManager(),
423 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
424 }
425
Argyrios Kyrtzidis374a00b2012-06-08 05:48:06 +0000426 return true;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000427}
428
429void FrontendAction::EndSourceFile() {
430 CompilerInstance &CI = getCompilerInstance();
431
Douglas Gregor92b97f22011-02-09 18:47:31 +0000432 // Inform the diagnostic client we are done with this source file.
433 CI.getDiagnosticClient().EndSourceFile();
434
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000435 // Finalize the action.
436 EndSourceFileAction();
437
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700438 // Sema references the ast consumer, so reset sema first.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000439 //
440 // FIXME: There is more per-file stuff we could just drop here?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700441 bool DisableFree = CI.getFrontendOpts().DisableFree;
442 if (DisableFree) {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000443 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700444 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000445 CI.resetAndLeakASTContext();
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000446 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700447 BuryPointer(CI.takeASTConsumer());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000448 } else {
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000449 if (!isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700450 CI.setSema(nullptr);
451 CI.setASTContext(nullptr);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000452 }
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700453 CI.setASTConsumer(nullptr);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000454 }
455
Daniel Dunbardbd82092010-03-23 05:09:10 +0000456 // Inform the preprocessor we are done.
457 if (CI.hasPreprocessor())
458 CI.getPreprocessor().EndSourceFile();
459
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000460 if (CI.getFrontendOpts().ShowStats) {
461 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
462 CI.getPreprocessor().PrintStats();
463 CI.getPreprocessor().getIdentifierTable().PrintStats();
464 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
465 CI.getSourceManager().PrintStats();
466 llvm::errs() << "\n";
467 }
468
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000469 // Cleanup the output streams, and erase the output files if instructed by the
470 // FrontendAction.
471 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000472
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700473 // FIXME: Only do this if DisableFree is set.
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000474 if (isCurrentFileAST()) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700475 CI.resetAndLeakSema();
Ted Kremenek4f327862011-03-21 18:40:17 +0000476 CI.resetAndLeakASTContext();
477 CI.resetAndLeakPreprocessor();
478 CI.resetAndLeakSourceManager();
479 CI.resetAndLeakFileManager();
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000480 }
481
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700482 setCompilerInstance(nullptr);
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000483 setCurrentInput(FrontendInputFile());
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000484}
485
Argyrios Kyrtzidis1f01f7c2013-06-11 00:36:55 +0000486bool FrontendAction::shouldEraseOutputFiles() {
487 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
488}
489
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000490//===----------------------------------------------------------------------===//
491// Utility Actions
492//===----------------------------------------------------------------------===//
493
494void ASTFrontendAction::ExecuteAction() {
495 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola0046ce52013-07-28 13:23:37 +0000496 if (!CI.hasPreprocessor())
497 return;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000498
499 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
500 // here so the source manager would be initialized.
501 if (hasCodeCompletionSupport() &&
502 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
503 CI.createCodeCompletionConsumer();
504
505 // Use a code completion consumer?
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700506 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000507 if (CI.hasCodeCompletionConsumer())
508 CompletionConsumer = &CI.getCodeCompletionConsumer();
509
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000510 if (!CI.hasSema())
Douglas Gregor467dc882011-08-25 22:30:56 +0000511 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregorf18d0d82010-08-12 23:31:19 +0000512
Erik Verbruggen6a91d382012-04-12 10:11:59 +0000513 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
514 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000515}
516
David Blaikie99ba9e32011-12-20 02:48:34 +0000517void PluginASTAction::anchor() { }
518
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000519ASTConsumer *
520PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000521 StringRef InFile) {
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000522 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbar4ee24092009-11-14 10:42:35 +0000523}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000524
525ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000526 StringRef InFile) {
Chandler Carruthf7f81882011-06-16 16:17:05 +0000527 return WrappedAction->CreateASTConsumer(CI, InFile);
528}
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000529bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
530 return WrappedAction->BeginInvocation(CI);
531}
Chandler Carruthf7f81882011-06-16 16:17:05 +0000532bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000533 StringRef Filename) {
Douglas Gregor1f6b2b52012-01-20 16:28:04 +0000534 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidise665d692011-06-18 00:53:41 +0000535 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthf7f81882011-06-16 16:17:05 +0000536 return WrappedAction->BeginSourceFileAction(CI, Filename);
537}
538void WrapperFrontendAction::ExecuteAction() {
539 WrappedAction->ExecuteAction();
540}
541void WrapperFrontendAction::EndSourceFileAction() {
542 WrappedAction->EndSourceFileAction();
543}
544
545bool WrapperFrontendAction::usesPreprocessorOnly() const {
546 return WrappedAction->usesPreprocessorOnly();
547}
Douglas Gregor467dc882011-08-25 22:30:56 +0000548TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
549 return WrappedAction->getTranslationUnitKind();
Chandler Carruthf7f81882011-06-16 16:17:05 +0000550}
551bool WrapperFrontendAction::hasPCHSupport() const {
552 return WrappedAction->hasPCHSupport();
553}
554bool WrapperFrontendAction::hasASTFileSupport() const {
555 return WrappedAction->hasASTFileSupport();
556}
557bool WrapperFrontendAction::hasIRSupport() const {
558 return WrappedAction->hasIRSupport();
559}
560bool WrapperFrontendAction::hasCodeCompletionSupport() const {
561 return WrappedAction->hasCodeCompletionSupport();
562}
563
564WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
565 : WrappedAction(WrappedAction) {}
566