blob: cd3dd1f17dd8008c95b70609902004358efbacd6 [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) {}
47 virtual ~DelegatingDeserializationListener() {
48 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,
74 MacroDefinition *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 Blaikie62a56f32014-07-17 22:34:12 +0000137ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
138 StringRef InFile) {
139 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
Nico Weber2992efa2011-01-25 20:34:14 +0000140 if (!Consumer)
Craig Topper49a27902014-05-22 04:46:25 +0000141 return nullptr;
Nico Weber2992efa2011-01-25 20:34:14 +0000142
143 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
144 return Consumer;
145
146 // Make sure the non-plugin consumer is first, so that plugins can't
147 // modifiy the AST.
David Blaikie62a56f32014-07-17 22:34:12 +0000148 std::vector<ASTConsumer*> Consumers(1, Consumer);
Nico Weber2992efa2011-01-25 20:34:14 +0000149
150 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
151 i != e; ++i) {
152 // This is O(|plugins| * |add_plugins|), but since both numbers are
153 // way below 50 in practice, that's ok.
154 for (FrontendPluginRegistry::iterator
155 it = FrontendPluginRegistry::begin(),
156 ie = FrontendPluginRegistry::end();
157 it != ie; ++it) {
David Blaikie62a56f32014-07-17 22:34:12 +0000158 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
159 std::unique_ptr<PluginASTAction> P(it->instantiate());
160 FrontendAction* c = P.get();
161 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
162 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
163 }
Nico Weber2992efa2011-01-25 20:34:14 +0000164 }
165 }
166
David Blaikie62a56f32014-07-17 22:34:12 +0000167 return new MultiplexConsumer(Consumers);
Nico Weber2992efa2011-01-25 20:34:14 +0000168}
169
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000170bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000171 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000172 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000173 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000174 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000175 setCompilerInstance(&CI);
176
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000177 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000178 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000179 if (!BeginInvocation(CI))
180 goto failure;
181
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000182 // AST files follow a very different path, since they share objects via the
183 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000184 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000185 assert(!usesPreprocessorOnly() &&
186 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000187 assert(hasASTFileSupport() &&
188 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000189
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000190 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000191
David Blaikie6f7382d2014-08-10 19:08:04 +0000192 std::unique_ptr<ASTUnit> AST =
193 ASTUnit::LoadFromASTFile(InputFile, Diags, CI.getFileSystemOpts());
David Blaikief62d4e72014-08-10 17:03:42 +0000194
Daniel Dunbar59203002009-12-03 01:45:44 +0000195 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000196 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000197
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000198 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000199 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000200 HasBegunSourceFile = true;
201
Daniel Dunbara0ff58d2009-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
David Blaikief62d4e72014-08-10 17:03:42 +0000209 setCurrentInput(Input, std::move(AST));
210
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000211 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000212 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000213 goto failure;
214
James Dennettf8317672013-01-23 00:45:44 +0000215 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000216 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000217 if (!CI.hasASTConsumer())
218 goto failure;
219
220 return true;
221 }
222
Ben Langmuir8832c062014-04-15 18:16:25 +0000223 if (!CI.hasVirtualFileSystem()) {
224 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
225 createVFSFromCompilerInvocation(CI.getInvocation(),
226 CI.getDiagnostics()))
227 CI.setVirtualFileSystem(VFS);
228 else
229 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000230 }
231
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000232 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000233 if (!CI.hasFileManager())
234 CI.createFileManager();
235 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000236 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000237
238 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000239 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000240 assert(hasIRSupport() &&
241 "This action does not have IR file support!");
242
243 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000244 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseea762b02012-08-10 01:06:08 +0000245 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000246
247 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000248 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000249 goto failure;
250
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000251 // Initialize the main file entry.
252 if (!CI.InitializeSourceManager(CurrentInput))
253 goto failure;
254
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000255 return true;
256 }
257
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000258 // If the implicit PCH include is actually a directory, rather than
259 // a single file, search for a suitable PCH file in that directory.
260 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
261 FileManager &FileMgr = CI.getFileManager();
262 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
263 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
264 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Rafael Espindolac0809172014-06-12 14:02:15 +0000265 std::error_code EC;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000266 SmallString<128> DirNative;
267 llvm::sys::path::native(PCHDir->getName(), DirNative);
268 bool Found = false;
269 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
270 Dir != DirEnd && !EC; Dir.increment(EC)) {
271 // Check whether this is an acceptable AST file.
272 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
273 CI.getLangOpts(),
Douglas Gregorb6368752012-10-24 23:41:50 +0000274 CI.getTargetOpts(),
275 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000276 PPOpts.ImplicitPCHInclude = Dir->path();
277 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000278 break;
279 }
280 }
281
282 if (!Found) {
283 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
284 return true;
285 }
286 }
287 }
288
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000289 // Set up the preprocessor.
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000290 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000291
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000292 // Inform the diagnostic client we are processing a source file.
293 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
294 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000295 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000296
297 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000298 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000299 goto failure;
300
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000301 // Initialize the main file entry. It is important that this occurs after
302 // BeginSourceFileAction, which may change CurrentInput during module builds.
303 if (!CI.InitializeSourceManager(CurrentInput))
304 goto failure;
305
James Dennettf8317672013-01-23 00:45:44 +0000306 // Create the AST context and consumer unless this is a preprocessor only
307 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000308 if (!usesPreprocessorOnly()) {
309 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000310
David Blaikie62a56f32014-07-17 22:34:12 +0000311 std::unique_ptr<ASTConsumer> Consumer(
312 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000313 if (!Consumer)
314 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000315
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000316 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000317
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000318 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
319 // Convert headers to PCH and chain them.
Alp Toker9e0523d2014-07-07 11:07:10 +0000320 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
321 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000322 if (!source)
323 goto failure;
Alp Toker9e0523d2014-07-07 11:07:10 +0000324 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000325 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000326 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
327 // Use PCH.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000328 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregor925296b2011-07-19 16:10:42 +0000329 ASTDeserializationListener *DeserialListener =
330 Consumer->GetASTDeserializationListener();
Nico Weber824285e2014-05-08 04:26:47 +0000331 bool DeleteDeserialListener = false;
332 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
333 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
334 DeleteDeserialListener);
335 DeleteDeserialListener = true;
336 }
337 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
338 DeserialListener = new DeserializedDeclsChecker(
339 CI.getASTContext(),
340 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
341 DeserialListener, DeleteDeserialListener);
342 DeleteDeserialListener = true;
343 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000344 CI.createPCHExternalASTSource(
Nico Weber824285e2014-05-08 04:26:47 +0000345 CI.getPreprocessorOpts().ImplicitPCHInclude,
346 CI.getPreprocessorOpts().DisablePCHValidation,
347 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
348 DeleteDeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000349 if (!CI.getASTContext().getExternalSource())
350 goto failure;
351 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000352
David Blaikie62a56f32014-07-17 22:34:12 +0000353 CI.setASTConsumer(Consumer.release());
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000354 if (!CI.hasASTConsumer())
355 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000356 }
357
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000358 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000359 // source.
360 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
361 Preprocessor &PP = CI.getPreprocessor();
Richard Smith053f6c62014-05-16 23:01:30 +0000362
363 // If modules are enabled, create the module manager before creating
364 // any builtins, so that all declarations know that they might be
365 // extended by an external source.
366 if (CI.getLangOpts().Modules)
367 CI.createModuleManager();
368
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000369 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000370 PP.getLangOpts());
Richard Smith053f6c62014-05-16 23:01:30 +0000371 } else {
372 // FIXME: If this is a problem, recover from it by creating a multiplex
373 // source.
374 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
375 "modules enabled but created an external source that "
376 "doesn't support modules");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000377 }
378
Douglas Gregore9fc3772012-01-26 07:55:45 +0000379 // If there is a layout overrides file, attach an external AST source that
380 // provides the layouts from that file.
381 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
382 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000383 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000384 Override(new LayoutOverrideSource(
385 CI.getFrontendOpts().OverrideRecordLayoutsFile));
386 CI.getASTContext().setExternalSource(Override);
387 }
Richard Smith053f6c62014-05-16 23:01:30 +0000388
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000389 return true;
390
391 // If we failed, reset state since the client will not end up calling the
392 // matching EndSourceFile().
393 failure:
394 if (isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000395 CI.setASTContext(nullptr);
396 CI.setPreprocessor(nullptr);
397 CI.setSourceManager(nullptr);
398 CI.setFileManager(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000399 }
400
Jordan Roseea762b02012-08-10 01:06:08 +0000401 if (HasBegunSourceFile)
402 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000403 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000404 setCurrentInput(FrontendInputFile());
Craig Topper49a27902014-05-22 04:46:25 +0000405 setCompilerInstance(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000406 return false;
407}
408
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000409bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000410 CompilerInstance &CI = getCompilerInstance();
411
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000412 if (CI.hasFrontendTimer()) {
413 llvm::TimeRegion Timer(CI.getFrontendTimer());
414 ExecuteAction();
415 }
416 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000417
Douglas Gregor5e306b12013-01-23 22:38:11 +0000418 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000419 // there were any module-build failures.
420 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
421 CI.hasPreprocessor()) {
Douglas Gregor5e306b12013-01-23 22:38:11 +0000422 GlobalModuleIndex::writeIndex(
423 CI.getFileManager(),
424 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
425 }
426
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000427 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000428}
429
430void FrontendAction::EndSourceFile() {
431 CompilerInstance &CI = getCompilerInstance();
432
Douglas Gregor1388a892011-02-09 18:47:31 +0000433 // Inform the diagnostic client we are done with this source file.
434 CI.getDiagnosticClient().EndSourceFile();
435
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000436 // Inform the preprocessor we are done.
437 if (CI.hasPreprocessor())
438 CI.getPreprocessor().EndSourceFile();
439
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000440 // Finalize the action.
441 EndSourceFileAction();
442
Nico Weber7de358e2014-04-24 02:42:04 +0000443 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000444 //
445 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000446 bool DisableFree = CI.getFrontendOpts().DisableFree;
447 if (DisableFree) {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000448 if (!isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000449 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000450 CI.resetAndLeakASTContext();
Douglas Gregor0e93f012010-08-12 23:31:19 +0000451 }
David Blaikie62a56f32014-07-17 22:34:12 +0000452 BuryPointer(CI.takeASTConsumer());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000453 } else {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000454 if (!isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000455 CI.setSema(nullptr);
456 CI.setASTContext(nullptr);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000457 }
Craig Topper49a27902014-05-22 04:46:25 +0000458 CI.setASTConsumer(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000459 }
460
461 if (CI.getFrontendOpts().ShowStats) {
462 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
463 CI.getPreprocessor().PrintStats();
464 CI.getPreprocessor().getIdentifierTable().PrintStats();
465 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
466 CI.getSourceManager().PrintStats();
467 llvm::errs() << "\n";
468 }
469
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000470 // Cleanup the output streams, and erase the output files if instructed by the
471 // FrontendAction.
472 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000473
Nico Weber1f29ccf2014-04-24 03:31:27 +0000474 // FIXME: Only do this if DisableFree is set.
475 if (isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000476 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000477 CI.resetAndLeakASTContext();
478 CI.resetAndLeakPreprocessor();
479 CI.resetAndLeakSourceManager();
480 CI.resetAndLeakFileManager();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000481 }
482
Craig Topper49a27902014-05-22 04:46:25 +0000483 setCompilerInstance(nullptr);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000484 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000485}
486
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000487bool FrontendAction::shouldEraseOutputFiles() {
488 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
489}
490
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000491//===----------------------------------------------------------------------===//
492// Utility Actions
493//===----------------------------------------------------------------------===//
494
495void ASTFrontendAction::ExecuteAction() {
496 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000497 if (!CI.hasPreprocessor())
498 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000499
500 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
501 // here so the source manager would be initialized.
502 if (hasCodeCompletionSupport() &&
503 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
504 CI.createCodeCompletionConsumer();
505
506 // Use a code completion consumer?
Craig Topper49a27902014-05-22 04:46:25 +0000507 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000508 if (CI.hasCodeCompletionConsumer())
509 CompletionConsumer = &CI.getCodeCompletionConsumer();
510
Douglas Gregor0e93f012010-08-12 23:31:19 +0000511 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000512 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000513
Erik Verbruggen6e922512012-04-12 10:11:59 +0000514 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
515 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000516}
517
David Blaikie68e081d2011-12-20 02:48:34 +0000518void PluginASTAction::anchor() { }
519
David Blaikie62a56f32014-07-17 22:34:12 +0000520ASTConsumer *
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000521PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000522 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000523 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000524}
Chandler Carruthb5703512011-06-16 16:17:05 +0000525
David Blaikie62a56f32014-07-17 22:34:12 +0000526ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
527 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000528 return WrappedAction->CreateASTConsumer(CI, InFile);
529}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000530bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
531 return WrappedAction->BeginInvocation(CI);
532}
Chandler Carruthb5703512011-06-16 16:17:05 +0000533bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000534 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000535 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000536 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthb5703512011-06-16 16:17:05 +0000537 return WrappedAction->BeginSourceFileAction(CI, Filename);
538}
539void WrapperFrontendAction::ExecuteAction() {
540 WrappedAction->ExecuteAction();
541}
542void WrapperFrontendAction::EndSourceFileAction() {
543 WrappedAction->EndSourceFileAction();
544}
545
546bool WrapperFrontendAction::usesPreprocessorOnly() const {
547 return WrappedAction->usesPreprocessorOnly();
548}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000549TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
550 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000551}
552bool WrapperFrontendAction::hasPCHSupport() const {
553 return WrappedAction->hasPCHSupport();
554}
555bool WrapperFrontendAction::hasASTFileSupport() const {
556 return WrappedAction->hasASTFileSupport();
557}
558bool WrapperFrontendAction::hasIRSupport() const {
559 return WrappedAction->hasIRSupport();
560}
561bool WrapperFrontendAction::hasCodeCompletionSupport() const {
562 return WrappedAction->hasCodeCompletionSupport();
563}
564
565WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
566 : WrappedAction(WrappedAction) {}
567