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