blob: ed9bb5e77d65f2158768037a038c4650cc935fb8 [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"
Mehdi Amini9670f842016-07-18 19:02:11 +000023#include "clang/Lex/PreprocessorOptions.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"
Mehdi Amini9670f842016-07-18 19:02:11 +000030#include "llvm/Support/Path.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"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000033#include <system_error>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +000034using namespace clang;
35
John Brawn4d79ec72016-08-05 11:01:08 +000036LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
NAKAMURA Takumiad4c06c2014-07-11 15:06:24 +000037
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000038namespace {
39
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000040class DelegatingDeserializationListener : public ASTDeserializationListener {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000041 ASTDeserializationListener *Previous;
Nico Weber824285e2014-05-08 04:26:47 +000042 bool DeletePrevious;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000043
44public:
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000045 explicit DelegatingDeserializationListener(
Nico Weber824285e2014-05-08 04:26:47 +000046 ASTDeserializationListener *Previous, bool DeletePrevious)
47 : Previous(Previous), DeletePrevious(DeletePrevious) {}
Alexander Kornienko34eb2072015-04-11 02:00:23 +000048 ~DelegatingDeserializationListener() override {
Nico Weber824285e2014-05-08 04:26:47 +000049 if (DeletePrevious)
50 delete Previous;
51 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000052
Craig Topperafa7cb32014-03-13 06:07:04 +000053 void ReaderInitialized(ASTReader *Reader) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000054 if (Previous)
55 Previous->ReaderInitialized(Reader);
56 }
Craig Topperafa7cb32014-03-13 06:07:04 +000057 void IdentifierRead(serialization::IdentID ID,
58 IdentifierInfo *II) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000059 if (Previous)
60 Previous->IdentifierRead(ID, II);
61 }
Craig Topperafa7cb32014-03-13 06:07:04 +000062 void TypeRead(serialization::TypeIdx Idx, QualType T) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000063 if (Previous)
64 Previous->TypeRead(Idx, T);
65 }
Craig Topperafa7cb32014-03-13 06:07:04 +000066 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000067 if (Previous)
68 Previous->DeclRead(ID, D);
69 }
Craig Topperafa7cb32014-03-13 06:07:04 +000070 void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000071 if (Previous)
72 Previous->SelectorRead(ID, Sel);
73 }
Craig Topperafa7cb32014-03-13 06:07:04 +000074 void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
Richard Smith66a81862015-05-04 02:25:31 +000075 MacroDefinitionRecord *MD) override {
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000076 if (Previous)
77 Previous->MacroDefinitionRead(PPID, MD);
78 }
79};
80
81/// \brief Dumps deserialized declarations.
82class DeserializedDeclsDumper : public DelegatingDeserializationListener {
83public:
Nico Weber824285e2014-05-08 04:26:47 +000084 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
85 bool DeletePrevious)
86 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000087
Craig Topperafa7cb32014-03-13 06:07:04 +000088 void DeclRead(serialization::DeclID ID, const Decl *D) override {
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000089 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
90 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
Benjamin Kramerdb0fc512012-02-07 11:57:57 +000091 llvm::outs() << " - " << *ND;
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000092 llvm::outs() << "\n";
93
Argyrios Kyrtzidisb12986f2011-10-28 22:54:31 +000094 DelegatingDeserializationListener::DeclRead(ID, D);
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000095 }
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +000096};
97
David Blaikie48b81282012-05-29 17:05:42 +000098/// \brief Checks deserialized declarations and emits error if a name
99/// matches one given in command-line using -error-on-deserialized-decl.
100class DeserializedDeclsChecker : public DelegatingDeserializationListener {
101 ASTContext &Ctx;
102 std::set<std::string> NamesToCheck;
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000103
David Blaikie48b81282012-05-29 17:05:42 +0000104public:
105 DeserializedDeclsChecker(ASTContext &Ctx,
106 const std::set<std::string> &NamesToCheck,
Nico Weber824285e2014-05-08 04:26:47 +0000107 ASTDeserializationListener *Previous,
108 bool DeletePrevious)
109 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
110 NamesToCheck(NamesToCheck) {}
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000111
Craig Topperafa7cb32014-03-13 06:07:04 +0000112 void DeclRead(serialization::DeclID ID, const Decl *D) override {
David Blaikie48b81282012-05-29 17:05:42 +0000113 if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
114 if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
115 unsigned DiagID
116 = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
117 "%0 was deserialized");
118 Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
119 << ND->getNameAsString();
120 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000121
David Blaikie48b81282012-05-29 17:05:42 +0000122 DelegatingDeserializationListener::DeclRead(ID, D);
123 }
Argyrios Kyrtzidis0427be92010-10-14 20:14:25 +0000124};
125
Argyrios Kyrtzidisa11aca42010-10-14 20:14:18 +0000126} // end anonymous namespace
127
Craig Topper49a27902014-05-22 04:46:25 +0000128FrontendAction::FrontendAction() : Instance(nullptr) {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000129
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000130FrontendAction::~FrontendAction() {}
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000131
Douglas Gregor32fbe312012-01-20 16:28:04 +0000132void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
David Blaikief62d4e72014-08-10 17:03:42 +0000133 std::unique_ptr<ASTUnit> AST) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000134 this->CurrentInput = CurrentInput;
David Blaikief62d4e72014-08-10 17:03:42 +0000135 CurrentASTUnit = std::move(AST);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000136}
137
David Blaikie6beb6aa2014-08-10 19:56:51 +0000138std::unique_ptr<ASTConsumer>
139FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
140 StringRef InFile) {
141 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
Nico Weber2992efa2011-01-25 20:34:14 +0000142 if (!Consumer)
Craig Topper49a27902014-05-22 04:46:25 +0000143 return nullptr;
Nico Weber2992efa2011-01-25 20:34:14 +0000144
John Brawn6c789742016-03-15 12:51:40 +0000145 // If there are no registered plugins we don't need to wrap the consumer
146 if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
Nico Weber2992efa2011-01-25 20:34:14 +0000147 return Consumer;
148
John Brawn6c789742016-03-15 12:51:40 +0000149 // Collect the list of plugins that go before the main action (in Consumers)
150 // or after it (in AfterConsumers)
David Blaikie6beb6aa2014-08-10 19:56:51 +0000151 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
John Brawn6c789742016-03-15 12:51:40 +0000152 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
153 for (FrontendPluginRegistry::iterator it = FrontendPluginRegistry::begin(),
154 ie = FrontendPluginRegistry::end();
155 it != ie; ++it) {
156 std::unique_ptr<PluginASTAction> P = it->instantiate();
157 PluginASTAction::ActionType ActionType = P->getActionType();
158 if (ActionType == PluginASTAction::Cmdline) {
159 // This is O(|plugins| * |add_plugins|), but since both numbers are
160 // way below 50 in practice, that's ok.
161 for (size_t i = 0, e = CI.getFrontendOpts().AddPluginActions.size();
162 i != e; ++i) {
163 if (it->getName() == CI.getFrontendOpts().AddPluginActions[i]) {
164 ActionType = PluginASTAction::AddAfterMainAction;
165 break;
166 }
167 }
Nico Weber2992efa2011-01-25 20:34:14 +0000168 }
John Brawn6c789742016-03-15 12:51:40 +0000169 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
170 ActionType == PluginASTAction::AddAfterMainAction) &&
171 P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs[it->getName()])) {
172 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
173 if (ActionType == PluginASTAction::AddBeforeMainAction) {
174 Consumers.push_back(std::move(PluginConsumer));
175 } else {
176 AfterConsumers.push_back(std::move(PluginConsumer));
177 }
178 }
179 }
180
181 // Add to Consumers the main consumer, then all the plugins that go after it
182 Consumers.push_back(std::move(Consumer));
183 for (auto &C : AfterConsumers) {
184 Consumers.push_back(std::move(C));
Nico Weber2992efa2011-01-25 20:34:14 +0000185 }
186
David Blaikie6beb6aa2014-08-10 19:56:51 +0000187 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
Nico Weber2992efa2011-01-25 20:34:14 +0000188}
189
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000190bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
Douglas Gregor32fbe312012-01-20 16:28:04 +0000191 const FrontendInputFile &Input) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000192 assert(!Instance && "Already processing a source file!");
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000193 assert(!Input.isEmpty() && "Unexpected empty filename!");
Douglas Gregor32fbe312012-01-20 16:28:04 +0000194 setCurrentInput(Input);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000195 setCompilerInstance(&CI);
196
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000197 StringRef InputFile = Input.getFile();
Jordan Roseea762b02012-08-10 01:06:08 +0000198 bool HasBegunSourceFile = false;
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000199 if (!BeginInvocation(CI))
200 goto failure;
201
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000202 // AST files follow a very different path, since they share objects via the
203 // AST unit.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000204 if (Input.getKind() == IK_AST) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000205 assert(!usesPreprocessorOnly() &&
206 "Attempt to pass AST file to preprocessor only action!");
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000207 assert(hasASTFileSupport() &&
208 "This action does not have AST file support!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000209
Dylan Noblesmithc95d8192012-02-20 14:00:23 +0000210 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
Alp Toker965f8822013-11-27 05:22:15 +0000211
Adrian Prantl6b21ab22015-08-27 19:46:20 +0000212 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
213 InputFile, CI.getPCHContainerReader(), Diags, CI.getFileSystemOpts(),
214 CI.getCodeGenOpts().DebugTypeExtRefs);
David Blaikief62d4e72014-08-10 17:03:42 +0000215
Daniel Dunbar59203002009-12-03 01:45:44 +0000216 if (!AST)
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000217 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000218
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000219 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000220 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Argyrios Kyrtzidisc00f43a2013-03-18 22:55:24 +0000221 HasBegunSourceFile = true;
222
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000223 // Set the shared objects, these are reset when we finish processing the
224 // file, otherwise the CompilerInstance will happily destroy them.
225 CI.setFileManager(&AST->getFileManager());
226 CI.setSourceManager(&AST->getSourceManager());
David Blaikie41565462017-01-05 19:48:07 +0000227 CI.setPreprocessor(AST->getPreprocessorPtr());
David Blaikieee123222017-02-08 20:51:11 +0000228 Preprocessor &PP = CI.getPreprocessor();
229 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
230 PP.getLangOpts());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000231 CI.setASTContext(&AST->getASTContext());
232
David Blaikief62d4e72014-08-10 17:03:42 +0000233 setCurrentInput(Input, std::move(AST));
234
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000235 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000236 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000237 goto failure;
238
James Dennettf8317672013-01-23 00:45:44 +0000239 // Create the AST consumer.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000240 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000241 if (!CI.hasASTConsumer())
242 goto failure;
243
244 return true;
245 }
246
Ben Langmuir8832c062014-04-15 18:16:25 +0000247 if (!CI.hasVirtualFileSystem()) {
248 if (IntrusiveRefCntPtr<vfs::FileSystem> VFS =
249 createVFSFromCompilerInvocation(CI.getInvocation(),
250 CI.getDiagnostics()))
251 CI.setVirtualFileSystem(VFS);
252 else
253 goto failure;
Ben Langmuir801272a2014-02-25 18:23:47 +0000254 }
255
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000256 // Set up the file and source managers, if needed.
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000257 if (!CI.hasFileManager())
258 CI.createFileManager();
259 if (!CI.hasSourceManager())
Chris Lattner5159f612010-11-23 08:35:12 +0000260 CI.createSourceManager(CI.getFileManager());
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000261
262 // IR files bypass the rest of initialization.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000263 if (Input.getKind() == IK_LLVM_IR) {
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000264 assert(hasIRSupport() &&
265 "This action does not have IR file support!");
266
267 // Inform the diagnostic client we are processing a source file.
Craig Topper49a27902014-05-22 04:46:25 +0000268 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
Jordan Roseea762b02012-08-10 01:06:08 +0000269 HasBegunSourceFile = true;
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000270
271 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000272 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000273 goto failure;
274
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000275 // Initialize the main file entry.
276 if (!CI.InitializeSourceManager(CurrentInput))
277 goto failure;
278
Daniel Dunbar9507f9c2010-06-07 23:26:47 +0000279 return true;
280 }
281
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000282 // If the implicit PCH include is actually a directory, rather than
283 // a single file, search for a suitable PCH file in that directory.
284 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
285 FileManager &FileMgr = CI.getFileManager();
286 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
287 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
Argyrios Kyrtzidisbd0b6512015-02-19 20:12:20 +0000288 std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000289 if (const DirectoryEntry *PCHDir = FileMgr.getDirectory(PCHInclude)) {
Rafael Espindolac0809172014-06-12 14:02:15 +0000290 std::error_code EC;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000291 SmallString<128> DirNative;
292 llvm::sys::path::native(PCHDir->getName(), DirNative);
293 bool Found = false;
Bruno Cardoso Lopesb4d56f12016-12-12 19:28:21 +0000294 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
295 for (vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC), DirEnd;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000296 Dir != DirEnd && !EC; Dir.increment(EC)) {
297 // Check whether this is an acceptable AST file.
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000298 if (ASTReader::isAcceptableASTFile(
Bruno Cardoso Lopesb4d56f12016-12-12 19:28:21 +0000299 Dir->getName(), FileMgr, CI.getPCHContainerReader(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000300 CI.getLangOpts(), CI.getTargetOpts(), CI.getPreprocessorOpts(),
301 SpecificModuleCachePath)) {
Bruno Cardoso Lopesb4d56f12016-12-12 19:28:21 +0000302 PPOpts.ImplicitPCHInclude = Dir->getName();
Argyrios Kyrtzidis48b72d82013-02-05 16:36:52 +0000303 Found = true;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000304 break;
305 }
306 }
307
308 if (!Found) {
309 CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
Richard Smith81c72482015-09-04 21:44:32 +0000310 goto failure;
Douglas Gregorfc9e7a22012-10-23 06:18:24 +0000311 }
312 }
313 }
314
Ted Kremenekeeccb302014-08-27 15:14:15 +0000315 // Set up the preprocessor if needed. When parsing model files the
316 // preprocessor of the original source is reused.
317 if (!isModelParsingAction())
318 CI.createPreprocessor(getTranslationUnitKind());
Daniel Dunbaraed46fc2010-06-07 23:23:50 +0000319
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000320 // Inform the diagnostic client we are processing a source file.
321 CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
322 &CI.getPreprocessor());
Jordan Roseea762b02012-08-10 01:06:08 +0000323 HasBegunSourceFile = true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000324
325 // Initialize the action.
Argyrios Kyrtzidis873c8582012-11-09 19:40:39 +0000326 if (!BeginSourceFileAction(CI, InputFile))
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000327 goto failure;
328
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000329 // Initialize the main file entry. It is important that this occurs after
330 // BeginSourceFileAction, which may change CurrentInput during module builds.
331 if (!CI.InitializeSourceManager(CurrentInput))
332 goto failure;
333
James Dennettf8317672013-01-23 00:45:44 +0000334 // Create the AST context and consumer unless this is a preprocessor only
335 // action.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000336 if (!usesPreprocessorOnly()) {
Ted Kremenekeeccb302014-08-27 15:14:15 +0000337 // Parsing a model file should reuse the existing ASTContext.
338 if (!isModelParsingAction())
339 CI.createASTContext();
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000340
David Blaikie6beb6aa2014-08-10 19:56:51 +0000341 std::unique_ptr<ASTConsumer> Consumer =
342 CreateWrappedASTConsumer(CI, InputFile);
Fariborz Jahanian2129ccf2010-10-29 19:49:13 +0000343 if (!Consumer)
344 goto failure;
Sebastian Redl07a89a82010-07-30 00:29:29 +0000345
Ted Kremenekeeccb302014-08-27 15:14:15 +0000346 // FIXME: should not overwrite ASTMutationListener when parsing model files?
347 if (!isModelParsingAction())
348 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
Richard Smithe842a472014-10-22 02:05:46 +0000349
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000350 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
351 // Convert headers to PCH and chain them.
Alp Toker9e0523d2014-07-07 11:07:10 +0000352 IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
353 source = createChainedIncludesSource(CI, FinalReader);
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000354 if (!source)
355 goto failure;
Alp Toker9e0523d2014-07-07 11:07:10 +0000356 CI.setModuleManager(static_cast<ASTReader *>(FinalReader.get()));
Argyrios Kyrtzidis35dcda72011-03-09 17:21:42 +0000357 CI.getASTContext().setExternalSource(source);
Vassil Vassileva2c2d942017-02-07 21:49:41 +0000358 } else if (CI.getLangOpts().Modules ||
359 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
360 // Use PCM or PCH.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000361 assert(hasPCHSupport() && "This action does not have PCH support!");
Douglas Gregor925296b2011-07-19 16:10:42 +0000362 ASTDeserializationListener *DeserialListener =
363 Consumer->GetASTDeserializationListener();
Nico Weber824285e2014-05-08 04:26:47 +0000364 bool DeleteDeserialListener = false;
365 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
366 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
367 DeleteDeserialListener);
368 DeleteDeserialListener = true;
369 }
370 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
371 DeserialListener = new DeserializedDeclsChecker(
372 CI.getASTContext(),
373 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
374 DeserialListener, DeleteDeserialListener);
375 DeleteDeserialListener = true;
376 }
Vassil Vassileva2c2d942017-02-07 21:49:41 +0000377 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
378 CI.createPCHExternalASTSource(
379 CI.getPreprocessorOpts().ImplicitPCHInclude,
380 CI.getPreprocessorOpts().DisablePCHValidation,
Nico Weber824285e2014-05-08 04:26:47 +0000381 CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, DeserialListener,
Vassil Vassileva2c2d942017-02-07 21:49:41 +0000382 DeleteDeserialListener);
383 if (!CI.getASTContext().getExternalSource())
384 goto failure;
385 }
386 // If modules are enabled, create the module manager before creating
387 // any builtins, so that all declarations know that they might be
388 // extended by an external source.
389 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
390 !CI.getASTContext().getExternalSource()) {
391 CI.createModuleManager();
392 CI.getModuleManager()->setDeserializationListener(DeserialListener,
393 DeleteDeserialListener);
394 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000395 }
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000396
David Blaikie6beb6aa2014-08-10 19:56:51 +0000397 CI.setASTConsumer(std::move(Consumer));
Sebastian Redl4d3af3e2010-07-09 21:00:24 +0000398 if (!CI.hasASTConsumer())
399 goto failure;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000400 }
401
Jonathan D. Turner0248f572011-08-05 22:17:03 +0000402 // Initialize built-in info as long as we aren't using an external AST
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000403 // source.
Vassil Vassileva2c2d942017-02-07 21:49:41 +0000404 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
405 !CI.getASTContext().getExternalSource()) {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000406 Preprocessor &PP = CI.getPreprocessor();
Eric Christopher02d5d862015-08-06 01:01:12 +0000407 PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
David Blaikiebbafb8a2012-03-11 07:00:24 +0000408 PP.getLangOpts());
Richard Smith053f6c62014-05-16 23:01:30 +0000409 } else {
410 // FIXME: If this is a problem, recover from it by creating a multiplex
411 // source.
412 assert((!CI.getLangOpts().Modules || CI.getModuleManager()) &&
413 "modules enabled but created an external source that "
414 "doesn't support modules");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000415 }
416
Richard Smithac425e92015-01-23 00:01:13 +0000417 // If we were asked to load any module map files, do so now.
418 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
419 if (auto *File = CI.getFileManager().getFile(Filename))
420 CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
421 File, /*IsSystem*/false);
422 else
423 CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
424 }
425
Richard Smithd4b230b2014-10-27 23:01:16 +0000426 // If we were asked to load any module files, do so now.
427 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
428 if (!CI.loadModuleFile(ModuleFile))
Richard Smithe842a472014-10-22 02:05:46 +0000429 goto failure;
Richard Smithe842a472014-10-22 02:05:46 +0000430
Douglas Gregore9fc3772012-01-26 07:55:45 +0000431 // If there is a layout overrides file, attach an external AST source that
432 // provides the layouts from that file.
Diana Picusac03b4b2017-01-25 07:27:05 +0000433 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
Douglas Gregore9fc3772012-01-26 07:55:45 +0000434 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
Diana Picusac03b4b2017-01-25 07:27:05 +0000435 IntrusiveRefCntPtr<ExternalASTSource>
Douglas Gregore9fc3772012-01-26 07:55:45 +0000436 Override(new LayoutOverrideSource(
437 CI.getFrontendOpts().OverrideRecordLayoutsFile));
438 CI.getASTContext().setExternalSource(Override);
439 }
Richard Smith053f6c62014-05-16 23:01:30 +0000440
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000441 return true;
442
443 // If we failed, reset state since the client will not end up calling the
444 // matching EndSourceFile().
445 failure:
446 if (isCurrentFileAST()) {
Craig Topper49a27902014-05-22 04:46:25 +0000447 CI.setASTContext(nullptr);
448 CI.setPreprocessor(nullptr);
449 CI.setSourceManager(nullptr);
450 CI.setFileManager(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000451 }
452
Jordan Roseea762b02012-08-10 01:06:08 +0000453 if (HasBegunSourceFile)
454 CI.getDiagnosticClient().EndSourceFile();
Benjamin Kramer3c717b42012-10-14 19:21:21 +0000455 CI.clearOutputFiles(/*EraseFiles=*/true);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000456 setCurrentInput(FrontendInputFile());
Craig Topper49a27902014-05-22 04:46:25 +0000457 setCompilerInstance(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000458 return false;
459}
460
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000461bool FrontendAction::Execute() {
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000462 CompilerInstance &CI = getCompilerInstance();
463
Kovarththanan Rajaratnam5505dff2009-11-29 09:57:35 +0000464 if (CI.hasFrontendTimer()) {
465 llvm::TimeRegion Timer(CI.getFrontendTimer());
466 ExecuteAction();
467 }
468 else ExecuteAction();
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000469
Douglas Gregor5e306b12013-01-23 22:38:11 +0000470 // If we are supposed to rebuild the global module index, do so now unless
Douglas Gregorc1bbec82013-01-25 00:45:27 +0000471 // there were any module-build failures.
472 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
473 CI.hasPreprocessor()) {
Richard Smith3938f0c2015-08-15 00:34:15 +0000474 StringRef Cache =
475 CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
476 if (!Cache.empty())
477 GlobalModuleIndex::writeIndex(CI.getFileManager(),
478 CI.getPCHContainerReader(), Cache);
Douglas Gregor5e306b12013-01-23 22:38:11 +0000479 }
480
Argyrios Kyrtzidis1416e172012-06-08 05:48:06 +0000481 return true;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000482}
483
484void FrontendAction::EndSourceFile() {
485 CompilerInstance &CI = getCompilerInstance();
486
Douglas Gregor1388a892011-02-09 18:47:31 +0000487 // Inform the diagnostic client we are done with this source file.
488 CI.getDiagnosticClient().EndSourceFile();
489
Benjamin Kramer88d99e42014-08-07 20:51:16 +0000490 // Inform the preprocessor we are done.
491 if (CI.hasPreprocessor())
492 CI.getPreprocessor().EndSourceFile();
493
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000494 // Finalize the action.
495 EndSourceFileAction();
496
Nico Weber7de358e2014-04-24 02:42:04 +0000497 // Sema references the ast consumer, so reset sema first.
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000498 //
499 // FIXME: There is more per-file stuff we could just drop here?
Nico Weber7de358e2014-04-24 02:42:04 +0000500 bool DisableFree = CI.getFrontendOpts().DisableFree;
501 if (DisableFree) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000502 CI.resetAndLeakSema();
503 CI.resetAndLeakASTContext();
David Blaikie6beb6aa2014-08-10 19:56:51 +0000504 BuryPointer(CI.takeASTConsumer().get());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000505 } else {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000506 CI.setSema(nullptr);
507 CI.setASTContext(nullptr);
Craig Topper49a27902014-05-22 04:46:25 +0000508 CI.setASTConsumer(nullptr);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000509 }
510
511 if (CI.getFrontendOpts().ShowStats) {
512 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFile() << "':\n";
513 CI.getPreprocessor().PrintStats();
514 CI.getPreprocessor().getIdentifierTable().PrintStats();
515 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
516 CI.getSourceManager().PrintStats();
517 llvm::errs() << "\n";
518 }
519
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000520 // Cleanup the output streams, and erase the output files if instructed by the
521 // FrontendAction.
522 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000523
Nico Weber1f29ccf2014-04-24 03:31:27 +0000524 if (isCurrentFileAST()) {
Duncan P. N. Exon Smith4a8212a2015-05-04 14:59:20 +0000525 if (DisableFree) {
526 CI.resetAndLeakPreprocessor();
527 CI.resetAndLeakSourceManager();
528 CI.resetAndLeakFileManager();
529 } else {
530 CI.setPreprocessor(nullptr);
531 CI.setSourceManager(nullptr);
532 CI.setFileManager(nullptr);
533 }
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000534 }
535
Craig Topper49a27902014-05-22 04:46:25 +0000536 setCompilerInstance(nullptr);
Douglas Gregor32fbe312012-01-20 16:28:04 +0000537 setCurrentInput(FrontendInputFile());
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000538}
539
Argyrios Kyrtzidisf0168de2013-06-11 00:36:55 +0000540bool FrontendAction::shouldEraseOutputFiles() {
541 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
542}
543
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000544//===----------------------------------------------------------------------===//
545// Utility Actions
546//===----------------------------------------------------------------------===//
547
548void ASTFrontendAction::ExecuteAction() {
549 CompilerInstance &CI = getCompilerInstance();
Rafael Espindola5150f2f2013-07-28 13:23:37 +0000550 if (!CI.hasPreprocessor())
551 return;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000552
553 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
554 // here so the source manager would be initialized.
555 if (hasCodeCompletionSupport() &&
556 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
557 CI.createCodeCompletionConsumer();
558
559 // Use a code completion consumer?
Craig Topper49a27902014-05-22 04:46:25 +0000560 CodeCompleteConsumer *CompletionConsumer = nullptr;
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000561 if (CI.hasCodeCompletionConsumer())
562 CompletionConsumer = &CI.getCodeCompletionConsumer();
563
Douglas Gregor0e93f012010-08-12 23:31:19 +0000564 if (!CI.hasSema())
Douglas Gregor69f74f82011-08-25 22:30:56 +0000565 CI.createSema(getTranslationUnitKind(), CompletionConsumer);
Douglas Gregor0e93f012010-08-12 23:31:19 +0000566
Erik Verbruggen6e922512012-04-12 10:11:59 +0000567 ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
568 CI.getFrontendOpts().SkipFunctionBodies);
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000569}
570
David Blaikie68e081d2011-12-20 02:48:34 +0000571void PluginASTAction::anchor() { }
572
David Blaikie6beb6aa2014-08-10 19:56:51 +0000573std::unique_ptr<ASTConsumer>
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000574PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000575 StringRef InFile) {
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000576 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
Daniel Dunbara0ff58d2009-11-14 10:42:35 +0000577}
Chandler Carruthb5703512011-06-16 16:17:05 +0000578
David Blaikie6beb6aa2014-08-10 19:56:51 +0000579std::unique_ptr<ASTConsumer>
580WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
581 StringRef InFile) {
Chandler Carruthb5703512011-06-16 16:17:05 +0000582 return WrappedAction->CreateASTConsumer(CI, InFile);
583}
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000584bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
585 return WrappedAction->BeginInvocation(CI);
586}
Chandler Carruthb5703512011-06-16 16:17:05 +0000587bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000588 StringRef Filename) {
Douglas Gregor32fbe312012-01-20 16:28:04 +0000589 WrappedAction->setCurrentInput(getCurrentInput());
Argyrios Kyrtzidis90b6a2a2011-06-18 00:53:41 +0000590 WrappedAction->setCompilerInstance(&CI);
Argyrios Kyrtzidise89a1792016-02-16 05:39:33 +0000591 auto Ret = WrappedAction->BeginSourceFileAction(CI, Filename);
592 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
593 setCurrentInput(WrappedAction->getCurrentInput());
594 return Ret;
Chandler Carruthb5703512011-06-16 16:17:05 +0000595}
596void WrapperFrontendAction::ExecuteAction() {
597 WrappedAction->ExecuteAction();
598}
599void WrapperFrontendAction::EndSourceFileAction() {
600 WrappedAction->EndSourceFileAction();
601}
602
603bool WrapperFrontendAction::usesPreprocessorOnly() const {
604 return WrappedAction->usesPreprocessorOnly();
605}
Douglas Gregor69f74f82011-08-25 22:30:56 +0000606TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
607 return WrappedAction->getTranslationUnitKind();
Chandler Carruthb5703512011-06-16 16:17:05 +0000608}
609bool WrapperFrontendAction::hasPCHSupport() const {
610 return WrappedAction->hasPCHSupport();
611}
612bool WrapperFrontendAction::hasASTFileSupport() const {
613 return WrappedAction->hasASTFileSupport();
614}
615bool WrapperFrontendAction::hasIRSupport() const {
616 return WrappedAction->hasIRSupport();
617}
618bool WrapperFrontendAction::hasCodeCompletionSupport() const {
619 return WrappedAction->hasCodeCompletionSupport();
620}
621
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +0000622WrapperFrontendAction::WrapperFrontendAction(
623 std::unique_ptr<FrontendAction> WrappedAction)
624 : WrappedAction(std::move(WrappedAction)) {}
Chandler Carruthb5703512011-06-16 16:17:05 +0000625