xref: /llvm-project/clang/lib/Lex/ModuleMap.cpp (revision 260fb2bc3f79019cae4e182a64c8752d3d25049e)
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/Lexer.h"
28 #include "clang/Lex/LiteralSupport.h"
29 #include "clang/Lex/Token.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/None.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallPtrSet.h"
34 #include "llvm/ADT/SmallString.h"
35 #include "llvm/ADT/SmallVector.h"
36 #include "llvm/ADT/StringMap.h"
37 #include "llvm/ADT/StringRef.h"
38 #include "llvm/ADT/StringSwitch.h"
39 #include "llvm/Support/Allocator.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/ErrorHandling.h"
42 #include "llvm/Support/MemoryBuffer.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/VirtualFileSystem.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstdint>
49 #include <cstring>
50 #include <string>
51 #include <system_error>
52 #include <utility>
53 
54 using namespace clang;
55 
56 void ModuleMapCallbacks::anchor() {}
57 
58 void ModuleMap::resolveLinkAsDependencies(Module *Mod) {
59   auto PendingLinkAs = PendingLinkAsModule.find(Mod->Name);
60   if (PendingLinkAs != PendingLinkAsModule.end()) {
61     for (auto &Name : PendingLinkAs->second) {
62       auto *M = findModule(Name.getKey());
63       if (M)
64         M->UseExportAsModuleLinkName = true;
65     }
66   }
67 }
68 
69 void ModuleMap::addLinkAsDependency(Module *Mod) {
70   if (findModule(Mod->ExportAsModule))
71     Mod->UseExportAsModuleLinkName = true;
72   else
73     PendingLinkAsModule[Mod->ExportAsModule].insert(Mod->Name);
74 }
75 
76 Module::HeaderKind ModuleMap::headerRoleToKind(ModuleHeaderRole Role) {
77   switch ((int)Role) {
78   default: llvm_unreachable("unknown header role");
79   case NormalHeader:
80     return Module::HK_Normal;
81   case PrivateHeader:
82     return Module::HK_Private;
83   case TextualHeader:
84     return Module::HK_Textual;
85   case PrivateHeader | TextualHeader:
86     return Module::HK_PrivateTextual;
87   }
88 }
89 
90 ModuleMap::ModuleHeaderRole
91 ModuleMap::headerKindToRole(Module::HeaderKind Kind) {
92   switch ((int)Kind) {
93   case Module::HK_Normal:
94     return NormalHeader;
95   case Module::HK_Private:
96     return PrivateHeader;
97   case Module::HK_Textual:
98     return TextualHeader;
99   case Module::HK_PrivateTextual:
100     return ModuleHeaderRole(PrivateHeader | TextualHeader);
101   case Module::HK_Excluded:
102     llvm_unreachable("unexpected header kind");
103   }
104   llvm_unreachable("unknown header kind");
105 }
106 
107 Module::ExportDecl
108 ModuleMap::resolveExport(Module *Mod,
109                          const Module::UnresolvedExportDecl &Unresolved,
110                          bool Complain) const {
111   // We may have just a wildcard.
112   if (Unresolved.Id.empty()) {
113     assert(Unresolved.Wildcard && "Invalid unresolved export");
114     return Module::ExportDecl(nullptr, true);
115   }
116 
117   // Resolve the module-id.
118   Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
119   if (!Context)
120     return {};
121 
122   return Module::ExportDecl(Context, Unresolved.Wildcard);
123 }
124 
125 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
126                                    bool Complain) const {
127   // Find the starting module.
128   Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
129   if (!Context) {
130     if (Complain)
131       Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
132       << Id[0].first << Mod->getFullModuleName();
133 
134     return nullptr;
135   }
136 
137   // Dig into the module path.
138   for (unsigned I = 1, N = Id.size(); I != N; ++I) {
139     Module *Sub = lookupModuleQualified(Id[I].first, Context);
140     if (!Sub) {
141       if (Complain)
142         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
143         << Id[I].first << Context->getFullModuleName()
144         << SourceRange(Id[0].second, Id[I-1].second);
145 
146       return nullptr;
147     }
148 
149     Context = Sub;
150   }
151 
152   return Context;
153 }
154 
155 /// Append to \p Paths the set of paths needed to get to the
156 /// subframework in which the given module lives.
157 static void appendSubframeworkPaths(Module *Mod,
158                                     SmallVectorImpl<char> &Path) {
159   // Collect the framework names from the given module to the top-level module.
160   SmallVector<StringRef, 2> Paths;
161   for (; Mod; Mod = Mod->Parent) {
162     if (Mod->IsFramework)
163       Paths.push_back(Mod->Name);
164   }
165 
166   if (Paths.empty())
167     return;
168 
169   // Add Frameworks/Name.framework for each subframework.
170   for (StringRef Framework : llvm::drop_begin(llvm::reverse(Paths)))
171     llvm::sys::path::append(Path, "Frameworks", Framework + ".framework");
172 }
173 
174 Optional<FileEntryRef> ModuleMap::findHeader(
175     Module *M, const Module::UnresolvedHeaderDirective &Header,
176     SmallVectorImpl<char> &RelativePathName, bool &NeedsFramework) {
177   // Search for the header file within the module's home directory.
178   auto *Directory = M->Directory;
179   SmallString<128> FullPathName(Directory->getName());
180 
181   auto GetFile = [&](StringRef Filename) -> Optional<FileEntryRef> {
182     auto File =
183         expectedToOptional(SourceMgr.getFileManager().getFileRef(Filename));
184     if (!File || (Header.Size && File->getSize() != *Header.Size) ||
185         (Header.ModTime && File->getModificationTime() != *Header.ModTime))
186       return None;
187     return *File;
188   };
189 
190   auto GetFrameworkFile = [&]() -> Optional<FileEntryRef> {
191     unsigned FullPathLength = FullPathName.size();
192     appendSubframeworkPaths(M, RelativePathName);
193     unsigned RelativePathLength = RelativePathName.size();
194 
195     // Check whether this file is in the public headers.
196     llvm::sys::path::append(RelativePathName, "Headers", Header.FileName);
197     llvm::sys::path::append(FullPathName, 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(RelativePathLength);
211     FullPathName.resize(FullPathLength);
212     llvm::sys::path::append(RelativePathName, "PrivateHeaders",
213                             Header.FileName);
214     llvm::sys::path::append(FullPathName, RelativePathName);
215     return GetFile(FullPathName);
216   };
217 
218   if (llvm::sys::path::is_absolute(Header.FileName)) {
219     RelativePathName.clear();
220     RelativePathName.append(Header.FileName.begin(), 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(RelativePathName, Header.FileName);
229   llvm::sys::path::append(FullPathName, RelativePathName);
230   auto NormalHdrFile = GetFile(FullPathName);
231 
232   if (!NormalHdrFile && Directory->getName().endswith(".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(Directory->getName());
237     RelativePathName.clear();
238     if (GetFrameworkFile()) {
239       Diags.Report(Header.FileNameLoc,
240                    diag::warn_mmap_incomplete_framework_module_declaration)
241           << Header.FileName << M->getFullModuleName();
242       NeedsFramework = true;
243     }
244     return None;
245   }
246 
247   return NormalHdrFile;
248 }
249 
250 void ModuleMap::resolveHeader(Module *Mod,
251                               const Module::UnresolvedHeaderDirective &Header,
252                               bool &NeedsFramework) {
253   SmallString<128> RelativePathName;
254   if (Optional<FileEntryRef> File =
255           findHeader(Mod, Header, RelativePathName, NeedsFramework)) {
256     if (Header.IsUmbrella) {
257       const DirectoryEntry *UmbrellaDir = &File->getDir().getDirEntry();
258       if (Module *UmbrellaMod = UmbrellaDirs[UmbrellaDir])
259         Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
260           << UmbrellaMod->getFullModuleName();
261       else
262         // Record this umbrella header.
263         setUmbrellaHeader(Mod, *File, Header.FileName, RelativePathName.str());
264     } else {
265       Module::Header H = {Header.FileName, std::string(RelativePathName.str()),
266                           *File};
267       if (Header.Kind == Module::HK_Excluded)
268         excludeHeader(Mod, H);
269       else
270         addHeader(Mod, H, headerKindToRole(Header.Kind));
271     }
272   } else if (Header.HasBuiltinHeader && !Header.Size && !Header.ModTime) {
273     // There's a builtin header but no corresponding on-disk header. Assume
274     // this was supposed to modularize the builtin header alone.
275   } else if (Header.Kind == Module::HK_Excluded) {
276     // Ignore missing excluded header files. They're optional anyway.
277   } else {
278     // If we find a module that has a missing header, we mark this module as
279     // unavailable and store the header directive for displaying diagnostics.
280     Mod->MissingHeaders.push_back(Header);
281     // A missing header with stat information doesn't make the module
282     // unavailable; this keeps our behavior consistent as headers are lazily
283     // resolved. (Such a module still can't be built though, except from
284     // preprocessed source.)
285     if (!Header.Size && !Header.ModTime)
286       Mod->markUnavailable(/*Unimportable=*/false);
287   }
288 }
289 
290 bool ModuleMap::resolveAsBuiltinHeader(
291     Module *Mod, const Module::UnresolvedHeaderDirective &Header) {
292   if (Header.Kind == Module::HK_Excluded ||
293       llvm::sys::path::is_absolute(Header.FileName) ||
294       Mod->isPartOfFramework() || !Mod->IsSystem || Header.IsUmbrella ||
295       !BuiltinIncludeDir || BuiltinIncludeDir == Mod->Directory ||
296       !isBuiltinHeader(Header.FileName))
297     return false;
298 
299   // This is a system module with a top-level header. This header
300   // may have a counterpart (or replacement) in the set of headers
301   // supplied by Clang. Find that builtin header.
302   SmallString<128> Path;
303   llvm::sys::path::append(Path, BuiltinIncludeDir->getName(), Header.FileName);
304   auto File = SourceMgr.getFileManager().getFile(Path);
305   if (!File)
306     return false;
307 
308   auto Role = headerKindToRole(Header.Kind);
309   Module::Header H = {Header.FileName, std::string(Path.str()), *File};
310   addHeader(Mod, H, Role);
311   return true;
312 }
313 
314 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
315                      const LangOptions &LangOpts, const TargetInfo *Target,
316                      HeaderSearch &HeaderInfo)
317     : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
318       HeaderInfo(HeaderInfo) {
319   MMapLangOpts.LineComment = true;
320 }
321 
322 ModuleMap::~ModuleMap() {
323   for (auto &M : Modules)
324     delete M.getValue();
325   for (auto *M : ShadowModules)
326     delete M;
327 }
328 
329 void ModuleMap::setTarget(const TargetInfo &Target) {
330   assert((!this->Target || this->Target == &Target) &&
331          "Improper target override");
332   this->Target = &Target;
333 }
334 
335 /// "Sanitize" a filename so that it can be used as an identifier.
336 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
337                                               SmallVectorImpl<char> &Buffer) {
338   if (Name.empty())
339     return Name;
340 
341   if (!isValidAsciiIdentifier(Name)) {
342     // If we don't already have something with the form of an identifier,
343     // create a buffer with the sanitized name.
344     Buffer.clear();
345     if (isDigit(Name[0]))
346       Buffer.push_back('_');
347     Buffer.reserve(Buffer.size() + Name.size());
348     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
349       if (isAsciiIdentifierContinue(Name[I]))
350         Buffer.push_back(Name[I]);
351       else
352         Buffer.push_back('_');
353     }
354 
355     Name = StringRef(Buffer.data(), Buffer.size());
356   }
357 
358   while (llvm::StringSwitch<bool>(Name)
359 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
360 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
361 #include "clang/Basic/TokenKinds.def"
362            .Default(false)) {
363     if (Name.data() != Buffer.data())
364       Buffer.append(Name.begin(), Name.end());
365     Buffer.push_back('_');
366     Name = StringRef(Buffer.data(), Buffer.size());
367   }
368 
369   return Name;
370 }
371 
372 /// Determine whether the given file name is the name of a builtin
373 /// header, supplied by Clang to replace, override, or augment existing system
374 /// headers.
375 bool ModuleMap::isBuiltinHeader(StringRef FileName) {
376   return llvm::StringSwitch<bool>(FileName)
377            .Case("float.h", true)
378            .Case("iso646.h", true)
379            .Case("limits.h", true)
380            .Case("stdalign.h", true)
381            .Case("stdarg.h", true)
382            .Case("stdatomic.h", true)
383            .Case("stdbool.h", true)
384            .Case("stddef.h", true)
385            .Case("stdint.h", true)
386            .Case("tgmath.h", true)
387            .Case("unwind.h", true)
388            .Default(false);
389 }
390 
391 bool ModuleMap::isBuiltinHeader(const FileEntry *File) {
392   return File->getDir() == BuiltinIncludeDir &&
393          ModuleMap::isBuiltinHeader(llvm::sys::path::filename(File->getName()));
394 }
395 
396 ModuleMap::HeadersMap::iterator
397 ModuleMap::findKnownHeader(const FileEntry *File) {
398   resolveHeaderDirectives(File);
399   HeadersMap::iterator Known = Headers.find(File);
400   if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
401       Known == Headers.end() && ModuleMap::isBuiltinHeader(File)) {
402     HeaderInfo.loadTopLevelSystemModules();
403     return Headers.find(File);
404   }
405   return Known;
406 }
407 
408 ModuleMap::KnownHeader
409 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
410                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
411   if (UmbrellaDirs.empty())
412     return {};
413 
414   const DirectoryEntry *Dir = File->getDir();
415   assert(Dir && "file in no directory");
416 
417   // Note: as an egregious but useful hack we use the real path here, because
418   // frameworks moving from top-level frameworks to embedded frameworks tend
419   // to be symlinked from the top-level location to the embedded location,
420   // and we need to resolve lookups as if we had found the embedded location.
421   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
422 
423   // Keep walking up the directory hierarchy, looking for a directory with
424   // an umbrella header.
425   do {
426     auto KnownDir = UmbrellaDirs.find(Dir);
427     if (KnownDir != UmbrellaDirs.end())
428       return KnownHeader(KnownDir->second, NormalHeader);
429 
430     IntermediateDirs.push_back(Dir);
431 
432     // Retrieve our parent path.
433     DirName = llvm::sys::path::parent_path(DirName);
434     if (DirName.empty())
435       break;
436 
437     // Resolve the parent path to a directory entry.
438     if (auto DirEntry = SourceMgr.getFileManager().getDirectory(DirName))
439       Dir = *DirEntry;
440     else
441       Dir = nullptr;
442   } while (Dir);
443   return {};
444 }
445 
446 static bool violatesPrivateInclude(Module *RequestingModule,
447                                    const FileEntry *IncFileEnt,
448                                    ModuleMap::KnownHeader Header) {
449 #ifndef NDEBUG
450   if (Header.getRole() & ModuleMap::PrivateHeader) {
451     // Check for consistency between the module header role
452     // as obtained from the lookup and as obtained from the module.
453     // This check is not cheap, so enable it only for debugging.
454     bool IsPrivate = false;
455     SmallVectorImpl<Module::Header> *HeaderList[] = {
456         &Header.getModule()->Headers[Module::HK_Private],
457         &Header.getModule()->Headers[Module::HK_PrivateTextual]};
458     for (auto *Hs : HeaderList)
459       IsPrivate |= llvm::any_of(
460           *Hs, [&](const Module::Header &H) { return H.Entry == IncFileEnt; });
461     assert(IsPrivate && "inconsistent headers and roles");
462   }
463 #endif
464   return !Header.isAccessibleFrom(RequestingModule);
465 }
466 
467 static Module *getTopLevelOrNull(Module *M) {
468   return M ? M->getTopLevelModule() : nullptr;
469 }
470 
471 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
472                                         bool RequestingModuleIsModuleInterface,
473                                         SourceLocation FilenameLoc,
474                                         StringRef Filename, FileEntryRef File) {
475   // No errors for indirect modules. This may be a bit of a problem for modules
476   // with no source files.
477   if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
478     return;
479 
480   if (RequestingModule) {
481     resolveUses(RequestingModule, /*Complain=*/false);
482     resolveHeaderDirectives(RequestingModule, /*File=*/llvm::None);
483   }
484 
485   bool Excluded = false;
486   Module *Private = nullptr;
487   Module *NotUsed = nullptr;
488 
489   HeadersMap::iterator Known = findKnownHeader(File);
490   if (Known != Headers.end()) {
491     for (const KnownHeader &Header : Known->second) {
492       // Remember private headers for later printing of a diagnostic.
493       if (violatesPrivateInclude(RequestingModule, File, Header)) {
494         Private = Header.getModule();
495         continue;
496       }
497 
498       // If uses need to be specified explicitly, we are only allowed to return
499       // modules that are explicitly used by the requesting module.
500       if (RequestingModule && LangOpts.ModulesDeclUse &&
501           !RequestingModule->directlyUses(Header.getModule())) {
502         NotUsed = Header.getModule();
503         continue;
504       }
505 
506       // We have found a module that we can happily use.
507       return;
508     }
509 
510     Excluded = true;
511   }
512 
513   // We have found a header, but it is private.
514   if (Private) {
515     Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module)
516         << Filename;
517     return;
518   }
519 
520   // We have found a module, but we don't use it.
521   if (NotUsed) {
522     Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
523         << RequestingModule->getTopLevelModule()->Name << Filename;
524     return;
525   }
526 
527   if (Excluded || isHeaderInUmbrellaDirs(File))
528     return;
529 
530   // At this point, only non-modular includes remain.
531 
532   if (RequestingModule && LangOpts.ModulesStrictDeclUse) {
533     Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
534         << RequestingModule->getTopLevelModule()->Name << Filename;
535   } else if (RequestingModule && RequestingModuleIsModuleInterface &&
536              LangOpts.isCompilingModule()) {
537     // Do not diagnose when we are not compiling a module.
538     diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
539         diag::warn_non_modular_include_in_framework_module :
540         diag::warn_non_modular_include_in_module;
541     Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName()
542         << File.getName();
543   }
544 }
545 
546 static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
547                                 const ModuleMap::KnownHeader &Old) {
548   // Prefer available modules.
549   // FIXME: Considering whether the module is available rather than merely
550   // importable is non-hermetic and can result in surprising behavior for
551   // prebuilt modules. Consider only checking for importability here.
552   if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable())
553     return true;
554 
555   // Prefer a public header over a private header.
556   if ((New.getRole() & ModuleMap::PrivateHeader) !=
557       (Old.getRole() & ModuleMap::PrivateHeader))
558     return !(New.getRole() & ModuleMap::PrivateHeader);
559 
560   // Prefer a non-textual header over a textual header.
561   if ((New.getRole() & ModuleMap::TextualHeader) !=
562       (Old.getRole() & ModuleMap::TextualHeader))
563     return !(New.getRole() & ModuleMap::TextualHeader);
564 
565   // Don't have a reason to choose between these. Just keep the first one.
566   return false;
567 }
568 
569 ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
570                                                       bool AllowTextual) {
571   auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
572     if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader)
573       return {};
574     return R;
575   };
576 
577   HeadersMap::iterator Known = findKnownHeader(File);
578   if (Known != Headers.end()) {
579     ModuleMap::KnownHeader Result;
580     // Iterate over all modules that 'File' is part of to find the best fit.
581     for (KnownHeader &H : Known->second) {
582       // Prefer a header from the source module over all others.
583       if (H.getModule()->getTopLevelModule() == SourceModule)
584         return MakeResult(H);
585       if (!Result || isBetterKnownHeader(H, Result))
586         Result = H;
587     }
588     return MakeResult(Result);
589   }
590 
591   return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File));
592 }
593 
594 ModuleMap::KnownHeader
595 ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
596   assert(!Headers.count(File) && "already have a module for this header");
597 
598   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
599   KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
600   if (H) {
601     Module *Result = H.getModule();
602 
603     // Search up the module stack until we find a module with an umbrella
604     // directory.
605     Module *UmbrellaModule = Result;
606     while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
607       UmbrellaModule = UmbrellaModule->Parent;
608 
609     if (UmbrellaModule->InferSubmodules) {
610       const FileEntry *UmbrellaModuleMap =
611           getModuleMapFileForUniquing(UmbrellaModule);
612 
613       // Infer submodules for each of the directories we found between
614       // the directory of the umbrella header and the directory where
615       // the actual header is located.
616       bool Explicit = UmbrellaModule->InferExplicitSubmodules;
617 
618       for (const DirectoryEntry *SkippedDir : llvm::reverse(SkippedDirs)) {
619         // Find or create the module that corresponds to this directory name.
620         SmallString<32> NameBuf;
621         StringRef Name = sanitizeFilenameAsIdentifier(
622             llvm::sys::path::stem(SkippedDir->getName()), NameBuf);
623         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
624                                     Explicit).first;
625         InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
626         Result->IsInferred = true;
627 
628         // Associate the module and the directory.
629         UmbrellaDirs[SkippedDir] = Result;
630 
631         // If inferred submodules export everything they import, add a
632         // wildcard to the set of exports.
633         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
634           Result->Exports.push_back(Module::ExportDecl(nullptr, true));
635       }
636 
637       // Infer a submodule with the same name as this header file.
638       SmallString<32> NameBuf;
639       StringRef Name = sanitizeFilenameAsIdentifier(
640                          llvm::sys::path::stem(File->getName()), NameBuf);
641       Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
642                                   Explicit).first;
643       InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
644       Result->IsInferred = true;
645       Result->addTopHeader(File);
646 
647       // If inferred submodules export everything they import, add a
648       // wildcard to the set of exports.
649       if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
650         Result->Exports.push_back(Module::ExportDecl(nullptr, true));
651     } else {
652       // Record each of the directories we stepped through as being part of
653       // the module we found, since the umbrella header covers them all.
654       for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
655         UmbrellaDirs[SkippedDirs[I]] = Result;
656     }
657 
658     KnownHeader Header(Result, NormalHeader);
659     Headers[File].push_back(Header);
660     return Header;
661   }
662 
663   return {};
664 }
665 
666 ArrayRef<ModuleMap::KnownHeader>
667 ModuleMap::findAllModulesForHeader(const FileEntry *File) {
668   HeadersMap::iterator Known = findKnownHeader(File);
669   if (Known != Headers.end())
670     return Known->second;
671 
672   if (findOrCreateModuleForHeaderInUmbrellaDir(File))
673     return Headers.find(File)->second;
674 
675   return None;
676 }
677 
678 ArrayRef<ModuleMap::KnownHeader>
679 ModuleMap::findResolvedModulesForHeader(const FileEntry *File) const {
680   // FIXME: Is this necessary?
681   resolveHeaderDirectives(File);
682   auto It = Headers.find(File);
683   if (It == Headers.end())
684     return None;
685   return It->second;
686 }
687 
688 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
689   return isHeaderUnavailableInModule(Header, nullptr);
690 }
691 
692 bool
693 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
694                                        const Module *RequestingModule) const {
695   resolveHeaderDirectives(Header);
696   HeadersMap::const_iterator Known = Headers.find(Header);
697   if (Known != Headers.end()) {
698     for (SmallVectorImpl<KnownHeader>::const_iterator
699              I = Known->second.begin(),
700              E = Known->second.end();
701          I != E; ++I) {
702 
703       if (I->isAvailable() &&
704           (!RequestingModule ||
705            I->getModule()->isSubModuleOf(RequestingModule))) {
706         // When no requesting module is available, the caller is looking if a
707         // header is part a module by only looking into the module map. This is
708         // done by warn_uncovered_module_header checks; don't consider textual
709         // headers part of it in this mode, otherwise we get misleading warnings
710         // that a umbrella header is not including a textual header.
711         if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader)
712           continue;
713         return false;
714       }
715     }
716     return true;
717   }
718 
719   const DirectoryEntry *Dir = Header->getDir();
720   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
721   StringRef DirName = Dir->getName();
722 
723   auto IsUnavailable = [&](const Module *M) {
724     return !M->isAvailable() && (!RequestingModule ||
725                                  M->isSubModuleOf(RequestingModule));
726   };
727 
728   // Keep walking up the directory hierarchy, looking for a directory with
729   // an umbrella header.
730   do {
731     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
732       = UmbrellaDirs.find(Dir);
733     if (KnownDir != UmbrellaDirs.end()) {
734       Module *Found = KnownDir->second;
735       if (IsUnavailable(Found))
736         return true;
737 
738       // Search up the module stack until we find a module with an umbrella
739       // directory.
740       Module *UmbrellaModule = Found;
741       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
742         UmbrellaModule = UmbrellaModule->Parent;
743 
744       if (UmbrellaModule->InferSubmodules) {
745         for (const DirectoryEntry *SkippedDir : llvm::reverse(SkippedDirs)) {
746           // Find or create the module that corresponds to this directory name.
747           SmallString<32> NameBuf;
748           StringRef Name = sanitizeFilenameAsIdentifier(
749               llvm::sys::path::stem(SkippedDir->getName()), NameBuf);
750           Found = lookupModuleQualified(Name, Found);
751           if (!Found)
752             return false;
753           if (IsUnavailable(Found))
754             return true;
755         }
756 
757         // Infer a submodule with the same name as this header file.
758         SmallString<32> NameBuf;
759         StringRef Name = sanitizeFilenameAsIdentifier(
760                            llvm::sys::path::stem(Header->getName()),
761                            NameBuf);
762         Found = lookupModuleQualified(Name, Found);
763         if (!Found)
764           return false;
765       }
766 
767       return IsUnavailable(Found);
768     }
769 
770     SkippedDirs.push_back(Dir);
771 
772     // Retrieve our parent path.
773     DirName = llvm::sys::path::parent_path(DirName);
774     if (DirName.empty())
775       break;
776 
777     // Resolve the parent path to a directory entry.
778     if (auto DirEntry = SourceMgr.getFileManager().getDirectory(DirName))
779       Dir = *DirEntry;
780     else
781       Dir = nullptr;
782   } while (Dir);
783 
784   return false;
785 }
786 
787 Module *ModuleMap::findModule(StringRef Name) const {
788   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
789   if (Known != Modules.end())
790     return Known->getValue();
791 
792   return nullptr;
793 }
794 
795 Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
796                                            Module *Context) const {
797   for(; Context; Context = Context->Parent) {
798     if (Module *Sub = lookupModuleQualified(Name, Context))
799       return Sub;
800   }
801 
802   return findModule(Name);
803 }
804 
805 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
806   if (!Context)
807     return findModule(Name);
808 
809   return Context->findSubmodule(Name);
810 }
811 
812 std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
813                                                         Module *Parent,
814                                                         bool IsFramework,
815                                                         bool IsExplicit) {
816   // Try to find an existing module with this name.
817   if (Module *Sub = lookupModuleQualified(Name, Parent))
818     return std::make_pair(Sub, false);
819 
820   // Create a new module with this name.
821   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
822                               IsExplicit, NumCreatedModules++);
823   if (!Parent) {
824     if (LangOpts.CurrentModule == Name)
825       SourceModule = Result;
826     Modules[Name] = Result;
827     ModuleScopeIDs[Result] = CurrentModuleScopeID;
828   }
829   return std::make_pair(Result, true);
830 }
831 
832 Module *ModuleMap::createGlobalModuleFragmentForModuleUnit(SourceLocation Loc,
833                                                            Module *Parent) {
834   auto *Result = new Module("<global>", Loc, Parent, /*IsFramework*/ false,
835                             /*IsExplicit*/ true, NumCreatedModules++);
836   Result->Kind = Module::GlobalModuleFragment;
837   // If the created module isn't owned by a parent, send it to PendingSubmodules
838   // to wait for its parent.
839   if (!Result->Parent)
840     PendingSubmodules.emplace_back(Result);
841   return Result;
842 }
843 
844 Module *
845 ModuleMap::createPrivateModuleFragmentForInterfaceUnit(Module *Parent,
846                                                        SourceLocation Loc) {
847   auto *Result =
848       new Module("<private>", Loc, Parent, /*IsFramework*/ false,
849                  /*IsExplicit*/ true, NumCreatedModules++);
850   Result->Kind = Module::PrivateModuleFragment;
851   return Result;
852 }
853 
854 Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,
855                                                 StringRef Name,
856                                                 Module *GlobalModule) {
857   assert(LangOpts.CurrentModule == Name && "module name mismatch");
858   assert(!Modules[Name] && "redefining existing module");
859 
860   auto *Result =
861       new Module(Name, Loc, nullptr, /*IsFramework*/ false,
862                  /*IsExplicit*/ false, NumCreatedModules++);
863   Result->Kind = Module::ModuleInterfaceUnit;
864   Modules[Name] = SourceModule = Result;
865 
866   // Reparent the current global module fragment as a submodule of this module.
867   for (auto &Submodule : PendingSubmodules) {
868     Submodule->setParent(Result);
869     Submodule.release(); // now owned by parent
870   }
871   PendingSubmodules.clear();
872 
873   // Mark the main source file as being within the newly-created module so that
874   // declarations and macros are properly visibility-restricted to it.
875   auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
876   assert(MainFile && "no input file for module interface");
877   Headers[MainFile].push_back(KnownHeader(Result, PrivateHeader));
878 
879   return Result;
880 }
881 
882 Module *ModuleMap::createHeaderModule(StringRef Name,
883                                       ArrayRef<Module::Header> Headers) {
884   assert(LangOpts.CurrentModule == Name && "module name mismatch");
885   assert(!Modules[Name] && "redefining existing module");
886 
887   auto *Result =
888       new Module(Name, SourceLocation(), nullptr, /*IsFramework*/ false,
889                  /*IsExplicit*/ false, NumCreatedModules++);
890   Result->Kind = Module::ModuleInterfaceUnit;
891   Modules[Name] = SourceModule = Result;
892 
893   for (const Module::Header &H : Headers) {
894     auto *M = new Module(H.NameAsWritten, SourceLocation(), Result,
895                          /*IsFramework*/ false,
896                          /*IsExplicit*/ true, NumCreatedModules++);
897     // Header modules are implicitly 'export *'.
898     M->Exports.push_back(Module::ExportDecl(nullptr, true));
899     addHeader(M, H, NormalHeader);
900   }
901 
902   return Result;
903 }
904 
905 Module *ModuleMap::createHeaderUnit(SourceLocation Loc, StringRef Name,
906                                     Module::Header H) {
907   assert(LangOpts.CurrentModule == Name && "module name mismatch");
908   assert(!Modules[Name] && "redefining existing module");
909 
910   auto *Result = new Module(Name, Loc, nullptr, /*IsFramework*/ false,
911                             /*IsExplicit*/ false, NumCreatedModules++);
912   Result->Kind = Module::ModuleHeaderUnit;
913   Modules[Name] = SourceModule = Result;
914   addHeader(Result, H, NormalHeader);
915   return Result;
916 }
917 
918 /// For a framework module, infer the framework against which we
919 /// should link.
920 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
921                                FileManager &FileMgr) {
922   assert(Mod->IsFramework && "Can only infer linking for framework modules");
923   assert(!Mod->isSubFramework() &&
924          "Can only infer linking for top-level frameworks");
925 
926   SmallString<128> LibName;
927   LibName += FrameworkDir->getName();
928   llvm::sys::path::append(LibName, Mod->Name);
929 
930   // The library name of a framework has more than one possible extension since
931   // the introduction of the text-based dynamic library format. We need to check
932   // for both before we give up.
933   for (const char *extension : {"", ".tbd"}) {
934     llvm::sys::path::replace_extension(LibName, extension);
935     if (FileMgr.getFile(LibName)) {
936       Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
937                                                        /*IsFramework=*/true));
938       return;
939     }
940   }
941 }
942 
943 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
944                                         bool IsSystem, Module *Parent) {
945   Attributes Attrs;
946   Attrs.IsSystem = IsSystem;
947   return inferFrameworkModule(FrameworkDir, Attrs, Parent);
948 }
949 
950 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
951                                         Attributes Attrs, Module *Parent) {
952   // Note: as an egregious but useful hack we use the real path here, because
953   // we might be looking at an embedded framework that symlinks out to a
954   // top-level framework, and we need to infer as if we were naming the
955   // top-level framework.
956   StringRef FrameworkDirName =
957       SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
958 
959   // In case this is a case-insensitive filesystem, use the canonical
960   // directory name as the ModuleName, since modules are case-sensitive.
961   // FIXME: we should be able to give a fix-it hint for the correct spelling.
962   SmallString<32> ModuleNameStorage;
963   StringRef ModuleName = sanitizeFilenameAsIdentifier(
964       llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage);
965 
966   // Check whether we've already found this module.
967   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
968     return Mod;
969 
970   FileManager &FileMgr = SourceMgr.getFileManager();
971 
972   // If the framework has a parent path from which we're allowed to infer
973   // a framework module, do so.
974   const FileEntry *ModuleMapFile = nullptr;
975   if (!Parent) {
976     // Determine whether we're allowed to infer a module map.
977     bool canInfer = false;
978     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
979       // Figure out the parent path.
980       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
981       if (auto ParentDir = FileMgr.getDirectory(Parent)) {
982         // Check whether we have already looked into the parent directory
983         // for a module map.
984         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
985           inferred = InferredDirectories.find(*ParentDir);
986         if (inferred == InferredDirectories.end()) {
987           // We haven't looked here before. Load a module map, if there is
988           // one.
989           bool IsFrameworkDir = Parent.endswith(".framework");
990           if (const FileEntry *ModMapFile =
991                 HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
992             parseModuleMapFile(ModMapFile, Attrs.IsSystem, *ParentDir);
993             inferred = InferredDirectories.find(*ParentDir);
994           }
995 
996           if (inferred == InferredDirectories.end())
997             inferred = InferredDirectories.insert(
998                          std::make_pair(*ParentDir, InferredDirectory())).first;
999         }
1000 
1001         if (inferred->second.InferModules) {
1002           // We're allowed to infer for this directory, but make sure it's okay
1003           // to infer this particular module.
1004           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
1005           canInfer =
1006               !llvm::is_contained(inferred->second.ExcludedModules, Name);
1007 
1008           Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
1009           Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
1010           Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
1011           Attrs.NoUndeclaredIncludes |=
1012               inferred->second.Attrs.NoUndeclaredIncludes;
1013           ModuleMapFile = inferred->second.ModuleMapFile;
1014         }
1015       }
1016     }
1017 
1018     // If we're not allowed to infer a framework module, don't.
1019     if (!canInfer)
1020       return nullptr;
1021   } else
1022     ModuleMapFile = getModuleMapFileForUniquing(Parent);
1023 
1024 
1025   // Look for an umbrella header.
1026   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
1027   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
1028   auto UmbrellaHeader = FileMgr.getFile(UmbrellaName);
1029 
1030   // FIXME: If there's no umbrella header, we could probably scan the
1031   // framework to load *everything*. But, it's not clear that this is a good
1032   // idea.
1033   if (!UmbrellaHeader)
1034     return nullptr;
1035 
1036   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
1037                               /*IsFramework=*/true, /*IsExplicit=*/false,
1038                               NumCreatedModules++);
1039   InferredModuleAllowedBy[Result] = ModuleMapFile;
1040   Result->IsInferred = true;
1041   if (!Parent) {
1042     if (LangOpts.CurrentModule == ModuleName)
1043       SourceModule = Result;
1044     Modules[ModuleName] = Result;
1045     ModuleScopeIDs[Result] = CurrentModuleScopeID;
1046   }
1047 
1048   Result->IsSystem |= Attrs.IsSystem;
1049   Result->IsExternC |= Attrs.IsExternC;
1050   Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
1051   Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
1052   Result->Directory = FrameworkDir;
1053 
1054   // Chop off the first framework bit, as that is implied.
1055   StringRef RelativePath = UmbrellaName.str().substr(
1056       Result->getTopLevelModule()->Directory->getName().size());
1057   RelativePath = llvm::sys::path::relative_path(RelativePath);
1058 
1059   // umbrella header "umbrella-header-name"
1060   setUmbrellaHeader(Result, *UmbrellaHeader, ModuleName + ".h", RelativePath);
1061 
1062   // export *
1063   Result->Exports.push_back(Module::ExportDecl(nullptr, true));
1064 
1065   // module * { export * }
1066   Result->InferSubmodules = true;
1067   Result->InferExportWildcard = true;
1068 
1069   // Look for subframeworks.
1070   std::error_code EC;
1071   SmallString<128> SubframeworksDirName
1072     = StringRef(FrameworkDir->getName());
1073   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
1074   llvm::sys::path::native(SubframeworksDirName);
1075   llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem();
1076   for (llvm::vfs::directory_iterator
1077            Dir = FS.dir_begin(SubframeworksDirName, EC),
1078            DirEnd;
1079        Dir != DirEnd && !EC; Dir.increment(EC)) {
1080     if (!StringRef(Dir->path()).endswith(".framework"))
1081       continue;
1082 
1083     if (auto SubframeworkDir =
1084             FileMgr.getDirectory(Dir->path())) {
1085       // Note: as an egregious but useful hack, we use the real path here and
1086       // check whether it is actually a subdirectory of the parent directory.
1087       // This will not be the case if the 'subframework' is actually a symlink
1088       // out to a top-level framework.
1089       StringRef SubframeworkDirName =
1090           FileMgr.getCanonicalName(*SubframeworkDir);
1091       bool FoundParent = false;
1092       do {
1093         // Get the parent directory name.
1094         SubframeworkDirName
1095           = llvm::sys::path::parent_path(SubframeworkDirName);
1096         if (SubframeworkDirName.empty())
1097           break;
1098 
1099         if (auto SubDir = FileMgr.getDirectory(SubframeworkDirName)) {
1100           if (*SubDir == FrameworkDir) {
1101             FoundParent = true;
1102             break;
1103           }
1104         }
1105       } while (true);
1106 
1107       if (!FoundParent)
1108         continue;
1109 
1110       // FIXME: Do we want to warn about subframeworks without umbrella headers?
1111       inferFrameworkModule(*SubframeworkDir, Attrs, Result);
1112     }
1113   }
1114 
1115   // If the module is a top-level framework, automatically link against the
1116   // framework.
1117   if (!Result->isSubFramework()) {
1118     inferFrameworkLink(Result, FrameworkDir, FileMgr);
1119   }
1120 
1121   return Result;
1122 }
1123 
1124 Module *ModuleMap::createShadowedModule(StringRef Name, bool IsFramework,
1125                                         Module *ShadowingModule) {
1126 
1127   // Create a new module with this name.
1128   Module *Result =
1129       new Module(Name, SourceLocation(), /*Parent=*/nullptr, IsFramework,
1130                  /*IsExplicit=*/false, NumCreatedModules++);
1131   Result->ShadowingModule = ShadowingModule;
1132   Result->markUnavailable(/*Unimportable*/true);
1133   ModuleScopeIDs[Result] = CurrentModuleScopeID;
1134   ShadowModules.push_back(Result);
1135 
1136   return Result;
1137 }
1138 
1139 void ModuleMap::setUmbrellaHeader(
1140     Module *Mod, const FileEntry *UmbrellaHeader, const Twine &NameAsWritten,
1141     const Twine &PathRelativeToRootModuleDirectory) {
1142   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
1143   Mod->Umbrella = UmbrellaHeader;
1144   Mod->UmbrellaAsWritten = NameAsWritten.str();
1145   Mod->UmbrellaRelativeToRootModuleDirectory =
1146       PathRelativeToRootModuleDirectory.str();
1147   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
1148 
1149   // Notify callbacks that we just added a new header.
1150   for (const auto &Cb : Callbacks)
1151     Cb->moduleMapAddUmbrellaHeader(&SourceMgr.getFileManager(), UmbrellaHeader);
1152 }
1153 
1154 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
1155                                const Twine &NameAsWritten,
1156                                const Twine &PathRelativeToRootModuleDirectory) {
1157   Mod->Umbrella = UmbrellaDir;
1158   Mod->UmbrellaAsWritten = NameAsWritten.str();
1159   Mod->UmbrellaRelativeToRootModuleDirectory =
1160       PathRelativeToRootModuleDirectory.str();
1161   UmbrellaDirs[UmbrellaDir] = Mod;
1162 }
1163 
1164 void ModuleMap::addUnresolvedHeader(Module *Mod,
1165                                     Module::UnresolvedHeaderDirective Header,
1166                                     bool &NeedsFramework) {
1167   // If there is a builtin counterpart to this file, add it now so it can
1168   // wrap the system header.
1169   if (resolveAsBuiltinHeader(Mod, Header)) {
1170     // If we have both a builtin and system version of the file, the
1171     // builtin version may want to inject macros into the system header, so
1172     // force the system header to be treated as a textual header in this
1173     // case.
1174     Header.Kind = headerRoleToKind(ModuleMap::ModuleHeaderRole(
1175         headerKindToRole(Header.Kind) | ModuleMap::TextualHeader));
1176     Header.HasBuiltinHeader = true;
1177   }
1178 
1179   // If possible, don't stat the header until we need to. This requires the
1180   // user to have provided us with some stat information about the file.
1181   // FIXME: Add support for lazily stat'ing umbrella headers and excluded
1182   // headers.
1183   if ((Header.Size || Header.ModTime) && !Header.IsUmbrella &&
1184       Header.Kind != Module::HK_Excluded) {
1185     // We expect more variation in mtime than size, so if we're given both,
1186     // use the mtime as the key.
1187     if (Header.ModTime)
1188       LazyHeadersByModTime[*Header.ModTime].push_back(Mod);
1189     else
1190       LazyHeadersBySize[*Header.Size].push_back(Mod);
1191     Mod->UnresolvedHeaders.push_back(Header);
1192     return;
1193   }
1194 
1195   // We don't have stat information or can't defer looking this file up.
1196   // Perform the lookup now.
1197   resolveHeader(Mod, Header, NeedsFramework);
1198 }
1199 
1200 void ModuleMap::resolveHeaderDirectives(const FileEntry *File) const {
1201   auto BySize = LazyHeadersBySize.find(File->getSize());
1202   if (BySize != LazyHeadersBySize.end()) {
1203     for (auto *M : BySize->second)
1204       resolveHeaderDirectives(M, File);
1205     LazyHeadersBySize.erase(BySize);
1206   }
1207 
1208   auto ByModTime = LazyHeadersByModTime.find(File->getModificationTime());
1209   if (ByModTime != LazyHeadersByModTime.end()) {
1210     for (auto *M : ByModTime->second)
1211       resolveHeaderDirectives(M, File);
1212     LazyHeadersByModTime.erase(ByModTime);
1213   }
1214 }
1215 
1216 void ModuleMap::resolveHeaderDirectives(
1217     Module *Mod, llvm::Optional<const FileEntry *> File) const {
1218   bool NeedsFramework = false;
1219   SmallVector<Module::UnresolvedHeaderDirective, 1> NewHeaders;
1220   const auto Size = File ? File.value()->getSize() : 0;
1221   const auto ModTime = File ? File.value()->getModificationTime() : 0;
1222 
1223   for (auto &Header : Mod->UnresolvedHeaders) {
1224     if (File && ((Header.ModTime && Header.ModTime != ModTime) ||
1225                  (Header.Size && Header.Size != Size)))
1226       NewHeaders.push_back(Header);
1227     else
1228       // This operation is logically const; we're just changing how we represent
1229       // the header information for this file.
1230       const_cast<ModuleMap *>(this)->resolveHeader(Mod, Header, NeedsFramework);
1231   }
1232   Mod->UnresolvedHeaders.swap(NewHeaders);
1233 }
1234 
1235 void ModuleMap::addHeader(Module *Mod, Module::Header Header,
1236                           ModuleHeaderRole Role, bool Imported) {
1237   KnownHeader KH(Mod, Role);
1238 
1239   // Only add each header to the headers list once.
1240   // FIXME: Should we diagnose if a header is listed twice in the
1241   // same module definition?
1242   auto &HeaderList = Headers[Header.Entry];
1243   if (llvm::is_contained(HeaderList, KH))
1244     return;
1245 
1246   HeaderList.push_back(KH);
1247   Mod->Headers[headerRoleToKind(Role)].push_back(Header);
1248 
1249   bool isCompilingModuleHeader =
1250       LangOpts.isCompilingModule() && Mod->getTopLevelModule() == SourceModule;
1251   if (!Imported || isCompilingModuleHeader) {
1252     // When we import HeaderFileInfo, the external source is expected to
1253     // set the isModuleHeader flag itself.
1254     HeaderInfo.MarkFileModuleHeader(Header.Entry, Role,
1255                                     isCompilingModuleHeader);
1256   }
1257 
1258   // Notify callbacks that we just added a new header.
1259   for (const auto &Cb : Callbacks)
1260     Cb->moduleMapAddHeader(Header.Entry->getName());
1261 }
1262 
1263 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) {
1264   // Add this as a known header so we won't implicitly add it to any
1265   // umbrella directory module.
1266   // FIXME: Should we only exclude it from umbrella modules within the
1267   // specified module?
1268   (void) Headers[Header.Entry];
1269 
1270   Mod->Headers[Module::HK_Excluded].push_back(std::move(Header));
1271 }
1272 
1273 const FileEntry *
1274 ModuleMap::getContainingModuleMapFile(const Module *Module) const {
1275   if (Module->DefinitionLoc.isInvalid())
1276     return nullptr;
1277 
1278   return SourceMgr.getFileEntryForID(
1279            SourceMgr.getFileID(Module->DefinitionLoc));
1280 }
1281 
1282 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
1283   if (M->IsInferred) {
1284     assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
1285     return InferredModuleAllowedBy.find(M)->second;
1286   }
1287   return getContainingModuleMapFile(M);
1288 }
1289 
1290 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
1291   assert(M->IsInferred && "module not inferred");
1292   InferredModuleAllowedBy[M] = ModMap;
1293 }
1294 
1295 void ModuleMap::addAdditionalModuleMapFile(const Module *M,
1296                                            const FileEntry *ModuleMap) {
1297   AdditionalModMaps[M].insert(ModuleMap);
1298 }
1299 
1300 LLVM_DUMP_METHOD void ModuleMap::dump() {
1301   llvm::errs() << "Modules:";
1302   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
1303                                         MEnd = Modules.end();
1304        M != MEnd; ++M)
1305     M->getValue()->print(llvm::errs(), 2);
1306 
1307   llvm::errs() << "Headers:";
1308   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
1309        H != HEnd; ++H) {
1310     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
1311     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
1312                                                       E = H->second.end();
1313          I != E; ++I) {
1314       if (I != H->second.begin())
1315         llvm::errs() << ",";
1316       llvm::errs() << I->getModule()->getFullModuleName();
1317     }
1318     llvm::errs() << "\n";
1319   }
1320 }
1321 
1322 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
1323   auto Unresolved = std::move(Mod->UnresolvedExports);
1324   Mod->UnresolvedExports.clear();
1325   for (auto &UE : Unresolved) {
1326     Module::ExportDecl Export = resolveExport(Mod, UE, Complain);
1327     if (Export.getPointer() || Export.getInt())
1328       Mod->Exports.push_back(Export);
1329     else
1330       Mod->UnresolvedExports.push_back(UE);
1331   }
1332   return !Mod->UnresolvedExports.empty();
1333 }
1334 
1335 bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
1336   auto Unresolved = std::move(Mod->UnresolvedDirectUses);
1337   Mod->UnresolvedDirectUses.clear();
1338   for (auto &UDU : Unresolved) {
1339     Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
1340     if (DirectUse)
1341       Mod->DirectUses.push_back(DirectUse);
1342     else
1343       Mod->UnresolvedDirectUses.push_back(UDU);
1344   }
1345   return !Mod->UnresolvedDirectUses.empty();
1346 }
1347 
1348 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
1349   auto Unresolved = std::move(Mod->UnresolvedConflicts);
1350   Mod->UnresolvedConflicts.clear();
1351   for (auto &UC : Unresolved) {
1352     if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) {
1353       Module::Conflict Conflict;
1354       Conflict.Other = OtherMod;
1355       Conflict.Message = UC.Message;
1356       Mod->Conflicts.push_back(Conflict);
1357     } else
1358       Mod->UnresolvedConflicts.push_back(UC);
1359   }
1360   return !Mod->UnresolvedConflicts.empty();
1361 }
1362 
1363 //----------------------------------------------------------------------------//
1364 // Module map file parser
1365 //----------------------------------------------------------------------------//
1366 
1367 namespace clang {
1368 
1369   /// A token in a module map file.
1370   struct MMToken {
1371     enum TokenKind {
1372       Comma,
1373       ConfigMacros,
1374       Conflict,
1375       EndOfFile,
1376       HeaderKeyword,
1377       Identifier,
1378       Exclaim,
1379       ExcludeKeyword,
1380       ExplicitKeyword,
1381       ExportKeyword,
1382       ExportAsKeyword,
1383       ExternKeyword,
1384       FrameworkKeyword,
1385       LinkKeyword,
1386       ModuleKeyword,
1387       Period,
1388       PrivateKeyword,
1389       UmbrellaKeyword,
1390       UseKeyword,
1391       RequiresKeyword,
1392       Star,
1393       StringLiteral,
1394       IntegerLiteral,
1395       TextualKeyword,
1396       LBrace,
1397       RBrace,
1398       LSquare,
1399       RSquare
1400     } Kind;
1401 
1402     SourceLocation::UIntTy Location;
1403     unsigned StringLength;
1404     union {
1405       // If Kind != IntegerLiteral.
1406       const char *StringData;
1407 
1408       // If Kind == IntegerLiteral.
1409       uint64_t IntegerValue;
1410     };
1411 
1412     void clear() {
1413       Kind = EndOfFile;
1414       Location = 0;
1415       StringLength = 0;
1416       StringData = nullptr;
1417     }
1418 
1419     bool is(TokenKind K) const { return Kind == K; }
1420 
1421     SourceLocation getLocation() const {
1422       return SourceLocation::getFromRawEncoding(Location);
1423     }
1424 
1425     uint64_t getInteger() const {
1426       return Kind == IntegerLiteral ? IntegerValue : 0;
1427     }
1428 
1429     StringRef getString() const {
1430       return Kind == IntegerLiteral ? StringRef()
1431                                     : StringRef(StringData, StringLength);
1432     }
1433   };
1434 
1435   class ModuleMapParser {
1436     Lexer &L;
1437     SourceManager &SourceMgr;
1438 
1439     /// Default target information, used only for string literal
1440     /// parsing.
1441     const TargetInfo *Target;
1442 
1443     DiagnosticsEngine &Diags;
1444     ModuleMap &Map;
1445 
1446     /// The current module map file.
1447     const FileEntry *ModuleMapFile;
1448 
1449     /// Source location of most recent parsed module declaration
1450     SourceLocation CurrModuleDeclLoc;
1451 
1452     /// The directory that file names in this module map file should
1453     /// be resolved relative to.
1454     const DirectoryEntry *Directory;
1455 
1456     /// Whether this module map is in a system header directory.
1457     bool IsSystem;
1458 
1459     /// Whether an error occurred.
1460     bool HadError = false;
1461 
1462     /// Stores string data for the various string literals referenced
1463     /// during parsing.
1464     llvm::BumpPtrAllocator StringData;
1465 
1466     /// The current token.
1467     MMToken Tok;
1468 
1469     /// The active module.
1470     Module *ActiveModule = nullptr;
1471 
1472     /// Whether a module uses the 'requires excluded' hack to mark its
1473     /// contents as 'textual'.
1474     ///
1475     /// On older Darwin SDK versions, 'requires excluded' is used to mark the
1476     /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
1477     /// non-modular headers.  For backwards compatibility, we continue to
1478     /// support this idiom for just these modules, and map the headers to
1479     /// 'textual' to match the original intent.
1480     llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
1481 
1482     /// Consume the current token and return its location.
1483     SourceLocation consumeToken();
1484 
1485     /// Skip tokens until we reach the a token with the given kind
1486     /// (or the end of the file).
1487     void skipUntil(MMToken::TokenKind K);
1488 
1489     using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>;
1490 
1491     bool parseModuleId(ModuleId &Id);
1492     void parseModuleDecl();
1493     void parseExternModuleDecl();
1494     void parseRequiresDecl();
1495     void parseHeaderDecl(MMToken::TokenKind, SourceLocation LeadingLoc);
1496     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
1497     void parseExportDecl();
1498     void parseExportAsDecl();
1499     void parseUseDecl();
1500     void parseLinkDecl();
1501     void parseConfigMacros();
1502     void parseConflict();
1503     void parseInferredModuleDecl(bool Framework, bool Explicit);
1504 
1505     /// Private modules are canonicalized as Foo_Private. Clang provides extra
1506     /// module map search logic to find the appropriate private module when PCH
1507     /// is used with implicit module maps. Warn when private modules are written
1508     /// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
1509     void diagnosePrivateModules(SourceLocation ExplicitLoc,
1510                                 SourceLocation FrameworkLoc);
1511 
1512     using Attributes = ModuleMap::Attributes;
1513 
1514     bool parseOptionalAttributes(Attributes &Attrs);
1515 
1516   public:
1517     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
1518                              const TargetInfo *Target, DiagnosticsEngine &Diags,
1519                              ModuleMap &Map, const FileEntry *ModuleMapFile,
1520                              const DirectoryEntry *Directory, bool IsSystem)
1521         : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
1522           ModuleMapFile(ModuleMapFile), Directory(Directory),
1523           IsSystem(IsSystem) {
1524       Tok.clear();
1525       consumeToken();
1526     }
1527 
1528     bool parseModuleMapFile();
1529 
1530     bool terminatedByDirective() { return false; }
1531     SourceLocation getLocation() { return Tok.getLocation(); }
1532   };
1533 
1534 } // namespace clang
1535 
1536 SourceLocation ModuleMapParser::consumeToken() {
1537   SourceLocation Result = Tok.getLocation();
1538 
1539 retry:
1540   Tok.clear();
1541   Token LToken;
1542   L.LexFromRawLexer(LToken);
1543   Tok.Location = LToken.getLocation().getRawEncoding();
1544   switch (LToken.getKind()) {
1545   case tok::raw_identifier: {
1546     StringRef RI = LToken.getRawIdentifier();
1547     Tok.StringData = RI.data();
1548     Tok.StringLength = RI.size();
1549     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
1550                  .Case("config_macros", MMToken::ConfigMacros)
1551                  .Case("conflict", MMToken::Conflict)
1552                  .Case("exclude", MMToken::ExcludeKeyword)
1553                  .Case("explicit", MMToken::ExplicitKeyword)
1554                  .Case("export", MMToken::ExportKeyword)
1555                  .Case("export_as", MMToken::ExportAsKeyword)
1556                  .Case("extern", MMToken::ExternKeyword)
1557                  .Case("framework", MMToken::FrameworkKeyword)
1558                  .Case("header", MMToken::HeaderKeyword)
1559                  .Case("link", MMToken::LinkKeyword)
1560                  .Case("module", MMToken::ModuleKeyword)
1561                  .Case("private", MMToken::PrivateKeyword)
1562                  .Case("requires", MMToken::RequiresKeyword)
1563                  .Case("textual", MMToken::TextualKeyword)
1564                  .Case("umbrella", MMToken::UmbrellaKeyword)
1565                  .Case("use", MMToken::UseKeyword)
1566                  .Default(MMToken::Identifier);
1567     break;
1568   }
1569 
1570   case tok::comma:
1571     Tok.Kind = MMToken::Comma;
1572     break;
1573 
1574   case tok::eof:
1575     Tok.Kind = MMToken::EndOfFile;
1576     break;
1577 
1578   case tok::l_brace:
1579     Tok.Kind = MMToken::LBrace;
1580     break;
1581 
1582   case tok::l_square:
1583     Tok.Kind = MMToken::LSquare;
1584     break;
1585 
1586   case tok::period:
1587     Tok.Kind = MMToken::Period;
1588     break;
1589 
1590   case tok::r_brace:
1591     Tok.Kind = MMToken::RBrace;
1592     break;
1593 
1594   case tok::r_square:
1595     Tok.Kind = MMToken::RSquare;
1596     break;
1597 
1598   case tok::star:
1599     Tok.Kind = MMToken::Star;
1600     break;
1601 
1602   case tok::exclaim:
1603     Tok.Kind = MMToken::Exclaim;
1604     break;
1605 
1606   case tok::string_literal: {
1607     if (LToken.hasUDSuffix()) {
1608       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
1609       HadError = true;
1610       goto retry;
1611     }
1612 
1613     // Parse the string literal.
1614     LangOptions LangOpts;
1615     StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
1616     if (StringLiteral.hadError)
1617       goto retry;
1618 
1619     // Copy the string literal into our string data allocator.
1620     unsigned Length = StringLiteral.GetStringLength();
1621     char *Saved = StringData.Allocate<char>(Length + 1);
1622     memcpy(Saved, StringLiteral.GetString().data(), Length);
1623     Saved[Length] = 0;
1624 
1625     // Form the token.
1626     Tok.Kind = MMToken::StringLiteral;
1627     Tok.StringData = Saved;
1628     Tok.StringLength = Length;
1629     break;
1630   }
1631 
1632   case tok::numeric_constant: {
1633     // We don't support any suffixes or other complications.
1634     SmallString<32> SpellingBuffer;
1635     SpellingBuffer.resize(LToken.getLength() + 1);
1636     const char *Start = SpellingBuffer.data();
1637     unsigned Length =
1638         Lexer::getSpelling(LToken, Start, SourceMgr, Map.LangOpts);
1639     uint64_t Value;
1640     if (StringRef(Start, Length).getAsInteger(0, Value)) {
1641       Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
1642       HadError = true;
1643       goto retry;
1644     }
1645 
1646     Tok.Kind = MMToken::IntegerLiteral;
1647     Tok.IntegerValue = Value;
1648     break;
1649   }
1650 
1651   case tok::comment:
1652     goto retry;
1653 
1654   case tok::hash:
1655     // A module map can be terminated prematurely by
1656     //   #pragma clang module contents
1657     // When building the module, we'll treat the rest of the file as the
1658     // contents of the module.
1659     {
1660       auto NextIsIdent = [&](StringRef Str) -> bool {
1661         L.LexFromRawLexer(LToken);
1662         return !LToken.isAtStartOfLine() && LToken.is(tok::raw_identifier) &&
1663                LToken.getRawIdentifier() == Str;
1664       };
1665       if (NextIsIdent("pragma") && NextIsIdent("clang") &&
1666           NextIsIdent("module") && NextIsIdent("contents")) {
1667         Tok.Kind = MMToken::EndOfFile;
1668         break;
1669       }
1670     }
1671     [[fallthrough]];
1672 
1673   default:
1674     Diags.Report(Tok.getLocation(), diag::err_mmap_unknown_token);
1675     HadError = true;
1676     goto retry;
1677   }
1678 
1679   return Result;
1680 }
1681 
1682 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1683   unsigned braceDepth = 0;
1684   unsigned squareDepth = 0;
1685   do {
1686     switch (Tok.Kind) {
1687     case MMToken::EndOfFile:
1688       return;
1689 
1690     case MMToken::LBrace:
1691       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1692         return;
1693 
1694       ++braceDepth;
1695       break;
1696 
1697     case MMToken::LSquare:
1698       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1699         return;
1700 
1701       ++squareDepth;
1702       break;
1703 
1704     case MMToken::RBrace:
1705       if (braceDepth > 0)
1706         --braceDepth;
1707       else if (Tok.is(K))
1708         return;
1709       break;
1710 
1711     case MMToken::RSquare:
1712       if (squareDepth > 0)
1713         --squareDepth;
1714       else if (Tok.is(K))
1715         return;
1716       break;
1717 
1718     default:
1719       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1720         return;
1721       break;
1722     }
1723 
1724    consumeToken();
1725   } while (true);
1726 }
1727 
1728 /// Parse a module-id.
1729 ///
1730 ///   module-id:
1731 ///     identifier
1732 ///     identifier '.' module-id
1733 ///
1734 /// \returns true if an error occurred, false otherwise.
1735 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1736   Id.clear();
1737   do {
1738     if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
1739       Id.push_back(
1740           std::make_pair(std::string(Tok.getString()), Tok.getLocation()));
1741       consumeToken();
1742     } else {
1743       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1744       return true;
1745     }
1746 
1747     if (!Tok.is(MMToken::Period))
1748       break;
1749 
1750     consumeToken();
1751   } while (true);
1752 
1753   return false;
1754 }
1755 
1756 namespace {
1757 
1758   /// Enumerates the known attributes.
1759   enum AttributeKind {
1760     /// An unknown attribute.
1761     AT_unknown,
1762 
1763     /// The 'system' attribute.
1764     AT_system,
1765 
1766     /// The 'extern_c' attribute.
1767     AT_extern_c,
1768 
1769     /// The 'exhaustive' attribute.
1770     AT_exhaustive,
1771 
1772     /// The 'no_undeclared_includes' attribute.
1773     AT_no_undeclared_includes
1774   };
1775 
1776 } // namespace
1777 
1778 /// Private modules are canonicalized as Foo_Private. Clang provides extra
1779 /// module map search logic to find the appropriate private module when PCH
1780 /// is used with implicit module maps. Warn when private modules are written
1781 /// in other ways (FooPrivate and Foo.Private), providing notes and fixits.
1782 void ModuleMapParser::diagnosePrivateModules(SourceLocation ExplicitLoc,
1783                                              SourceLocation FrameworkLoc) {
1784   auto GenNoteAndFixIt = [&](StringRef BadName, StringRef Canonical,
1785                              const Module *M, SourceRange ReplLoc) {
1786     auto D = Diags.Report(ActiveModule->DefinitionLoc,
1787                           diag::note_mmap_rename_top_level_private_module);
1788     D << BadName << M->Name;
1789     D << FixItHint::CreateReplacement(ReplLoc, Canonical);
1790   };
1791 
1792   for (auto E = Map.module_begin(); E != Map.module_end(); ++E) {
1793     auto const *M = E->getValue();
1794     if (M->Directory != ActiveModule->Directory)
1795       continue;
1796 
1797     SmallString<128> FullName(ActiveModule->getFullModuleName());
1798     if (!FullName.startswith(M->Name) && !FullName.endswith("Private"))
1799       continue;
1800     SmallString<128> FixedPrivModDecl;
1801     SmallString<128> Canonical(M->Name);
1802     Canonical.append("_Private");
1803 
1804     // Foo.Private -> Foo_Private
1805     if (ActiveModule->Parent && ActiveModule->Name == "Private" && !M->Parent &&
1806         M->Name == ActiveModule->Parent->Name) {
1807       Diags.Report(ActiveModule->DefinitionLoc,
1808                    diag::warn_mmap_mismatched_private_submodule)
1809           << FullName;
1810 
1811       SourceLocation FixItInitBegin = CurrModuleDeclLoc;
1812       if (FrameworkLoc.isValid())
1813         FixItInitBegin = FrameworkLoc;
1814       if (ExplicitLoc.isValid())
1815         FixItInitBegin = ExplicitLoc;
1816 
1817       if (FrameworkLoc.isValid() || ActiveModule->Parent->IsFramework)
1818         FixedPrivModDecl.append("framework ");
1819       FixedPrivModDecl.append("module ");
1820       FixedPrivModDecl.append(Canonical);
1821 
1822       GenNoteAndFixIt(FullName, FixedPrivModDecl, M,
1823                       SourceRange(FixItInitBegin, ActiveModule->DefinitionLoc));
1824       continue;
1825     }
1826 
1827     // FooPrivate and whatnots -> Foo_Private
1828     if (!ActiveModule->Parent && !M->Parent && M->Name != ActiveModule->Name &&
1829         ActiveModule->Name != Canonical) {
1830       Diags.Report(ActiveModule->DefinitionLoc,
1831                    diag::warn_mmap_mismatched_private_module_name)
1832           << ActiveModule->Name;
1833       GenNoteAndFixIt(ActiveModule->Name, Canonical, M,
1834                       SourceRange(ActiveModule->DefinitionLoc));
1835     }
1836   }
1837 }
1838 
1839 /// Parse a module declaration.
1840 ///
1841 ///   module-declaration:
1842 ///     'extern' 'module' module-id string-literal
1843 ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1844 ///       { module-member* }
1845 ///
1846 ///   module-member:
1847 ///     requires-declaration
1848 ///     header-declaration
1849 ///     submodule-declaration
1850 ///     export-declaration
1851 ///     export-as-declaration
1852 ///     link-declaration
1853 ///
1854 ///   submodule-declaration:
1855 ///     module-declaration
1856 ///     inferred-submodule-declaration
1857 void ModuleMapParser::parseModuleDecl() {
1858   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1859          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
1860   if (Tok.is(MMToken::ExternKeyword)) {
1861     parseExternModuleDecl();
1862     return;
1863   }
1864 
1865   // Parse 'explicit' or 'framework' keyword, if present.
1866   SourceLocation ExplicitLoc;
1867   SourceLocation FrameworkLoc;
1868   bool Explicit = false;
1869   bool Framework = false;
1870 
1871   // Parse 'explicit' keyword, if present.
1872   if (Tok.is(MMToken::ExplicitKeyword)) {
1873     ExplicitLoc = consumeToken();
1874     Explicit = true;
1875   }
1876 
1877   // Parse 'framework' keyword, if present.
1878   if (Tok.is(MMToken::FrameworkKeyword)) {
1879     FrameworkLoc = consumeToken();
1880     Framework = true;
1881   }
1882 
1883   // Parse 'module' keyword.
1884   if (!Tok.is(MMToken::ModuleKeyword)) {
1885     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1886     consumeToken();
1887     HadError = true;
1888     return;
1889   }
1890   CurrModuleDeclLoc = consumeToken(); // 'module' keyword
1891 
1892   // If we have a wildcard for the module name, this is an inferred submodule.
1893   // Parse it.
1894   if (Tok.is(MMToken::Star))
1895     return parseInferredModuleDecl(Framework, Explicit);
1896 
1897   // Parse the module name.
1898   ModuleId Id;
1899   if (parseModuleId(Id)) {
1900     HadError = true;
1901     return;
1902   }
1903 
1904   if (ActiveModule) {
1905     if (Id.size() > 1) {
1906       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1907         << SourceRange(Id.front().second, Id.back().second);
1908 
1909       HadError = true;
1910       return;
1911     }
1912   } else if (Id.size() == 1 && Explicit) {
1913     // Top-level modules can't be explicit.
1914     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1915     Explicit = false;
1916     ExplicitLoc = SourceLocation();
1917     HadError = true;
1918   }
1919 
1920   Module *PreviousActiveModule = ActiveModule;
1921   if (Id.size() > 1) {
1922     // This module map defines a submodule. Go find the module of which it
1923     // is a submodule.
1924     ActiveModule = nullptr;
1925     const Module *TopLevelModule = nullptr;
1926     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1927       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1928         if (I == 0)
1929           TopLevelModule = Next;
1930         ActiveModule = Next;
1931         continue;
1932       }
1933 
1934       Diags.Report(Id[I].second, diag::err_mmap_missing_parent_module)
1935           << Id[I].first << (ActiveModule != nullptr)
1936           << (ActiveModule
1937                   ? ActiveModule->getTopLevelModule()->getFullModuleName()
1938                   : "");
1939       HadError = true;
1940     }
1941 
1942     if (TopLevelModule &&
1943         ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
1944       assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
1945              "submodule defined in same file as 'module *' that allowed its "
1946              "top-level module");
1947       Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
1948     }
1949   }
1950 
1951   StringRef ModuleName = Id.back().first;
1952   SourceLocation ModuleNameLoc = Id.back().second;
1953 
1954   // Parse the optional attribute list.
1955   Attributes Attrs;
1956   if (parseOptionalAttributes(Attrs))
1957     return;
1958 
1959   // Parse the opening brace.
1960   if (!Tok.is(MMToken::LBrace)) {
1961     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1962       << ModuleName;
1963     HadError = true;
1964     return;
1965   }
1966   SourceLocation LBraceLoc = consumeToken();
1967 
1968   // Determine whether this (sub)module has already been defined.
1969   Module *ShadowingModule = nullptr;
1970   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1971     // We might see a (re)definition of a module that we already have a
1972     // definition for in two cases:
1973     //  - If we loaded one definition from an AST file and we've just found a
1974     //    corresponding definition in a module map file, or
1975     bool LoadedFromASTFile = Existing->DefinitionLoc.isInvalid();
1976     //  - If we're building a (preprocessed) module and we've just loaded the
1977     //    module map file from which it was created.
1978     bool ParsedAsMainInput =
1979         Map.LangOpts.getCompilingModule() == LangOptions::CMK_ModuleMap &&
1980         Map.LangOpts.CurrentModule == ModuleName &&
1981         SourceMgr.getDecomposedLoc(ModuleNameLoc).first !=
1982             SourceMgr.getDecomposedLoc(Existing->DefinitionLoc).first;
1983     if (!ActiveModule && (LoadedFromASTFile || ParsedAsMainInput)) {
1984       // Skip the module definition.
1985       skipUntil(MMToken::RBrace);
1986       if (Tok.is(MMToken::RBrace))
1987         consumeToken();
1988       else {
1989         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1990         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1991         HadError = true;
1992       }
1993       return;
1994     }
1995 
1996     if (!Existing->Parent && Map.mayShadowNewModule(Existing)) {
1997       ShadowingModule = Existing;
1998     } else {
1999       // This is not a shawdowed module decl, it is an illegal redefinition.
2000       Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
2001           << ModuleName;
2002       Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
2003 
2004       // Skip the module definition.
2005       skipUntil(MMToken::RBrace);
2006       if (Tok.is(MMToken::RBrace))
2007         consumeToken();
2008 
2009       HadError = true;
2010       return;
2011     }
2012   }
2013 
2014   // Start defining this module.
2015   if (ShadowingModule) {
2016     ActiveModule =
2017         Map.createShadowedModule(ModuleName, Framework, ShadowingModule);
2018   } else {
2019     ActiveModule =
2020         Map.findOrCreateModule(ModuleName, ActiveModule, Framework, Explicit)
2021             .first;
2022   }
2023 
2024   ActiveModule->DefinitionLoc = ModuleNameLoc;
2025   if (Attrs.IsSystem || IsSystem)
2026     ActiveModule->IsSystem = true;
2027   if (Attrs.IsExternC)
2028     ActiveModule->IsExternC = true;
2029   if (Attrs.NoUndeclaredIncludes)
2030     ActiveModule->NoUndeclaredIncludes = true;
2031   ActiveModule->Directory = Directory;
2032 
2033   StringRef MapFileName(ModuleMapFile->getName());
2034   if (MapFileName.endswith("module.private.modulemap") ||
2035       MapFileName.endswith("module_private.map")) {
2036     ActiveModule->ModuleMapIsPrivate = true;
2037   }
2038 
2039   // Private modules named as FooPrivate, Foo.Private or similar are likely a
2040   // user error; provide warnings, notes and fixits to direct users to use
2041   // Foo_Private instead.
2042   SourceLocation StartLoc =
2043       SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
2044   if (Map.HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
2045       !Diags.isIgnored(diag::warn_mmap_mismatched_private_submodule,
2046                        StartLoc) &&
2047       !Diags.isIgnored(diag::warn_mmap_mismatched_private_module_name,
2048                        StartLoc) &&
2049       ActiveModule->ModuleMapIsPrivate)
2050     diagnosePrivateModules(ExplicitLoc, FrameworkLoc);
2051 
2052   bool Done = false;
2053   do {
2054     switch (Tok.Kind) {
2055     case MMToken::EndOfFile:
2056     case MMToken::RBrace:
2057       Done = true;
2058       break;
2059 
2060     case MMToken::ConfigMacros:
2061       parseConfigMacros();
2062       break;
2063 
2064     case MMToken::Conflict:
2065       parseConflict();
2066       break;
2067 
2068     case MMToken::ExplicitKeyword:
2069     case MMToken::ExternKeyword:
2070     case MMToken::FrameworkKeyword:
2071     case MMToken::ModuleKeyword:
2072       parseModuleDecl();
2073       break;
2074 
2075     case MMToken::ExportKeyword:
2076       parseExportDecl();
2077       break;
2078 
2079     case MMToken::ExportAsKeyword:
2080       parseExportAsDecl();
2081       break;
2082 
2083     case MMToken::UseKeyword:
2084       parseUseDecl();
2085       break;
2086 
2087     case MMToken::RequiresKeyword:
2088       parseRequiresDecl();
2089       break;
2090 
2091     case MMToken::TextualKeyword:
2092       parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
2093       break;
2094 
2095     case MMToken::UmbrellaKeyword: {
2096       SourceLocation UmbrellaLoc = consumeToken();
2097       if (Tok.is(MMToken::HeaderKeyword))
2098         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
2099       else
2100         parseUmbrellaDirDecl(UmbrellaLoc);
2101       break;
2102     }
2103 
2104     case MMToken::ExcludeKeyword:
2105       parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
2106       break;
2107 
2108     case MMToken::PrivateKeyword:
2109       parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
2110       break;
2111 
2112     case MMToken::HeaderKeyword:
2113       parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
2114       break;
2115 
2116     case MMToken::LinkKeyword:
2117       parseLinkDecl();
2118       break;
2119 
2120     default:
2121       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
2122       consumeToken();
2123       break;
2124     }
2125   } while (!Done);
2126 
2127   if (Tok.is(MMToken::RBrace))
2128     consumeToken();
2129   else {
2130     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2131     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2132     HadError = true;
2133   }
2134 
2135   // If the active module is a top-level framework, and there are no link
2136   // libraries, automatically link against the framework.
2137   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
2138       ActiveModule->LinkLibraries.empty()) {
2139     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
2140   }
2141 
2142   // If the module meets all requirements but is still unavailable, mark the
2143   // whole tree as unavailable to prevent it from building.
2144   if (!ActiveModule->IsAvailable && !ActiveModule->IsUnimportable &&
2145       ActiveModule->Parent) {
2146     ActiveModule->getTopLevelModule()->markUnavailable(/*Unimportable=*/false);
2147     ActiveModule->getTopLevelModule()->MissingHeaders.append(
2148       ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
2149   }
2150 
2151   // We're done parsing this module. Pop back to the previous module.
2152   ActiveModule = PreviousActiveModule;
2153 }
2154 
2155 /// Parse an extern module declaration.
2156 ///
2157 ///   extern module-declaration:
2158 ///     'extern' 'module' module-id string-literal
2159 void ModuleMapParser::parseExternModuleDecl() {
2160   assert(Tok.is(MMToken::ExternKeyword));
2161   SourceLocation ExternLoc = consumeToken(); // 'extern' keyword
2162 
2163   // Parse 'module' keyword.
2164   if (!Tok.is(MMToken::ModuleKeyword)) {
2165     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2166     consumeToken();
2167     HadError = true;
2168     return;
2169   }
2170   consumeToken(); // 'module' keyword
2171 
2172   // Parse the module name.
2173   ModuleId Id;
2174   if (parseModuleId(Id)) {
2175     HadError = true;
2176     return;
2177   }
2178 
2179   // Parse the referenced module map file name.
2180   if (!Tok.is(MMToken::StringLiteral)) {
2181     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
2182     HadError = true;
2183     return;
2184   }
2185   std::string FileName = std::string(Tok.getString());
2186   consumeToken(); // filename
2187 
2188   StringRef FileNameRef = FileName;
2189   SmallString<128> ModuleMapFileName;
2190   if (llvm::sys::path::is_relative(FileNameRef)) {
2191     ModuleMapFileName += Directory->getName();
2192     llvm::sys::path::append(ModuleMapFileName, FileName);
2193     FileNameRef = ModuleMapFileName;
2194   }
2195   if (auto File = SourceMgr.getFileManager().getFile(FileNameRef))
2196     Map.parseModuleMapFile(
2197         *File, IsSystem,
2198         Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
2199             ? Directory
2200             : (*File)->getDir(),
2201         FileID(), nullptr, ExternLoc);
2202 }
2203 
2204 /// Whether to add the requirement \p Feature to the module \p M.
2205 ///
2206 /// This preserves backwards compatibility for two hacks in the Darwin system
2207 /// module map files:
2208 ///
2209 /// 1. The use of 'requires excluded' to make headers non-modular, which
2210 ///    should really be mapped to 'textual' now that we have this feature.  We
2211 ///    drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
2212 ///    true.  Later, this bit will be used to map all the headers inside this
2213 ///    module to 'textual'.
2214 ///
2215 ///    This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
2216 ///
2217 /// 2. Removes a bogus cplusplus requirement from IOKit.avc.  This requirement
2218 ///    was never correct and causes issues now that we check it, so drop it.
2219 static bool shouldAddRequirement(Module *M, StringRef Feature,
2220                                  bool &IsRequiresExcludedHack) {
2221   if (Feature == "excluded" &&
2222       (M->fullModuleNameIs({"Darwin", "C", "excluded"}) ||
2223        M->fullModuleNameIs({"Tcl", "Private"}))) {
2224     IsRequiresExcludedHack = true;
2225     return false;
2226   } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})) {
2227     return false;
2228   }
2229 
2230   return true;
2231 }
2232 
2233 /// Parse a requires declaration.
2234 ///
2235 ///   requires-declaration:
2236 ///     'requires' feature-list
2237 ///
2238 ///   feature-list:
2239 ///     feature ',' feature-list
2240 ///     feature
2241 ///
2242 ///   feature:
2243 ///     '!'[opt] identifier
2244 void ModuleMapParser::parseRequiresDecl() {
2245   assert(Tok.is(MMToken::RequiresKeyword));
2246 
2247   // Parse 'requires' keyword.
2248   consumeToken();
2249 
2250   // Parse the feature-list.
2251   do {
2252     bool RequiredState = true;
2253     if (Tok.is(MMToken::Exclaim)) {
2254       RequiredState = false;
2255       consumeToken();
2256     }
2257 
2258     if (!Tok.is(MMToken::Identifier)) {
2259       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
2260       HadError = true;
2261       return;
2262     }
2263 
2264     // Consume the feature name.
2265     std::string Feature = std::string(Tok.getString());
2266     consumeToken();
2267 
2268     bool IsRequiresExcludedHack = false;
2269     bool ShouldAddRequirement =
2270         shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
2271 
2272     if (IsRequiresExcludedHack)
2273       UsesRequiresExcludedHack.insert(ActiveModule);
2274 
2275     if (ShouldAddRequirement) {
2276       // Add this feature.
2277       ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts,
2278                                    *Map.Target);
2279     }
2280 
2281     if (!Tok.is(MMToken::Comma))
2282       break;
2283 
2284     // Consume the comma.
2285     consumeToken();
2286   } while (true);
2287 }
2288 
2289 /// Parse a header declaration.
2290 ///
2291 ///   header-declaration:
2292 ///     'textual'[opt] 'header' string-literal
2293 ///     'private' 'textual'[opt] 'header' string-literal
2294 ///     'exclude' 'header' string-literal
2295 ///     'umbrella' 'header' string-literal
2296 ///
2297 /// FIXME: Support 'private textual header'.
2298 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
2299                                       SourceLocation LeadingLoc) {
2300   // We've already consumed the first token.
2301   ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
2302   if (LeadingToken == MMToken::PrivateKeyword) {
2303     Role = ModuleMap::PrivateHeader;
2304     // 'private' may optionally be followed by 'textual'.
2305     if (Tok.is(MMToken::TextualKeyword)) {
2306       LeadingToken = Tok.Kind;
2307       consumeToken();
2308     }
2309   }
2310 
2311   if (LeadingToken == MMToken::TextualKeyword)
2312     Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2313 
2314   if (UsesRequiresExcludedHack.count(ActiveModule)) {
2315     // Mark this header 'textual' (see doc comment for
2316     // Module::UsesRequiresExcludedHack).
2317     Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
2318   }
2319 
2320   if (LeadingToken != MMToken::HeaderKeyword) {
2321     if (!Tok.is(MMToken::HeaderKeyword)) {
2322       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2323           << (LeadingToken == MMToken::PrivateKeyword ? "private" :
2324               LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
2325               LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
2326       return;
2327     }
2328     consumeToken();
2329   }
2330 
2331   // Parse the header name.
2332   if (!Tok.is(MMToken::StringLiteral)) {
2333     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2334       << "header";
2335     HadError = true;
2336     return;
2337   }
2338   Module::UnresolvedHeaderDirective Header;
2339   Header.FileName = std::string(Tok.getString());
2340   Header.FileNameLoc = consumeToken();
2341   Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
2342   Header.Kind =
2343       (LeadingToken == MMToken::ExcludeKeyword ? Module::HK_Excluded
2344                                                : Map.headerRoleToKind(Role));
2345 
2346   // Check whether we already have an umbrella.
2347   if (Header.IsUmbrella && ActiveModule->Umbrella) {
2348     Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
2349       << ActiveModule->getFullModuleName();
2350     HadError = true;
2351     return;
2352   }
2353 
2354   // If we were given stat information, parse it so we can skip looking for
2355   // the file.
2356   if (Tok.is(MMToken::LBrace)) {
2357     SourceLocation LBraceLoc = consumeToken();
2358 
2359     while (!Tok.is(MMToken::RBrace) && !Tok.is(MMToken::EndOfFile)) {
2360       enum Attribute { Size, ModTime, Unknown };
2361       StringRef Str = Tok.getString();
2362       SourceLocation Loc = consumeToken();
2363       switch (llvm::StringSwitch<Attribute>(Str)
2364                   .Case("size", Size)
2365                   .Case("mtime", ModTime)
2366                   .Default(Unknown)) {
2367       case Size:
2368         if (Header.Size)
2369           Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
2370         if (!Tok.is(MMToken::IntegerLiteral)) {
2371           Diags.Report(Tok.getLocation(),
2372                        diag::err_mmap_invalid_header_attribute_value) << Str;
2373           skipUntil(MMToken::RBrace);
2374           break;
2375         }
2376         Header.Size = Tok.getInteger();
2377         consumeToken();
2378         break;
2379 
2380       case ModTime:
2381         if (Header.ModTime)
2382           Diags.Report(Loc, diag::err_mmap_duplicate_header_attribute) << Str;
2383         if (!Tok.is(MMToken::IntegerLiteral)) {
2384           Diags.Report(Tok.getLocation(),
2385                        diag::err_mmap_invalid_header_attribute_value) << Str;
2386           skipUntil(MMToken::RBrace);
2387           break;
2388         }
2389         Header.ModTime = Tok.getInteger();
2390         consumeToken();
2391         break;
2392 
2393       case Unknown:
2394         Diags.Report(Loc, diag::err_mmap_expected_header_attribute);
2395         skipUntil(MMToken::RBrace);
2396         break;
2397       }
2398     }
2399 
2400     if (Tok.is(MMToken::RBrace))
2401       consumeToken();
2402     else {
2403       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2404       Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2405       HadError = true;
2406     }
2407   }
2408 
2409   bool NeedsFramework = false;
2410   Map.addUnresolvedHeader(ActiveModule, std::move(Header), NeedsFramework);
2411 
2412   if (NeedsFramework && ActiveModule)
2413     Diags.Report(CurrModuleDeclLoc, diag::note_mmap_add_framework_keyword)
2414       << ActiveModule->getFullModuleName()
2415       << FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
2416 }
2417 
2418 static int compareModuleHeaders(const Module::Header *A,
2419                                 const Module::Header *B) {
2420   return A->NameAsWritten.compare(B->NameAsWritten);
2421 }
2422 
2423 /// Parse an umbrella directory declaration.
2424 ///
2425 ///   umbrella-dir-declaration:
2426 ///     umbrella string-literal
2427 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
2428   // Parse the directory name.
2429   if (!Tok.is(MMToken::StringLiteral)) {
2430     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
2431       << "umbrella";
2432     HadError = true;
2433     return;
2434   }
2435 
2436   std::string DirName = std::string(Tok.getString());
2437   std::string DirNameAsWritten = DirName;
2438   SourceLocation DirNameLoc = consumeToken();
2439 
2440   // Check whether we already have an umbrella.
2441   if (ActiveModule->Umbrella) {
2442     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
2443       << ActiveModule->getFullModuleName();
2444     HadError = true;
2445     return;
2446   }
2447 
2448   // Look for this file.
2449   const DirectoryEntry *Dir = nullptr;
2450   if (llvm::sys::path::is_absolute(DirName)) {
2451     if (auto D = SourceMgr.getFileManager().getDirectory(DirName))
2452       Dir = *D;
2453   } else {
2454     SmallString<128> PathName;
2455     PathName = Directory->getName();
2456     llvm::sys::path::append(PathName, DirName);
2457     if (auto D = SourceMgr.getFileManager().getDirectory(PathName))
2458       Dir = *D;
2459   }
2460 
2461   if (!Dir) {
2462     Diags.Report(DirNameLoc, diag::warn_mmap_umbrella_dir_not_found)
2463       << DirName;
2464     return;
2465   }
2466 
2467   if (UsesRequiresExcludedHack.count(ActiveModule)) {
2468     // Mark this header 'textual' (see doc comment for
2469     // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the
2470     // directory is relatively expensive, in practice this only applies to the
2471     // uncommonly used Tcl module on Darwin platforms.
2472     std::error_code EC;
2473     SmallVector<Module::Header, 6> Headers;
2474     llvm::vfs::FileSystem &FS =
2475         SourceMgr.getFileManager().getVirtualFileSystem();
2476     for (llvm::vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
2477          I != E && !EC; I.increment(EC)) {
2478       if (auto FE = SourceMgr.getFileManager().getFile(I->path())) {
2479         Module::Header Header = {"", std::string(I->path()), *FE};
2480         Headers.push_back(std::move(Header));
2481       }
2482     }
2483 
2484     // Sort header paths so that the pcm doesn't depend on iteration order.
2485     llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
2486 
2487     for (auto &Header : Headers)
2488       Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);
2489     return;
2490   }
2491 
2492   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
2493     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
2494       << OwningModule->getFullModuleName();
2495     HadError = true;
2496     return;
2497   }
2498 
2499   // Record this umbrella directory.
2500   Map.setUmbrellaDir(ActiveModule, Dir, DirNameAsWritten, DirName);
2501 }
2502 
2503 /// Parse a module export declaration.
2504 ///
2505 ///   export-declaration:
2506 ///     'export' wildcard-module-id
2507 ///
2508 ///   wildcard-module-id:
2509 ///     identifier
2510 ///     '*'
2511 ///     identifier '.' wildcard-module-id
2512 void ModuleMapParser::parseExportDecl() {
2513   assert(Tok.is(MMToken::ExportKeyword));
2514   SourceLocation ExportLoc = consumeToken();
2515 
2516   // Parse the module-id with an optional wildcard at the end.
2517   ModuleId ParsedModuleId;
2518   bool Wildcard = false;
2519   do {
2520     // FIXME: Support string-literal module names here.
2521     if (Tok.is(MMToken::Identifier)) {
2522       ParsedModuleId.push_back(
2523           std::make_pair(std::string(Tok.getString()), Tok.getLocation()));
2524       consumeToken();
2525 
2526       if (Tok.is(MMToken::Period)) {
2527         consumeToken();
2528         continue;
2529       }
2530 
2531       break;
2532     }
2533 
2534     if(Tok.is(MMToken::Star)) {
2535       Wildcard = true;
2536       consumeToken();
2537       break;
2538     }
2539 
2540     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
2541     HadError = true;
2542     return;
2543   } while (true);
2544 
2545   Module::UnresolvedExportDecl Unresolved = {
2546     ExportLoc, ParsedModuleId, Wildcard
2547   };
2548   ActiveModule->UnresolvedExports.push_back(Unresolved);
2549 }
2550 
2551 /// Parse a module export_as declaration.
2552 ///
2553 ///   export-as-declaration:
2554 ///     'export_as' identifier
2555 void ModuleMapParser::parseExportAsDecl() {
2556   assert(Tok.is(MMToken::ExportAsKeyword));
2557   consumeToken();
2558 
2559   if (!Tok.is(MMToken::Identifier)) {
2560     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
2561     HadError = true;
2562     return;
2563   }
2564 
2565   if (ActiveModule->Parent) {
2566     Diags.Report(Tok.getLocation(), diag::err_mmap_submodule_export_as);
2567     consumeToken();
2568     return;
2569   }
2570 
2571   if (!ActiveModule->ExportAsModule.empty()) {
2572     if (ActiveModule->ExportAsModule == Tok.getString()) {
2573       Diags.Report(Tok.getLocation(), diag::warn_mmap_redundant_export_as)
2574         << ActiveModule->Name << Tok.getString();
2575     } else {
2576       Diags.Report(Tok.getLocation(), diag::err_mmap_conflicting_export_as)
2577         << ActiveModule->Name << ActiveModule->ExportAsModule
2578         << Tok.getString();
2579     }
2580   }
2581 
2582   ActiveModule->ExportAsModule = std::string(Tok.getString());
2583   Map.addLinkAsDependency(ActiveModule);
2584 
2585   consumeToken();
2586 }
2587 
2588 /// Parse a module use declaration.
2589 ///
2590 ///   use-declaration:
2591 ///     'use' wildcard-module-id
2592 void ModuleMapParser::parseUseDecl() {
2593   assert(Tok.is(MMToken::UseKeyword));
2594   auto KWLoc = consumeToken();
2595   // Parse the module-id.
2596   ModuleId ParsedModuleId;
2597   parseModuleId(ParsedModuleId);
2598 
2599   if (ActiveModule->Parent)
2600     Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule);
2601   else
2602     ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
2603 }
2604 
2605 /// Parse a link declaration.
2606 ///
2607 ///   module-declaration:
2608 ///     'link' 'framework'[opt] string-literal
2609 void ModuleMapParser::parseLinkDecl() {
2610   assert(Tok.is(MMToken::LinkKeyword));
2611   SourceLocation LinkLoc = consumeToken();
2612 
2613   // Parse the optional 'framework' keyword.
2614   bool IsFramework = false;
2615   if (Tok.is(MMToken::FrameworkKeyword)) {
2616     consumeToken();
2617     IsFramework = true;
2618   }
2619 
2620   // Parse the library name
2621   if (!Tok.is(MMToken::StringLiteral)) {
2622     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
2623       << IsFramework << SourceRange(LinkLoc);
2624     HadError = true;
2625     return;
2626   }
2627 
2628   std::string LibraryName = std::string(Tok.getString());
2629   consumeToken();
2630   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
2631                                                             IsFramework));
2632 }
2633 
2634 /// Parse a configuration macro declaration.
2635 ///
2636 ///   module-declaration:
2637 ///     'config_macros' attributes[opt] config-macro-list?
2638 ///
2639 ///   config-macro-list:
2640 ///     identifier (',' identifier)?
2641 void ModuleMapParser::parseConfigMacros() {
2642   assert(Tok.is(MMToken::ConfigMacros));
2643   SourceLocation ConfigMacrosLoc = consumeToken();
2644 
2645   // Only top-level modules can have configuration macros.
2646   if (ActiveModule->Parent) {
2647     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
2648   }
2649 
2650   // Parse the optional attributes.
2651   Attributes Attrs;
2652   if (parseOptionalAttributes(Attrs))
2653     return;
2654 
2655   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
2656     ActiveModule->ConfigMacrosExhaustive = true;
2657   }
2658 
2659   // If we don't have an identifier, we're done.
2660   // FIXME: Support macros with the same name as a keyword here.
2661   if (!Tok.is(MMToken::Identifier))
2662     return;
2663 
2664   // Consume the first identifier.
2665   if (!ActiveModule->Parent) {
2666     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2667   }
2668   consumeToken();
2669 
2670   do {
2671     // If there's a comma, consume it.
2672     if (!Tok.is(MMToken::Comma))
2673       break;
2674     consumeToken();
2675 
2676     // We expect to see a macro name here.
2677     // FIXME: Support macros with the same name as a keyword here.
2678     if (!Tok.is(MMToken::Identifier)) {
2679       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
2680       break;
2681     }
2682 
2683     // Consume the macro name.
2684     if (!ActiveModule->Parent) {
2685       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2686     }
2687     consumeToken();
2688   } while (true);
2689 }
2690 
2691 /// Format a module-id into a string.
2692 static std::string formatModuleId(const ModuleId &Id) {
2693   std::string result;
2694   {
2695     llvm::raw_string_ostream OS(result);
2696 
2697     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
2698       if (I)
2699         OS << ".";
2700       OS << Id[I].first;
2701     }
2702   }
2703 
2704   return result;
2705 }
2706 
2707 /// Parse a conflict declaration.
2708 ///
2709 ///   module-declaration:
2710 ///     'conflict' module-id ',' string-literal
2711 void ModuleMapParser::parseConflict() {
2712   assert(Tok.is(MMToken::Conflict));
2713   SourceLocation ConflictLoc = consumeToken();
2714   Module::UnresolvedConflict Conflict;
2715 
2716   // Parse the module-id.
2717   if (parseModuleId(Conflict.Id))
2718     return;
2719 
2720   // Parse the ','.
2721   if (!Tok.is(MMToken::Comma)) {
2722     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
2723       << SourceRange(ConflictLoc);
2724     return;
2725   }
2726   consumeToken();
2727 
2728   // Parse the message.
2729   if (!Tok.is(MMToken::StringLiteral)) {
2730     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
2731       << formatModuleId(Conflict.Id);
2732     return;
2733   }
2734   Conflict.Message = Tok.getString().str();
2735   consumeToken();
2736 
2737   // Add this unresolved conflict.
2738   ActiveModule->UnresolvedConflicts.push_back(Conflict);
2739 }
2740 
2741 /// Parse an inferred module declaration (wildcard modules).
2742 ///
2743 ///   module-declaration:
2744 ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
2745 ///       { inferred-module-member* }
2746 ///
2747 ///   inferred-module-member:
2748 ///     'export' '*'
2749 ///     'exclude' identifier
2750 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
2751   assert(Tok.is(MMToken::Star));
2752   SourceLocation StarLoc = consumeToken();
2753   bool Failed = false;
2754 
2755   // Inferred modules must be submodules.
2756   if (!ActiveModule && !Framework) {
2757     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
2758     Failed = true;
2759   }
2760 
2761   if (ActiveModule) {
2762     // Inferred modules must have umbrella directories.
2763     if (!Failed && ActiveModule->IsAvailable &&
2764         !ActiveModule->getUmbrellaDir()) {
2765       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
2766       Failed = true;
2767     }
2768 
2769     // Check for redefinition of an inferred module.
2770     if (!Failed && ActiveModule->InferSubmodules) {
2771       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
2772       if (ActiveModule->InferredSubmoduleLoc.isValid())
2773         Diags.Report(ActiveModule->InferredSubmoduleLoc,
2774                      diag::note_mmap_prev_definition);
2775       Failed = true;
2776     }
2777 
2778     // Check for the 'framework' keyword, which is not permitted here.
2779     if (Framework) {
2780       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
2781       Framework = false;
2782     }
2783   } else if (Explicit) {
2784     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
2785     Explicit = false;
2786   }
2787 
2788   // If there were any problems with this inferred submodule, skip its body.
2789   if (Failed) {
2790     if (Tok.is(MMToken::LBrace)) {
2791       consumeToken();
2792       skipUntil(MMToken::RBrace);
2793       if (Tok.is(MMToken::RBrace))
2794         consumeToken();
2795     }
2796     HadError = true;
2797     return;
2798   }
2799 
2800   // Parse optional attributes.
2801   Attributes Attrs;
2802   if (parseOptionalAttributes(Attrs))
2803     return;
2804 
2805   if (ActiveModule) {
2806     // Note that we have an inferred submodule.
2807     ActiveModule->InferSubmodules = true;
2808     ActiveModule->InferredSubmoduleLoc = StarLoc;
2809     ActiveModule->InferExplicitSubmodules = Explicit;
2810   } else {
2811     // We'll be inferring framework modules for this directory.
2812     Map.InferredDirectories[Directory].InferModules = true;
2813     Map.InferredDirectories[Directory].Attrs = Attrs;
2814     Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
2815     // FIXME: Handle the 'framework' keyword.
2816   }
2817 
2818   // Parse the opening brace.
2819   if (!Tok.is(MMToken::LBrace)) {
2820     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
2821     HadError = true;
2822     return;
2823   }
2824   SourceLocation LBraceLoc = consumeToken();
2825 
2826   // Parse the body of the inferred submodule.
2827   bool Done = false;
2828   do {
2829     switch (Tok.Kind) {
2830     case MMToken::EndOfFile:
2831     case MMToken::RBrace:
2832       Done = true;
2833       break;
2834 
2835     case MMToken::ExcludeKeyword:
2836       if (ActiveModule) {
2837         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2838           << (ActiveModule != nullptr);
2839         consumeToken();
2840         break;
2841       }
2842 
2843       consumeToken();
2844       // FIXME: Support string-literal module names here.
2845       if (!Tok.is(MMToken::Identifier)) {
2846         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
2847         break;
2848       }
2849 
2850       Map.InferredDirectories[Directory].ExcludedModules.push_back(
2851           std::string(Tok.getString()));
2852       consumeToken();
2853       break;
2854 
2855     case MMToken::ExportKeyword:
2856       if (!ActiveModule) {
2857         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2858           << (ActiveModule != nullptr);
2859         consumeToken();
2860         break;
2861       }
2862 
2863       consumeToken();
2864       if (Tok.is(MMToken::Star))
2865         ActiveModule->InferExportWildcard = true;
2866       else
2867         Diags.Report(Tok.getLocation(),
2868                      diag::err_mmap_expected_export_wildcard);
2869       consumeToken();
2870       break;
2871 
2872     case MMToken::ExplicitKeyword:
2873     case MMToken::ModuleKeyword:
2874     case MMToken::HeaderKeyword:
2875     case MMToken::PrivateKeyword:
2876     case MMToken::UmbrellaKeyword:
2877     default:
2878       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2879           << (ActiveModule != nullptr);
2880       consumeToken();
2881       break;
2882     }
2883   } while (!Done);
2884 
2885   if (Tok.is(MMToken::RBrace))
2886     consumeToken();
2887   else {
2888     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2889     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2890     HadError = true;
2891   }
2892 }
2893 
2894 /// Parse optional attributes.
2895 ///
2896 ///   attributes:
2897 ///     attribute attributes
2898 ///     attribute
2899 ///
2900 ///   attribute:
2901 ///     [ identifier ]
2902 ///
2903 /// \param Attrs Will be filled in with the parsed attributes.
2904 ///
2905 /// \returns true if an error occurred, false otherwise.
2906 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
2907   bool HadError = false;
2908 
2909   while (Tok.is(MMToken::LSquare)) {
2910     // Consume the '['.
2911     SourceLocation LSquareLoc = consumeToken();
2912 
2913     // Check whether we have an attribute name here.
2914     if (!Tok.is(MMToken::Identifier)) {
2915       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
2916       skipUntil(MMToken::RSquare);
2917       if (Tok.is(MMToken::RSquare))
2918         consumeToken();
2919       HadError = true;
2920     }
2921 
2922     // Decode the attribute name.
2923     AttributeKind Attribute
2924       = llvm::StringSwitch<AttributeKind>(Tok.getString())
2925           .Case("exhaustive", AT_exhaustive)
2926           .Case("extern_c", AT_extern_c)
2927           .Case("no_undeclared_includes", AT_no_undeclared_includes)
2928           .Case("system", AT_system)
2929           .Default(AT_unknown);
2930     switch (Attribute) {
2931     case AT_unknown:
2932       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
2933         << Tok.getString();
2934       break;
2935 
2936     case AT_system:
2937       Attrs.IsSystem = true;
2938       break;
2939 
2940     case AT_extern_c:
2941       Attrs.IsExternC = true;
2942       break;
2943 
2944     case AT_exhaustive:
2945       Attrs.IsExhaustive = true;
2946       break;
2947 
2948     case AT_no_undeclared_includes:
2949       Attrs.NoUndeclaredIncludes = true;
2950       break;
2951     }
2952     consumeToken();
2953 
2954     // Consume the ']'.
2955     if (!Tok.is(MMToken::RSquare)) {
2956       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
2957       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
2958       skipUntil(MMToken::RSquare);
2959       HadError = true;
2960     }
2961 
2962     if (Tok.is(MMToken::RSquare))
2963       consumeToken();
2964   }
2965 
2966   return HadError;
2967 }
2968 
2969 /// Parse a module map file.
2970 ///
2971 ///   module-map-file:
2972 ///     module-declaration*
2973 bool ModuleMapParser::parseModuleMapFile() {
2974   do {
2975     switch (Tok.Kind) {
2976     case MMToken::EndOfFile:
2977       return HadError;
2978 
2979     case MMToken::ExplicitKeyword:
2980     case MMToken::ExternKeyword:
2981     case MMToken::ModuleKeyword:
2982     case MMToken::FrameworkKeyword:
2983       parseModuleDecl();
2984       break;
2985 
2986     case MMToken::Comma:
2987     case MMToken::ConfigMacros:
2988     case MMToken::Conflict:
2989     case MMToken::Exclaim:
2990     case MMToken::ExcludeKeyword:
2991     case MMToken::ExportKeyword:
2992     case MMToken::ExportAsKeyword:
2993     case MMToken::HeaderKeyword:
2994     case MMToken::Identifier:
2995     case MMToken::LBrace:
2996     case MMToken::LinkKeyword:
2997     case MMToken::LSquare:
2998     case MMToken::Period:
2999     case MMToken::PrivateKeyword:
3000     case MMToken::RBrace:
3001     case MMToken::RSquare:
3002     case MMToken::RequiresKeyword:
3003     case MMToken::Star:
3004     case MMToken::StringLiteral:
3005     case MMToken::IntegerLiteral:
3006     case MMToken::TextualKeyword:
3007     case MMToken::UmbrellaKeyword:
3008     case MMToken::UseKeyword:
3009       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
3010       HadError = true;
3011       consumeToken();
3012       break;
3013     }
3014   } while (true);
3015 }
3016 
3017 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
3018                                    const DirectoryEntry *Dir, FileID ID,
3019                                    unsigned *Offset,
3020                                    SourceLocation ExternModuleLoc) {
3021   assert(Target && "Missing target information");
3022   llvm::DenseMap<const FileEntry *, bool>::iterator Known
3023     = ParsedModuleMap.find(File);
3024   if (Known != ParsedModuleMap.end())
3025     return Known->second;
3026 
3027   // If the module map file wasn't already entered, do so now.
3028   if (ID.isInvalid()) {
3029     auto FileCharacter =
3030         IsSystem ? SrcMgr::C_System_ModuleMap : SrcMgr::C_User_ModuleMap;
3031     ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
3032   }
3033 
3034   assert(Target && "Missing target information");
3035   llvm::Optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getBufferOrNone(ID);
3036   if (!Buffer)
3037     return ParsedModuleMap[File] = true;
3038   assert((!Offset || *Offset <= Buffer->getBufferSize()) &&
3039          "invalid buffer offset");
3040 
3041   // Parse this module map file.
3042   Lexer L(SourceMgr.getLocForStartOfFile(ID), MMapLangOpts,
3043           Buffer->getBufferStart(),
3044           Buffer->getBufferStart() + (Offset ? *Offset : 0),
3045           Buffer->getBufferEnd());
3046   SourceLocation Start = L.getSourceLocation();
3047   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
3048                          IsSystem);
3049   bool Result = Parser.parseModuleMapFile();
3050   ParsedModuleMap[File] = Result;
3051 
3052   if (Offset) {
3053     auto Loc = SourceMgr.getDecomposedLoc(Parser.getLocation());
3054     assert(Loc.first == ID && "stopped in a different file?");
3055     *Offset = Loc.second;
3056   }
3057 
3058   // Notify callbacks that we parsed it.
3059   for (const auto &Cb : Callbacks)
3060     Cb->moduleMapFileRead(Start, *File, IsSystem);
3061 
3062   return Result;
3063 }
3064