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