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