blob: 149212290423de0a8a92842fa55f0cb82d2ab652 [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"
15#include "clang/Frontend/CompilerInstance.h"
16#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber2992efa2011-01-25 20:34:14 +000017#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000018#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber2992efa2011-01-25 20:34:14 +000019#include "clang/Frontend/MultiplexConsumer.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000020#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000021#include "clang/Lex/HeaderSearch.h"
22#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000023#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000024#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turner0248f572011-08-05 22:17:03 +000025#include "clang/Serialization/ASTReader.h"
Douglas Gregor5e306b12013-01-23 22:38:11 +000026#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000027#include "llvm/Support/ErrorHandling.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000030#include "llvm/Support/Timer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000031#include "llvm/Support/raw_ostream.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000032#include <system_error>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000033using namespace clang;
34
NAKAMURA Takumiad4c06c2014-07-11 15:06:24 +000035template class llvm::Registry<clang::PluginASTAction>;
36
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000037namespace {
38
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000039class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000040 ASTDeserializationListener *Previous;
Nico Weber824285e2014-05-08 04:26:47 +000041 bool DeletePrevious;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000042
43public:
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000044 explicit DelegatingDeserializationListener(
Nico Weber824285e2014-05-08 04:26:47 +000045 ASTDeserializationListener *Previous, bool DeletePrevious)
46 : Previous(Previous), DeletePrevious(DeletePrevious) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000047 ~DelegatingDeserializationListener() override {
Nico Weber824285e2014-05-08 04:26:47 +000048 if (DeletePrevious)
49 delete Previous;
50 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000051
Craig Topperafa7cb32014-03-13 06:07:04 +000052 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000053 if (Previous)
54 Previous->ReaderInitialized(Reader);
55 }
Craig Topperafa7cb32014-03-13 06:07:04 +000056 void IdentifierRead(serialization::IdentID ID,
57 IdentifierInfo *II) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000058 if (Previous)
59 Previous->IdentifierRead(ID, II);
60 }
Craig Topperafa7cb32014-03-13 06:07:04 +000061 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000062 if (Previous)
63 Previous->TypeRead(Idx, T);
64 }
Craig Topperafa7cb32014-03-13 06:07:04 +000065 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000066 if (Previous)
67 Previous->DeclRead(ID, D);
68 }
Craig Topperafa7cb32014-03-13 06:07:04 +000069 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000070 if (Previous)
71 Previous->SelectorRead(ID, Sel);
72 }
Craig Topperafa7cb32014-03-13 06:07:04 +000073 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
Richard Smith66a81862015-05-04 02:25:31 +000074 MacroDefinitionRecord *MD) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000075 if (Previous)
76 Previous->MacroDefinitionRead(PPID, MD);
77 }
78};
79
80/// \brief Dumps deserialized declarations.
81class DeserializedDeclsDumper : public DelegatingDeserializationListener {
82public:
Nico Weber824285e2014-05-08 04:26:47 +000083 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
84 bool DeletePrevious)
85 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000086
Craig Topperafa7cb32014-03-13 06:07:04 +000087 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000088 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
89 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramerdb0fc512012-02-07 11:57:57 +000090 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000091 llvm::outs() << "\n";
92
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000093 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000094 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000095};
96
David Blaikie48b81282012-05-29 17:05:42 +000097/// \brief Checks deserialized declarations and emits error if a name
98/// matches one given in command-line using -error-on-deserialized-decl.
99class DeserializedDeclsChecker : public DelegatingDeserializationListener {
100 ASTContext &Ctx;
101 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000102
David Blaikie48b81282012-05-29 17:05:42 +0000103public:
104 DeserializedDeclsChecker(ASTContext &Ctx,
105 const std::set<std::string> &NamesToCheck,
Nico Weber824285e2014-05-08 04:26:47 +0000106 ASTDeserializationListener *Previous,
107 bool DeletePrevious)
108 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
109 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000110
Craig Topperafa7cb32014-03-13 06:07:04 +0000111 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikie48b81282012-05-29 17:05:42 +0000112 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
113 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
114 unsigned DiagID
115 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
116 "%0 was deserialized");
117 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
118 << ND->getNameAsString();
119 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000120
David Blaikie48b81282012-05-29 17:05:42 +0000121 DelegatingDeserializationListener::DeclRead(ID, D);
122 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000123};
124
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000125} // end anonymous namespace
126
Craig Topper49a27902014-05-22 04:46:25 +0000127FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000128
129FrontendAction::~FrontendAction() {}
130
Douglas Gregor32fbe312012-01-20 16:28:04 +0000131void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
David Blaikief62d4e72014-08-10 17:03:42 +0000132 std::unique_ptr<ASTUnit> AST) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000133 this->CurrentInput = CurrentInput;
David Blaikief62d4e72014-08-10 17:03:42 +0000134 CurrentASTUnit = std::move(AST);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000135}
136
David Blaikie6beb6aa2014-08-10 19:56:51 +0000137std::unique_ptr<ASTConsumer>
138FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
139 StringRef InFile) {
140 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
Nico Weber2992efa2011-01-25 20:34:14 +0000141 if (!Consumer)
Craig Topper49a27902014-05-22 04:46:25 +0000142 return nullptr;
Nico Weber2992efa2011-01-25 20:34:14 +0000143
144 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
145 return Consumer;
146
147 // Make sure the non-plugin consumer is first, so that plugins can't
148 // modifiy the AST.
David Blaikie6beb6aa2014-08-10 19:56:51 +0000149 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
150 Consumers.push_back(std::move(Consumer));
Nico Weber2992efa2011-01-25 20:34:14 +0000151
152 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
153 i != e; ++i) {
154 // This is O(|plugins| * |add_plugins|), but since both numbers are
155 // way below 50 in practice, that's ok.
156 for (FrontendPluginRegistry::iterator
157 it = FrontendPluginRegistry::begin(),
158 ie = FrontendPluginRegistry::end();
159 it != ie; ++it) {
David Blaikie6beb6aa2014-08-10 19:56:51 +0000160 if (it->getName() != CI.getFrontendOpts().AddPluginActions[i])
161 continue;
162 std::unique_ptr<PluginASTAction> P = it->instantiate();
163 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
164 Consumers.push_back(P->CreateASTConsumer(CI, InFile));
Nico Weber2992efa2011-01-25 20:34:14 +0000165 }
166 }
167
David Blaikie6beb6aa2014-08-10 19:56:51 +0000168 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Nico Weber2992efa2011-01-25 20:34:14 +0000169}
170
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000171bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000172 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000173 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000174 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000175 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000176 setCompilerInstance(&CI);
177
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000178 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000179 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000180 if (!BeginInvocation(CI))
181 goto failure;
182
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000183 // AST files follow a very different path, since they share objects via the
184 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000185 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000186 assert(!usesPreprocessorOnly() &&
187 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000188 assert(hasASTFileSupport() &&
189 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000190
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000191 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000192
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000193 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
194 InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(),
195 CI.getCodeGenOpts().DebugTypeExtRefs);
David Blaikief62d4e72014-08-10 17:03:42 +0000196
Daniel Dunbar59203002009-12-03 01:45:44 +0000197 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000198 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000199
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000200 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000201 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000202 HasBegunSourceFile = true;
203
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000204 // Set the shared objects, these are reset when we finish processing the
205 // file, otherwise the CompilerInstance will happily destroy them.
206 CI.setFileManager(&AST->getFileManager());
207 CI.setSourceManager(&AST->getSourceManager());
208 CI.setPreprocessor(&AST->getPreprocessor());
209 CI.setASTContext(&AST->getASTContext());
210
David Blaikief62d4e72014-08-10 17:03:42 +0000211 setCurrentInput(Input, std::move(AST));
212
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000213 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000214 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000215 goto failure;
216
James Dennettf8317672013-01-23 00:45:44 +0000217 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000218 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000219 if (!CI.hasASTConsumer())
220 goto failure;
221
222 return true;
223 }
224
Ben Langmuir8832c062014-04-15 18:16:25 +0000225 if (!CI.hasVirtualFileSystem()) {
226 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
227 createVFSFromCompilerInvocation(CI.getInvocation(),
228 CI.getDiagnostics()))
229 CI.setVirtualFileSystem(VFS);
230 else
231 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000232 }
233
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000234 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000235 if (!CI.hasFileManager())
236 CI.createFileManager();
237 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000238 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000239
240 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000241 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000242 assert(hasIRSupport() &&
243 "This action does not have IR file support!");
244
245 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000246 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseea762b02012-08-10 01:06:08 +0000247 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000248
249 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000250 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000251 goto failure;
252
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000253 // Initialize the main file entry.
254 if (!CI.InitializeSourceManager(CurrentInput))
255 goto failure;
256
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000257 return true;
258 }
259
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000260 // If the implicit PCH include is actually a directory, rather than
261 // a single file, search for a suitable PCH file in that directory.
262 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
263 FileManager &FileMgr = CI.getFileManager();
264 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
265 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000266 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000267 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Rafael Espindolac0809172014-06-12 14:02:15 +0000268 std::error_code EC;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000269 SmallString<128> DirNative;
270 llvm::sys::path::native(PCHDir->getName(), DirNative);
271 bool Found = false;
Yaron Keren92e1b622015-03-18 10:17:07 +0000272 for (llvm::sys::fs::directory_iterator Dir(DirNative, EC), DirEnd;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000273 Dir != DirEnd && !EC; Dir.increment(EC)) {
274 // Check whether this is an acceptable AST file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000275 if (ASTReader::isAcceptableASTFile(
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000276 Dir->path(), FileMgr, CI.getPCHContainerReader(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000277 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
278 SpecificModuleCachePath)) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000279 PPOpts.ImplicitPCHInclude = Dir->path();
280 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000281 break;
282 }
283 }
284
285 if (!Found) {
286 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
287 return true;
288 }
289 }
290 }
291
Ted Kremenekeeccb302014-08-27 15:14:15 +0000292 // Set up the preprocessor if needed. When parsing model files the
293 // preprocessor of the original source is reused.
294 if (!isModelParsingAction())
295 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000296
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000297 // Inform the diagnostic client we are processing a source file.
298 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
299 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000300 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000301
302 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000303 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000304 goto failure;
305
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000306 // Initialize the main file entry. It is important that this occurs after
307 // BeginSourceFileAction, which may change CurrentInput during module builds.
308 if (!CI.InitializeSourceManager(CurrentInput))
309 goto failure;
310
James Dennettf8317672013-01-23 00:45:44 +0000311 // Create the AST context and consumer unless this is a preprocessor only
312 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000313 if (!usesPreprocessorOnly()) {
Ted Kremenekeeccb302014-08-27 15:14:15 +0000314 // Parsing a model file should reuse the existing ASTContext.
315 if (!isModelParsingAction())
316 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000317
David Blaikie6beb6aa2014-08-10 19:56:51 +0000318 std::unique_ptr<ASTConsumer> Consumer =
319 CreateWrappedASTConsumer(CI, InputFile);
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000320 if (!Consumer)
321 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000322
Ted Kremenekeeccb302014-08-27 15:14:15 +0000323 // FIXME: should not overwrite ASTMutationListener when parsing model files?
324 if (!isModelParsingAction())
325 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Richard Smithe842a472014-10-22 02:05:46 +0000326
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000327 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
328 // Convert headers to PCH and chain them.
Alp Toker9e0523d2014-07-07 11:07:10 +0000329 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
330 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000331 if (!source)
332 goto failure;
Alp Toker9e0523d2014-07-07 11:07:10 +0000333 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000334 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000335 } 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();
Nico Weber824285e2014-05-08 04:26:47 +0000340 bool DeleteDeserialListener = false;
341 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
342 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
343 DeleteDeserialListener);
344 DeleteDeserialListener = true;
345 }
346 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
347 DeserialListener = new DeserializedDeclsChecker(
348 CI.getASTContext(),
349 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
350 DeserialListener, DeleteDeserialListener);
351 DeleteDeserialListener = true;
352 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000353 CI.createPCHExternalASTSource(
Nico Weber824285e2014-05-08 04:26:47 +0000354 CI.getPreprocessorOpts().ImplicitPCHInclude,
355 CI.getPreprocessorOpts().DisablePCHValidation,
356 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
357 DeleteDeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000358 if (!CI.getASTContext().getExternalSource())
359 goto failure;
360 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000361
David Blaikie6beb6aa2014-08-10 19:56:51 +0000362 CI.setASTConsumer(std::move(Consumer));
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000363 if (!CI.hasASTConsumer())
364 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000365 }
366
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000367 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000368 // source.
369 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
370 Preprocessor &PP = CI.getPreprocessor();
Richard Smith053f6c62014-05-16 23:01:30 +0000371
372 // If modules are enabled, create the module manager before creating
373 // any builtins, so that all declarations know that they might be
374 // extended by an external source.
375 if (CI.getLangOpts().Modules)
376 CI.createModuleManager();
377
Eric Christopher02d5d862015-08-06 01:01:12 +0000378 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000379 PP.getLangOpts());
Richard Smith053f6c62014-05-16 23:01:30 +0000380 } else {
381 // FIXME: If this is a problem, recover from it by creating a multiplex
382 // source.
383 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
384 "modules enabled but created an external source that "
385 "doesn't support modules");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000386 }
387
Richard Smithac425e92015-01-23 00:01:13 +0000388 // If we were asked to load any module map files, do so now.
389 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
390 if (auto *File = CI.getFileManager().getFile(Filename))
391 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
392 File, /*IsSystem*/false);
393 else
394 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
395 }
396
Richard Smithd4b230b2014-10-27 23:01:16 +0000397 // If we were asked to load any module files, do so now.
398 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
399 if (!CI.loadModuleFile(ModuleFile))
Richard Smithe842a472014-10-22 02:05:46 +0000400 goto failure;
Richard Smithe842a472014-10-22 02:05:46 +0000401
Douglas Gregore9fc3772012-01-26 07:55:45 +0000402 // If there is a layout overrides file, attach an external AST source that
403 // provides the layouts from that file.
404 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
405 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000406 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000407 Override(new LayoutOverrideSource(
408 CI.getFrontendOpts().OverrideRecordLayoutsFile));
409 CI.getASTContext().setExternalSource(Override);
410 }
Richard Smith053f6c62014-05-16 23:01:30 +0000411
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000412 return true;
413
414 // If we failed, reset state since the client will not end up calling the
415 // matching EndSourceFile().
416 failure:
417 if (isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000418 CI.setASTContext(nullptr);
419 CI.setPreprocessor(nullptr);
420 CI.setSourceManager(nullptr);
421 CI.setFileManager(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000422 }
423
Jordan Roseea762b02012-08-10 01:06:08 +0000424 if (HasBegunSourceFile)
425 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000426 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000427 setCurrentInput(FrontendInputFile());
Craig Topper49a27902014-05-22 04:46:25 +0000428 setCompilerInstance(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000429 return false;
430}
431
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000432bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000433 CompilerInstance &CI = getCompilerInstance();
434
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000435 if (CI.hasFrontendTimer()) {
436 llvm::TimeRegion Timer(CI.getFrontendTimer());
437 ExecuteAction();
438 }
439 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000440
Douglas Gregor5e306b12013-01-23 22:38:11 +0000441 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000442 // there were any module-build failures.
443 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
444 CI.hasPreprocessor()) {
Richard Smith3938f0c2015-08-15 00:34:15 +0000445 StringRef Cache =
446 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
447 if (!Cache.empty())
448 GlobalModuleIndex::writeIndex(CI.getFileManager(),
449 CI.getPCHContainerReader(), Cache);
Douglas Gregor5e306b12013-01-23 22:38:11 +0000450 }
451
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000452 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000453}
454
455void FrontendAction::EndSourceFile() {
456 CompilerInstance &CI = getCompilerInstance();
457
Douglas Gregor1388a892011-02-09 18:47:31 +0000458 // Inform the diagnostic client we are done with this source file.
459 CI.getDiagnosticClient().EndSourceFile();
460
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000461 // Inform the preprocessor we are done.
462 if (CI.hasPreprocessor())
463 CI.getPreprocessor().EndSourceFile();
464
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000465 // Finalize the action.
466 EndSourceFileAction();
467
Nico Weber7de358e2014-04-24 02:42:04 +0000468 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000469 //
470 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000471 bool DisableFree = CI.getFrontendOpts().DisableFree;
472 if (DisableFree) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000473 CI.resetAndLeakSema();
474 CI.resetAndLeakASTContext();
David Blaikie6beb6aa2014-08-10 19:56:51 +0000475 BuryPointer(CI.takeASTConsumer().get());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000476 } else {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000477 CI.setSema(nullptr);
478 CI.setASTContext(nullptr);
Craig Topper49a27902014-05-22 04:46:25 +0000479 CI.setASTConsumer(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000480 }
481
482 if (CI.getFrontendOpts().ShowStats) {
483 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
484 CI.getPreprocessor().PrintStats();
485 CI.getPreprocessor().getIdentifierTable().PrintStats();
486 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
487 CI.getSourceManager().PrintStats();
488 llvm::errs() << "\n";
489 }
490
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000491 // Cleanup the output streams, and erase the output files if instructed by the
492 // FrontendAction.
493 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000494
Nico Weber1f29ccf2014-04-24 03:31:27 +0000495 if (isCurrentFileAST()) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000496 if (DisableFree) {
497 CI.resetAndLeakPreprocessor();
498 CI.resetAndLeakSourceManager();
499 CI.resetAndLeakFileManager();
500 } else {
501 CI.setPreprocessor(nullptr);
502 CI.setSourceManager(nullptr);
503 CI.setFileManager(nullptr);
504 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000505 }
506
Craig Topper49a27902014-05-22 04:46:25 +0000507 setCompilerInstance(nullptr);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000508 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000509}
510
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000511bool FrontendAction::shouldEraseOutputFiles() {
512 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
513}
514
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000515//===----------------------------------------------------------------------===//
516// Utility Actions
517//===----------------------------------------------------------------------===//
518
519void ASTFrontendAction::ExecuteAction() {
520 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000521 if (!CI.hasPreprocessor())
522 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000523
524 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
525 // here so the source manager would be initialized.
526 if (hasCodeCompletionSupport() &&
527 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
528 CI.createCodeCompletionConsumer();
529
530 // Use a code completion consumer?
Craig Topper49a27902014-05-22 04:46:25 +0000531 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000532 if (CI.hasCodeCompletionConsumer())
533 CompletionConsumer = &CI.getCodeCompletionConsumer();
534
Douglas Gregor0e93f012010-08-12 23:31:19 +0000535 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000536 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000537
Erik Verbruggen6e922512012-04-12 10:11:59 +0000538 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
539 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000540}
541
David Blaikie68e081d2011-12-20 02:48:34 +0000542void PluginASTAction::anchor() { }
543
David Blaikie6beb6aa2014-08-10 19:56:51 +0000544std::unique_ptr<ASTConsumer>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000545PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000546 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000547 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000548}
Chandler Carruthb5703512011-06-16 16:17:05 +0000549
David Blaikie6beb6aa2014-08-10 19:56:51 +0000550std::unique_ptr<ASTConsumer>
551WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
552 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000553 return WrappedAction->CreateASTConsumer(CI, InFile);
554}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000555bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
556 return WrappedAction->BeginInvocation(CI);
557}
Chandler Carruthb5703512011-06-16 16:17:05 +0000558bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000559 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000560 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000561 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthb5703512011-06-16 16:17:05 +0000562 return WrappedAction->BeginSourceFileAction(CI, Filename);
563}
564void WrapperFrontendAction::ExecuteAction() {
565 WrappedAction->ExecuteAction();
566}
567void WrapperFrontendAction::EndSourceFileAction() {
568 WrappedAction->EndSourceFileAction();
569}
570
571bool WrapperFrontendAction::usesPreprocessorOnly() const {
572 return WrappedAction->usesPreprocessorOnly();
573}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000574TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
575 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000576}
577bool WrapperFrontendAction::hasPCHSupport() const {
578 return WrappedAction->hasPCHSupport();
579}
580bool WrapperFrontendAction::hasASTFileSupport() const {
581 return WrappedAction->hasASTFileSupport();
582}
583bool WrapperFrontendAction::hasIRSupport() const {
584 return WrappedAction->hasIRSupport();
585}
586bool WrapperFrontendAction::hasCodeCompletionSupport() const {
587 return WrappedAction->hasCodeCompletionSupport();
588}
589
590WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
591 : WrappedAction(WrappedAction) {}
592