1//===- ModuleMap.cpp - Describe the layout of modules ---------------------===//
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// This file defines the ModuleMap implementation, which describes the layout
10// of a module as it relates to headers.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/ModuleMap.h"
15#include "clang/Basic/CharInfo.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/FileManager.h"
18#include "clang/Basic/LLVM.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/Module.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Lex/HeaderSearch.h"
25#include "clang/Lex/HeaderSearchOptions.h"
26#include "clang/Lex/LexDiagnostic.h"
27#include "clang/Lex/ModuleMapFile.h"
28#include "llvm/ADT/DenseMap.h"
29#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/SmallPtrSet.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/StringMap.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/ADT/StringSwitch.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/Path.h"
38#include "llvm/Support/VirtualFileSystem.h"
39#include "llvm/Support/raw_ostream.h"
40#include <cassert>
41#include <cstring>
42#include <optional>
43#include <string>
44#include <system_error>
45#include <utility>
46
47using namespace clang;
48
49static constexpr llvm::StringRef kPrivateModuleSuffix = "_Private";
50
51void ModuleMapCallbacks::anchor() {}
52
53void ModuleMap::resolveLinkAsDependencies(Module *Mod) {
54 auto PendingLinkAs = PendingLinkAsModule.find(Key: Mod->Name);
55 if (PendingLinkAs != PendingLinkAsModule.end()) {
56 for (auto &Name : PendingLinkAs->second) {
57 auto *M = findModule(Name: Name.getKey());
58 if (M)
59 M->UseExportAsModuleLinkName = true;
60 }
61 }
62}
63
64void ModuleMap::addLinkAsDependency(Module *Mod) {
65 if (findModule(Name: Mod->ExportAsModule))
66 Mod->UseExportAsModuleLinkName = true;
67 else
68 PendingLinkAsModule[Mod->ExportAsModule].insert(key: Mod->Name);
69}
70
71Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) {
72 switch ((int)Role) {
73 case NormalHeader:
74 return Module::HK_Normal;
75 case PrivateHeader:
76 return Module::HK_Private;
77 case TextualHeader:
78 return Module::HK_Textual;
79 case PrivateHeader | TextualHeader:
80 return Module::HK_PrivateTextual;
81 case ExcludedHeader:
82 return Module::HK_Excluded;
83 }
84 llvm_unreachable("unknown header role");
85}
86
87ModuleMap::ModuleHeaderRole
88ModuleMap::headerKindToRole(Module::HeaderKind Kind) {
89 switch ((int)Kind) {
90 case Module::HK_Normal:
91 return NormalHeader;
92 case Module::HK_Private:
93 return PrivateHeader;
94 case Module::HK_Textual:
95 return TextualHeader;
96 case Module::HK_PrivateTextual:
97 return ModuleHeaderRole(PrivateHeader | TextualHeader);
98 case Module::HK_Excluded:
99 return ExcludedHeader;
100 }
101 llvm_unreachable("unknown header kind");
102}
103
104bool ModuleMap::isModular(ModuleHeaderRole Role) {
105 return !(Role & (ModuleMap::TextualHeader | ModuleMap::ExcludedHeader));
106}
107
108Module::ExportDecl
109ModuleMap::resolveExport(Module *Mod,
110 const Module::UnresolvedExportDecl &Unresolved,
111 bool Complain) const {
112 // We may have just a wildcard.
113 if (Unresolved.Id.empty()) {
114 assert(Unresolved.Wildcard && "Invalid unresolved export");
115 return Module::ExportDecl(nullptr, true);
116 }
117
118 // Resolve the module-id.
119 Module *Context = resolveModuleId(Id: Unresolved.Id, Mod, Complain);
120 if (!Context)
121 return {};
122
123 return Module::ExportDecl(Context, Unresolved.Wildcard);
124}
125
126Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
127 bool Complain) const {
128 // Find the starting module.
129 Module *Context = lookupModuleUnqualified(Name: Id[0].first, Context: Mod);
130 if (!Context) {
131 if (Complain)
132 Diags.Report(Loc: Id[0].second, DiagID: diag::err_mmap_missing_module_unqualified)
133 << Id[0].first << Mod->getFullModuleName();
134
135 return nullptr;
136 }
137
138 // Dig into the module path.
139 for (unsigned I = 1, N = Id.size(); I != N; ++I) {
140 Module *Sub = lookupModuleQualified(Name: Id[I].first, Context);
141 if (!Sub) {
142 if (Complain)
143 Diags.Report(Loc: Id[I].second, DiagID: diag::err_mmap_missing_module_qualified)
144 << Id[I].first << Context->getFullModuleName()
145 << SourceRange(Id[0].second, Id[I-1].second);
146
147 return nullptr;
148 }
149
150 Context = Sub;
151 }
152
153 return Context;
154}
155
156/// Append to \p Paths the set of paths needed to get to the
157/// subframework in which the given module lives.
158static void appendSubframeworkPaths(Module *Mod,
159 SmallVectorImpl<char> &Path) {
160 // Collect the framework names from the given module to the top-level module.
161 SmallVector<StringRef, 2> Paths;
162 for (; Mod; Mod = Mod->Parent) {
163 if (Mod->IsFramework)
164 Paths.push_back(Elt: Mod->Name);
165 }
166
167 if (Paths.empty())
168 return;
169
170 // Add Frameworks/Name.framework for each subframework.
171 for (StringRef Framework : llvm::drop_begin(RangeOrContainer: llvm::reverse(C&: Paths)))
172 llvm::sys::path::append(path&: Path, a: "Frameworks", b: Framework + ".framework");
173}
174
175OptionalFileEntryRef ModuleMap::findHeader(
176 Module *M, const Module::UnresolvedHeaderDirective &Header,
177 SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) {
178 auto GetFile = [&](StringRef Filename) -> OptionalFileEntryRef {
179 auto File = SourceMgr.getFileManager().getOptionalFileRef(Filename);
180 if (!File || (Header.Size && File->getSize() != *Header.Size) ||
181 (Header.ModTime && File->getModificationTime() != *Header.ModTime))
182 return std::nullopt;
183 return *File;
184 };
185
186 if (llvm::sys::path::is_absolute(path: Header.FileName)) {
187 RelativePathName.clear();
188 RelativePathName.append(in_start: Header.FileName.begin(), in_end: Header.FileName.end());
189 return GetFile(Header.FileName);
190 }
191
192 // Search for the header file within the module's home directory.
193 auto Directory = M->Directory;
194 if (!Directory)
195 return std::nullopt;
196 SmallString<128> FullPathName(Directory->getName());
197
198 auto GetFrameworkFile = [&]() -> OptionalFileEntryRef {
199 unsigned FullPathLength = FullPathName.size();
200 appendSubframeworkPaths(Mod: M, Path&: RelativePathName);
201 unsigned RelativePathLength = RelativePathName.size();
202
203 // Check whether this file is in the public headers.
204 llvm::sys::path::append(path&: RelativePathName, a: "Headers", b: Header.FileName);
205 llvm::sys::path::append(path&: FullPathName, a: RelativePathName);
206 if (auto File = GetFile(FullPathName))
207 return File;
208
209 // Check whether this file is in the private headers.
210 // Ideally, private modules in the form 'FrameworkName.Private' should
211 // be defined as 'module FrameworkName.Private', and not as
212 // 'framework module FrameworkName.Private', since a 'Private.Framework'
213 // does not usually exist. However, since both are currently widely used
214 // for private modules, make sure we find the right path in both cases.
215 if (M->IsFramework && M->Name == "Private")
216 RelativePathName.clear();
217 else
218 RelativePathName.resize(N: RelativePathLength);
219 FullPathName.resize(N: FullPathLength);
220 llvm::sys::path::append(path&: RelativePathName, a: "PrivateHeaders",
221 b: Header.FileName);
222 llvm::sys::path::append(path&: FullPathName, a: RelativePathName);
223 return GetFile(FullPathName);
224 };
225
226 if (M->isPartOfFramework())
227 return GetFrameworkFile();
228
229 // Lookup for normal headers.
230 llvm::sys::path::append(path&: RelativePathName, a: Header.FileName);
231 llvm::sys::path::append(path&: FullPathName, a: RelativePathName);
232 auto NormalHdrFile = GetFile(FullPathName);
233
234 if (!NormalHdrFile && Directory->getName().ends_with(Suffix: ".framework")) {
235 // The lack of 'framework' keyword in a module declaration it's a simple
236 // mistake we can diagnose when the header exists within the proper
237 // framework style path.
238 FullPathName.assign(RHS: Directory->getName());
239 RelativePathName.clear();
240 if (GetFrameworkFile()) {
241 Diags.Report(Loc: Header.FileNameLoc,
242 DiagID: diag::warn_mmap_incomplete_framework_module_declaration)
243 << Header.FileName << M->getFullModuleName();
244 NeedsFramework = true;
245 }
246 return std::nullopt;
247 }
248
249 return NormalHdrFile;
250}
251
252/// Determine whether the given file name is the name of a builtin
253/// header, supplied by Clang to replace, override, or augment existing system
254/// headers.
255static bool isBuiltinHeaderName(StringRef FileName) {
256 return llvm::StringSwitch<bool>(FileName)
257 .Case(S: "float.h", Value: true)
258 .Case(S: "iso646.h", Value: true)
259 .Case(S: "limits.h", Value: true)
260 .Case(S: "stdalign.h", Value: true)
261 .Case(S: "stdarg.h", Value: true)
262 .Case(S: "stdatomic.h", Value: true)
263 .Case(S: "stdbool.h", Value: true)
264 .Case(S: "stdckdint.h", Value: true)
265 .Case(S: "stdcountof.h", Value: true)
266 .Case(S: "stddef.h", Value: true)
267 .Case(S: "stdint.h", Value: true)
268 .Case(S: "tgmath.h", Value: true)
269 .Case(S: "unwind.h", Value: true)
270 .Default(Value: false);
271}
272
273/// Determine whether the given module name is the name of a builtin
274/// module that is cyclic with a system module on some platforms.
275static bool isBuiltInModuleName(StringRef ModuleName) {
276 return llvm::StringSwitch<bool>(ModuleName)
277 .Case(S: "_Builtin_float", Value: true)
278 .Case(S: "_Builtin_inttypes", Value: true)
279 .Case(S: "_Builtin_iso646", Value: true)
280 .Case(S: "_Builtin_limits", Value: true)
281 .Case(S: "_Builtin_stdalign", Value: true)
282 .Case(S: "_Builtin_stdarg", Value: true)
283 .Case(S: "_Builtin_stdatomic", Value: true)
284 .Case(S: "_Builtin_stdbool", Value: true)
285 .Case(S: "_Builtin_stddef", Value: true)
286 .Case(S: "_Builtin_stdint", Value: true)
287 .Case(S: "_Builtin_stdnoreturn", Value: true)
288 .Case(S: "_Builtin_tgmath", Value: true)
289 .Case(S: "_Builtin_unwind", Value: true)
290 .Default(Value: false);
291}
292
293void ModuleMap::resolveHeader(Module *Mod,
294 const Module::UnresolvedHeaderDirective &Header,
295 bool &NeedsFramework) {
296 SmallString<128> RelativePathName;
297 if (OptionalFileEntryRef File =
298 findHeader(M: Mod, Header, RelativePathName, NeedsFramework)) {
299 if (Header.IsUmbrella) {
300 const DirectoryEntry *UmbrellaDir = &File->getDir().getDirEntry();
301 if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir])
302 Diags.Report(Loc: Header.FileNameLoc, DiagID: diag::err_mmap_umbrella_clash)
303 << UmbrellaMod->getFullModuleName();
304 else
305 // Record this umbrella header.
306 setUmbrellaHeaderAsWritten(Mod, UmbrellaHeader: *File, NameAsWritten: Header.FileName,
307 PathRelativeToRootModuleDirectory: RelativePathName.str(), Loc: Header.FileNameLoc);
308 } else {
309 Module::Header H = {.NameAsWritten: Header.FileName, .PathRelativeToRootModuleDirectory: std::string(RelativePathName),
310 .Entry: *File};
311 addHeader(Mod, Header: H, Role: headerKindToRole(Kind: Header.Kind), /*Imported=*/false,
312 Loc: Header.FileNameLoc);
313 }
314 } else if (Header.HasBuiltinHeader && !Header.Size && !Header.ModTime) {
315 // There's a builtin header but no corresponding on-disk header. Assume
316 // this was supposed to modularize the builtin header alone.
317 } else if (Header.Kind == Module::HK_Excluded) {
318 // Ignore missing excluded header files. They're optional anyway.
319 } else {
320 // If we find a module that has a missing header, we mark this module as
321 // unavailable and store the header directive for displaying diagnostics.
322 Mod->MissingHeaders.push_back(Elt: Header);
323 // A missing header with stat information doesn't make the module
324 // unavailable; this keeps our behavior consistent as headers are lazily
325 // resolved. (Such a module still can't be built though, except from
326 // preprocessed source.)
327 if (!Header.Size && !Header.ModTime)
328 Mod->markUnavailable(/*Unimportable=*/false);
329 }
330}
331
332bool ModuleMap::resolveAsBuiltinHeader(
333 Module *Mod, const Module::UnresolvedHeaderDirective &Header) {
334 if (Header.Kind == Module::HK_Excluded ||
335 llvm::sys::path::is_absolute(path: Header.FileName) ||
336 Mod->isPartOfFramework() || !Mod->IsSystem || Header.IsUmbrella ||
337 !BuiltinIncludeDir || BuiltinIncludeDir == Mod->Directory ||
338 !LangOpts.BuiltinHeadersInSystemModules || !isBuiltinHeaderName(FileName: Header.FileName))
339 return false;
340
341 // This is a system module with a top-level header. This header
342 // may have a counterpart (or replacement) in the set of headers
343 // supplied by Clang. Find that builtin header.
344 SmallString<128> Path;
345 llvm::sys::path::append(path&: Path, a: BuiltinIncludeDir->getName(), b: Header.FileName);
346 auto File = SourceMgr.getFileManager().getOptionalFileRef(Filename: Path);
347 if (!File)
348 return false;
349
350 Module::Header H = {.NameAsWritten: Header.FileName, .PathRelativeToRootModuleDirectory: Header.FileName, .Entry: *File};
351 auto Role = headerKindToRole(Kind: Header.Kind);
352 addHeader(Mod, Header: H, Role);
353 return true;
354}
355
356ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
357 const LangOptions &LangOpts, const TargetInfo *Target,
358 HeaderSearch &HeaderInfo)
359 : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
360 HeaderInfo(HeaderInfo) {
361}
362
363ModuleMap::~ModuleMap() = default;
364
365void ModuleMap::setTarget(const TargetInfo &Target) {
366 assert((!this->Target || this->Target == &Target) &&
367 "Improper target override");
368 this->Target = &Target;
369}
370
371/// "Sanitize" a filename so that it can be used as an identifier.
372static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
373 SmallVectorImpl<char> &Buffer) {
374 if (Name.empty())
375 return Name;
376
377 if (!isValidAsciiIdentifier(S: Name)) {
378 // If we don't already have something with the form of an identifier,
379 // create a buffer with the sanitized name.
380 Buffer.clear();
381 if (isDigit(c: Name[0]))
382 Buffer.push_back(Elt: '_');
383 Buffer.reserve(N: Buffer.size() + Name.size());
384 for (unsigned I = 0, N = Name.size(); I != N; ++I) {
385 if (isAsciiIdentifierContinue(c: Name[I]))
386 Buffer.push_back(Elt: Name[I]);
387 else
388 Buffer.push_back(Elt: '_');
389 }
390
391 Name = StringRef(Buffer.data(), Buffer.size());
392 }
393
394 while (llvm::StringSwitch<bool>(Name)
395#define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
396#define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
397#include "clang/Basic/TokenKinds.def"
398 .Default(Value: false)) {
399 if (Name.data() != Buffer.data())
400 Buffer.append(in_start: Name.begin(), in_end: Name.end());
401 Buffer.push_back(Elt: '_');
402 Name = StringRef(Buffer.data(), Buffer.size());
403 }
404
405 return Name;
406}
407
408bool ModuleMap::isBuiltinHeader(FileEntryRef File) {
409 return File.getDir() == BuiltinIncludeDir && LangOpts.BuiltinHeadersInSystemModules &&
410 isBuiltinHeaderName(FileName: llvm::sys::path::filename(path: File.getName()));
411}
412
413bool ModuleMap::shouldImportRelativeToBuiltinIncludeDir(StringRef FileName,
414 Module *Module) const {
415 return LangOpts.BuiltinHeadersInSystemModules && BuiltinIncludeDir &&
416 Module->IsSystem && !Module->isPartOfFramework() &&
417 isBuiltinHeaderName(FileName);
418}
419
420ModuleMap::HeadersMap::iterator ModuleMap::findKnownHeader(FileEntryRef File) {
421 resolveHeaderDirectives(File);
422 HeadersMap::iterator Known = Headers.find(Val: File);
423 if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
424 Known == Headers.end() && ModuleMap::isBuiltinHeader(File)) {
425 HeaderInfo.loadTopLevelSystemModules();
426 return Headers.find(Val: File);
427 }
428 return Known;
429}
430
431ModuleMap::KnownHeader ModuleMap::findHeaderInUmbrellaDirs(
432 FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs) {
433 if (UmbrellaDirs.empty())
434 return {};
435
436 OptionalDirectoryEntryRef Dir = File.getDir();
437
438 // Note: as an egregious but useful hack we use the real path here, because
439 // frameworks moving from top-level frameworks to embedded frameworks tend
440 // to be symlinked from the top-level location to the embedded location,
441 // and we need to resolve lookups as if we had found the embedded location.
442 StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir: *Dir);
443
444 // Keep walking up the directory hierarchy, looking for a directory with
445 // an umbrella header.
446 do {
447 auto KnownDir = UmbrellaDirs.find(Val: *Dir);
448 if (KnownDir != UmbrellaDirs.end())
449 return KnownHeader(KnownDir->second, NormalHeader);
450
451 IntermediateDirs.push_back(Elt: *Dir);
452
453 // Retrieve our parent path.
454 DirName = llvm::sys::path::parent_path(path: DirName);
455 if (DirName.empty())
456 break;
457
458 // Resolve the parent path to a directory entry.
459 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName);
460 } while (Dir);
461 return {};
462}
463
464static bool violatesPrivateInclude(Module *RequestingModule,
465 const FileEntry *IncFileEnt,
466 ModuleMap::KnownHeader Header) {
467#ifndef NDEBUG
468 if (Header.getRole() & ModuleMap::PrivateHeader) {
469 // Check for consistency between the module header role
470 // as obtained from the lookup and as obtained from the module.
471 // This check is not cheap, so enable it only for debugging.
472 bool IsPrivate = false;
473 ArrayRef<Module::Header> HeaderList[] = {
474 Header.getModule()->getHeaders(Module::HK_Private),
475 Header.getModule()->getHeaders(Module::HK_PrivateTextual)};
476 for (auto Hs : HeaderList)
477 IsPrivate |= llvm::any_of(
478 Hs, [&](const Module::Header &H) { return H.Entry == IncFileEnt; });
479 assert(IsPrivate && "inconsistent headers and roles");
480 }
481#endif
482 return !Header.isAccessibleFrom(M: RequestingModule);
483}
484
485static Module *getTopLevelOrNull(Module *M) {
486 return M ? M->getTopLevelModule() : nullptr;
487}
488
489void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
490 bool RequestingModuleIsModuleInterface,
491 SourceLocation FilenameLoc,
492 StringRef Filename, FileEntryRef File) {
493 if (RequestingModule) {
494 resolveUses(Mod: RequestingModule, /*Complain=*/false);
495 resolveHeaderDirectives(Mod: RequestingModule, /*File=*/std::nullopt);
496 }
497
498 HeadersMap::iterator Known = findKnownHeader(File);
499
500 diagnoseDuplicateHeaderOwnership(FilenameLoc, Filename, File, Known);
501
502 // No errors for indirect modules. This may be a bit of a problem for modules
503 // with no source files.
504 Module *TopLevelRequestingModule = getTopLevelOrNull(M: RequestingModule);
505 Module *TopLevelSourceModule = getTopLevelOrNull(M: SourceModule);
506 bool IsPublicForMainPrivateModule = false;
507 if (TopLevelRequestingModule != TopLevelSourceModule) {
508 // Suppose we have a pair of files foo.cpp / foo.h.
509 // Our build system may want to verify that foo.cpp only uses things
510 // declared in the implementation_deps of foo, while foo.h only uses things
511 // declared in interface_deps. This requires them to be two seperate
512 // modules, foo_Private and foo. This check is required to ensure that foo.h
513 // is still checked. Otherwise, foo.h would never be checked, since it will
514 // never be the top-level module.
515 if (TopLevelRequestingModule && TopLevelSourceModule &&
516 llvm::StringRef(TopLevelSourceModule->Name)
517 .ends_with(Suffix: kPrivateModuleSuffix) &&
518 llvm::StringRef(TopLevelSourceModule->Name)
519 .drop_back(N: kPrivateModuleSuffix.size()) ==
520 TopLevelRequestingModule->Name) {
521 IsPublicForMainPrivateModule = true;
522 } else {
523 return;
524 }
525 }
526
527 bool Excluded = false;
528 bool UsedByPrivateModule = false;
529 Module *Private = nullptr;
530 Module *NotUsed = nullptr;
531
532 if (Known != Headers.end()) {
533 for (const KnownHeader &Header : Known->second) {
534 // Excluded headers don't really belong to a module.
535 if (Header.getRole() == ModuleMap::ExcludedHeader) {
536 Excluded = true;
537 continue;
538 }
539
540 // Remember private headers for later printing of a diagnostic.
541 if (violatesPrivateInclude(RequestingModule, IncFileEnt: File, Header)) {
542 Private = Header.getModule();
543 continue;
544 }
545
546 // If uses need to be specified explicitly, we are only allowed to return
547 // modules that are explicitly used by the requesting module.
548 if (RequestingModule && LangOpts.ModulesDeclUse &&
549 !RequestingModule->directlyUses(Requested: Header.getModule())) {
550 NotUsed = Header.getModule();
551 if (IsPublicForMainPrivateModule) {
552 UsedByPrivateModule = SourceModule->directlyUses(Requested: Header.getModule());
553 }
554 continue;
555 }
556
557 // We have found a module that we can happily use.
558 return;
559 }
560
561 Excluded = true;
562 }
563
564 // We have found a header, but it is private.
565 if (Private) {
566 Diags.Report(Loc: FilenameLoc, DiagID: diag::warn_use_of_private_header_outside_module)
567 << Filename;
568 return;
569 }
570
571 // We have found a module, but we don't use it.
572 if (NotUsed) {
573 if (UsedByPrivateModule) {
574 Diags.Report(Loc: FilenameLoc, DiagID: diag::err_undeclared_use_of_module_private)
575 << RequestingModule->getTopLevelModule()->Name << Filename
576 << NotUsed->Name;
577 } else {
578 Diags.Report(Loc: FilenameLoc, DiagID: diag::err_undeclared_use_of_module_indirect)
579 << RequestingModule->getTopLevelModule()->Name << Filename
580 << NotUsed->Name;
581 }
582 return;
583 }
584
585 if (Excluded || isHeaderInUmbrellaDirs(File))
586 return;
587
588 // At this point, only non-modular includes remain.
589
590 if (RequestingModule && LangOpts.ModulesStrictDeclUse) {
591 Diags.Report(Loc: FilenameLoc, DiagID: diag::err_undeclared_use_of_module)
592 << RequestingModule->getTopLevelModule()->Name << Filename;
593 } else if (RequestingModule && RequestingModuleIsModuleInterface &&
594 LangOpts.isCompilingModule()) {
595 // Do not diagnose when we are not compiling a module.
596 diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
597 diag::warn_non_modular_include_in_framework_module :
598 diag::warn_non_modular_include_in_module;
599 Diags.Report(Loc: FilenameLoc, DiagID) << RequestingModule->getFullModuleName()
600 << File.getName();
601 }
602}
603
604void ModuleMap::diagnoseDuplicateHeaderOwnership(SourceLocation FilenameLoc,
605 StringRef Filename,
606 FileEntryRef File,
607 HeadersMap::iterator Known) {
608 if (Known == Headers.end())
609 return;
610
611 if (Diags.isIgnored(DiagID: diag::warn_mmap_duplicate_header_ownership, Loc: FilenameLoc))
612 return;
613
614 // Only diagnose each header once.
615 if (!DiagnosedDuplicateHeaders.insert(V: &File.getFileEntry()).second)
616 return;
617
618 struct OwnerInfo {
619 Module *Mod;
620 SourceLocation Loc;
621 bool IsUmbrella;
622 };
623
624 // Collect distinct top-level modules that explicitly own this header with
625 // a modular (non-textual, non-excluded) role.
626 SmallVector<OwnerInfo, 2> OwningModules;
627 llvm::SmallPtrSet<Module *, 2> SeenTopLevel;
628 for (const KnownHeader &H : Known->second) {
629 if (!isModular(Role: H.getRole()))
630 continue;
631 Module *TopLevel = H.getModule()->getTopLevelModule();
632 if (!SeenTopLevel.insert(Ptr: TopLevel).second)
633 continue;
634 auto It = HeaderOwnerLocs.find(Val: {&File.getFileEntry(), H.getModule()});
635 SourceLocation OwnerLoc =
636 It != HeaderOwnerLocs.end() ? It->second : SourceLocation();
637 OwningModules.push_back(Elt: {.Mod: TopLevel, .Loc: OwnerLoc, /*IsUmbrella=*/false});
638 }
639
640 // Need at least one explicit owner for there to be a conflict, since
641 // umbrella coverage can only add one more.
642 if (OwningModules.empty())
643 return;
644
645 // Also check umbrella directory coverage for additional owners from different
646 // top-level modules — but only if the header isn't excluded from the umbrella
647 // module. Explicit headers take precedence over umbrella dirs in module
648 // resolution, but a header owned by one module that another module's umbrella
649 // covers can still create problems.
650 SmallVector<DirectoryEntryRef, 2> IntermediateDirs;
651 if (KnownHeader UmbrellaOwner =
652 findHeaderInUmbrellaDirs(File, IntermediateDirs)) {
653 Module *TopLevel = UmbrellaOwner.getModule()->getTopLevelModule();
654 // Only add if it's a different top-level module and the header isn't
655 // excluded from the umbrella module.
656 if (SeenTopLevel.insert(Ptr: TopLevel).second) {
657 // Check that the header isn't excluded in the umbrella module.
658 bool IsExcluded =
659 llvm::any_of(Range&: Known->second, P: [TopLevel](const KnownHeader &H) {
660 return H.getModule()->getTopLevelModule() == TopLevel &&
661 H.getRole() == ExcludedHeader;
662 });
663 if (!IsExcluded) {
664 OwningModules.push_back(Elt: {.Mod: TopLevel,
665 .Loc: UmbrellaOwner.getModule()->UmbrellaDeclLoc,
666 /*IsUmbrella=*/true});
667 }
668 }
669 }
670
671 if (OwningModules.size() < 2)
672 return;
673
674 Diags.Report(Loc: FilenameLoc, DiagID: diag::warn_mmap_duplicate_header_ownership)
675 << Filename;
676 for (const auto &Owner : OwningModules) {
677 unsigned NoteID = Owner.IsUmbrella
678 ? diag::note_mmap_header_covered_by_umbrella
679 : diag::note_mmap_header_owned_by;
680 Diags.Report(Loc: Owner.Loc, DiagID: NoteID) << Owner.Mod->getFullModuleName();
681 }
682}
683
684static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
685 const ModuleMap::KnownHeader &Old) {
686 // Prefer available modules.
687 // FIXME: Considering whether the module is available rather than merely
688 // importable is non-hermetic and can result in surprising behavior for
689 // prebuilt modules. Consider only checking for importability here.
690 if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable())
691 return true;
692
693 // Prefer a public header over a private header.
694 if ((New.getRole() & ModuleMap::PrivateHeader) !=
695 (Old.getRole() & ModuleMap::PrivateHeader))
696 return !(New.getRole() & ModuleMap::PrivateHeader);
697
698 // Prefer a non-textual header over a textual header.
699 if ((New.getRole() & ModuleMap::TextualHeader) !=
700 (Old.getRole() & ModuleMap::TextualHeader))
701 return !(New.getRole() & ModuleMap::TextualHeader);
702
703 // Prefer a non-excluded header over an excluded header.
704 if ((New.getRole() == ModuleMap::ExcludedHeader) !=
705 (Old.getRole() == ModuleMap::ExcludedHeader))
706 return New.getRole() != ModuleMap::ExcludedHeader;
707
708 // Don't have a reason to choose between these. Just keep the first one.
709 return false;
710}
711
712ModuleMap::KnownHeader ModuleMap::findModuleForHeader(FileEntryRef File,
713 bool AllowTextual,
714 bool AllowExcluded) {
715 auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
716 if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader)
717 return {};
718 return R;
719 };
720
721 HeadersMap::iterator Known = findKnownHeader(File);
722 if (Known != Headers.end()) {
723 ModuleMap::KnownHeader Result;
724 // Iterate over all modules that 'File' is part of to find the best fit.
725 for (KnownHeader &H : Known->second) {
726 // Cannot use a module if the header is excluded in it.
727 if (!AllowExcluded && H.getRole() == ModuleMap::ExcludedHeader)
728 continue;
729 // Prefer a header from the source module over all others.
730 if (H.getModule()->getTopLevelModule() == SourceModule)
731 return MakeResult(H);
732 if (!Result || isBetterKnownHeader(New: H, Old: Result))
733 Result = H;
734 }
735 return MakeResult(Result);
736 }
737
738 return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File));
739}
740
741OptionalFileEntryRef ModuleMap::findUmbrellaHeaderForModule(
742 Module *M, std::string NameAsWritten,
743 SmallVectorImpl<char> &RelativePathName) {
744 Module::UnresolvedHeaderDirective Header;
745 Header.FileName = std::move(NameAsWritten);
746 Header.IsUmbrella = true;
747 bool NeedsFramework;
748 return findHeader(M, Header, RelativePathName, NeedsFramework);
749}
750
751ModuleMap::KnownHeader
752ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) {
753 assert(!Headers.count(File) && "already have a module for this header");
754
755 SmallVector<DirectoryEntryRef, 2> SkippedDirs;
756 KnownHeader H = findHeaderInUmbrellaDirs(File, IntermediateDirs&: SkippedDirs);
757 if (H) {
758 Module *Result = H.getModule();
759
760 // Search up the module stack until we find a module with an umbrella
761 // directory.
762 Module *UmbrellaModule = Result;
763 while (!UmbrellaModule->getEffectiveUmbrellaDir() && UmbrellaModule->Parent)
764 UmbrellaModule = UmbrellaModule->Parent;
765
766 if (UmbrellaModule->InferSubmodules) {
767 FileID UmbrellaModuleMap = getModuleMapFileIDForUniquing(M: UmbrellaModule);
768
769 // Infer submodules for each of the directories we found between
770 // the directory of the umbrella header and the directory where
771 // the actual header is located.
772 bool Explicit = UmbrellaModule->InferExplicitSubmodules;
773
774 for (DirectoryEntryRef SkippedDir : llvm::reverse(C&: SkippedDirs)) {
775 // Find or create the module that corresponds to this directory name.
776 SmallString<32> NameBuf;
777 StringRef Name = sanitizeFilenameAsIdentifier(
778 Name: llvm::sys::path::stem(path: SkippedDir.getName()), Buffer&: NameBuf);
779 Result = findOrCreateModuleFirst(Name, Parent: Result, /*IsFramework=*/false,
780 IsExplicit: Explicit);
781 setInferredModuleAllowedBy(M: Result, ModMapFID: UmbrellaModuleMap);
782
783 // Associate the module and the directory.
784 UmbrellaDirs[SkippedDir] = Result;
785
786 // If inferred submodules export everything they import, add a
787 // wildcard to the set of exports.
788 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
789 Result->Exports.push_back(Elt: Module::ExportDecl(nullptr, true));
790 }
791
792 // Infer a submodule with the same name as this header file.
793 SmallString<32> NameBuf;
794 StringRef Name = sanitizeFilenameAsIdentifier(
795 Name: llvm::sys::path::stem(path: File.getName()), Buffer&: NameBuf);
796 Result = findOrCreateModuleFirst(Name, Parent: Result, /*IsFramework=*/false,
797 IsExplicit: Explicit);
798 setInferredModuleAllowedBy(M: Result, ModMapFID: UmbrellaModuleMap);
799 Result->addTopHeader(File);
800
801 // If inferred submodules export everything they import, add a
802 // wildcard to the set of exports.
803 if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
804 Result->Exports.push_back(Elt: Module::ExportDecl(nullptr, true));
805 } else {
806 // Record each of the directories we stepped through as being part of
807 // the module we found, since the umbrella header covers them all.
808 for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
809 UmbrellaDirs[SkippedDirs[I]] = Result;
810 }
811
812 KnownHeader Header(Result, NormalHeader);
813 Headers[File].push_back(Elt: Header);
814 return Header;
815 }
816
817 return {};
818}
819
820ArrayRef<ModuleMap::KnownHeader>
821ModuleMap::findAllModulesForHeader(FileEntryRef File) {
822 HeadersMap::iterator Known = findKnownHeader(File);
823 if (Known != Headers.end())
824 return Known->second;
825
826 if (findOrCreateModuleForHeaderInUmbrellaDir(File))
827 return Headers.find(Val: File)->second;
828
829 return {};
830}
831
832ArrayRef<ModuleMap::KnownHeader>
833ModuleMap::findResolvedModulesForHeader(FileEntryRef File) const {
834 // FIXME: Is this necessary?
835 resolveHeaderDirectives(File);
836 auto It = Headers.find(Val: File);
837 if (It == Headers.end())
838 return {};
839 return It->second;
840}
841
842bool ModuleMap::isHeaderInUnavailableModule(FileEntryRef Header) const {
843 return isHeaderUnavailableInModule(Header, RequestingModule: nullptr);
844}
845
846bool ModuleMap::isHeaderUnavailableInModule(
847 FileEntryRef Header, const Module *RequestingModule) const {
848 resolveHeaderDirectives(File: Header);
849 HeadersMap::const_iterator Known = Headers.find(Val: Header);
850 if (Known != Headers.end()) {
851 for (SmallVectorImpl<KnownHeader>::const_iterator
852 I = Known->second.begin(),
853 E = Known->second.end();
854 I != E; ++I) {
855
856 if (I->getRole() == ModuleMap::ExcludedHeader)
857 continue;
858
859 if (I->isAvailable() &&
860 (!RequestingModule ||
861 I->getModule()->isSubModuleOf(Other: RequestingModule))) {
862 // When no requesting module is available, the caller is looking if a
863 // header is part a module by only looking into the module map. This is
864 // done by warn_uncovered_module_header checks; don't consider textual
865 // headers part of it in this mode, otherwise we get misleading warnings
866 // that a umbrella header is not including a textual header.
867 if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader)
868 continue;
869 return false;
870 }
871 }
872 return true;
873 }
874
875 OptionalDirectoryEntryRef Dir = Header.getDir();
876 SmallVector<DirectoryEntryRef, 2> SkippedDirs;
877 StringRef DirName = Dir->getName();
878
879 auto IsUnavailable = [&](const Module *M) {
880 return !M->isAvailable() && (!RequestingModule ||
881 M->isSubModuleOf(Other: RequestingModule));
882 };
883
884 // Keep walking up the directory hierarchy, looking for a directory with
885 // an umbrella header.
886 do {
887 auto KnownDir = UmbrellaDirs.find(Val: *Dir);
888 if (KnownDir != UmbrellaDirs.end()) {
889 Module *Found = KnownDir->second;
890 if (IsUnavailable(Found))
891 return true;
892
893 // Search up the module stack until we find a module with an umbrella
894 // directory.
895 Module *UmbrellaModule = Found;
896 while (!UmbrellaModule->getEffectiveUmbrellaDir() &&
897 UmbrellaModule->Parent)
898 UmbrellaModule = UmbrellaModule->Parent;
899
900 if (UmbrellaModule->InferSubmodules) {
901 for (DirectoryEntryRef SkippedDir : llvm::reverse(C&: SkippedDirs)) {
902 // Find or create the module that corresponds to this directory name.
903 SmallString<32> NameBuf;
904 StringRef Name = sanitizeFilenameAsIdentifier(
905 Name: llvm::sys::path::stem(path: SkippedDir.getName()), Buffer&: NameBuf);
906 Found = lookupModuleQualified(Name, Context: Found);
907 if (!Found)
908 return false;
909 if (IsUnavailable(Found))
910 return true;
911 }
912
913 // Infer a submodule with the same name as this header file.
914 SmallString<32> NameBuf;
915 StringRef Name = sanitizeFilenameAsIdentifier(
916 Name: llvm::sys::path::stem(path: Header.getName()),
917 Buffer&: NameBuf);
918 Found = lookupModuleQualified(Name, Context: Found);
919 if (!Found)
920 return false;
921 }
922
923 return IsUnavailable(Found);
924 }
925
926 SkippedDirs.push_back(Elt: *Dir);
927
928 // Retrieve our parent path.
929 DirName = llvm::sys::path::parent_path(path: DirName);
930 if (DirName.empty())
931 break;
932
933 // Resolve the parent path to a directory entry.
934 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName);
935 } while (Dir);
936
937 return false;
938}
939
940Module *ModuleMap::findModule(StringRef Name) const {
941 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Key: Name);
942 if (Known != Modules.end())
943 return Known->getValue();
944
945 return nullptr;
946}
947
948Module *ModuleMap::findOrInferSubmodule(Module *Parent, StringRef Name) {
949 if (Module *SubM = Parent->findSubmodule(Name))
950 return SubM;
951 if (!Parent->InferSubmodules)
952 return nullptr;
953 Module *Result = new (ModulesAlloc.Allocate())
954 Module(ModuleConstructorTag{}, Name, SourceLocation(), Parent, false,
955 Parent->InferExplicitSubmodules, 0);
956 Result->InferExplicitSubmodules = Parent->InferExplicitSubmodules;
957 Result->InferSubmodules = Parent->InferSubmodules;
958 Result->InferExportWildcard = Parent->InferExportWildcard;
959 if (Result->InferExportWildcard)
960 Result->Exports.push_back(Elt: Module::ExportDecl(nullptr, true));
961 return Result;
962}
963
964Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
965 Module *Context) const {
966 for(; Context; Context = Context->Parent) {
967 if (Module *Sub = lookupModuleQualified(Name, Context))
968 return Sub;
969 }
970
971 return findModule(Name);
972}
973
974ModuleRef ModuleMap::lookupModuleQualified(StringRef Name,
975 Module *Context) const {
976 if (!Context)
977 return findModule(Name);
978
979 return Context->findSubmodule(Name);
980}
981
982std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
983 Module *Parent,
984 bool IsFramework,
985 bool IsExplicit) {
986 // Try to find an existing module with this name.
987 if (ModuleRef Sub = lookupModuleQualified(Name, Context: Parent); Sub.getExisting())
988 return std::make_pair(x: Sub.getExisting(), y: false);
989
990 // Create a new module with this name.
991 Module *M = createModule(Name, Parent, IsFramework, IsExplicit);
992 return std::make_pair(x&: M, y: true);
993}
994
995Module *ModuleMap::createModule(StringRef Name, Module *Parent,
996 bool IsFramework, bool IsExplicit) {
997 assert(!lookupModuleQualified(Name, Parent).getExisting() &&
998 "Creating duplicate submodule");
999
1000 Module *Result = new (ModulesAlloc.Allocate())
1001 Module(ModuleConstructorTag{}, Name, SourceLocation(), Parent,
1002 IsFramework, IsExplicit, NumCreatedModules++);
1003 if (!Parent) {
1004 if (LangOpts.CurrentModule == Name)
1005 SourceModule = Result;
1006 Modules[Name] = Result;
1007 ModuleScopeIDs[Result] = CurrentModuleScopeID;
1008 }
1009 return Result;
1010}
1011
1012Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc,
1013 Module *Parent) {
1014 auto *Result = new (ModulesAlloc.Allocate()) Module(
1015 ModuleConstructorTag{}, "<global>", Loc, Parent, /*IsFramework=*/false,
1016 /*IsExplicit=*/true, NumCreatedModules++);
1017 Result->Kind = Module::ExplicitGlobalModuleFragment;
1018 // If the created module isn't owned by a parent, send it to PendingSubmodules
1019 // to wait for its parent.
1020 if (!Result->Parent)
1021 PendingSubmodules.emplace_back(Args&: Result);
1022 return Result;
1023}
1024
1025Module *
1026ModuleMap::createImplicitGlobalModuleFragmentForModuleUnit(SourceLocation Loc,
1027 Module *Parent) {
1028 assert(Parent && "We should only create an implicit global module fragment "
1029 "in a module purview");
1030 // Note: Here the `IsExplicit` parameter refers to the semantics in clang
1031 // modules. All the non-explicit submodules in clang modules will be exported
1032 // too. Here we simplify the implementation by using the concept.
1033 auto *Result = new (ModulesAlloc.Allocate())
1034 Module(ModuleConstructorTag{}, "<implicit global>", Loc, Parent,
1035 /*IsFramework=*/false, /*IsExplicit=*/false, NumCreatedModules++);
1036 Result->Kind = Module::ImplicitGlobalModuleFragment;
1037 return Result;
1038}
1039
1040Module *
1041ModuleMap::createPrivateModuleFragmentForInterfaceUnit(Module *Parent,
1042 SourceLocation Loc) {
1043 auto *Result = new (ModulesAlloc.Allocate()) Module(
1044 ModuleConstructorTag{}, "<private>", Loc, Parent, /*IsFramework=*/false,
1045 /*IsExplicit=*/true, NumCreatedModules++);
1046 Result->Kind = Module::PrivateModuleFragment;
1047 return Result;
1048}
1049
1050Module *ModuleMap::createModuleUnitWithKind(SourceLocation Loc, StringRef Name,
1051 Module::ModuleKind Kind) {
1052 auto *Result = new (ModulesAlloc.Allocate())
1053 Module(ModuleConstructorTag{}, Name, Loc, nullptr, /*IsFramework=*/false,
1054 /*IsExplicit=*/false, NumCreatedModules++);
1055 Result->Kind = Kind;
1056
1057 // Reparent any current global module fragment as a submodule of this module.
1058 for (auto &Submodule : PendingSubmodules)
1059 Submodule->setParent(Result);
1060 PendingSubmodules.clear();
1061 return Result;
1062}
1063
1064Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,
1065 StringRef Name) {
1066 assert(LangOpts.CurrentModule == Name && "module name mismatch");
1067 assert(!Modules[Name] && "redefining existing module");
1068
1069 auto *Result =
1070 createModuleUnitWithKind(Loc, Name, Kind: Module::ModuleInterfaceUnit);
1071 Modules[Name] = SourceModule = Result;
1072
1073 // Mark the main source file as being within the newly-created module so that
1074 // declarations and macros are properly visibility-restricted to it.
1075 auto MainFile = SourceMgr.getFileEntryRefForID(FID: SourceMgr.getMainFileID());
1076 assert(MainFile && "no input file for module interface");
1077 Headers[*MainFile].push_back(Elt: KnownHeader(Result, PrivateHeader));
1078
1079 return Result;
1080}
1081
1082Module *ModuleMap::createModuleForImplementationUnit(SourceLocation Loc,
1083 StringRef Name) {
1084 assert(LangOpts.CurrentModule == Name && "module name mismatch");
1085 // The interface for this implementation must exist and be loaded.
1086 assert(Modules[Name] && Modules[Name]->Kind == Module::ModuleInterfaceUnit &&
1087 "creating implementation module without an interface");
1088
1089 // Create an entry in the modules map to own the implementation unit module.
1090 // User module names must not start with a period (so that this cannot clash
1091 // with any legal user-defined module name).
1092 StringRef IName = ".ImplementationUnit";
1093 assert(!Modules[IName] && "multiple implementation units?");
1094
1095 auto *Result =
1096 createModuleUnitWithKind(Loc, Name, Kind: Module::ModuleImplementationUnit);
1097 Modules[IName] = SourceModule = Result;
1098
1099 // Check that the main file is present.
1100 assert(SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) &&
1101 "no input file for module implementation");
1102
1103 return Result;
1104}
1105
1106Module *ModuleMap::createHeaderUnit(SourceLocation Loc, StringRef Name,
1107 Module::Header H) {
1108 assert(LangOpts.CurrentModule == Name && "module name mismatch");
1109 assert(!Modules[Name] && "redefining existing module");
1110
1111 auto *Result = new (ModulesAlloc.Allocate())
1112 Module(ModuleConstructorTag{}, Name, Loc, nullptr, /*IsFramework=*/false,
1113 /*IsExplicit=*/false, NumCreatedModules++);
1114 Result->Kind = Module::ModuleHeaderUnit;
1115 Modules[Name] = SourceModule = Result;
1116 addHeader(Mod: Result, Header: H, Role: NormalHeader);
1117 return Result;
1118}
1119
1120/// For a framework module, infer the framework against which we
1121/// should link.
1122static void inferFrameworkLink(Module *Mod) {
1123 assert(Mod->IsFramework && "Can only infer linking for framework modules");
1124 assert(!Mod->isSubFramework() &&
1125 "Can only infer linking for top-level frameworks");
1126
1127 StringRef FrameworkName(Mod->Name);
1128 FrameworkName.consume_back(Suffix: "_Private");
1129 Mod->LinkLibraries.push_back(Elt: Module::LinkLibrary(FrameworkName.str(),
1130 /*IsFramework=*/true));
1131}
1132
1133Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir,
1134 bool IsSystem, Module *Parent) {
1135 Attributes Attrs;
1136 Attrs.IsSystem = IsSystem;
1137 return inferFrameworkModule(FrameworkDir, Attrs, Parent);
1138}
1139
1140Module *ModuleMap::inferFrameworkModule(DirectoryEntryRef FrameworkDir,
1141 Attributes Attrs, Module *Parent) {
1142 // Note: as an egregious but useful hack we use the real path here, because
1143 // we might be looking at an embedded framework that symlinks out to a
1144 // top-level framework, and we need to infer as if we were naming the
1145 // top-level framework.
1146 StringRef FrameworkDirName =
1147 SourceMgr.getFileManager().getCanonicalName(Dir: FrameworkDir);
1148
1149 // In case this is a case-insensitive filesystem, use the canonical
1150 // directory name as the ModuleName, since modules are case-sensitive.
1151 // FIXME: we should be able to give a fix-it hint for the correct spelling.
1152 SmallString<32> ModuleNameStorage;
1153 StringRef ModuleName = sanitizeFilenameAsIdentifier(
1154 Name: llvm::sys::path::stem(path: FrameworkDirName), Buffer&: ModuleNameStorage);
1155
1156 // Check whether we've already found this module.
1157 if (Module *Mod = lookupModuleQualified(Name: ModuleName, Context: Parent))
1158 return Mod;
1159
1160 FileManager &FileMgr = SourceMgr.getFileManager();
1161
1162 // If the framework has a parent path from which we're allowed to infer
1163 // a framework module, do so.
1164 FileID ModuleMapFID;
1165 if (!Parent) {
1166 // Determine whether we're allowed to infer a module map.
1167 bool canInfer = false;
1168 if (llvm::sys::path::has_parent_path(path: FrameworkDirName)) {
1169 // Figure out the parent path.
1170 StringRef Parent = llvm::sys::path::parent_path(path: FrameworkDirName);
1171 if (auto ParentDir = FileMgr.getOptionalDirectoryRef(DirName: Parent)) {
1172 // Check whether we have already looked into the parent directory
1173 // for a module map.
1174 llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
1175 inferred = InferredDirectories.find(Val: *ParentDir);
1176 if (inferred == InferredDirectories.end()) {
1177 // We haven't looked here before. Load a module map, if there is
1178 // one.
1179 bool IsFrameworkDir = Parent.ends_with(Suffix: ".framework");
1180 if (OptionalFileEntryRef ModMapFile =
1181 HeaderInfo.lookupModuleMapFile(Dir: *ParentDir, IsFramework: IsFrameworkDir)) {
1182 // TODO: Parsing a module map should populate `InferredDirectories`
1183 // so we don't need to do a full load here.
1184 parseAndLoadModuleMapFile(File: *ModMapFile, IsSystem: Attrs.IsSystem,
1185 /*ImplicitlyDiscovered=*/true,
1186 HomeDir: *ParentDir);
1187 inferred = InferredDirectories.find(Val: *ParentDir);
1188 }
1189
1190 if (inferred == InferredDirectories.end())
1191 inferred = InferredDirectories.insert(
1192 KV: std::make_pair(x&: *ParentDir, y: InferredDirectory())).first;
1193 }
1194
1195 if (inferred->second.InferModules) {
1196 // We're allowed to infer for this directory, but make sure it's okay
1197 // to infer this particular module.
1198 StringRef Name = llvm::sys::path::stem(path: FrameworkDirName);
1199 canInfer =
1200 !llvm::is_contained(Range: inferred->second.ExcludedModules, Element: Name);
1201
1202 Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
1203 Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
1204 Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
1205 Attrs.NoUndeclaredIncludes |=
1206 inferred->second.Attrs.NoUndeclaredIncludes;
1207 ModuleMapFID = inferred->second.ModuleMapFID;
1208 }
1209 }
1210 }
1211
1212 // If we're not allowed to infer a framework module, don't.
1213 if (!canInfer)
1214 return nullptr;
1215 } else {
1216 ModuleMapFID = getModuleMapFileIDForUniquing(M: Parent);
1217 }
1218
1219 // Look for an umbrella header.
1220 SmallString<128> UmbrellaName = FrameworkDir.getName();
1221 llvm::sys::path::append(path&: UmbrellaName, a: "Headers", b: ModuleName + ".h");
1222 auto UmbrellaHeader = FileMgr.getOptionalFileRef(Filename: UmbrellaName);
1223
1224 // FIXME: If there's no umbrella header, we could probably scan the
1225 // framework to load *everything*. But, it's not clear that this is a good
1226 // idea.
1227 if (!UmbrellaHeader)
1228 return nullptr;
1229
1230 Module *Result = new (ModulesAlloc.Allocate())
1231 Module(ModuleConstructorTag{}, ModuleName, SourceLocation(), Parent,
1232 /*IsFramework=*/true, /*IsExplicit=*/false, NumCreatedModules++);
1233 setInferredModuleAllowedBy(M: Result, ModMapFID: ModuleMapFID);
1234 if (!Parent) {
1235 if (LangOpts.CurrentModule == ModuleName)
1236 SourceModule = Result;
1237 Modules[ModuleName] = Result;
1238 ModuleScopeIDs[Result] = CurrentModuleScopeID;
1239 }
1240
1241 Result->IsSystem |= Attrs.IsSystem;
1242 Result->IsExternC |= Attrs.IsExternC;
1243 Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
1244 Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
1245 Result->Directory = FrameworkDir;
1246
1247 // Chop off the first framework bit, as that is implied.
1248 StringRef RelativePath = UmbrellaName.str().substr(
1249 Start: Result->getTopLevelModule()->Directory->getName().size());
1250 RelativePath = llvm::sys::path::relative_path(path: RelativePath);
1251
1252 // umbrella header "umbrella-header-name"
1253 setUmbrellaHeaderAsWritten(Mod: Result, UmbrellaHeader: *UmbrellaHeader, NameAsWritten: ModuleName + ".h",
1254 PathRelativeToRootModuleDirectory: RelativePath);
1255
1256 // export *
1257 Result->Exports.push_back(Elt: Module::ExportDecl(nullptr, true));
1258
1259 // module * { export * }
1260 Result->InferSubmodules = true;
1261 Result->InferExportWildcard = true;
1262
1263 // Look for subframeworks.
1264 std::error_code EC;
1265 SmallString<128> SubframeworksDirName = FrameworkDir.getName();
1266 llvm::sys::path::append(path&: SubframeworksDirName, a: "Frameworks");
1267 llvm::sys::path::native(path&: SubframeworksDirName);
1268 llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1269 // Every entry here becomes a submodule of this framework.
1270 recordDirectoryDependencies(Mod: Result, Dir: SubframeworksDirName);
1271 for (llvm::vfs::directory_iterator
1272 Dir = FS.dir_begin(Dir: SubframeworksDirName, EC),
1273 DirEnd;
1274 Dir != DirEnd && !EC; Dir.increment(EC)) {
1275 if (!StringRef(Dir->path()).ends_with(Suffix: ".framework"))
1276 continue;
1277
1278 if (auto SubframeworkDir = FileMgr.getOptionalDirectoryRef(DirName: Dir->path())) {
1279 // Note: as an egregious but useful hack, we use the real path here and
1280 // check whether it is actually a subdirectory of the parent directory.
1281 // This will not be the case if the 'subframework' is actually a symlink
1282 // out to a top-level framework.
1283 StringRef SubframeworkDirName =
1284 FileMgr.getCanonicalName(Dir: *SubframeworkDir);
1285 bool FoundParent = false;
1286 do {
1287 // Get the parent directory name.
1288 SubframeworkDirName
1289 = llvm::sys::path::parent_path(path: SubframeworkDirName);
1290 if (SubframeworkDirName.empty())
1291 break;
1292
1293 if (auto SubDir =
1294 FileMgr.getOptionalDirectoryRef(DirName: SubframeworkDirName)) {
1295 if (*SubDir == FrameworkDir) {
1296 FoundParent = true;
1297 break;
1298 }
1299 }
1300 } while (true);
1301
1302 if (!FoundParent)
1303 continue;
1304
1305 // FIXME: Do we want to warn about subframeworks without umbrella headers?
1306 inferFrameworkModule(FrameworkDir: *SubframeworkDir, Attrs, Parent: Result);
1307 }
1308 }
1309
1310 // If the module is a top-level framework, automatically link against the
1311 // framework.
1312 if (!Result->isSubFramework())
1313 inferFrameworkLink(Mod: Result);
1314
1315 return Result;
1316}
1317
1318Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework,
1319 Module *ShadowingModule) {
1320
1321 // Create a new module with this name.
1322 Module *Result = new (ModulesAlloc.Allocate())
1323 Module(ModuleConstructorTag{}, Name, SourceLocation(), /*Parent=*/nullptr,
1324 IsFramework, /*IsExplicit=*/false, NumCreatedModules++);
1325 Result->ShadowingModule = ShadowingModule;
1326 Result->markUnavailable(/*Unimportable*/true);
1327 ModuleScopeIDs[Result] = CurrentModuleScopeID;
1328 ShadowModules.push_back(Elt: Result);
1329
1330 return Result;
1331}
1332
1333void ModuleMap::setUmbrellaHeaderAsWritten(
1334 Module *Mod, FileEntryRef UmbrellaHeader, const Twine &NameAsWritten,
1335 const Twine &PathRelativeToRootModuleDirectory, SourceLocation Loc) {
1336 Headers[UmbrellaHeader].push_back(Elt: KnownHeader(Mod, NormalHeader));
1337 if (Loc.isValid())
1338 HeaderOwnerLocs[{&UmbrellaHeader.getFileEntry(), Mod}] = Loc;
1339 Mod->Umbrella = UmbrellaHeader;
1340 Mod->UmbrellaDeclLoc = Loc;
1341 Mod->UmbrellaAsWritten = NameAsWritten.str();
1342 Mod->UmbrellaRelativeToRootModuleDirectory =
1343 PathRelativeToRootModuleDirectory.str();
1344 UmbrellaDirs[UmbrellaHeader.getDir()] = Mod;
1345 // A header added next to the umbrella header doesn't change the module, but
1346 // it should rebuild to warn that the umbrella header doesn't include it.
1347 recordDirectoryDependencies(Mod, Dir: UmbrellaHeader.getDir().getName());
1348
1349 // Notify callbacks that we just added a new header.
1350 for (const auto &Cb : Callbacks)
1351 Cb->moduleMapAddUmbrellaHeader(Header: UmbrellaHeader);
1352}
1353
1354void ModuleMap::recordDirectoryDependencies(Module *Mod, StringRef Dir) {
1355 FileManager &FileMgr = SourceMgr.getFileManager();
1356 SmallVector<std::string, 2> Sources;
1357 FileMgr.getVirtualFileSystem().getDirectoryContentRealSources(Dir, Out&: Sources);
1358 for (StringRef Source : Sources) {
1359 SmallString<256> Canonical(Source);
1360 FileMgr.makeAbsolutePath(Path&: Canonical);
1361 llvm::sys::path::remove_dots(path&: Canonical, /*remove_dot_dot=*/true);
1362 Mod->addDirectoryDependency(Path: Canonical);
1363 }
1364}
1365
1366void ModuleMap::setUmbrellaDirAsWritten(
1367 Module *Mod, DirectoryEntryRef UmbrellaDir, const Twine &NameAsWritten,
1368 const Twine &PathRelativeToRootModuleDirectory, SourceLocation Loc) {
1369 Mod->Umbrella = UmbrellaDir;
1370 Mod->UmbrellaDeclLoc = Loc;
1371 Mod->UmbrellaAsWritten = NameAsWritten.str();
1372 Mod->UmbrellaRelativeToRootModuleDirectory =
1373 PathRelativeToRootModuleDirectory.str();
1374 UmbrellaDirs[UmbrellaDir] = Mod;
1375 // The umbrella directory tree is enumerated to collect the module's headers.
1376 recordDirectoryDependencies(Mod, Dir: UmbrellaDir.getName());
1377}
1378
1379void ModuleMap::addUnresolvedHeader(Module *Mod,
1380 Module::UnresolvedHeaderDirective Header,
1381 bool &NeedsFramework) {
1382 // If there is a builtin counterpart to this file, add it now so it can
1383 // wrap the system header.
1384 if (resolveAsBuiltinHeader(Mod, Header)) {
1385 // If we have both a builtin and system version of the file, the
1386 // builtin version may want to inject macros into the system header, so
1387 // force the system header to be treated as a textual header in this
1388 // case.
1389 Header.Kind = headerRoleToKind(Role: ModuleMap::ModuleHeaderRole(
1390 headerKindToRole(Kind: Header.Kind) | ModuleMap::TextualHeader));
1391 Header.HasBuiltinHeader = true;
1392 }
1393
1394 // If possible, don't stat the header until we need to. This requires the
1395 // user to have provided us with some stat information about the file.
1396 // FIXME: Add support for lazily stat'ing umbrella headers and excluded
1397 // headers.
1398 if ((Header.Size || Header.ModTime) && !Header.IsUmbrella &&
1399 Header.Kind != Module::HK_Excluded) {
1400 // We expect more variation in mtime than size, so if we're given both,
1401 // use the mtime as the key.
1402 if (Header.ModTime)
1403 LazyHeadersByModTime[*Header.ModTime].push_back(NewVal: Mod);
1404 else
1405 LazyHeadersBySize[*Header.Size].push_back(NewVal: Mod);
1406 Mod->UnresolvedHeaders.push_back(Elt: Header);
1407 return;
1408 }
1409
1410 // We don't have stat information or can't defer looking this file up.
1411 // Perform the lookup now.
1412 resolveHeader(Mod, Header, NeedsFramework);
1413}
1414
1415void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const {
1416 auto BySize = LazyHeadersBySize.find(Val: File->getSize());
1417 if (BySize != LazyHeadersBySize.end()) {
1418 for (auto *M : BySize->second)
1419 resolveHeaderDirectives(Mod: M, File);
1420 LazyHeadersBySize.erase(I: BySize);
1421 }
1422
1423 auto ByModTime = LazyHeadersByModTime.find(Val: File->getModificationTime());
1424 if (ByModTime != LazyHeadersByModTime.end()) {
1425 for (auto *M : ByModTime->second)
1426 resolveHeaderDirectives(Mod: M, File);
1427 LazyHeadersByModTime.erase(I: ByModTime);
1428 }
1429}
1430
1431void ModuleMap::resolveHeaderDirectives(
1432 Module *Mod, std::optional<const FileEntry *> File) const {
1433 bool NeedsFramework = false;
1434 SmallVector<Module::UnresolvedHeaderDirective, 1> NewHeaders;
1435 const auto Size = File ? (*File)->getSize() : 0;
1436 const auto ModTime = File ? (*File)->getModificationTime() : 0;
1437
1438 for (auto &Header : Mod->UnresolvedHeaders) {
1439 if (File && ((Header.ModTime && Header.ModTime != ModTime) ||
1440 (Header.Size && Header.Size != Size)))
1441 NewHeaders.push_back(Elt: Header);
1442 else
1443 // This operation is logically const; we're just changing how we represent
1444 // the header information for this file.
1445 const_cast<ModuleMap *>(this)->resolveHeader(Mod, Header, NeedsFramework);
1446 }
1447 Mod->UnresolvedHeaders.swap(RHS&: NewHeaders);
1448}
1449
1450void ModuleMap::addHeader(Module *Mod, Module::Header Header,
1451 ModuleHeaderRole Role, bool Imported,
1452 SourceLocation Loc) {
1453 KnownHeader KH(Mod, Role);
1454
1455 FileEntryRef HeaderEntry = Header.Entry;
1456
1457 // Only add each header to the headers list once.
1458 // FIXME: Should we diagnose if a header is listed twice in the
1459 // same module definition?
1460 auto &HeaderList = Headers[HeaderEntry];
1461 if (llvm::is_contained(Range&: HeaderList, Element: KH))
1462 return;
1463
1464 if (Loc.isValid())
1465 HeaderOwnerLocs[{&HeaderEntry.getFileEntry(), Mod}] = Loc;
1466
1467 HeaderList.push_back(Elt: KH);
1468 Mod->addHeader(HK: headerRoleToKind(Role), H: std::move(Header));
1469
1470 bool isCompilingModuleHeader = Mod->isForBuilding(LangOpts);
1471 if (!Imported || isCompilingModuleHeader) {
1472 // When we import HeaderFileInfo, the external source is expected to
1473 // set the isModuleHeader flag itself.
1474 HeaderInfo.MarkFileModuleHeader(FE: HeaderEntry, Role, isCompilingModuleHeader);
1475 }
1476
1477 // Notify callbacks that we just added a new header.
1478 for (const auto &Cb : Callbacks)
1479 Cb->moduleMapAddHeader(Filename: HeaderEntry.getName());
1480}
1481
1482bool ModuleMap::parseModuleMapFile(FileEntryRef File, bool IsSystem,
1483 bool ImplicitlyDiscovered,
1484 DirectoryEntryRef Dir, FileID ID,
1485 SourceLocation ExternModuleLoc) {
1486 llvm::DenseMap<const FileEntry *, const modulemap::ModuleMapFile *>::iterator
1487 Known = ParsedModuleMap.find(Val: File);
1488 if (Known != ParsedModuleMap.end())
1489 return Known->second == nullptr;
1490
1491 // If the module map file wasn't already entered, do so now.
1492 if (ID.isInvalid()) {
1493 FileID &LocalFID = ModuleMapLocalFileID[File];
1494 if (LocalFID.isInvalid()) {
1495 auto FileCharacter =
1496 IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
1497 LocalFID = SourceMgr.createFileID(SourceFile: File, IncludePos: ExternModuleLoc, FileCharacter);
1498 }
1499 ID = LocalFID;
1500 }
1501
1502 std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(FID: ID);
1503 if (!Buffer) {
1504 ParsedModuleMap[File] = nullptr;
1505 return true;
1506 }
1507
1508 Diags.Report(DiagID: diag::remark_mmap_parse) << File.getName();
1509 std::optional<modulemap::ModuleMapFile> MaybeMMF = modulemap::parseModuleMap(
1510 ID, Dir, SM&: SourceMgr, Diags, IsSystem, ImplicitlyDiscovered, Offset: nullptr);
1511
1512 if (!MaybeMMF) {
1513 ParsedModuleMap[File] = nullptr;
1514 return true;
1515 }
1516
1517 ParsedModuleMaps.push_back(
1518 x: std::make_unique<modulemap::ModuleMapFile>(args: std::move(*MaybeMMF)));
1519 const modulemap::ModuleMapFile &MMF = *ParsedModuleMaps.back();
1520 std::vector<const modulemap::ExternModuleDecl *> PendingExternalModuleMaps;
1521 std::function<void(const modulemap::ModuleDecl &)> CollectExternDecls =
1522 [&](const modulemap::ModuleDecl &MD) {
1523 for (const auto &Decl : MD.Decls) {
1524 std::visit(visitor: llvm::makeVisitor(
1525 Callables: [&](const modulemap::ModuleDecl &SubMD) {
1526 // Skip inferred submodules (module *)
1527 if (SubMD.Id.front().first == "*")
1528 return;
1529 CollectExternDecls(SubMD);
1530 },
1531 Callables: [&](const modulemap::ExternModuleDecl &EMD) {
1532 PendingExternalModuleMaps.push_back(x: &EMD);
1533 },
1534 Callables: [&](const auto &) {
1535 // Ignore other decls
1536 }),
1537 variants: Decl);
1538 }
1539 };
1540
1541 for (const auto &Decl : MMF.Decls) {
1542 std::visit(visitor: llvm::makeVisitor(
1543 Callables: [&](const modulemap::ModuleDecl &MD) {
1544 // Only use the first part of the name even for submodules.
1545 // This will correctly load the submodule declarations when
1546 // the module is loaded.
1547 auto &ModuleDecls =
1548 ParsedModules[StringRef(MD.Id.front().first)];
1549 ModuleDecls.push_back(Elt: std::pair(&MMF, &MD));
1550 CollectExternDecls(MD);
1551 },
1552 Callables: [&](const modulemap::ExternModuleDecl &EMD) {
1553 PendingExternalModuleMaps.push_back(x: &EMD);
1554 }),
1555 variants: Decl);
1556 }
1557
1558 for (const modulemap::ExternModuleDecl *EMD : PendingExternalModuleMaps) {
1559 StringRef FileNameRef = EMD->Path;
1560 SmallString<128> ModuleMapFileName;
1561 if (llvm::sys::path::is_relative(path: FileNameRef)) {
1562 ModuleMapFileName += Dir.getName();
1563 llvm::sys::path::append(path&: ModuleMapFileName, a: EMD->Path);
1564 FileNameRef = ModuleMapFileName;
1565 }
1566
1567 if (auto EFile =
1568 SourceMgr.getFileManager().getOptionalFileRef(Filename: FileNameRef)) {
1569 parseModuleMapFile(File: *EFile, IsSystem, ImplicitlyDiscovered,
1570 Dir: EFile->getDir(), ID: FileID(), ExternModuleLoc);
1571 }
1572 }
1573
1574 ParsedModuleMap[File] = &MMF;
1575
1576 for (const auto &Cb : Callbacks)
1577 Cb->moduleMapFileRead(FileStart: SourceLocation(), File, IsSystem);
1578
1579 return false;
1580}
1581
1582void ModuleMap::loadAllParsedModules() {
1583 for (const auto &Entry : ParsedModules)
1584 findOrLoadModule(Name: Entry.first());
1585}
1586
1587FileID ModuleMap::getContainingModuleMapFileID(const Module *Module) const {
1588 if (Module->DefinitionLoc.isInvalid())
1589 return {};
1590
1591 return SourceMgr.getFileID(SpellingLoc: Module->DefinitionLoc);
1592}
1593
1594OptionalFileEntryRef
1595ModuleMap::getContainingModuleMapFile(const Module *Module) const {
1596 return SourceMgr.getFileEntryRefForID(FID: getContainingModuleMapFileID(Module));
1597}
1598
1599FileID ModuleMap::getModuleMapFileIDForUniquing(const Module *M) const {
1600 if (M->IsInferred) {
1601 assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
1602 return InferredModuleAllowedBy.find(Val: M)->second;
1603 }
1604 return getContainingModuleMapFileID(Module: M);
1605}
1606
1607OptionalFileEntryRef
1608ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
1609 return SourceMgr.getFileEntryRefForID(FID: getModuleMapFileIDForUniquing(M));
1610}
1611
1612void ModuleMap::setInferredModuleAllowedBy(Module *M, FileID ModMapFID) {
1613 M->IsInferred = true;
1614 InferredModuleAllowedBy[M] = ModMapFID;
1615}
1616
1617std::error_code
1618ModuleMap::canonicalizeModuleMapPath(SmallVectorImpl<char> &Path) {
1619 StringRef Dir = llvm::sys::path::parent_path(path: {Path.data(), Path.size()});
1620
1621 // Do not canonicalize within the framework; the module map loader expects
1622 // Modules/ not Versions/A/Modules.
1623 if (llvm::sys::path::filename(path: Dir) == "Modules") {
1624 StringRef Parent = llvm::sys::path::parent_path(path: Dir);
1625 if (Parent.ends_with(Suffix: ".framework"))
1626 Dir = Parent;
1627 }
1628
1629 FileManager &FM = SourceMgr.getFileManager();
1630 auto DirEntry = FM.getDirectoryRef(DirName: Dir.empty() ? "." : Dir);
1631 if (!DirEntry)
1632 return llvm::errorToErrorCode(Err: DirEntry.takeError());
1633
1634 // Canonicalize the directory.
1635 StringRef CanonicalDir = FM.getCanonicalName(Dir: *DirEntry);
1636 if (CanonicalDir != Dir)
1637 llvm::sys::path::replace_path_prefix(Path, OldPrefix: Dir, NewPrefix: CanonicalDir);
1638
1639 // In theory, the filename component should also be canonicalized if it
1640 // on a case-insensitive filesystem. However, the extra canonicalization is
1641 // expensive and if clang looked up the filename it will always be lowercase.
1642
1643 // Remove ., remove redundant separators, and switch to native separators.
1644 // This is needed for separators between CanonicalDir and the filename.
1645 llvm::sys::path::remove_dots(path&: Path);
1646
1647 return std::error_code();
1648}
1649
1650void ModuleMap::addAdditionalModuleMapFile(const Module *M,
1651 FileEntryRef ModuleMap) {
1652 AdditionalModMaps[M].insert(V: ModuleMap);
1653}
1654
1655LLVM_DUMP_METHOD void ModuleMap::dump() {
1656 llvm::errs() << "Modules:";
1657 for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
1658 MEnd = Modules.end();
1659 M != MEnd; ++M)
1660 M->getValue()->print(OS&: llvm::errs(), Indent: 2);
1661
1662 llvm::errs() << "Headers:";
1663 for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
1664 H != HEnd; ++H) {
1665 llvm::errs() << " \"" << H->first.getName() << "\" -> ";
1666 for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
1667 E = H->second.end();
1668 I != E; ++I) {
1669 if (I != H->second.begin())
1670 llvm::errs() << ",";
1671 llvm::errs() << I->getModule()->getFullModuleName();
1672 }
1673 llvm::errs() << "\n";
1674 }
1675}
1676
1677bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
1678 auto Unresolved = std::move(Mod->UnresolvedExports);
1679 Mod->UnresolvedExports.clear();
1680 for (auto &UE : Unresolved) {
1681 Module::ExportDecl Export = resolveExport(Mod, Unresolved: UE, Complain);
1682 if (Export.first || Export.second)
1683 Mod->Exports.push_back(Elt: Export);
1684 else
1685 Mod->UnresolvedExports.push_back(Elt: UE);
1686 }
1687 return !Mod->UnresolvedExports.empty();
1688}
1689
1690bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
1691 auto *Top = Mod->getTopLevelModule();
1692 auto Unresolved = std::move(Top->UnresolvedDirectUses);
1693 Top->UnresolvedDirectUses.clear();
1694 for (auto &UDU : Unresolved) {
1695 Module *DirectUse = resolveModuleId(Id: UDU, Mod: Top, Complain);
1696 if (DirectUse)
1697 Top->DirectUses.push_back(Elt: DirectUse);
1698 else
1699 Top->UnresolvedDirectUses.push_back(Elt: UDU);
1700 }
1701 return !Top->UnresolvedDirectUses.empty();
1702}
1703
1704bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
1705 auto Unresolved = std::move(Mod->UnresolvedConflicts);
1706 Mod->UnresolvedConflicts.clear();
1707 for (auto &UC : Unresolved) {
1708 if (Module *OtherMod = resolveModuleId(Id: UC.Id, Mod, Complain)) {
1709 Module::Conflict Conflict;
1710 Conflict.Other = OtherMod;
1711 Conflict.Message = UC.Message;
1712 Mod->Conflicts.push_back(x: Conflict);
1713 } else
1714 Mod->UnresolvedConflicts.push_back(x: UC);
1715 }
1716 return !Mod->UnresolvedConflicts.empty();
1717}
1718
1719//----------------------------------------------------------------------------//
1720// Module map file loader
1721//----------------------------------------------------------------------------//
1722
1723namespace clang {
1724class ModuleMapLoader {
1725 SourceManager &SourceMgr;
1726
1727 DiagnosticsEngine &Diags;
1728 ModuleMap &Map;
1729
1730 /// The current module map file.
1731 FileID ModuleMapFID;
1732
1733 /// Source location of most recent loaded module declaration
1734 SourceLocation CurrModuleDeclLoc;
1735
1736 /// The directory that file names in this module map file should
1737 /// be resolved relative to.
1738 DirectoryEntryRef Directory;
1739
1740 /// Whether this module map is in a system header directory.
1741 bool IsSystem;
1742
1743 bool ImplicitlyDiscovered;
1744
1745 /// Whether an error occurred.
1746 bool HadError = false;
1747
1748 /// The active module.
1749 Module *ActiveModule = nullptr;
1750
1751 /// Whether a module uses the 'requires excluded' hack to mark its
1752 /// contents as 'textual'.
1753 ///
1754 /// On older Darwin SDK versions, 'requires excluded' is used to mark the
1755 /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
1756 /// non-modular headers. For backwards compatibility, we continue to
1757 /// support this idiom for just these modules, and map the headers to
1758 /// 'textual' to match the original intent.
1759 llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
1760
1761 void handleModuleDecl(const modulemap::ModuleDecl &MD);
1762 void handleExternModuleDecl(const modulemap::ExternModuleDecl &EMD);
1763 void handleRequiresDecl(const modulemap::RequiresDecl &RD);
1764 void handleHeaderDecl(const modulemap::HeaderDecl &HD);
1765 void handleUmbrellaDirDecl(const modulemap::UmbrellaDirDecl &UDD);
1766 void handleExportDecl(const modulemap::ExportDecl &ED);
1767 void handleExportAsDecl(const modulemap::ExportAsDecl &EAD);
1768 void handleUseDecl(const modulemap::UseDecl &UD);
1769 void handleLinkDecl(const modulemap::LinkDecl &LD);
1770 void handleConfigMacros(const modulemap::ConfigMacrosDecl &CMD);
1771 void handleConflict(const modulemap::ConflictDecl &CD);
1772 void handleInferredModuleDecl(const modulemap::ModuleDecl &MD);
1773
1774 /// Private modules are canonicalized as Foo_Private. Clang provides extra
1775 /// module map search logic to find the appropriate private module when PCH
1776 /// is used with implicit module maps. Warn when private modules are written
1777 /// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
1778 void diagnosePrivateModules(SourceLocation StartLoc);
1779
1780 using Attributes = ModuleMap::Attributes;
1781
1782public:
1783 ModuleMapLoader(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
1784 ModuleMap &Map, FileID ModuleMapFID,
1785 DirectoryEntryRef Directory, bool IsSystem,
1786 bool ImplicitlyDiscovered)
1787 : SourceMgr(SourceMgr), Diags(Diags), Map(Map),
1788 ModuleMapFID(ModuleMapFID), Directory(Directory), IsSystem(IsSystem),
1789 ImplicitlyDiscovered(ImplicitlyDiscovered) {}
1790
1791 bool loadModuleDecl(const modulemap::ModuleDecl &MD);
1792 bool loadExternModuleDecl(const modulemap::ExternModuleDecl &EMD);
1793 bool parseAndLoadModuleMapFile(const modulemap::ModuleMapFile &MMF);
1794};
1795
1796} // namespace clang
1797
1798/// Private modules are canonicalized as Foo_Private. Clang provides extra
1799/// module map search logic to find the appropriate private module when PCH
1800/// is used with implicit module maps. Warn when private modules are written
1801/// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
1802void ModuleMapLoader::diagnosePrivateModules(SourceLocation StartLoc) {
1803 auto GenNoteAndFixIt = [&](StringRef BadName, StringRef Canonical,
1804 const Module *M, SourceRange ReplLoc) {
1805 auto D = Diags.Report(Loc: ActiveModule->DefinitionLoc,
1806 DiagID: diag::note_mmap_rename_top_level_private_module);
1807 D << BadName << M->Name;
1808 D << FixItHint::CreateReplacement(RemoveRange: ReplLoc, Code: Canonical);
1809 };
1810
1811 for (auto E = Map.module_begin(); E != Map.module_end(); ++E) {
1812 auto const *M = E->getValue();
1813 if (M->Directory != ActiveModule->Directory)
1814 continue;
1815
1816 SmallString<128> FullName(ActiveModule->getFullModuleName());
1817 if (!FullName.starts_with(Prefix: M->Name) && !FullName.ends_with(Suffix: "Private"))
1818 continue;
1819 SmallString<128> FixedPrivModDecl;
1820 SmallString<128> Canonical(M->Name);
1821 Canonical.append(RHS: "_Private");
1822
1823 // Foo.Private -> Foo_Private
1824 if (ActiveModule->Parent && ActiveModule->Name == "Private" && !M->Parent &&
1825 M->Name == ActiveModule->Parent->Name) {
1826 Diags.Report(Loc: ActiveModule->DefinitionLoc,
1827 DiagID: diag::warn_mmap_mismatched_private_submodule)
1828 << FullName;
1829
1830 SourceLocation FixItInitBegin = CurrModuleDeclLoc;
1831 if (StartLoc.isValid())
1832 FixItInitBegin = StartLoc;
1833
1834 if (ActiveModule->Parent->IsFramework)
1835 FixedPrivModDecl.append(RHS: "framework ");
1836 FixedPrivModDecl.append(RHS: "module ");
1837 FixedPrivModDecl.append(RHS: Canonical);
1838
1839 GenNoteAndFixIt(FullName, FixedPrivModDecl, M,
1840 SourceRange(FixItInitBegin, ActiveModule->DefinitionLoc));
1841 continue;
1842 }
1843
1844 // FooPrivate and whatnots -> Foo_Private
1845 if (!ActiveModule->Parent && !M->Parent && M->Name != ActiveModule->Name &&
1846 ActiveModule->Name != Canonical) {
1847 Diags.Report(Loc: ActiveModule->DefinitionLoc,
1848 DiagID: diag::warn_mmap_mismatched_private_module_name)
1849 << ActiveModule->Name;
1850 GenNoteAndFixIt(ActiveModule->Name, Canonical, M,
1851 SourceRange(ActiveModule->DefinitionLoc));
1852 }
1853 }
1854}
1855
1856void ModuleMapLoader::handleModuleDecl(const modulemap::ModuleDecl &MD) {
1857 if (MD.Id.front().first == "*")
1858 return handleInferredModuleDecl(MD);
1859
1860 CurrModuleDeclLoc = MD.Location;
1861
1862 Module *PreviousActiveModule = ActiveModule;
1863 if (MD.Id.size() > 1) {
1864 // This module map defines a submodule. Go find the module of which it
1865 // is a submodule.
1866 ActiveModule = nullptr;
1867 const Module *TopLevelModule = nullptr;
1868 for (unsigned I = 0, N = MD.Id.size() - 1; I != N; ++I) {
1869 if (Module *Next =
1870 Map.lookupModuleQualified(Name: MD.Id[I].first, Context: ActiveModule)) {
1871 if (I == 0)
1872 TopLevelModule = Next;
1873 ActiveModule = Next;
1874 continue;
1875 }
1876
1877 Diags.Report(Loc: MD.Id[I].second, DiagID: diag::err_mmap_missing_parent_module)
1878 << MD.Id[I].first << (ActiveModule != nullptr)
1879 << (ActiveModule
1880 ? ActiveModule->getTopLevelModule()->getFullModuleName()
1881 : "");
1882 HadError = true;
1883 }
1884
1885 if (TopLevelModule &&
1886 ModuleMapFID != Map.getContainingModuleMapFileID(Module: TopLevelModule)) {
1887 assert(ModuleMapFID !=
1888 Map.getModuleMapFileIDForUniquing(TopLevelModule) &&
1889 "submodule defined in same file as 'module *' that allowed its "
1890 "top-level module");
1891 Map.addAdditionalModuleMapFile(
1892 M: TopLevelModule, ModuleMap: *SourceMgr.getFileEntryRefForID(FID: ModuleMapFID));
1893 }
1894 }
1895
1896 StringRef ModuleName = MD.Id.back().first;
1897 SourceLocation ModuleNameLoc = MD.Id.back().second;
1898
1899 // Determine whether this (sub)module has already been defined.
1900 Module *ShadowingModule = nullptr;
1901 if (Module *Existing = Map.lookupModuleQualified(Name: ModuleName, Context: ActiveModule)) {
1902 // We might see a (re)definition of a module that we already have a
1903 // definition for in four cases:
1904 // - If the Existing module was loaded from an AST file and we've found its
1905 // original source module map, or we cannot determine Existing's
1906 // definition location.
1907 bool LoadedFromASTFile = Existing->IsFromModuleFile;
1908 if (LoadedFromASTFile) {
1909 OptionalFileEntryRef ExistingModMapFile =
1910 Map.getContainingModuleMapFile(Module: Existing);
1911 OptionalFileEntryRef CurrentModMapFile =
1912 SourceMgr.getFileEntryRefForID(FID: ModuleMapFID);
1913 if ((ExistingModMapFile && CurrentModMapFile &&
1914 *ExistingModMapFile == *CurrentModMapFile) ||
1915 Existing->DefinitionLoc.isInvalid()) {
1916 // If we do not know Existing's definition location, we have
1917 // no way of checking against it, and hence we stay conservative and do
1918 // not check for duplicating module definitions.
1919 LoadedFromASTFile = true;
1920 } else
1921 LoadedFromASTFile = false;
1922 }
1923 // - If we previously inferred this module from different module map file.
1924 bool Inferred = Existing->IsInferred;
1925 // - If we're building a framework that vends a module map, we might've
1926 // previously seen the one in intermediate products and now the system
1927 // one.
1928 // FIXME: If we're parsing module map file that looks like this:
1929 // framework module FW { ... }
1930 // module FW.Sub { ... }
1931 // We can't check the framework qualifier, since it's not attached to
1932 // the definition of Sub. Checking that qualifier on \c Existing is
1933 // not correct either, since we might've previously seen:
1934 // module FW { ... }
1935 // module FW.Sub { ... }
1936 // We should enforce consistency of redefinitions so that we can rely
1937 // that \c Existing is part of a framework iff the redefinition of FW
1938 // we have just skipped had it too. Once we do that, stop checking
1939 // the local framework qualifier and only rely on \c Existing.
1940 bool PartOfFramework = MD.Framework || Existing->isPartOfFramework();
1941 // - If we're building a (preprocessed) module and we've just loaded the
1942 // module map file from which it was created.
1943 bool ParsedAsMainInput =
1944 Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
1945 Map.LangOpts.CurrentModule == ModuleName &&
1946 SourceMgr.getDecomposedLoc(Loc: ModuleNameLoc).first !=
1947 SourceMgr.getDecomposedLoc(Loc: Existing->DefinitionLoc).first;
1948 // TODO: Remove this check when we can avoid loading module maps multiple
1949 // times.
1950 bool SameModuleDecl = ModuleNameLoc == Existing->DefinitionLoc;
1951 if (LoadedFromASTFile || Inferred || PartOfFramework || ParsedAsMainInput ||
1952 SameModuleDecl) {
1953 ActiveModule = PreviousActiveModule;
1954 // Skip the module definition.
1955 return;
1956 }
1957
1958 if (!Existing->Parent && Map.mayShadowNewModule(ExistingModule: Existing)) {
1959 ShadowingModule = Existing;
1960 } else {
1961 // This is not a shawdowed module decl, it is an illegal redefinition.
1962 Diags.Report(Loc: ModuleNameLoc, DiagID: diag::err_mmap_module_redefinition)
1963 << ModuleName;
1964 Diags.Report(Loc: Existing->DefinitionLoc, DiagID: diag::note_mmap_prev_definition);
1965 HadError = true;
1966 return;
1967 }
1968 }
1969
1970 // Start defining this module.
1971 if (ShadowingModule) {
1972 ActiveModule =
1973 Map.createShadowedModule(Name: ModuleName, IsFramework: MD.Framework, ShadowingModule);
1974 } else {
1975 ActiveModule = Map.findOrCreateModuleFirst(Name: ModuleName, Parent: ActiveModule,
1976 IsFramework: MD.Framework, IsExplicit: MD.Explicit);
1977 }
1978
1979 ActiveModule->DefinitionLoc = ModuleNameLoc;
1980 if (MD.Attrs.IsSystem || IsSystem)
1981 ActiveModule->IsSystem = true;
1982 if (MD.Attrs.IsExternC)
1983 ActiveModule->IsExternC = true;
1984 if (MD.Attrs.NoUndeclaredIncludes)
1985 ActiveModule->NoUndeclaredIncludes = true;
1986 ActiveModule->Directory = Directory;
1987
1988 StringRef MapFileName(
1989 SourceMgr.getFileEntryRefForID(FID: ModuleMapFID)->getName());
1990 if (MapFileName.ends_with(Suffix: "module.private.modulemap") ||
1991 MapFileName.ends_with(Suffix: "module_private.map")) {
1992 ActiveModule->ModuleMapIsPrivate = true;
1993 }
1994
1995 // Private modules named as FooPrivate, Foo.Private or similar are likely a
1996 // user error; provide warnings, notes and fixits to direct users to use
1997 // Foo_Private instead.
1998 SourceLocation StartLoc =
1999 SourceMgr.getLocForStartOfFile(FID: SourceMgr.getMainFileID());
2000 if (Map.HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
2001 !Diags.isIgnored(DiagID: diag::warn_mmap_mismatched_private_submodule,
2002 Loc: StartLoc) &&
2003 !Diags.isIgnored(DiagID: diag::warn_mmap_mismatched_private_module_name,
2004 Loc: StartLoc) &&
2005 ActiveModule->ModuleMapIsPrivate)
2006 diagnosePrivateModules(StartLoc: MD.Location);
2007
2008 for (const modulemap::Decl &Decl : MD.Decls) {
2009 std::visit(
2010 visitor: llvm::makeVisitor(
2011 Callables: [&](const modulemap::RequiresDecl &RD) { handleRequiresDecl(RD); },
2012 Callables: [&](const modulemap::HeaderDecl &HD) { handleHeaderDecl(HD); },
2013 Callables: [&](const modulemap::UmbrellaDirDecl &UDD) {
2014 handleUmbrellaDirDecl(UDD);
2015 },
2016 Callables: [&](const modulemap::ModuleDecl &MD) { handleModuleDecl(MD); },
2017 Callables: [&](const modulemap::ExportDecl &ED) { handleExportDecl(ED); },
2018 Callables: [&](const modulemap::ExportAsDecl &EAD) {
2019 handleExportAsDecl(EAD);
2020 },
2021 Callables: [&](const modulemap::ExternModuleDecl &EMD) {
2022 handleExternModuleDecl(EMD);
2023 },
2024 Callables: [&](const modulemap::UseDecl &UD) { handleUseDecl(UD); },
2025 Callables: [&](const modulemap::LinkDecl &LD) { handleLinkDecl(LD); },
2026 Callables: [&](const modulemap::ConfigMacrosDecl &CMD) {
2027 handleConfigMacros(CMD);
2028 },
2029 Callables: [&](const modulemap::ConflictDecl &CD) { handleConflict(CD); },
2030 Callables: [&](const modulemap::ExcludeDecl &ED) {
2031 Diags.Report(Loc: ED.Location, DiagID: diag::err_mmap_expected_member);
2032 }),
2033 variants: Decl);
2034 }
2035
2036 // If the active module is a top-level framework, and there are no link
2037 // libraries, automatically link against the framework.
2038 if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
2039 ActiveModule->LinkLibraries.empty())
2040 inferFrameworkLink(Mod: ActiveModule);
2041
2042 // If the module meets all requirements but is still unavailable, mark the
2043 // whole tree as unavailable to prevent it from building.
2044 if (!ActiveModule->IsAvailable && !ActiveModule->IsUnimportable &&
2045 ActiveModule->Parent) {
2046 ActiveModule->getTopLevelModule()->markUnavailable(/*Unimportable=*/false);
2047 ActiveModule->getTopLevelModule()->MissingHeaders.append(
2048 in_start: ActiveModule->MissingHeaders.begin(), in_end: ActiveModule->MissingHeaders.end());
2049 }
2050
2051 // We're done parsing this module. Pop back to the previous module.
2052 ActiveModule = PreviousActiveModule;
2053}
2054
2055void ModuleMapLoader::handleExternModuleDecl(
2056 const modulemap::ExternModuleDecl &EMD) {
2057 StringRef FileNameRef = EMD.Path;
2058 SmallString<128> ModuleMapFileName;
2059 if (llvm::sys::path::is_relative(path: FileNameRef)) {
2060 ModuleMapFileName += Directory.getName();
2061 llvm::sys::path::append(path&: ModuleMapFileName, a: EMD.Path);
2062 // As extern module declarations are parsed recursively, relative paths
2063 // to those modules can become arbitrarily long.
2064 // If the OS name length limit is exceeded when trying to get the file ref
2065 // we can silently fail to find an extern module that exists.
2066 // To mitigate this, collapse relative paths containing '../' for when
2067 // constructing the name of each module file referenced as an extern module.
2068 llvm::sys::path::remove_dots(path&: ModuleMapFileName, /*remove_dot_dot=*/true);
2069 FileNameRef = ModuleMapFileName;
2070 }
2071 if (auto File = SourceMgr.getFileManager().getOptionalFileRef(Filename: FileNameRef))
2072 Map.parseAndLoadModuleMapFile(
2073 File: *File, IsSystem, ImplicitlyDiscovered,
2074 HomeDir: Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
2075 ? Directory
2076 : File->getDir(),
2077 ID: FileID(), Offset: nullptr, ExternModuleLoc: EMD.Location);
2078}
2079
2080/// Whether to add the requirement \p Feature to the module \p M.
2081///
2082/// This preserves backwards compatibility for two hacks in the Darwin system
2083/// module map files:
2084///
2085/// 1. The use of 'requires excluded' to make headers non-modular, which
2086/// should really be mapped to 'textual' now that we have this feature. We
2087/// drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
2088/// true. Later, this bit will be used to map all the headers inside this
2089/// module to 'textual'.
2090///
2091/// This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
2092///
2093/// 2. Removes a bogus cplusplus requirement from IOKit.avc. This requirement
2094/// was never correct and causes issues now that we check it, so drop it.
2095static bool shouldAddRequirement(Module *M, StringRef Feature,
2096 bool &IsRequiresExcludedHack) {
2097 if (Feature == "excluded" &&
2098 (M->fullModuleNameIs(nameParts: {"Darwin", "C", "excluded"}) ||
2099 M->fullModuleNameIs(nameParts: {"Tcl", "Private"}))) {
2100 IsRequiresExcludedHack = true;
2101 return false;
2102 } else if (Feature == "cplusplus" && M->fullModuleNameIs(nameParts: {"IOKit", "avc"})) {
2103 return false;
2104 }
2105
2106 return true;
2107}
2108
2109void ModuleMapLoader::handleRequiresDecl(const modulemap::RequiresDecl &RD) {
2110
2111 for (const modulemap::RequiresFeature &RF : RD.Features) {
2112 bool IsRequiresExcludedHack = false;
2113 bool ShouldAddRequirement =
2114 shouldAddRequirement(M: ActiveModule, Feature: RF.Feature, IsRequiresExcludedHack);
2115
2116 if (IsRequiresExcludedHack)
2117 UsesRequiresExcludedHack.insert(Ptr: ActiveModule);
2118
2119 if (ShouldAddRequirement) {
2120 // Add this feature.
2121 ActiveModule->addRequirement(Feature: RF.Feature, RequiredState: RF.RequiredState, LangOpts: Map.LangOpts,
2122 Target: *Map.Target);
2123 }
2124 }
2125}
2126
2127void ModuleMapLoader::handleHeaderDecl(const modulemap::HeaderDecl &HD) {
2128 // We've already consumed the first token.
2129 ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
2130
2131 if (HD.Private) {
2132 Role = ModuleMap::PrivateHeader;
2133 } else if (HD.Excluded) {
2134 Role = ModuleMap::ExcludedHeader;
2135 }
2136
2137 if (HD.Textual)
2138 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2139
2140 if (UsesRequiresExcludedHack.count(Ptr: ActiveModule)) {
2141 // Mark this header 'textual' (see doc comment for
2142 // Module::UsesRequiresExcludedHack).
2143 Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2144 }
2145
2146 Module::UnresolvedHeaderDirective Header;
2147 Header.FileName = HD.Path;
2148 Header.FileNameLoc = HD.PathLoc;
2149 Header.IsUmbrella = HD.Umbrella;
2150 Header.Kind = Map.headerRoleToKind(Role);
2151
2152 // Check whether we already have an umbrella.
2153 if (Header.IsUmbrella &&
2154 !std::holds_alternative<std::monostate>(v: ActiveModule->Umbrella)) {
2155 Diags.Report(Loc: Header.FileNameLoc, DiagID: diag::err_mmap_umbrella_clash)
2156 << ActiveModule->getFullModuleName();
2157 HadError = true;
2158 return;
2159 }
2160
2161 if (ImplicitlyDiscovered) {
2162 SmallString<128> NormalizedPath(HD.Path);
2163 llvm::sys::path::remove_dots(path&: NormalizedPath, /*remove_dot_dot=*/true);
2164 if (NormalizedPath.starts_with(Prefix: ".."))
2165 Diags.Report(Loc: HD.PathLoc, DiagID: diag::warn_mmap_path_outside_directory);
2166 }
2167
2168 if (HD.Size)
2169 Header.Size = HD.Size;
2170 if (HD.MTime)
2171 Header.ModTime = HD.MTime;
2172
2173 bool NeedsFramework = false;
2174 // Don't add headers to the builtin modules if the builtin headers belong to
2175 // the system modules, with the exception of __stddef_max_align_t.h which
2176 // always had its own module.
2177 if (!Map.LangOpts.BuiltinHeadersInSystemModules ||
2178 !isBuiltInModuleName(ModuleName: ActiveModule->getTopLevelModuleName()) ||
2179 ActiveModule->fullModuleNameIs(nameParts: {"_Builtin_stddef", "max_align_t"}))
2180 Map.addUnresolvedHeader(Mod: ActiveModule, Header: std::move(Header), NeedsFramework);
2181
2182 if (NeedsFramework)
2183 Diags.Report(Loc: CurrModuleDeclLoc, DiagID: diag::note_mmap_add_framework_keyword)
2184 << ActiveModule->getFullModuleName()
2185 << FixItHint::CreateReplacement(RemoveRange: CurrModuleDeclLoc, Code: "framework module");
2186}
2187
2188static bool compareModuleHeaders(const Module::Header &A,
2189 const Module::Header &B) {
2190 return A.NameAsWritten < B.NameAsWritten;
2191}
2192
2193void ModuleMapLoader::handleUmbrellaDirDecl(
2194 const modulemap::UmbrellaDirDecl &UDD) {
2195 std::string DirName = std::string(UDD.Path);
2196 std::string DirNameAsWritten = DirName;
2197
2198 // Check whether we already have an umbrella.
2199 if (!std::holds_alternative<std::monostate>(v: ActiveModule->Umbrella)) {
2200 Diags.Report(Loc: UDD.Location, DiagID: diag::err_mmap_umbrella_clash)
2201 << ActiveModule->getFullModuleName();
2202 HadError = true;
2203 return;
2204 }
2205
2206 if (ImplicitlyDiscovered) {
2207 SmallString<128> NormalizedPath(UDD.Path);
2208 llvm::sys::path::remove_dots(path&: NormalizedPath, /*remove_dot_dot=*/true);
2209 if (NormalizedPath.starts_with(Prefix: ".."))
2210 Diags.Report(Loc: UDD.Location, DiagID: diag::warn_mmap_path_outside_directory);
2211 }
2212
2213 // Look for this file.
2214 OptionalDirectoryEntryRef Dir;
2215 if (llvm::sys::path::is_absolute(path: DirName)) {
2216 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName);
2217 } else {
2218 SmallString<128> PathName;
2219 PathName = Directory.getName();
2220 llvm::sys::path::append(path&: PathName, a: DirName);
2221 Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName: PathName);
2222 }
2223
2224 if (!Dir) {
2225 Diags.Report(Loc: UDD.Location, DiagID: diag::warn_mmap_umbrella_dir_not_found)
2226 << DirName;
2227 return;
2228 }
2229
2230 if (UsesRequiresExcludedHack.count(Ptr: ActiveModule)) {
2231 // Mark this header 'textual' (see doc comment for
2232 // ModuleMapLoader::UsesRequiresExcludedHack). Although iterating over the
2233 // directory is relatively expensive, in practice this only applies to the
2234 // uncommonly used Tcl module on Darwin platforms.
2235 std::error_code EC;
2236 SmallVector<Module::Header, 6> Headers;
2237 llvm::vfs::FileSystem &FS =
2238 SourceMgr.getFileManager().getVirtualFileSystem();
2239 Map.recordDirectoryDependencies(Mod: ActiveModule, Dir: Dir->getName());
2240 for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
2241 I != E && !EC; I.increment(EC)) {
2242 if (auto FE = SourceMgr.getFileManager().getOptionalFileRef(Filename: I->path())) {
2243 Module::Header Header = {.NameAsWritten: "", .PathRelativeToRootModuleDirectory: std::string(I->path()), .Entry: *FE};
2244 Headers.push_back(Elt: std::move(Header));
2245 }
2246 }
2247
2248 // Sort header paths so that the pcm doesn't depend on iteration order.
2249 llvm::stable_sort(Range&: Headers, C: compareModuleHeaders);
2250
2251 for (auto &Header : Headers)
2252 Map.addHeader(Mod: ActiveModule, Header: std::move(Header), Role: ModuleMap::TextualHeader);
2253 return;
2254 }
2255
2256 if (Module *OwningModule = Map.UmbrellaDirs[*Dir]) {
2257 Diags.Report(Loc: UDD.Location, DiagID: diag::err_mmap_umbrella_clash)
2258 << OwningModule->getFullModuleName();
2259 HadError = true;
2260 return;
2261 }
2262
2263 // Record this umbrella directory.
2264 Map.setUmbrellaDirAsWritten(Mod: ActiveModule, UmbrellaDir: *Dir, NameAsWritten: DirNameAsWritten, PathRelativeToRootModuleDirectory: DirName,
2265 Loc: UDD.Location);
2266}
2267
2268void ModuleMapLoader::handleExportDecl(const modulemap::ExportDecl &ED) {
2269 Module::UnresolvedExportDecl Unresolved = {.ExportLoc: ED.Location, .Id: ED.Id, .Wildcard: ED.Wildcard};
2270 ActiveModule->UnresolvedExports.push_back(Elt: Unresolved);
2271}
2272
2273void ModuleMapLoader::handleExportAsDecl(const modulemap::ExportAsDecl &EAD) {
2274 const auto &ModName = EAD.Id.front();
2275
2276 if (!ActiveModule->ExportAsModule.empty()) {
2277 if (ActiveModule->ExportAsModule == ModName.first) {
2278 Diags.Report(Loc: ModName.second, DiagID: diag::warn_mmap_redundant_export_as)
2279 << ActiveModule->Name << ModName.first;
2280 } else {
2281 Diags.Report(Loc: ModName.second, DiagID: diag::err_mmap_conflicting_export_as)
2282 << ActiveModule->Name << ActiveModule->ExportAsModule
2283 << ModName.first;
2284 }
2285 }
2286
2287 ActiveModule->ExportAsModule = ModName.first;
2288 Map.addLinkAsDependency(Mod: ActiveModule);
2289}
2290
2291void ModuleMapLoader::handleUseDecl(const modulemap::UseDecl &UD) {
2292 if (ActiveModule->Parent)
2293 Diags.Report(Loc: UD.Location, DiagID: diag::err_mmap_use_decl_submodule);
2294 else
2295 ActiveModule->UnresolvedDirectUses.push_back(Elt: UD.Id);
2296}
2297
2298void ModuleMapLoader::handleLinkDecl(const modulemap::LinkDecl &LD) {
2299 ActiveModule->LinkLibraries.push_back(
2300 Elt: Module::LinkLibrary(std::string{LD.Library}, LD.Framework));
2301}
2302
2303void ModuleMapLoader::handleConfigMacros(
2304 const modulemap::ConfigMacrosDecl &CMD) {
2305 if (ActiveModule->Parent) {
2306 Diags.Report(Loc: CMD.Location, DiagID: diag::err_mmap_config_macro_submodule);
2307 return;
2308 }
2309
2310 // TODO: Is this really the behavior we want for multiple config_macros
2311 // declarations? If any of them are exhaustive then all of them are.
2312 if (CMD.Exhaustive) {
2313 ActiveModule->ConfigMacrosExhaustive = true;
2314 }
2315 ActiveModule->ConfigMacros.insert(position: ActiveModule->ConfigMacros.end(),
2316 first: CMD.Macros.begin(), last: CMD.Macros.end());
2317}
2318
2319void ModuleMapLoader::handleConflict(const modulemap::ConflictDecl &CD) {
2320 Module::UnresolvedConflict Conflict;
2321
2322 Conflict.Id = CD.Id;
2323 Conflict.Message = CD.Message;
2324
2325 // FIXME: when we move to C++20 we should consider using emplace_back
2326 ActiveModule->UnresolvedConflicts.push_back(x: std::move(Conflict));
2327}
2328
2329void ModuleMapLoader::handleInferredModuleDecl(
2330 const modulemap::ModuleDecl &MD) {
2331 SourceLocation StarLoc = MD.Id.front().second;
2332
2333 // Inferred modules must be submodules.
2334 if (!ActiveModule && !MD.Framework) {
2335 Diags.Report(Loc: StarLoc, DiagID: diag::err_mmap_top_level_inferred_submodule);
2336 return;
2337 }
2338
2339 if (ActiveModule) {
2340 // Inferred modules must have umbrella directories.
2341 if (ActiveModule->IsAvailable && !ActiveModule->getEffectiveUmbrellaDir()) {
2342 Diags.Report(Loc: StarLoc, DiagID: diag::err_mmap_inferred_no_umbrella);
2343 return;
2344 }
2345
2346 // Check for redefinition of an inferred module.
2347 if (ActiveModule->InferSubmodules) {
2348 Diags.Report(Loc: StarLoc, DiagID: diag::err_mmap_inferred_redef);
2349 if (ActiveModule->InferredSubmoduleLoc.isValid())
2350 Diags.Report(Loc: ActiveModule->InferredSubmoduleLoc,
2351 DiagID: diag::note_mmap_prev_definition);
2352 return;
2353 }
2354
2355 // Check for the 'framework' keyword, which is not permitted here.
2356 if (MD.Framework) {
2357 Diags.Report(Loc: StarLoc, DiagID: diag::err_mmap_inferred_framework_submodule);
2358 return;
2359 }
2360 } else if (MD.Explicit) {
2361 Diags.Report(Loc: StarLoc, DiagID: diag::err_mmap_explicit_inferred_framework);
2362 return;
2363 }
2364
2365 if (ActiveModule) {
2366 // Note that we have an inferred submodule.
2367 ActiveModule->InferSubmodules = true;
2368 ActiveModule->InferredSubmoduleLoc = StarLoc;
2369 ActiveModule->InferExplicitSubmodules = MD.Explicit;
2370 } else {
2371 // We'll be inferring framework modules for this directory.
2372 auto &InfDir = Map.InferredDirectories[Directory];
2373 InfDir.InferModules = true;
2374 InfDir.Attrs = MD.Attrs;
2375 InfDir.ModuleMapFID = ModuleMapFID;
2376 // FIXME: Handle the 'framework' keyword.
2377 }
2378
2379 for (const modulemap::Decl &Decl : MD.Decls) {
2380 std::visit(
2381 visitor: llvm::makeVisitor(
2382 Callables: [&](const auto &Other) {
2383 Diags.Report(Other.Location,
2384 diag::err_mmap_expected_inferred_member)
2385 << (ActiveModule != nullptr);
2386 },
2387 Callables: [&](const modulemap::ExcludeDecl &ED) {
2388 // Only inferred frameworks can have exclude decls
2389 if (ActiveModule) {
2390 Diags.Report(Loc: ED.Location,
2391 DiagID: diag::err_mmap_expected_inferred_member)
2392 << (ActiveModule != nullptr);
2393 HadError = true;
2394 return;
2395 }
2396 Map.InferredDirectories[Directory].ExcludedModules.emplace_back(
2397 Args: ED.Module);
2398 },
2399 Callables: [&](const modulemap::ExportDecl &ED) {
2400 // Only inferred submodules can have export decls
2401 if (!ActiveModule) {
2402 Diags.Report(Loc: ED.Location,
2403 DiagID: diag::err_mmap_expected_inferred_member)
2404 << (ActiveModule != nullptr);
2405 HadError = true;
2406 return;
2407 }
2408
2409 if (ED.Wildcard && ED.Id.size() == 0)
2410 ActiveModule->InferExportWildcard = true;
2411 else
2412 Diags.Report(Loc: ED.Id.front().second,
2413 DiagID: diag::err_mmap_expected_export_wildcard);
2414 }),
2415 variants: Decl);
2416 }
2417}
2418
2419bool ModuleMapLoader::loadModuleDecl(const modulemap::ModuleDecl &MD) {
2420 handleModuleDecl(MD);
2421 return HadError;
2422}
2423
2424bool ModuleMapLoader::loadExternModuleDecl(
2425 const modulemap::ExternModuleDecl &EMD) {
2426 handleExternModuleDecl(EMD);
2427 return HadError;
2428}
2429
2430bool ModuleMapLoader::parseAndLoadModuleMapFile(
2431 const modulemap::ModuleMapFile &MMF) {
2432 for (const auto &Decl : MMF.Decls) {
2433 std::visit(
2434 visitor: llvm::makeVisitor(
2435 Callables: [&](const modulemap::ModuleDecl &MD) { handleModuleDecl(MD); },
2436 Callables: [&](const modulemap::ExternModuleDecl &EMD) {
2437 handleExternModuleDecl(EMD);
2438 }),
2439 variants: Decl);
2440 }
2441 return HadError;
2442}
2443
2444Module *ModuleMap::findOrLoadModule(StringRef Name) {
2445 llvm::StringMap<Module *>::const_iterator Known = Modules.find(Key: Name);
2446 if (Known != Modules.end())
2447 return Known->getValue();
2448
2449 auto ParsedMod = ParsedModules.find(Key: Name);
2450 if (ParsedMod == ParsedModules.end())
2451 return nullptr;
2452
2453 Diags.Report(DiagID: diag::remark_mmap_load_module) << Name;
2454
2455 for (const auto &ModuleDecl : ParsedMod->second) {
2456 const modulemap::ModuleMapFile &MMF = *ModuleDecl.first;
2457 ModuleMapLoader Loader(SourceMgr, Diags, const_cast<ModuleMap &>(*this),
2458 MMF.ID, *MMF.Dir, MMF.IsSystem,
2459 MMF.ImplicitlyDiscovered);
2460 if (Loader.loadModuleDecl(MD: *ModuleDecl.second))
2461 return nullptr;
2462 }
2463
2464 return findModule(Name);
2465}
2466
2467bool ModuleMap::parseAndLoadModuleMapFile(FileEntryRef File, bool IsSystem,
2468 bool ImplicitlyDiscovered,
2469 DirectoryEntryRef Dir, FileID ID,
2470 unsigned *Offset,
2471 SourceLocation ExternModuleLoc) {
2472 assert(Target && "Missing target information");
2473 llvm::DenseMap<const FileEntry *, bool>::iterator Known =
2474 LoadedModuleMap.find(Val: File);
2475 if (Known != LoadedModuleMap.end())
2476 return Known->second;
2477
2478 // If the module map file wasn't already entered, do so now.
2479 if (ID.isInvalid()) {
2480 // TODO: The way we compute affecting module maps requires this to be a
2481 // local FileID. This should be changed to reuse loaded FileIDs when
2482 // available, and change the way that affecting module maps are
2483 // computed to not require this.
2484 FileID &LocalFID = ModuleMapLocalFileID[File];
2485 if (LocalFID.isInvalid()) {
2486 auto FileCharacter =
2487 IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
2488 LocalFID = SourceMgr.createFileID(SourceFile: File, IncludePos: ExternModuleLoc, FileCharacter);
2489 }
2490 ID = LocalFID;
2491 }
2492
2493 assert(Target && "Missing target information");
2494 std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(FID: ID);
2495 if (!Buffer)
2496 return LoadedModuleMap[File] = true;
2497 assert((!Offset || *Offset <= Buffer->getBufferSize()) &&
2498 "invalid buffer offset");
2499
2500 std::optional<modulemap::ModuleMapFile> MMF = modulemap::parseModuleMap(
2501 ID, Dir, SM&: SourceMgr, Diags, IsSystem, ImplicitlyDiscovered, Offset);
2502 bool Result = false;
2503 if (MMF) {
2504 Diags.Report(DiagID: diag::remark_mmap_load) << File.getName();
2505 ModuleMapLoader Loader(SourceMgr, Diags, *this, ID, Dir, IsSystem,
2506 ImplicitlyDiscovered);
2507 Result = Loader.parseAndLoadModuleMapFile(MMF: *MMF);
2508
2509 // Also record that this was parsed if it wasn't previously. This is used
2510 // for diagnostics.
2511 llvm::DenseMap<const FileEntry *,
2512 const modulemap::ModuleMapFile *>::iterator PKnown =
2513 ParsedModuleMap.find(Val: File);
2514 if (PKnown == ParsedModuleMap.end()) {
2515 ParsedModuleMaps.push_back(
2516 x: std::make_unique<modulemap::ModuleMapFile>(args: std::move(*MMF)));
2517 ParsedModuleMap[File] = &*ParsedModuleMaps.back();
2518 }
2519 }
2520 LoadedModuleMap[File] = Result;
2521
2522 // Notify callbacks that we observed it.
2523 // FIXME: We should only report module maps that were actually used.
2524 for (const auto &Cb : Callbacks)
2525 Cb->moduleMapFileRead(FileStart: MMF ? MMF->Start : SourceLocation(), File, IsSystem);
2526
2527 return Result;
2528}
2529