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