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