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
373TranslationUnitKind FrontendAction::getTranslationUnitKind() {
374 // The ASTContext, if exists, knows the exact TUKind of the frondend.
375 if (Instance && Instance->hasASTContext())
376 return Instance->getASTContext().TUKind;
377 return TU_Complete;
378}
379
380bool FrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
381 if (CurrentInput.isPreprocessed())
382 CI.getPreprocessor().SetMacroExpansionOnlyInDirectives();
383 return true;
384}
385
386void FrontendAction::EndSourceFileAction() {
387 if (CurrentInput.isPreprocessed())
388 // Reset the preprocessor macro expansion to the default.
389 getCompilerInstance().getPreprocessor().SetEnableMacroExpansion();
390}
391
392void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
393 std::unique_ptr<ASTUnit> AST) {
394 this->CurrentInput = CurrentInput;
395 CurrentASTUnit = std::move(AST);
396}
397
398std::unique_ptr<ASTUnit> FrontendAction::takeCurrentASTUnit() {
399 return std::move(CurrentASTUnit);
400}
401
402Module *FrontendAction::getCurrentModule() const {
403 CompilerInstance &CI = getCompilerInstance();
404 return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
405 ModuleName: CI.getLangOpts().CurrentModule, ImportLoc: SourceLocation(), /*AllowSearch=*/false);
406}
407
408std::unique_ptr<ASTConsumer>
409FrontendAction::CreateWrappedASTConsumer(CompilerInstance &CI,
410 StringRef InFile) {
411 std::unique_ptr<ASTConsumer> Consumer = CreateASTConsumer(CI, InFile);
412 if (!Consumer)
413 return nullptr;
414
415 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
416 llvm::StringRef DumpDeserializedDeclarationRangesPath =
417 CI.getFrontendOpts().DumpMinimizationHintsPath;
418 if (!DumpDeserializedDeclarationRangesPath.empty()) {
419 std::error_code ErrorCode;
420 auto FileStream = std::make_unique<llvm::raw_fd_ostream>(
421 args&: DumpDeserializedDeclarationRangesPath, args&: ErrorCode,
422 args: llvm::sys::fs::OF_TextWithCRLF);
423 if (!ErrorCode) {
424 Consumers.push_back(x: std::make_unique<DeserializedDeclsSourceRangePrinter>(
425 args&: CI.getSourceManager(), args: std::move(FileStream)));
426 } else {
427 llvm::errs() << "Failed to create output file for "
428 "-dump-minimization-hints flag, file path: "
429 << DumpDeserializedDeclarationRangesPath
430 << ", error: " << ErrorCode.message() << "\n";
431 }
432 }
433
434 // Validate -add-plugin args.
435 bool FoundAllPlugins = true;
436 for (const std::string &Arg : CI.getFrontendOpts().AddPluginActions) {
437 bool Found = false;
438 for (const FrontendPluginRegistry::entry &Plugin :
439 FrontendPluginRegistry::entries()) {
440 if (Plugin.getName() == Arg)
441 Found = true;
442 }
443 if (!Found) {
444 CI.getDiagnostics().Report(DiagID: diag::err_fe_invalid_plugin_name) << Arg;
445 FoundAllPlugins = false;
446 }
447 }
448 if (!FoundAllPlugins)
449 return nullptr;
450
451 // If this is a code completion run, avoid invoking the plugin consumers
452 if (CI.hasCodeCompletionConsumer())
453 return Consumer;
454
455 // Collect the list of plugins that go before the main action (in Consumers)
456 // or after it (in AfterConsumers)
457 std::vector<std::unique_ptr<ASTConsumer>> AfterConsumers;
458 for (const FrontendPluginRegistry::entry &Plugin :
459 FrontendPluginRegistry::entries()) {
460 std::unique_ptr<PluginASTAction> P = Plugin.instantiate();
461 PluginASTAction::ActionType ActionType = P->getActionType();
462 if (ActionType == PluginASTAction::CmdlineAfterMainAction ||
463 ActionType == PluginASTAction::CmdlineBeforeMainAction) {
464 // This is O(|plugins| * |add_plugins|), but since both numbers are
465 // way below 50 in practice, that's ok.
466 if (llvm::is_contained(Range&: CI.getFrontendOpts().AddPluginActions,
467 Element: Plugin.getName())) {
468 if (ActionType == PluginASTAction::CmdlineBeforeMainAction)
469 ActionType = PluginASTAction::AddBeforeMainAction;
470 else
471 ActionType = PluginASTAction::AddAfterMainAction;
472 }
473 }
474 if ((ActionType == PluginASTAction::AddBeforeMainAction ||
475 ActionType == PluginASTAction::AddAfterMainAction) &&
476 P->ParseArgs(
477 CI,
478 arg: CI.getFrontendOpts().PluginArgs[std::string(Plugin.getName())])) {
479 std::unique_ptr<ASTConsumer> PluginConsumer = P->CreateASTConsumer(CI, InFile);
480 if (ActionType == PluginASTAction::AddBeforeMainAction) {
481 Consumers.push_back(x: std::move(PluginConsumer));
482 } else {
483 AfterConsumers.push_back(x: std::move(PluginConsumer));
484 }
485 }
486 }
487
488 // Add to Consumers the main consumer, then all the plugins that go after it
489 Consumers.push_back(x: std::move(Consumer));
490 if (!AfterConsumers.empty()) {
491 // If we have plugins after the main consumer, which may be the codegen
492 // action, they likely will need the ASTContext, so don't clear it in the
493 // codegen action.
494 CI.getCodeGenOpts().ClearASTBeforeBackend = false;
495 for (auto &C : AfterConsumers)
496 Consumers.push_back(x: std::move(C));
497 }
498
499 assert(Consumers.size() >= 1 && "should have added the main consumer");
500 if (Consumers.size() == 1)
501 return std::move(Consumers.front());
502 return std::make_unique<MultiplexConsumer>(args: std::move(Consumers));
503}
504
505/// For preprocessed files, if the first line is the linemarker and specifies
506/// the original source file name, use that name as the input file name.
507/// Returns the location of the first token after the line marker directive.
508///
509/// \param CI The compiler instance.
510/// \param InputFile Populated with the filename from the line marker.
511/// \param IsModuleMap If \c true, add a line note corresponding to this line
512/// directive. (We need to do this because the directive will not be
513/// visited by the preprocessor.)
514static SourceLocation ReadOriginalFileName(CompilerInstance &CI,
515 std::string &InputFile,
516 bool IsModuleMap = false) {
517 auto &SourceMgr = CI.getSourceManager();
518 auto MainFileID = SourceMgr.getMainFileID();
519
520 auto MainFileBuf = SourceMgr.getBufferOrNone(FID: MainFileID);
521 if (!MainFileBuf)
522 return SourceLocation();
523
524 auto RawLexer = std::make_unique<Lexer>(args&: MainFileID, args&: *MainFileBuf, args&: SourceMgr,
525 args&: CI.getLangOpts());
526
527 // If the first line has the syntax of
528 //
529 // # NUM "FILENAME"
530 //
531 // we use FILENAME as the input file name.
532 Token T;
533 if (RawLexer->LexFromRawLexer(Result&: T) || T.getKind() != tok::hash)
534 return SourceLocation();
535 if (RawLexer->LexFromRawLexer(Result&: T) || T.isAtStartOfLine() ||
536 T.getKind() != tok::numeric_constant)
537 return SourceLocation();
538
539 unsigned LineNo;
540 SourceLocation LineNoLoc = T.getLocation();
541 if (IsModuleMap) {
542 llvm::SmallString<16> Buffer;
543 if (Lexer::getSpelling(loc: LineNoLoc, buffer&: Buffer, SM: SourceMgr, options: CI.getLangOpts())
544 .getAsInteger(Radix: 10, Result&: LineNo))
545 return SourceLocation();
546 }
547
548 RawLexer->LexIncludeFilename(FilenameTok&: T);
549 if (T.isAtStartOfLine() || T.getKind() != tok::header_name)
550 return SourceLocation();
551
552 Preprocessor &PP = CI.getPreprocessor();
553 SmallString<128> HeaderNameBuffer;
554 StringRef HeaderName = PP.getSpelling(Tok: T, Buffer&: HeaderNameBuffer);
555 PP.GetLineDirectiveFilenameSpelling(Loc: T.getLocation(), Buffer&: HeaderName);
556
557 RawLexer->LexFromRawLexer(Result&: T);
558 if (T.isNot(K: tok::eof) && !T.isAtStartOfLine())
559 return SourceLocation();
560
561 InputFile = HeaderName.str();
562
563 if (IsModuleMap)
564 CI.getSourceManager().AddLineNote(
565 Loc: LineNoLoc, LineNo, FilenameID: SourceMgr.getLineTableFilenameID(Str: InputFile), IsFileEntry: false,
566 IsFileExit: false, FileKind: SrcMgr::C_User_ModuleMap);
567
568 return T.getLocation();
569}
570
571static SmallVectorImpl<char> &
572operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
573 Includes.append(in_start: RHS.begin(), in_end: RHS.end());
574 return Includes;
575}
576
577static void addHeaderInclude(StringRef HeaderName,
578 SmallVectorImpl<char> &Includes,
579 const LangOptions &LangOpts,
580 bool IsExternC) {
581 if (IsExternC && LangOpts.CPlusPlus)
582 Includes += "extern \"C\" {\n";
583 if (LangOpts.ObjC)
584 Includes += "#import \"";
585 else
586 Includes += "#include \"";
587
588 Includes += HeaderName;
589
590 Includes += "\"\n";
591 if (IsExternC && LangOpts.CPlusPlus)
592 Includes += "}\n";
593}
594
595/// Collect the set of header includes needed to construct the given
596/// module and update the TopHeaders file set of the module.
597///
598/// \param Module The module we're collecting includes from.
599///
600/// \param Includes Will be augmented with the set of \#includes or \#imports
601/// needed to load all of the named headers.
602static std::error_code collectModuleHeaderIncludes(
603 const LangOptions &LangOpts, FileManager &FileMgr, DiagnosticsEngine &Diag,
604 ModuleMap &ModMap, clang::Module *Module, SmallVectorImpl<char> &Includes) {
605 // Don't collect any headers for unavailable modules.
606 if (!Module->isAvailable())
607 return std::error_code();
608
609 // Resolve all lazy header directives to header files.
610 ModMap.resolveHeaderDirectives(Mod: Module, /*File=*/std::nullopt);
611
612 // If any headers are missing, we can't build this module. In most cases,
613 // diagnostics for this should have already been produced; we only get here
614 // if explicit stat information was provided.
615 // FIXME: If the name resolves to a file with different stat information,
616 // produce a better diagnostic.
617 if (!Module->MissingHeaders.empty()) {
618 auto &MissingHeader = Module->MissingHeaders.front();
619 Diag.Report(Loc: MissingHeader.FileNameLoc, DiagID: diag::err_module_header_missing)
620 << MissingHeader.IsUmbrella << MissingHeader.FileName;
621 return std::error_code();
622 }
623
624 // Add includes for each of these headers.
625 for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
626 for (const Module::Header &H : Module->getHeaders(HK)) {
627 Module->addTopHeader(File: H.Entry);
628 // Use the path as specified in the module map file. We'll look for this
629 // file relative to the module build directory (the directory containing
630 // the module map file) so this will find the same file that we found
631 // while parsing the module map.
632 addHeaderInclude(HeaderName: H.PathRelativeToRootModuleDirectory, Includes, LangOpts,
633 IsExternC: Module->IsExternC);
634 }
635 }
636 // Note that Module->PrivateHeaders will not be a TopHeader.
637
638 if (std::optional<Module::Header> UmbrellaHeader =
639 Module->getUmbrellaHeaderAsWritten()) {
640 Module->addTopHeader(File: UmbrellaHeader->Entry);
641 if (Module->Parent)
642 // Include the umbrella header for submodules.
643 addHeaderInclude(HeaderName: UmbrellaHeader->PathRelativeToRootModuleDirectory,
644 Includes, LangOpts, IsExternC: Module->IsExternC);
645 } else if (std::optional<Module::DirectoryName> UmbrellaDir =
646 Module->getUmbrellaDirAsWritten()) {
647 // Add all of the headers we find in this subdirectory.
648 std::error_code EC;
649 SmallString<128> DirNative;
650 llvm::sys::path::native(path: UmbrellaDir->Entry.getName(), result&: DirNative);
651
652 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
653 SmallVector<std::pair<std::string, std::string>, 8> HeaderPaths;
654 for (llvm::vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
655 Dir != End && !EC; Dir.increment(EC)) {
656 // Check whether this entry has an extension typically associated with
657 // headers.
658 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(path: Dir->path()))
659 .Cases(CaseStrings: {".h", ".H", ".hh", ".hpp"}, Value: true)
660 .Default(Value: false))
661 continue;
662
663 // Compute the relative path from the directory to this file.
664 SmallVector<StringRef, 16> Components;
665 auto PathIt = llvm::sys::path::rbegin(path: Dir->path());
666 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
667 Components.push_back(Elt: *PathIt);
668 SmallString<128> RelativeHeader(
669 UmbrellaDir->PathRelativeToRootModuleDirectory);
670 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
671 ++It)
672 llvm::sys::path::append(path&: RelativeHeader, a: *It);
673
674 HeaderPaths.push_back(
675 Elt: std::make_pair(x: Dir->path().str(), y: RelativeHeader.c_str()));
676 }
677
678 if (EC)
679 return EC;
680
681 // Sort header paths and make the header inclusion order deterministic
682 // across different OSs and filesystems. As the header search table
683 // serialization order depends on the file reference UID, we need to create
684 // file references in deterministic order too.
685 llvm::sort(C&: HeaderPaths, Comp: llvm::less_first());
686 for (auto &[Path, RelPath] : HeaderPaths) {
687 auto Header = FileMgr.getOptionalFileRef(Filename: Path);
688 // FIXME: This shouldn't happen unless there is a file system race. Is
689 // that worth diagnosing?
690 if (!Header)
691 continue;
692
693 // If this header is marked 'unavailable' in this module, don't include
694 // it.
695 if (ModMap.isHeaderUnavailableInModule(Header: *Header, RequestingModule: Module))
696 continue;
697
698 // Include this header as part of the umbrella directory.
699 Module->addTopHeader(File: *Header);
700 addHeaderInclude(HeaderName: RelPath, Includes, LangOpts, IsExternC: Module->IsExternC);
701 }
702 }
703
704 // Recurse into submodules.
705 for (clang::Module *Submodule : Module->submodules())
706 if (std::error_code Err = collectModuleHeaderIncludes(
707 LangOpts, FileMgr, Diag, ModMap, Module: Submodule, Includes))
708 return Err;
709
710 return std::error_code();
711}
712
713static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
714 bool IsPreprocessed,
715 std::string &PresumedModuleMapFile,
716 unsigned &Offset) {
717 auto &SrcMgr = CI.getSourceManager();
718 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
719
720 // Map the current input to a file.
721 FileID ModuleMapID = SrcMgr.getMainFileID();
722 OptionalFileEntryRef ModuleMap = SrcMgr.getFileEntryRefForID(FID: ModuleMapID);
723 assert(ModuleMap && "MainFileID without FileEntry");
724
725 // If the module map is preprocessed, handle the initial line marker;
726 // line directives are not part of the module map syntax in general.
727 Offset = 0;
728 if (IsPreprocessed) {
729 SourceLocation EndOfLineMarker =
730 ReadOriginalFileName(CI, InputFile&: PresumedModuleMapFile, /*IsModuleMap*/ true);
731 if (EndOfLineMarker.isValid())
732 Offset = CI.getSourceManager().getDecomposedLoc(Loc: EndOfLineMarker).second;
733 }
734
735 // Load the module map file.
736 if (HS.parseAndLoadModuleMapFile(File: *ModuleMap, IsSystem,
737 /*ImplicitlyDiscovered=*/false, ID: ModuleMapID,
738 Offset: &Offset, OriginalModuleMapFile: PresumedModuleMapFile))
739 return true;
740
741 if (SrcMgr.getBufferOrFake(FID: ModuleMapID).getBufferSize() == Offset)
742 Offset = 0;
743
744 // Infer framework module if possible.
745 if (HS.getModuleMap().canInferFrameworkModule(Dir: ModuleMap->getDir())) {
746 SmallString<128> InferredFrameworkPath = ModuleMap->getDir().getName();
747 llvm::sys::path::append(path&: InferredFrameworkPath,
748 a: CI.getLangOpts().ModuleName + ".framework");
749 if (auto Dir =
750 CI.getFileManager().getOptionalDirectoryRef(DirName: InferredFrameworkPath))
751 (void)HS.getModuleMap().inferFrameworkModule(FrameworkDir: *Dir, IsSystem, Parent: nullptr);
752 }
753
754 return false;
755}
756
757static Module *prepareToBuildModule(CompilerInstance &CI,
758 StringRef ModuleMapFilename) {
759 if (CI.getLangOpts().CurrentModule.empty()) {
760 CI.getDiagnostics().Report(DiagID: diag::err_missing_module_name);
761
762 // FIXME: Eventually, we could consider asking whether there was just
763 // a single module described in the module map, and use that as a
764 // default. Then it would be fairly trivial to just "compile" a module
765 // map with a single module (the common case).
766 return nullptr;
767 }
768
769 // Dig out the module definition.
770 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
771 Module *M = HS.lookupModule(ModuleName: CI.getLangOpts().CurrentModule, ImportLoc: SourceLocation(),
772 /*AllowSearch=*/true);
773 if (!M) {
774 CI.getDiagnostics().Report(DiagID: diag::err_missing_module)
775 << CI.getLangOpts().CurrentModule << ModuleMapFilename;
776
777 return nullptr;
778 }
779
780 // Check whether we can build this module at all.
781 if (Preprocessor::checkModuleIsAvailable(LangOpts: CI.getLangOpts(), TargetInfo: CI.getTarget(), M: *M,
782 Diags&: CI.getDiagnostics()))
783 return nullptr;
784
785 // Inform the preprocessor that includes from within the input buffer should
786 // be resolved relative to the build directory of the module map file.
787 CI.getPreprocessor().setMainFileDir(*M->Directory);
788
789 // If the module was inferred from a different module map (via an expanded
790 // umbrella module definition), track that fact.
791 // FIXME: It would be preferable to fill this in as part of processing
792 // the module map, rather than adding it after the fact.
793 StringRef OriginalModuleMapName = CI.getFrontendOpts().OriginalModuleMap;
794 if (!OriginalModuleMapName.empty()) {
795 auto OriginalModuleMap =
796 CI.getFileManager().getOptionalFileRef(Filename: OriginalModuleMapName,
797 /*openFile*/ OpenFile: true);
798 if (!OriginalModuleMap) {
799 CI.getDiagnostics().Report(DiagID: diag::err_module_map_not_found)
800 << OriginalModuleMapName;
801 return nullptr;
802 }
803 if (*OriginalModuleMap != CI.getSourceManager().getFileEntryRefForID(
804 FID: CI.getSourceManager().getMainFileID())) {
805 auto FileCharacter =
806 M->IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
807 FileID OriginalModuleMapFID = CI.getSourceManager().getOrCreateFileID(
808 SourceFile: *OriginalModuleMap, FileCharacter);
809 CI.getPreprocessor()
810 .getHeaderSearchInfo()
811 .getModuleMap()
812 .setInferredModuleAllowedBy(M, ModMapFID: OriginalModuleMapFID);
813 }
814 }
815
816 // If we're being run from the command-line, the module build stack will not
817 // have been filled in yet, so complete it now in order to allow us to detect
818 // module cycles.
819 SourceManager &SourceMgr = CI.getSourceManager();
820 if (SourceMgr.getModuleBuildStack().empty())
821 SourceMgr.pushModuleBuildStack(moduleName: CI.getLangOpts().CurrentModule,
822 importLoc: FullSourceLoc(SourceLocation(), SourceMgr));
823 return M;
824}
825
826/// Compute the input buffer that should be used to build the specified module.
827static std::unique_ptr<llvm::MemoryBuffer>
828getInputBufferForModule(CompilerInstance &CI, Module *M) {
829 FileManager &FileMgr = CI.getFileManager();
830
831 // Collect the set of #includes we need to build the module.
832 SmallString<256> HeaderContents;
833 std::error_code Err = std::error_code();
834 if (std::optional<Module::Header> UmbrellaHeader =
835 M->getUmbrellaHeaderAsWritten())
836 addHeaderInclude(HeaderName: UmbrellaHeader->PathRelativeToRootModuleDirectory,
837 Includes&: HeaderContents, LangOpts: CI.getLangOpts(), IsExternC: M->IsExternC);
838 Err = collectModuleHeaderIncludes(
839 LangOpts: CI.getLangOpts(), FileMgr, Diag&: CI.getDiagnostics(),
840 ModMap&: CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module: M,
841 Includes&: HeaderContents);
842
843 if (Err) {
844 CI.getDiagnostics().Report(DiagID: diag::err_module_cannot_create_includes)
845 << M->getFullModuleName() << Err.message();
846 return nullptr;
847 }
848
849 return llvm::MemoryBuffer::getMemBufferCopy(
850 InputData: HeaderContents, BufferName: Module::getModuleInputBufferName());
851}
852
853bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
854 const FrontendInputFile &RealInput) {
855 FrontendInputFile Input(RealInput);
856 assert(!Instance && "Already processing a source file!");
857 assert(!Input.isEmpty() && "Unexpected empty filename!");
858 setCurrentInput(CurrentInput: Input);
859 setCompilerInstance(&CI);
860
861 bool HasBegunSourceFile = false;
862 bool ReplayASTFile = Input.getKind().getFormat() == InputKind::Precompiled &&
863 usesPreprocessorOnly();
864
865 // If we fail, reset state since the client will not end up calling the
866 // matching EndSourceFile(). All paths that return true should release this.
867 llvm::scope_exit FailureCleanup([&]() {
868 if (HasBegunSourceFile)
869 CI.getDiagnosticClient().EndSourceFile();
870 CI.setASTConsumer(nullptr);
871 CI.clearOutputFiles(/*EraseFiles=*/true);
872 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
873 setCurrentInput(CurrentInput: FrontendInputFile());
874 setCompilerInstance(nullptr);
875 });
876
877 if (!BeginInvocation(CI))
878 return false;
879
880 // The list of module files the input AST file depends on. This is separate
881 // from FrontendOptions::ModuleFiles, because those only represent explicit
882 // modules, while this is capable of representing implicit ones too.
883 SmallVector<ModuleFileName> ModuleFiles;
884
885 // If we're replaying the build of an AST file, import it and set up
886 // the initial state from its build.
887 if (ReplayASTFile) {
888 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CI.getDiagnosticsPtr();
889
890 // The AST unit populates its own diagnostics engine rather than ours.
891 auto ASTDiags = llvm::makeIntrusiveRefCnt<DiagnosticsEngine>(
892 A: Diags->getDiagnosticIDs(), A&: Diags->getDiagnosticOptions());
893 ASTDiags->setClient(client: Diags->getClient(), /*OwnsClient*/ShouldOwnClient: false);
894
895 // FIXME: What if the input is a memory buffer?
896 StringRef InputFile = Input.getFile();
897
898 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
899 Filename: InputFile, PCHContainerRdr: CI.getPCHContainerReader(), ToLoad: ASTUnit::LoadPreprocessorOnly,
900 VFS: CI.getVirtualFileSystemPtr(), DiagOpts: nullptr, Diags: ASTDiags, FileSystemOpts: CI.getFileSystemOpts(),
901 HSOpts: CI.getHeaderSearchOpts());
902 if (!AST)
903 return false;
904
905 // Options relating to how we treat the input (but not what we do with it)
906 // are inherited from the AST unit.
907 CI.getHeaderSearchOpts() = AST->getHeaderSearchOpts();
908 CI.getPreprocessorOpts() = AST->getPreprocessorOpts();
909 CI.getLangOpts() = AST->getLangOpts();
910
911 // Set the shared objects, these are reset when we finish processing the
912 // file, otherwise the CompilerInstance will happily destroy them.
913 CI.setVirtualFileSystem(AST->getFileManager().getVirtualFileSystemPtr());
914 CI.setFileManager(AST->getFileManagerPtr());
915 CI.createSourceManager();
916 CI.getSourceManager().initializeForReplay(Old: AST->getSourceManager());
917
918 // Preload all the module files loaded transitively by the AST unit. Also
919 // load all module map files that were parsed as part of building the AST
920 // unit.
921 if (auto ASTReader = AST->getASTReader()) {
922 auto &MM = ASTReader->getModuleManager();
923 auto &PrimaryModule = MM.getPrimaryModule();
924
925 for (serialization::ModuleFile &MF : MM)
926 if (&MF != &PrimaryModule)
927 ModuleFiles.emplace_back(Args&: MF.FileName);
928
929 ASTReader->visitTopLevelModuleMaps(MF&: PrimaryModule, Visitor: [&](FileEntryRef FE) {
930 CI.getFrontendOpts().ModuleMapFiles.push_back(
931 x: std::string(FE.getName()));
932 });
933 }
934
935 // Set up the input file for replay purposes.
936 auto Kind = AST->getInputKind();
937 if (Kind.getFormat() == InputKind::ModuleMap) {
938 Module *ASTModule =
939 AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
940 ModuleName: AST->getLangOpts().CurrentModule, ImportLoc: SourceLocation(),
941 /*AllowSearch*/ false);
942 assert(ASTModule && "module file does not define its own module");
943 Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
944 } else {
945 auto &OldSM = AST->getSourceManager();
946 FileID ID = OldSM.getMainFileID();
947 if (auto File = OldSM.getFileEntryRefForID(FID: ID))
948 Input = FrontendInputFile(File->getName(), Kind);
949 else
950 Input = FrontendInputFile(OldSM.getBufferOrFake(FID: ID), Kind);
951 }
952 setCurrentInput(CurrentInput: Input, AST: std::move(AST));
953 }
954
955 // AST files follow a very different path, since they share objects via the
956 // AST unit.
957 if (Input.getKind().getFormat() == InputKind::Precompiled) {
958 assert(!usesPreprocessorOnly() && "this case was handled above");
959 assert(hasASTFileSupport() &&
960 "This action does not have AST file support!");
961
962 IntrusiveRefCntPtr<DiagnosticsEngine> Diags = CI.getDiagnosticsPtr();
963
964 // FIXME: What if the input is a memory buffer?
965 StringRef InputFile = Input.getFile();
966
967 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromASTFile(
968 Filename: InputFile, PCHContainerRdr: CI.getPCHContainerReader(), ToLoad: ASTUnit::LoadEverything,
969 VFS: CI.getVirtualFileSystemPtr(), DiagOpts: nullptr, Diags, FileSystemOpts: CI.getFileSystemOpts(),
970 HSOpts: CI.getHeaderSearchOpts(), LangOpts: &CI.getLangOpts());
971
972 if (!AST)
973 return false;
974
975 // Inform the diagnostic client we are processing a source file.
976 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(), PP: nullptr);
977 HasBegunSourceFile = true;
978
979 // Set the shared objects, these are reset when we finish processing the
980 // file, otherwise the CompilerInstance will happily destroy them.
981 CI.setVirtualFileSystem(AST->getVirtualFileSystemPtr());
982 CI.setFileManager(AST->getFileManagerPtr());
983 CI.setSourceManager(AST->getSourceManagerPtr());
984 CI.setPreprocessor(AST->getPreprocessorPtr());
985 Preprocessor &PP = CI.getPreprocessor();
986 PP.getBuiltinInfo().initializeBuiltins(Table&: PP.getIdentifierTable(),
987 LangOpts: PP.getLangOpts());
988 CI.setASTContext(AST->getASTContextPtr());
989
990 setCurrentInput(CurrentInput: Input, AST: std::move(AST));
991
992 // Initialize the action.
993 if (!BeginSourceFileAction(CI))
994 return false;
995
996 // Create the AST consumer.
997 CI.setASTConsumer(CreateWrappedASTConsumer(CI, InFile: InputFile));
998 if (!CI.hasASTConsumer())
999 return false;
1000
1001 FailureCleanup.release();
1002 return true;
1003 }
1004
1005 // Set up the file system, file and source managers, if needed.
1006 if (!CI.hasVirtualFileSystem())
1007 CI.createVirtualFileSystem();
1008 if (!CI.hasFileManager())
1009 CI.createFileManager();
1010 if (!CI.hasSourceManager()) {
1011 CI.createSourceManager();
1012 if (CI.getDiagnosticOpts().getFormat() == DiagnosticOptions::SARIF) {
1013 static_cast<SARIFDiagnosticPrinter *>(&CI.getDiagnosticClient())
1014 ->setSarifWriter(
1015 std::make_unique<SarifDocumentWriter>(args&: CI.getSourceManager()));
1016 }
1017 }
1018
1019 // Set up embedding for any specified files. Do this before we load any
1020 // source files, including the primary module map for the compilation.
1021 for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
1022 if (auto FE = CI.getFileManager().getOptionalFileRef(Filename: F, /*openFile*/OpenFile: true))
1023 CI.getSourceManager().setFileIsTransient(*FE);
1024 else
1025 CI.getDiagnostics().Report(DiagID: diag::err_modules_embed_file_not_found) << F;
1026 }
1027 if (CI.getFrontendOpts().ModulesEmbedAllFiles)
1028 CI.getSourceManager().setAllFilesAreTransient(true);
1029
1030 // IR files bypass the rest of initialization.
1031 if (Input.getKind().getLanguage() == Language::LLVM_IR) {
1032 if (!hasIRSupport()) {
1033 CI.getDiagnostics().Report(DiagID: diag::err_ast_action_on_llvm_ir)
1034 << Input.getFile();
1035 return false;
1036 }
1037
1038 // Inform the diagnostic client we are processing a source file.
1039 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(), PP: nullptr);
1040 HasBegunSourceFile = true;
1041
1042 // Initialize the action.
1043 if (!BeginSourceFileAction(CI))
1044 return false;
1045
1046 // Initialize the main file entry.
1047 if (!CI.InitializeSourceManager(Input: CurrentInput))
1048 return false;
1049
1050 FailureCleanup.release();
1051 return true;
1052 }
1053
1054 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1055 FileManager &FileMgr = CI.getFileManager();
1056 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
1057
1058 // Canonicalize ImplicitPCHInclude. This way, all the downstream code,
1059 // including the ASTWriter, will receive the absolute path to the included
1060 // PCH.
1061 SmallString<128> PCHIncludePath(PPOpts.ImplicitPCHInclude);
1062 FileMgr.makeAbsolutePath(Path&: PCHIncludePath);
1063 llvm::sys::path::remove_dots(path&: PCHIncludePath, remove_dot_dot: true);
1064 PPOpts.ImplicitPCHInclude = PCHIncludePath.str();
1065 StringRef PCHInclude = PPOpts.ImplicitPCHInclude;
1066
1067 // If the implicit PCH include is actually a directory, rather than
1068 // a single file, search for a suitable PCH file in that directory.
1069 if (auto PCHDir = FileMgr.getOptionalDirectoryRef(DirName: PCHInclude)) {
1070 std::error_code EC;
1071 SmallString<128> DirNative;
1072 llvm::sys::path::native(path: PCHDir->getName(), result&: DirNative);
1073 bool Found = false;
1074 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1075 std::string SpecificModuleCachePath = createSpecificModuleCachePath(
1076 FileMgr&: CI.getFileManager(), ModuleCachePath: CI.getHeaderSearchOpts().ModuleCachePath,
1077 DisableModuleHash: CI.getHeaderSearchOpts().DisableModuleHash,
1078 ContextHash: CI.getInvocation().computeContextHash());
1079 for (llvm::vfs::directory_iterator Dir = FS.dir_begin(Dir: DirNative, EC),
1080 DirEnd;
1081 Dir != DirEnd && !EC; Dir.increment(EC)) {
1082 // Check whether this is an acceptable AST file.
1083 if (ASTReader::isAcceptableASTFile(
1084 Filename: Dir->path(), FileMgr, ModCache: CI.getModuleCache(),
1085 PCHContainerRdr: CI.getPCHContainerReader(), LangOpts: CI.getLangOpts(),
1086 CGOpts: CI.getCodeGenOpts(), TargetOpts: CI.getTargetOpts(),
1087 PPOpts: CI.getPreprocessorOpts(), HSOpts: CI.getHeaderSearchOpts(),
1088 SpecificModuleCachePath,
1089 /*RequireStrictOptionMatches=*/true)) {
1090 PPOpts.ImplicitPCHInclude = std::string(Dir->path());
1091 Found = true;
1092 break;
1093 }
1094 }
1095
1096 if (!Found) {
1097 CI.getDiagnostics().Report(DiagID: diag::err_fe_no_pch_in_dir) << PCHInclude;
1098 return false;
1099 }
1100 }
1101 }
1102
1103 // Set up the preprocessor if needed. When parsing model files the
1104 // preprocessor of the original source is reused.
1105 if (!isModelParsingAction())
1106 CI.createPreprocessor(TUKind: getTranslationUnitKind());
1107
1108 // Inform the diagnostic client we are processing a source file.
1109 CI.getDiagnosticClient().BeginSourceFile(LangOpts: CI.getLangOpts(),
1110 PP: &CI.getPreprocessor());
1111 HasBegunSourceFile = true;
1112
1113 // Handle C++20 header units.
1114 // Here, the user has the option to specify that the header name should be
1115 // looked up in the pre-processor search paths (and the main filename as
1116 // passed by the driver might therefore be incomplete until that look-up).
1117 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
1118 !Input.getKind().isPreprocessed()) {
1119 StringRef FileName = Input.getFile();
1120 InputKind Kind = Input.getKind();
1121 if (Kind.getHeaderUnitKind() != InputKind::HeaderUnit_Abs) {
1122 assert(CI.hasPreprocessor() &&
1123 "trying to build a header unit without a Pre-processor?");
1124 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
1125 // Relative searches begin from CWD.
1126 auto Dir = CI.getFileManager().getOptionalDirectoryRef(DirName: ".");
1127 SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 1> CWD;
1128 CWD.push_back(Elt: {std::nullopt, *Dir});
1129 OptionalFileEntryRef FE =
1130 HS.LookupFile(Filename: FileName, IncludeLoc: SourceLocation(),
1131 /*Angled*/ isAngled: Input.getKind().getHeaderUnitKind() ==
1132 InputKind::HeaderUnit_System,
1133 FromDir: nullptr, CurDir: nullptr, Includers: CWD, SearchPath: nullptr, RelativePath: nullptr, RequestingModule: nullptr,
1134 SuggestedModule: nullptr, IsMapped: nullptr, IsFrameworkFound: nullptr);
1135 if (!FE) {
1136 CI.getDiagnostics().Report(DiagID: diag::err_module_header_file_not_found)
1137 << FileName;
1138 return false;
1139 }
1140 // We now have the filename...
1141 FileName = FE->getName();
1142 // ... still a header unit, but now use the path as written.
1143 Kind = Input.getKind().withHeaderUnit(HU: InputKind::HeaderUnit_Abs);
1144 Input = FrontendInputFile(FileName, Kind, Input.isSystem());
1145 }
1146 // Unless the user has overridden the name, the header unit module name is
1147 // the pathname for the file.
1148 if (CI.getLangOpts().ModuleName.empty())
1149 CI.getLangOpts().ModuleName = std::string(FileName);
1150 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
1151 }
1152
1153 if (!CI.InitializeSourceManager(Input))
1154 return false;
1155
1156 if (CI.getLangOpts().CPlusPlusModules && Input.getKind().isHeaderUnit() &&
1157 Input.getKind().isPreprocessed() && !usesPreprocessorOnly()) {
1158 // We have an input filename like foo.iih, but we want to find the right
1159 // module name (and original file, to build the map entry).
1160 // Check if the first line specifies the original source file name with a
1161 // linemarker.
1162 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
1163 ReadOriginalFileName(CI, InputFile&: PresumedInputFile);
1164 // Unless the user overrides this, the module name is the name by which the
1165 // original file was known.
1166 if (CI.getLangOpts().ModuleName.empty())
1167 CI.getLangOpts().ModuleName = std::string(PresumedInputFile);
1168 CI.getLangOpts().CurrentModule = CI.getLangOpts().ModuleName;
1169 }
1170
1171 // For module map files, we first parse the module map and synthesize a
1172 // "<module-includes>" buffer before more conventional processing.
1173 if (Input.getKind().getFormat() == InputKind::ModuleMap) {
1174 CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
1175
1176 std::string PresumedModuleMapFile;
1177 unsigned OffsetToContents;
1178 if (loadModuleMapForModuleBuild(CI, IsSystem: Input.isSystem(),
1179 IsPreprocessed: Input.isPreprocessed(),
1180 PresumedModuleMapFile, Offset&: OffsetToContents))
1181 return false;
1182
1183 auto *CurrentModule = prepareToBuildModule(CI, ModuleMapFilename: Input.getFile());
1184 if (!CurrentModule)
1185 return false;
1186
1187 CurrentModule->PresumedModuleMapFile = PresumedModuleMapFile;
1188
1189 if (OffsetToContents)
1190 // If the module contents are in the same file, skip to them.
1191 CI.getPreprocessor().setSkipMainFilePreamble(Bytes: OffsetToContents, StartOfLine: true);
1192 else {
1193 // Otherwise, convert the module description to a suitable input buffer.
1194 auto Buffer = getInputBufferForModule(CI, M: CurrentModule);
1195 if (!Buffer)
1196 return false;
1197
1198 // Reinitialize the main file entry to refer to the new input.
1199 auto Kind = CurrentModule->IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
1200 auto &SourceMgr = CI.getSourceManager();
1201 auto BufferID = SourceMgr.createFileID(Buffer: std::move(Buffer), FileCharacter: Kind);
1202 assert(BufferID.isValid() && "couldn't create module buffer ID");
1203 SourceMgr.setMainFileID(BufferID);
1204 }
1205 }
1206
1207 // Initialize the action.
1208 if (!BeginSourceFileAction(CI))
1209 return false;
1210
1211 // If we were asked to load any module map files, do so now.
1212 for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
1213 if (auto File = CI.getFileManager().getOptionalFileRef(Filename))
1214 CI.getPreprocessor().getHeaderSearchInfo().parseAndLoadModuleMapFile(
1215 File: *File, /*IsSystem*/ false, /*ImplicitlyDiscovered=*/false);
1216 else
1217 CI.getDiagnostics().Report(DiagID: diag::err_module_map_not_found) << Filename;
1218 }
1219
1220 // If compiling implementation of a module, load its module map file now.
1221 (void)CI.getPreprocessor().getCurrentModuleImplementation();
1222
1223 // Add a module declaration scope so that modules from -fmodule-map-file
1224 // arguments may shadow modules found implicitly in search paths.
1225 CI.getPreprocessor()
1226 .getHeaderSearchInfo()
1227 .getModuleMap()
1228 .finishModuleDeclarationScope();
1229
1230 // Create the AST context and consumer unless this is a preprocessor only
1231 // action.
1232 if (!usesPreprocessorOnly()) {
1233 // Parsing a model file should reuse the existing ASTContext.
1234 if (!isModelParsingAction())
1235 CI.createASTContext();
1236
1237 // For preprocessed files, check if the first line specifies the original
1238 // source file name with a linemarker.
1239 std::string PresumedInputFile = std::string(getCurrentFileOrBufferName());
1240 if (Input.isPreprocessed())
1241 ReadOriginalFileName(CI, InputFile&: PresumedInputFile);
1242
1243 std::unique_ptr<ASTConsumer> Consumer =
1244 CreateWrappedASTConsumer(CI, InFile: PresumedInputFile);
1245 if (!Consumer)
1246 return false;
1247
1248 // FIXME: should not overwrite ASTMutationListener when parsing model files?
1249 if (!isModelParsingAction())
1250 CI.getASTContext().setASTMutationListener(Consumer->GetASTMutationListener());
1251
1252 if (!CI.getPreprocessorOpts().ChainedIncludes.empty()) {
1253 // Convert headers to PCH and chain them.
1254 IntrusiveRefCntPtr<ExternalSemaSource> source;
1255 IntrusiveRefCntPtr<ASTReader> FinalReader;
1256 source = createChainedIncludesSource(CI, OutReader&: FinalReader);
1257 if (!source)
1258 return false;
1259 CI.setASTReader(FinalReader);
1260 CI.getASTContext().setExternalSource(source);
1261 } else if (CI.getLangOpts().Modules ||
1262 !CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1263 // Use PCM or PCH.
1264 assert(hasPCHSupport() && "This action does not have PCH support!");
1265 ASTDeserializationListener *DeserialListener =
1266 Consumer->GetASTDeserializationListener();
1267 bool DeleteDeserialListener = false;
1268 if (CI.getPreprocessorOpts().DumpDeserializedPCHDecls) {
1269 DeserialListener = new DeserializedDeclsDumper(DeserialListener,
1270 DeleteDeserialListener);
1271 DeleteDeserialListener = true;
1272 }
1273 if (!CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn.empty()) {
1274 DeserialListener = new DeserializedDeclsChecker(
1275 CI.getASTContext(),
1276 CI.getPreprocessorOpts().DeserializedPCHDeclsToErrorOn,
1277 DeserialListener, DeleteDeserialListener);
1278 DeleteDeserialListener = true;
1279 }
1280 if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) {
1281 CI.createPCHExternalASTSource(
1282 Path: CI.getPreprocessorOpts().ImplicitPCHInclude,
1283 DisableValidation: CI.getPreprocessorOpts().DisablePCHOrModuleValidation,
1284 AllowPCHWithCompilerErrors: CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
1285 DeserializationListener: DeserialListener, OwnDeserializationListener: DeleteDeserialListener);
1286 if (!CI.getASTContext().getExternalSource())
1287 return false;
1288 }
1289 // If modules are enabled, create the AST reader before creating
1290 // any builtins, so that all declarations know that they might be
1291 // extended by an external source.
1292 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
1293 !CI.getASTContext().getExternalSource()) {
1294 CI.createASTReader();
1295 CI.getASTReader()->setDeserializationListener(Listener: DeserialListener,
1296 TakeOwnership: DeleteDeserialListener);
1297 }
1298 }
1299
1300 CI.setASTConsumer(std::move(Consumer));
1301 if (!CI.hasASTConsumer())
1302 return false;
1303 }
1304
1305 // Initialize built-in info as long as we aren't using an external AST
1306 // source.
1307 if (CI.getLangOpts().Modules || !CI.hasASTContext() ||
1308 !CI.getASTContext().getExternalSource()) {
1309 Preprocessor &PP = CI.getPreprocessor();
1310 PP.getBuiltinInfo().initializeBuiltins(Table&: PP.getIdentifierTable(),
1311 LangOpts: PP.getLangOpts());
1312 } else {
1313 // FIXME: If this is a problem, recover from it by creating a multiplex
1314 // source.
1315 assert((!CI.getLangOpts().Modules || CI.getASTReader()) &&
1316 "modules enabled but created an external source that "
1317 "doesn't support modules");
1318 }
1319
1320 // If we were asked to load any module files, do so now.
1321 for (const auto &ModuleFile : CI.getFrontendOpts().ModuleFiles) {
1322 serialization::ModuleFile *Loaded = nullptr;
1323 if (!CI.loadModuleFile(FileName: ModuleFileName::makeExplicit(Name: ModuleFile), LoadedModuleFile&: Loaded))
1324 return false;
1325
1326 if (Loaded && Loaded->StandardCXXModule)
1327 CI.getDiagnostics().Report(
1328 DiagID: diag::warn_eagerly_load_for_standard_cplusplus_modules);
1329 }
1330
1331 // If we were asked to load any module files by the ASTUnit, do so now.
1332 for (const auto &ModuleFile : ModuleFiles) {
1333 serialization::ModuleFile *Loaded = nullptr;
1334 if (!CI.loadModuleFile(FileName: ModuleFile, LoadedModuleFile&: Loaded))
1335 return false;
1336
1337 if (Loaded && Loaded->StandardCXXModule)
1338 CI.getDiagnostics().Report(
1339 DiagID: diag::warn_eagerly_load_for_standard_cplusplus_modules);
1340 }
1341
1342 // If there is a layout overrides file, attach an external AST source that
1343 // provides the layouts from that file.
1344 if (!CI.getFrontendOpts().OverrideRecordLayoutsFile.empty() &&
1345 CI.hasASTContext() && !CI.getASTContext().getExternalSource()) {
1346 auto Override = llvm::makeIntrusiveRefCnt<LayoutOverrideSource>(
1347 A&: CI.getFrontendOpts().OverrideRecordLayoutsFile);
1348 CI.getASTContext().setExternalSource(Override);
1349 }
1350
1351 // Setup HLSL External Sema Source
1352 if (CI.getLangOpts().HLSL && CI.hasASTContext()) {
1353 auto HLSLSema = llvm::makeIntrusiveRefCnt<HLSLExternalSemaSource>();
1354 if (auto SemaSource = dyn_cast_if_present<ExternalSemaSource>(
1355 Val: CI.getASTContext().getExternalSourcePtr())) {
1356 auto MultiSema = llvm::makeIntrusiveRefCnt<MultiplexExternalSemaSource>(
1357 A: std::move(SemaSource), A: std::move(HLSLSema));
1358 CI.getASTContext().setExternalSource(std::move(MultiSema));
1359 } else
1360 CI.getASTContext().setExternalSource(std::move(HLSLSema));
1361 }
1362
1363 FailureCleanup.release();
1364 return true;
1365}
1366
1367llvm::Error FrontendAction::Execute() {
1368 CompilerInstance &CI = getCompilerInstance();
1369 ExecuteAction();
1370
1371 // If we are supposed to rebuild the global module index, do so now unless
1372 // there were any module-build failures.
1373 if (CI.shouldBuildGlobalModuleIndex() && CI.hasFileManager() &&
1374 CI.hasPreprocessor()) {
1375 StringRef Cache =
1376 CI.getPreprocessor().getHeaderSearchInfo().getSpecificModuleCachePath();
1377 if (!Cache.empty()) {
1378 if (llvm::Error Err = GlobalModuleIndex::writeIndex(
1379 FileMgr&: CI.getFileManager(), PCHContainerRdr: CI.getPCHContainerReader(), Path: Cache)) {
1380 // FIXME this drops the error on the floor, but
1381 // Index/pch-from-libclang.c seems to rely on dropping at least some of
1382 // the error conditions!
1383 consumeError(Err: std::move(Err));
1384 }
1385 }
1386 }
1387
1388 return llvm::Error::success();
1389}
1390
1391void FrontendAction::EndSourceFile() {
1392 CompilerInstance &CI = getCompilerInstance();
1393
1394 // Inform the preprocessor we are done.
1395 if (CI.hasPreprocessor())
1396 CI.getPreprocessor().EndSourceFile();
1397
1398 // Inform the diagnostic client we are done with this source file.
1399 // Do this after notifying the preprocessor, so that end-of-file preprocessor
1400 // callbacks can report diagnostics.
1401 CI.getDiagnosticClient().EndSourceFile();
1402
1403 // Finalize the action.
1404 EndSourceFileAction();
1405
1406 // Sema references the ast consumer, so reset sema first.
1407 //
1408 // FIXME: There is more per-file stuff we could just drop here?
1409 bool DisableFree = CI.getFrontendOpts().DisableFree;
1410 if (DisableFree) {
1411 CI.resetAndLeakSema();
1412 CI.resetAndLeakASTContext();
1413 llvm::BuryPointer(Ptr: CI.takeASTConsumer().get());
1414 } else {
1415 CI.setSema(nullptr);
1416 CI.setASTContext(nullptr);
1417 CI.setASTConsumer(nullptr);
1418 }
1419
1420 if (CI.getFrontendOpts().ShowStats) {
1421 llvm::errs() << "\nSTATISTICS FOR '" << getCurrentFileOrBufferName() << "':\n";
1422 if (CI.hasPreprocessor()) {
1423 CI.getPreprocessor().PrintStats();
1424 CI.getPreprocessor().getIdentifierTable().PrintStats();
1425 CI.getPreprocessor().getHeaderSearchInfo().PrintStats();
1426 }
1427 if (CI.hasSourceManager()) {
1428 CI.getSourceManager().PrintStats();
1429 }
1430 llvm::errs() << "\n";
1431 }
1432
1433 // Cleanup the output streams, and erase the output files if instructed by the
1434 // FrontendAction.
1435 CI.clearOutputFiles(/*EraseFiles=*/shouldEraseOutputFiles());
1436
1437 // The resources are owned by AST when the current file is AST.
1438 // So we reset the resources here to avoid users accessing it
1439 // accidently.
1440 if (isCurrentFileAST()) {
1441 if (DisableFree) {
1442 CI.resetAndLeakPreprocessor();
1443 CI.resetAndLeakSourceManager();
1444 CI.resetAndLeakFileManager();
1445 llvm::BuryPointer(Ptr: std::move(CurrentASTUnit));
1446 } else {
1447 CI.setPreprocessor(nullptr);
1448 CI.setSourceManager(nullptr);
1449 CI.setFileManager(nullptr);
1450 }
1451 }
1452
1453 setCompilerInstance(nullptr);
1454 setCurrentInput(CurrentInput: FrontendInputFile());
1455 CI.getLangOpts().setCompilingModule(LangOptions::CMK_None);
1456}
1457
1458bool FrontendAction::shouldEraseOutputFiles() {
1459 return getCompilerInstance().getDiagnostics().hasErrorOccurred();
1460}
1461
1462//===----------------------------------------------------------------------===//
1463// Utility Actions
1464//===----------------------------------------------------------------------===//
1465
1466void ASTFrontendAction::ExecuteAction() {
1467 CompilerInstance &CI = getCompilerInstance();
1468 if (!CI.hasPreprocessor())
1469 return;
1470 // This is a fallback: If the client forgets to invoke this, we mark the
1471 // current stack as the bottom. Though not optimal, this could help prevent
1472 // stack overflow during deep recursion.
1473 clang::noteBottomOfStack();
1474
1475 // FIXME: Move the truncation aspect of this into Sema, we delayed this till
1476 // here so the source manager would be initialized.
1477 if (hasCodeCompletionSupport() &&
1478 !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
1479 CI.createCodeCompletionConsumer();
1480
1481 // Use a code completion consumer?
1482 CodeCompleteConsumer *CompletionConsumer = nullptr;
1483 if (CI.hasCodeCompletionConsumer())
1484 CompletionConsumer = &CI.getCodeCompletionConsumer();
1485
1486 if (!CI.hasSema())
1487 CI.createSema(TUKind: getTranslationUnitKind(), CompletionConsumer);
1488
1489 ParseAST(S&: CI.getSema(), PrintStats: CI.getFrontendOpts().ShowStats,
1490 SkipFunctionBodies: CI.getFrontendOpts().SkipFunctionBodies);
1491}
1492
1493void PluginASTAction::anchor() { }
1494
1495std::unique_ptr<ASTConsumer>
1496PreprocessorFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1497 StringRef InFile) {
1498 llvm_unreachable("Invalid CreateASTConsumer on preprocessor action!");
1499}
1500
1501bool WrapperFrontendAction::PrepareToExecuteAction(CompilerInstance &CI) {
1502 return WrappedAction->PrepareToExecuteAction(CI);
1503}
1504std::unique_ptr<ASTConsumer>
1505WrapperFrontendAction::CreateASTConsumer(CompilerInstance &CI,
1506 StringRef InFile) {
1507 return WrappedAction->CreateASTConsumer(CI, InFile);
1508}
1509bool WrapperFrontendAction::BeginInvocation(CompilerInstance &CI) {
1510 return WrappedAction->BeginInvocation(CI);
1511}
1512bool WrapperFrontendAction::BeginSourceFileAction(CompilerInstance &CI) {
1513 WrappedAction->setCurrentInput(CurrentInput: getCurrentInput());
1514 WrappedAction->setCompilerInstance(&CI);
1515 auto Ret = WrappedAction->BeginSourceFileAction(CI);
1516 // BeginSourceFileAction may change CurrentInput, e.g. during module builds.
1517 setCurrentInput(CurrentInput: WrappedAction->getCurrentInput());
1518 return Ret;
1519}
1520void WrapperFrontendAction::ExecuteAction() {
1521 WrappedAction->ExecuteAction();
1522}
1523void WrapperFrontendAction::EndSourceFile() { WrappedAction->EndSourceFile(); }
1524void WrapperFrontendAction::EndSourceFileAction() {
1525 WrappedAction->EndSourceFileAction();
1526}
1527bool WrapperFrontendAction::shouldEraseOutputFiles() {
1528 return WrappedAction->shouldEraseOutputFiles();
1529}
1530
1531bool WrapperFrontendAction::usesPreprocessorOnly() const {
1532 return WrappedAction->usesPreprocessorOnly();
1533}
1534TranslationUnitKind WrapperFrontendAction::getTranslationUnitKind() {
1535 return WrappedAction->getTranslationUnitKind();
1536}
1537bool WrapperFrontendAction::hasPCHSupport() const {
1538 return WrappedAction->hasPCHSupport();
1539}
1540bool WrapperFrontendAction::hasASTFileSupport() const {
1541 return WrappedAction->hasASTFileSupport();
1542}
1543bool WrapperFrontendAction::hasIRSupport() const {
1544 return WrappedAction->hasIRSupport();
1545}
1546bool WrapperFrontendAction::hasCodeCompletionSupport() const {
1547 return WrappedAction->hasCodeCompletionSupport();
1548}
1549
1550WrapperFrontendAction::WrapperFrontendAction(
1551 std::unique_ptr<FrontendAction> WrappedAction)
1552 : WrappedAction(std::move(WrappedAction)) {}
1553