1e5dd7070Spatrick //===--- FrontendAction.cpp -----------------------------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick
9e5dd7070Spatrick #include "clang/Frontend/FrontendAction.h"
10e5dd7070Spatrick #include "clang/AST/ASTConsumer.h"
11e5dd7070Spatrick #include "clang/AST/ASTContext.h"
12e5dd7070Spatrick #include "clang/AST/DeclGroup.h"
13e5dd7070Spatrick #include "clang/Basic/Builtins.h"
14*12c85518Srobert #include "clang/Basic/DiagnosticOptions.h"
15*12c85518Srobert #include "clang/Basic/FileEntry.h"
16e5dd7070Spatrick #include "clang/Basic/LangStandard.h"
17*12c85518Srobert #include "clang/Basic/Sarif.h"
18e5dd7070Spatrick #include "clang/Frontend/ASTUnit.h"
19e5dd7070Spatrick #include "clang/Frontend/CompilerInstance.h"
20e5dd7070Spatrick #include "clang/Frontend/FrontendDiagnostic.h"
21e5dd7070Spatrick #include "clang/Frontend/FrontendPluginRegistry.h"
22e5dd7070Spatrick #include "clang/Frontend/LayoutOverrideSource.h"
23e5dd7070Spatrick #include "clang/Frontend/MultiplexConsumer.h"
24*12c85518Srobert #include "clang/Frontend/SARIFDiagnosticPrinter.h"
25e5dd7070Spatrick #include "clang/Frontend/Utils.h"
26e5dd7070Spatrick #include "clang/Lex/HeaderSearch.h"
27e5dd7070Spatrick #include "clang/Lex/LiteralSupport.h"
28e5dd7070Spatrick #include "clang/Lex/Preprocessor.h"
29e5dd7070Spatrick #include "clang/Lex/PreprocessorOptions.h"
30e5dd7070Spatrick #include "clang/Parse/ParseAST.h"
31*12c85518Srobert #include "clang/Sema/HLSLExternalSemaSource.h"
32*12c85518Srobert #include "clang/Sema/MultiplexExternalSemaSource.h"
33e5dd7070Spatrick #include "clang/Serialization/ASTDeserializationListener.h"
34e5dd7070Spatrick #include "clang/Serialization/ASTReader.h"
35e5dd7070Spatrick #include "clang/Serialization/GlobalModuleIndex.h"
36*12c85518Srobert #include "llvm/ADT/ScopeExit.h"
37e5dd7070Spatrick #include "llvm/Support/BuryPointer.h"
38e5dd7070Spatrick #include "llvm/Support/ErrorHandling.h"
39e5dd7070Spatrick #include "llvm/Support/FileSystem.h"
40e5dd7070Spatrick #include "llvm/Support/Path.h"
41e5dd7070Spatrick #include "llvm/Support/Timer.h"
42e5dd7070Spatrick #include "llvm/Support/raw_ostream.h"
43*12c85518Srobert #include <memory>
44e5dd7070Spatrick #include <system_error>
45e5dd7070Spatrick using namespace clang;
46e5dd7070Spatrick
47e5dd7070Spatrick LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
48e5dd7070Spatrick
49e5dd7070Spatrick namespace {
50e5dd7070Spatrick
51e5dd7070Spatrick class DelegatingDeserializationListener : public ASTDeserializationListener {
52e5dd7070Spatrick ASTDeserializationListener *Previous;
53e5dd7070Spatrick bool DeletePrevious;
54e5dd7070Spatrick
55e5dd7070Spatrick public:
DelegatingDeserializationListener(ASTDeserializationListener * Previous,bool DeletePrevious)56e5dd7070Spatrick explicit DelegatingDeserializationListener(
57e5dd7070Spatrick ASTDeserializationListener *Previous, bool DeletePrevious)
58e5dd7070Spatrick : Previous(Previous), DeletePrevious(DeletePrevious) {}
~DelegatingDeserializationListener()59e5dd7070Spatrick ~DelegatingDeserializationListener() override {
60e5dd7070Spatrick if (DeletePrevious)
61e5dd7070Spatrick delete Previous;
62e5dd7070Spatrick }
63e5dd7070Spatrick
ReaderInitialized(ASTReader * Reader)64e5dd7070Spatrick void ReaderInitialized(ASTReader *Reader) override {
65e5dd7070Spatrick if (Previous)
66e5dd7070Spatrick Previous->ReaderInitialized(Reader);
67e5dd7070Spatrick }
IdentifierRead(serialization::IdentID ID,IdentifierInfo * II)68e5dd7070Spatrick void IdentifierRead(serialization::IdentID ID,
69e5dd7070Spatrick IdentifierInfo *II) override {
70e5dd7070Spatrick if (Previous)
71e5dd7070Spatrick Previous->IdentifierRead(ID, II);
72e5dd7070Spatrick }
TypeRead(serialization::TypeIdx Idx,QualType T)73e5dd7070Spatrick void TypeRead(serialization::TypeIdx Idx, QualType T) override {
74e5dd7070Spatrick if (Previous)
75e5dd7070Spatrick Previous->TypeRead(Idx, T);
76e5dd7070Spatrick }
DeclRead(serialization::DeclID ID,const Decl * D)77e5dd7070Spatrick void DeclRead(serialization::DeclID ID, const Decl *D) override {
78e5dd7070Spatrick if (Previous)
79e5dd7070Spatrick Previous->DeclRead(ID, D);
80e5dd7070Spatrick }
SelectorRead(serialization::SelectorID ID,Selector Sel)81e5dd7070Spatrick void SelectorRead(serialization::SelectorID ID, Selector Sel) override {
82e5dd7070Spatrick if (Previous)
83e5dd7070Spatrick Previous->SelectorRead(ID, Sel);
84e5dd7070Spatrick }
MacroDefinitionRead(serialization::PreprocessedEntityID PPID,MacroDefinitionRecord * MD)85e5dd7070Spatrick void MacroDefinitionRead(serialization::PreprocessedEntityID PPID,
86e5dd7070Spatrick MacroDefinitionRecord *MD) override {
87e5dd7070Spatrick if (Previous)
88e5dd7070Spatrick Previous->MacroDefinitionRead(PPID, MD);
89e5dd7070Spatrick }
90e5dd7070Spatrick };
91e5dd7070Spatrick
92e5dd7070Spatrick /// Dumps deserialized declarations.
93e5dd7070Spatrick class DeserializedDeclsDumper : public DelegatingDeserializationListener {
94e5dd7070Spatrick public:
DeserializedDeclsDumper(ASTDeserializationListener * Previous,bool DeletePrevious)95e5dd7070Spatrick explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
96e5dd7070Spatrick bool DeletePrevious)
97e5dd7070Spatrick : DelegatingDeserializationListener(Previous, DeletePrevious) {}
98e5dd7070Spatrick
DeclRead(serialization::DeclID ID,const Decl * D)99e5dd7070Spatrick void DeclRead(serialization::DeclID ID, const Decl *D) override {
100e5dd7070Spatrick llvm::outs() << "PCH DECL: " << D->getDeclKindName();
101e5dd7070Spatrick if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
102e5dd7070Spatrick llvm::outs() << " - ";
103e5dd7070Spatrick ND->printQualifiedName(llvm::outs());
104e5dd7070Spatrick }
105e5dd7070Spatrick llvm::outs() << "\n";
106e5dd7070Spatrick
107e5dd7070Spatrick DelegatingDeserializationListener::DeclRead(ID, D);
108e5dd7070Spatrick }
109e5dd7070Spatrick };
110e5dd7070Spatrick
111e5dd7070Spatrick /// Checks deserialized declarations and emits error if a name
112e5dd7070Spatrick /// matches one given in command-line using -error-on-deserialized-decl.
113e5dd7070Spatrick class DeserializedDeclsChecker : public DelegatingDeserializationListener {
114e5dd7070Spatrick ASTContext &Ctx;
115e5dd7070Spatrick std::set<std::string> NamesToCheck;
116e5dd7070Spatrick
117e5dd7070Spatrick public:
DeserializedDeclsChecker(ASTContext & Ctx,const std::set<std::string> & NamesToCheck,ASTDeserializationListener * Previous,bool DeletePrevious)118e5dd7070Spatrick DeserializedDeclsChecker(ASTContext &Ctx,
119e5dd7070Spatrick const std::set<std::string> &NamesToCheck,
120e5dd7070Spatrick ASTDeserializationListener *Previous,
121e5dd7070Spatrick bool DeletePrevious)
122e5dd7070Spatrick : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
123e5dd7070Spatrick NamesToCheck(NamesToCheck) {}
124e5dd7070Spatrick
DeclRead(serialization::DeclID ID,const Decl * D)125e5dd7070Spatrick void DeclRead(serialization::DeclID ID, const Decl *D) override {
126e5dd7070Spatrick if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
127e5dd7070Spatrick if (NamesToCheck.find(ND->getNameAsString()) != NamesToCheck.end()) {
128e5dd7070Spatrick unsigned DiagID
129e5dd7070Spatrick = Ctx.getDiagnostics().getCustomDiagID(DiagnosticsEngine::Error,
130e5dd7070Spatrick "%0 was deserialized");
131e5dd7070Spatrick Ctx.getDiagnostics().Report(Ctx.getFullLoc(D->getLocation()), DiagID)
132a9ac8606Spatrick << ND;
133e5dd7070Spatrick }
134e5dd7070Spatrick
135e5dd7070Spatrick DelegatingDeserializationListener::DeclRead(ID, D);
136e5dd7070Spatrick }
137e5dd7070Spatrick };
138e5dd7070Spatrick
139e5dd7070Spatrick } // end anonymous namespace
140e5dd7070Spatrick
FrontendAction()141e5dd7070Spatrick FrontendAction::FrontendAction() : Instance(nullptr) {}
142e5dd7070Spatrick
~FrontendAction()143e5dd7070Spatrick FrontendAction::~FrontendAction() {}
144e5dd7070Spatrick
setCurrentInput(const FrontendInputFile & CurrentInput,std::unique_ptr<ASTUnit> AST)145e5dd7070Spatrick void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
146e5dd7070Spatrick std::unique_ptr<ASTUnit> AST) {
147e5dd7070Spatrick this->CurrentInput = CurrentInput;
148e5dd7070Spatrick CurrentASTUnit = std::move(AST);
149e5dd7070Spatrick }
150e5dd7070Spatrick
getCurrentModule() const151e5dd7070Spatrick Module *FrontendAction::getCurrentModule() const {
152e5dd7070Spatrick CompilerInstance &CI = getCompilerInstance();
153e5dd7070Spatrick return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
154*12c85518Srobert CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false);
155e5dd7070Spatrick }
156e5dd7070Spatrick
157e5dd7070Spatrick std::unique_ptr<ASTConsumer>
CreateWrappedASTConsumer(CompilerInstance & CI,StringRef InFile)158e5dd7070Spatrick FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
159e5dd7070Spatrick StringRef InFile) {
160e5dd7070Spatrick std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
161e5dd7070Spatrick if (!Consumer)
162e5dd7070Spatrick return nullptr;
163e5dd7070Spatrick
164e5dd7070Spatrick // Validate -add-plugin args.
165e5dd7070Spatrick bool FoundAllPlugins = true;
166e5dd7070Spatrick for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
167e5dd7070Spatrick bool Found = false;
168ec727ea7Spatrick for (const FrontendPluginRegistry::entry &Plugin :
169ec727ea7Spatrick FrontendPluginRegistry::entries()) {
170ec727ea7Spatrick if (Plugin.getName() == Arg)
171e5dd7070Spatrick Found = true;
172e5dd7070Spatrick }
173e5dd7070Spatrick if (!Found) {
174e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_fe_invalid_plugin_name) << Arg;
175e5dd7070Spatrick FoundAllPlugins = false;
176e5dd7070Spatrick }
177e5dd7070Spatrick }
178e5dd7070Spatrick if (!FoundAllPlugins)
179e5dd7070Spatrick return nullptr;
180e5dd7070Spatrick
181e5dd7070Spatrick // If there are no registered plugins we don't need to wrap the consumer
182e5dd7070Spatrick if (FrontendPluginRegistry::begin() == FrontendPluginRegistry::end())
183e5dd7070Spatrick return Consumer;
184e5dd7070Spatrick
185e5dd7070Spatrick // If this is a code completion run, avoid invoking the plugin consumers
186e5dd7070Spatrick if (CI.hasCodeCompletionConsumer())
187e5dd7070Spatrick return Consumer;
188e5dd7070Spatrick
189e5dd7070Spatrick // Collect the list of plugins that go before the main action (in Consumers)
190e5dd7070Spatrick // or after it (in AfterConsumers)
191e5dd7070Spatrick std::vector<std::unique_ptr<ASTConsumer>> Consumers;
192e5dd7070Spatrick std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
193ec727ea7Spatrick for (const FrontendPluginRegistry::entry &Plugin :
194ec727ea7Spatrick FrontendPluginRegistry::entries()) {
195ec727ea7Spatrick std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
196e5dd7070Spatrick PluginASTAction::ActionType ActionType = P->getActionType();
197*12c85518Srobert if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
198*12c85518Srobert ActionType == PluginASTAction::CmdlineBeforeMainAction) {
199e5dd7070Spatrick // This is O(|plugins| * |add_plugins|), but since both numbers are
200e5dd7070Spatrick // way below 50 in practice, that's ok.
201*12c85518Srobert if (llvm::is_contained(CI.getFrontendOpts().AddPluginActions,
202*12c85518Srobert Plugin.getName())) {
203*12c85518Srobert if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
204*12c85518Srobert ActionType = PluginASTAction::AddBeforeMainAction;
205*12c85518Srobert else
206e5dd7070Spatrick ActionType = PluginASTAction::AddAfterMainAction;
207e5dd7070Spatrick }
208*12c85518Srobert }
209e5dd7070Spatrick if ((ActionType == PluginASTAction::AddBeforeMainAction ||
210e5dd7070Spatrick ActionType == PluginASTAction::AddAfterMainAction) &&
211ec727ea7Spatrick P->ParseArgs(
212ec727ea7Spatrick CI,
213ec727ea7Spatrick CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
214e5dd7070Spatrick std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
215e5dd7070Spatrick if (ActionType == PluginASTAction::AddBeforeMainAction) {
216e5dd7070Spatrick Consumers.push_back(std::move(PluginConsumer));
217e5dd7070Spatrick } else {
218e5dd7070Spatrick AfterConsumers.push_back(std::move(PluginConsumer));
219e5dd7070Spatrick }
220e5dd7070Spatrick }
221e5dd7070Spatrick }
222e5dd7070Spatrick
223e5dd7070Spatrick // Add to Consumers the main consumer, then all the plugins that go after it
224e5dd7070Spatrick Consumers.push_back(std::move(Consumer));
225*12c85518Srobert if (!AfterConsumers.empty()) {
226*12c85518Srobert // If we have plugins after the main consumer, which may be the codegen
227*12c85518Srobert // action, they likely will need the ASTContext, so don't clear it in the
228*12c85518Srobert // codegen action.
229*12c85518Srobert CI.getCodeGenOpts().ClearASTBeforeBackend = false;
230*12c85518Srobert for (auto &C : AfterConsumers)
231e5dd7070Spatrick Consumers.push_back(std::move(C));
232e5dd7070Spatrick }
233e5dd7070Spatrick
234e5dd7070Spatrick return std::make_unique<MultiplexConsumer>(std::move(Consumers));
235e5dd7070Spatrick }
236e5dd7070Spatrick
237e5dd7070Spatrick /// For preprocessed files, if the first line is the linemarker and specifies
238e5dd7070Spatrick /// the original source file name, use that name as the input file name.
239e5dd7070Spatrick /// Returns the location of the first token after the line marker directive.
240e5dd7070Spatrick ///
241e5dd7070Spatrick /// \param CI The compiler instance.
242e5dd7070Spatrick /// \param InputFile Populated with the filename from the line marker.
243e5dd7070Spatrick /// \param IsModuleMap If \c true, add a line note corresponding to this line
244e5dd7070Spatrick /// directive. (We need to do this because the directive will not be
245e5dd7070Spatrick /// visited by the preprocessor.)
ReadOriginalFileName(CompilerInstance & CI,std::string & InputFile,bool IsModuleMap=false)246e5dd7070Spatrick static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
247e5dd7070Spatrick std::string &InputFile,
248e5dd7070Spatrick bool IsModuleMap = false) {
249e5dd7070Spatrick auto &SourceMgr = CI.getSourceManager();
250e5dd7070Spatrick auto MainFileID = SourceMgr.getMainFileID();
251e5dd7070Spatrick
252a9ac8606Spatrick auto MainFileBuf = SourceMgr.getBufferOrNone(MainFileID);
253a9ac8606Spatrick if (!MainFileBuf)
254e5dd7070Spatrick return SourceLocation();
255e5dd7070Spatrick
256e5dd7070Spatrick std::unique_ptr<Lexer> RawLexer(
257a9ac8606Spatrick new Lexer(MainFileID, *MainFileBuf, SourceMgr, CI.getLangOpts()));
258e5dd7070Spatrick
259e5dd7070Spatrick // If the first line has the syntax of
260e5dd7070Spatrick //
261e5dd7070Spatrick // # NUM "FILENAME"
262e5dd7070Spatrick //
263e5dd7070Spatrick // we use FILENAME as the input file name.
264e5dd7070Spatrick Token T;
265e5dd7070Spatrick if (RawLexer->LexFromRawLexer(T) || T.getKind() != tok::hash)
266e5dd7070Spatrick return SourceLocation();
267e5dd7070Spatrick if (RawLexer->LexFromRawLexer(T) || T.isAtStartOfLine() ||
268e5dd7070Spatrick T.getKind() != tok::numeric_constant)
269e5dd7070Spatrick return SourceLocation();
270e5dd7070Spatrick
271e5dd7070Spatrick unsigned LineNo;
272e5dd7070Spatrick SourceLocation LineNoLoc = T.getLocation();
273e5dd7070Spatrick if (IsModuleMap) {
274e5dd7070Spatrick llvm::SmallString<16> Buffer;
275e5dd7070Spatrick if (Lexer::getSpelling(LineNoLoc, Buffer, SourceMgr, CI.getLangOpts())
276e5dd7070Spatrick .getAsInteger(10, LineNo))
277e5dd7070Spatrick return SourceLocation();
278e5dd7070Spatrick }
279e5dd7070Spatrick
280e5dd7070Spatrick RawLexer->LexFromRawLexer(T);
281e5dd7070Spatrick if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
282e5dd7070Spatrick return SourceLocation();
283e5dd7070Spatrick
284e5dd7070Spatrick StringLiteralParser Literal(T, CI.getPreprocessor());
285e5dd7070Spatrick if (Literal.hadError)
286e5dd7070Spatrick return SourceLocation();
287e5dd7070Spatrick RawLexer->LexFromRawLexer(T);
288e5dd7070Spatrick if (T.isNot(tok::eof) && !T.isAtStartOfLine())
289e5dd7070Spatrick return SourceLocation();
290e5dd7070Spatrick InputFile = Literal.GetString().str();
291e5dd7070Spatrick
292e5dd7070Spatrick if (IsModuleMap)
293e5dd7070Spatrick CI.getSourceManager().AddLineNote(
294e5dd7070Spatrick LineNoLoc, LineNo, SourceMgr.getLineTableFilenameID(InputFile), false,
295e5dd7070Spatrick false, SrcMgr::C_User_ModuleMap);
296e5dd7070Spatrick
297e5dd7070Spatrick return T.getLocation();
298e5dd7070Spatrick }
299e5dd7070Spatrick
300e5dd7070Spatrick static SmallVectorImpl<char> &
operator +=(SmallVectorImpl<char> & Includes,StringRef RHS)301e5dd7070Spatrick operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
302e5dd7070Spatrick Includes.append(RHS.begin(), RHS.end());
303e5dd7070Spatrick return Includes;
304e5dd7070Spatrick }
305e5dd7070Spatrick
addHeaderInclude(StringRef HeaderName,SmallVectorImpl<char> & Includes,const LangOptions & LangOpts,bool IsExternC)306e5dd7070Spatrick static void addHeaderInclude(StringRef HeaderName,
307e5dd7070Spatrick SmallVectorImpl<char> &Includes,
308e5dd7070Spatrick const LangOptions &LangOpts,
309e5dd7070Spatrick bool IsExternC) {
310e5dd7070Spatrick if (IsExternC && LangOpts.CPlusPlus)
311e5dd7070Spatrick Includes += "extern \"C\" {\n";
312e5dd7070Spatrick if (LangOpts.ObjC)
313e5dd7070Spatrick Includes += "#import \"";
314e5dd7070Spatrick else
315e5dd7070Spatrick Includes += "#include \"";
316e5dd7070Spatrick
317e5dd7070Spatrick Includes += HeaderName;
318e5dd7070Spatrick
319e5dd7070Spatrick Includes += "\"\n";
320e5dd7070Spatrick if (IsExternC && LangOpts.CPlusPlus)
321e5dd7070Spatrick Includes += "}\n";
322e5dd7070Spatrick }
323e5dd7070Spatrick
324e5dd7070Spatrick /// Collect the set of header includes needed to construct the given
325e5dd7070Spatrick /// module and update the TopHeaders file set of the module.
326e5dd7070Spatrick ///
327e5dd7070Spatrick /// \param Module The module we're collecting includes from.
328e5dd7070Spatrick ///
329e5dd7070Spatrick /// \param Includes Will be augmented with the set of \#includes or \#imports
330e5dd7070Spatrick /// needed to load all of the named headers.
collectModuleHeaderIncludes(const LangOptions & LangOpts,FileManager & FileMgr,DiagnosticsEngine & Diag,ModuleMap & ModMap,clang::Module * Module,SmallVectorImpl<char> & Includes)331e5dd7070Spatrick static std::error_code collectModuleHeaderIncludes(
332e5dd7070Spatrick const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
333e5dd7070Spatrick ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
334e5dd7070Spatrick // Don't collect any headers for unavailable modules.
335e5dd7070Spatrick if (!Module->isAvailable())
336e5dd7070Spatrick return std::error_code();
337e5dd7070Spatrick
338e5dd7070Spatrick // Resolve all lazy header directives to header files.
339*12c85518Srobert ModMap.resolveHeaderDirectives(Module, /*File=*/std::nullopt);
340e5dd7070Spatrick
341e5dd7070Spatrick // If any headers are missing, we can't build this module. In most cases,
342e5dd7070Spatrick // diagnostics for this should have already been produced; we only get here
343e5dd7070Spatrick // if explicit stat information was provided.
344e5dd7070Spatrick // FIXME: If the name resolves to a file with different stat information,
345e5dd7070Spatrick // produce a better diagnostic.
346e5dd7070Spatrick if (!Module->MissingHeaders.empty()) {
347e5dd7070Spatrick auto &MissingHeader = Module->MissingHeaders.front();
348e5dd7070Spatrick Diag.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
349e5dd7070Spatrick << MissingHeader.IsUmbrella << MissingHeader.FileName;
350e5dd7070Spatrick return std::error_code();
351e5dd7070Spatrick }
352e5dd7070Spatrick
353e5dd7070Spatrick // Add includes for each of these headers.
354e5dd7070Spatrick for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
355e5dd7070Spatrick for (Module::Header &H : Module->Headers[HK]) {
356e5dd7070Spatrick Module->addTopHeader(H.Entry);
357e5dd7070Spatrick // Use the path as specified in the module map file. We'll look for this
358e5dd7070Spatrick // file relative to the module build directory (the directory containing
359e5dd7070Spatrick // the module map file) so this will find the same file that we found
360e5dd7070Spatrick // while parsing the module map.
361a9ac8606Spatrick addHeaderInclude(H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
362a9ac8606Spatrick Module->IsExternC);
363e5dd7070Spatrick }
364e5dd7070Spatrick }
365e5dd7070Spatrick // Note that Module->PrivateHeaders will not be a TopHeader.
366e5dd7070Spatrick
367e5dd7070Spatrick if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
368e5dd7070Spatrick Module->addTopHeader(UmbrellaHeader.Entry);
369e5dd7070Spatrick if (Module->Parent)
370e5dd7070Spatrick // Include the umbrella header for submodules.
371a9ac8606Spatrick addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
372a9ac8606Spatrick Includes, LangOpts, Module->IsExternC);
373e5dd7070Spatrick } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
374e5dd7070Spatrick // Add all of the headers we find in this subdirectory.
375e5dd7070Spatrick std::error_code EC;
376e5dd7070Spatrick SmallString<128> DirNative;
377e5dd7070Spatrick llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
378e5dd7070Spatrick
379e5dd7070Spatrick llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
380*12c85518Srobert SmallVector<
381*12c85518Srobert std::pair<std::string, OptionalFileEntryRefDegradesToFileEntryPtr>, 8>
382*12c85518Srobert Headers;
383e5dd7070Spatrick for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
384e5dd7070Spatrick Dir != End && !EC; Dir.increment(EC)) {
385e5dd7070Spatrick // Check whether this entry has an extension typically associated with
386e5dd7070Spatrick // headers.
387e5dd7070Spatrick if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
388e5dd7070Spatrick .Cases(".h", ".H", ".hh", ".hpp", true)
389e5dd7070Spatrick .Default(false))
390e5dd7070Spatrick continue;
391e5dd7070Spatrick
392*12c85518Srobert auto Header = FileMgr.getOptionalFileRef(Dir->path());
393e5dd7070Spatrick // FIXME: This shouldn't happen unless there is a file system race. Is
394e5dd7070Spatrick // that worth diagnosing?
395e5dd7070Spatrick if (!Header)
396e5dd7070Spatrick continue;
397e5dd7070Spatrick
398e5dd7070Spatrick // If this header is marked 'unavailable' in this module, don't include
399e5dd7070Spatrick // it.
400e5dd7070Spatrick if (ModMap.isHeaderUnavailableInModule(*Header, Module))
401e5dd7070Spatrick continue;
402e5dd7070Spatrick
403e5dd7070Spatrick // Compute the relative path from the directory to this file.
404e5dd7070Spatrick SmallVector<StringRef, 16> Components;
405e5dd7070Spatrick auto PathIt = llvm::sys::path::rbegin(Dir->path());
406e5dd7070Spatrick for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
407e5dd7070Spatrick Components.push_back(*PathIt);
408a9ac8606Spatrick SmallString<128> RelativeHeader(
409a9ac8606Spatrick UmbrellaDir.PathRelativeToRootModuleDirectory);
410e5dd7070Spatrick for (auto It = Components.rbegin(), End = Components.rend(); It != End;
411e5dd7070Spatrick ++It)
412e5dd7070Spatrick llvm::sys::path::append(RelativeHeader, *It);
413e5dd7070Spatrick
414ec727ea7Spatrick std::string RelName = RelativeHeader.c_str();
415ec727ea7Spatrick Headers.push_back(std::make_pair(RelName, *Header));
416e5dd7070Spatrick }
417e5dd7070Spatrick
418e5dd7070Spatrick if (EC)
419e5dd7070Spatrick return EC;
420ec727ea7Spatrick
421ec727ea7Spatrick // Sort header paths and make the header inclusion order deterministic
422ec727ea7Spatrick // across different OSs and filesystems.
423*12c85518Srobert llvm::sort(Headers, llvm::less_first());
424ec727ea7Spatrick for (auto &H : Headers) {
425ec727ea7Spatrick // Include this header as part of the umbrella directory.
426ec727ea7Spatrick Module->addTopHeader(H.second);
427ec727ea7Spatrick addHeaderInclude(H.first, Includes, LangOpts, Module->IsExternC);
428ec727ea7Spatrick }
429e5dd7070Spatrick }
430e5dd7070Spatrick
431e5dd7070Spatrick // Recurse into submodules.
432e5dd7070Spatrick for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
433e5dd7070Spatrick SubEnd = Module->submodule_end();
434e5dd7070Spatrick Sub != SubEnd; ++Sub)
435e5dd7070Spatrick if (std::error_code Err = collectModuleHeaderIncludes(
436e5dd7070Spatrick LangOpts, FileMgr, Diag, ModMap, *Sub, Includes))
437e5dd7070Spatrick return Err;
438e5dd7070Spatrick
439e5dd7070Spatrick return std::error_code();
440e5dd7070Spatrick }
441e5dd7070Spatrick
loadModuleMapForModuleBuild(CompilerInstance & CI,bool IsSystem,bool IsPreprocessed,std::string & PresumedModuleMapFile,unsigned & Offset)442e5dd7070Spatrick static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
443e5dd7070Spatrick bool IsPreprocessed,
444e5dd7070Spatrick std::string &PresumedModuleMapFile,
445e5dd7070Spatrick unsigned &Offset) {
446e5dd7070Spatrick auto &SrcMgr = CI.getSourceManager();
447e5dd7070Spatrick HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
448e5dd7070Spatrick
449e5dd7070Spatrick // Map the current input to a file.
450e5dd7070Spatrick FileID ModuleMapID = SrcMgr.getMainFileID();
451e5dd7070Spatrick const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
452e5dd7070Spatrick
453e5dd7070Spatrick // If the module map is preprocessed, handle the initial line marker;
454e5dd7070Spatrick // line directives are not part of the module map syntax in general.
455e5dd7070Spatrick Offset = 0;
456e5dd7070Spatrick if (IsPreprocessed) {
457e5dd7070Spatrick SourceLocation EndOfLineMarker =
458e5dd7070Spatrick ReadOriginalFileName(CI, PresumedModuleMapFile, /*IsModuleMap*/ true);
459e5dd7070Spatrick if (EndOfLineMarker.isValid())
460e5dd7070Spatrick Offset = CI.getSourceManager().getDecomposedLoc(EndOfLineMarker).second;
461e5dd7070Spatrick }
462e5dd7070Spatrick
463e5dd7070Spatrick // Load the module map file.
464e5dd7070Spatrick if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
465e5dd7070Spatrick PresumedModuleMapFile))
466e5dd7070Spatrick return true;
467e5dd7070Spatrick
468a9ac8606Spatrick if (SrcMgr.getBufferOrFake(ModuleMapID).getBufferSize() == Offset)
469e5dd7070Spatrick Offset = 0;
470e5dd7070Spatrick
471*12c85518Srobert // Infer framework module if possible.
472*12c85518Srobert if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
473*12c85518Srobert SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
474*12c85518Srobert llvm::sys::path::append(InferredFrameworkPath,
475*12c85518Srobert CI.getLangOpts().ModuleName + ".framework");
476*12c85518Srobert if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
477*12c85518Srobert (void)HS.getModuleMap().inferFrameworkModule(*Dir, IsSystem, nullptr);
478*12c85518Srobert }
479*12c85518Srobert
480e5dd7070Spatrick return false;
481e5dd7070Spatrick }
482e5dd7070Spatrick
prepareToBuildModule(CompilerInstance & CI,StringRef ModuleMapFilename)483e5dd7070Spatrick static Module *prepareToBuildModule(CompilerInstance &CI,
484e5dd7070Spatrick StringRef ModuleMapFilename) {
485e5dd7070Spatrick if (CI.getLangOpts().CurrentModule.empty()) {
486e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_missing_module_name);
487e5dd7070Spatrick
488e5dd7070Spatrick // FIXME: Eventually, we could consider asking whether there was just
489e5dd7070Spatrick // a single module described in the module map, and use that as a
490e5dd7070Spatrick // default. Then it would be fairly trivial to just "compile" a module
491e5dd7070Spatrick // map with a single module (the common case).
492e5dd7070Spatrick return nullptr;
493e5dd7070Spatrick }
494e5dd7070Spatrick
495e5dd7070Spatrick // Dig out the module definition.
496e5dd7070Spatrick HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
497*12c85518Srobert Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
498a9ac8606Spatrick /*AllowSearch=*/true);
499e5dd7070Spatrick if (!M) {
500e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_missing_module)
501e5dd7070Spatrick << CI.getLangOpts().CurrentModule << ModuleMapFilename;
502e5dd7070Spatrick
503e5dd7070Spatrick return nullptr;
504e5dd7070Spatrick }
505e5dd7070Spatrick
506e5dd7070Spatrick // Check whether we can build this module at all.
507e5dd7070Spatrick if (Preprocessor::checkModuleIsAvailable(CI.getLangOpts(), CI.getTarget(),
508e5dd7070Spatrick CI.getDiagnostics(), M))
509e5dd7070Spatrick return nullptr;
510e5dd7070Spatrick
511e5dd7070Spatrick // Inform the preprocessor that includes from within the input buffer should
512e5dd7070Spatrick // be resolved relative to the build directory of the module map file.
513e5dd7070Spatrick CI.getPreprocessor().setMainFileDir(M->Directory);
514e5dd7070Spatrick
515e5dd7070Spatrick // If the module was inferred from a different module map (via an expanded
516e5dd7070Spatrick // umbrella module definition), track that fact.
517e5dd7070Spatrick // FIXME: It would be preferable to fill this in as part of processing
518e5dd7070Spatrick // the module map, rather than adding it after the fact.
519e5dd7070Spatrick StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
520e5dd7070Spatrick if (!OriginalModuleMapName.empty()) {
521e5dd7070Spatrick auto OriginalModuleMap =
522e5dd7070Spatrick CI.getFileManager().getFile(OriginalModuleMapName,
523e5dd7070Spatrick /*openFile*/ true);
524e5dd7070Spatrick if (!OriginalModuleMap) {
525e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_module_map_not_found)
526e5dd7070Spatrick << OriginalModuleMapName;
527e5dd7070Spatrick return nullptr;
528e5dd7070Spatrick }
529e5dd7070Spatrick if (*OriginalModuleMap != CI.getSourceManager().getFileEntryForID(
530e5dd7070Spatrick CI.getSourceManager().getMainFileID())) {
531e5dd7070Spatrick M->IsInferred = true;
532e5dd7070Spatrick CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()
533e5dd7070Spatrick .setInferredModuleAllowedBy(M, *OriginalModuleMap);
534e5dd7070Spatrick }
535e5dd7070Spatrick }
536e5dd7070Spatrick
537e5dd7070Spatrick // If we're being run from the command-line, the module build stack will not
538e5dd7070Spatrick // have been filled in yet, so complete it now in order to allow us to detect
539e5dd7070Spatrick // module cycles.
540e5dd7070Spatrick SourceManager &SourceMgr = CI.getSourceManager();
541e5dd7070Spatrick if (SourceMgr.getModuleBuildStack().empty())
542e5dd7070Spatrick SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
543e5dd7070Spatrick FullSourceLoc(SourceLocation(), SourceMgr));
544e5dd7070Spatrick return M;
545e5dd7070Spatrick }
546e5dd7070Spatrick
547e5dd7070Spatrick /// Compute the input buffer that should be used to build the specified module.
548e5dd7070Spatrick static std::unique_ptr<llvm::MemoryBuffer>
getInputBufferForModule(CompilerInstance & CI,Module * M)549e5dd7070Spatrick getInputBufferForModule(CompilerInstance &CI, Module *M) {
550e5dd7070Spatrick FileManager &FileMgr = CI.getFileManager();
551e5dd7070Spatrick
552e5dd7070Spatrick // Collect the set of #includes we need to build the module.
553e5dd7070Spatrick SmallString<256> HeaderContents;
554e5dd7070Spatrick std::error_code Err = std::error_code();
555e5dd7070Spatrick if (Module::Header UmbrellaHeader = M->getUmbrellaHeader())
556a9ac8606Spatrick addHeaderInclude(UmbrellaHeader.PathRelativeToRootModuleDirectory,
557a9ac8606Spatrick HeaderContents, CI.getLangOpts(), M->IsExternC);
558e5dd7070Spatrick Err = collectModuleHeaderIncludes(
559e5dd7070Spatrick CI.getLangOpts(), FileMgr, CI.getDiagnostics(),
560e5dd7070Spatrick CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), M,
561e5dd7070Spatrick HeaderContents);
562e5dd7070Spatrick
563e5dd7070Spatrick if (Err) {
564e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
565e5dd7070Spatrick << M->getFullModuleName() << Err.message();
566e5dd7070Spatrick return nullptr;
567e5dd7070Spatrick }
568e5dd7070Spatrick
569e5dd7070Spatrick return llvm::MemoryBuffer::getMemBufferCopy(
570e5dd7070Spatrick HeaderContents, Module::getModuleInputBufferName());
571e5dd7070Spatrick }
572e5dd7070Spatrick
BeginSourceFile(CompilerInstance & CI,const FrontendInputFile & RealInput)573e5dd7070Spatrick bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
574e5dd7070Spatrick const FrontendInputFile &RealInput) {
575e5dd7070Spatrick FrontendInputFile Input(RealInput);
576e5dd7070Spatrick assert(!Instance && "Already processing a source file!");
577e5dd7070Spatrick assert(!Input.isEmpty() && "Unexpected empty filename!");
578e5dd7070Spatrick setCurrentInput(Input);
579e5dd7070Spatrick setCompilerInstance(&CI);
580e5dd7070Spatrick
581e5dd7070Spatrick bool HasBegunSourceFile = false;
582e5dd7070Spatrick bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
583e5dd7070Spatrick usesPreprocessorOnly();
584*12c85518Srobert
585*12c85518Srobert // If we fail, reset state since the client will not end up calling the
586*12c85518Srobert // matching EndSourceFile(). All paths that return true should release this.
587*12c85518Srobert auto FailureCleanup = llvm::make_scope_exit([&]() {
588*12c85518Srobert if (HasBegunSourceFile)
589*12c85518Srobert CI.getDiagnosticClient().EndSourceFile();
590*12c85518Srobert CI.setASTConsumer(nullptr);
591*12c85518Srobert CI.clearOutputFiles(/*EraseFiles=*/true);
592*12c85518Srobert CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
593*12c85518Srobert setCurrentInput(FrontendInputFile());
594*12c85518Srobert setCompilerInstance(nullptr);
595*12c85518Srobert });
596*12c85518Srobert
597e5dd7070Spatrick if (!BeginInvocation(CI))
598*12c85518Srobert return false;
599e5dd7070Spatrick
600e5dd7070Spatrick // If we're replaying the build of an AST file, import it and set up
601e5dd7070Spatrick // the initial state from its build.
602e5dd7070Spatrick if (ReplayASTFile) {
603e5dd7070Spatrick IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
604e5dd7070Spatrick
605e5dd7070Spatrick // The AST unit populates its own diagnostics engine rather than ours.
606e5dd7070Spatrick IntrusiveRefCntPtr<DiagnosticsEngine> ASTDiags(
607e5dd7070Spatrick new DiagnosticsEngine(Diags->getDiagnosticIDs(),
608e5dd7070Spatrick &Diags->getDiagnosticOptions()));
609e5dd7070Spatrick ASTDiags->setClient(Diags->getClient(), /*OwnsClient*/false);
610e5dd7070Spatrick
611e5dd7070Spatrick // FIXME: What if the input is a memory buffer?
612e5dd7070Spatrick StringRef InputFile = Input.getFile();
613e5dd7070Spatrick
614e5dd7070Spatrick std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
615ec727ea7Spatrick std::string(InputFile), CI.getPCHContainerReader(),
616ec727ea7Spatrick ASTUnit::LoadPreprocessorOnly, ASTDiags, CI.getFileSystemOpts(),
617ec727ea7Spatrick CI.getCodeGenOpts().DebugTypeExtRefs);
618e5dd7070Spatrick if (!AST)
619*12c85518Srobert return false;
620e5dd7070Spatrick
621e5dd7070Spatrick // Options relating to how we treat the input (but not what we do with it)
622e5dd7070Spatrick // are inherited from the AST unit.
623e5dd7070Spatrick CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
624e5dd7070Spatrick CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
625e5dd7070Spatrick CI.getLangOpts() = AST->getLangOpts();
626e5dd7070Spatrick
627e5dd7070Spatrick // Set the shared objects, these are reset when we finish processing the
628e5dd7070Spatrick // file, otherwise the CompilerInstance will happily destroy them.
629e5dd7070Spatrick CI.setFileManager(&AST->getFileManager());
630e5dd7070Spatrick CI.createSourceManager(CI.getFileManager());
631e5dd7070Spatrick CI.getSourceManager().initializeForReplay(AST->getSourceManager());
632e5dd7070Spatrick
633e5dd7070Spatrick // Preload all the module files loaded transitively by the AST unit. Also
634e5dd7070Spatrick // load all module map files that were parsed as part of building the AST
635e5dd7070Spatrick // unit.
636e5dd7070Spatrick if (auto ASTReader = AST->getASTReader()) {
637e5dd7070Spatrick auto &MM = ASTReader->getModuleManager();
638e5dd7070Spatrick auto &PrimaryModule = MM.getPrimaryModule();
639e5dd7070Spatrick
640e5dd7070Spatrick for (serialization::ModuleFile &MF : MM)
641e5dd7070Spatrick if (&MF != &PrimaryModule)
642e5dd7070Spatrick CI.getFrontendOpts().ModuleFiles.push_back(MF.FileName);
643e5dd7070Spatrick
644*12c85518Srobert ASTReader->visitTopLevelModuleMaps(PrimaryModule, [&](FileEntryRef FE) {
645ec727ea7Spatrick CI.getFrontendOpts().ModuleMapFiles.push_back(
646*12c85518Srobert std::string(FE.getName()));
647e5dd7070Spatrick });
648e5dd7070Spatrick }
649e5dd7070Spatrick
650e5dd7070Spatrick // Set up the input file for replay purposes.
651e5dd7070Spatrick auto Kind = AST->getInputKind();
652e5dd7070Spatrick if (Kind.getFormat() == InputKind::ModuleMap) {
653e5dd7070Spatrick Module *ASTModule =
654e5dd7070Spatrick AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
655*12c85518Srobert AST->getLangOpts().CurrentModule, SourceLocation(),
656*12c85518Srobert /*AllowSearch*/ false);
657e5dd7070Spatrick assert(ASTModule && "module file does not define its own module");
658e5dd7070Spatrick Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
659e5dd7070Spatrick } else {
660e5dd7070Spatrick auto &OldSM = AST->getSourceManager();
661e5dd7070Spatrick FileID ID = OldSM.getMainFileID();
662e5dd7070Spatrick if (auto *File = OldSM.getFileEntryForID(ID))
663e5dd7070Spatrick Input = FrontendInputFile(File->getName(), Kind);
664e5dd7070Spatrick else
665a9ac8606Spatrick Input = FrontendInputFile(OldSM.getBufferOrFake(ID), Kind);
666e5dd7070Spatrick }
667e5dd7070Spatrick setCurrentInput(Input, std::move(AST));
668e5dd7070Spatrick }
669e5dd7070Spatrick
670e5dd7070Spatrick // AST files follow a very different path, since they share objects via the
671e5dd7070Spatrick // AST unit.
672e5dd7070Spatrick if (Input.getKind().getFormat() == InputKind::Precompiled) {
673e5dd7070Spatrick assert(!usesPreprocessorOnly() && "this case was handled above");
674e5dd7070Spatrick assert(hasASTFileSupport() &&
675e5dd7070Spatrick "This action does not have AST file support!");
676e5dd7070Spatrick
677e5dd7070Spatrick IntrusiveRefCntPtr<DiagnosticsEngine> Diags(&CI.getDiagnostics());
678e5dd7070Spatrick
679e5dd7070Spatrick // FIXME: What if the input is a memory buffer?
680e5dd7070Spatrick StringRef InputFile = Input.getFile();
681e5dd7070Spatrick
682e5dd7070Spatrick std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
683ec727ea7Spatrick std::string(InputFile), CI.getPCHContainerReader(),
684ec727ea7Spatrick ASTUnit::LoadEverything, Diags, CI.getFileSystemOpts(),
685ec727ea7Spatrick CI.getCodeGenOpts().DebugTypeExtRefs);
686e5dd7070Spatrick
687e5dd7070Spatrick if (!AST)
688*12c85518Srobert return false;
689e5dd7070Spatrick
690e5dd7070Spatrick // Inform the diagnostic client we are processing a source file.
691e5dd7070Spatrick CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
692e5dd7070Spatrick HasBegunSourceFile = true;
693e5dd7070Spatrick
694e5dd7070Spatrick // Set the shared objects, these are reset when we finish processing the
695e5dd7070Spatrick // file, otherwise the CompilerInstance will happily destroy them.
696e5dd7070Spatrick CI.setFileManager(&AST->getFileManager());
697e5dd7070Spatrick CI.setSourceManager(&AST->getSourceManager());
698e5dd7070Spatrick CI.setPreprocessor(AST->getPreprocessorPtr());
699e5dd7070Spatrick Preprocessor &PP = CI.getPreprocessor();
700e5dd7070Spatrick PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
701e5dd7070Spatrick PP.getLangOpts());
702e5dd7070Spatrick CI.setASTContext(&AST->getASTContext());
703e5dd7070Spatrick
704e5dd7070Spatrick setCurrentInput(Input, std::move(AST));
705e5dd7070Spatrick
706e5dd7070Spatrick // Initialize the action.
707e5dd7070Spatrick if (!BeginSourceFileAction(CI))
708*12c85518Srobert return false;
709e5dd7070Spatrick
710e5dd7070Spatrick // Create the AST consumer.
711e5dd7070Spatrick CI.setASTConsumer(CreateWrappedASTConsumer(CI, InputFile));
712e5dd7070Spatrick if (!CI.hasASTConsumer())
713*12c85518Srobert return false;
714e5dd7070Spatrick
715*12c85518Srobert FailureCleanup.release();
716e5dd7070Spatrick return true;
717e5dd7070Spatrick }
718e5dd7070Spatrick
719e5dd7070Spatrick // Set up the file and source managers, if needed.
720e5dd7070Spatrick if (!CI.hasFileManager()) {
721e5dd7070Spatrick if (!CI.createFileManager()) {
722*12c85518Srobert return false;
723e5dd7070Spatrick }
724e5dd7070Spatrick }
725*12c85518Srobert if (!CI.hasSourceManager()) {
726e5dd7070Spatrick CI.createSourceManager(CI.getFileManager());
727*12c85518Srobert if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
728*12c85518Srobert static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
729*12c85518Srobert ->setSarifWriter(
730*12c85518Srobert std::make_unique<SarifDocumentWriter>(CI.getSourceManager()));
731*12c85518Srobert }
732*12c85518Srobert }
733e5dd7070Spatrick
734e5dd7070Spatrick // Set up embedding for any specified files. Do this before we load any
735e5dd7070Spatrick // source files, including the primary module map for the compilation.
736e5dd7070Spatrick for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
737e5dd7070Spatrick if (auto FE = CI.getFileManager().getFile(F, /*openFile*/true))
738e5dd7070Spatrick CI.getSourceManager().setFileIsTransient(*FE);
739e5dd7070Spatrick else
740e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
741e5dd7070Spatrick }
742e5dd7070Spatrick if (CI.getFrontendOpts().ModulesEmbedAllFiles)
743e5dd7070Spatrick CI.getSourceManager().setAllFilesAreTransient(true);
744e5dd7070Spatrick
745e5dd7070Spatrick // IR files bypass the rest of initialization.
746e5dd7070Spatrick if (Input.getKind().getLanguage() == Language::LLVM_IR) {
747e5dd7070Spatrick assert(hasIRSupport() &&
748e5dd7070Spatrick "This action does not have IR file support!");
749e5dd7070Spatrick
750e5dd7070Spatrick // Inform the diagnostic client we are processing a source file.
751e5dd7070Spatrick CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), nullptr);
752e5dd7070Spatrick HasBegunSourceFile = true;
753e5dd7070Spatrick
754e5dd7070Spatrick // Initialize the action.
755e5dd7070Spatrick if (!BeginSourceFileAction(CI))
756*12c85518Srobert return false;
757e5dd7070Spatrick
758e5dd7070Spatrick // Initialize the main file entry.
759e5dd7070Spatrick if (!CI.InitializeSourceManager(CurrentInput))
760*12c85518Srobert return false;
761e5dd7070Spatrick
762*12c85518Srobert FailureCleanup.release();
763e5dd7070Spatrick return true;
764e5dd7070Spatrick }
765e5dd7070Spatrick
766e5dd7070Spatrick // If the implicit PCH include is actually a directory, rather than
767e5dd7070Spatrick // a single file, search for a suitable PCH file in that directory.
768e5dd7070Spatrick if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
769e5dd7070Spatrick FileManager &FileMgr = CI.getFileManager();
770e5dd7070Spatrick PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
771e5dd7070Spatrick StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
772e5dd7070Spatrick std::string SpecificModuleCachePath = CI.getSpecificModuleCachePath();
773*12c85518Srobert if (auto PCHDir = FileMgr.getOptionalDirectoryRef(PCHInclude)) {
774e5dd7070Spatrick std::error_code EC;
775e5dd7070Spatrick SmallString<128> DirNative;
776*12c85518Srobert llvm::sys::path::native(PCHDir->getName(), DirNative);
777e5dd7070Spatrick bool Found = false;
778e5dd7070Spatrick llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
779e5dd7070Spatrick for (llvm::vfs::directory_iterator Dir = FS.dir_begin(DirNative, EC),
780e5dd7070Spatrick DirEnd;
781e5dd7070Spatrick Dir != DirEnd && !EC; Dir.increment(EC)) {
782e5dd7070Spatrick // Check whether this is an acceptable AST file.
783e5dd7070Spatrick if (ASTReader::isAcceptableASTFile(
784*12c85518Srobert Dir->path(), FileMgr, CI.getModuleCache(),
785*12c85518Srobert CI.getPCHContainerReader(), CI.getLangOpts(),
786*12c85518Srobert CI.getTargetOpts(), CI.getPreprocessorOpts(),
787*12c85518Srobert SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) {
788ec727ea7Spatrick PPOpts.ImplicitPCHInclude = std::string(Dir->path());
789e5dd7070Spatrick Found = true;
790e5dd7070Spatrick break;
791e5dd7070Spatrick }
792e5dd7070Spatrick }
793e5dd7070Spatrick
794e5dd7070Spatrick if (!Found) {
795e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_fe_no_pch_in_dir) << PCHInclude;
796*12c85518Srobert return false;
797e5dd7070Spatrick }
798e5dd7070Spatrick }
799e5dd7070Spatrick }
800e5dd7070Spatrick
801e5dd7070Spatrick // Set up the preprocessor if needed. When parsing model files the
802e5dd7070Spatrick // preprocessor of the original source is reused.
803e5dd7070Spatrick if (!isModelParsingAction())
804e5dd7070Spatrick CI.createPreprocessor(getTranslationUnitKind());
805e5dd7070Spatrick
806e5dd7070Spatrick // Inform the diagnostic client we are processing a source file.
807e5dd7070Spatrick CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(),
808e5dd7070Spatrick &CI.getPreprocessor());
809e5dd7070Spatrick HasBegunSourceFile = true;
810e5dd7070Spatrick
811*12c85518Srobert // Handle C++20 header units.
812*12c85518Srobert // Here, the user has the option to specify that the header name should be
813*12c85518Srobert // looked up in the pre-processor search paths (and the main filename as
814*12c85518Srobert // passed by the driver might therefore be incomplete until that look-up).
815*12c85518Srobert if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
816*12c85518Srobert !Input.getKind().isPreprocessed()) {
817*12c85518Srobert StringRef FileName = Input.getFile();
818*12c85518Srobert InputKind Kind = Input.getKind();
819*12c85518Srobert if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
820*12c85518Srobert assert(CI.hasPreprocessor() &&
821*12c85518Srobert "trying to build a header unit without a Pre-processor?");
822*12c85518Srobert HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
823*12c85518Srobert // Relative searches begin from CWD.
824*12c85518Srobert const DirectoryEntry *Dir = nullptr;
825*12c85518Srobert if (auto DirOrErr = CI.getFileManager().getDirectory("."))
826*12c85518Srobert Dir = *DirOrErr;
827*12c85518Srobert SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 1> CWD;
828*12c85518Srobert CWD.push_back({nullptr, Dir});
829*12c85518Srobert OptionalFileEntryRef FE =
830*12c85518Srobert HS.LookupFile(FileName, SourceLocation(),
831*12c85518Srobert /*Angled*/ Input.getKind().getHeaderUnitKind() ==
832*12c85518Srobert InputKind::HeaderUnit_System,
833*12c85518Srobert nullptr, nullptr, CWD, nullptr, nullptr, nullptr,
834*12c85518Srobert nullptr, nullptr, nullptr);
835*12c85518Srobert if (!FE) {
836*12c85518Srobert CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
837*12c85518Srobert << FileName;
838*12c85518Srobert return false;
839*12c85518Srobert }
840*12c85518Srobert // We now have the filename...
841*12c85518Srobert FileName = FE->getFileEntry().getName();
842*12c85518Srobert // ... still a header unit, but now use the path as written.
843*12c85518Srobert Kind = Input.getKind().withHeaderUnit(InputKind::HeaderUnit_Abs);
844*12c85518Srobert Input = FrontendInputFile(FileName, Kind, Input.isSystem());
845*12c85518Srobert }
846*12c85518Srobert // Unless the user has overridden the name, the header unit module name is
847*12c85518Srobert // the pathname for the file.
848*12c85518Srobert if (CI.getLangOpts().ModuleName.empty())
849*12c85518Srobert CI.getLangOpts().ModuleName = std::string(FileName);
850*12c85518Srobert CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
851*12c85518Srobert }
852*12c85518Srobert
853e5dd7070Spatrick if (!CI.InitializeSourceManager(Input))
854*12c85518Srobert return false;
855*12c85518Srobert
856*12c85518Srobert if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
857*12c85518Srobert Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
858*12c85518Srobert // We have an input filename like foo.iih, but we want to find the right
859*12c85518Srobert // module name (and original file, to build the map entry).
860*12c85518Srobert // Check if the first line specifies the original source file name with a
861*12c85518Srobert // linemarker.
862*12c85518Srobert std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
863*12c85518Srobert ReadOriginalFileName(CI, PresumedInputFile);
864*12c85518Srobert // Unless the user overrides this, the module name is the name by which the
865*12c85518Srobert // original file was known.
866*12c85518Srobert if (CI.getLangOpts().ModuleName.empty())
867*12c85518Srobert CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
868*12c85518Srobert CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
869*12c85518Srobert }
870e5dd7070Spatrick
871e5dd7070Spatrick // For module map files, we first parse the module map and synthesize a
872e5dd7070Spatrick // "<module-includes>" buffer before more conventional processing.
873e5dd7070Spatrick if (Input.getKind().getFormat() == InputKind::ModuleMap) {
874e5dd7070Spatrick CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
875e5dd7070Spatrick
876e5dd7070Spatrick std::string PresumedModuleMapFile;
877e5dd7070Spatrick unsigned OffsetToContents;
878e5dd7070Spatrick if (loadModuleMapForModuleBuild(CI, Input.isSystem(),
879e5dd7070Spatrick Input.isPreprocessed(),
880e5dd7070Spatrick PresumedModuleMapFile, OffsetToContents))
881*12c85518Srobert return false;
882e5dd7070Spatrick
883e5dd7070Spatrick auto *CurrentModule = prepareToBuildModule(CI, Input.getFile());
884e5dd7070Spatrick if (!CurrentModule)
885*12c85518Srobert return false;
886e5dd7070Spatrick
887e5dd7070Spatrick CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
888e5dd7070Spatrick
889e5dd7070Spatrick if (OffsetToContents)
890e5dd7070Spatrick // If the module contents are in the same file, skip to them.
891e5dd7070Spatrick CI.getPreprocessor().setSkipMainFilePreamble(OffsetToContents, true);
892e5dd7070Spatrick else {
893e5dd7070Spatrick // Otherwise, convert the module description to a suitable input buffer.
894e5dd7070Spatrick auto Buffer = getInputBufferForModule(CI, CurrentModule);
895e5dd7070Spatrick if (!Buffer)
896*12c85518Srobert return false;
897e5dd7070Spatrick
898e5dd7070Spatrick // Reinitialize the main file entry to refer to the new input.
899e5dd7070Spatrick auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
900e5dd7070Spatrick auto &SourceMgr = CI.getSourceManager();
901e5dd7070Spatrick auto BufferID = SourceMgr.createFileID(std::move(Buffer), Kind);
902ec727ea7Spatrick assert(BufferID.isValid() && "couldn't create module buffer ID");
903e5dd7070Spatrick SourceMgr.setMainFileID(BufferID);
904e5dd7070Spatrick }
905e5dd7070Spatrick }
906e5dd7070Spatrick
907e5dd7070Spatrick // Initialize the action.
908e5dd7070Spatrick if (!BeginSourceFileAction(CI))
909*12c85518Srobert return false;
910e5dd7070Spatrick
911e5dd7070Spatrick // If we were asked to load any module map files, do so now.
912e5dd7070Spatrick for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
913e5dd7070Spatrick if (auto File = CI.getFileManager().getFile(Filename))
914e5dd7070Spatrick CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
915e5dd7070Spatrick *File, /*IsSystem*/false);
916e5dd7070Spatrick else
917e5dd7070Spatrick CI.getDiagnostics().Report(diag::err_module_map_not_found) << Filename;
918e5dd7070Spatrick }
919e5dd7070Spatrick
920*12c85518Srobert // If compiling implementation of a module, load its module map file now.
921*12c85518Srobert (void)CI.getPreprocessor().getCurrentModuleImplementation();
922*12c85518Srobert
923e5dd7070Spatrick // Add a module declaration scope so that modules from -fmodule-map-file
924e5dd7070Spatrick // arguments may shadow modules found implicitly in search paths.
925e5dd7070Spatrick CI.getPreprocessor()
926e5dd7070Spatrick .getHeaderSearchInfo()
927e5dd7070Spatrick .getModuleMap()
928e5dd7070Spatrick .finishModuleDeclarationScope();
929e5dd7070Spatrick
930e5dd7070Spatrick // Create the AST context and consumer unless this is a preprocessor only
931e5dd7070Spatrick // action.
932e5dd7070Spatrick if (!usesPreprocessorOnly()) {
933e5dd7070Spatrick // Parsing a model file should reuse the existing ASTContext.
934e5dd7070Spatrick if (!isModelParsingAction())
935e5dd7070Spatrick CI.createASTContext();
936e5dd7070Spatrick
937e5dd7070Spatrick // For preprocessed files, check if the first line specifies the original
938e5dd7070Spatrick // source file name with a linemarker.
939ec727ea7Spatrick std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
940e5dd7070Spatrick if (Input.isPreprocessed())
941e5dd7070Spatrick ReadOriginalFileName(CI, PresumedInputFile);
942e5dd7070Spatrick
943e5dd7070Spatrick std::unique_ptr<ASTConsumer> Consumer =
944e5dd7070Spatrick CreateWrappedASTConsumer(CI, PresumedInputFile);
945e5dd7070Spatrick if (!Consumer)
946*12c85518Srobert return false;
947e5dd7070Spatrick
948e5dd7070Spatrick // FIXME: should not overwrite ASTMutationListener when parsing model files?
949e5dd7070Spatrick if (!isModelParsingAction())
950e5dd7070Spatrick CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
951e5dd7070Spatrick
952e5dd7070Spatrick if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
953e5dd7070Spatrick // Convert headers to PCH and chain them.
954e5dd7070Spatrick IntrusiveRefCntPtr<ExternalSemaSource> source, FinalReader;
955e5dd7070Spatrick source = createChainedIncludesSource(CI, FinalReader);
956e5dd7070Spatrick if (!source)
957*12c85518Srobert return false;
958ec727ea7Spatrick CI.setASTReader(static_cast<ASTReader *>(FinalReader.get()));
959e5dd7070Spatrick CI.getASTContext().setExternalSource(source);
960e5dd7070Spatrick } else if (CI.getLangOpts().Modules ||
961e5dd7070Spatrick !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
962e5dd7070Spatrick // Use PCM or PCH.
963e5dd7070Spatrick assert(hasPCHSupport() && "This action does not have PCH support!");
964e5dd7070Spatrick ASTDeserializationListener *DeserialListener =
965e5dd7070Spatrick Consumer->GetASTDeserializationListener();
966e5dd7070Spatrick bool DeleteDeserialListener = false;
967e5dd7070Spatrick if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
968e5dd7070Spatrick DeserialListener = new DeserializedDeclsDumper(DeserialListener,
969e5dd7070Spatrick DeleteDeserialListener);
970e5dd7070Spatrick DeleteDeserialListener = true;
971e5dd7070Spatrick }
972e5dd7070Spatrick if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
973e5dd7070Spatrick DeserialListener = new DeserializedDeclsChecker(
974e5dd7070Spatrick CI.getASTContext(),
975e5dd7070Spatrick CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
976e5dd7070Spatrick DeserialListener, DeleteDeserialListener);
977e5dd7070Spatrick DeleteDeserialListener = true;
978e5dd7070Spatrick }
979e5dd7070Spatrick if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
980e5dd7070Spatrick CI.createPCHExternalASTSource(
981e5dd7070Spatrick CI.getPreprocessorOpts().ImplicitPCHInclude,
982a9ac8606Spatrick CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
983a9ac8606Spatrick CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
984a9ac8606Spatrick DeserialListener, DeleteDeserialListener);
985e5dd7070Spatrick if (!CI.getASTContext().getExternalSource())
986*12c85518Srobert return false;
987e5dd7070Spatrick }
988ec727ea7Spatrick // If modules are enabled, create the AST reader before creating
989e5dd7070Spatrick // any builtins, so that all declarations know that they might be
990e5dd7070Spatrick // extended by an external source.
991e5dd7070Spatrick if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
992e5dd7070Spatrick !CI.getASTContext().getExternalSource()) {
993e5dd7070Spatrick CI.createASTReader();
994e5dd7070Spatrick CI.getASTReader()->setDeserializationListener(DeserialListener,
995e5dd7070Spatrick DeleteDeserialListener);
996e5dd7070Spatrick }
997e5dd7070Spatrick }
998e5dd7070Spatrick
999e5dd7070Spatrick CI.setASTConsumer(std::move(Consumer));
1000e5dd7070Spatrick if (!CI.hasASTConsumer())
1001*12c85518Srobert return false;
1002e5dd7070Spatrick }
1003e5dd7070Spatrick
1004e5dd7070Spatrick // Initialize built-in info as long as we aren't using an external AST
1005e5dd7070Spatrick // source.
1006e5dd7070Spatrick if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
1007e5dd7070Spatrick !CI.getASTContext().getExternalSource()) {
1008e5dd7070Spatrick Preprocessor &PP = CI.getPreprocessor();
1009e5dd7070Spatrick PP.getBuiltinInfo().initializeBuiltins(PP.getIdentifierTable(),
1010e5dd7070Spatrick PP.getLangOpts());
1011e5dd7070Spatrick } else {
1012e5dd7070Spatrick // FIXME: If this is a problem, recover from it by creating a multiplex
1013e5dd7070Spatrick // source.
1014e5dd7070Spatrick assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
1015e5dd7070Spatrick "modules enabled but created an external source that "
1016e5dd7070Spatrick "doesn't support modules");
1017e5dd7070Spatrick }
1018e5dd7070Spatrick
1019e5dd7070Spatrick // If we were asked to load any module files, do so now.
1020e5dd7070Spatrick for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles)
1021e5dd7070Spatrick if (!CI.loadModuleFile(ModuleFile))
1022*12c85518Srobert return false;
1023e5dd7070Spatrick
1024e5dd7070Spatrick // If there is a layout overrides file, attach an external AST source that
1025e5dd7070Spatrick // provides the layouts from that file.
1026e5dd7070Spatrick if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
1027e5dd7070Spatrick CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
1028e5dd7070Spatrick IntrusiveRefCntPtr<ExternalASTSource>
1029e5dd7070Spatrick Override(new LayoutOverrideSource(
1030e5dd7070Spatrick CI.getFrontendOpts().OverrideRecordLayoutsFile));
1031e5dd7070Spatrick CI.getASTContext().setExternalSource(Override);
1032e5dd7070Spatrick }
1033e5dd7070Spatrick
1034*12c85518Srobert // Setup HLSL External Sema Source
1035*12c85518Srobert if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
1036*12c85518Srobert IntrusiveRefCntPtr<ExternalSemaSource> HLSLSema(
1037*12c85518Srobert new HLSLExternalSemaSource());
1038*12c85518Srobert if (auto *SemaSource = dyn_cast_if_present<ExternalSemaSource>(
1039*12c85518Srobert CI.getASTContext().getExternalSource())) {
1040*12c85518Srobert IntrusiveRefCntPtr<ExternalSemaSource> MultiSema(
1041*12c85518Srobert new MultiplexExternalSemaSource(SemaSource, HLSLSema.get()));
1042*12c85518Srobert CI.getASTContext().setExternalSource(MultiSema);
1043*12c85518Srobert } else
1044*12c85518Srobert CI.getASTContext().setExternalSource(HLSLSema);
1045*12c85518Srobert }
1046e5dd7070Spatrick
1047*12c85518Srobert FailureCleanup.release();
1048*12c85518Srobert return true;
1049e5dd7070Spatrick }
1050e5dd7070Spatrick
Execute()1051e5dd7070Spatrick llvm::Error FrontendAction::Execute() {
1052e5dd7070Spatrick CompilerInstance &CI = getCompilerInstance();
1053e5dd7070Spatrick
1054e5dd7070Spatrick if (CI.hasFrontendTimer()) {
1055e5dd7070Spatrick llvm::TimeRegion Timer(CI.getFrontendTimer());
1056e5dd7070Spatrick ExecuteAction();
1057e5dd7070Spatrick }
1058e5dd7070Spatrick else ExecuteAction();
1059e5dd7070Spatrick
1060e5dd7070Spatrick // If we are supposed to rebuild the global module index, do so now unless
1061e5dd7070Spatrick // there were any module-build failures.
1062e5dd7070Spatrick if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
1063e5dd7070Spatrick CI.hasPreprocessor()) {
1064e5dd7070Spatrick StringRef Cache =
1065e5dd7070Spatrick CI.getPreprocessor().getHeaderSearchInfo().getModuleCachePath();
1066e5dd7070Spatrick if (!Cache.empty()) {
1067e5dd7070Spatrick if (llvm::Error Err = GlobalModuleIndex::writeIndex(
1068e5dd7070Spatrick CI.getFileManager(), CI.getPCHContainerReader(), Cache)) {
1069e5dd7070Spatrick // FIXME this drops the error on the floor, but
1070e5dd7070Spatrick // Index/pch-from-libclang.c seems to rely on dropping at least some of
1071e5dd7070Spatrick // the error conditions!
1072e5dd7070Spatrick consumeError(std::move(Err));
1073e5dd7070Spatrick }
1074e5dd7070Spatrick }
1075e5dd7070Spatrick }
1076e5dd7070Spatrick
1077e5dd7070Spatrick return llvm::Error::success();
1078e5dd7070Spatrick }
1079e5dd7070Spatrick
EndSourceFile()1080e5dd7070Spatrick void FrontendAction::EndSourceFile() {
1081e5dd7070Spatrick CompilerInstance &CI = getCompilerInstance();
1082e5dd7070Spatrick
1083e5dd7070Spatrick // Inform the diagnostic client we are done with this source file.
1084e5dd7070Spatrick CI.getDiagnosticClient().EndSourceFile();
1085e5dd7070Spatrick
1086e5dd7070Spatrick // Inform the preprocessor we are done.
1087e5dd7070Spatrick if (CI.hasPreprocessor())
1088e5dd7070Spatrick CI.getPreprocessor().EndSourceFile();
1089e5dd7070Spatrick
1090e5dd7070Spatrick // Finalize the action.
1091e5dd7070Spatrick EndSourceFileAction();
1092e5dd7070Spatrick
1093e5dd7070Spatrick // Sema references the ast consumer, so reset sema first.
1094e5dd7070Spatrick //
1095e5dd7070Spatrick // FIXME: There is more per-file stuff we could just drop here?
1096e5dd7070Spatrick bool DisableFree = CI.getFrontendOpts().DisableFree;
1097e5dd7070Spatrick if (DisableFree) {
1098e5dd7070Spatrick CI.resetAndLeakSema();
1099e5dd7070Spatrick CI.resetAndLeakASTContext();
1100e5dd7070Spatrick llvm::BuryPointer(CI.takeASTConsumer().get());
1101e5dd7070Spatrick } else {
1102e5dd7070Spatrick CI.setSema(nullptr);
1103e5dd7070Spatrick CI.setASTContext(nullptr);
1104e5dd7070Spatrick CI.setASTConsumer(nullptr);
1105e5dd7070Spatrick }
1106e5dd7070Spatrick
1107e5dd7070Spatrick if (CI.getFrontendOpts().ShowStats) {
1108*12c85518Srobert llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
1109e5dd7070Spatrick CI.getPreprocessor().PrintStats();
1110e5dd7070Spatrick CI.getPreprocessor().getIdentifierTable().PrintStats();
1111e5dd7070Spatrick CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1112e5dd7070Spatrick CI.getSourceManager().PrintStats();
1113e5dd7070Spatrick llvm::errs() << "\n";
1114e5dd7070Spatrick }
1115e5dd7070Spatrick
1116e5dd7070Spatrick // Cleanup the output streams, and erase the output files if instructed by the
1117e5dd7070Spatrick // FrontendAction.
1118e5dd7070Spatrick CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
1119e5dd7070Spatrick
1120e5dd7070Spatrick if (isCurrentFileAST()) {
1121e5dd7070Spatrick if (DisableFree) {
1122e5dd7070Spatrick CI.resetAndLeakPreprocessor();
1123e5dd7070Spatrick CI.resetAndLeakSourceManager();
1124e5dd7070Spatrick CI.resetAndLeakFileManager();
1125e5dd7070Spatrick llvm::BuryPointer(std::move(CurrentASTUnit));
1126e5dd7070Spatrick } else {
1127e5dd7070Spatrick CI.setPreprocessor(nullptr);
1128e5dd7070Spatrick CI.setSourceManager(nullptr);
1129e5dd7070Spatrick CI.setFileManager(nullptr);
1130e5dd7070Spatrick }
1131e5dd7070Spatrick }
1132e5dd7070Spatrick
1133e5dd7070Spatrick setCompilerInstance(nullptr);
1134e5dd7070Spatrick setCurrentInput(FrontendInputFile());
1135e5dd7070Spatrick CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1136e5dd7070Spatrick }
1137e5dd7070Spatrick
shouldEraseOutputFiles()1138e5dd7070Spatrick bool FrontendAction::shouldEraseOutputFiles() {
1139e5dd7070Spatrick return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1140e5dd7070Spatrick }
1141e5dd7070Spatrick
1142e5dd7070Spatrick //===----------------------------------------------------------------------===//
1143e5dd7070Spatrick // Utility Actions
1144e5dd7070Spatrick //===----------------------------------------------------------------------===//
1145e5dd7070Spatrick
ExecuteAction()1146e5dd7070Spatrick void ASTFrontendAction::ExecuteAction() {
1147e5dd7070Spatrick CompilerInstance &CI = getCompilerInstance();
1148e5dd7070Spatrick if (!CI.hasPreprocessor())
1149e5dd7070Spatrick return;
1150e5dd7070Spatrick
1151e5dd7070Spatrick // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1152e5dd7070Spatrick // here so the source manager would be initialized.
1153e5dd7070Spatrick if (hasCodeCompletionSupport() &&
1154e5dd7070Spatrick !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
1155e5dd7070Spatrick CI.createCodeCompletionConsumer();
1156e5dd7070Spatrick
1157e5dd7070Spatrick // Use a code completion consumer?
1158e5dd7070Spatrick CodeCompleteConsumer *CompletionConsumer = nullptr;
1159e5dd7070Spatrick if (CI.hasCodeCompletionConsumer())
1160e5dd7070Spatrick CompletionConsumer = &CI.getCodeCompletionConsumer();
1161e5dd7070Spatrick
1162e5dd7070Spatrick if (!CI.hasSema())
1163e5dd7070Spatrick CI.createSema(getTranslationUnitKind(), CompletionConsumer);
1164e5dd7070Spatrick
1165e5dd7070Spatrick ParseAST(CI.getSema(), CI.getFrontendOpts().ShowStats,
1166e5dd7070Spatrick CI.getFrontendOpts().SkipFunctionBodies);
1167e5dd7070Spatrick }
1168e5dd7070Spatrick
anchor()1169e5dd7070Spatrick void PluginASTAction::anchor() { }
1170e5dd7070Spatrick
1171e5dd7070Spatrick std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)1172e5dd7070Spatrick PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1173e5dd7070Spatrick StringRef InFile) {
1174e5dd7070Spatrick llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1175e5dd7070Spatrick }
1176e5dd7070Spatrick
PrepareToExecuteAction(CompilerInstance & CI)1177e5dd7070Spatrick bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1178e5dd7070Spatrick return WrappedAction->PrepareToExecuteAction(CI);
1179e5dd7070Spatrick }
1180e5dd7070Spatrick std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)1181e5dd7070Spatrick WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1182e5dd7070Spatrick StringRef InFile) {
1183e5dd7070Spatrick return WrappedAction->CreateASTConsumer(CI, InFile);
1184e5dd7070Spatrick }
BeginInvocation(CompilerInstance & CI)1185e5dd7070Spatrick bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1186e5dd7070Spatrick return WrappedAction->BeginInvocation(CI);
1187e5dd7070Spatrick }
BeginSourceFileAction(CompilerInstance & CI)1188e5dd7070Spatrick bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1189e5dd7070Spatrick WrappedAction->setCurrentInput(getCurrentInput());
1190e5dd7070Spatrick WrappedAction->setCompilerInstance(&CI);
1191e5dd7070Spatrick auto Ret = WrappedAction->BeginSourceFileAction(CI);
1192e5dd7070Spatrick // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1193e5dd7070Spatrick setCurrentInput(WrappedAction->getCurrentInput());
1194e5dd7070Spatrick return Ret;
1195e5dd7070Spatrick }
ExecuteAction()1196e5dd7070Spatrick void WrapperFrontendAction::ExecuteAction() {
1197e5dd7070Spatrick WrappedAction->ExecuteAction();
1198e5dd7070Spatrick }
EndSourceFile()1199a9ac8606Spatrick void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
EndSourceFileAction()1200e5dd7070Spatrick void WrapperFrontendAction::EndSourceFileAction() {
1201e5dd7070Spatrick WrappedAction->EndSourceFileAction();
1202e5dd7070Spatrick }
shouldEraseOutputFiles()1203ec727ea7Spatrick bool WrapperFrontendAction::shouldEraseOutputFiles() {
1204ec727ea7Spatrick return WrappedAction->shouldEraseOutputFiles();
1205ec727ea7Spatrick }
1206e5dd7070Spatrick
usesPreprocessorOnly() const1207e5dd7070Spatrick bool WrapperFrontendAction::usesPreprocessorOnly() const {
1208e5dd7070Spatrick return WrappedAction->usesPreprocessorOnly();
1209e5dd7070Spatrick }
getTranslationUnitKind()1210e5dd7070Spatrick TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1211e5dd7070Spatrick return WrappedAction->getTranslationUnitKind();
1212e5dd7070Spatrick }
hasPCHSupport() const1213e5dd7070Spatrick bool WrapperFrontendAction::hasPCHSupport() const {
1214e5dd7070Spatrick return WrappedAction->hasPCHSupport();
1215e5dd7070Spatrick }
hasASTFileSupport() const1216e5dd7070Spatrick bool WrapperFrontendAction::hasASTFileSupport() const {
1217e5dd7070Spatrick return WrappedAction->hasASTFileSupport();
1218e5dd7070Spatrick }
hasIRSupport() const1219e5dd7070Spatrick bool WrapperFrontendAction::hasIRSupport() const {
1220e5dd7070Spatrick return WrappedAction->hasIRSupport();
1221e5dd7070Spatrick }
hasCodeCompletionSupport() const1222e5dd7070Spatrick bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1223e5dd7070Spatrick return WrappedAction->hasCodeCompletionSupport();
1224e5dd7070Spatrick }
1225e5dd7070Spatrick
WrapperFrontendAction(std::unique_ptr<FrontendAction> WrappedAction)1226e5dd7070Spatrick WrapperFrontendAction::WrapperFrontendAction(
1227e5dd7070Spatrick std::unique_ptr<FrontendAction> WrappedAction)
1228e5dd7070Spatrick : WrappedAction(std::move(WrappedAction)) {}
1229