xref: /llvm-project/clang/lib/Lex/ModuleMap.cpp (revision 4d9230107591dc1f8e9cc3a6edfd4d3e16c6c497)
1 //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the ModuleMap implementation, which describes the layout
11 // of a module as it relates to headers.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/ModuleMap.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/DiagnosticOptions.h"
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "clang/Basic/TargetOptions.h"
21 #include "clang/Lex/HeaderSearch.h"
22 #include "clang/Lex/HeaderSearchOptions.h"
23 #include "clang/Lex/LexDiagnostic.h"
24 #include "clang/Lex/Lexer.h"
25 #include "clang/Lex/LiteralSupport.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Support/Allocator.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/Host.h"
31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include <stdlib.h>
34 #if defined(LLVM_ON_UNIX)
35 #include <limits.h>
36 #endif
37 using namespace clang;
38 
39 Module::ExportDecl
40 ModuleMap::resolveExport(Module *Mod,
41                          const Module::UnresolvedExportDecl &Unresolved,
42                          bool Complain) const {
43   // We may have just a wildcard.
44   if (Unresolved.Id.empty()) {
45     assert(Unresolved.Wildcard && "Invalid unresolved export");
46     return Module::ExportDecl(nullptr, true);
47   }
48 
49   // Resolve the module-id.
50   Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
51   if (!Context)
52     return Module::ExportDecl();
53 
54   return Module::ExportDecl(Context, Unresolved.Wildcard);
55 }
56 
57 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
58                                    bool Complain) const {
59   // Find the starting module.
60   Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
61   if (!Context) {
62     if (Complain)
63       Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
64       << Id[0].first << Mod->getFullModuleName();
65 
66     return nullptr;
67   }
68 
69   // Dig into the module path.
70   for (unsigned I = 1, N = Id.size(); I != N; ++I) {
71     Module *Sub = lookupModuleQualified(Id[I].first, Context);
72     if (!Sub) {
73       if (Complain)
74         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
75         << Id[I].first << Context->getFullModuleName()
76         << SourceRange(Id[0].second, Id[I-1].second);
77 
78       return nullptr;
79     }
80 
81     Context = Sub;
82   }
83 
84   return Context;
85 }
86 
87 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
88                      const LangOptions &LangOpts, const TargetInfo *Target,
89                      HeaderSearch &HeaderInfo)
90     : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
91       HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
92       SourceModule(nullptr), NumCreatedModules(0) {
93   MMapLangOpts.LineComment = true;
94   MMapLangOpts.ModularCodegen = LangOpts.ModularCodegen;
95 }
96 
97 ModuleMap::~ModuleMap() {
98   for (auto &M : Modules)
99     delete M.getValue();
100 }
101 
102 void ModuleMap::setTarget(const TargetInfo &Target) {
103   assert((!this->Target || this->Target == &Target) &&
104          "Improper target override");
105   this->Target = &Target;
106 }
107 
108 /// \brief "Sanitize" a filename so that it can be used as an identifier.
109 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
110                                               SmallVectorImpl<char> &Buffer) {
111   if (Name.empty())
112     return Name;
113 
114   if (!isValidIdentifier(Name)) {
115     // If we don't already have something with the form of an identifier,
116     // create a buffer with the sanitized name.
117     Buffer.clear();
118     if (isDigit(Name[0]))
119       Buffer.push_back('_');
120     Buffer.reserve(Buffer.size() + Name.size());
121     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
122       if (isIdentifierBody(Name[I]))
123         Buffer.push_back(Name[I]);
124       else
125         Buffer.push_back('_');
126     }
127 
128     Name = StringRef(Buffer.data(), Buffer.size());
129   }
130 
131   while (llvm::StringSwitch<bool>(Name)
132 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
133 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
134 #include "clang/Basic/TokenKinds.def"
135            .Default(false)) {
136     if (Name.data() != Buffer.data())
137       Buffer.append(Name.begin(), Name.end());
138     Buffer.push_back('_');
139     Name = StringRef(Buffer.data(), Buffer.size());
140   }
141 
142   return Name;
143 }
144 
145 /// \brief Determine whether the given file name is the name of a builtin
146 /// header, supplied by Clang to replace, override, or augment existing system
147 /// headers.
148 bool ModuleMap::isBuiltinHeader(StringRef FileName) {
149   return llvm::StringSwitch<bool>(FileName)
150            .Case("float.h", true)
151            .Case("iso646.h", true)
152            .Case("limits.h", true)
153            .Case("stdalign.h", true)
154            .Case("stdarg.h", true)
155            .Case("stdatomic.h", true)
156            .Case("stdbool.h", true)
157            .Case("stddef.h", true)
158            .Case("stdint.h", true)
159            .Case("tgmath.h", true)
160            .Case("unwind.h", true)
161            .Default(false);
162 }
163 
164 ModuleMap::HeadersMap::iterator
165 ModuleMap::findKnownHeader(const FileEntry *File) {
166   HeadersMap::iterator Known = Headers.find(File);
167   if (HeaderInfo.getHeaderSearchOpts().ImplicitModuleMaps &&
168       Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
169       ModuleMap::isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
170     HeaderInfo.loadTopLevelSystemModules();
171     return Headers.find(File);
172   }
173   return Known;
174 }
175 
176 ModuleMap::KnownHeader
177 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
178                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
179   if (UmbrellaDirs.empty())
180     return KnownHeader();
181 
182   const DirectoryEntry *Dir = File->getDir();
183   assert(Dir && "file in no directory");
184 
185   // Note: as an egregious but useful hack we use the real path here, because
186   // frameworks moving from top-level frameworks to embedded frameworks tend
187   // to be symlinked from the top-level location to the embedded location,
188   // and we need to resolve lookups as if we had found the embedded location.
189   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
190 
191   // Keep walking up the directory hierarchy, looking for a directory with
192   // an umbrella header.
193   do {
194     auto KnownDir = UmbrellaDirs.find(Dir);
195     if (KnownDir != UmbrellaDirs.end())
196       return KnownHeader(KnownDir->second, NormalHeader);
197 
198     IntermediateDirs.push_back(Dir);
199 
200     // Retrieve our parent path.
201     DirName = llvm::sys::path::parent_path(DirName);
202     if (DirName.empty())
203       break;
204 
205     // Resolve the parent path to a directory entry.
206     Dir = SourceMgr.getFileManager().getDirectory(DirName);
207   } while (Dir);
208   return KnownHeader();
209 }
210 
211 static bool violatesPrivateInclude(Module *RequestingModule,
212                                    const FileEntry *IncFileEnt,
213                                    ModuleMap::KnownHeader Header) {
214 #ifndef NDEBUG
215   if (Header.getRole() & ModuleMap::PrivateHeader) {
216     // Check for consistency between the module header role
217     // as obtained from the lookup and as obtained from the module.
218     // This check is not cheap, so enable it only for debugging.
219     bool IsPrivate = false;
220     SmallVectorImpl<Module::Header> *HeaderList[] = {
221         &Header.getModule()->Headers[Module::HK_Private],
222         &Header.getModule()->Headers[Module::HK_PrivateTextual]};
223     for (auto *Hs : HeaderList)
224       IsPrivate |=
225           std::find_if(Hs->begin(), Hs->end(), [&](const Module::Header &H) {
226             return H.Entry == IncFileEnt;
227           }) != Hs->end();
228     assert(IsPrivate && "inconsistent headers and roles");
229   }
230 #endif
231   return !Header.isAccessibleFrom(RequestingModule);
232 }
233 
234 static Module *getTopLevelOrNull(Module *M) {
235   return M ? M->getTopLevelModule() : nullptr;
236 }
237 
238 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
239                                         bool RequestingModuleIsModuleInterface,
240                                         SourceLocation FilenameLoc,
241                                         StringRef Filename,
242                                         const FileEntry *File) {
243   // No errors for indirect modules. This may be a bit of a problem for modules
244   // with no source files.
245   if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
246     return;
247 
248   if (RequestingModule)
249     resolveUses(RequestingModule, /*Complain=*/false);
250 
251   bool Excluded = false;
252   Module *Private = nullptr;
253   Module *NotUsed = nullptr;
254 
255   HeadersMap::iterator Known = findKnownHeader(File);
256   if (Known != Headers.end()) {
257     for (const KnownHeader &Header : Known->second) {
258       // Remember private headers for later printing of a diagnostic.
259       if (violatesPrivateInclude(RequestingModule, File, Header)) {
260         Private = Header.getModule();
261         continue;
262       }
263 
264       // If uses need to be specified explicitly, we are only allowed to return
265       // modules that are explicitly used by the requesting module.
266       if (RequestingModule && LangOpts.ModulesDeclUse &&
267           !RequestingModule->directlyUses(Header.getModule())) {
268         NotUsed = Header.getModule();
269         continue;
270       }
271 
272       // We have found a module that we can happily use.
273       return;
274     }
275 
276     Excluded = true;
277   }
278 
279   // We have found a header, but it is private.
280   if (Private) {
281     Diags.Report(FilenameLoc, diag::warn_use_of_private_header_outside_module)
282         << Filename;
283     return;
284   }
285 
286   // We have found a module, but we don't use it.
287   if (NotUsed) {
288     Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
289         << RequestingModule->getFullModuleName() << Filename;
290     return;
291   }
292 
293   if (Excluded || isHeaderInUmbrellaDirs(File))
294     return;
295 
296   // At this point, only non-modular includes remain.
297 
298   if (LangOpts.ModulesStrictDeclUse) {
299     Diags.Report(FilenameLoc, diag::err_undeclared_use_of_module)
300         << RequestingModule->getFullModuleName() << Filename;
301   } else if (RequestingModule && RequestingModuleIsModuleInterface &&
302              LangOpts.isCompilingModule()) {
303     // Do not diagnose when we are not compiling a module.
304     diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
305         diag::warn_non_modular_include_in_framework_module :
306         diag::warn_non_modular_include_in_module;
307     Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName()
308         << File->getName();
309   }
310 }
311 
312 static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
313                                 const ModuleMap::KnownHeader &Old) {
314   // Prefer available modules.
315   if (New.getModule()->isAvailable() && !Old.getModule()->isAvailable())
316     return true;
317 
318   // Prefer a public header over a private header.
319   if ((New.getRole() & ModuleMap::PrivateHeader) !=
320       (Old.getRole() & ModuleMap::PrivateHeader))
321     return !(New.getRole() & ModuleMap::PrivateHeader);
322 
323   // Prefer a non-textual header over a textual header.
324   if ((New.getRole() & ModuleMap::TextualHeader) !=
325       (Old.getRole() & ModuleMap::TextualHeader))
326     return !(New.getRole() & ModuleMap::TextualHeader);
327 
328   // Don't have a reason to choose between these. Just keep the first one.
329   return false;
330 }
331 
332 ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
333                                                       bool AllowTextual) {
334   auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
335     if (!AllowTextual && R.getRole() & ModuleMap::TextualHeader)
336       return ModuleMap::KnownHeader();
337     return R;
338   };
339 
340   HeadersMap::iterator Known = findKnownHeader(File);
341   if (Known != Headers.end()) {
342     ModuleMap::KnownHeader Result;
343     // Iterate over all modules that 'File' is part of to find the best fit.
344     for (KnownHeader &H : Known->second) {
345       // Prefer a header from the source module over all others.
346       if (H.getModule()->getTopLevelModule() == SourceModule)
347         return MakeResult(H);
348       if (!Result || isBetterKnownHeader(H, Result))
349         Result = H;
350     }
351     return MakeResult(Result);
352   }
353 
354   return MakeResult(findOrCreateModuleForHeaderInUmbrellaDir(File));
355 }
356 
357 ModuleMap::KnownHeader
358 ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
359   assert(!Headers.count(File) && "already have a module for this header");
360 
361   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
362   KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
363   if (H) {
364     Module *Result = H.getModule();
365 
366     // Search up the module stack until we find a module with an umbrella
367     // directory.
368     Module *UmbrellaModule = Result;
369     while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
370       UmbrellaModule = UmbrellaModule->Parent;
371 
372     if (UmbrellaModule->InferSubmodules) {
373       const FileEntry *UmbrellaModuleMap =
374           getModuleMapFileForUniquing(UmbrellaModule);
375 
376       // Infer submodules for each of the directories we found between
377       // the directory of the umbrella header and the directory where
378       // the actual header is located.
379       bool Explicit = UmbrellaModule->InferExplicitSubmodules;
380 
381       for (unsigned I = SkippedDirs.size(); I != 0; --I) {
382         // Find or create the module that corresponds to this directory name.
383         SmallString<32> NameBuf;
384         StringRef Name = sanitizeFilenameAsIdentifier(
385             llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf);
386         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
387                                     Explicit).first;
388         InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
389         Result->IsInferred = true;
390 
391         // Associate the module and the directory.
392         UmbrellaDirs[SkippedDirs[I-1]] = Result;
393 
394         // If inferred submodules export everything they import, add a
395         // wildcard to the set of exports.
396         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
397           Result->Exports.push_back(Module::ExportDecl(nullptr, true));
398       }
399 
400       // Infer a submodule with the same name as this header file.
401       SmallString<32> NameBuf;
402       StringRef Name = sanitizeFilenameAsIdentifier(
403                          llvm::sys::path::stem(File->getName()), NameBuf);
404       Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
405                                   Explicit).first;
406       InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
407       Result->IsInferred = true;
408       Result->addTopHeader(File);
409 
410       // If inferred submodules export everything they import, add a
411       // wildcard to the set of exports.
412       if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
413         Result->Exports.push_back(Module::ExportDecl(nullptr, true));
414     } else {
415       // Record each of the directories we stepped through as being part of
416       // the module we found, since the umbrella header covers them all.
417       for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
418         UmbrellaDirs[SkippedDirs[I]] = Result;
419     }
420 
421     KnownHeader Header(Result, NormalHeader);
422     Headers[File].push_back(Header);
423     return Header;
424   }
425 
426   return KnownHeader();
427 }
428 
429 ArrayRef<ModuleMap::KnownHeader>
430 ModuleMap::findAllModulesForHeader(const FileEntry *File) const {
431   auto It = Headers.find(File);
432   if (It == Headers.end())
433     return None;
434   return It->second;
435 }
436 
437 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
438   return isHeaderUnavailableInModule(Header, nullptr);
439 }
440 
441 bool
442 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
443                                        const Module *RequestingModule) const {
444   HeadersMap::const_iterator Known = Headers.find(Header);
445   if (Known != Headers.end()) {
446     for (SmallVectorImpl<KnownHeader>::const_iterator
447              I = Known->second.begin(),
448              E = Known->second.end();
449          I != E; ++I) {
450 
451       if (I->isAvailable() &&
452           (!RequestingModule ||
453            I->getModule()->isSubModuleOf(RequestingModule))) {
454         // When no requesting module is available, the caller is looking if a
455         // header is part a module by only looking into the module map. This is
456         // done by warn_uncovered_module_header checks; don't consider textual
457         // headers part of it in this mode, otherwise we get misleading warnings
458         // that a umbrella header is not including a textual header.
459         if (!RequestingModule && I->getRole() == ModuleMap::TextualHeader)
460           continue;
461         return false;
462       }
463     }
464     return true;
465   }
466 
467   const DirectoryEntry *Dir = Header->getDir();
468   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
469   StringRef DirName = Dir->getName();
470 
471   auto IsUnavailable = [&](const Module *M) {
472     return !M->isAvailable() && (!RequestingModule ||
473                                  M->isSubModuleOf(RequestingModule));
474   };
475 
476   // Keep walking up the directory hierarchy, looking for a directory with
477   // an umbrella header.
478   do {
479     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
480       = UmbrellaDirs.find(Dir);
481     if (KnownDir != UmbrellaDirs.end()) {
482       Module *Found = KnownDir->second;
483       if (IsUnavailable(Found))
484         return true;
485 
486       // Search up the module stack until we find a module with an umbrella
487       // directory.
488       Module *UmbrellaModule = Found;
489       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
490         UmbrellaModule = UmbrellaModule->Parent;
491 
492       if (UmbrellaModule->InferSubmodules) {
493         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
494           // Find or create the module that corresponds to this directory name.
495           SmallString<32> NameBuf;
496           StringRef Name = sanitizeFilenameAsIdentifier(
497                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
498                              NameBuf);
499           Found = lookupModuleQualified(Name, Found);
500           if (!Found)
501             return false;
502           if (IsUnavailable(Found))
503             return true;
504         }
505 
506         // Infer a submodule with the same name as this header file.
507         SmallString<32> NameBuf;
508         StringRef Name = sanitizeFilenameAsIdentifier(
509                            llvm::sys::path::stem(Header->getName()),
510                            NameBuf);
511         Found = lookupModuleQualified(Name, Found);
512         if (!Found)
513           return false;
514       }
515 
516       return IsUnavailable(Found);
517     }
518 
519     SkippedDirs.push_back(Dir);
520 
521     // Retrieve our parent path.
522     DirName = llvm::sys::path::parent_path(DirName);
523     if (DirName.empty())
524       break;
525 
526     // Resolve the parent path to a directory entry.
527     Dir = SourceMgr.getFileManager().getDirectory(DirName);
528   } while (Dir);
529 
530   return false;
531 }
532 
533 Module *ModuleMap::findModule(StringRef Name) const {
534   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
535   if (Known != Modules.end())
536     return Known->getValue();
537 
538   return nullptr;
539 }
540 
541 Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
542                                            Module *Context) const {
543   for(; Context; Context = Context->Parent) {
544     if (Module *Sub = lookupModuleQualified(Name, Context))
545       return Sub;
546   }
547 
548   return findModule(Name);
549 }
550 
551 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
552   if (!Context)
553     return findModule(Name);
554 
555   return Context->findSubmodule(Name);
556 }
557 
558 std::pair<Module *, bool> ModuleMap::findOrCreateModule(StringRef Name,
559                                                         Module *Parent,
560                                                         bool IsFramework,
561                                                         bool IsExplicit) {
562   // Try to find an existing module with this name.
563   if (Module *Sub = lookupModuleQualified(Name, Parent))
564     return std::make_pair(Sub, false);
565 
566   // Create a new module with this name.
567   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
568                               IsExplicit, NumCreatedModules++);
569   if (!Parent) {
570     if (LangOpts.CurrentModule == Name)
571       SourceModule = Result;
572     Modules[Name] = Result;
573   }
574   return std::make_pair(Result, true);
575 }
576 
577 Module *ModuleMap::createModuleForInterfaceUnit(SourceLocation Loc,
578                                                 StringRef Name) {
579   assert(LangOpts.CurrentModule == Name && "module name mismatch");
580   assert(!Modules[Name] && "redefining existing module");
581 
582   auto *Result =
583       new Module(Name, Loc, nullptr, /*IsFramework*/ false,
584                  /*IsExplicit*/ false, NumCreatedModules++);
585   Modules[Name] = SourceModule = Result;
586 
587   // Mark the main source file as being within the newly-created module so that
588   // declarations and macros are properly visibility-restricted to it.
589   auto *MainFile = SourceMgr.getFileEntryForID(SourceMgr.getMainFileID());
590   assert(MainFile && "no input file for module interface");
591   Headers[MainFile].push_back(KnownHeader(Result, PrivateHeader));
592 
593   return Result;
594 }
595 
596 /// \brief For a framework module, infer the framework against which we
597 /// should link.
598 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
599                                FileManager &FileMgr) {
600   assert(Mod->IsFramework && "Can only infer linking for framework modules");
601   assert(!Mod->isSubFramework() &&
602          "Can only infer linking for top-level frameworks");
603 
604   SmallString<128> LibName;
605   LibName += FrameworkDir->getName();
606   llvm::sys::path::append(LibName, Mod->Name);
607 
608   // The library name of a framework has more than one possible extension since
609   // the introduction of the text-based dynamic library format. We need to check
610   // for both before we give up.
611   for (const char *extension : {"", ".tbd"}) {
612     llvm::sys::path::replace_extension(LibName, extension);
613     if (FileMgr.getFile(LibName)) {
614       Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
615                                                        /*IsFramework=*/true));
616       return;
617     }
618   }
619 }
620 
621 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
622                                         bool IsSystem, Module *Parent) {
623   Attributes Attrs;
624   Attrs.IsSystem = IsSystem;
625   return inferFrameworkModule(FrameworkDir, Attrs, Parent);
626 }
627 
628 Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
629                                         Attributes Attrs, Module *Parent) {
630   // Note: as an egregious but useful hack we use the real path here, because
631   // we might be looking at an embedded framework that symlinks out to a
632   // top-level framework, and we need to infer as if we were naming the
633   // top-level framework.
634   StringRef FrameworkDirName =
635       SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
636 
637   // In case this is a case-insensitive filesystem, use the canonical
638   // directory name as the ModuleName, since modules are case-sensitive.
639   // FIXME: we should be able to give a fix-it hint for the correct spelling.
640   SmallString<32> ModuleNameStorage;
641   StringRef ModuleName = sanitizeFilenameAsIdentifier(
642       llvm::sys::path::stem(FrameworkDirName), ModuleNameStorage);
643 
644   // Check whether we've already found this module.
645   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
646     return Mod;
647 
648   FileManager &FileMgr = SourceMgr.getFileManager();
649 
650   // If the framework has a parent path from which we're allowed to infer
651   // a framework module, do so.
652   const FileEntry *ModuleMapFile = nullptr;
653   if (!Parent) {
654     // Determine whether we're allowed to infer a module map.
655     bool canInfer = false;
656     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
657       // Figure out the parent path.
658       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
659       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
660         // Check whether we have already looked into the parent directory
661         // for a module map.
662         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
663           inferred = InferredDirectories.find(ParentDir);
664         if (inferred == InferredDirectories.end()) {
665           // We haven't looked here before. Load a module map, if there is
666           // one.
667           bool IsFrameworkDir = Parent.endswith(".framework");
668           if (const FileEntry *ModMapFile =
669                 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) {
670             parseModuleMapFile(ModMapFile, Attrs.IsSystem, ParentDir);
671             inferred = InferredDirectories.find(ParentDir);
672           }
673 
674           if (inferred == InferredDirectories.end())
675             inferred = InferredDirectories.insert(
676                          std::make_pair(ParentDir, InferredDirectory())).first;
677         }
678 
679         if (inferred->second.InferModules) {
680           // We're allowed to infer for this directory, but make sure it's okay
681           // to infer this particular module.
682           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
683           canInfer = std::find(inferred->second.ExcludedModules.begin(),
684                                inferred->second.ExcludedModules.end(),
685                                Name) == inferred->second.ExcludedModules.end();
686 
687           Attrs.IsSystem |= inferred->second.Attrs.IsSystem;
688           Attrs.IsExternC |= inferred->second.Attrs.IsExternC;
689           Attrs.IsExhaustive |= inferred->second.Attrs.IsExhaustive;
690           Attrs.NoUndeclaredIncludes |=
691               inferred->second.Attrs.NoUndeclaredIncludes;
692           ModuleMapFile = inferred->second.ModuleMapFile;
693         }
694       }
695     }
696 
697     // If we're not allowed to infer a framework module, don't.
698     if (!canInfer)
699       return nullptr;
700   } else
701     ModuleMapFile = getModuleMapFileForUniquing(Parent);
702 
703 
704   // Look for an umbrella header.
705   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
706   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
707   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
708 
709   // FIXME: If there's no umbrella header, we could probably scan the
710   // framework to load *everything*. But, it's not clear that this is a good
711   // idea.
712   if (!UmbrellaHeader)
713     return nullptr;
714 
715   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
716                               /*IsFramework=*/true, /*IsExplicit=*/false,
717                               NumCreatedModules++);
718   InferredModuleAllowedBy[Result] = ModuleMapFile;
719   Result->IsInferred = true;
720   if (!Parent) {
721     if (LangOpts.CurrentModule == ModuleName)
722       SourceModule = Result;
723     Modules[ModuleName] = Result;
724   }
725 
726   Result->IsSystem |= Attrs.IsSystem;
727   Result->IsExternC |= Attrs.IsExternC;
728   Result->ConfigMacrosExhaustive |= Attrs.IsExhaustive;
729   Result->NoUndeclaredIncludes |= Attrs.NoUndeclaredIncludes;
730   Result->Directory = FrameworkDir;
731 
732   // umbrella header "umbrella-header-name"
733   //
734   // The "Headers/" component of the name is implied because this is
735   // a framework module.
736   setUmbrellaHeader(Result, UmbrellaHeader, ModuleName + ".h");
737 
738   // export *
739   Result->Exports.push_back(Module::ExportDecl(nullptr, true));
740 
741   // module * { export * }
742   Result->InferSubmodules = true;
743   Result->InferExportWildcard = true;
744 
745   // Look for subframeworks.
746   std::error_code EC;
747   SmallString<128> SubframeworksDirName
748     = StringRef(FrameworkDir->getName());
749   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
750   llvm::sys::path::native(SubframeworksDirName);
751   vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
752   for (vfs::directory_iterator Dir = FS.dir_begin(SubframeworksDirName, EC),
753                                DirEnd;
754        Dir != DirEnd && !EC; Dir.increment(EC)) {
755     if (!StringRef(Dir->getName()).endswith(".framework"))
756       continue;
757 
758     if (const DirectoryEntry *SubframeworkDir =
759             FileMgr.getDirectory(Dir->getName())) {
760       // Note: as an egregious but useful hack, we use the real path here and
761       // check whether it is actually a subdirectory of the parent directory.
762       // This will not be the case if the 'subframework' is actually a symlink
763       // out to a top-level framework.
764       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
765       bool FoundParent = false;
766       do {
767         // Get the parent directory name.
768         SubframeworkDirName
769           = llvm::sys::path::parent_path(SubframeworkDirName);
770         if (SubframeworkDirName.empty())
771           break;
772 
773         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
774           FoundParent = true;
775           break;
776         }
777       } while (true);
778 
779       if (!FoundParent)
780         continue;
781 
782       // FIXME: Do we want to warn about subframeworks without umbrella headers?
783       inferFrameworkModule(SubframeworkDir, Attrs, Result);
784     }
785   }
786 
787   // If the module is a top-level framework, automatically link against the
788   // framework.
789   if (!Result->isSubFramework()) {
790     inferFrameworkLink(Result, FrameworkDir, FileMgr);
791   }
792 
793   return Result;
794 }
795 
796 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader,
797                                   Twine NameAsWritten) {
798   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
799   Mod->Umbrella = UmbrellaHeader;
800   Mod->UmbrellaAsWritten = NameAsWritten.str();
801   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
802 
803   // Notify callbacks that we just added a new header.
804   for (const auto &Cb : Callbacks)
805     Cb->moduleMapAddUmbrellaHeader(&SourceMgr.getFileManager(), UmbrellaHeader);
806 }
807 
808 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir,
809                                Twine NameAsWritten) {
810   Mod->Umbrella = UmbrellaDir;
811   Mod->UmbrellaAsWritten = NameAsWritten.str();
812   UmbrellaDirs[UmbrellaDir] = Mod;
813 }
814 
815 static Module::HeaderKind headerRoleToKind(ModuleMap::ModuleHeaderRole Role) {
816   switch ((int)Role) {
817   default: llvm_unreachable("unknown header role");
818   case ModuleMap::NormalHeader:
819     return Module::HK_Normal;
820   case ModuleMap::PrivateHeader:
821     return Module::HK_Private;
822   case ModuleMap::TextualHeader:
823     return Module::HK_Textual;
824   case ModuleMap::PrivateHeader | ModuleMap::TextualHeader:
825     return Module::HK_PrivateTextual;
826   }
827 }
828 
829 void ModuleMap::addHeader(Module *Mod, Module::Header Header,
830                           ModuleHeaderRole Role, bool Imported) {
831   KnownHeader KH(Mod, Role);
832 
833   // Only add each header to the headers list once.
834   // FIXME: Should we diagnose if a header is listed twice in the
835   // same module definition?
836   auto &HeaderList = Headers[Header.Entry];
837   for (auto H : HeaderList)
838     if (H == KH)
839       return;
840 
841   HeaderList.push_back(KH);
842   Mod->Headers[headerRoleToKind(Role)].push_back(Header);
843 
844   bool isCompilingModuleHeader =
845       LangOpts.isCompilingModule() && Mod->getTopLevelModule() == SourceModule;
846   if (!Imported || isCompilingModuleHeader) {
847     // When we import HeaderFileInfo, the external source is expected to
848     // set the isModuleHeader flag itself.
849     HeaderInfo.MarkFileModuleHeader(Header.Entry, Role,
850                                     isCompilingModuleHeader);
851   }
852 
853   // Notify callbacks that we just added a new header.
854   for (const auto &Cb : Callbacks)
855     Cb->moduleMapAddHeader(Header.Entry->getName());
856 }
857 
858 void ModuleMap::excludeHeader(Module *Mod, Module::Header Header) {
859   // Add this as a known header so we won't implicitly add it to any
860   // umbrella directory module.
861   // FIXME: Should we only exclude it from umbrella modules within the
862   // specified module?
863   (void) Headers[Header.Entry];
864 
865   Mod->Headers[Module::HK_Excluded].push_back(std::move(Header));
866 }
867 
868 const FileEntry *
869 ModuleMap::getContainingModuleMapFile(const Module *Module) const {
870   if (Module->DefinitionLoc.isInvalid())
871     return nullptr;
872 
873   return SourceMgr.getFileEntryForID(
874            SourceMgr.getFileID(Module->DefinitionLoc));
875 }
876 
877 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
878   if (M->IsInferred) {
879     assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
880     return InferredModuleAllowedBy.find(M)->second;
881   }
882   return getContainingModuleMapFile(M);
883 }
884 
885 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
886   assert(M->IsInferred && "module not inferred");
887   InferredModuleAllowedBy[M] = ModMap;
888 }
889 
890 LLVM_DUMP_METHOD void ModuleMap::dump() {
891   llvm::errs() << "Modules:";
892   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
893                                         MEnd = Modules.end();
894        M != MEnd; ++M)
895     M->getValue()->print(llvm::errs(), 2);
896 
897   llvm::errs() << "Headers:";
898   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
899        H != HEnd; ++H) {
900     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
901     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
902                                                       E = H->second.end();
903          I != E; ++I) {
904       if (I != H->second.begin())
905         llvm::errs() << ",";
906       llvm::errs() << I->getModule()->getFullModuleName();
907     }
908     llvm::errs() << "\n";
909   }
910 }
911 
912 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
913   auto Unresolved = std::move(Mod->UnresolvedExports);
914   Mod->UnresolvedExports.clear();
915   for (auto &UE : Unresolved) {
916     Module::ExportDecl Export = resolveExport(Mod, UE, Complain);
917     if (Export.getPointer() || Export.getInt())
918       Mod->Exports.push_back(Export);
919     else
920       Mod->UnresolvedExports.push_back(UE);
921   }
922   return !Mod->UnresolvedExports.empty();
923 }
924 
925 bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
926   auto Unresolved = std::move(Mod->UnresolvedDirectUses);
927   Mod->UnresolvedDirectUses.clear();
928   for (auto &UDU : Unresolved) {
929     Module *DirectUse = resolveModuleId(UDU, Mod, Complain);
930     if (DirectUse)
931       Mod->DirectUses.push_back(DirectUse);
932     else
933       Mod->UnresolvedDirectUses.push_back(UDU);
934   }
935   return !Mod->UnresolvedDirectUses.empty();
936 }
937 
938 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
939   auto Unresolved = std::move(Mod->UnresolvedConflicts);
940   Mod->UnresolvedConflicts.clear();
941   for (auto &UC : Unresolved) {
942     if (Module *OtherMod = resolveModuleId(UC.Id, Mod, Complain)) {
943       Module::Conflict Conflict;
944       Conflict.Other = OtherMod;
945       Conflict.Message = UC.Message;
946       Mod->Conflicts.push_back(Conflict);
947     } else
948       Mod->UnresolvedConflicts.push_back(UC);
949   }
950   return !Mod->UnresolvedConflicts.empty();
951 }
952 
953 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
954   if (Loc.isInvalid())
955     return nullptr;
956 
957   if (UmbrellaDirs.empty() && Headers.empty())
958     return nullptr;
959 
960   // Use the expansion location to determine which module we're in.
961   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
962   if (!ExpansionLoc.isFileID())
963     return nullptr;
964 
965   const SourceManager &SrcMgr = Loc.getManager();
966   FileID ExpansionFileID = ExpansionLoc.getFileID();
967 
968   while (const FileEntry *ExpansionFile
969            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
970     // Find the module that owns this header (if any).
971     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
972       return Mod;
973 
974     // No module owns this header, so look up the inclusion chain to see if
975     // any included header has an associated module.
976     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
977     if (IncludeLoc.isInvalid())
978       return nullptr;
979 
980     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
981   }
982 
983   return nullptr;
984 }
985 
986 //----------------------------------------------------------------------------//
987 // Module map file parser
988 //----------------------------------------------------------------------------//
989 
990 namespace clang {
991   /// \brief A token in a module map file.
992   struct MMToken {
993     enum TokenKind {
994       Comma,
995       ConfigMacros,
996       Conflict,
997       EndOfFile,
998       HeaderKeyword,
999       Identifier,
1000       Exclaim,
1001       ExcludeKeyword,
1002       ExplicitKeyword,
1003       ExportKeyword,
1004       ExternKeyword,
1005       FrameworkKeyword,
1006       LinkKeyword,
1007       ModuleKeyword,
1008       Period,
1009       PrivateKeyword,
1010       UmbrellaKeyword,
1011       UseKeyword,
1012       RequiresKeyword,
1013       Star,
1014       StringLiteral,
1015       TextualKeyword,
1016       LBrace,
1017       RBrace,
1018       LSquare,
1019       RSquare
1020     } Kind;
1021 
1022     unsigned Location;
1023     unsigned StringLength;
1024     const char *StringData;
1025 
1026     void clear() {
1027       Kind = EndOfFile;
1028       Location = 0;
1029       StringLength = 0;
1030       StringData = nullptr;
1031     }
1032 
1033     bool is(TokenKind K) const { return Kind == K; }
1034 
1035     SourceLocation getLocation() const {
1036       return SourceLocation::getFromRawEncoding(Location);
1037     }
1038 
1039     StringRef getString() const {
1040       return StringRef(StringData, StringLength);
1041     }
1042   };
1043 
1044   class ModuleMapParser {
1045     Lexer &L;
1046     SourceManager &SourceMgr;
1047 
1048     /// \brief Default target information, used only for string literal
1049     /// parsing.
1050     const TargetInfo *Target;
1051 
1052     DiagnosticsEngine &Diags;
1053     ModuleMap &Map;
1054 
1055     /// \brief The current module map file.
1056     const FileEntry *ModuleMapFile;
1057 
1058     /// \brief The directory that file names in this module map file should
1059     /// be resolved relative to.
1060     const DirectoryEntry *Directory;
1061 
1062     /// \brief The directory containing Clang-supplied headers.
1063     const DirectoryEntry *BuiltinIncludeDir;
1064 
1065     /// \brief Whether this module map is in a system header directory.
1066     bool IsSystem;
1067 
1068     /// \brief Whether an error occurred.
1069     bool HadError;
1070 
1071     /// \brief Stores string data for the various string literals referenced
1072     /// during parsing.
1073     llvm::BumpPtrAllocator StringData;
1074 
1075     /// \brief The current token.
1076     MMToken Tok;
1077 
1078     /// \brief The active module.
1079     Module *ActiveModule;
1080 
1081     /// \brief Whether a module uses the 'requires excluded' hack to mark its
1082     /// contents as 'textual'.
1083     ///
1084     /// On older Darwin SDK versions, 'requires excluded' is used to mark the
1085     /// contents of the Darwin.C.excluded (assert.h) and Tcl.Private modules as
1086     /// non-modular headers.  For backwards compatibility, we continue to
1087     /// support this idiom for just these modules, and map the headers to
1088     /// 'textual' to match the original intent.
1089     llvm::SmallPtrSet<Module *, 2> UsesRequiresExcludedHack;
1090 
1091     /// \brief Consume the current token and return its location.
1092     SourceLocation consumeToken();
1093 
1094     /// \brief Skip tokens until we reach the a token with the given kind
1095     /// (or the end of the file).
1096     void skipUntil(MMToken::TokenKind K);
1097 
1098     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
1099     bool parseModuleId(ModuleId &Id);
1100     void parseModuleDecl();
1101     void parseExternModuleDecl();
1102     void parseRequiresDecl();
1103     void parseHeaderDecl(clang::MMToken::TokenKind,
1104                          SourceLocation LeadingLoc);
1105     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
1106     void parseExportDecl();
1107     void parseUseDecl();
1108     void parseLinkDecl();
1109     void parseConfigMacros();
1110     void parseConflict();
1111     void parseInferredModuleDecl(bool Framework, bool Explicit);
1112 
1113     typedef ModuleMap::Attributes Attributes;
1114     bool parseOptionalAttributes(Attributes &Attrs);
1115 
1116   public:
1117     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
1118                              const TargetInfo *Target,
1119                              DiagnosticsEngine &Diags,
1120                              ModuleMap &Map,
1121                              const FileEntry *ModuleMapFile,
1122                              const DirectoryEntry *Directory,
1123                              const DirectoryEntry *BuiltinIncludeDir,
1124                              bool IsSystem)
1125       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
1126         ModuleMapFile(ModuleMapFile), Directory(Directory),
1127         BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem),
1128         HadError(false), ActiveModule(nullptr)
1129     {
1130       Tok.clear();
1131       consumeToken();
1132     }
1133 
1134     bool parseModuleMapFile();
1135   };
1136 }
1137 
1138 SourceLocation ModuleMapParser::consumeToken() {
1139 retry:
1140   SourceLocation Result = Tok.getLocation();
1141   Tok.clear();
1142 
1143   Token LToken;
1144   L.LexFromRawLexer(LToken);
1145   Tok.Location = LToken.getLocation().getRawEncoding();
1146   switch (LToken.getKind()) {
1147   case tok::raw_identifier: {
1148     StringRef RI = LToken.getRawIdentifier();
1149     Tok.StringData = RI.data();
1150     Tok.StringLength = RI.size();
1151     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
1152                  .Case("config_macros", MMToken::ConfigMacros)
1153                  .Case("conflict", MMToken::Conflict)
1154                  .Case("exclude", MMToken::ExcludeKeyword)
1155                  .Case("explicit", MMToken::ExplicitKeyword)
1156                  .Case("export", MMToken::ExportKeyword)
1157                  .Case("extern", MMToken::ExternKeyword)
1158                  .Case("framework", MMToken::FrameworkKeyword)
1159                  .Case("header", MMToken::HeaderKeyword)
1160                  .Case("link", MMToken::LinkKeyword)
1161                  .Case("module", MMToken::ModuleKeyword)
1162                  .Case("private", MMToken::PrivateKeyword)
1163                  .Case("requires", MMToken::RequiresKeyword)
1164                  .Case("textual", MMToken::TextualKeyword)
1165                  .Case("umbrella", MMToken::UmbrellaKeyword)
1166                  .Case("use", MMToken::UseKeyword)
1167                  .Default(MMToken::Identifier);
1168     break;
1169   }
1170 
1171   case tok::comma:
1172     Tok.Kind = MMToken::Comma;
1173     break;
1174 
1175   case tok::eof:
1176     Tok.Kind = MMToken::EndOfFile;
1177     break;
1178 
1179   case tok::l_brace:
1180     Tok.Kind = MMToken::LBrace;
1181     break;
1182 
1183   case tok::l_square:
1184     Tok.Kind = MMToken::LSquare;
1185     break;
1186 
1187   case tok::period:
1188     Tok.Kind = MMToken::Period;
1189     break;
1190 
1191   case tok::r_brace:
1192     Tok.Kind = MMToken::RBrace;
1193     break;
1194 
1195   case tok::r_square:
1196     Tok.Kind = MMToken::RSquare;
1197     break;
1198 
1199   case tok::star:
1200     Tok.Kind = MMToken::Star;
1201     break;
1202 
1203   case tok::exclaim:
1204     Tok.Kind = MMToken::Exclaim;
1205     break;
1206 
1207   case tok::string_literal: {
1208     if (LToken.hasUDSuffix()) {
1209       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
1210       HadError = true;
1211       goto retry;
1212     }
1213 
1214     // Parse the string literal.
1215     LangOptions LangOpts;
1216     StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
1217     if (StringLiteral.hadError)
1218       goto retry;
1219 
1220     // Copy the string literal into our string data allocator.
1221     unsigned Length = StringLiteral.GetStringLength();
1222     char *Saved = StringData.Allocate<char>(Length + 1);
1223     memcpy(Saved, StringLiteral.GetString().data(), Length);
1224     Saved[Length] = 0;
1225 
1226     // Form the token.
1227     Tok.Kind = MMToken::StringLiteral;
1228     Tok.StringData = Saved;
1229     Tok.StringLength = Length;
1230     break;
1231   }
1232 
1233   case tok::comment:
1234     goto retry;
1235 
1236   default:
1237     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
1238     HadError = true;
1239     goto retry;
1240   }
1241 
1242   return Result;
1243 }
1244 
1245 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1246   unsigned braceDepth = 0;
1247   unsigned squareDepth = 0;
1248   do {
1249     switch (Tok.Kind) {
1250     case MMToken::EndOfFile:
1251       return;
1252 
1253     case MMToken::LBrace:
1254       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1255         return;
1256 
1257       ++braceDepth;
1258       break;
1259 
1260     case MMToken::LSquare:
1261       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1262         return;
1263 
1264       ++squareDepth;
1265       break;
1266 
1267     case MMToken::RBrace:
1268       if (braceDepth > 0)
1269         --braceDepth;
1270       else if (Tok.is(K))
1271         return;
1272       break;
1273 
1274     case MMToken::RSquare:
1275       if (squareDepth > 0)
1276         --squareDepth;
1277       else if (Tok.is(K))
1278         return;
1279       break;
1280 
1281     default:
1282       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1283         return;
1284       break;
1285     }
1286 
1287    consumeToken();
1288   } while (true);
1289 }
1290 
1291 /// \brief Parse a module-id.
1292 ///
1293 ///   module-id:
1294 ///     identifier
1295 ///     identifier '.' module-id
1296 ///
1297 /// \returns true if an error occurred, false otherwise.
1298 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1299   Id.clear();
1300   do {
1301     if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
1302       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1303       consumeToken();
1304     } else {
1305       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1306       return true;
1307     }
1308 
1309     if (!Tok.is(MMToken::Period))
1310       break;
1311 
1312     consumeToken();
1313   } while (true);
1314 
1315   return false;
1316 }
1317 
1318 namespace {
1319   /// \brief Enumerates the known attributes.
1320   enum AttributeKind {
1321     /// \brief An unknown attribute.
1322     AT_unknown,
1323     /// \brief The 'system' attribute.
1324     AT_system,
1325     /// \brief The 'extern_c' attribute.
1326     AT_extern_c,
1327     /// \brief The 'exhaustive' attribute.
1328     AT_exhaustive,
1329     /// \brief The 'no_undeclared_includes' attribute.
1330     AT_no_undeclared_includes
1331   };
1332 }
1333 
1334 /// \brief Parse a module declaration.
1335 ///
1336 ///   module-declaration:
1337 ///     'extern' 'module' module-id string-literal
1338 ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1339 ///       { module-member* }
1340 ///
1341 ///   module-member:
1342 ///     requires-declaration
1343 ///     header-declaration
1344 ///     submodule-declaration
1345 ///     export-declaration
1346 ///     link-declaration
1347 ///
1348 ///   submodule-declaration:
1349 ///     module-declaration
1350 ///     inferred-submodule-declaration
1351 void ModuleMapParser::parseModuleDecl() {
1352   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1353          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
1354   if (Tok.is(MMToken::ExternKeyword)) {
1355     parseExternModuleDecl();
1356     return;
1357   }
1358 
1359   // Parse 'explicit' or 'framework' keyword, if present.
1360   SourceLocation ExplicitLoc;
1361   bool Explicit = false;
1362   bool Framework = false;
1363 
1364   // Parse 'explicit' keyword, if present.
1365   if (Tok.is(MMToken::ExplicitKeyword)) {
1366     ExplicitLoc = consumeToken();
1367     Explicit = true;
1368   }
1369 
1370   // Parse 'framework' keyword, if present.
1371   if (Tok.is(MMToken::FrameworkKeyword)) {
1372     consumeToken();
1373     Framework = true;
1374   }
1375 
1376   // Parse 'module' keyword.
1377   if (!Tok.is(MMToken::ModuleKeyword)) {
1378     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1379     consumeToken();
1380     HadError = true;
1381     return;
1382   }
1383   consumeToken(); // 'module' keyword
1384 
1385   // If we have a wildcard for the module name, this is an inferred submodule.
1386   // Parse it.
1387   if (Tok.is(MMToken::Star))
1388     return parseInferredModuleDecl(Framework, Explicit);
1389 
1390   // Parse the module name.
1391   ModuleId Id;
1392   if (parseModuleId(Id)) {
1393     HadError = true;
1394     return;
1395   }
1396 
1397   if (ActiveModule) {
1398     if (Id.size() > 1) {
1399       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1400         << SourceRange(Id.front().second, Id.back().second);
1401 
1402       HadError = true;
1403       return;
1404     }
1405   } else if (Id.size() == 1 && Explicit) {
1406     // Top-level modules can't be explicit.
1407     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1408     Explicit = false;
1409     ExplicitLoc = SourceLocation();
1410     HadError = true;
1411   }
1412 
1413   Module *PreviousActiveModule = ActiveModule;
1414   if (Id.size() > 1) {
1415     // This module map defines a submodule. Go find the module of which it
1416     // is a submodule.
1417     ActiveModule = nullptr;
1418     const Module *TopLevelModule = nullptr;
1419     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1420       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1421         if (I == 0)
1422           TopLevelModule = Next;
1423         ActiveModule = Next;
1424         continue;
1425       }
1426 
1427       if (ActiveModule) {
1428         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1429           << Id[I].first
1430           << ActiveModule->getTopLevelModule()->getFullModuleName();
1431       } else {
1432         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1433       }
1434       HadError = true;
1435       return;
1436     }
1437 
1438     if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
1439       assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
1440              "submodule defined in same file as 'module *' that allowed its "
1441              "top-level module");
1442       Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
1443     }
1444   }
1445 
1446   StringRef ModuleName = Id.back().first;
1447   SourceLocation ModuleNameLoc = Id.back().second;
1448 
1449   // Parse the optional attribute list.
1450   Attributes Attrs;
1451   if (parseOptionalAttributes(Attrs))
1452     return;
1453 
1454 
1455   // Parse the opening brace.
1456   if (!Tok.is(MMToken::LBrace)) {
1457     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1458       << ModuleName;
1459     HadError = true;
1460     return;
1461   }
1462   SourceLocation LBraceLoc = consumeToken();
1463 
1464   // Determine whether this (sub)module has already been defined.
1465   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1466     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1467       // Skip the module definition.
1468       skipUntil(MMToken::RBrace);
1469       if (Tok.is(MMToken::RBrace))
1470         consumeToken();
1471       else {
1472         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1473         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1474         HadError = true;
1475       }
1476       return;
1477     }
1478 
1479     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1480       << ModuleName;
1481     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1482 
1483     // Skip the module definition.
1484     skipUntil(MMToken::RBrace);
1485     if (Tok.is(MMToken::RBrace))
1486       consumeToken();
1487 
1488     HadError = true;
1489     return;
1490   }
1491 
1492   // Start defining this module.
1493   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1494                                         Explicit).first;
1495   ActiveModule->DefinitionLoc = ModuleNameLoc;
1496   if (Attrs.IsSystem || IsSystem)
1497     ActiveModule->IsSystem = true;
1498   if (Attrs.IsExternC)
1499     ActiveModule->IsExternC = true;
1500   if (Attrs.NoUndeclaredIncludes ||
1501       (!ActiveModule->Parent && ModuleName == "Darwin"))
1502     ActiveModule->NoUndeclaredIncludes = true;
1503   ActiveModule->Directory = Directory;
1504 
1505   if (!ActiveModule->Parent) {
1506     StringRef MapFileName(ModuleMapFile->getName());
1507     if (MapFileName.endswith("module.private.modulemap") ||
1508         MapFileName.endswith("module_private.map")) {
1509       // Adding a top-level module from a private modulemap is likely a
1510       // user error; we check to see if there's another top-level module
1511       // defined in the non-private map in the same dir, and if so emit a
1512       // warning.
1513       for (auto E = Map.module_begin(); E != Map.module_end(); ++E) {
1514         auto const *M = E->getValue();
1515         if (!M->Parent &&
1516             M->Directory == ActiveModule->Directory &&
1517             M->Name != ActiveModule->Name) {
1518           Diags.Report(ActiveModule->DefinitionLoc,
1519                        diag::warn_mmap_mismatched_top_level_private)
1520             << ActiveModule->Name << M->Name;
1521           // The pattern we're defending against here is typically due to
1522           // a module named FooPrivate which is supposed to be a submodule
1523           // called Foo.Private. Emit a fixit in that case.
1524           auto D =
1525             Diags.Report(ActiveModule->DefinitionLoc,
1526                          diag::note_mmap_rename_top_level_private_as_submodule);
1527           D << ActiveModule->Name << M->Name;
1528           StringRef Bad(ActiveModule->Name);
1529           if (Bad.consume_back("Private")) {
1530             SmallString<128> Fixed = Bad;
1531             Fixed.append(".Private");
1532             D << FixItHint::CreateReplacement(ActiveModule->DefinitionLoc,
1533                                               Fixed);
1534           }
1535           break;
1536         }
1537       }
1538     }
1539   }
1540 
1541   bool Done = false;
1542   do {
1543     switch (Tok.Kind) {
1544     case MMToken::EndOfFile:
1545     case MMToken::RBrace:
1546       Done = true;
1547       break;
1548 
1549     case MMToken::ConfigMacros:
1550       parseConfigMacros();
1551       break;
1552 
1553     case MMToken::Conflict:
1554       parseConflict();
1555       break;
1556 
1557     case MMToken::ExplicitKeyword:
1558     case MMToken::ExternKeyword:
1559     case MMToken::FrameworkKeyword:
1560     case MMToken::ModuleKeyword:
1561       parseModuleDecl();
1562       break;
1563 
1564     case MMToken::ExportKeyword:
1565       parseExportDecl();
1566       break;
1567 
1568     case MMToken::UseKeyword:
1569       parseUseDecl();
1570       break;
1571 
1572     case MMToken::RequiresKeyword:
1573       parseRequiresDecl();
1574       break;
1575 
1576     case MMToken::TextualKeyword:
1577       parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
1578       break;
1579 
1580     case MMToken::UmbrellaKeyword: {
1581       SourceLocation UmbrellaLoc = consumeToken();
1582       if (Tok.is(MMToken::HeaderKeyword))
1583         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1584       else
1585         parseUmbrellaDirDecl(UmbrellaLoc);
1586       break;
1587     }
1588 
1589     case MMToken::ExcludeKeyword:
1590       parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
1591       break;
1592 
1593     case MMToken::PrivateKeyword:
1594       parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
1595       break;
1596 
1597     case MMToken::HeaderKeyword:
1598       parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
1599       break;
1600 
1601     case MMToken::LinkKeyword:
1602       parseLinkDecl();
1603       break;
1604 
1605     default:
1606       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1607       consumeToken();
1608       break;
1609     }
1610   } while (!Done);
1611 
1612   if (Tok.is(MMToken::RBrace))
1613     consumeToken();
1614   else {
1615     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1616     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1617     HadError = true;
1618   }
1619 
1620   // If the active module is a top-level framework, and there are no link
1621   // libraries, automatically link against the framework.
1622   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
1623       ActiveModule->LinkLibraries.empty()) {
1624     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
1625   }
1626 
1627   // If the module meets all requirements but is still unavailable, mark the
1628   // whole tree as unavailable to prevent it from building.
1629   if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement &&
1630       ActiveModule->Parent) {
1631     ActiveModule->getTopLevelModule()->markUnavailable();
1632     ActiveModule->getTopLevelModule()->MissingHeaders.append(
1633       ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
1634   }
1635 
1636   // We're done parsing this module. Pop back to the previous module.
1637   ActiveModule = PreviousActiveModule;
1638 }
1639 
1640 /// \brief Parse an extern module declaration.
1641 ///
1642 ///   extern module-declaration:
1643 ///     'extern' 'module' module-id string-literal
1644 void ModuleMapParser::parseExternModuleDecl() {
1645   assert(Tok.is(MMToken::ExternKeyword));
1646   SourceLocation ExternLoc = consumeToken(); // 'extern' keyword
1647 
1648   // Parse 'module' keyword.
1649   if (!Tok.is(MMToken::ModuleKeyword)) {
1650     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1651     consumeToken();
1652     HadError = true;
1653     return;
1654   }
1655   consumeToken(); // 'module' keyword
1656 
1657   // Parse the module name.
1658   ModuleId Id;
1659   if (parseModuleId(Id)) {
1660     HadError = true;
1661     return;
1662   }
1663 
1664   // Parse the referenced module map file name.
1665   if (!Tok.is(MMToken::StringLiteral)) {
1666     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
1667     HadError = true;
1668     return;
1669   }
1670   std::string FileName = Tok.getString();
1671   consumeToken(); // filename
1672 
1673   StringRef FileNameRef = FileName;
1674   SmallString<128> ModuleMapFileName;
1675   if (llvm::sys::path::is_relative(FileNameRef)) {
1676     ModuleMapFileName += Directory->getName();
1677     llvm::sys::path::append(ModuleMapFileName, FileName);
1678     FileNameRef = ModuleMapFileName;
1679   }
1680   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
1681     Map.parseModuleMapFile(
1682         File, /*IsSystem=*/false,
1683         Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
1684             ? Directory
1685             : File->getDir(), ExternLoc);
1686 }
1687 
1688 /// Whether to add the requirement \p Feature to the module \p M.
1689 ///
1690 /// This preserves backwards compatibility for two hacks in the Darwin system
1691 /// module map files:
1692 ///
1693 /// 1. The use of 'requires excluded' to make headers non-modular, which
1694 ///    should really be mapped to 'textual' now that we have this feature.  We
1695 ///    drop the 'excluded' requirement, and set \p IsRequiresExcludedHack to
1696 ///    true.  Later, this bit will be used to map all the headers inside this
1697 ///    module to 'textual'.
1698 ///
1699 ///    This affects Darwin.C.excluded (for assert.h) and Tcl.Private.
1700 ///
1701 /// 2. Removes a bogus cplusplus requirement from IOKit.avc.  This requirement
1702 ///    was never correct and causes issues now that we check it, so drop it.
1703 static bool shouldAddRequirement(Module *M, StringRef Feature,
1704                                  bool &IsRequiresExcludedHack) {
1705   if (Feature == "excluded" &&
1706       (M->fullModuleNameIs({"Darwin", "C", "excluded"}) ||
1707        M->fullModuleNameIs({"Tcl", "Private"}))) {
1708     IsRequiresExcludedHack = true;
1709     return false;
1710   } else if (Feature == "cplusplus" && M->fullModuleNameIs({"IOKit", "avc"})) {
1711     return false;
1712   }
1713 
1714   return true;
1715 }
1716 
1717 /// \brief Parse a requires declaration.
1718 ///
1719 ///   requires-declaration:
1720 ///     'requires' feature-list
1721 ///
1722 ///   feature-list:
1723 ///     feature ',' feature-list
1724 ///     feature
1725 ///
1726 ///   feature:
1727 ///     '!'[opt] identifier
1728 void ModuleMapParser::parseRequiresDecl() {
1729   assert(Tok.is(MMToken::RequiresKeyword));
1730 
1731   // Parse 'requires' keyword.
1732   consumeToken();
1733 
1734   // Parse the feature-list.
1735   do {
1736     bool RequiredState = true;
1737     if (Tok.is(MMToken::Exclaim)) {
1738       RequiredState = false;
1739       consumeToken();
1740     }
1741 
1742     if (!Tok.is(MMToken::Identifier)) {
1743       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
1744       HadError = true;
1745       return;
1746     }
1747 
1748     // Consume the feature name.
1749     std::string Feature = Tok.getString();
1750     consumeToken();
1751 
1752     bool IsRequiresExcludedHack = false;
1753     bool ShouldAddRequirement =
1754         shouldAddRequirement(ActiveModule, Feature, IsRequiresExcludedHack);
1755 
1756     if (IsRequiresExcludedHack)
1757       UsesRequiresExcludedHack.insert(ActiveModule);
1758 
1759     if (ShouldAddRequirement) {
1760       // Add this feature.
1761       ActiveModule->addRequirement(Feature, RequiredState, Map.LangOpts,
1762                                    *Map.Target);
1763     }
1764 
1765     if (!Tok.is(MMToken::Comma))
1766       break;
1767 
1768     // Consume the comma.
1769     consumeToken();
1770   } while (true);
1771 }
1772 
1773 /// \brief Append to \p Paths the set of paths needed to get to the
1774 /// subframework in which the given module lives.
1775 static void appendSubframeworkPaths(Module *Mod,
1776                                     SmallVectorImpl<char> &Path) {
1777   // Collect the framework names from the given module to the top-level module.
1778   SmallVector<StringRef, 2> Paths;
1779   for (; Mod; Mod = Mod->Parent) {
1780     if (Mod->IsFramework)
1781       Paths.push_back(Mod->Name);
1782   }
1783 
1784   if (Paths.empty())
1785     return;
1786 
1787   // Add Frameworks/Name.framework for each subframework.
1788   for (unsigned I = Paths.size() - 1; I != 0; --I)
1789     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
1790 }
1791 
1792 /// \brief Parse a header declaration.
1793 ///
1794 ///   header-declaration:
1795 ///     'textual'[opt] 'header' string-literal
1796 ///     'private' 'textual'[opt] 'header' string-literal
1797 ///     'exclude' 'header' string-literal
1798 ///     'umbrella' 'header' string-literal
1799 ///
1800 /// FIXME: Support 'private textual header'.
1801 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
1802                                       SourceLocation LeadingLoc) {
1803   // We've already consumed the first token.
1804   ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
1805   if (LeadingToken == MMToken::PrivateKeyword) {
1806     Role = ModuleMap::PrivateHeader;
1807     // 'private' may optionally be followed by 'textual'.
1808     if (Tok.is(MMToken::TextualKeyword)) {
1809       LeadingToken = Tok.Kind;
1810       consumeToken();
1811     }
1812   }
1813 
1814   if (LeadingToken == MMToken::TextualKeyword)
1815     Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
1816 
1817   if (UsesRequiresExcludedHack.count(ActiveModule)) {
1818     // Mark this header 'textual' (see doc comment for
1819     // Module::UsesRequiresExcludedHack).
1820     Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
1821   }
1822 
1823   if (LeadingToken != MMToken::HeaderKeyword) {
1824     if (!Tok.is(MMToken::HeaderKeyword)) {
1825       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1826           << (LeadingToken == MMToken::PrivateKeyword ? "private" :
1827               LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
1828               LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
1829       return;
1830     }
1831     consumeToken();
1832   }
1833 
1834   // Parse the header name.
1835   if (!Tok.is(MMToken::StringLiteral)) {
1836     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1837       << "header";
1838     HadError = true;
1839     return;
1840   }
1841   Module::UnresolvedHeaderDirective Header;
1842   Header.FileName = Tok.getString();
1843   Header.FileNameLoc = consumeToken();
1844 
1845   // Check whether we already have an umbrella.
1846   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
1847     Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
1848       << ActiveModule->getFullModuleName();
1849     HadError = true;
1850     return;
1851   }
1852 
1853   // Look for this file.
1854   const FileEntry *File = nullptr;
1855   const FileEntry *BuiltinFile = nullptr;
1856   SmallString<128> RelativePathName;
1857   if (llvm::sys::path::is_absolute(Header.FileName)) {
1858     RelativePathName = Header.FileName;
1859     File = SourceMgr.getFileManager().getFile(RelativePathName);
1860   } else {
1861     // Search for the header file within the search directory.
1862     SmallString<128> FullPathName(Directory->getName());
1863     unsigned FullPathLength = FullPathName.size();
1864 
1865     if (ActiveModule->isPartOfFramework()) {
1866       appendSubframeworkPaths(ActiveModule, RelativePathName);
1867 
1868       // Check whether this file is in the public headers.
1869       llvm::sys::path::append(RelativePathName, "Headers", Header.FileName);
1870       llvm::sys::path::append(FullPathName, RelativePathName);
1871       File = SourceMgr.getFileManager().getFile(FullPathName);
1872 
1873       if (!File) {
1874         // Check whether this file is in the private headers.
1875         // FIXME: Should we retain the subframework paths here?
1876         RelativePathName.clear();
1877         FullPathName.resize(FullPathLength);
1878         llvm::sys::path::append(RelativePathName, "PrivateHeaders",
1879                                 Header.FileName);
1880         llvm::sys::path::append(FullPathName, RelativePathName);
1881         File = SourceMgr.getFileManager().getFile(FullPathName);
1882       }
1883     } else {
1884       // Lookup for normal headers.
1885       llvm::sys::path::append(RelativePathName, Header.FileName);
1886       llvm::sys::path::append(FullPathName, RelativePathName);
1887       File = SourceMgr.getFileManager().getFile(FullPathName);
1888 
1889       // If this is a system module with a top-level header, this header
1890       // may have a counterpart (or replacement) in the set of headers
1891       // supplied by Clang. Find that builtin header.
1892       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
1893           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
1894           ModuleMap::isBuiltinHeader(Header.FileName)) {
1895         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
1896         llvm::sys::path::append(BuiltinPathName, Header.FileName);
1897         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
1898 
1899         // If Clang supplies this header but the underlying system does not,
1900         // just silently swap in our builtin version. Otherwise, we'll end
1901         // up adding both (later).
1902         if (BuiltinFile && !File) {
1903           File = BuiltinFile;
1904           RelativePathName = BuiltinPathName;
1905           BuiltinFile = nullptr;
1906         }
1907       }
1908     }
1909   }
1910 
1911   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
1912   // Come up with a lazy way to do this.
1913   if (File) {
1914     if (LeadingToken == MMToken::UmbrellaKeyword) {
1915       const DirectoryEntry *UmbrellaDir = File->getDir();
1916       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1917         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
1918           << UmbrellaModule->getFullModuleName();
1919         HadError = true;
1920       } else {
1921         // Record this umbrella header.
1922         Map.setUmbrellaHeader(ActiveModule, File, RelativePathName.str());
1923       }
1924     } else if (LeadingToken == MMToken::ExcludeKeyword) {
1925       Module::Header H = {RelativePathName.str(), File};
1926       Map.excludeHeader(ActiveModule, H);
1927     } else {
1928       // If there is a builtin counterpart to this file, add it now so it can
1929       // wrap the system header.
1930       if (BuiltinFile) {
1931         // FIXME: Taking the name from the FileEntry is unstable and can give
1932         // different results depending on how we've previously named that file
1933         // in this build.
1934         Module::Header H = { BuiltinFile->getName(), BuiltinFile };
1935         Map.addHeader(ActiveModule, H, Role);
1936 
1937         // If we have both a builtin and system version of the file, the
1938         // builtin version may want to inject macros into the system header, so
1939         // force the system header to be treated as a textual header in this
1940         // case.
1941         Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
1942       }
1943 
1944       // Record this header.
1945       Module::Header H = { RelativePathName.str(), File };
1946       Map.addHeader(ActiveModule, H, Role);
1947     }
1948   } else if (LeadingToken != MMToken::ExcludeKeyword) {
1949     // Ignore excluded header files. They're optional anyway.
1950 
1951     // If we find a module that has a missing header, we mark this module as
1952     // unavailable and store the header directive for displaying diagnostics.
1953     Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
1954     ActiveModule->markUnavailable();
1955     ActiveModule->MissingHeaders.push_back(Header);
1956   }
1957 }
1958 
1959 static int compareModuleHeaders(const Module::Header *A,
1960                                 const Module::Header *B) {
1961   return A->NameAsWritten.compare(B->NameAsWritten);
1962 }
1963 
1964 /// \brief Parse an umbrella directory declaration.
1965 ///
1966 ///   umbrella-dir-declaration:
1967 ///     umbrella string-literal
1968 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1969   // Parse the directory name.
1970   if (!Tok.is(MMToken::StringLiteral)) {
1971     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1972       << "umbrella";
1973     HadError = true;
1974     return;
1975   }
1976 
1977   std::string DirName = Tok.getString();
1978   SourceLocation DirNameLoc = consumeToken();
1979 
1980   // Check whether we already have an umbrella.
1981   if (ActiveModule->Umbrella) {
1982     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1983       << ActiveModule->getFullModuleName();
1984     HadError = true;
1985     return;
1986   }
1987 
1988   // Look for this file.
1989   const DirectoryEntry *Dir = nullptr;
1990   if (llvm::sys::path::is_absolute(DirName))
1991     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1992   else {
1993     SmallString<128> PathName;
1994     PathName = Directory->getName();
1995     llvm::sys::path::append(PathName, DirName);
1996     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1997   }
1998 
1999   if (!Dir) {
2000     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
2001       << DirName;
2002     HadError = true;
2003     return;
2004   }
2005 
2006   if (UsesRequiresExcludedHack.count(ActiveModule)) {
2007     // Mark this header 'textual' (see doc comment for
2008     // ModuleMapParser::UsesRequiresExcludedHack). Although iterating over the
2009     // directory is relatively expensive, in practice this only applies to the
2010     // uncommonly used Tcl module on Darwin platforms.
2011     std::error_code EC;
2012     SmallVector<Module::Header, 6> Headers;
2013     vfs::FileSystem &FS = *SourceMgr.getFileManager().getVirtualFileSystem();
2014     for (vfs::recursive_directory_iterator I(FS, Dir->getName(), EC), E;
2015          I != E && !EC; I.increment(EC)) {
2016       if (const FileEntry *FE =
2017               SourceMgr.getFileManager().getFile(I->getName())) {
2018 
2019         Module::Header Header = {I->getName(), FE};
2020         Headers.push_back(std::move(Header));
2021       }
2022     }
2023 
2024     // Sort header paths so that the pcm doesn't depend on iteration order.
2025     llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
2026 
2027     for (auto &Header : Headers)
2028       Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);
2029     return;
2030   }
2031 
2032   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
2033     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
2034       << OwningModule->getFullModuleName();
2035     HadError = true;
2036     return;
2037   }
2038 
2039   // Record this umbrella directory.
2040   Map.setUmbrellaDir(ActiveModule, Dir, DirName);
2041 }
2042 
2043 /// \brief Parse a module export declaration.
2044 ///
2045 ///   export-declaration:
2046 ///     'export' wildcard-module-id
2047 ///
2048 ///   wildcard-module-id:
2049 ///     identifier
2050 ///     '*'
2051 ///     identifier '.' wildcard-module-id
2052 void ModuleMapParser::parseExportDecl() {
2053   assert(Tok.is(MMToken::ExportKeyword));
2054   SourceLocation ExportLoc = consumeToken();
2055 
2056   // Parse the module-id with an optional wildcard at the end.
2057   ModuleId ParsedModuleId;
2058   bool Wildcard = false;
2059   do {
2060     // FIXME: Support string-literal module names here.
2061     if (Tok.is(MMToken::Identifier)) {
2062       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
2063                                               Tok.getLocation()));
2064       consumeToken();
2065 
2066       if (Tok.is(MMToken::Period)) {
2067         consumeToken();
2068         continue;
2069       }
2070 
2071       break;
2072     }
2073 
2074     if(Tok.is(MMToken::Star)) {
2075       Wildcard = true;
2076       consumeToken();
2077       break;
2078     }
2079 
2080     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
2081     HadError = true;
2082     return;
2083   } while (true);
2084 
2085   Module::UnresolvedExportDecl Unresolved = {
2086     ExportLoc, ParsedModuleId, Wildcard
2087   };
2088   ActiveModule->UnresolvedExports.push_back(Unresolved);
2089 }
2090 
2091 /// \brief Parse a module use declaration.
2092 ///
2093 ///   use-declaration:
2094 ///     'use' wildcard-module-id
2095 void ModuleMapParser::parseUseDecl() {
2096   assert(Tok.is(MMToken::UseKeyword));
2097   auto KWLoc = consumeToken();
2098   // Parse the module-id.
2099   ModuleId ParsedModuleId;
2100   parseModuleId(ParsedModuleId);
2101 
2102   if (ActiveModule->Parent)
2103     Diags.Report(KWLoc, diag::err_mmap_use_decl_submodule);
2104   else
2105     ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
2106 }
2107 
2108 /// \brief Parse a link declaration.
2109 ///
2110 ///   module-declaration:
2111 ///     'link' 'framework'[opt] string-literal
2112 void ModuleMapParser::parseLinkDecl() {
2113   assert(Tok.is(MMToken::LinkKeyword));
2114   SourceLocation LinkLoc = consumeToken();
2115 
2116   // Parse the optional 'framework' keyword.
2117   bool IsFramework = false;
2118   if (Tok.is(MMToken::FrameworkKeyword)) {
2119     consumeToken();
2120     IsFramework = true;
2121   }
2122 
2123   // Parse the library name
2124   if (!Tok.is(MMToken::StringLiteral)) {
2125     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
2126       << IsFramework << SourceRange(LinkLoc);
2127     HadError = true;
2128     return;
2129   }
2130 
2131   std::string LibraryName = Tok.getString();
2132   consumeToken();
2133   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
2134                                                             IsFramework));
2135 }
2136 
2137 /// \brief Parse a configuration macro declaration.
2138 ///
2139 ///   module-declaration:
2140 ///     'config_macros' attributes[opt] config-macro-list?
2141 ///
2142 ///   config-macro-list:
2143 ///     identifier (',' identifier)?
2144 void ModuleMapParser::parseConfigMacros() {
2145   assert(Tok.is(MMToken::ConfigMacros));
2146   SourceLocation ConfigMacrosLoc = consumeToken();
2147 
2148   // Only top-level modules can have configuration macros.
2149   if (ActiveModule->Parent) {
2150     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
2151   }
2152 
2153   // Parse the optional attributes.
2154   Attributes Attrs;
2155   if (parseOptionalAttributes(Attrs))
2156     return;
2157 
2158   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
2159     ActiveModule->ConfigMacrosExhaustive = true;
2160   }
2161 
2162   // If we don't have an identifier, we're done.
2163   // FIXME: Support macros with the same name as a keyword here.
2164   if (!Tok.is(MMToken::Identifier))
2165     return;
2166 
2167   // Consume the first identifier.
2168   if (!ActiveModule->Parent) {
2169     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2170   }
2171   consumeToken();
2172 
2173   do {
2174     // If there's a comma, consume it.
2175     if (!Tok.is(MMToken::Comma))
2176       break;
2177     consumeToken();
2178 
2179     // We expect to see a macro name here.
2180     // FIXME: Support macros with the same name as a keyword here.
2181     if (!Tok.is(MMToken::Identifier)) {
2182       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
2183       break;
2184     }
2185 
2186     // Consume the macro name.
2187     if (!ActiveModule->Parent) {
2188       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
2189     }
2190     consumeToken();
2191   } while (true);
2192 }
2193 
2194 /// \brief Format a module-id into a string.
2195 static std::string formatModuleId(const ModuleId &Id) {
2196   std::string result;
2197   {
2198     llvm::raw_string_ostream OS(result);
2199 
2200     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
2201       if (I)
2202         OS << ".";
2203       OS << Id[I].first;
2204     }
2205   }
2206 
2207   return result;
2208 }
2209 
2210 /// \brief Parse a conflict declaration.
2211 ///
2212 ///   module-declaration:
2213 ///     'conflict' module-id ',' string-literal
2214 void ModuleMapParser::parseConflict() {
2215   assert(Tok.is(MMToken::Conflict));
2216   SourceLocation ConflictLoc = consumeToken();
2217   Module::UnresolvedConflict Conflict;
2218 
2219   // Parse the module-id.
2220   if (parseModuleId(Conflict.Id))
2221     return;
2222 
2223   // Parse the ','.
2224   if (!Tok.is(MMToken::Comma)) {
2225     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
2226       << SourceRange(ConflictLoc);
2227     return;
2228   }
2229   consumeToken();
2230 
2231   // Parse the message.
2232   if (!Tok.is(MMToken::StringLiteral)) {
2233     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
2234       << formatModuleId(Conflict.Id);
2235     return;
2236   }
2237   Conflict.Message = Tok.getString().str();
2238   consumeToken();
2239 
2240   // Add this unresolved conflict.
2241   ActiveModule->UnresolvedConflicts.push_back(Conflict);
2242 }
2243 
2244 /// \brief Parse an inferred module declaration (wildcard modules).
2245 ///
2246 ///   module-declaration:
2247 ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
2248 ///       { inferred-module-member* }
2249 ///
2250 ///   inferred-module-member:
2251 ///     'export' '*'
2252 ///     'exclude' identifier
2253 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
2254   assert(Tok.is(MMToken::Star));
2255   SourceLocation StarLoc = consumeToken();
2256   bool Failed = false;
2257 
2258   // Inferred modules must be submodules.
2259   if (!ActiveModule && !Framework) {
2260     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
2261     Failed = true;
2262   }
2263 
2264   if (ActiveModule) {
2265     // Inferred modules must have umbrella directories.
2266     if (!Failed && ActiveModule->IsAvailable &&
2267         !ActiveModule->getUmbrellaDir()) {
2268       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
2269       Failed = true;
2270     }
2271 
2272     // Check for redefinition of an inferred module.
2273     if (!Failed && ActiveModule->InferSubmodules) {
2274       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
2275       if (ActiveModule->InferredSubmoduleLoc.isValid())
2276         Diags.Report(ActiveModule->InferredSubmoduleLoc,
2277                      diag::note_mmap_prev_definition);
2278       Failed = true;
2279     }
2280 
2281     // Check for the 'framework' keyword, which is not permitted here.
2282     if (Framework) {
2283       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
2284       Framework = false;
2285     }
2286   } else if (Explicit) {
2287     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
2288     Explicit = false;
2289   }
2290 
2291   // If there were any problems with this inferred submodule, skip its body.
2292   if (Failed) {
2293     if (Tok.is(MMToken::LBrace)) {
2294       consumeToken();
2295       skipUntil(MMToken::RBrace);
2296       if (Tok.is(MMToken::RBrace))
2297         consumeToken();
2298     }
2299     HadError = true;
2300     return;
2301   }
2302 
2303   // Parse optional attributes.
2304   Attributes Attrs;
2305   if (parseOptionalAttributes(Attrs))
2306     return;
2307 
2308   if (ActiveModule) {
2309     // Note that we have an inferred submodule.
2310     ActiveModule->InferSubmodules = true;
2311     ActiveModule->InferredSubmoduleLoc = StarLoc;
2312     ActiveModule->InferExplicitSubmodules = Explicit;
2313   } else {
2314     // We'll be inferring framework modules for this directory.
2315     Map.InferredDirectories[Directory].InferModules = true;
2316     Map.InferredDirectories[Directory].Attrs = Attrs;
2317     Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
2318     // FIXME: Handle the 'framework' keyword.
2319   }
2320 
2321   // Parse the opening brace.
2322   if (!Tok.is(MMToken::LBrace)) {
2323     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
2324     HadError = true;
2325     return;
2326   }
2327   SourceLocation LBraceLoc = consumeToken();
2328 
2329   // Parse the body of the inferred submodule.
2330   bool Done = false;
2331   do {
2332     switch (Tok.Kind) {
2333     case MMToken::EndOfFile:
2334     case MMToken::RBrace:
2335       Done = true;
2336       break;
2337 
2338     case MMToken::ExcludeKeyword: {
2339       if (ActiveModule) {
2340         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2341           << (ActiveModule != nullptr);
2342         consumeToken();
2343         break;
2344       }
2345 
2346       consumeToken();
2347       // FIXME: Support string-literal module names here.
2348       if (!Tok.is(MMToken::Identifier)) {
2349         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
2350         break;
2351       }
2352 
2353       Map.InferredDirectories[Directory].ExcludedModules
2354         .push_back(Tok.getString());
2355       consumeToken();
2356       break;
2357     }
2358 
2359     case MMToken::ExportKeyword:
2360       if (!ActiveModule) {
2361         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2362           << (ActiveModule != nullptr);
2363         consumeToken();
2364         break;
2365       }
2366 
2367       consumeToken();
2368       if (Tok.is(MMToken::Star))
2369         ActiveModule->InferExportWildcard = true;
2370       else
2371         Diags.Report(Tok.getLocation(),
2372                      diag::err_mmap_expected_export_wildcard);
2373       consumeToken();
2374       break;
2375 
2376     case MMToken::ExplicitKeyword:
2377     case MMToken::ModuleKeyword:
2378     case MMToken::HeaderKeyword:
2379     case MMToken::PrivateKeyword:
2380     case MMToken::UmbrellaKeyword:
2381     default:
2382       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
2383           << (ActiveModule != nullptr);
2384       consumeToken();
2385       break;
2386     }
2387   } while (!Done);
2388 
2389   if (Tok.is(MMToken::RBrace))
2390     consumeToken();
2391   else {
2392     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
2393     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
2394     HadError = true;
2395   }
2396 }
2397 
2398 /// \brief Parse optional attributes.
2399 ///
2400 ///   attributes:
2401 ///     attribute attributes
2402 ///     attribute
2403 ///
2404 ///   attribute:
2405 ///     [ identifier ]
2406 ///
2407 /// \param Attrs Will be filled in with the parsed attributes.
2408 ///
2409 /// \returns true if an error occurred, false otherwise.
2410 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
2411   bool HadError = false;
2412 
2413   while (Tok.is(MMToken::LSquare)) {
2414     // Consume the '['.
2415     SourceLocation LSquareLoc = consumeToken();
2416 
2417     // Check whether we have an attribute name here.
2418     if (!Tok.is(MMToken::Identifier)) {
2419       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
2420       skipUntil(MMToken::RSquare);
2421       if (Tok.is(MMToken::RSquare))
2422         consumeToken();
2423       HadError = true;
2424     }
2425 
2426     // Decode the attribute name.
2427     AttributeKind Attribute
2428       = llvm::StringSwitch<AttributeKind>(Tok.getString())
2429           .Case("exhaustive", AT_exhaustive)
2430           .Case("extern_c", AT_extern_c)
2431           .Case("no_undeclared_includes", AT_no_undeclared_includes)
2432           .Case("system", AT_system)
2433           .Default(AT_unknown);
2434     switch (Attribute) {
2435     case AT_unknown:
2436       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
2437         << Tok.getString();
2438       break;
2439 
2440     case AT_system:
2441       Attrs.IsSystem = true;
2442       break;
2443 
2444     case AT_extern_c:
2445       Attrs.IsExternC = true;
2446       break;
2447 
2448     case AT_exhaustive:
2449       Attrs.IsExhaustive = true;
2450       break;
2451 
2452     case AT_no_undeclared_includes:
2453       Attrs.NoUndeclaredIncludes = true;
2454       break;
2455     }
2456     consumeToken();
2457 
2458     // Consume the ']'.
2459     if (!Tok.is(MMToken::RSquare)) {
2460       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
2461       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
2462       skipUntil(MMToken::RSquare);
2463       HadError = true;
2464     }
2465 
2466     if (Tok.is(MMToken::RSquare))
2467       consumeToken();
2468   }
2469 
2470   return HadError;
2471 }
2472 
2473 /// \brief Parse a module map file.
2474 ///
2475 ///   module-map-file:
2476 ///     module-declaration*
2477 bool ModuleMapParser::parseModuleMapFile() {
2478   do {
2479     switch (Tok.Kind) {
2480     case MMToken::EndOfFile:
2481       return HadError;
2482 
2483     case MMToken::ExplicitKeyword:
2484     case MMToken::ExternKeyword:
2485     case MMToken::ModuleKeyword:
2486     case MMToken::FrameworkKeyword:
2487       parseModuleDecl();
2488       break;
2489 
2490     case MMToken::Comma:
2491     case MMToken::ConfigMacros:
2492     case MMToken::Conflict:
2493     case MMToken::Exclaim:
2494     case MMToken::ExcludeKeyword:
2495     case MMToken::ExportKeyword:
2496     case MMToken::HeaderKeyword:
2497     case MMToken::Identifier:
2498     case MMToken::LBrace:
2499     case MMToken::LinkKeyword:
2500     case MMToken::LSquare:
2501     case MMToken::Period:
2502     case MMToken::PrivateKeyword:
2503     case MMToken::RBrace:
2504     case MMToken::RSquare:
2505     case MMToken::RequiresKeyword:
2506     case MMToken::Star:
2507     case MMToken::StringLiteral:
2508     case MMToken::TextualKeyword:
2509     case MMToken::UmbrellaKeyword:
2510     case MMToken::UseKeyword:
2511       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2512       HadError = true;
2513       consumeToken();
2514       break;
2515     }
2516   } while (true);
2517 }
2518 
2519 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
2520                                    const DirectoryEntry *Dir,
2521                                    SourceLocation ExternModuleLoc) {
2522   llvm::DenseMap<const FileEntry *, bool>::iterator Known
2523     = ParsedModuleMap.find(File);
2524   if (Known != ParsedModuleMap.end())
2525     return Known->second;
2526 
2527   assert(Target && "Missing target information");
2528   auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
2529   FileID ID = SourceMgr.createFileID(File, ExternModuleLoc, FileCharacter);
2530   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2531   if (!Buffer)
2532     return ParsedModuleMap[File] = true;
2533 
2534   // Parse this module map file.
2535   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
2536   SourceLocation Start = L.getSourceLocation();
2537   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
2538                          BuiltinIncludeDir, IsSystem);
2539   bool Result = Parser.parseModuleMapFile();
2540   ParsedModuleMap[File] = Result;
2541 
2542   // Notify callbacks that we parsed it.
2543   for (const auto &Cb : Callbacks)
2544     Cb->moduleMapFileRead(Start, *File, IsSystem);
2545   return Result;
2546 }
2547