1718292f2SDouglas Gregor //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
2718292f2SDouglas Gregor //
3718292f2SDouglas Gregor //                     The LLVM Compiler Infrastructure
4718292f2SDouglas Gregor //
5718292f2SDouglas Gregor // This file is distributed under the University of Illinois Open Source
6718292f2SDouglas Gregor // License. See LICENSE.TXT for details.
7718292f2SDouglas Gregor //
8718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
9718292f2SDouglas Gregor //
10718292f2SDouglas Gregor // This file defines the ModuleMap implementation, which describes the layout
11718292f2SDouglas Gregor // of a module as it relates to headers.
12718292f2SDouglas Gregor //
13718292f2SDouglas Gregor //===----------------------------------------------------------------------===//
14718292f2SDouglas Gregor #include "clang/Lex/ModuleMap.h"
15a7d03840SJordan Rose #include "clang/Basic/CharInfo.h"
16718292f2SDouglas Gregor #include "clang/Basic/Diagnostic.h"
17811db4eaSDouglas Gregor #include "clang/Basic/DiagnosticOptions.h"
18718292f2SDouglas Gregor #include "clang/Basic/FileManager.h"
19718292f2SDouglas Gregor #include "clang/Basic/TargetInfo.h"
20718292f2SDouglas Gregor #include "clang/Basic/TargetOptions.h"
21b146baabSArgyrios Kyrtzidis #include "clang/Lex/HeaderSearch.h"
223a02247dSChandler Carruth #include "clang/Lex/LexDiagnostic.h"
233a02247dSChandler Carruth #include "clang/Lex/Lexer.h"
243a02247dSChandler Carruth #include "clang/Lex/LiteralSupport.h"
253a02247dSChandler Carruth #include "llvm/ADT/StringRef.h"
263a02247dSChandler Carruth #include "llvm/ADT/StringSwitch.h"
27718292f2SDouglas Gregor #include "llvm/Support/Allocator.h"
28e89dbc1dSDouglas Gregor #include "llvm/Support/FileSystem.h"
29718292f2SDouglas Gregor #include "llvm/Support/Host.h"
30552c169eSRafael Espindola #include "llvm/Support/Path.h"
31718292f2SDouglas Gregor #include "llvm/Support/raw_ostream.h"
3207c22b78SDouglas Gregor #include <stdlib.h>
3301c7cfa2SDouglas Gregor #if defined(LLVM_ON_UNIX)
34eadae014SDmitri Gribenko #include <limits.h>
3501c7cfa2SDouglas Gregor #endif
36718292f2SDouglas Gregor using namespace clang;
37718292f2SDouglas Gregor 
382b82c2a5SDouglas Gregor Module::ExportDecl
392b82c2a5SDouglas Gregor ModuleMap::resolveExport(Module *Mod,
402b82c2a5SDouglas Gregor                          const Module::UnresolvedExportDecl &Unresolved,
41e4412640SArgyrios Kyrtzidis                          bool Complain) const {
42f5eedd05SDouglas Gregor   // We may have just a wildcard.
43f5eedd05SDouglas Gregor   if (Unresolved.Id.empty()) {
44f5eedd05SDouglas Gregor     assert(Unresolved.Wildcard && "Invalid unresolved export");
45f5eedd05SDouglas Gregor     return Module::ExportDecl(0, true);
46f5eedd05SDouglas Gregor   }
47f5eedd05SDouglas Gregor 
48fb912657SDouglas Gregor   // Resolve the module-id.
49fb912657SDouglas Gregor   Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
50fb912657SDouglas Gregor   if (!Context)
51fb912657SDouglas Gregor     return Module::ExportDecl();
52fb912657SDouglas Gregor 
53fb912657SDouglas Gregor   return Module::ExportDecl(Context, Unresolved.Wildcard);
54fb912657SDouglas Gregor }
55fb912657SDouglas Gregor 
56fb912657SDouglas Gregor Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
57fb912657SDouglas Gregor                                    bool Complain) const {
582b82c2a5SDouglas Gregor   // Find the starting module.
59fb912657SDouglas Gregor   Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
602b82c2a5SDouglas Gregor   if (!Context) {
612b82c2a5SDouglas Gregor     if (Complain)
62*0761a8a0SDaniel Jasper       Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
63fb912657SDouglas Gregor       << Id[0].first << Mod->getFullModuleName();
642b82c2a5SDouglas Gregor 
65fb912657SDouglas Gregor     return 0;
662b82c2a5SDouglas Gregor   }
672b82c2a5SDouglas Gregor 
682b82c2a5SDouglas Gregor   // Dig into the module path.
69fb912657SDouglas Gregor   for (unsigned I = 1, N = Id.size(); I != N; ++I) {
70fb912657SDouglas Gregor     Module *Sub = lookupModuleQualified(Id[I].first, Context);
712b82c2a5SDouglas Gregor     if (!Sub) {
722b82c2a5SDouglas Gregor       if (Complain)
73*0761a8a0SDaniel Jasper         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
74fb912657SDouglas Gregor         << Id[I].first << Context->getFullModuleName()
75fb912657SDouglas Gregor         << SourceRange(Id[0].second, Id[I-1].second);
762b82c2a5SDouglas Gregor 
77fb912657SDouglas Gregor       return 0;
782b82c2a5SDouglas Gregor     }
792b82c2a5SDouglas Gregor 
802b82c2a5SDouglas Gregor     Context = Sub;
812b82c2a5SDouglas Gregor   }
822b82c2a5SDouglas Gregor 
83fb912657SDouglas Gregor   return Context;
842b82c2a5SDouglas Gregor }
852b82c2a5SDouglas Gregor 
86*0761a8a0SDaniel Jasper ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
87b146baabSArgyrios Kyrtzidis                      const LangOptions &LangOpts, const TargetInfo *Target,
88b146baabSArgyrios Kyrtzidis                      HeaderSearch &HeaderInfo)
89*0761a8a0SDaniel Jasper     : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
901f76c4e8SManuel Klimek       HeaderInfo(HeaderInfo), BuiltinIncludeDir(0), CompilingModule(0),
91*0761a8a0SDaniel Jasper       SourceModule(0) {}
92718292f2SDouglas Gregor 
93718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
945acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
955acdf59eSDouglas Gregor                                         IEnd = Modules.end();
965acdf59eSDouglas Gregor        I != IEnd; ++I) {
975acdf59eSDouglas Gregor     delete I->getValue();
985acdf59eSDouglas Gregor   }
99718292f2SDouglas Gregor }
100718292f2SDouglas Gregor 
10189929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) {
10289929282SDouglas Gregor   assert((!this->Target || this->Target == &Target) &&
10389929282SDouglas Gregor          "Improper target override");
10489929282SDouglas Gregor   this->Target = &Target;
10589929282SDouglas Gregor }
10689929282SDouglas Gregor 
107056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier.
108056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
109056396aeSDouglas Gregor                                               SmallVectorImpl<char> &Buffer) {
110056396aeSDouglas Gregor   if (Name.empty())
111056396aeSDouglas Gregor     return Name;
112056396aeSDouglas Gregor 
113a7d03840SJordan Rose   if (!isValidIdentifier(Name)) {
114056396aeSDouglas Gregor     // If we don't already have something with the form of an identifier,
115056396aeSDouglas Gregor     // create a buffer with the sanitized name.
116056396aeSDouglas Gregor     Buffer.clear();
117a7d03840SJordan Rose     if (isDigit(Name[0]))
118056396aeSDouglas Gregor       Buffer.push_back('_');
119056396aeSDouglas Gregor     Buffer.reserve(Buffer.size() + Name.size());
120056396aeSDouglas Gregor     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
121a7d03840SJordan Rose       if (isIdentifierBody(Name[I]))
122056396aeSDouglas Gregor         Buffer.push_back(Name[I]);
123056396aeSDouglas Gregor       else
124056396aeSDouglas Gregor         Buffer.push_back('_');
125056396aeSDouglas Gregor     }
126056396aeSDouglas Gregor 
127056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
128056396aeSDouglas Gregor   }
129056396aeSDouglas Gregor 
130056396aeSDouglas Gregor   while (llvm::StringSwitch<bool>(Name)
131056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
132056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
133056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def"
134056396aeSDouglas Gregor            .Default(false)) {
135056396aeSDouglas Gregor     if (Name.data() != Buffer.data())
136056396aeSDouglas Gregor       Buffer.append(Name.begin(), Name.end());
137056396aeSDouglas Gregor     Buffer.push_back('_');
138056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
139056396aeSDouglas Gregor   }
140056396aeSDouglas Gregor 
141056396aeSDouglas Gregor   return Name;
142056396aeSDouglas Gregor }
143056396aeSDouglas Gregor 
14434d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin
14534d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system
14634d52749SDouglas Gregor /// headers.
14734d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) {
14834d52749SDouglas Gregor   return llvm::StringSwitch<bool>(FileName)
14934d52749SDouglas Gregor            .Case("float.h", true)
15034d52749SDouglas Gregor            .Case("iso646.h", true)
15134d52749SDouglas Gregor            .Case("limits.h", true)
15234d52749SDouglas Gregor            .Case("stdalign.h", true)
15334d52749SDouglas Gregor            .Case("stdarg.h", true)
15434d52749SDouglas Gregor            .Case("stdbool.h", true)
15534d52749SDouglas Gregor            .Case("stddef.h", true)
15634d52749SDouglas Gregor            .Case("stdint.h", true)
15734d52749SDouglas Gregor            .Case("tgmath.h", true)
15834d52749SDouglas Gregor            .Case("unwind.h", true)
15934d52749SDouglas Gregor            .Default(false);
16034d52749SDouglas Gregor }
16134d52749SDouglas Gregor 
16297da9178SDaniel Jasper ModuleMap::KnownHeader
16397da9178SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File,
1644eaf0a6cSDaniel Jasper                                Module *RequestingModule,
1654eaf0a6cSDaniel Jasper                                bool *FoundInModule) {
16659527666SDouglas Gregor   HeadersMap::iterator Known = Headers.find(File);
1674eaf0a6cSDaniel Jasper 
1684eaf0a6cSDaniel Jasper   // If we've found a builtin header within Clang's builtin include directory,
1694eaf0a6cSDaniel Jasper   // load all of the module maps to see if it will get associated with a
1704eaf0a6cSDaniel Jasper   // specific module (e.g., in /usr/include).
1714eaf0a6cSDaniel Jasper   if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
1724eaf0a6cSDaniel Jasper       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
1734eaf0a6cSDaniel Jasper     HeaderInfo.loadTopLevelSystemModules();
1744eaf0a6cSDaniel Jasper     Known = Headers.find(File);
1754eaf0a6cSDaniel Jasper   }
1764eaf0a6cSDaniel Jasper 
1771fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
17897da9178SDaniel Jasper     ModuleMap::KnownHeader Result = KnownHeader();
1791fb5c3a6SDouglas Gregor 
18097da9178SDaniel Jasper     // Iterate over all modules that 'File' is part of to find the best fit.
18197da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
18297da9178SDaniel Jasper                                                 E = Known->second.end();
18397da9178SDaniel Jasper          I != E; ++I) {
1844eaf0a6cSDaniel Jasper       // Cannot use a module if the header is excluded in it.
1854eaf0a6cSDaniel Jasper       if (I->getRole() == ModuleMap::ExcludedHeader)
1864eaf0a6cSDaniel Jasper         continue;
1874eaf0a6cSDaniel Jasper 
1884eaf0a6cSDaniel Jasper       if (FoundInModule)
1894eaf0a6cSDaniel Jasper         *FoundInModule = true;
1904eaf0a6cSDaniel Jasper 
1914eaf0a6cSDaniel Jasper       // Cannot use a module if it is unavailable.
1924eaf0a6cSDaniel Jasper       if (!I->getModule()->isAvailable())
19397da9178SDaniel Jasper         continue;
19497da9178SDaniel Jasper 
19597da9178SDaniel Jasper       // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
19697da9178SDaniel Jasper       // module we are looking for.
19797da9178SDaniel Jasper       if (I->getModule() == RequestingModule)
19897da9178SDaniel Jasper         return *I;
19997da9178SDaniel Jasper 
20097da9178SDaniel Jasper       // If uses need to be specified explicitly, we are only allowed to return
20197da9178SDaniel Jasper       // modules that are explicitly used by the requesting module.
20297da9178SDaniel Jasper       if (RequestingModule && LangOpts.ModulesDeclUse &&
20397da9178SDaniel Jasper           std::find(RequestingModule->DirectUses.begin(),
20497da9178SDaniel Jasper                     RequestingModule->DirectUses.end(),
20597da9178SDaniel Jasper                     I->getModule()) == RequestingModule->DirectUses.end())
20697da9178SDaniel Jasper         continue;
2074eaf0a6cSDaniel Jasper 
20897da9178SDaniel Jasper       Result = *I;
20997da9178SDaniel Jasper       // If 'File' is a public header of this module, this is as good as we
21097da9178SDaniel Jasper       // are going to get.
21197da9178SDaniel Jasper       if (I->getRole() == ModuleMap::NormalHeader)
21297da9178SDaniel Jasper         break;
21397da9178SDaniel Jasper     }
21497da9178SDaniel Jasper     return Result;
2151fb5c3a6SDouglas Gregor   }
216ab0c8a84SDouglas Gregor 
217b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
218f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
219e00c8b20SDouglas Gregor 
22074260502SDouglas Gregor   // Note: as an egregious but useful hack we use the real path here, because
22174260502SDouglas Gregor   // frameworks moving from top-level frameworks to embedded frameworks tend
22274260502SDouglas Gregor   // to be symlinked from the top-level location to the embedded location,
22374260502SDouglas Gregor   // and we need to resolve lookups as if we had found the embedded location.
2241f76c4e8SManuel Klimek   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
225a89c5ac4SDouglas Gregor 
226a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
227a89c5ac4SDouglas Gregor   // an umbrella header.
228b65dbfffSDouglas Gregor   do {
229a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
230a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
231a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
232a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
233930a85ccSDouglas Gregor 
234930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
23573141fa9SDouglas Gregor       // directory.
236930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
23773141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
238930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
239930a85ccSDouglas Gregor 
240930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
241a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
242a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
243a89c5ac4SDouglas Gregor         // the actual header is located.
2449458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
2459458f82dSDouglas Gregor 
2467033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
247a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
248056396aeSDouglas Gregor           SmallString<32> NameBuf;
249056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
250056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
251056396aeSDouglas Gregor                              NameBuf);
252a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2539458f82dSDouglas Gregor                                       Explicit).first;
254a89c5ac4SDouglas Gregor 
255a89c5ac4SDouglas Gregor           // Associate the module and the directory.
256a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
257a89c5ac4SDouglas Gregor 
258a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
259a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
260930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
261a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
262a89c5ac4SDouglas Gregor         }
263a89c5ac4SDouglas Gregor 
264a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
265056396aeSDouglas Gregor         SmallString<32> NameBuf;
266056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
267056396aeSDouglas Gregor                            llvm::sys::path::stem(File->getName()), NameBuf);
268a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2699458f82dSDouglas Gregor                                     Explicit).first;
2703c5305c1SArgyrios Kyrtzidis         Result->addTopHeader(File);
271a89c5ac4SDouglas Gregor 
272a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
273a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
274930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
275a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
276a89c5ac4SDouglas Gregor       } else {
277a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
278a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
279a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
280a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
281a89c5ac4SDouglas Gregor       }
282a89c5ac4SDouglas Gregor 
28397da9178SDaniel Jasper       Headers[File].push_back(KnownHeader(Result, NormalHeader));
2841fb5c3a6SDouglas Gregor 
2851fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
2861fb5c3a6SDouglas Gregor       // that it maps to anything.
2871fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
288b53e5483SLawrence Crowl         return KnownHeader();
2891fb5c3a6SDouglas Gregor 
29097da9178SDaniel Jasper       return Headers[File].back();
291a89c5ac4SDouglas Gregor     }
292a89c5ac4SDouglas Gregor 
293a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
294a89c5ac4SDouglas Gregor 
295b65dbfffSDouglas Gregor     // Retrieve our parent path.
296b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
297b65dbfffSDouglas Gregor     if (DirName.empty())
298b65dbfffSDouglas Gregor       break;
299b65dbfffSDouglas Gregor 
300b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
3011f76c4e8SManuel Klimek     Dir = SourceMgr.getFileManager().getDirectory(DirName);
302a89c5ac4SDouglas Gregor   } while (Dir);
303b65dbfffSDouglas Gregor 
304b53e5483SLawrence Crowl   return KnownHeader();
305ab0c8a84SDouglas Gregor }
306ab0c8a84SDouglas Gregor 
307e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
308e4412640SArgyrios Kyrtzidis   HeadersMap::const_iterator Known = Headers.find(Header);
30997da9178SDaniel Jasper   if (Known != Headers.end()) {
31097da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator
31197da9178SDaniel Jasper              I = Known->second.begin(),
31297da9178SDaniel Jasper              E = Known->second.end();
31397da9178SDaniel Jasper          I != E; ++I) {
31497da9178SDaniel Jasper       if (I->isAvailable())
31597da9178SDaniel Jasper         return false;
31697da9178SDaniel Jasper     }
31797da9178SDaniel Jasper     return true;
31897da9178SDaniel Jasper   }
3191fb5c3a6SDouglas Gregor 
3201fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
321f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
3221fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
3231fb5c3a6SDouglas Gregor 
3241fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
3251fb5c3a6SDouglas Gregor   // an umbrella header.
3261fb5c3a6SDouglas Gregor   do {
327e4412640SArgyrios Kyrtzidis     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
3281fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
3291fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
3301fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
3311fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
3321fb5c3a6SDouglas Gregor         return true;
3331fb5c3a6SDouglas Gregor 
3341fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
3351fb5c3a6SDouglas Gregor       // directory.
3361fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
3371fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
3381fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
3391fb5c3a6SDouglas Gregor 
3401fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
3411fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
3421fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
343056396aeSDouglas Gregor           SmallString<32> NameBuf;
344056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
345056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
346056396aeSDouglas Gregor                              NameBuf);
3471fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
3481fb5c3a6SDouglas Gregor           if (!Found)
3491fb5c3a6SDouglas Gregor             return false;
3501fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
3511fb5c3a6SDouglas Gregor             return true;
3521fb5c3a6SDouglas Gregor         }
3531fb5c3a6SDouglas Gregor 
3541fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
355056396aeSDouglas Gregor         SmallString<32> NameBuf;
356056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
357056396aeSDouglas Gregor                            llvm::sys::path::stem(Header->getName()),
358056396aeSDouglas Gregor                            NameBuf);
3591fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
3601fb5c3a6SDouglas Gregor         if (!Found)
3611fb5c3a6SDouglas Gregor           return false;
3621fb5c3a6SDouglas Gregor       }
3631fb5c3a6SDouglas Gregor 
3641fb5c3a6SDouglas Gregor       return !Found->isAvailable();
3651fb5c3a6SDouglas Gregor     }
3661fb5c3a6SDouglas Gregor 
3671fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
3681fb5c3a6SDouglas Gregor 
3691fb5c3a6SDouglas Gregor     // Retrieve our parent path.
3701fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
3711fb5c3a6SDouglas Gregor     if (DirName.empty())
3721fb5c3a6SDouglas Gregor       break;
3731fb5c3a6SDouglas Gregor 
3741fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
3751f76c4e8SManuel Klimek     Dir = SourceMgr.getFileManager().getDirectory(DirName);
3761fb5c3a6SDouglas Gregor   } while (Dir);
3771fb5c3a6SDouglas Gregor 
3781fb5c3a6SDouglas Gregor   return false;
3791fb5c3a6SDouglas Gregor }
3801fb5c3a6SDouglas Gregor 
381e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const {
382e4412640SArgyrios Kyrtzidis   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
38388bdfb0eSDouglas Gregor   if (Known != Modules.end())
38488bdfb0eSDouglas Gregor     return Known->getValue();
38588bdfb0eSDouglas Gregor 
38688bdfb0eSDouglas Gregor   return 0;
38788bdfb0eSDouglas Gregor }
38888bdfb0eSDouglas Gregor 
389e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
390e4412640SArgyrios Kyrtzidis                                            Module *Context) const {
3912b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
3922b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
3932b82c2a5SDouglas Gregor       return Sub;
3942b82c2a5SDouglas Gregor   }
3952b82c2a5SDouglas Gregor 
3962b82c2a5SDouglas Gregor   return findModule(Name);
3972b82c2a5SDouglas Gregor }
3982b82c2a5SDouglas Gregor 
399e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
4002b82c2a5SDouglas Gregor   if (!Context)
4012b82c2a5SDouglas Gregor     return findModule(Name);
4022b82c2a5SDouglas Gregor 
403eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
4042b82c2a5SDouglas Gregor }
4052b82c2a5SDouglas Gregor 
406de3ef502SDouglas Gregor std::pair<Module *, bool>
40769021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
40869021974SDouglas Gregor                               bool IsExplicit) {
40969021974SDouglas Gregor   // Try to find an existing module with this name.
410eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
411eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
41269021974SDouglas Gregor 
41369021974SDouglas Gregor   // Create a new module with this name.
41469021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
41569021974SDouglas Gregor                               IsExplicit);
416ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == Name) {
417ba7f2f71SDaniel Jasper     SourceModule = Result;
418ba7f2f71SDaniel Jasper     SourceModuleName = Name;
419ba7f2f71SDaniel Jasper   }
4206f722b4eSArgyrios Kyrtzidis   if (!Parent) {
42169021974SDouglas Gregor     Modules[Name] = Result;
4226f722b4eSArgyrios Kyrtzidis     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
4236f722b4eSArgyrios Kyrtzidis         Name == LangOpts.CurrentModule) {
4246f722b4eSArgyrios Kyrtzidis       CompilingModule = Result;
4256f722b4eSArgyrios Kyrtzidis     }
4266f722b4eSArgyrios Kyrtzidis   }
42769021974SDouglas Gregor   return std::make_pair(Result, true);
42869021974SDouglas Gregor }
42969021974SDouglas Gregor 
4309194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
431e4412640SArgyrios Kyrtzidis                                         StringRef Name, bool &IsSystem) const {
4329194a91dSDouglas Gregor   // Check whether we have already looked into the parent directory
4339194a91dSDouglas Gregor   // for a module map.
434e4412640SArgyrios Kyrtzidis   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4359194a91dSDouglas Gregor     inferred = InferredDirectories.find(ParentDir);
4369194a91dSDouglas Gregor   if (inferred == InferredDirectories.end())
4379194a91dSDouglas Gregor     return false;
4389194a91dSDouglas Gregor 
4399194a91dSDouglas Gregor   if (!inferred->second.InferModules)
4409194a91dSDouglas Gregor     return false;
4419194a91dSDouglas Gregor 
4429194a91dSDouglas Gregor   // We're allowed to infer for this directory, but make sure it's okay
4439194a91dSDouglas Gregor   // to infer this particular module.
4449194a91dSDouglas Gregor   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
4459194a91dSDouglas Gregor                             inferred->second.ExcludedModules.end(),
4469194a91dSDouglas Gregor                             Name) == inferred->second.ExcludedModules.end();
4479194a91dSDouglas Gregor 
4489194a91dSDouglas Gregor   if (canInfer && inferred->second.InferSystemModules)
4499194a91dSDouglas Gregor     IsSystem = true;
4509194a91dSDouglas Gregor 
4519194a91dSDouglas Gregor   return canInfer;
4529194a91dSDouglas Gregor }
4539194a91dSDouglas Gregor 
45411dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we
45511dfe6feSDouglas Gregor /// should link.
45611dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
45711dfe6feSDouglas Gregor                                FileManager &FileMgr) {
45811dfe6feSDouglas Gregor   assert(Mod->IsFramework && "Can only infer linking for framework modules");
45911dfe6feSDouglas Gregor   assert(!Mod->isSubFramework() &&
46011dfe6feSDouglas Gregor          "Can only infer linking for top-level frameworks");
46111dfe6feSDouglas Gregor 
46211dfe6feSDouglas Gregor   SmallString<128> LibName;
46311dfe6feSDouglas Gregor   LibName += FrameworkDir->getName();
46411dfe6feSDouglas Gregor   llvm::sys::path::append(LibName, Mod->Name);
46511dfe6feSDouglas Gregor   if (FileMgr.getFile(LibName)) {
46611dfe6feSDouglas Gregor     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
46711dfe6feSDouglas Gregor                                                      /*IsFramework=*/true));
46811dfe6feSDouglas Gregor   }
46911dfe6feSDouglas Gregor }
47011dfe6feSDouglas Gregor 
471de3ef502SDouglas Gregor Module *
47256c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
473e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
474a686e1b0SDouglas Gregor                                 bool IsSystem,
475e89dbc1dSDouglas Gregor                                 Module *Parent) {
47656c64013SDouglas Gregor   // Check whether we've already found this module.
477e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
478e89dbc1dSDouglas Gregor     return Mod;
479e89dbc1dSDouglas Gregor 
4801f76c4e8SManuel Klimek   FileManager &FileMgr = SourceMgr.getFileManager();
48156c64013SDouglas Gregor 
4829194a91dSDouglas Gregor   // If the framework has a parent path from which we're allowed to infer
4839194a91dSDouglas Gregor   // a framework module, do so.
4849194a91dSDouglas Gregor   if (!Parent) {
4854ddf2221SDouglas Gregor     // Determine whether we're allowed to infer a module map.
486e00c8b20SDouglas Gregor 
4874ddf2221SDouglas Gregor     // Note: as an egregious but useful hack we use the real path here, because
4884ddf2221SDouglas Gregor     // we might be looking at an embedded framework that symlinks out to a
4894ddf2221SDouglas Gregor     // top-level framework, and we need to infer as if we were naming the
4904ddf2221SDouglas Gregor     // top-level framework.
491e00c8b20SDouglas Gregor     StringRef FrameworkDirName
4921f76c4e8SManuel Klimek       = SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
4934ddf2221SDouglas Gregor 
4949194a91dSDouglas Gregor     bool canInfer = false;
4954ddf2221SDouglas Gregor     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
4969194a91dSDouglas Gregor       // Figure out the parent path.
4974ddf2221SDouglas Gregor       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
4989194a91dSDouglas Gregor       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
4999194a91dSDouglas Gregor         // Check whether we have already looked into the parent directory
5009194a91dSDouglas Gregor         // for a module map.
501e4412640SArgyrios Kyrtzidis         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
5029194a91dSDouglas Gregor           inferred = InferredDirectories.find(ParentDir);
5039194a91dSDouglas Gregor         if (inferred == InferredDirectories.end()) {
5049194a91dSDouglas Gregor           // We haven't looked here before. Load a module map, if there is
5059194a91dSDouglas Gregor           // one.
5069194a91dSDouglas Gregor           SmallString<128> ModMapPath = Parent;
5079194a91dSDouglas Gregor           llvm::sys::path::append(ModMapPath, "module.map");
5089194a91dSDouglas Gregor           if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
509963c5535SDouglas Gregor             parseModuleMapFile(ModMapFile, IsSystem);
5109194a91dSDouglas Gregor             inferred = InferredDirectories.find(ParentDir);
5119194a91dSDouglas Gregor           }
5129194a91dSDouglas Gregor 
5139194a91dSDouglas Gregor           if (inferred == InferredDirectories.end())
5149194a91dSDouglas Gregor             inferred = InferredDirectories.insert(
5159194a91dSDouglas Gregor                          std::make_pair(ParentDir, InferredDirectory())).first;
5169194a91dSDouglas Gregor         }
5179194a91dSDouglas Gregor 
5189194a91dSDouglas Gregor         if (inferred->second.InferModules) {
5199194a91dSDouglas Gregor           // We're allowed to infer for this directory, but make sure it's okay
5209194a91dSDouglas Gregor           // to infer this particular module.
5214ddf2221SDouglas Gregor           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
5229194a91dSDouglas Gregor           canInfer = std::find(inferred->second.ExcludedModules.begin(),
5239194a91dSDouglas Gregor                                inferred->second.ExcludedModules.end(),
5249194a91dSDouglas Gregor                                Name) == inferred->second.ExcludedModules.end();
5259194a91dSDouglas Gregor 
5269194a91dSDouglas Gregor           if (inferred->second.InferSystemModules)
5279194a91dSDouglas Gregor             IsSystem = true;
5289194a91dSDouglas Gregor         }
5299194a91dSDouglas Gregor       }
5309194a91dSDouglas Gregor     }
5319194a91dSDouglas Gregor 
5329194a91dSDouglas Gregor     // If we're not allowed to infer a framework module, don't.
5339194a91dSDouglas Gregor     if (!canInfer)
5349194a91dSDouglas Gregor       return 0;
5359194a91dSDouglas Gregor   }
5369194a91dSDouglas Gregor 
5379194a91dSDouglas Gregor 
53856c64013SDouglas Gregor   // Look for an umbrella header.
5392c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
54017381a06SBenjamin Kramer   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
541e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
54256c64013SDouglas Gregor 
54356c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
54456c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
54556c64013SDouglas Gregor   // idea.
54656c64013SDouglas Gregor   if (!UmbrellaHeader)
54756c64013SDouglas Gregor     return 0;
54856c64013SDouglas Gregor 
549e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
550e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
551ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == ModuleName) {
552ba7f2f71SDaniel Jasper     SourceModule = Result;
553ba7f2f71SDaniel Jasper     SourceModuleName = ModuleName;
554ba7f2f71SDaniel Jasper   }
555a686e1b0SDouglas Gregor   if (IsSystem)
556a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
557a686e1b0SDouglas Gregor 
558eb90e830SDouglas Gregor   if (!Parent)
559e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
560e89dbc1dSDouglas Gregor 
561322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
56273141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
56397da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
5644dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
565d8bd7537SDouglas Gregor 
566d8bd7537SDouglas Gregor   // export *
567d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
568d8bd7537SDouglas Gregor 
569a89c5ac4SDouglas Gregor   // module * { export * }
570a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
571a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
572a89c5ac4SDouglas Gregor 
573e89dbc1dSDouglas Gregor   // Look for subframeworks.
574e89dbc1dSDouglas Gregor   llvm::error_code EC;
5752c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
576ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
577e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
5782d4d8cb3SBenjamin Kramer   llvm::sys::path::native(SubframeworksDirName);
579ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
5802d4d8cb3SBenjamin Kramer          Dir(SubframeworksDirName.str(), EC), DirEnd;
581e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
582e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
583e89dbc1dSDouglas Gregor       continue;
584f2161a70SDouglas Gregor 
585e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
586e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
58707c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
58807c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
58907c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
59007c22b78SDouglas Gregor       // out to a top-level framework.
591e00c8b20SDouglas Gregor       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
59207c22b78SDouglas Gregor       bool FoundParent = false;
59307c22b78SDouglas Gregor       do {
59407c22b78SDouglas Gregor         // Get the parent directory name.
59507c22b78SDouglas Gregor         SubframeworkDirName
59607c22b78SDouglas Gregor           = llvm::sys::path::parent_path(SubframeworkDirName);
59707c22b78SDouglas Gregor         if (SubframeworkDirName.empty())
59807c22b78SDouglas Gregor           break;
59907c22b78SDouglas Gregor 
60007c22b78SDouglas Gregor         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
60107c22b78SDouglas Gregor           FoundParent = true;
60207c22b78SDouglas Gregor           break;
60307c22b78SDouglas Gregor         }
60407c22b78SDouglas Gregor       } while (true);
60507c22b78SDouglas Gregor 
60607c22b78SDouglas Gregor       if (!FoundParent)
60707c22b78SDouglas Gregor         continue;
60807c22b78SDouglas Gregor 
609e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
610056396aeSDouglas Gregor       SmallString<32> NameBuf;
611056396aeSDouglas Gregor       inferFrameworkModule(sanitizeFilenameAsIdentifier(
612056396aeSDouglas Gregor                              llvm::sys::path::stem(Dir->path()), NameBuf),
613056396aeSDouglas Gregor                            SubframeworkDir, IsSystem, Result);
614e89dbc1dSDouglas Gregor     }
615e89dbc1dSDouglas Gregor   }
616e89dbc1dSDouglas Gregor 
61711dfe6feSDouglas Gregor   // If the module is a top-level framework, automatically link against the
61811dfe6feSDouglas Gregor   // framework.
61911dfe6feSDouglas Gregor   if (!Result->isSubFramework()) {
62011dfe6feSDouglas Gregor     inferFrameworkLink(Result, FrameworkDir, FileMgr);
62111dfe6feSDouglas Gregor   }
62211dfe6feSDouglas Gregor 
62356c64013SDouglas Gregor   return Result;
62456c64013SDouglas Gregor }
62556c64013SDouglas Gregor 
626a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
62797da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
62873141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
6297033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
630a89c5ac4SDouglas Gregor }
631a89c5ac4SDouglas Gregor 
632524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
633524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
634524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
635524e33e1SDouglas Gregor }
636524e33e1SDouglas Gregor 
63759527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
638b53e5483SLawrence Crowl                           ModuleHeaderRole Role) {
639b53e5483SLawrence Crowl   if (Role == ExcludedHeader) {
64059527666SDouglas Gregor     Mod->ExcludedHeaders.push_back(Header);
641b146baabSArgyrios Kyrtzidis   } else {
642b53e5483SLawrence Crowl     if (Role == PrivateHeader)
643b53e5483SLawrence Crowl       Mod->PrivateHeaders.push_back(Header);
644b53e5483SLawrence Crowl     else
645b53e5483SLawrence Crowl       Mod->NormalHeaders.push_back(Header);
6466f722b4eSArgyrios Kyrtzidis     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
647b53e5483SLawrence Crowl     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
648b146baabSArgyrios Kyrtzidis   }
64997da9178SDaniel Jasper   Headers[Header].push_back(KnownHeader(Mod, Role));
650a89c5ac4SDouglas Gregor }
651a89c5ac4SDouglas Gregor 
652514b636aSDouglas Gregor const FileEntry *
653e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const {
6541f76c4e8SManuel Klimek   if (Module->DefinitionLoc.isInvalid())
655514b636aSDouglas Gregor     return 0;
656514b636aSDouglas Gregor 
6571f76c4e8SManuel Klimek   return SourceMgr.getFileEntryForID(
6581f76c4e8SManuel Klimek            SourceMgr.getFileID(Module->DefinitionLoc));
659514b636aSDouglas Gregor }
660514b636aSDouglas Gregor 
661718292f2SDouglas Gregor void ModuleMap::dump() {
662718292f2SDouglas Gregor   llvm::errs() << "Modules:";
663718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
664718292f2SDouglas Gregor                                         MEnd = Modules.end();
665718292f2SDouglas Gregor        M != MEnd; ++M)
666d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
667718292f2SDouglas Gregor 
668718292f2SDouglas Gregor   llvm::errs() << "Headers:";
66959527666SDouglas Gregor   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
670718292f2SDouglas Gregor        H != HEnd; ++H) {
67197da9178SDaniel Jasper     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
67297da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
67397da9178SDaniel Jasper                                                       E = H->second.end();
67497da9178SDaniel Jasper          I != E; ++I) {
67597da9178SDaniel Jasper       if (I != H->second.begin())
67697da9178SDaniel Jasper         llvm::errs() << ",";
67797da9178SDaniel Jasper       llvm::errs() << I->getModule()->getFullModuleName();
67897da9178SDaniel Jasper     }
67997da9178SDaniel Jasper     llvm::errs() << "\n";
680718292f2SDouglas Gregor   }
681718292f2SDouglas Gregor }
682718292f2SDouglas Gregor 
6832b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
6842b82c2a5SDouglas Gregor   bool HadError = false;
6852b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
6862b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
6872b82c2a5SDouglas Gregor                                               Complain);
688f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
6892b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
6902b82c2a5SDouglas Gregor     else
6912b82c2a5SDouglas Gregor       HadError = true;
6922b82c2a5SDouglas Gregor   }
6932b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
6942b82c2a5SDouglas Gregor   return HadError;
6952b82c2a5SDouglas Gregor }
6962b82c2a5SDouglas Gregor 
697ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
698ba7f2f71SDaniel Jasper   bool HadError = false;
699ba7f2f71SDaniel Jasper   for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) {
700ba7f2f71SDaniel Jasper     Module *DirectUse =
701ba7f2f71SDaniel Jasper         resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain);
702ba7f2f71SDaniel Jasper     if (DirectUse)
703ba7f2f71SDaniel Jasper       Mod->DirectUses.push_back(DirectUse);
704ba7f2f71SDaniel Jasper     else
705ba7f2f71SDaniel Jasper       HadError = true;
706ba7f2f71SDaniel Jasper   }
707ba7f2f71SDaniel Jasper   Mod->UnresolvedDirectUses.clear();
708ba7f2f71SDaniel Jasper   return HadError;
709ba7f2f71SDaniel Jasper }
710ba7f2f71SDaniel Jasper 
711fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
712fb912657SDouglas Gregor   bool HadError = false;
713fb912657SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
714fb912657SDouglas Gregor     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
715fb912657SDouglas Gregor                                        Mod, Complain);
716fb912657SDouglas Gregor     if (!OtherMod) {
717fb912657SDouglas Gregor       HadError = true;
718fb912657SDouglas Gregor       continue;
719fb912657SDouglas Gregor     }
720fb912657SDouglas Gregor 
721fb912657SDouglas Gregor     Module::Conflict Conflict;
722fb912657SDouglas Gregor     Conflict.Other = OtherMod;
723fb912657SDouglas Gregor     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
724fb912657SDouglas Gregor     Mod->Conflicts.push_back(Conflict);
725fb912657SDouglas Gregor   }
726fb912657SDouglas Gregor   Mod->UnresolvedConflicts.clear();
727fb912657SDouglas Gregor   return HadError;
728fb912657SDouglas Gregor }
729fb912657SDouglas Gregor 
7300093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
7310093b3c7SDouglas Gregor   if (Loc.isInvalid())
7320093b3c7SDouglas Gregor     return 0;
7330093b3c7SDouglas Gregor 
7340093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
7350093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
7360093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
7370093b3c7SDouglas Gregor     return 0;
7380093b3c7SDouglas Gregor 
7390093b3c7SDouglas Gregor 
7400093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
7410093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
742224d8a74SDouglas Gregor 
743224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
744224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
745224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
746b53e5483SLawrence Crowl     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
747224d8a74SDouglas Gregor       return Mod;
748224d8a74SDouglas Gregor 
749224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
750224d8a74SDouglas Gregor     // any included header has an associated module.
751224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
752224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
7530093b3c7SDouglas Gregor       return 0;
7540093b3c7SDouglas Gregor 
755224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
756224d8a74SDouglas Gregor   }
757224d8a74SDouglas Gregor 
758224d8a74SDouglas Gregor   return 0;
7590093b3c7SDouglas Gregor }
7600093b3c7SDouglas Gregor 
761718292f2SDouglas Gregor //----------------------------------------------------------------------------//
762718292f2SDouglas Gregor // Module map file parser
763718292f2SDouglas Gregor //----------------------------------------------------------------------------//
764718292f2SDouglas Gregor 
765718292f2SDouglas Gregor namespace clang {
766718292f2SDouglas Gregor   /// \brief A token in a module map file.
767718292f2SDouglas Gregor   struct MMToken {
768718292f2SDouglas Gregor     enum TokenKind {
7691fb5c3a6SDouglas Gregor       Comma,
77035b13eceSDouglas Gregor       ConfigMacros,
771fb912657SDouglas Gregor       Conflict,
772718292f2SDouglas Gregor       EndOfFile,
773718292f2SDouglas Gregor       HeaderKeyword,
774718292f2SDouglas Gregor       Identifier,
775a3feee2aSRichard Smith       Exclaim,
77659527666SDouglas Gregor       ExcludeKeyword,
777718292f2SDouglas Gregor       ExplicitKeyword,
7782b82c2a5SDouglas Gregor       ExportKeyword,
77997292843SDaniel Jasper       ExternKeyword,
780755b2055SDouglas Gregor       FrameworkKeyword,
7816ddfca91SDouglas Gregor       LinkKeyword,
782718292f2SDouglas Gregor       ModuleKeyword,
7832b82c2a5SDouglas Gregor       Period,
784b53e5483SLawrence Crowl       PrivateKeyword,
785718292f2SDouglas Gregor       UmbrellaKeyword,
786ba7f2f71SDaniel Jasper       UseKeyword,
7871fb5c3a6SDouglas Gregor       RequiresKeyword,
7882b82c2a5SDouglas Gregor       Star,
789718292f2SDouglas Gregor       StringLiteral,
790718292f2SDouglas Gregor       LBrace,
791a686e1b0SDouglas Gregor       RBrace,
792a686e1b0SDouglas Gregor       LSquare,
793a686e1b0SDouglas Gregor       RSquare
794718292f2SDouglas Gregor     } Kind;
795718292f2SDouglas Gregor 
796718292f2SDouglas Gregor     unsigned Location;
797718292f2SDouglas Gregor     unsigned StringLength;
798718292f2SDouglas Gregor     const char *StringData;
799718292f2SDouglas Gregor 
800718292f2SDouglas Gregor     void clear() {
801718292f2SDouglas Gregor       Kind = EndOfFile;
802718292f2SDouglas Gregor       Location = 0;
803718292f2SDouglas Gregor       StringLength = 0;
804718292f2SDouglas Gregor       StringData = 0;
805718292f2SDouglas Gregor     }
806718292f2SDouglas Gregor 
807718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
808718292f2SDouglas Gregor 
809718292f2SDouglas Gregor     SourceLocation getLocation() const {
810718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
811718292f2SDouglas Gregor     }
812718292f2SDouglas Gregor 
813718292f2SDouglas Gregor     StringRef getString() const {
814718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
815718292f2SDouglas Gregor     }
816718292f2SDouglas Gregor   };
817718292f2SDouglas Gregor 
8189194a91dSDouglas Gregor   /// \brief The set of attributes that can be attached to a module.
8194442605fSBill Wendling   struct Attributes {
82035b13eceSDouglas Gregor     Attributes() : IsSystem(), IsExhaustive() { }
8219194a91dSDouglas Gregor 
8229194a91dSDouglas Gregor     /// \brief Whether this is a system module.
8239194a91dSDouglas Gregor     unsigned IsSystem : 1;
82435b13eceSDouglas Gregor 
82535b13eceSDouglas Gregor     /// \brief Whether this is an exhaustive set of configuration macros.
82635b13eceSDouglas Gregor     unsigned IsExhaustive : 1;
8279194a91dSDouglas Gregor   };
8289194a91dSDouglas Gregor 
8299194a91dSDouglas Gregor 
830718292f2SDouglas Gregor   class ModuleMapParser {
831718292f2SDouglas Gregor     Lexer &L;
832718292f2SDouglas Gregor     SourceManager &SourceMgr;
833bc10b9fbSDouglas Gregor 
834bc10b9fbSDouglas Gregor     /// \brief Default target information, used only for string literal
835bc10b9fbSDouglas Gregor     /// parsing.
836bc10b9fbSDouglas Gregor     const TargetInfo *Target;
837bc10b9fbSDouglas Gregor 
838718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
839718292f2SDouglas Gregor     ModuleMap &Map;
840718292f2SDouglas Gregor 
8415257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
8425257fc63SDouglas Gregor     const DirectoryEntry *Directory;
8435257fc63SDouglas Gregor 
8443ec6663bSDouglas Gregor     /// \brief The directory containing Clang-supplied headers.
8453ec6663bSDouglas Gregor     const DirectoryEntry *BuiltinIncludeDir;
8463ec6663bSDouglas Gregor 
847963c5535SDouglas Gregor     /// \brief Whether this module map is in a system header directory.
848963c5535SDouglas Gregor     bool IsSystem;
849963c5535SDouglas Gregor 
850718292f2SDouglas Gregor     /// \brief Whether an error occurred.
851718292f2SDouglas Gregor     bool HadError;
852718292f2SDouglas Gregor 
853718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
854718292f2SDouglas Gregor     /// during parsing.
855718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
856718292f2SDouglas Gregor 
857718292f2SDouglas Gregor     /// \brief The current token.
858718292f2SDouglas Gregor     MMToken Tok;
859718292f2SDouglas Gregor 
860718292f2SDouglas Gregor     /// \brief The active module.
861de3ef502SDouglas Gregor     Module *ActiveModule;
862718292f2SDouglas Gregor 
863718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
864718292f2SDouglas Gregor     SourceLocation consumeToken();
865718292f2SDouglas Gregor 
866718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
867718292f2SDouglas Gregor     /// (or the end of the file).
868718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
869718292f2SDouglas Gregor 
870f857950dSDmitri Gribenko     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
871e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
872718292f2SDouglas Gregor     void parseModuleDecl();
87397292843SDaniel Jasper     void parseExternModuleDecl();
8741fb5c3a6SDouglas Gregor     void parseRequiresDecl();
875b53e5483SLawrence Crowl     void parseHeaderDecl(clang::MMToken::TokenKind,
876b53e5483SLawrence Crowl                          SourceLocation LeadingLoc);
877524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
8782b82c2a5SDouglas Gregor     void parseExportDecl();
879ba7f2f71SDaniel Jasper     void parseUseDecl();
8806ddfca91SDouglas Gregor     void parseLinkDecl();
88135b13eceSDouglas Gregor     void parseConfigMacros();
882fb912657SDouglas Gregor     void parseConflict();
8839194a91dSDouglas Gregor     void parseInferredModuleDecl(bool Framework, bool Explicit);
8844442605fSBill Wendling     bool parseOptionalAttributes(Attributes &Attrs);
885718292f2SDouglas Gregor 
8867033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
8877033127bSDouglas Gregor 
888718292f2SDouglas Gregor   public:
889718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
890bc10b9fbSDouglas Gregor                              const TargetInfo *Target,
891718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
8925257fc63SDouglas Gregor                              ModuleMap &Map,
8933ec6663bSDouglas Gregor                              const DirectoryEntry *Directory,
894963c5535SDouglas Gregor                              const DirectoryEntry *BuiltinIncludeDir,
895963c5535SDouglas Gregor                              bool IsSystem)
896bc10b9fbSDouglas Gregor       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
8973ec6663bSDouglas Gregor         Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
898963c5535SDouglas Gregor         IsSystem(IsSystem), HadError(false), ActiveModule(0)
899718292f2SDouglas Gregor     {
900718292f2SDouglas Gregor       Tok.clear();
901718292f2SDouglas Gregor       consumeToken();
902718292f2SDouglas Gregor     }
903718292f2SDouglas Gregor 
904718292f2SDouglas Gregor     bool parseModuleMapFile();
905718292f2SDouglas Gregor   };
906718292f2SDouglas Gregor }
907718292f2SDouglas Gregor 
908718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
909718292f2SDouglas Gregor retry:
910718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
911718292f2SDouglas Gregor   Tok.clear();
912718292f2SDouglas Gregor 
913718292f2SDouglas Gregor   Token LToken;
914718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
915718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
916718292f2SDouglas Gregor   switch (LToken.getKind()) {
917718292f2SDouglas Gregor   case tok::raw_identifier:
918718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
919718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
920718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
92135b13eceSDouglas Gregor                  .Case("config_macros", MMToken::ConfigMacros)
922fb912657SDouglas Gregor                  .Case("conflict", MMToken::Conflict)
92359527666SDouglas Gregor                  .Case("exclude", MMToken::ExcludeKeyword)
924718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
9252b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
92697292843SDaniel Jasper                  .Case("extern", MMToken::ExternKeyword)
927755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
92835b13eceSDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
9296ddfca91SDouglas Gregor                  .Case("link", MMToken::LinkKeyword)
930718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
931b53e5483SLawrence Crowl                  .Case("private", MMToken::PrivateKeyword)
9321fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
933718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
934ba7f2f71SDaniel Jasper                  .Case("use", MMToken::UseKeyword)
935718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
936718292f2SDouglas Gregor     break;
937718292f2SDouglas Gregor 
9381fb5c3a6SDouglas Gregor   case tok::comma:
9391fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
9401fb5c3a6SDouglas Gregor     break;
9411fb5c3a6SDouglas Gregor 
942718292f2SDouglas Gregor   case tok::eof:
943718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
944718292f2SDouglas Gregor     break;
945718292f2SDouglas Gregor 
946718292f2SDouglas Gregor   case tok::l_brace:
947718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
948718292f2SDouglas Gregor     break;
949718292f2SDouglas Gregor 
950a686e1b0SDouglas Gregor   case tok::l_square:
951a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
952a686e1b0SDouglas Gregor     break;
953a686e1b0SDouglas Gregor 
9542b82c2a5SDouglas Gregor   case tok::period:
9552b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
9562b82c2a5SDouglas Gregor     break;
9572b82c2a5SDouglas Gregor 
958718292f2SDouglas Gregor   case tok::r_brace:
959718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
960718292f2SDouglas Gregor     break;
961718292f2SDouglas Gregor 
962a686e1b0SDouglas Gregor   case tok::r_square:
963a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
964a686e1b0SDouglas Gregor     break;
965a686e1b0SDouglas Gregor 
9662b82c2a5SDouglas Gregor   case tok::star:
9672b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
9682b82c2a5SDouglas Gregor     break;
9692b82c2a5SDouglas Gregor 
970a3feee2aSRichard Smith   case tok::exclaim:
971a3feee2aSRichard Smith     Tok.Kind = MMToken::Exclaim;
972a3feee2aSRichard Smith     break;
973a3feee2aSRichard Smith 
974718292f2SDouglas Gregor   case tok::string_literal: {
975d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
976d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
977d67aea28SRichard Smith       HadError = true;
978d67aea28SRichard Smith       goto retry;
979d67aea28SRichard Smith     }
980d67aea28SRichard Smith 
981718292f2SDouglas Gregor     // Parse the string literal.
982718292f2SDouglas Gregor     LangOptions LangOpts;
983718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
984718292f2SDouglas Gregor     if (StringLiteral.hadError)
985718292f2SDouglas Gregor       goto retry;
986718292f2SDouglas Gregor 
987718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
988718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
989718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
990718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
991718292f2SDouglas Gregor     Saved[Length] = 0;
992718292f2SDouglas Gregor 
993718292f2SDouglas Gregor     // Form the token.
994718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
995718292f2SDouglas Gregor     Tok.StringData = Saved;
996718292f2SDouglas Gregor     Tok.StringLength = Length;
997718292f2SDouglas Gregor     break;
998718292f2SDouglas Gregor   }
999718292f2SDouglas Gregor 
1000718292f2SDouglas Gregor   case tok::comment:
1001718292f2SDouglas Gregor     goto retry;
1002718292f2SDouglas Gregor 
1003718292f2SDouglas Gregor   default:
1004718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
1005718292f2SDouglas Gregor     HadError = true;
1006718292f2SDouglas Gregor     goto retry;
1007718292f2SDouglas Gregor   }
1008718292f2SDouglas Gregor 
1009718292f2SDouglas Gregor   return Result;
1010718292f2SDouglas Gregor }
1011718292f2SDouglas Gregor 
1012718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1013718292f2SDouglas Gregor   unsigned braceDepth = 0;
1014a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
1015718292f2SDouglas Gregor   do {
1016718292f2SDouglas Gregor     switch (Tok.Kind) {
1017718292f2SDouglas Gregor     case MMToken::EndOfFile:
1018718292f2SDouglas Gregor       return;
1019718292f2SDouglas Gregor 
1020718292f2SDouglas Gregor     case MMToken::LBrace:
1021a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1022718292f2SDouglas Gregor         return;
1023718292f2SDouglas Gregor 
1024718292f2SDouglas Gregor       ++braceDepth;
1025718292f2SDouglas Gregor       break;
1026718292f2SDouglas Gregor 
1027a686e1b0SDouglas Gregor     case MMToken::LSquare:
1028a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1029a686e1b0SDouglas Gregor         return;
1030a686e1b0SDouglas Gregor 
1031a686e1b0SDouglas Gregor       ++squareDepth;
1032a686e1b0SDouglas Gregor       break;
1033a686e1b0SDouglas Gregor 
1034718292f2SDouglas Gregor     case MMToken::RBrace:
1035718292f2SDouglas Gregor       if (braceDepth > 0)
1036718292f2SDouglas Gregor         --braceDepth;
1037718292f2SDouglas Gregor       else if (Tok.is(K))
1038718292f2SDouglas Gregor         return;
1039718292f2SDouglas Gregor       break;
1040718292f2SDouglas Gregor 
1041a686e1b0SDouglas Gregor     case MMToken::RSquare:
1042a686e1b0SDouglas Gregor       if (squareDepth > 0)
1043a686e1b0SDouglas Gregor         --squareDepth;
1044a686e1b0SDouglas Gregor       else if (Tok.is(K))
1045a686e1b0SDouglas Gregor         return;
1046a686e1b0SDouglas Gregor       break;
1047a686e1b0SDouglas Gregor 
1048718292f2SDouglas Gregor     default:
1049a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1050718292f2SDouglas Gregor         return;
1051718292f2SDouglas Gregor       break;
1052718292f2SDouglas Gregor     }
1053718292f2SDouglas Gregor 
1054718292f2SDouglas Gregor    consumeToken();
1055718292f2SDouglas Gregor   } while (true);
1056718292f2SDouglas Gregor }
1057718292f2SDouglas Gregor 
1058e7ab3669SDouglas Gregor /// \brief Parse a module-id.
1059e7ab3669SDouglas Gregor ///
1060e7ab3669SDouglas Gregor ///   module-id:
1061e7ab3669SDouglas Gregor ///     identifier
1062e7ab3669SDouglas Gregor ///     identifier '.' module-id
1063e7ab3669SDouglas Gregor ///
1064e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
1065e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1066e7ab3669SDouglas Gregor   Id.clear();
1067e7ab3669SDouglas Gregor   do {
10683cd34c76SDaniel Jasper     if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
1069e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1070e7ab3669SDouglas Gregor       consumeToken();
1071e7ab3669SDouglas Gregor     } else {
1072e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1073e7ab3669SDouglas Gregor       return true;
1074e7ab3669SDouglas Gregor     }
1075e7ab3669SDouglas Gregor 
1076e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
1077e7ab3669SDouglas Gregor       break;
1078e7ab3669SDouglas Gregor 
1079e7ab3669SDouglas Gregor     consumeToken();
1080e7ab3669SDouglas Gregor   } while (true);
1081e7ab3669SDouglas Gregor 
1082e7ab3669SDouglas Gregor   return false;
1083e7ab3669SDouglas Gregor }
1084e7ab3669SDouglas Gregor 
1085a686e1b0SDouglas Gregor namespace {
1086a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
1087a686e1b0SDouglas Gregor   enum AttributeKind {
1088a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
1089a686e1b0SDouglas Gregor     AT_unknown,
1090a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
109135b13eceSDouglas Gregor     AT_system,
109235b13eceSDouglas Gregor     /// \brief The 'exhaustive' attribute.
109335b13eceSDouglas Gregor     AT_exhaustive
1094a686e1b0SDouglas Gregor   };
1095a686e1b0SDouglas Gregor }
1096a686e1b0SDouglas Gregor 
1097718292f2SDouglas Gregor /// \brief Parse a module declaration.
1098718292f2SDouglas Gregor ///
1099718292f2SDouglas Gregor ///   module-declaration:
110097292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
1101a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1102a686e1b0SDouglas Gregor ///       { module-member* }
1103a686e1b0SDouglas Gregor ///
1104718292f2SDouglas Gregor ///   module-member:
11051fb5c3a6SDouglas Gregor ///     requires-declaration
1106718292f2SDouglas Gregor ///     header-declaration
1107e7ab3669SDouglas Gregor ///     submodule-declaration
11082b82c2a5SDouglas Gregor ///     export-declaration
11096ddfca91SDouglas Gregor ///     link-declaration
111073441091SDouglas Gregor ///
111173441091SDouglas Gregor ///   submodule-declaration:
111273441091SDouglas Gregor ///     module-declaration
111373441091SDouglas Gregor ///     inferred-submodule-declaration
1114718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
1115755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
111697292843SDaniel Jasper          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
111797292843SDaniel Jasper   if (Tok.is(MMToken::ExternKeyword)) {
111897292843SDaniel Jasper     parseExternModuleDecl();
111997292843SDaniel Jasper     return;
112097292843SDaniel Jasper   }
112197292843SDaniel Jasper 
1122f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
1123e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
1124718292f2SDouglas Gregor   bool Explicit = false;
1125f2161a70SDouglas Gregor   bool Framework = false;
1126755b2055SDouglas Gregor 
1127f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
1128f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
1129e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
1130f2161a70SDouglas Gregor     Explicit = true;
1131f2161a70SDouglas Gregor   }
1132f2161a70SDouglas Gregor 
1133f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
1134755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
1135755b2055SDouglas Gregor     consumeToken();
1136755b2055SDouglas Gregor     Framework = true;
1137755b2055SDouglas Gregor   }
1138718292f2SDouglas Gregor 
1139718292f2SDouglas Gregor   // Parse 'module' keyword.
1140718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
1141d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1142718292f2SDouglas Gregor     consumeToken();
1143718292f2SDouglas Gregor     HadError = true;
1144718292f2SDouglas Gregor     return;
1145718292f2SDouglas Gregor   }
1146718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
1147718292f2SDouglas Gregor 
114873441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
114973441091SDouglas Gregor   // Parse it.
115073441091SDouglas Gregor   if (Tok.is(MMToken::Star))
11519194a91dSDouglas Gregor     return parseInferredModuleDecl(Framework, Explicit);
115273441091SDouglas Gregor 
1153718292f2SDouglas Gregor   // Parse the module name.
1154e7ab3669SDouglas Gregor   ModuleId Id;
1155e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
1156718292f2SDouglas Gregor     HadError = true;
1157718292f2SDouglas Gregor     return;
1158718292f2SDouglas Gregor   }
1159e7ab3669SDouglas Gregor 
1160e7ab3669SDouglas Gregor   if (ActiveModule) {
1161e7ab3669SDouglas Gregor     if (Id.size() > 1) {
1162e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1163e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
1164e7ab3669SDouglas Gregor 
1165e7ab3669SDouglas Gregor       HadError = true;
1166e7ab3669SDouglas Gregor       return;
1167e7ab3669SDouglas Gregor     }
1168e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
1169e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
1170e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1171e7ab3669SDouglas Gregor     Explicit = false;
1172e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
1173e7ab3669SDouglas Gregor     HadError = true;
1174e7ab3669SDouglas Gregor   }
1175e7ab3669SDouglas Gregor 
1176e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
1177e7ab3669SDouglas Gregor   if (Id.size() > 1) {
1178e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
1179e7ab3669SDouglas Gregor     // is a submodule.
1180e7ab3669SDouglas Gregor     ActiveModule = 0;
1181e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1182e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1183e7ab3669SDouglas Gregor         ActiveModule = Next;
1184e7ab3669SDouglas Gregor         continue;
1185e7ab3669SDouglas Gregor       }
1186e7ab3669SDouglas Gregor 
1187e7ab3669SDouglas Gregor       if (ActiveModule) {
1188e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1189e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
1190e7ab3669SDouglas Gregor       } else {
1191e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1192e7ab3669SDouglas Gregor       }
1193e7ab3669SDouglas Gregor       HadError = true;
1194e7ab3669SDouglas Gregor       return;
1195e7ab3669SDouglas Gregor     }
1196e7ab3669SDouglas Gregor   }
1197e7ab3669SDouglas Gregor 
1198e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
1199e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
1200718292f2SDouglas Gregor 
1201a686e1b0SDouglas Gregor   // Parse the optional attribute list.
12024442605fSBill Wendling   Attributes Attrs;
12039194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
1204a686e1b0SDouglas Gregor 
1205718292f2SDouglas Gregor   // Parse the opening brace.
1206718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
1207718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1208718292f2SDouglas Gregor       << ModuleName;
1209718292f2SDouglas Gregor     HadError = true;
1210718292f2SDouglas Gregor     return;
1211718292f2SDouglas Gregor   }
1212718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
1213718292f2SDouglas Gregor 
1214718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
1215eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1216fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1217fcc54a3bSDouglas Gregor       // Skip the module definition.
1218fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
1219fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
1220fcc54a3bSDouglas Gregor         consumeToken();
1221fcc54a3bSDouglas Gregor       else {
1222fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1223fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1224fcc54a3bSDouglas Gregor         HadError = true;
1225fcc54a3bSDouglas Gregor       }
1226fcc54a3bSDouglas Gregor       return;
1227fcc54a3bSDouglas Gregor     }
1228fcc54a3bSDouglas Gregor 
1229718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1230718292f2SDouglas Gregor       << ModuleName;
1231eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1232718292f2SDouglas Gregor 
1233718292f2SDouglas Gregor     // Skip the module definition.
1234718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
1235718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
1236718292f2SDouglas Gregor       consumeToken();
1237718292f2SDouglas Gregor 
1238718292f2SDouglas Gregor     HadError = true;
1239718292f2SDouglas Gregor     return;
1240718292f2SDouglas Gregor   }
1241718292f2SDouglas Gregor 
1242718292f2SDouglas Gregor   // Start defining this module.
1243eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1244eb90e830SDouglas Gregor                                         Explicit).first;
1245eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
1246963c5535SDouglas Gregor   if (Attrs.IsSystem || IsSystem)
1247a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
1248718292f2SDouglas Gregor 
1249718292f2SDouglas Gregor   bool Done = false;
1250718292f2SDouglas Gregor   do {
1251718292f2SDouglas Gregor     switch (Tok.Kind) {
1252718292f2SDouglas Gregor     case MMToken::EndOfFile:
1253718292f2SDouglas Gregor     case MMToken::RBrace:
1254718292f2SDouglas Gregor       Done = true;
1255718292f2SDouglas Gregor       break;
1256718292f2SDouglas Gregor 
125735b13eceSDouglas Gregor     case MMToken::ConfigMacros:
125835b13eceSDouglas Gregor       parseConfigMacros();
125935b13eceSDouglas Gregor       break;
126035b13eceSDouglas Gregor 
1261fb912657SDouglas Gregor     case MMToken::Conflict:
1262fb912657SDouglas Gregor       parseConflict();
1263fb912657SDouglas Gregor       break;
1264fb912657SDouglas Gregor 
1265718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
126697292843SDaniel Jasper     case MMToken::ExternKeyword:
1267f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
1268718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1269718292f2SDouglas Gregor       parseModuleDecl();
1270718292f2SDouglas Gregor       break;
1271718292f2SDouglas Gregor 
12722b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
12732b82c2a5SDouglas Gregor       parseExportDecl();
12742b82c2a5SDouglas Gregor       break;
12752b82c2a5SDouglas Gregor 
1276ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
1277ba7f2f71SDaniel Jasper       parseUseDecl();
1278ba7f2f71SDaniel Jasper       break;
1279ba7f2f71SDaniel Jasper 
12801fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
12811fb5c3a6SDouglas Gregor       parseRequiresDecl();
12821fb5c3a6SDouglas Gregor       break;
12831fb5c3a6SDouglas Gregor 
1284524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
1285524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
1286524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
1287b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1288524e33e1SDouglas Gregor       else
1289524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
1290718292f2SDouglas Gregor       break;
1291524e33e1SDouglas Gregor     }
1292718292f2SDouglas Gregor 
129359527666SDouglas Gregor     case MMToken::ExcludeKeyword: {
129459527666SDouglas Gregor       SourceLocation ExcludeLoc = consumeToken();
129559527666SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword)) {
1296b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc);
129759527666SDouglas Gregor       } else {
129859527666SDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
129959527666SDouglas Gregor           << "exclude";
130059527666SDouglas Gregor       }
130159527666SDouglas Gregor       break;
130259527666SDouglas Gregor     }
130359527666SDouglas Gregor 
1304b53e5483SLawrence Crowl     case MMToken::PrivateKeyword: {
1305b53e5483SLawrence Crowl       SourceLocation PrivateLoc = consumeToken();
1306b53e5483SLawrence Crowl       if (Tok.is(MMToken::HeaderKeyword)) {
1307b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc);
1308b53e5483SLawrence Crowl       } else {
1309b53e5483SLawrence Crowl         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1310b53e5483SLawrence Crowl           << "private";
1311b53e5483SLawrence Crowl       }
1312b53e5483SLawrence Crowl       break;
1313b53e5483SLawrence Crowl     }
1314b53e5483SLawrence Crowl 
1315322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
1316b53e5483SLawrence Crowl       parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation());
1317718292f2SDouglas Gregor       break;
1318718292f2SDouglas Gregor 
13196ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
13206ddfca91SDouglas Gregor       parseLinkDecl();
13216ddfca91SDouglas Gregor       break;
13226ddfca91SDouglas Gregor 
1323718292f2SDouglas Gregor     default:
1324718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1325718292f2SDouglas Gregor       consumeToken();
1326718292f2SDouglas Gregor       break;
1327718292f2SDouglas Gregor     }
1328718292f2SDouglas Gregor   } while (!Done);
1329718292f2SDouglas Gregor 
1330718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1331718292f2SDouglas Gregor     consumeToken();
1332718292f2SDouglas Gregor   else {
1333718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1334718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1335718292f2SDouglas Gregor     HadError = true;
1336718292f2SDouglas Gregor   }
1337718292f2SDouglas Gregor 
133811dfe6feSDouglas Gregor   // If the active module is a top-level framework, and there are no link
133911dfe6feSDouglas Gregor   // libraries, automatically link against the framework.
134011dfe6feSDouglas Gregor   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
134111dfe6feSDouglas Gregor       ActiveModule->LinkLibraries.empty()) {
134211dfe6feSDouglas Gregor     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
134311dfe6feSDouglas Gregor   }
134411dfe6feSDouglas Gregor 
1345e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1346e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1347718292f2SDouglas Gregor }
1348718292f2SDouglas Gregor 
134997292843SDaniel Jasper /// \brief Parse an extern module declaration.
135097292843SDaniel Jasper ///
135197292843SDaniel Jasper ///   extern module-declaration:
135297292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
135397292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() {
135497292843SDaniel Jasper   assert(Tok.is(MMToken::ExternKeyword));
135597292843SDaniel Jasper   consumeToken(); // 'extern' keyword
135697292843SDaniel Jasper 
135797292843SDaniel Jasper   // Parse 'module' keyword.
135897292843SDaniel Jasper   if (!Tok.is(MMToken::ModuleKeyword)) {
135997292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
136097292843SDaniel Jasper     consumeToken();
136197292843SDaniel Jasper     HadError = true;
136297292843SDaniel Jasper     return;
136397292843SDaniel Jasper   }
136497292843SDaniel Jasper   consumeToken(); // 'module' keyword
136597292843SDaniel Jasper 
136697292843SDaniel Jasper   // Parse the module name.
136797292843SDaniel Jasper   ModuleId Id;
136897292843SDaniel Jasper   if (parseModuleId(Id)) {
136997292843SDaniel Jasper     HadError = true;
137097292843SDaniel Jasper     return;
137197292843SDaniel Jasper   }
137297292843SDaniel Jasper 
137397292843SDaniel Jasper   // Parse the referenced module map file name.
137497292843SDaniel Jasper   if (!Tok.is(MMToken::StringLiteral)) {
137597292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
137697292843SDaniel Jasper     HadError = true;
137797292843SDaniel Jasper     return;
137897292843SDaniel Jasper   }
137997292843SDaniel Jasper   std::string FileName = Tok.getString();
138097292843SDaniel Jasper   consumeToken(); // filename
138197292843SDaniel Jasper 
138297292843SDaniel Jasper   StringRef FileNameRef = FileName;
138397292843SDaniel Jasper   SmallString<128> ModuleMapFileName;
138497292843SDaniel Jasper   if (llvm::sys::path::is_relative(FileNameRef)) {
138597292843SDaniel Jasper     ModuleMapFileName += Directory->getName();
138697292843SDaniel Jasper     llvm::sys::path::append(ModuleMapFileName, FileName);
138797292843SDaniel Jasper     FileNameRef = ModuleMapFileName.str();
138897292843SDaniel Jasper   }
138997292843SDaniel Jasper   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
139097292843SDaniel Jasper     Map.parseModuleMapFile(File, /*IsSystem=*/false);
139197292843SDaniel Jasper }
139297292843SDaniel Jasper 
13931fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
13941fb5c3a6SDouglas Gregor ///
13951fb5c3a6SDouglas Gregor ///   requires-declaration:
13961fb5c3a6SDouglas Gregor ///     'requires' feature-list
13971fb5c3a6SDouglas Gregor ///
13981fb5c3a6SDouglas Gregor ///   feature-list:
1399a3feee2aSRichard Smith ///     feature ',' feature-list
1400a3feee2aSRichard Smith ///     feature
1401a3feee2aSRichard Smith ///
1402a3feee2aSRichard Smith ///   feature:
1403a3feee2aSRichard Smith ///     '!'[opt] identifier
14041fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
14051fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
14061fb5c3a6SDouglas Gregor 
14071fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
14081fb5c3a6SDouglas Gregor   consumeToken();
14091fb5c3a6SDouglas Gregor 
14101fb5c3a6SDouglas Gregor   // Parse the feature-list.
14111fb5c3a6SDouglas Gregor   do {
1412a3feee2aSRichard Smith     bool RequiredState = true;
1413a3feee2aSRichard Smith     if (Tok.is(MMToken::Exclaim)) {
1414a3feee2aSRichard Smith       RequiredState = false;
1415a3feee2aSRichard Smith       consumeToken();
1416a3feee2aSRichard Smith     }
1417a3feee2aSRichard Smith 
14181fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
14191fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
14201fb5c3a6SDouglas Gregor       HadError = true;
14211fb5c3a6SDouglas Gregor       return;
14221fb5c3a6SDouglas Gregor     }
14231fb5c3a6SDouglas Gregor 
14241fb5c3a6SDouglas Gregor     // Consume the feature name.
14251fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
14261fb5c3a6SDouglas Gregor     consumeToken();
14271fb5c3a6SDouglas Gregor 
14281fb5c3a6SDouglas Gregor     // Add this feature.
1429a3feee2aSRichard Smith     ActiveModule->addRequirement(Feature, RequiredState,
1430a3feee2aSRichard Smith                                  Map.LangOpts, *Map.Target);
14311fb5c3a6SDouglas Gregor 
14321fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
14331fb5c3a6SDouglas Gregor       break;
14341fb5c3a6SDouglas Gregor 
14351fb5c3a6SDouglas Gregor     // Consume the comma.
14361fb5c3a6SDouglas Gregor     consumeToken();
14371fb5c3a6SDouglas Gregor   } while (true);
14381fb5c3a6SDouglas Gregor }
14391fb5c3a6SDouglas Gregor 
1440f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1441f2161a70SDouglas Gregor /// subframework in which the given module lives.
1442bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1443f857950dSDmitri Gribenko                                     SmallVectorImpl<char> &Path) {
1444f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1445f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Paths;
1446f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1447f2161a70SDouglas Gregor     if (Mod->IsFramework)
1448f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1449f2161a70SDouglas Gregor   }
1450f2161a70SDouglas Gregor 
1451f2161a70SDouglas Gregor   if (Paths.empty())
1452f2161a70SDouglas Gregor     return;
1453f2161a70SDouglas Gregor 
1454f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
145517381a06SBenjamin Kramer   for (unsigned I = Paths.size() - 1; I != 0; --I)
145617381a06SBenjamin Kramer     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
1457f2161a70SDouglas Gregor }
1458f2161a70SDouglas Gregor 
1459718292f2SDouglas Gregor /// \brief Parse a header declaration.
1460718292f2SDouglas Gregor ///
1461718292f2SDouglas Gregor ///   header-declaration:
1462322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
146359527666SDouglas Gregor ///     'exclude'[opt] 'header' string-literal
1464b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
1465b53e5483SLawrence Crowl                                       SourceLocation LeadingLoc) {
1466718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
14671871ed3dSBenjamin Kramer   consumeToken();
1468718292f2SDouglas Gregor 
1469718292f2SDouglas Gregor   // Parse the header name.
1470718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1471718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1472718292f2SDouglas Gregor       << "header";
1473718292f2SDouglas Gregor     HadError = true;
1474718292f2SDouglas Gregor     return;
1475718292f2SDouglas Gregor   }
1476*0761a8a0SDaniel Jasper   Module::HeaderDirective Header;
1477*0761a8a0SDaniel Jasper   Header.FileName = Tok.getString();
1478*0761a8a0SDaniel Jasper   Header.FileNameLoc = consumeToken();
1479718292f2SDouglas Gregor 
1480524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1481b53e5483SLawrence Crowl   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
1482*0761a8a0SDaniel Jasper     Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
1483524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1484322f633cSDouglas Gregor     HadError = true;
1485322f633cSDouglas Gregor     return;
1486322f633cSDouglas Gregor   }
1487322f633cSDouglas Gregor 
14885257fc63SDouglas Gregor   // Look for this file.
1489e7ab3669SDouglas Gregor   const FileEntry *File = 0;
14903ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
14912c1dd271SDylan Noblesmith   SmallString<128> PathName;
1492*0761a8a0SDaniel Jasper   if (llvm::sys::path::is_absolute(Header.FileName)) {
1493*0761a8a0SDaniel Jasper     PathName = Header.FileName;
1494e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
14957033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
14967033127bSDouglas Gregor     PathName = Dir->getName();
1497*0761a8a0SDaniel Jasper     llvm::sys::path::append(PathName, Header.FileName);
14987033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1499e7ab3669SDouglas Gregor   } else {
1500e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
15017033127bSDouglas Gregor     PathName = Directory->getName();
1502e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1503755b2055SDouglas Gregor 
1504f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1505f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1506755b2055SDouglas Gregor 
1507e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
1508*0761a8a0SDaniel Jasper       llvm::sys::path::append(PathName, "Headers", Header.FileName);
1509e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1510e7ab3669SDouglas Gregor 
1511e7ab3669SDouglas Gregor       if (!File) {
1512e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1513e7ab3669SDouglas Gregor         PathName.resize(PathLength);
1514*0761a8a0SDaniel Jasper         llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName);
1515e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1516e7ab3669SDouglas Gregor       }
1517e7ab3669SDouglas Gregor     } else {
1518e7ab3669SDouglas Gregor       // Lookup for normal headers.
1519*0761a8a0SDaniel Jasper       llvm::sys::path::append(PathName, Header.FileName);
1520e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
15213ec6663bSDouglas Gregor 
15223ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
15233ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
15243ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
1525b53e5483SLawrence Crowl       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
1526b53e5483SLawrence Crowl           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
1527*0761a8a0SDaniel Jasper           isBuiltinHeader(Header.FileName)) {
15282c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
1529*0761a8a0SDaniel Jasper         llvm::sys::path::append(BuiltinPathName, Header.FileName);
15303ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
15313ec6663bSDouglas Gregor 
15323ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
15333ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
15343ec6663bSDouglas Gregor         // up adding both (later).
15353ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
15363ec6663bSDouglas Gregor           File = BuiltinFile;
15373ec6663bSDouglas Gregor           BuiltinFile = 0;
15383ec6663bSDouglas Gregor         }
15393ec6663bSDouglas Gregor       }
1540e7ab3669SDouglas Gregor     }
1541e7ab3669SDouglas Gregor   }
15425257fc63SDouglas Gregor 
15435257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
15445257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1545e7ab3669SDouglas Gregor   if (File) {
154697da9178SDaniel Jasper     if (LeadingToken == MMToken::UmbrellaKeyword) {
1547322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
154859527666SDouglas Gregor       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1549b53e5483SLawrence Crowl         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
155059527666SDouglas Gregor           << UmbrellaModule->getFullModuleName();
1551322f633cSDouglas Gregor         HadError = true;
15525257fc63SDouglas Gregor       } else {
1553322f633cSDouglas Gregor         // Record this umbrella header.
1554322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1555322f633cSDouglas Gregor       }
1556322f633cSDouglas Gregor     } else {
1557322f633cSDouglas Gregor       // Record this header.
1558b53e5483SLawrence Crowl       ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
1559b53e5483SLawrence Crowl       if (LeadingToken == MMToken::ExcludeKeyword)
1560b53e5483SLawrence Crowl         Role = ModuleMap::ExcludedHeader;
1561b53e5483SLawrence Crowl       else if (LeadingToken == MMToken::PrivateKeyword)
1562b53e5483SLawrence Crowl         Role = ModuleMap::PrivateHeader;
1563b53e5483SLawrence Crowl       else
1564b53e5483SLawrence Crowl         assert(LeadingToken == MMToken::HeaderKeyword);
1565b53e5483SLawrence Crowl 
1566b53e5483SLawrence Crowl       Map.addHeader(ActiveModule, File, Role);
15673ec6663bSDouglas Gregor 
15683ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
15693ec6663bSDouglas Gregor       if (BuiltinFile)
1570b53e5483SLawrence Crowl         Map.addHeader(ActiveModule, BuiltinFile, Role);
15715257fc63SDouglas Gregor     }
1572b53e5483SLawrence Crowl   } else if (LeadingToken != MMToken::ExcludeKeyword) {
15734b27a64bSDouglas Gregor     // Ignore excluded header files. They're optional anyway.
15744b27a64bSDouglas Gregor 
1575*0761a8a0SDaniel Jasper     // If we find a module that has a missing header, we mark this module as
1576*0761a8a0SDaniel Jasper     // unavailable and store the header directive for displaying diagnostics.
1577*0761a8a0SDaniel Jasper     // Other submodules in the same module can still be used.
1578*0761a8a0SDaniel Jasper     Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
1579*0761a8a0SDaniel Jasper     ActiveModule->IsAvailable = false;
1580*0761a8a0SDaniel Jasper     ActiveModule->MissingHeaders.push_back(Header);
15815257fc63SDouglas Gregor   }
1582718292f2SDouglas Gregor }
1583718292f2SDouglas Gregor 
1584524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1585524e33e1SDouglas Gregor ///
1586524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1587524e33e1SDouglas Gregor ///     umbrella string-literal
1588524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1589524e33e1SDouglas Gregor   // Parse the directory name.
1590524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1591524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1592524e33e1SDouglas Gregor       << "umbrella";
1593524e33e1SDouglas Gregor     HadError = true;
1594524e33e1SDouglas Gregor     return;
1595524e33e1SDouglas Gregor   }
1596524e33e1SDouglas Gregor 
1597524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1598524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1599524e33e1SDouglas Gregor 
1600524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1601524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1602524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1603524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1604524e33e1SDouglas Gregor     HadError = true;
1605524e33e1SDouglas Gregor     return;
1606524e33e1SDouglas Gregor   }
1607524e33e1SDouglas Gregor 
1608524e33e1SDouglas Gregor   // Look for this file.
1609524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1610524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1611524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1612524e33e1SDouglas Gregor   else {
16132c1dd271SDylan Noblesmith     SmallString<128> PathName;
1614524e33e1SDouglas Gregor     PathName = Directory->getName();
1615524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1616524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1617524e33e1SDouglas Gregor   }
1618524e33e1SDouglas Gregor 
1619524e33e1SDouglas Gregor   if (!Dir) {
1620524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1621524e33e1SDouglas Gregor       << DirName;
1622524e33e1SDouglas Gregor     HadError = true;
1623524e33e1SDouglas Gregor     return;
1624524e33e1SDouglas Gregor   }
1625524e33e1SDouglas Gregor 
1626524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1627524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1628524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1629524e33e1SDouglas Gregor     HadError = true;
1630524e33e1SDouglas Gregor     return;
1631524e33e1SDouglas Gregor   }
1632524e33e1SDouglas Gregor 
1633524e33e1SDouglas Gregor   // Record this umbrella directory.
1634524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1635524e33e1SDouglas Gregor }
1636524e33e1SDouglas Gregor 
16372b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
16382b82c2a5SDouglas Gregor ///
16392b82c2a5SDouglas Gregor ///   export-declaration:
16402b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
16412b82c2a5SDouglas Gregor ///
16422b82c2a5SDouglas Gregor ///   wildcard-module-id:
16432b82c2a5SDouglas Gregor ///     identifier
16442b82c2a5SDouglas Gregor ///     '*'
16452b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
16462b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
16472b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
16482b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
16492b82c2a5SDouglas Gregor 
16502b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
16512b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
16522b82c2a5SDouglas Gregor   bool Wildcard = false;
16532b82c2a5SDouglas Gregor   do {
16542b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
16552b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
16562b82c2a5SDouglas Gregor                                               Tok.getLocation()));
16572b82c2a5SDouglas Gregor       consumeToken();
16582b82c2a5SDouglas Gregor 
16592b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
16602b82c2a5SDouglas Gregor         consumeToken();
16612b82c2a5SDouglas Gregor         continue;
16622b82c2a5SDouglas Gregor       }
16632b82c2a5SDouglas Gregor 
16642b82c2a5SDouglas Gregor       break;
16652b82c2a5SDouglas Gregor     }
16662b82c2a5SDouglas Gregor 
16672b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
16682b82c2a5SDouglas Gregor       Wildcard = true;
1669f5eedd05SDouglas Gregor       consumeToken();
16702b82c2a5SDouglas Gregor       break;
16712b82c2a5SDouglas Gregor     }
16722b82c2a5SDouglas Gregor 
1673ba7f2f71SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
16742b82c2a5SDouglas Gregor     HadError = true;
16752b82c2a5SDouglas Gregor     return;
16762b82c2a5SDouglas Gregor   } while (true);
16772b82c2a5SDouglas Gregor 
16782b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
16792b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
16802b82c2a5SDouglas Gregor   };
16812b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
16822b82c2a5SDouglas Gregor }
16832b82c2a5SDouglas Gregor 
1684ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration.
1685ba7f2f71SDaniel Jasper ///
1686ba7f2f71SDaniel Jasper ///   uses-declaration:
1687ba7f2f71SDaniel Jasper ///     'uses' wildcard-module-id
1688ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() {
1689ba7f2f71SDaniel Jasper   assert(Tok.is(MMToken::UseKeyword));
1690ba7f2f71SDaniel Jasper   consumeToken();
1691ba7f2f71SDaniel Jasper   // Parse the module-id.
1692ba7f2f71SDaniel Jasper   ModuleId ParsedModuleId;
16933cd34c76SDaniel Jasper   parseModuleId(ParsedModuleId);
1694ba7f2f71SDaniel Jasper 
1695ba7f2f71SDaniel Jasper   ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
1696ba7f2f71SDaniel Jasper }
1697ba7f2f71SDaniel Jasper 
16986ddfca91SDouglas Gregor /// \brief Parse a link declaration.
16996ddfca91SDouglas Gregor ///
17006ddfca91SDouglas Gregor ///   module-declaration:
17016ddfca91SDouglas Gregor ///     'link' 'framework'[opt] string-literal
17026ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() {
17036ddfca91SDouglas Gregor   assert(Tok.is(MMToken::LinkKeyword));
17046ddfca91SDouglas Gregor   SourceLocation LinkLoc = consumeToken();
17056ddfca91SDouglas Gregor 
17066ddfca91SDouglas Gregor   // Parse the optional 'framework' keyword.
17076ddfca91SDouglas Gregor   bool IsFramework = false;
17086ddfca91SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
17096ddfca91SDouglas Gregor     consumeToken();
17106ddfca91SDouglas Gregor     IsFramework = true;
17116ddfca91SDouglas Gregor   }
17126ddfca91SDouglas Gregor 
17136ddfca91SDouglas Gregor   // Parse the library name
17146ddfca91SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
17156ddfca91SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
17166ddfca91SDouglas Gregor       << IsFramework << SourceRange(LinkLoc);
17176ddfca91SDouglas Gregor     HadError = true;
17186ddfca91SDouglas Gregor     return;
17196ddfca91SDouglas Gregor   }
17206ddfca91SDouglas Gregor 
17216ddfca91SDouglas Gregor   std::string LibraryName = Tok.getString();
17226ddfca91SDouglas Gregor   consumeToken();
17236ddfca91SDouglas Gregor   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
17246ddfca91SDouglas Gregor                                                             IsFramework));
17256ddfca91SDouglas Gregor }
17266ddfca91SDouglas Gregor 
172735b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration.
172835b13eceSDouglas Gregor ///
172935b13eceSDouglas Gregor ///   module-declaration:
173035b13eceSDouglas Gregor ///     'config_macros' attributes[opt] config-macro-list?
173135b13eceSDouglas Gregor ///
173235b13eceSDouglas Gregor ///   config-macro-list:
173335b13eceSDouglas Gregor ///     identifier (',' identifier)?
173435b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() {
173535b13eceSDouglas Gregor   assert(Tok.is(MMToken::ConfigMacros));
173635b13eceSDouglas Gregor   SourceLocation ConfigMacrosLoc = consumeToken();
173735b13eceSDouglas Gregor 
173835b13eceSDouglas Gregor   // Only top-level modules can have configuration macros.
173935b13eceSDouglas Gregor   if (ActiveModule->Parent) {
174035b13eceSDouglas Gregor     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
174135b13eceSDouglas Gregor   }
174235b13eceSDouglas Gregor 
174335b13eceSDouglas Gregor   // Parse the optional attributes.
174435b13eceSDouglas Gregor   Attributes Attrs;
174535b13eceSDouglas Gregor   parseOptionalAttributes(Attrs);
174635b13eceSDouglas Gregor   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
174735b13eceSDouglas Gregor     ActiveModule->ConfigMacrosExhaustive = true;
174835b13eceSDouglas Gregor   }
174935b13eceSDouglas Gregor 
175035b13eceSDouglas Gregor   // If we don't have an identifier, we're done.
175135b13eceSDouglas Gregor   if (!Tok.is(MMToken::Identifier))
175235b13eceSDouglas Gregor     return;
175335b13eceSDouglas Gregor 
175435b13eceSDouglas Gregor   // Consume the first identifier.
175535b13eceSDouglas Gregor   if (!ActiveModule->Parent) {
175635b13eceSDouglas Gregor     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
175735b13eceSDouglas Gregor   }
175835b13eceSDouglas Gregor   consumeToken();
175935b13eceSDouglas Gregor 
176035b13eceSDouglas Gregor   do {
176135b13eceSDouglas Gregor     // If there's a comma, consume it.
176235b13eceSDouglas Gregor     if (!Tok.is(MMToken::Comma))
176335b13eceSDouglas Gregor       break;
176435b13eceSDouglas Gregor     consumeToken();
176535b13eceSDouglas Gregor 
176635b13eceSDouglas Gregor     // We expect to see a macro name here.
176735b13eceSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
176835b13eceSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
176935b13eceSDouglas Gregor       break;
177035b13eceSDouglas Gregor     }
177135b13eceSDouglas Gregor 
177235b13eceSDouglas Gregor     // Consume the macro name.
177335b13eceSDouglas Gregor     if (!ActiveModule->Parent) {
177435b13eceSDouglas Gregor       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
177535b13eceSDouglas Gregor     }
177635b13eceSDouglas Gregor     consumeToken();
177735b13eceSDouglas Gregor   } while (true);
177835b13eceSDouglas Gregor }
177935b13eceSDouglas Gregor 
1780fb912657SDouglas Gregor /// \brief Format a module-id into a string.
1781fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) {
1782fb912657SDouglas Gregor   std::string result;
1783fb912657SDouglas Gregor   {
1784fb912657SDouglas Gregor     llvm::raw_string_ostream OS(result);
1785fb912657SDouglas Gregor 
1786fb912657SDouglas Gregor     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
1787fb912657SDouglas Gregor       if (I)
1788fb912657SDouglas Gregor         OS << ".";
1789fb912657SDouglas Gregor       OS << Id[I].first;
1790fb912657SDouglas Gregor     }
1791fb912657SDouglas Gregor   }
1792fb912657SDouglas Gregor 
1793fb912657SDouglas Gregor   return result;
1794fb912657SDouglas Gregor }
1795fb912657SDouglas Gregor 
1796fb912657SDouglas Gregor /// \brief Parse a conflict declaration.
1797fb912657SDouglas Gregor ///
1798fb912657SDouglas Gregor ///   module-declaration:
1799fb912657SDouglas Gregor ///     'conflict' module-id ',' string-literal
1800fb912657SDouglas Gregor void ModuleMapParser::parseConflict() {
1801fb912657SDouglas Gregor   assert(Tok.is(MMToken::Conflict));
1802fb912657SDouglas Gregor   SourceLocation ConflictLoc = consumeToken();
1803fb912657SDouglas Gregor   Module::UnresolvedConflict Conflict;
1804fb912657SDouglas Gregor 
1805fb912657SDouglas Gregor   // Parse the module-id.
1806fb912657SDouglas Gregor   if (parseModuleId(Conflict.Id))
1807fb912657SDouglas Gregor     return;
1808fb912657SDouglas Gregor 
1809fb912657SDouglas Gregor   // Parse the ','.
1810fb912657SDouglas Gregor   if (!Tok.is(MMToken::Comma)) {
1811fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
1812fb912657SDouglas Gregor       << SourceRange(ConflictLoc);
1813fb912657SDouglas Gregor     return;
1814fb912657SDouglas Gregor   }
1815fb912657SDouglas Gregor   consumeToken();
1816fb912657SDouglas Gregor 
1817fb912657SDouglas Gregor   // Parse the message.
1818fb912657SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1819fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
1820fb912657SDouglas Gregor       << formatModuleId(Conflict.Id);
1821fb912657SDouglas Gregor     return;
1822fb912657SDouglas Gregor   }
1823fb912657SDouglas Gregor   Conflict.Message = Tok.getString().str();
1824fb912657SDouglas Gregor   consumeToken();
1825fb912657SDouglas Gregor 
1826fb912657SDouglas Gregor   // Add this unresolved conflict.
1827fb912657SDouglas Gregor   ActiveModule->UnresolvedConflicts.push_back(Conflict);
1828fb912657SDouglas Gregor }
1829fb912657SDouglas Gregor 
18306ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules).
18319194a91dSDouglas Gregor ///
18329194a91dSDouglas Gregor ///   module-declaration:
18339194a91dSDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
18349194a91dSDouglas Gregor ///       { inferred-module-member* }
18359194a91dSDouglas Gregor ///
18369194a91dSDouglas Gregor ///   inferred-module-member:
18379194a91dSDouglas Gregor ///     'export' '*'
18389194a91dSDouglas Gregor ///     'exclude' identifier
18399194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
184073441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
184173441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
184273441091SDouglas Gregor   bool Failed = false;
184373441091SDouglas Gregor 
184473441091SDouglas Gregor   // Inferred modules must be submodules.
18459194a91dSDouglas Gregor   if (!ActiveModule && !Framework) {
184673441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
184773441091SDouglas Gregor     Failed = true;
184873441091SDouglas Gregor   }
184973441091SDouglas Gregor 
18509194a91dSDouglas Gregor   if (ActiveModule) {
1851524e33e1SDouglas Gregor     // Inferred modules must have umbrella directories.
1852524e33e1SDouglas Gregor     if (!Failed && !ActiveModule->getUmbrellaDir()) {
185373441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
185473441091SDouglas Gregor       Failed = true;
185573441091SDouglas Gregor     }
185673441091SDouglas Gregor 
185773441091SDouglas Gregor     // Check for redefinition of an inferred module.
1858dd005f69SDouglas Gregor     if (!Failed && ActiveModule->InferSubmodules) {
185973441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1860dd005f69SDouglas Gregor       if (ActiveModule->InferredSubmoduleLoc.isValid())
1861dd005f69SDouglas Gregor         Diags.Report(ActiveModule->InferredSubmoduleLoc,
186273441091SDouglas Gregor                      diag::note_mmap_prev_definition);
186373441091SDouglas Gregor       Failed = true;
186473441091SDouglas Gregor     }
186573441091SDouglas Gregor 
18669194a91dSDouglas Gregor     // Check for the 'framework' keyword, which is not permitted here.
18679194a91dSDouglas Gregor     if (Framework) {
18689194a91dSDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
18699194a91dSDouglas Gregor       Framework = false;
18709194a91dSDouglas Gregor     }
18719194a91dSDouglas Gregor   } else if (Explicit) {
18729194a91dSDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
18739194a91dSDouglas Gregor     Explicit = false;
18749194a91dSDouglas Gregor   }
18759194a91dSDouglas Gregor 
187673441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
187773441091SDouglas Gregor   if (Failed) {
187873441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
187973441091SDouglas Gregor       consumeToken();
188073441091SDouglas Gregor       skipUntil(MMToken::RBrace);
188173441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
188273441091SDouglas Gregor         consumeToken();
188373441091SDouglas Gregor     }
188473441091SDouglas Gregor     HadError = true;
188573441091SDouglas Gregor     return;
188673441091SDouglas Gregor   }
188773441091SDouglas Gregor 
18889194a91dSDouglas Gregor   // Parse optional attributes.
18894442605fSBill Wendling   Attributes Attrs;
18909194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
18919194a91dSDouglas Gregor 
18929194a91dSDouglas Gregor   if (ActiveModule) {
189373441091SDouglas Gregor     // Note that we have an inferred submodule.
1894dd005f69SDouglas Gregor     ActiveModule->InferSubmodules = true;
1895dd005f69SDouglas Gregor     ActiveModule->InferredSubmoduleLoc = StarLoc;
1896dd005f69SDouglas Gregor     ActiveModule->InferExplicitSubmodules = Explicit;
18979194a91dSDouglas Gregor   } else {
18989194a91dSDouglas Gregor     // We'll be inferring framework modules for this directory.
18999194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferModules = true;
19009194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
19019194a91dSDouglas Gregor   }
190273441091SDouglas Gregor 
190373441091SDouglas Gregor   // Parse the opening brace.
190473441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
190573441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
190673441091SDouglas Gregor     HadError = true;
190773441091SDouglas Gregor     return;
190873441091SDouglas Gregor   }
190973441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
191073441091SDouglas Gregor 
191173441091SDouglas Gregor   // Parse the body of the inferred submodule.
191273441091SDouglas Gregor   bool Done = false;
191373441091SDouglas Gregor   do {
191473441091SDouglas Gregor     switch (Tok.Kind) {
191573441091SDouglas Gregor     case MMToken::EndOfFile:
191673441091SDouglas Gregor     case MMToken::RBrace:
191773441091SDouglas Gregor       Done = true;
191873441091SDouglas Gregor       break;
191973441091SDouglas Gregor 
19209194a91dSDouglas Gregor     case MMToken::ExcludeKeyword: {
19219194a91dSDouglas Gregor       if (ActiveModule) {
19229194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1923162405daSDouglas Gregor           << (ActiveModule != 0);
19249194a91dSDouglas Gregor         consumeToken();
19259194a91dSDouglas Gregor         break;
19269194a91dSDouglas Gregor       }
19279194a91dSDouglas Gregor 
19289194a91dSDouglas Gregor       consumeToken();
19299194a91dSDouglas Gregor       if (!Tok.is(MMToken::Identifier)) {
19309194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
19319194a91dSDouglas Gregor         break;
19329194a91dSDouglas Gregor       }
19339194a91dSDouglas Gregor 
19349194a91dSDouglas Gregor       Map.InferredDirectories[Directory].ExcludedModules
19359194a91dSDouglas Gregor         .push_back(Tok.getString());
19369194a91dSDouglas Gregor       consumeToken();
19379194a91dSDouglas Gregor       break;
19389194a91dSDouglas Gregor     }
19399194a91dSDouglas Gregor 
19409194a91dSDouglas Gregor     case MMToken::ExportKeyword:
19419194a91dSDouglas Gregor       if (!ActiveModule) {
19429194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1943162405daSDouglas Gregor           << (ActiveModule != 0);
19449194a91dSDouglas Gregor         consumeToken();
19459194a91dSDouglas Gregor         break;
19469194a91dSDouglas Gregor       }
19479194a91dSDouglas Gregor 
194873441091SDouglas Gregor       consumeToken();
194973441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1950dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
195173441091SDouglas Gregor       else
195273441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
195373441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
195473441091SDouglas Gregor       consumeToken();
195573441091SDouglas Gregor       break;
195673441091SDouglas Gregor 
195773441091SDouglas Gregor     case MMToken::ExplicitKeyword:
195873441091SDouglas Gregor     case MMToken::ModuleKeyword:
195973441091SDouglas Gregor     case MMToken::HeaderKeyword:
1960b53e5483SLawrence Crowl     case MMToken::PrivateKeyword:
196173441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
196273441091SDouglas Gregor     default:
19639194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1964162405daSDouglas Gregor           << (ActiveModule != 0);
196573441091SDouglas Gregor       consumeToken();
196673441091SDouglas Gregor       break;
196773441091SDouglas Gregor     }
196873441091SDouglas Gregor   } while (!Done);
196973441091SDouglas Gregor 
197073441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
197173441091SDouglas Gregor     consumeToken();
197273441091SDouglas Gregor   else {
197373441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
197473441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
197573441091SDouglas Gregor     HadError = true;
197673441091SDouglas Gregor   }
197773441091SDouglas Gregor }
197873441091SDouglas Gregor 
19799194a91dSDouglas Gregor /// \brief Parse optional attributes.
19809194a91dSDouglas Gregor ///
19819194a91dSDouglas Gregor ///   attributes:
19829194a91dSDouglas Gregor ///     attribute attributes
19839194a91dSDouglas Gregor ///     attribute
19849194a91dSDouglas Gregor ///
19859194a91dSDouglas Gregor ///   attribute:
19869194a91dSDouglas Gregor ///     [ identifier ]
19879194a91dSDouglas Gregor ///
19889194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes.
19899194a91dSDouglas Gregor ///
19909194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise.
19914442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
19929194a91dSDouglas Gregor   bool HadError = false;
19939194a91dSDouglas Gregor 
19949194a91dSDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
19959194a91dSDouglas Gregor     // Consume the '['.
19969194a91dSDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
19979194a91dSDouglas Gregor 
19989194a91dSDouglas Gregor     // Check whether we have an attribute name here.
19999194a91dSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
20009194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
20019194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
20029194a91dSDouglas Gregor       if (Tok.is(MMToken::RSquare))
20039194a91dSDouglas Gregor         consumeToken();
20049194a91dSDouglas Gregor       HadError = true;
20059194a91dSDouglas Gregor     }
20069194a91dSDouglas Gregor 
20079194a91dSDouglas Gregor     // Decode the attribute name.
20089194a91dSDouglas Gregor     AttributeKind Attribute
20099194a91dSDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
201035b13eceSDouglas Gregor           .Case("exhaustive", AT_exhaustive)
20119194a91dSDouglas Gregor           .Case("system", AT_system)
20129194a91dSDouglas Gregor           .Default(AT_unknown);
20139194a91dSDouglas Gregor     switch (Attribute) {
20149194a91dSDouglas Gregor     case AT_unknown:
20159194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
20169194a91dSDouglas Gregor         << Tok.getString();
20179194a91dSDouglas Gregor       break;
20189194a91dSDouglas Gregor 
20199194a91dSDouglas Gregor     case AT_system:
20209194a91dSDouglas Gregor       Attrs.IsSystem = true;
20219194a91dSDouglas Gregor       break;
202235b13eceSDouglas Gregor 
202335b13eceSDouglas Gregor     case AT_exhaustive:
202435b13eceSDouglas Gregor       Attrs.IsExhaustive = true;
202535b13eceSDouglas Gregor       break;
20269194a91dSDouglas Gregor     }
20279194a91dSDouglas Gregor     consumeToken();
20289194a91dSDouglas Gregor 
20299194a91dSDouglas Gregor     // Consume the ']'.
20309194a91dSDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
20319194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
20329194a91dSDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
20339194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
20349194a91dSDouglas Gregor       HadError = true;
20359194a91dSDouglas Gregor     }
20369194a91dSDouglas Gregor 
20379194a91dSDouglas Gregor     if (Tok.is(MMToken::RSquare))
20389194a91dSDouglas Gregor       consumeToken();
20399194a91dSDouglas Gregor   }
20409194a91dSDouglas Gregor 
20419194a91dSDouglas Gregor   return HadError;
20429194a91dSDouglas Gregor }
20439194a91dSDouglas Gregor 
20447033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
20457033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
20467033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
20477033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
20487033127bSDouglas Gregor     // If we have an umbrella directory, use that.
20497033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
20507033127bSDouglas Gregor       return Mod->getUmbrellaDir();
20517033127bSDouglas Gregor 
20527033127bSDouglas Gregor     // If we have a framework directory, stop looking.
20537033127bSDouglas Gregor     if (Mod->IsFramework)
20547033127bSDouglas Gregor       return 0;
20557033127bSDouglas Gregor   }
20567033127bSDouglas Gregor 
20577033127bSDouglas Gregor   return 0;
20587033127bSDouglas Gregor }
20597033127bSDouglas Gregor 
2060718292f2SDouglas Gregor /// \brief Parse a module map file.
2061718292f2SDouglas Gregor ///
2062718292f2SDouglas Gregor ///   module-map-file:
2063718292f2SDouglas Gregor ///     module-declaration*
2064718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
2065718292f2SDouglas Gregor   do {
2066718292f2SDouglas Gregor     switch (Tok.Kind) {
2067718292f2SDouglas Gregor     case MMToken::EndOfFile:
2068718292f2SDouglas Gregor       return HadError;
2069718292f2SDouglas Gregor 
2070e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
207197292843SDaniel Jasper     case MMToken::ExternKeyword:
2072718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
2073755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
2074718292f2SDouglas Gregor       parseModuleDecl();
2075718292f2SDouglas Gregor       break;
2076718292f2SDouglas Gregor 
20771fb5c3a6SDouglas Gregor     case MMToken::Comma:
207835b13eceSDouglas Gregor     case MMToken::ConfigMacros:
2079fb912657SDouglas Gregor     case MMToken::Conflict:
2080a3feee2aSRichard Smith     case MMToken::Exclaim:
208159527666SDouglas Gregor     case MMToken::ExcludeKeyword:
20822b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
2083718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
2084718292f2SDouglas Gregor     case MMToken::Identifier:
2085718292f2SDouglas Gregor     case MMToken::LBrace:
20866ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
2087a686e1b0SDouglas Gregor     case MMToken::LSquare:
20882b82c2a5SDouglas Gregor     case MMToken::Period:
2089b53e5483SLawrence Crowl     case MMToken::PrivateKeyword:
2090718292f2SDouglas Gregor     case MMToken::RBrace:
2091a686e1b0SDouglas Gregor     case MMToken::RSquare:
20921fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
20932b82c2a5SDouglas Gregor     case MMToken::Star:
2094718292f2SDouglas Gregor     case MMToken::StringLiteral:
2095718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
2096ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
2097718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2098718292f2SDouglas Gregor       HadError = true;
2099718292f2SDouglas Gregor       consumeToken();
2100718292f2SDouglas Gregor       break;
2101718292f2SDouglas Gregor     }
2102718292f2SDouglas Gregor   } while (true);
2103718292f2SDouglas Gregor }
2104718292f2SDouglas Gregor 
2105963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) {
21064ddf2221SDouglas Gregor   llvm::DenseMap<const FileEntry *, bool>::iterator Known
21074ddf2221SDouglas Gregor     = ParsedModuleMap.find(File);
21084ddf2221SDouglas Gregor   if (Known != ParsedModuleMap.end())
21094ddf2221SDouglas Gregor     return Known->second;
21104ddf2221SDouglas Gregor 
211189929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
21121f76c4e8SManuel Klimek   FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
21131f76c4e8SManuel Klimek   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2114718292f2SDouglas Gregor   if (!Buffer)
21154ddf2221SDouglas Gregor     return ParsedModuleMap[File] = true;
2116718292f2SDouglas Gregor 
2117718292f2SDouglas Gregor   // Parse this module map file.
21181f76c4e8SManuel Klimek   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
2119*0761a8a0SDaniel Jasper   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File->getDir(),
2120963c5535SDouglas Gregor                          BuiltinIncludeDir, IsSystem);
2121718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
21224ddf2221SDouglas Gregor   ParsedModuleMap[File] = Result;
2123718292f2SDouglas Gregor   return Result;
2124718292f2SDouglas Gregor }
2125