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