blob: 77eb1a14434f514d2d6e60262bbf173603a297da [file] [log] [blame]
Daniel Dunbara0ff58d2009-11-14 10:42:35 +00001//===--- FrontendAction.cpp -----------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Frontend/FrontendAction.h"
Sebastian Redl07a89a82010-07-30 00:29:29 +000011#include "clang/AST/ASTConsumer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000012#include "clang/AST/ASTContext.h"
Nico Weber2992efa2011-01-25 20:34:14 +000013#include "clang/AST/DeclGroup.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000014#include "clang/Frontend/ASTUnit.h"
Chandler Carruthf887db02011-12-09 01:55:54 +000015#include "clang/Frontend/ChainedIncludesSource.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000016#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Frontend/FrontendDiagnostic.h"
Nico Weber2992efa2011-01-25 20:34:14 +000018#include "clang/Frontend/FrontendPluginRegistry.h"
Douglas Gregore9fc3772012-01-26 07:55:45 +000019#include "clang/Frontend/LayoutOverrideSource.h"
Nico Weber2992efa2011-01-25 20:34:14 +000020#include "clang/Frontend/MultiplexConsumer.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "clang/Frontend/Utils.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Lex/HeaderSearch.h"
23#include "clang/Lex/Preprocessor.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Parse/ParseAST.h"
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000025#include "clang/Serialization/ASTDeserializationListener.h"
Jonathan D. Turner0248f572011-08-05 22:17:03 +000026#include "clang/Serialization/ASTReader.h"
Douglas Gregor5e306b12013-01-23 22:38:11 +000027#include "clang/Serialization/GlobalModuleIndex.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000028#include "llvm/Support/ErrorHandling.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000029#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/Support/Timer.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000032#include "llvm/Support/raw_ostream.h"
Douglas Gregorfc9e7a22012-10-23 06:18:24 +000033#include "llvm/Support/system_error.h"
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000034using namespace clang;
35
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000036namespace {
37
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000038class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000039 ASTDeserializationListener *Previous;
Nico Weber824285e2014-05-08 04:26:47 +000040 bool DeletePrevious;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000041
42public:
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000043 explicit DelegatingDeserializationListener(
Nico Weber824285e2014-05-08 04:26:47 +000044 ASTDeserializationListener *Previous, bool DeletePrevious)
45 : Previous(Previous), DeletePrevious(DeletePrevious) {}
46 virtual ~DelegatingDeserializationListener() {
47 if (DeletePrevious)
48 delete Previous;
49 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000050
Craig Topperafa7cb32014-03-13 06:07:04 +000051 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000052 if (Previous)
53 Previous->ReaderInitialized(Reader);
54 }
Craig Topperafa7cb32014-03-13 06:07:04 +000055 void IdentifierRead(serialization::IdentID ID,
56 IdentifierInfo *II) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000057 if (Previous)
58 Previous->IdentifierRead(ID, II);
59 }
Craig Topperafa7cb32014-03-13 06:07:04 +000060 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000061 if (Previous)
62 Previous->TypeRead(Idx, T);
63 }
Craig Topperafa7cb32014-03-13 06:07:04 +000064 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000065 if (Previous)
66 Previous->DeclRead(ID, D);
67 }
Craig Topperafa7cb32014-03-13 06:07:04 +000068 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000069 if (Previous)
70 Previous->SelectorRead(ID, Sel);
71 }
Craig Topperafa7cb32014-03-13 06:07:04 +000072 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
73 MacroDefinition *MD) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000074 if (Previous)
75 Previous->MacroDefinitionRead(PPID, MD);
76 }
77};
78
79/// \brief Dumps deserialized declarations.
80class DeserializedDeclsDumper : public DelegatingDeserializationListener {
81public:
Nico Weber824285e2014-05-08 04:26:47 +000082 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
83 bool DeletePrevious)
84 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000085
Craig Topperafa7cb32014-03-13 06:07:04 +000086 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000087 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
88 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramerdb0fc512012-02-07 11:57:57 +000089 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000090 llvm::outs() << "\n";
91
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000092 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000093 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000094};
95
David Blaikie48b81282012-05-29 17:05:42 +000096/// \brief Checks deserialized declarations and emits error if a name
97/// matches one given in command-line using -error-on-deserialized-decl.
98class DeserializedDeclsChecker : public DelegatingDeserializationListener {
99 ASTContext &Ctx;
100 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000101
David Blaikie48b81282012-05-29 17:05:42 +0000102public:
103 DeserializedDeclsChecker(ASTContext &Ctx,
104 const std::set<std::string> &NamesToCheck,
Nico Weber824285e2014-05-08 04:26:47 +0000105 ASTDeserializationListener *Previous,
106 bool DeletePrevious)
107 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
108 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000109
Craig Topperafa7cb32014-03-13 06:07:04 +0000110 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikie48b81282012-05-29 17:05:42 +0000111 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
112 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
113 unsigned DiagID
114 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
115 "%0 was deserialized");
116 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
117 << ND->getNameAsString();
118 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000119
David Blaikie48b81282012-05-29 17:05:42 +0000120 DelegatingDeserializationListener::DeclRead(ID, D);
121 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000122};
123
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000124} // end anonymous namespace
125
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000126FrontendAction::FrontendAction() : Instance(0) {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000127
128FrontendAction::~FrontendAction() {}
129
Douglas Gregor32fbe312012-01-20 16:28:04 +0000130void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
131 ASTUnit *AST) {
132 this->CurrentInput = CurrentInput;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000133 CurrentASTUnit.reset(AST);
134}
135
Nico Weber2992efa2011-01-25 20:34:14 +0000136ASTConsumer* FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000137 StringRef InFile) {
Nico Weber2992efa2011-01-25 20:34:14 +0000138 ASTConsumer* Consumer = CreateASTConsumer(CI, InFile);
139 if (!Consumer)
140 return 0;
141
142 if (CI.getFrontendOpts().AddPluginActions.size() == 0)
143 return Consumer;
144
145 // Make sure the non-plugin consumer is first, so that plugins can't
146 // modifiy the AST.
147 std::vector<ASTConsumer*> Consumers(1, Consumer);
148
149 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
150 i != e; ++i) {
151 // This is O(|plugins| * |add_plugins|), but since both numbers are
152 // way below 50 in practice, that's ok.
153 for (FrontendPluginRegistry::iterator
154 it = FrontendPluginRegistry::begin(),
155 ie = FrontendPluginRegistry::end();
156 it != ie; ++it) {
157 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
Ahmed Charlesb8984322014-03-07 20:03:18 +0000158 std::unique_ptr<PluginASTAction> P(it->instantiate());
Nico Weber2992efa2011-01-25 20:34:14 +0000159 FrontendAction* c = P.get();
Nico Weber741bf9d2011-01-29 21:21:49 +0000160 if (P->ParseArgs(CI, CI.getFrontendOpts().AddPluginArgs[i]))
Nico Weber2992efa2011-01-25 20:34:14 +0000161 Consumers.push_back(c->CreateASTConsumer(CI, InFile));
162 }
163 }
164 }
165
166 return new MultiplexConsumer(Consumers);
167}
168
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000169bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000170 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000171 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000172 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000173 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000174 setCompilerInstance(&CI);
175
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000176 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000177 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000178 if (!BeginInvocation(CI))
179 goto failure;
180
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000181 // AST files follow a very different path, since they share objects via the
182 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000183 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000184 assert(!usesPreprocessorOnly() &&
185 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000186 assert(hasASTFileSupport() &&
187 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000188
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000189 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000190
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000191 ASTUnit *AST = ASTUnit::LoadFromASTFile(InputFile, Diags,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000192 CI.getFileSystemOpts());
Daniel Dunbar59203002009-12-03 01:45:44 +0000193 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000194 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000195
Douglas Gregor32fbe312012-01-20 16:28:04 +0000196 setCurrentInput(Input, AST);
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.
199 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
200 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
209 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000210 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000211 goto failure;
212
James Dennettf8317672013-01-23 00:45:44 +0000213 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000214 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000215 if (!CI.hasASTConsumer())
216 goto failure;
217
218 return true;
219 }
220
Ben Langmuir8832c062014-04-15 18:16:25 +0000221 if (!CI.hasVirtualFileSystem()) {
222 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
223 createVFSFromCompilerInvocation(CI.getInvocation(),
224 CI.getDiagnostics()))
225 CI.setVirtualFileSystem(VFS);
226 else
227 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000228 }
229
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000230 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000231 if (!CI.hasFileManager())
232 CI.createFileManager();
233 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000234 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000235
236 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000237 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000238 assert(hasIRSupport() &&
239 "This action does not have IR file support!");
240
241 // Inform the diagnostic client we are processing a source file.
242 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
Jordan Roseea762b02012-08-10 01:06:08 +0000243 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000244
245 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000246 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000247 goto failure;
248
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000249 // Initialize the main file entry.
250 if (!CI.InitializeSourceManager(CurrentInput))
251 goto failure;
252
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000253 return true;
254 }
255
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000256 // If the implicit PCH include is actually a directory, rather than
257 // a single file, search for a suitable PCH file in that directory.
258 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
259 FileManager &FileMgr = CI.getFileManager();
260 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
261 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
262 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
263 llvm::error_code EC;
264 SmallString<128> DirNative;
265 llvm::sys::path::native(PCHDir->getName(), DirNative);
266 bool Found = false;
267 for (llvm::sys::fs::directory_iterator Dir(DirNative.str(), EC), DirEnd;
268 Dir != DirEnd && !EC; Dir.increment(EC)) {
269 // Check whether this is an acceptable AST file.
270 if (ASTReader::isAcceptableASTFile(Dir->path(), FileMgr,
271 CI.getLangOpts(),
Douglas Gregorb6368752012-10-24 23:41:50 +0000272 CI.getTargetOpts(),
273 CI.getPreprocessorOpts())) {
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000274 PPOpts.ImplicitPCHInclude = Dir->path();
275 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000276 break;
277 }
278 }
279
280 if (!Found) {
281 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
282 return true;
283 }
284 }
285 }
286
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000287 // Set up the preprocessor.
Argyrios Kyrtzidise1974dc2014-03-07 07:47:58 +0000288 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000289
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000290 // Inform the diagnostic client we are processing a source file.
291 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
292 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000293 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000294
295 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000296 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000297 goto failure;
298
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000299 // Initialize the main file entry. It is important that this occurs after
300 // BeginSourceFileAction, which may change CurrentInput during module builds.
301 if (!CI.InitializeSourceManager(CurrentInput))
302 goto failure;
303
James Dennettf8317672013-01-23 00:45:44 +0000304 // Create the AST context and consumer unless this is a preprocessor only
305 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000306 if (!usesPreprocessorOnly()) {
307 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000308
Ahmed Charlesb8984322014-03-07 20:03:18 +0000309 std::unique_ptr<ASTConsumer> Consumer(
310 CreateWrappedASTConsumer(CI, InputFile));
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000311 if (!Consumer)
312 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000313
Argyrios Kyrtzidis65ad5692010-10-24 17:26:36 +0000314 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Douglas Gregorcb28f9d2012-10-09 23:05:51 +0000315
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000316 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
317 // Convert headers to PCH and chain them.
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000318 IntrusiveRefCntPtr<ChainedIncludesSource> source;
319 source = ChainedIncludesSource::create(CI);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000320 if (!source)
321 goto failure;
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000322 CI.setModuleManager(static_cast<ASTReader*>(&source->getFinalReader()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000323 CI.getASTContext().setExternalSource(source);
324
325 } 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
Ahmed Charles9a16beb2014-03-07 19:33:25 +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();
361 PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000362 PP.getLangOpts());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000363 }
364
Douglas Gregore9fc3772012-01-26 07:55:45 +0000365 // If there is a layout overrides file, attach an external AST source that
366 // provides the layouts from that file.
367 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
368 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Argyrios Kyrtzidis1b7ed912014-02-27 04:11:59 +0000369 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000370 Override(new LayoutOverrideSource(
371 CI.getFrontendOpts().OverrideRecordLayoutsFile));
372 CI.getASTContext().setExternalSource(Override);
373 }
374
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000375 return true;
376
377 // If we failed, reset state since the client will not end up calling the
378 // matching EndSourceFile().
379 failure:
380 if (isCurrentFileAST()) {
Ted Kremenek5e14d392011-03-21 18:40:17 +0000381 CI.setASTContext(0);
382 CI.setPreprocessor(0);
383 CI.setSourceManager(0);
384 CI.setFileManager(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000385 }
386
Jordan Roseea762b02012-08-10 01:06:08 +0000387 if (HasBegunSourceFile)
388 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000389 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000390 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000391 setCompilerInstance(0);
392 return false;
393}
394
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000395bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000396 CompilerInstance &CI = getCompilerInstance();
397
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000398 if (CI.hasFrontendTimer()) {
399 llvm::TimeRegion Timer(CI.getFrontendTimer());
400 ExecuteAction();
401 }
402 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000403
Douglas Gregor5e306b12013-01-23 22:38:11 +0000404 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000405 // there were any module-build failures.
406 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
407 CI.hasPreprocessor()) {
Douglas Gregor5e306b12013-01-23 22:38:11 +0000408 GlobalModuleIndex::writeIndex(
409 CI.getFileManager(),
410 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath());
411 }
412
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000413 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000414}
415
416void FrontendAction::EndSourceFile() {
417 CompilerInstance &CI = getCompilerInstance();
418
Douglas Gregor1388a892011-02-09 18:47:31 +0000419 // Inform the diagnostic client we are done with this source file.
420 CI.getDiagnosticClient().EndSourceFile();
421
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000422 // Finalize the action.
423 EndSourceFileAction();
424
Nico Weber7de358e2014-04-24 02:42:04 +0000425 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000426 //
427 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000428 bool DisableFree = CI.getFrontendOpts().DisableFree;
429 if (DisableFree) {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000430 if (!isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000431 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000432 CI.resetAndLeakASTContext();
Douglas Gregor0e93f012010-08-12 23:31:19 +0000433 }
Nico Weber7de358e2014-04-24 02:42:04 +0000434 BuryPointer(CI.takeASTConsumer());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000435 } else {
Douglas Gregor0e93f012010-08-12 23:31:19 +0000436 if (!isCurrentFileAST()) {
437 CI.setSema(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000438 CI.setASTContext(0);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000439 }
440 CI.setASTConsumer(0);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000441 }
442
Daniel Dunbarcb9eaf52010-03-23 05:09:10 +0000443 // Inform the preprocessor we are done.
444 if (CI.hasPreprocessor())
445 CI.getPreprocessor().EndSourceFile();
446
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000447 if (CI.getFrontendOpts().ShowStats) {
448 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
449 CI.getPreprocessor().PrintStats();
450 CI.getPreprocessor().getIdentifierTable().PrintStats();
451 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
452 CI.getSourceManager().PrintStats();
453 llvm::errs() << "\n";
454 }
455
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000456 // Cleanup the output streams, and erase the output files if instructed by the
457 // FrontendAction.
458 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000459
Nico Weber1f29ccf2014-04-24 03:31:27 +0000460 // FIXME: Only do this if DisableFree is set.
461 if (isCurrentFileAST()) {
Nico Weber1ada62652014-04-24 00:51:03 +0000462 CI.resetAndLeakSema();
Ted Kremenek5e14d392011-03-21 18:40:17 +0000463 CI.resetAndLeakASTContext();
464 CI.resetAndLeakPreprocessor();
465 CI.resetAndLeakSourceManager();
466 CI.resetAndLeakFileManager();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000467 }
468
469 setCompilerInstance(0);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000470 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000471}
472
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000473bool FrontendAction::shouldEraseOutputFiles() {
474 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
475}
476
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000477//===----------------------------------------------------------------------===//
478// Utility Actions
479//===----------------------------------------------------------------------===//
480
481void ASTFrontendAction::ExecuteAction() {
482 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000483 if (!CI.hasPreprocessor())
484 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000485
486 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
487 // here so the source manager would be initialized.
488 if (hasCodeCompletionSupport() &&
489 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
490 CI.createCodeCompletionConsumer();
491
492 // Use a code completion consumer?
493 CodeCompleteConsumer *CompletionConsumer = 0;
494 if (CI.hasCodeCompletionConsumer())
495 CompletionConsumer = &CI.getCodeCompletionConsumer();
496
Douglas Gregor0e93f012010-08-12 23:31:19 +0000497 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000498 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000499
Erik Verbruggen6e922512012-04-12 10:11:59 +0000500 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
501 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000502}
503
David Blaikie68e081d2011-12-20 02:48:34 +0000504void PluginASTAction::anchor() { }
505
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000506ASTConsumer *
507PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000508 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000509 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000510}
Chandler Carruthb5703512011-06-16 16:17:05 +0000511
512ASTConsumer *WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000513 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000514 return WrappedAction->CreateASTConsumer(CI, InFile);
515}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000516bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
517 return WrappedAction->BeginInvocation(CI);
518}
Chandler Carruthb5703512011-06-16 16:17:05 +0000519bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000520 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000521 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000522 WrappedAction->setCompilerInstance(&CI);
Chandler Carruthb5703512011-06-16 16:17:05 +0000523 return WrappedAction->BeginSourceFileAction(CI, Filename);
524}
525void WrapperFrontendAction::ExecuteAction() {
526 WrappedAction->ExecuteAction();
527}
528void WrapperFrontendAction::EndSourceFileAction() {
529 WrappedAction->EndSourceFileAction();
530}
531
532bool WrapperFrontendAction::usesPreprocessorOnly() const {
533 return WrappedAction->usesPreprocessorOnly();
534}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000535TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
536 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000537}
538bool WrapperFrontendAction::hasPCHSupport() const {
539 return WrappedAction->hasPCHSupport();
540}
541bool WrapperFrontendAction::hasASTFileSupport() const {
542 return WrappedAction->hasASTFileSupport();
543}
544bool WrapperFrontendAction::hasIRSupport() const {
545 return WrappedAction->hasIRSupport();
546}
547bool WrapperFrontendAction::hasCodeCompletionSupport() const {
548 return WrappedAction->hasCodeCompletionSupport();
549}
550
551WrapperFrontendAction::WrapperFrontendAction(FrontendAction *WrappedAction)
552 : WrappedAction(WrappedAction) {}
553