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