blob: cf13d7b9ba8a9b41d7b919c225b0df7e3cb1701a [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,
132 ASTUnit *AST) {
133 this->CurrentInput = CurrentInput;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000134 CurrentASTUnit.reset(AST);
135}
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
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000192 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000193 CI.getFileSystemOpts());
Daniel Dunbar59203002009-12-03 01:45:44 +0000194 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000195 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000196
Douglas Gregor32fbe312012-01-20 16:28:04 +0000197 setCurrentInput(Input, AST);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000198
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000199 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000200 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000201 HasBegunSourceFile = true;
202
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000203 // Set the shared objects, these are reset when we finish processing the
204 // file, otherwise the CompilerInstance will happily destroy them.
205 CI.setFileManager(&AST->getFileManager());
206 CI.setSourceManager(&AST->getSourceManager());
207 CI.setPreprocessor(&AST->getPreprocessor());
208 CI.setASTContext(&AST->getASTContext());
209
210 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000211 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000212 goto failure;
213
James Dennettf8317672013-01-23 00:45:44 +0000214 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000215 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000216 if (!CI.hasASTConsumer())
217 goto failure;
218
219 return true;
220 }
221
Ben Langmuir8832c062014-04-15 18:16:25 +0000222 if (!CI.hasVirtualFileSystem()) {
223 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
224 createVFSFromCompilerInvocation(CI.getInvocation(),
225 CI.getDiagnostics()))
226 CI.setVirtualFileSystem(VFS);
227 else
228 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000229 }
230
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000231 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000232 if (!CI.hasFileManager())
233 CI.createFileManager();
234 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000235 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000236
237 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000238 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000239 assert(hasIRSupport() &&
240 "This action does not have IR file support!");
241
242 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000243 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseea762b02012-08-10 01:06:08 +0000244 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000245
246 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000247 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000248 goto failure;
249
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000250 // Initialize the main file entry.
251 if (!CI.InitializeSourceManager(CurrentInput))
252 goto failure;
253
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000254 return true;
255 }
256
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000257 // If the implicit PCH include is actually a directory, rather than
258 // a single file, search for a suitable PCH file in that directory.
259 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
260 FileManager &FileMgr = CI.getFileManager();
261 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
262 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
263 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Rafael Espindolac0809172014-06-12 14:02:15 +0000264 std::error_code EC;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000265 SmallString<128> DirNative;
266 llvm::sys::path::native(PCHDir->getName(), DirNative);
267 bool Found = false;
268 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
269 Dir != DirEnd && !EC; Dir.increment(EC)) {
270 // Check whether this is an acceptable AST file.
271 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
272 CI.getLangOpts(),
Douglas Gregorb6368752012-10-24 23:41:50 +0000273 CI.getTargetOpts(),
274 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000275 PPOpts.ImplicitPCHInclude = Dir->path();
276 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000277 break;
278 }
279 }
280
281 if (!Found) {
282 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
283 return true;
284 }
285 }
286 }
287
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000288 // Set up the preprocessor.
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000289 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000290
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000291 // Inform the diagnostic client we are processing a source file.
292 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
293 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000294 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000295
296 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000297 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000298 goto failure;
299
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000300 // Initialize the main file entry. It is important that this occurs after
301 // BeginSourceFileAction, which may change CurrentInput during module builds.
302 if (!CI.InitializeSourceManager(CurrentInput))
303 goto failure;
304
James Dennettf8317672013-01-23 00:45:44 +0000305 // Create the AST context and consumer unless this is a preprocessor only
306 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000307 if (!usesPreprocessorOnly()) {
308 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000309
David Blaikie62a56f32014-07-17 22:34:12 +0000310 std::unique_ptr<ASTConsumer> Consumer(
311 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000312 if (!Consumer)
313 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000314
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000315 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000316
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000317 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
318 // Convert headers to PCH and chain them.
Alp Toker9e0523d2014-07-07 11:07:10 +0000319 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
320 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000321 if (!source)
322 goto failure;
Alp Toker9e0523d2014-07-07 11:07:10 +0000323 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000324 CI.getASTContext().setExternalSource(source);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000325 } else if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
326 // Use PCH.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000327 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregor925296b2011-07-19 16:10:42 +0000328 ASTDeserializationListener *DeserialListener =
329 Consumer->GetASTDeserializationListener();
Nico Weber824285e2014-05-08 04:26:47 +0000330 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 Dunbara0ff58d2009-11-14 10:42:35 +0000343 CI.createPCHExternalASTSource(
Nico Weber824285e2014-05-08 04:26:47 +0000344 CI.getPreprocessorOpts().ImplicitPCHInclude,
345 CI.getPreprocessorOpts().DisablePCHValidation,
346 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
347 DeleteDeserialListener);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000348 if (!CI.getASTContext().getExternalSource())
349 goto failure;
350 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000351
David Blaikie62a56f32014-07-17 22:34:12 +0000352 CI.setASTConsumer(Consumer.release());
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000353 if (!CI.hasASTConsumer())
354 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000355 }
356
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000357 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000358 // source.
359 if (!CI.hasASTContext() || !CI.getASTContext().getExternalSource()) {
360 Preprocessor &PP = CI.getPreprocessor();
Richard Smith053f6c62014-05-16 23:01:30 +0000361
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 Dunbara0ff58d2009-11-14 10:42:35 +0000368 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000369 PP.getLangOpts());
Richard Smith053f6c62014-05-16 23:01:30 +0000370 } 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 Dunbara0ff58d2009-11-14 10:42:35 +0000376 }
377
Douglas Gregore9fc3772012-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()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000382 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000383 Override(new LayoutOverrideSource(
384 CI.getFrontendOpts().OverrideRecordLayoutsFile));
385 CI.getASTContext().setExternalSource(Override);
386 }
Richard Smith053f6c62014-05-16 23:01:30 +0000387
Daniel Dunbara0ff58d2009-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()) {
Craig Topper49a27902014-05-22 04:46:25 +0000394 CI.setASTContext(nullptr);
395 CI.setPreprocessor(nullptr);
396 CI.setSourceManager(nullptr);
397 CI.setFileManager(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000398 }
399
Jordan Roseea762b02012-08-10 01:06:08 +0000400 if (HasBegunSourceFile)
401 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000402 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000403 setCurrentInput(FrontendInputFile());
Craig Topper49a27902014-05-22 04:46:25 +0000404 setCompilerInstance(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000405 return false;
406}
407
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000408bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000409 CompilerInstance &CI = getCompilerInstance();
410
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000411 if (CI.hasFrontendTimer()) {
412 llvm::TimeRegion Timer(CI.getFrontendTimer());
413 ExecuteAction();
414 }
415 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000416
Douglas Gregor5e306b12013-01-23 22:38:11 +0000417 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000418 // there were any module-build failures.
419 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
420 CI.hasPreprocessor()) {
Douglas Gregor5e306b12013-01-23 22:38:11 +0000421 GlobalModuleIndex::writeIndex(
422 CI.getFileManager(),
423 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
424 }
425
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000426 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000427}
428
429void FrontendAction::EndSourceFile() {
430 CompilerInstance &CI = getCompilerInstance();
431
Douglas Gregor1388a892011-02-09 18:47:31 +0000432 // Inform the diagnostic client we are done with this source file.
433 CI.getDiagnosticClient().EndSourceFile();
434
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000435 // Inform the preprocessor we are done.
436 if (CI.hasPreprocessor())
437 CI.getPreprocessor().EndSourceFile();
438
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000439 // Finalize the action.
440 EndSourceFileAction();
441
Nico Weber7de358e2014-04-24 02:42:04 +0000442 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000443 //
444 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000445 bool DisableFree = CI.getFrontendOpts().DisableFree;
446 if (DisableFree) {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000447 if (!isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000448 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000449 CI.resetAndLeakASTContext();
Douglas Gregor0e93f012010-08-12 23:31:19 +0000450 }
David Blaikie62a56f32014-07-17 22:34:12 +0000451 BuryPointer(CI.takeASTConsumer());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000452 } else {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000453 if (!isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000454 CI.setSema(nullptr);
455 CI.setASTContext(nullptr);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000456 }
Craig Topper49a27902014-05-22 04:46:25 +0000457 CI.setASTConsumer(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000458 }
459
460 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 Kyrtzidisf0168de2013-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 Dunbara0ff58d2009-11-14 10:42:35 +0000472
Nico Weber1f29ccf2014-04-24 03:31:27 +0000473 // FIXME: Only do this if DisableFree is set.
474 if (isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000475 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000476 CI.resetAndLeakASTContext();
477 CI.resetAndLeakPreprocessor();
478 CI.resetAndLeakSourceManager();
479 CI.resetAndLeakFileManager();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000480 }
481
Craig Topper49a27902014-05-22 04:46:25 +0000482 setCompilerInstance(nullptr);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000483 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000484}
485
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000486bool FrontendAction::shouldEraseOutputFiles() {
487 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
488}
489
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000490//===----------------------------------------------------------------------===//
491// Utility Actions
492//===----------------------------------------------------------------------===//
493
494void ASTFrontendAction::ExecuteAction() {
495 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000496 if (!CI.hasPreprocessor())
497 return;
Daniel Dunbara0ff58d2009-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?
Craig Topper49a27902014-05-22 04:46:25 +0000506 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000507 if (CI.hasCodeCompletionConsumer())
508 CompletionConsumer = &CI.getCodeCompletionConsumer();
509
Douglas Gregor0e93f012010-08-12 23:31:19 +0000510 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000511 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000512
Erik Verbruggen6e922512012-04-12 10:11:59 +0000513 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
514 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000515}
516
David Blaikie68e081d2011-12-20 02:48:34 +0000517void PluginASTAction::anchor() { }
518
David Blaikie62a56f32014-07-17 22:34:12 +0000519ASTConsumer *
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000520PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000521 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000522 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000523}
Chandler Carruthb5703512011-06-16 16:17:05 +0000524
David Blaikie62a56f32014-07-17 22:34:12 +0000525ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
526 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000527 return WrappedAction->CreateASTConsumer(CI, InFile);
528}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000529bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
530 return WrappedAction->BeginInvocation(CI);
531}
Chandler Carruthb5703512011-06-16 16:17:05 +0000532bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000533 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000534 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000535 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthb5703512011-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 Gregor69f74f82011-08-25 22:30:56 +0000548TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
549 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-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