1//===--- FrontendAction.cpp -----------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Frontend/FrontendAction.h"
10#include "clang/AST/ASTConsumer.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/AST/Decl.h"
13#include "clang/AST/DeclGroup.h"
14#include "clang/Basic/Builtins.h"
15#include "clang/Basic/DiagnosticFrontend.h"
16#include "clang/Basic/DiagnosticOptions.h"
17#include "clang/Basic/FileEntry.h"
18#include "clang/Basic/LangOptions.h"
19#include "clang/Basic/LangStandard.h"
20#include "clang/Basic/Sarif.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/Stack.h"
24#include "clang/Basic/TokenKinds.h"
25#include "clang/Frontend/ASTUnit.h"
26#include "clang/Frontend/CompilerInstance.h"
27#include "clang/Frontend/FrontendPluginRegistry.h"
28#include "clang/Frontend/LayoutOverrideSource.h"
29#include "clang/Frontend/MultiplexConsumer.h"
30#include "clang/Frontend/SARIFDiagnosticPrinter.h"
31#include "clang/Frontend/Utils.h"
32#include "clang/Lex/HeaderSearch.h"
33#include "clang/Lex/LiteralSupport.h"
34#include "clang/Lex/Preprocessor.h"
35#include "clang/Lex/PreprocessorOptions.h"
36#include "clang/Parse/ParseAST.h"
37#include "clang/Sema/HLSLExternalSemaSource.h"
38#include "clang/Sema/MultiplexExternalSemaSource.h"
39#include "clang/Serialization/ASTDeserializationListener.h"
40#include "clang/Serialization/ASTReader.h"
41#include "clang/Serialization/GlobalModuleIndex.h"
42#include "clang/Support/Compiler.h"
43#include "llvm/ADT/ScopeExit.h"
44#include "llvm/ADT/SmallPtrSet.h"
45#include "llvm/ADT/StringRef.h"
46#include "llvm/Support/BuryPointer.h"
47#include "llvm/Support/ErrorHandling.h"
48#include "llvm/Support/FileSystem.h"
49#include "llvm/Support/Path.h"
50#include "llvm/Support/Timer.h"
51#include "llvm/Support/raw_ostream.h"
52#include <memory>
53#include <system_error>
54using namespace clang;
55
56LLVM_INSTANTIATE_REGISTRY_EX(CLANG_ABI_EXPORT, FrontendPluginRegistry)
57
58namespace {
59
60/// DeserializedDeclsLineRangePrinter dumps ranges of deserialized declarations
61/// to aid debugging and bug minimization. It implements ASTConsumer and
62/// ASTDeserializationListener, so that an object of
63/// DeserializedDeclsLineRangePrinter registers as its own listener. The
64/// ASTDeserializationListener interface provides the DeclRead callback that we
65/// use to collect the deserialized Decls. Note that printing or otherwise
66/// processing them as this point is dangerous, since that could trigger
67/// additional deserialization and crash compilation. Therefore, we process the
68/// collected Decls in HandleTranslationUnit method of ASTConsumer. This is a
69/// safe point, since we know that by this point all the Decls needed by the
70/// compiler frontend have been deserialized. In case our processing causes
71/// further deserialization, DeclRead from the listener might be called again.
72/// However, at that point we don't accept any more Decls for processing.
73class DeserializedDeclsSourceRangePrinter : public ASTConsumer,
74 ASTDeserializationListener {
75public:
76 explicit DeserializedDeclsSourceRangePrinter(
77 SourceManager &SM, std::unique_ptr<llvm::raw_fd_ostream> OS)
78 : ASTDeserializationListener(), SM(SM), OS(std::move(OS)) {}
79
80 ASTDeserializationListener *GetASTDeserializationListener() override {
81 return this;
82 }
83
84 void DeclRead(GlobalDeclID ID, const Decl *D) override {
85 if (!IsCollectingDecls)
86 return;
87 if (!D || isa<TranslationUnitDecl>(Val: D) || isa<LinkageSpecDecl>(Val: D) ||
88 isa<NamespaceDecl>(Val: D) || isa<ExportDecl>(Val: D)) {
89 // These decls cover a lot of nested declarations that might not be used,
90 // reducing the granularity and making the output less useful.
91 return;
92 }
93 if (isa<ParmVarDecl>(Val: D)) {
94 // Parameters are covered by their functions.
95 return;
96 }
97 auto *DC = D->getLexicalDeclContext();
98 if (!DC || !shouldIncludeDeclsIn(DC))
99 return;
100
101 PendingDecls.push_back(x: D);
102 for (; (isa<ExportDecl>(Val: DC) || isa<NamespaceDecl>(Val: DC)) &&
103 ProcessedDeclContexts.insert(Ptr: DC).second;
104 DC = DC->getLexicalParent()) {
105 // Add any interesting decl contexts that we have not seen before.
106 // Note that we filter them out from DeclRead as that would include all
107 // redeclarations of namespaces, potentially those that do not have any
108 // imported declarations.
109 PendingDecls.push_back(x: cast<Decl>(Val: DC));
110 }
111 }
112
113 struct Position {
114 unsigned Line;
115 unsigned Column;
116
117 bool operator<(const Position &other) const {
118 return std::tie(args: Line, args: Column) < std::tie(args: other.Line, args: other.Column);
119 }
120
121 static Position GetBeginSpelling(const SourceManager &SM,
122 const CharSourceRange &R) {
123 SourceLocation Begin = R.getBegin();
124 return {.Line: SM.getSpellingLineNumber(Loc: Begin),
125 .Column: SM.getSpellingColumnNumber(Loc: Begin)};
126 }
127
128 static Position GetEndSpelling(const SourceManager &SM,
129 const CharSourceRange &Range,
130 const LangOptions &LangOpts) {
131 // For token ranges, compute end location for end character of the range.
132 CharSourceRange R = Lexer::getAsCharRange(Range, SM, LangOpts);
133 SourceLocation End = R.getEnd();
134 // Relex the token past the end location of the last token in the source
135 // range. If it's a semicolon, advance the location by one token.
136 Token PossiblySemi;
137 Lexer::getRawToken(Loc: End, Result&: PossiblySemi, SM, LangOpts, IgnoreWhiteSpace: true);
138 if (PossiblySemi.is(K: tok::semi))
139 End = End.getLocWithOffset(Offset: 1);
140 // Column number of the returned end position is exclusive.
141 return {.Line: SM.getSpellingLineNumber(Loc: End), .Column: SM.getSpellingColumnNumber(Loc: End)};
142 }
143 };
144
145 struct RequiredRanges {
146 StringRef Filename;
147 std::vector<std::pair<Position, Position>> FromTo;
148 };
149 void HandleTranslationUnit(ASTContext &Context) override {
150 assert(IsCollectingDecls && "HandleTranslationUnit called twice?");
151 IsCollectingDecls = false;
152
153 // Merge ranges in each of the files.
154 struct FileData {
155 std::vector<std::pair<Position, Position>> FromTo;
156 OptionalFileEntryRef Ref;
157 };
158 llvm::DenseMap<const FileEntry *, FileData> FileToRanges;
159
160 for (const Decl *D : PendingDecls) {
161 for (CharSourceRange R : getRangesToMark(D)) {
162 if (!R.isValid())
163 continue;
164
165 auto *F = SM.getFileEntryForID(FID: SM.getFileID(SpellingLoc: R.getBegin()));
166 if (F != SM.getFileEntryForID(FID: SM.getFileID(SpellingLoc: R.getEnd()))) {
167 // Such cases are rare and difficult to handle.
168 continue;
169 }
170
171 auto &Data = FileToRanges[F];
172 if (!Data.Ref)
173 Data.Ref = SM.getFileEntryRefForID(FID: SM.getFileID(SpellingLoc: R.getBegin()));
174 Data.FromTo.push_back(
175 x: {Position::GetBeginSpelling(SM, R),
176 Position::GetEndSpelling(SM, Range: R, LangOpts: D->getLangOpts())});
177 }
178 }
179
180 // To simplify output, merge consecutive and intersecting ranges.
181 std::vector<RequiredRanges> Result;
182 for (auto &[F, Data] : FileToRanges) {
183 auto &FromTo = Data.FromTo;
184 assert(!FromTo.empty());
185
186 if (!Data.Ref)
187 continue;
188
189 llvm::sort(C&: FromTo);
190
191 std::vector<std::pair<Position, Position>> MergedRanges;
192 MergedRanges.push_back(x: FromTo.front());
193 for (auto It = FromTo.begin() + 1; It < FromTo.end(); ++It) {
194 if (MergedRanges.back().second < It->first) {
195 MergedRanges.push_back(x: *It);
196 continue;
197 }
198 if (MergedRanges.back().second < It->second)
199 MergedRanges.back().second = It->second;
200 }
201 Result.push_back(x: {.Filename: Data.Ref->getName(), .FromTo: std::move(MergedRanges)});
202 }
203 printJson(Result);
204 }
205
206private:
207 std::vector<const Decl *> PendingDecls;
208 llvm::SmallPtrSet<const DeclContext *, 0> ProcessedDeclContexts;
209 bool IsCollectingDecls = true;
210 const SourceManager &SM;
211 std::unique_ptr<llvm::raw_ostream> OS;
212
213 static bool shouldIncludeDeclsIn(const DeclContext *DC) {
214 assert(DC && "DC is null");
215 // We choose to work at namespace level to reduce complexity and the number
216 // of cases we care about.
217 // We still need to carefully handle composite declarations like
218 // `ExportDecl`.
219 for (; DC; DC = DC->getLexicalParent()) {
220 if (DC->isFileContext())
221 return true;
222 if (isa<ExportDecl>(Val: DC))
223 continue; // Depends on the parent.
224 return false;
225 }
226 llvm_unreachable("DeclContext chain must end with a translation unit");
227 }
228
229 llvm::SmallVector<CharSourceRange, 2> getRangesToMark(const Decl *D) {
230 if (auto *ED = dyn_cast<ExportDecl>(Val: D)) {
231 if (!ED->hasBraces())
232 return {SM.getExpansionRange(Loc: ED->getExportLoc())};
233
234 return {SM.getExpansionRange(Range: SourceRange(
235 ED->getExportLoc(),
236 lexForLBrace(TokenBeforeLBrace: ED->getExportLoc(), LangOpts: D->getLangOpts()))),
237 SM.getExpansionRange(Loc: ED->getRBraceLoc())};
238 }
239
240 auto *NS = dyn_cast<NamespaceDecl>(Val: D);
241 if (!NS)
242 return {SM.getExpansionRange(Range: D->getSourceRange())};
243
244 SourceLocation LBraceLoc;
245 if (NS->isAnonymousNamespace()) {
246 LBraceLoc = NS->getLocation();
247 } else {
248 // Start with the location of the identifier.
249 SourceLocation TokenBeforeLBrace = NS->getLocation();
250 if (NS->hasAttrs()) {
251 for (auto *A : NS->getAttrs()) {
252 // But attributes may go after it.
253 if (SM.isBeforeInTranslationUnit(LHS: TokenBeforeLBrace,
254 RHS: A->getRange().getEnd())) {
255 // Give up, the attributes are often coming from macros and we
256 // cannot skip them reliably.
257 return {};
258 }
259 }
260 }
261 LBraceLoc = lexForLBrace(TokenBeforeLBrace, LangOpts: D->getLangOpts());
262 }
263 return {SM.getExpansionRange(Range: SourceRange(NS->getBeginLoc(), LBraceLoc)),
264 SM.getExpansionRange(Loc: NS->getRBraceLoc())};
265 }
266
267 void printJson(llvm::ArrayRef<RequiredRanges> Result) {
268 *OS << "{\n";
269 *OS << R"( "required_ranges": [)" << "\n";
270 for (size_t I = 0; I < Result.size(); ++I) {
271 auto &F = Result[I].Filename;
272 auto &MergedRanges = Result[I].FromTo;
273 *OS << R"( {)" << "\n";
274 *OS << R"( "file": ")" << F << "\"," << "\n";
275 *OS << R"( "range": [)" << "\n";
276 for (size_t J = 0; J < MergedRanges.size(); ++J) {
277 auto &From = MergedRanges[J].first;
278 auto &To = MergedRanges[J].second;
279 *OS << R"( {)" << "\n";
280 *OS << R"( "from": {)" << "\n";
281 *OS << R"( "line": )" << From.Line << ",\n";
282 *OS << R"( "column": )" << From.Column << "\n"
283 << R"( },)" << "\n";
284 *OS << R"( "to": {)" << "\n";
285 *OS << R"( "line": )" << To.Line << ",\n";
286 *OS << R"( "column": )" << To.Column << "\n"
287 << R"( })" << "\n";
288 *OS << R"( })";
289 if (J < MergedRanges.size() - 1) {
290 *OS << ",";
291 }
292 *OS << "\n";
293 }
294 *OS << " ]" << "\n" << " }";
295 if (I < Result.size() - 1)
296 *OS << ",";
297 *OS << "\n";
298 }
299 *OS << " ]\n";
300 *OS << "}\n";
301
302 OS->flush();
303 }
304
305 SourceLocation lexForLBrace(SourceLocation TokenBeforeLBrace,
306 const LangOptions &LangOpts) {
307 // Now skip one token, the next should be the lbrace.
308 Token Tok;
309 if (Lexer::getRawToken(Loc: TokenBeforeLBrace, Result&: Tok, SM, LangOpts, IgnoreWhiteSpace: true) ||
310 Lexer::getRawToken(Loc: Tok.getEndLoc(), Result&: Tok, SM, LangOpts, IgnoreWhiteSpace: true) ||
311 Tok.getKind() != tok::l_brace) {
312 // On error or if we did not find the token we expected, avoid marking
313 // everything inside the namespace as used.
314 return SourceLocation();
315 }
316 return Tok.getLocation();
317 }
318};
319
320/// Dumps deserialized declarations.
321class DeserializedDeclsDumper : public DelegatingDeserializationListener {
322public:
323 explicit DeserializedDeclsDumper(ASTDeserializationListener *Previous,
324 bool DeletePrevious)
325 : DelegatingDeserializationListener(Previous, DeletePrevious) {}
326
327 void DeclRead(GlobalDeclID ID, const Decl *D) override {
328 llvm::outs() << "PCH DECL: " << D->getDeclKindName();
329 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D)) {
330 llvm::outs() << " - ";
331 ND->printQualifiedName(OS&: llvm::outs());
332 }
333 llvm::outs() << "\n";
334
335 DelegatingDeserializationListener::DeclRead(ID, D);
336 }
337};
338
339/// Checks deserialized declarations and emits error if a name
340/// matches one given in command-line using -error-on-deserialized-decl.
341class DeserializedDeclsChecker : public DelegatingDeserializationListener {
342 ASTContext &Ctx;
343 std::set<std::string> NamesToCheck;
344
345public:
346 DeserializedDeclsChecker(ASTContext &Ctx,
347 const std::set<std::string> &NamesToCheck,
348 ASTDeserializationListener *Previous,
349 bool DeletePrevious)
350 : DelegatingDeserializationListener(Previous, DeletePrevious), Ctx(Ctx),
351 NamesToCheck(NamesToCheck) {}
352
353 void DeclRead(GlobalDeclID ID, const Decl *D) override {
354 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D))
355 if (NamesToCheck.find(x: ND->getNameAsString()) != NamesToCheck.end()) {
356 unsigned DiagID
357 = Ctx.getDiagnostics().getCustomDiagID(L: DiagnosticsEngine::Error,
358 FormatString: "%0 was deserialized");
359 Ctx.getDiagnostics().Report(Loc: Ctx.getFullLoc(Loc: D->getLocation()), DiagID)
360 << ND;
361 }
362
363 DelegatingDeserializationListener::DeclRead(ID, D);
364 }
365};
366
367} // end anonymous namespace
368
369FrontendAction::FrontendAction() : Instance(nullptr) {}
370
371FrontendAction::~FrontendAction() {}
372
373void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
374 std::unique_ptr<ASTUnit> AST) {
375 this->CurrentInput = CurrentInput;
376 CurrentASTUnit = std::move(AST);
377}
378
379Module *FrontendAction::getCurrentModule() const {
380 CompilerInstance &CI = getCompilerInstance();
381 return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
382 ModuleName: CI.getLangOpts().CurrentModule, ImportLoc: SourceLocation(), /*AllowSearch*/false);
383}
384
385std::unique_ptr<ASTConsumer>
386FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
387 StringRef InFile) {
388 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
389 if (!Consumer)
390 return nullptr;
391
392 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
393 llvm::StringRef DumpDeserializedDeclarationRangesPath =
394 CI.getFrontendOpts().DumpMinimizationHintsPath;
395 if (!DumpDeserializedDeclarationRangesPath.empty()) {
396 std::error_code ErrorCode;
397 auto FileStream = std::make_unique<llvm::raw_fd_ostream>(
398 args&: DumpDeserializedDeclarationRangesPath, args&: ErrorCode,
399 args: llvm::sys::fs::OF_TextWithCRLF);
400 if (!ErrorCode) {
401 Consumers.push_back(x: std::make_unique<DeserializedDeclsSourceRangePrinter>(
402 args&: CI.getSourceManager(), args: std::move(FileStream)));
403 } else {
404 llvm::errs() << "Failed to create output file for "
405 "-dump-minimization-hints flag, file path: "
406 << DumpDeserializedDeclarationRangesPath
407 << ", error: " << ErrorCode.message() << "\n";
408 }
409 }
410
411 // Validate -add-plugin args.
412 bool FoundAllPlugins = true;
413 for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
414 bool Found = false;
415 for (const FrontendPluginRegistry::entry &Plugin :
416 FrontendPluginRegistry::entries()) {
417 if (Plugin.getName() == Arg)
418 Found = true;
419 }
420 if (!Found) {
421 CI.getDiagnostics().Report(DiagID: diag::err_fe_invalid_plugin_name) << Arg;
422 FoundAllPlugins = false;
423 }
424 }
425 if (!FoundAllPlugins)
426 return nullptr;
427
428 // If this is a code completion run, avoid invoking the plugin consumers
429 if (CI.hasCodeCompletionConsumer())
430 return Consumer;
431
432 // Collect the list of plugins that go before the main action (in Consumers)
433 // or after it (in AfterConsumers)
434 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
435 for (const FrontendPluginRegistry::entry &Plugin :
436 FrontendPluginRegistry::entries()) {
437 std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
438 PluginASTAction::ActionType ActionType = P->getActionType();
439 if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
440 ActionType == PluginASTAction::CmdlineBeforeMainAction) {
441 // This is O(|plugins| * |add_plugins|), but since both numbers are
442 // way below 50 in practice, that's ok.
443 if (llvm::is_contained(Range&: CI.getFrontendOpts().AddPluginActions,
444 Element: Plugin.getName())) {
445 if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
446 ActionType = PluginASTAction::AddBeforeMainAction;
447 else
448 ActionType = PluginASTAction::AddAfterMainAction;
449 }
450 }
451 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
452 ActionType == PluginASTAction::AddAfterMainAction) &&
453 P->ParseArgs(
454 CI,
455 arg: CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
456 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
457 if (ActionType == PluginASTAction::AddBeforeMainAction) {
458 Consumers.push_back(x: std::move(PluginConsumer));
459 } else {
460 AfterConsumers.push_back(x: std::move(PluginConsumer));
461 }
462 }
463 }
464
465 // Add to Consumers the main consumer, then all the plugins that go after it
466 Consumers.push_back(x: std::move(Consumer));
467 if (!AfterConsumers.empty()) {
468 // If we have plugins after the main consumer, which may be the codegen
469 // action, they likely will need the ASTContext, so don't clear it in the
470 // codegen action.
471 CI.getCodeGenOpts().ClearASTBeforeBackend = false;
472 for (auto &C : AfterConsumers)
473 Consumers.push_back(x: std::move(C));
474 }
475
476 assert(Consumers.size() >= 1 && "should have added the main consumer");
477 if (Consumers.size() == 1)
478 return std::move(Consumers.front());
479 return std::make_unique<MultiplexConsumer>(args: std::move(Consumers));
480}
481
482/// For preprocessed files, if the first line is the linemarker and specifies
483/// the original source file name, use that name as the input file name.
484/// Returns the location of the first token after the line marker directive.
485///
486/// \param CI The compiler instance.
487/// \param InputFile Populated with the filename from the line marker.
488/// \param IsModuleMap If \c true, add a line note corresponding to this line
489/// directive. (We need to do this because the directive will not be
490/// visited by the preprocessor.)
491static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
492 std::string &InputFile,
493 bool IsModuleMap = false) {
494 auto &SourceMgr = CI.getSourceManager();
495 auto MainFileID = SourceMgr.getMainFileID();
496
497 auto MainFileBuf = SourceMgr.getBufferOrNone(FID: MainFileID);
498 if (!MainFileBuf)
499 return SourceLocation();
500
501 auto RawLexer = std::make_unique<Lexer>(args&: MainFileID, args&: *MainFileBuf, args&: SourceMgr,
502 args&: CI.getLangOpts());
503
504 // If the first line has the syntax of
505 //
506 // # NUM "FILENAME"
507 //
508 // we use FILENAME as the input file name.
509 Token T;
510 if (RawLexer->LexFromRawLexer(Result&: T) || T.getKind() != tok::hash)
511 return SourceLocation();
512 if (RawLexer->LexFromRawLexer(Result&: T) || T.isAtStartOfLine() ||
513 T.getKind() != tok::numeric_constant)
514 return SourceLocation();
515
516 unsigned LineNo;
517 SourceLocation LineNoLoc = T.getLocation();
518 if (IsModuleMap) {
519 llvm::SmallString<16> Buffer;
520 if (Lexer::getSpelling(loc: LineNoLoc, buffer&: Buffer, SM: SourceMgr, options: CI.getLangOpts())
521 .getAsInteger(Radix: 10, Result&: LineNo))
522 return SourceLocation();
523 }
524
525 RawLexer->LexFromRawLexer(Result&: T);
526 if (T.isAtStartOfLine() || T.getKind() != tok::string_literal)
527 return SourceLocation();
528
529 StringLiteralParser Literal(T, CI.getPreprocessor(),
530 StringLiteralEvalMethod::Unevaluated);
531 if (Literal.hadError)
532 return SourceLocation();
533 RawLexer->LexFromRawLexer(Result&: T);
534 if (T.isNot(K: tok::eof) && !T.isAtStartOfLine())
535 return SourceLocation();
536 InputFile = Literal.GetString().str();
537
538 if (IsModuleMap)
539 CI.getSourceManager().AddLineNote(
540 Loc: LineNoLoc, LineNo, FilenameID: SourceMgr.getLineTableFilenameID(Str: InputFile), IsFileEntry: false,
541 IsFileExit: false, FileKind: SrcMgr::C_User_ModuleMap);
542
543 return T.getLocation();
544}
545
546static SmallVectorImpl<char> &
547operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
548 Includes.append(in_start: RHS.begin(), in_end: RHS.end());
549 return Includes;
550}
551
552static void addHeaderInclude(StringRef HeaderName,
553 SmallVectorImpl<char> &Includes,
554 const LangOptions &LangOpts,
555 bool IsExternC) {
556 if (IsExternC && LangOpts.CPlusPlus)
557 Includes += "extern \"C\" {\n";
558 if (LangOpts.ObjC)
559 Includes += "#import \"";
560 else
561 Includes += "#include \"";
562
563 Includes += HeaderName;
564
565 Includes += "\"\n";
566 if (IsExternC && LangOpts.CPlusPlus)
567 Includes += "}\n";
568}
569
570/// Collect the set of header includes needed to construct the given
571/// module and update the TopHeaders file set of the module.
572///
573/// \param Module The module we're collecting includes from.
574///
575/// \param Includes Will be augmented with the set of \#includes or \#imports
576/// needed to load all of the named headers.
577static std::error_code collectModuleHeaderIncludes(
578 const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
579 ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
580 // Don't collect any headers for unavailable modules.
581 if (!Module->isAvailable())
582 return std::error_code();
583
584 // Resolve all lazy header directives to header files.
585 ModMap.resolveHeaderDirectives(Mod: Module, /*File=*/std::nullopt);
586
587 // If any headers are missing, we can't build this module. In most cases,
588 // diagnostics for this should have already been produced; we only get here
589 // if explicit stat information was provided.
590 // FIXME: If the name resolves to a file with different stat information,
591 // produce a better diagnostic.
592 if (!Module->MissingHeaders.empty()) {
593 auto &MissingHeader = Module->MissingHeaders.front();
594 Diag.Report(Loc: MissingHeader.FileNameLoc, DiagID: diag::err_module_header_missing)
595 << MissingHeader.IsUmbrella << MissingHeader.FileName;
596 return std::error_code();
597 }
598
599 // Add includes for each of these headers.
600 for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
601 for (const Module::Header &H : Module->getHeaders(HK)) {
602 Module->addTopHeader(File: H.Entry);
603 // Use the path as specified in the module map file. We'll look for this
604 // file relative to the module build directory (the directory containing
605 // the module map file) so this will find the same file that we found
606 // while parsing the module map.
607 addHeaderInclude(HeaderName: H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
608 IsExternC: Module->IsExternC);
609 }
610 }
611 // Note that Module->PrivateHeaders will not be a TopHeader.
612
613 if (std::optional<Module::Header> UmbrellaHeader =
614 Module->getUmbrellaHeaderAsWritten()) {
615 Module->addTopHeader(File: UmbrellaHeader->Entry);
616 if (Module->Parent)
617 // Include the umbrella header for submodules.
618 addHeaderInclude(HeaderName: UmbrellaHeader->PathRelativeToRootModuleDirectory,
619 Includes, LangOpts, IsExternC: Module->IsExternC);
620 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
621 Module->getUmbrellaDirAsWritten()) {
622 // Add all of the headers we find in this subdirectory.
623 std::error_code EC;
624 SmallString<128> DirNative;
625 llvm::sys::path::native(path: UmbrellaDir->Entry.getName(), result&: DirNative);
626
627 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
628 SmallVector<std::pair<std::string, std::string>, 8> HeaderPaths;
629 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
630 Dir != End && !EC; Dir.increment(EC)) {
631 // Check whether this entry has an extension typically associated with
632 // headers.
633 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(path: Dir->path()))
634 .Cases(CaseStrings: {".h", ".H", ".hh", ".hpp"}, Value: true)
635 .Default(Value: false))
636 continue;
637
638 // Compute the relative path from the directory to this file.
639 SmallVector<StringRef, 16> Components;
640 auto PathIt = llvm::sys::path::rbegin(path: Dir->path());
641 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
642 Components.push_back(Elt: *PathIt);
643 SmallString<128> RelativeHeader(
644 UmbrellaDir->PathRelativeToRootModuleDirectory);
645 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
646 ++It)
647 llvm::sys::path::append(path&: RelativeHeader, a: *It);
648
649 HeaderPaths.push_back(
650 Elt: std::make_pair(x: Dir->path().str(), y: RelativeHeader.c_str()));
651 }
652
653 if (EC)
654 return EC;
655
656 // Sort header paths and make the header inclusion order deterministic
657 // across different OSs and filesystems. As the header search table
658 // serialization order depends on the file reference UID, we need to create
659 // file references in deterministic order too.
660 llvm::sort(C&: HeaderPaths, Comp: llvm::less_first());
661 for (auto &[Path, RelPath] : HeaderPaths) {
662 auto Header = FileMgr.getOptionalFileRef(Filename: Path);
663 // FIXME: This shouldn't happen unless there is a file system race. Is
664 // that worth diagnosing?
665 if (!Header)
666 continue;
667
668 // If this header is marked 'unavailable' in this module, don't include
669 // it.
670 if (ModMap.isHeaderUnavailableInModule(Header: *Header, RequestingModule: Module))
671 continue;
672
673 // Include this header as part of the umbrella directory.
674 Module->addTopHeader(File: *Header);
675 addHeaderInclude(HeaderName: RelPath, Includes, LangOpts, IsExternC: Module->IsExternC);
676 }
677 }
678
679 // Recurse into submodules.
680 for (clang::Module *Submodule : Module->submodules())
681 if (std::error_code Err = collectModuleHeaderIncludes(
682 LangOpts, FileMgr, Diag, ModMap, Module: Submodule, Includes))
683 return Err;
684
685 return std::error_code();
686}
687
688static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
689 bool IsPreprocessed,
690 std::string &PresumedModuleMapFile,
691 unsigned &Offset) {
692 auto &SrcMgr = CI.getSourceManager();
693 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
694
695 // Map the current input to a file.
696 FileID ModuleMapID = SrcMgr.getMainFileID();
697 OptionalFileEntryRef ModuleMap = SrcMgr.getFileEntryRefForID(FID: ModuleMapID);
698 assert(ModuleMap && "MainFileID without FileEntry");
699
700 // If the module map is preprocessed, handle the initial line marker;
701 // line directives are not part of the module map syntax in general.
702 Offset = 0;
703 if (IsPreprocessed) {
704 SourceLocation EndOfLineMarker =
705 ReadOriginalFileName(CI, InputFile&: PresumedModuleMapFile, /*IsModuleMap*/ true);
706 if (EndOfLineMarker.isValid())
707 Offset = CI.getSourceManager().getDecomposedLoc(Loc: EndOfLineMarker).second;
708 }
709
710 // Load the module map file.
711 if (HS.parseAndLoadModuleMapFile(File: *ModuleMap, IsSystem,
712 /*ImplicitlyDiscovered=*/false, ID: ModuleMapID,
713 Offset: &Offset, OriginalModuleMapFile: PresumedModuleMapFile))
714 return true;
715
716 if (SrcMgr.getBufferOrFake(FID: ModuleMapID).getBufferSize() == Offset)
717 Offset = 0;
718
719 // Infer framework module if possible.
720 if (HS.getModuleMap().canInferFrameworkModule(Dir: ModuleMap->getDir())) {
721 SmallString<128> InferredFrameworkPath = ModuleMap->getDir().getName();
722 llvm::sys::path::append(path&: InferredFrameworkPath,
723 a: CI.getLangOpts().ModuleName + ".framework");
724 if (auto Dir =
725 CI.getFileManager().getOptionalDirectoryRef(DirName: InferredFrameworkPath))
726 (void)HS.getModuleMap().inferFrameworkModule(FrameworkDir: *Dir, IsSystem, Parent: nullptr);
727 }
728
729 return false;
730}
731
732static Module *prepareToBuildModule(CompilerInstance &CI,
733 StringRef ModuleMapFilename) {
734 if (CI.getLangOpts().CurrentModule.empty()) {
735 CI.getDiagnostics().Report(DiagID: diag::err_missing_module_name);
736
737 // FIXME: Eventually, we could consider asking whether there was just
738 // a single module described in the module map, and use that as a
739 // default. Then it would be fairly trivial to just "compile" a module
740 // map with a single module (the common case).
741 return nullptr;
742 }
743
744 // Dig out the module definition.
745 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
746 Module *M = HS.lookupModule(ModuleName: CI.getLangOpts().CurrentModule, ImportLoc: SourceLocation(),
747 /*AllowSearch=*/true);
748 if (!M) {
749 CI.getDiagnostics().Report(DiagID: diag::err_missing_module)
750 << CI.getLangOpts().CurrentModule << ModuleMapFilename;
751
752 return nullptr;
753 }
754
755 // Check whether we can build this module at all.
756 if (Preprocessor::checkModuleIsAvailable(LangOpts: CI.getLangOpts(), TargetInfo: CI.getTarget(), M: *M,
757 Diags&: CI.getDiagnostics()))
758 return nullptr;
759
760 // Inform the preprocessor that includes from within the input buffer should
761 // be resolved relative to the build directory of the module map file.
762 CI.getPreprocessor().setMainFileDir(*M->Directory);
763
764 // If the module was inferred from a different module map (via an expanded
765 // umbrella module definition), track that fact.
766 // FIXME: It would be preferable to fill this in as part of processing
767 // the module map, rather than adding it after the fact.
768 StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
769 if (!OriginalModuleMapName.empty()) {
770 auto OriginalModuleMap =
771 CI.getFileManager().getOptionalFileRef(Filename: OriginalModuleMapName,
772 /*openFile*/ OpenFile: true);
773 if (!OriginalModuleMap) {
774 CI.getDiagnostics().Report(DiagID: diag::err_module_map_not_found)
775 << OriginalModuleMapName;
776 return nullptr;
777 }
778 if (*OriginalModuleMap != CI.getSourceManager().getFileEntryRefForID(
779 FID: CI.getSourceManager().getMainFileID())) {
780 auto FileCharacter =
781 M->IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
782 FileID OriginalModuleMapFID = CI.getSourceManager().getOrCreateFileID(
783 SourceFile: *OriginalModuleMap, FileCharacter);
784 CI.getPreprocessor()
785 .getHeaderSearchInfo()
786 .getModuleMap()
787 .setInferredModuleAllowedBy(M, ModMapFID: OriginalModuleMapFID);
788 }
789 }
790
791 // If we're being run from the command-line, the module build stack will not
792 // have been filled in yet, so complete it now in order to allow us to detect
793 // module cycles.
794 SourceManager &SourceMgr = CI.getSourceManager();
795 if (SourceMgr.getModuleBuildStack().empty())
796 SourceMgr.pushModuleBuildStack(moduleName: CI.getLangOpts().CurrentModule,
797 importLoc: FullSourceLoc(SourceLocation(), SourceMgr));
798 return M;
799}
800
801/// Compute the input buffer that should be used to build the specified module.
802static std::unique_ptr<llvm::MemoryBuffer>
803getInputBufferForModule(CompilerInstance &CI, Module *M) {
804 FileManager &FileMgr = CI.getFileManager();
805
806 // Collect the set of #includes we need to build the module.
807 SmallString<256> HeaderContents;
808 std::error_code Err = std::error_code();
809 if (std::optional<Module::Header> UmbrellaHeader =
810 M->getUmbrellaHeaderAsWritten())
811 addHeaderInclude(HeaderName: UmbrellaHeader->PathRelativeToRootModuleDirectory,
812 Includes&: HeaderContents, LangOpts: CI.getLangOpts(), IsExternC: M->IsExternC);
813 Err = collectModuleHeaderIncludes(
814 LangOpts: CI.getLangOpts(), FileMgr, Diag&: CI.getDiagnostics(),
815 ModMap&: CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module: M,
816 Includes&: HeaderContents);
817
818 if (Err) {
819 CI.getDiagnostics().Report(DiagID: diag::err_module_cannot_create_includes)
820 << M->getFullModuleName() << Err.message();
821 return nullptr;
822 }
823
824 return llvm::MemoryBuffer::getMemBufferCopy(
825 InputData: HeaderContents, BufferName: Module::getModuleInputBufferName());
826}
827
828bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
829 const FrontendInputFile &RealInput) {
830 FrontendInputFile Input(RealInput);
831 assert(!Instance && "Already processing a source file!");
832 assert(!Input.isEmpty() && "Unexpected empty filename!");
833 setCurrentInput(CurrentInput: Input);
834 setCompilerInstance(&CI);
835
836 bool HasBegunSourceFile = false;
837 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
838 usesPreprocessorOnly();
839
840 // If we fail, reset state since the client will not end up calling the
841 // matching EndSourceFile(). All paths that return true should release this.
842 llvm::scope_exit FailureCleanup([&]() {
843 if (HasBegunSourceFile)
844 CI.getDiagnosticClient().EndSourceFile();
845 CI.setASTConsumer(nullptr);
846 CI.clearOutputFiles(/*EraseFiles=*/true);
847 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
848 setCurrentInput(CurrentInput: FrontendInputFile());
849 setCompilerInstance(nullptr);
850 });
851
852 if (!BeginInvocation(CI))
853 return false;
854
855 // The list of module files the input AST file depends on. This is separate
856 // from FrontendOptions::ModuleFiles, because those only represent explicit
857 // modules, while this is capable of representing implicit ones too.
858 SmallVector<ModuleFileName> ModuleFiles;
859
860 // If we're replaying the build of an AST file, import it and set up
861 // the initial state from its build.
862 if (ReplayASTFile) {
863 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CI.getDiagnosticsPtr();
864
865 // The AST unit populates its own diagnostics engine rather than ours.
866 auto ASTDiags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
867 A: Diags->getDiagnosticIDs(), A&: Diags->getDiagnosticOptions());
868 ASTDiags->setClient(client: Diags->getClient(), /*OwnsClient*/ShouldOwnClient: false);
869
870 // FIXME: What if the input is a memory buffer?
871 StringRef InputFile = Input.getFile();
872
873 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
874 Filename: InputFile, PCHContainerRdr: CI.getPCHContainerReader(), ToLoad: ASTUnit::LoadPreprocessorOnly,
875 VFS: CI.getVirtualFileSystemPtr(), DiagOpts: nullptr, Diags: ASTDiags, FileSystemOpts: CI.getFileSystemOpts(),
876 HSOpts: CI.getHeaderSearchOpts());
877 if (!AST)
878 return false;
879
880 // Options relating to how we treat the input (but not what we do with it)
881 // are inherited from the AST unit.
882 CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
883 CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
884 CI.getLangOpts() = AST->getLangOpts();
885
886 // Set the shared objects, these are reset when we finish processing the
887 // file, otherwise the CompilerInstance will happily destroy them.
888 CI.setVirtualFileSystem(AST->getFileManager().getVirtualFileSystemPtr());
889 CI.setFileManager(AST->getFileManagerPtr());
890 CI.createSourceManager();
891 CI.getSourceManager().initializeForReplay(Old: AST->getSourceManager());
892
893 // Preload all the module files loaded transitively by the AST unit. Also
894 // load all module map files that were parsed as part of building the AST
895 // unit.
896 if (auto ASTReader = AST->getASTReader()) {
897 auto &MM = ASTReader->getModuleManager();
898 auto &PrimaryModule = MM.getPrimaryModule();
899
900 for (serialization::ModuleFile &MF : MM)
901 if (&MF != &PrimaryModule)
902 ModuleFiles.emplace_back(Args&: MF.FileName);
903
904 ASTReader->visitTopLevelModuleMaps(MF&: PrimaryModule, Visitor: [&](FileEntryRef FE) {
905 CI.getFrontendOpts().ModuleMapFiles.push_back(
906 x: std::string(FE.getName()));
907 });
908 }
909
910 // Set up the input file for replay purposes.
911 auto Kind = AST->getInputKind();
912 if (Kind.getFormat() == InputKind::ModuleMap) {
913 Module *ASTModule =
914 AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
915 ModuleName: AST->getLangOpts().CurrentModule, ImportLoc: SourceLocation(),
916 /*AllowSearch*/ false);
917 assert(ASTModule && "module file does not define its own module");
918 Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
919 } else {
920 auto &OldSM = AST->getSourceManager();
921 FileID ID = OldSM.getMainFileID();
922 if (auto File = OldSM.getFileEntryRefForID(FID: ID))
923 Input = FrontendInputFile(File->getName(), Kind);
924 else
925 Input = FrontendInputFile(OldSM.getBufferOrFake(FID: ID), Kind);
926 }
927 setCurrentInput(CurrentInput: Input, AST: std::move(AST));
928 }
929
930 // AST files follow a very different path, since they share objects via the
931 // AST unit.
932 if (Input.getKind().getFormat() == InputKind::Precompiled) {
933 assert(!usesPreprocessorOnly() && "this case was handled above");
934 assert(hasASTFileSupport() &&
935 "This action does not have AST file support!");
936
937 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CI.getDiagnosticsPtr();
938
939 // FIXME: What if the input is a memory buffer?
940 StringRef InputFile = Input.getFile();
941
942 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
943 Filename: InputFile, PCHContainerRdr: CI.getPCHContainerReader(), ToLoad: ASTUnit::LoadEverything,
944 VFS: CI.getVirtualFileSystemPtr(), DiagOpts: nullptr, Diags, FileSystemOpts: CI.getFileSystemOpts(),
945 HSOpts: CI.getHeaderSearchOpts(), LangOpts: &CI.getLangOpts());
946
947 if (!AST)
948 return false;
949
950 // Inform the diagnostic client we are processing a source file.
951 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(), PP: nullptr);
952 HasBegunSourceFile = true;
953
954 // Set the shared objects, these are reset when we finish processing the
955 // file, otherwise the CompilerInstance will happily destroy them.
956 CI.setVirtualFileSystem(AST->getVirtualFileSystemPtr());
957 CI.setFileManager(AST->getFileManagerPtr());
958 CI.setSourceManager(AST->getSourceManagerPtr());
959 CI.setPreprocessor(AST->getPreprocessorPtr());
960 Preprocessor &PP = CI.getPreprocessor();
961 PP.getBuiltinInfo().initializeBuiltins(Table&: PP.getIdentifierTable(),
962 LangOpts: PP.getLangOpts());
963 CI.setASTContext(AST->getASTContextPtr());
964
965 setCurrentInput(CurrentInput: Input, AST: std::move(AST));
966
967 // Initialize the action.
968 if (!BeginSourceFileAction(CI))
969 return false;
970
971 // Create the AST consumer.
972 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InFile: InputFile));
973 if (!CI.hasASTConsumer())
974 return false;
975
976 FailureCleanup.release();
977 return true;
978 }
979
980 // Set up the file system, file and source managers, if needed.
981 if (!CI.hasVirtualFileSystem())
982 CI.createVirtualFileSystem();
983 if (!CI.hasFileManager())
984 CI.createFileManager();
985 if (!CI.hasSourceManager()) {
986 CI.createSourceManager();
987 if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
988 static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
989 ->setSarifWriter(
990 std::make_unique<SarifDocumentWriter>(args&: CI.getSourceManager()));
991 }
992 }
993
994 // Set up embedding for any specified files. Do this before we load any
995 // source files, including the primary module map for the compilation.
996 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
997 if (auto FE = CI.getFileManager().getOptionalFileRef(Filename: F, /*openFile*/OpenFile: true))
998 CI.getSourceManager().setFileIsTransient(*FE);
999 else
1000 CI.getDiagnostics().Report(DiagID: diag::err_modules_embed_file_not_found) << F;
1001 }
1002 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
1003 CI.getSourceManager().setAllFilesAreTransient(true);
1004
1005 // IR files bypass the rest of initialization.
1006 if (Input.getKind().getLanguage() == Language::LLVM_IR) {
1007 if (!hasIRSupport()) {
1008 CI.getDiagnostics().Report(DiagID: diag::err_ast_action_on_llvm_ir)
1009 << Input.getFile();
1010 return false;
1011 }
1012
1013 // Inform the diagnostic client we are processing a source file.
1014 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(), PP: nullptr);
1015 HasBegunSourceFile = true;
1016
1017 // Initialize the action.
1018 if (!BeginSourceFileAction(CI))
1019 return false;
1020
1021 // Initialize the main file entry.
1022 if (!CI.InitializeSourceManager(Input: CurrentInput))
1023 return false;
1024
1025 FailureCleanup.release();
1026 return true;
1027 }
1028
1029 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1030 FileManager &FileMgr = CI.getFileManager();
1031 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
1032
1033 // Canonicalize ImplicitPCHInclude. This way, all the downstream code,
1034 // including the ASTWriter, will receive the absolute path to the included
1035 // PCH.
1036 SmallString<128> PCHIncludePath(PPOpts.ImplicitPCHInclude);
1037 FileMgr.makeAbsolutePath(Path&: PCHIncludePath);
1038 llvm::sys::path::remove_dots(path&: PCHIncludePath, remove_dot_dot: true);
1039 PPOpts.ImplicitPCHInclude = PCHIncludePath.str();
1040 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
1041
1042 // If the implicit PCH include is actually a directory, rather than
1043 // a single file, search for a suitable PCH file in that directory.
1044 if (auto PCHDir = FileMgr.getOptionalDirectoryRef(DirName: PCHInclude)) {
1045 std::error_code EC;
1046 SmallString<128> DirNative;
1047 llvm::sys::path::native(path: PCHDir->getName(), result&: DirNative);
1048 bool Found = false;
1049 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1050 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
1051 FileMgr&: CI.getFileManager(), ModuleCachePath: CI.getHeaderSearchOpts().ModuleCachePath,
1052 DisableModuleHash: CI.getHeaderSearchOpts().DisableModuleHash,
1053 ContextHash: CI.getInvocation().computeContextHash());
1054 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(Dir: DirNative, EC),
1055 DirEnd;
1056 Dir != DirEnd && !EC; Dir.increment(EC)) {
1057 // Check whether this is an acceptable AST file.
1058 if (ASTReader::isAcceptableASTFile(
1059 Filename: Dir->path(), FileMgr, ModCache: CI.getModuleCache(),
1060 PCHContainerRdr: CI.getPCHContainerReader(), LangOpts: CI.getLangOpts(),
1061 CGOpts: CI.getCodeGenOpts(), TargetOpts: CI.getTargetOpts(),
1062 PPOpts: CI.getPreprocessorOpts(), HSOpts: CI.getHeaderSearchOpts(),
1063 SpecificModuleCachePath,
1064 /*RequireStrictOptionMatches=*/true)) {
1065 PPOpts.ImplicitPCHInclude = std::string(Dir->path());
1066 Found = true;
1067 break;
1068 }
1069 }
1070
1071 if (!Found) {
1072 CI.getDiagnostics().Report(DiagID: diag::err_fe_no_pch_in_dir) << PCHInclude;
1073 return false;
1074 }
1075 }
1076 }
1077
1078 // Set up the preprocessor if needed. When parsing model files the
1079 // preprocessor of the original source is reused.
1080 if (!isModelParsingAction())
1081 CI.createPreprocessor(TUKind: getTranslationUnitKind());
1082
1083 // Inform the diagnostic client we are processing a source file.
1084 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(),
1085 PP: &CI.getPreprocessor());
1086 HasBegunSourceFile = true;
1087
1088 // Handle C++20 header units.
1089 // Here, the user has the option to specify that the header name should be
1090 // looked up in the pre-processor search paths (and the main filename as
1091 // passed by the driver might therefore be incomplete until that look-up).
1092 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
1093 !Input.getKind().isPreprocessed()) {
1094 StringRef FileName = Input.getFile();
1095 InputKind Kind = Input.getKind();
1096 if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
1097 assert(CI.hasPreprocessor() &&
1098 "trying to build a header unit without a Pre-processor?");
1099 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
1100 // Relative searches begin from CWD.
1101 auto Dir = CI.getFileManager().getOptionalDirectoryRef(DirName: ".");
1102 SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 1> CWD;
1103 CWD.push_back(Elt: {std::nullopt, *Dir});
1104 OptionalFileEntryRef FE =
1105 HS.LookupFile(Filename: FileName, IncludeLoc: SourceLocation(),
1106 /*Angled*/ isAngled: Input.getKind().getHeaderUnitKind() ==
1107 InputKind::HeaderUnit_System,
1108 FromDir: nullptr, CurDir: nullptr, Includers: CWD, SearchPath: nullptr, RelativePath: nullptr, RequestingModule: nullptr,
1109 SuggestedModule: nullptr, IsMapped: nullptr, IsFrameworkFound: nullptr);
1110 if (!FE) {
1111 CI.getDiagnostics().Report(DiagID: diag::err_module_header_file_not_found)
1112 << FileName;
1113 return false;
1114 }
1115 // We now have the filename...
1116 FileName = FE->getName();
1117 // ... still a header unit, but now use the path as written.
1118 Kind = Input.getKind().withHeaderUnit(HU: InputKind::HeaderUnit_Abs);
1119 Input = FrontendInputFile(FileName, Kind, Input.isSystem());
1120 }
1121 // Unless the user has overridden the name, the header unit module name is
1122 // the pathname for the file.
1123 if (CI.getLangOpts().ModuleName.empty())
1124 CI.getLangOpts().ModuleName = std::string(FileName);
1125 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
1126 }
1127
1128 if (!CI.InitializeSourceManager(Input))
1129 return false;
1130
1131 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
1132 Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
1133 // We have an input filename like foo.iih, but we want to find the right
1134 // module name (and original file, to build the map entry).
1135 // Check if the first line specifies the original source file name with a
1136 // linemarker.
1137 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
1138 ReadOriginalFileName(CI, InputFile&: PresumedInputFile);
1139 // Unless the user overrides this, the module name is the name by which the
1140 // original file was known.
1141 if (CI.getLangOpts().ModuleName.empty())
1142 CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
1143 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
1144 }
1145
1146 // For module map files, we first parse the module map and synthesize a
1147 // "<module-includes>" buffer before more conventional processing.
1148 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
1149 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
1150
1151 std::string PresumedModuleMapFile;
1152 unsigned OffsetToContents;
1153 if (loadModuleMapForModuleBuild(CI, IsSystem: Input.isSystem(),
1154 IsPreprocessed: Input.isPreprocessed(),
1155 PresumedModuleMapFile, Offset&: OffsetToContents))
1156 return false;
1157
1158 auto *CurrentModule = prepareToBuildModule(CI, ModuleMapFilename: Input.getFile());
1159 if (!CurrentModule)
1160 return false;
1161
1162 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
1163
1164 if (OffsetToContents)
1165 // If the module contents are in the same file, skip to them.
1166 CI.getPreprocessor().setSkipMainFilePreamble(Bytes: OffsetToContents, StartOfLine: true);
1167 else {
1168 // Otherwise, convert the module description to a suitable input buffer.
1169 auto Buffer = getInputBufferForModule(CI, M: CurrentModule);
1170 if (!Buffer)
1171 return false;
1172
1173 // Reinitialize the main file entry to refer to the new input.
1174 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
1175 auto &SourceMgr = CI.getSourceManager();
1176 auto BufferID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter: Kind);
1177 assert(BufferID.isValid() && "couldn't create module buffer ID");
1178 SourceMgr.setMainFileID(BufferID);
1179 }
1180 }
1181
1182 // Initialize the action.
1183 if (!BeginSourceFileAction(CI))
1184 return false;
1185
1186 // If we were asked to load any module map files, do so now.
1187 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
1188 if (auto File = CI.getFileManager().getOptionalFileRef(Filename))
1189 CI.getPreprocessor().getHeaderSearchInfo().parseAndLoadModuleMapFile(
1190 File: *File, /*IsSystem*/ false, /*ImplicitlyDiscovered=*/false);
1191 else
1192 CI.getDiagnostics().Report(DiagID: diag::err_module_map_not_found) << Filename;
1193 }
1194
1195 // If compiling implementation of a module, load its module map file now.
1196 (void)CI.getPreprocessor().getCurrentModuleImplementation();
1197
1198 // Add a module declaration scope so that modules from -fmodule-map-file
1199 // arguments may shadow modules found implicitly in search paths.
1200 CI.getPreprocessor()
1201 .getHeaderSearchInfo()
1202 .getModuleMap()
1203 .finishModuleDeclarationScope();
1204
1205 // Create the AST context and consumer unless this is a preprocessor only
1206 // action.
1207 if (!usesPreprocessorOnly()) {
1208 // Parsing a model file should reuse the existing ASTContext.
1209 if (!isModelParsingAction())
1210 CI.createASTContext();
1211
1212 // For preprocessed files, check if the first line specifies the original
1213 // source file name with a linemarker.
1214 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
1215 if (Input.isPreprocessed())
1216 ReadOriginalFileName(CI, InputFile&: PresumedInputFile);
1217
1218 std::unique_ptr<ASTConsumer> Consumer =
1219 CreateWrappedASTConsumer(CI, InFile: PresumedInputFile);
1220 if (!Consumer)
1221 return false;
1222
1223 // FIXME: should not overwrite ASTMutationListener when parsing model files?
1224 if (!isModelParsingAction())
1225 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
1226
1227 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
1228 // Convert headers to PCH and chain them.
1229 IntrusiveRefCntPtr<ExternalSemaSource> source;
1230 IntrusiveRefCntPtr<ASTReader> FinalReader;
1231 source = createChainedIncludesSource(CI, OutReader&: FinalReader);
1232 if (!source)
1233 return false;
1234 CI.setASTReader(FinalReader);
1235 CI.getASTContext().setExternalSource(source);
1236 } else if (CI.getLangOpts().Modules ||
1237 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1238 // Use PCM or PCH.
1239 assert(hasPCHSupport() && "This action does not have PCH support!");
1240 ASTDeserializationListener *DeserialListener =
1241 Consumer->GetASTDeserializationListener();
1242 bool DeleteDeserialListener = false;
1243 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
1244 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
1245 DeleteDeserialListener);
1246 DeleteDeserialListener = true;
1247 }
1248 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
1249 DeserialListener = new DeserializedDeclsChecker(
1250 CI.getASTContext(),
1251 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
1252 DeserialListener, DeleteDeserialListener);
1253 DeleteDeserialListener = true;
1254 }
1255 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1256 CI.createPCHExternalASTSource(
1257 Path: CI.getPreprocessorOpts().ImplicitPCHInclude,
1258 DisableValidation: CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
1259 AllowPCHWithCompilerErrors: CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
1260 DeserializationListener: DeserialListener, OwnDeserializationListener: DeleteDeserialListener);
1261 if (!CI.getASTContext().getExternalSource())
1262 return false;
1263 }
1264 // If modules are enabled, create the AST reader before creating
1265 // any builtins, so that all declarations know that they might be
1266 // extended by an external source.
1267 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
1268 !CI.getASTContext().getExternalSource()) {
1269 CI.createASTReader();
1270 CI.getASTReader()->setDeserializationListener(Listener: DeserialListener,
1271 TakeOwnership: DeleteDeserialListener);
1272 }
1273 }
1274
1275 CI.setASTConsumer(std::move(Consumer));
1276 if (!CI.hasASTConsumer())
1277 return false;
1278 }
1279
1280 // Initialize built-in info as long as we aren't using an external AST
1281 // source.
1282 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
1283 !CI.getASTContext().getExternalSource()) {
1284 Preprocessor &PP = CI.getPreprocessor();
1285 PP.getBuiltinInfo().initializeBuiltins(Table&: PP.getIdentifierTable(),
1286 LangOpts: PP.getLangOpts());
1287 } else {
1288 // FIXME: If this is a problem, recover from it by creating a multiplex
1289 // source.
1290 assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
1291 "modules enabled but created an external source that "
1292 "doesn't support modules");
1293 }
1294
1295 // If we were asked to load any module files, do so now.
1296 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) {
1297 serialization::ModuleFile *Loaded = nullptr;
1298 if (!CI.loadModuleFile(FileName: ModuleFileName::makeExplicit(Name: ModuleFile), LoadedModuleFile&: Loaded))
1299 return false;
1300
1301 if (Loaded && Loaded->StandardCXXModule)
1302 CI.getDiagnostics().Report(
1303 DiagID: diag::warn_eagerly_load_for_standard_cplusplus_modules);
1304 }
1305
1306 // If we were asked to load any module files by the ASTUnit, do so now.
1307 for (const auto &ModuleFile : ModuleFiles) {
1308 serialization::ModuleFile *Loaded = nullptr;
1309 if (!CI.loadModuleFile(FileName: ModuleFile, LoadedModuleFile&: Loaded))
1310 return false;
1311
1312 if (Loaded && Loaded->StandardCXXModule)
1313 CI.getDiagnostics().Report(
1314 DiagID: diag::warn_eagerly_load_for_standard_cplusplus_modules);
1315 }
1316
1317 // If there is a layout overrides file, attach an external AST source that
1318 // provides the layouts from that file.
1319 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
1320 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
1321 auto Override = llvm::makeIntrusiveRefCnt<LayoutOverrideSource>(
1322 A&: CI.getFrontendOpts().OverrideRecordLayoutsFile);
1323 CI.getASTContext().setExternalSource(Override);
1324 }
1325
1326 // Setup HLSL External Sema Source
1327 if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
1328 auto HLSLSema = llvm::makeIntrusiveRefCnt<HLSLExternalSemaSource>();
1329 if (auto SemaSource = dyn_cast_if_present<ExternalSemaSource>(
1330 Val: CI.getASTContext().getExternalSourcePtr())) {
1331 auto MultiSema = llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>(
1332 A: std::move(SemaSource), A: std::move(HLSLSema));
1333 CI.getASTContext().setExternalSource(std::move(MultiSema));
1334 } else
1335 CI.getASTContext().setExternalSource(std::move(HLSLSema));
1336 }
1337
1338 FailureCleanup.release();
1339 return true;
1340}
1341
1342llvm::Error FrontendAction::Execute() {
1343 CompilerInstance &CI = getCompilerInstance();
1344 ExecuteAction();
1345
1346 // If we are supposed to rebuild the global module index, do so now unless
1347 // there were any module-build failures.
1348 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
1349 CI.hasPreprocessor()) {
1350 StringRef Cache =
1351 CI.getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
1352 if (!Cache.empty()) {
1353 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
1354 FileMgr&: CI.getFileManager(), PCHContainerRdr: CI.getPCHContainerReader(), Path: Cache)) {
1355 // FIXME this drops the error on the floor, but
1356 // Index/pch-from-libclang.c seems to rely on dropping at least some of
1357 // the error conditions!
1358 consumeError(Err: std::move(Err));
1359 }
1360 }
1361 }
1362
1363 return llvm::Error::success();
1364}
1365
1366void FrontendAction::EndSourceFile() {
1367 CompilerInstance &CI = getCompilerInstance();
1368
1369 // Inform the preprocessor we are done.
1370 if (CI.hasPreprocessor())
1371 CI.getPreprocessor().EndSourceFile();
1372
1373 // Inform the diagnostic client we are done with this source file.
1374 // Do this after notifying the preprocessor, so that end-of-file preprocessor
1375 // callbacks can report diagnostics.
1376 CI.getDiagnosticClient().EndSourceFile();
1377
1378 // Finalize the action.
1379 EndSourceFileAction();
1380
1381 // Sema references the ast consumer, so reset sema first.
1382 //
1383 // FIXME: There is more per-file stuff we could just drop here?
1384 bool DisableFree = CI.getFrontendOpts().DisableFree;
1385 if (DisableFree) {
1386 CI.resetAndLeakSema();
1387 CI.resetAndLeakASTContext();
1388 llvm::BuryPointer(Ptr: CI.takeASTConsumer().get());
1389 } else {
1390 CI.setSema(nullptr);
1391 CI.setASTContext(nullptr);
1392 CI.setASTConsumer(nullptr);
1393 }
1394
1395 if (CI.getFrontendOpts().ShowStats) {
1396 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
1397 if (CI.hasPreprocessor()) {
1398 CI.getPreprocessor().PrintStats();
1399 CI.getPreprocessor().getIdentifierTable().PrintStats();
1400 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1401 }
1402 if (CI.hasSourceManager()) {
1403 CI.getSourceManager().PrintStats();
1404 }
1405 llvm::errs() << "\n";
1406 }
1407
1408 // Cleanup the output streams, and erase the output files if instructed by the
1409 // FrontendAction.
1410 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
1411
1412 // The resources are owned by AST when the current file is AST.
1413 // So we reset the resources here to avoid users accessing it
1414 // accidently.
1415 if (isCurrentFileAST()) {
1416 if (DisableFree) {
1417 CI.resetAndLeakPreprocessor();
1418 CI.resetAndLeakSourceManager();
1419 CI.resetAndLeakFileManager();
1420 llvm::BuryPointer(Ptr: std::move(CurrentASTUnit));
1421 } else {
1422 CI.setPreprocessor(nullptr);
1423 CI.setSourceManager(nullptr);
1424 CI.setFileManager(nullptr);
1425 }
1426 }
1427
1428 setCompilerInstance(nullptr);
1429 setCurrentInput(CurrentInput: FrontendInputFile());
1430 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1431}
1432
1433bool FrontendAction::shouldEraseOutputFiles() {
1434 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1435}
1436
1437//===----------------------------------------------------------------------===//
1438// Utility Actions
1439//===----------------------------------------------------------------------===//
1440
1441void ASTFrontendAction::ExecuteAction() {
1442 CompilerInstance &CI = getCompilerInstance();
1443 if (!CI.hasPreprocessor())
1444 return;
1445 // This is a fallback: If the client forgets to invoke this, we mark the
1446 // current stack as the bottom. Though not optimal, this could help prevent
1447 // stack overflow during deep recursion.
1448 clang::noteBottomOfStack();
1449
1450 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1451 // here so the source manager would be initialized.
1452 if (hasCodeCompletionSupport() &&
1453 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
1454 CI.createCodeCompletionConsumer();
1455
1456 // Use a code completion consumer?
1457 CodeCompleteConsumer *CompletionConsumer = nullptr;
1458 if (CI.hasCodeCompletionConsumer())
1459 CompletionConsumer = &CI.getCodeCompletionConsumer();
1460
1461 if (!CI.hasSema())
1462 CI.createSema(TUKind: getTranslationUnitKind(), CompletionConsumer);
1463
1464 ParseAST(S&: CI.getSema(), PrintStats: CI.getFrontendOpts().ShowStats,
1465 SkipFunctionBodies: CI.getFrontendOpts().SkipFunctionBodies);
1466}
1467
1468void PluginASTAction::anchor() { }
1469
1470std::unique_ptr<ASTConsumer>
1471PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1472 StringRef InFile) {
1473 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1474}
1475
1476bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1477 return WrappedAction->PrepareToExecuteAction(CI);
1478}
1479std::unique_ptr<ASTConsumer>
1480WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1481 StringRef InFile) {
1482 return WrappedAction->CreateASTConsumer(CI, InFile);
1483}
1484bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1485 return WrappedAction->BeginInvocation(CI);
1486}
1487bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1488 WrappedAction->setCurrentInput(CurrentInput: getCurrentInput());
1489 WrappedAction->setCompilerInstance(&CI);
1490 auto Ret = WrappedAction->BeginSourceFileAction(CI);
1491 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1492 setCurrentInput(CurrentInput: WrappedAction->getCurrentInput());
1493 return Ret;
1494}
1495void WrapperFrontendAction::ExecuteAction() {
1496 WrappedAction->ExecuteAction();
1497}
1498void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
1499void WrapperFrontendAction::EndSourceFileAction() {
1500 WrappedAction->EndSourceFileAction();
1501}
1502bool WrapperFrontendAction::shouldEraseOutputFiles() {
1503 return WrappedAction->shouldEraseOutputFiles();
1504}
1505
1506bool WrapperFrontendAction::usesPreprocessorOnly() const {
1507 return WrappedAction->usesPreprocessorOnly();
1508}
1509TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1510 return WrappedAction->getTranslationUnitKind();
1511}
1512bool WrapperFrontendAction::hasPCHSupport() const {
1513 return WrappedAction->hasPCHSupport();
1514}
1515bool WrapperFrontendAction::hasASTFileSupport() const {
1516 return WrappedAction->hasASTFileSupport();
1517}
1518bool WrapperFrontendAction::hasIRSupport() const {
1519 return WrappedAction->hasIRSupport();
1520}
1521bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1522 return WrappedAction->hasCodeCompletionSupport();
1523}
1524
1525WrapperFrontendAction::WrapperFrontendAction(
1526 std::unique_ptr<FrontendAction> WrappedAction)
1527 : WrappedAction(std::move(WrappedAction)) {}
1528