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)
62fb912657SDouglas Gregor       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)
73fb912657SDouglas Gregor         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*1f76c4e8SManuel Klimek ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticConsumer &DC,
87b146baabSArgyrios Kyrtzidis                      const LangOptions &LangOpts, const TargetInfo *Target,
88b146baabSArgyrios Kyrtzidis                      HeaderSearch &HeaderInfo)
89*1f76c4e8SManuel Klimek     : SourceMgr(SourceMgr), LangOpts(LangOpts), Target(Target),
90*1f76c4e8SManuel Klimek       HeaderInfo(HeaderInfo), BuiltinIncludeDir(0), CompilingModule(0),
91*1f76c4e8SManuel Klimek       SourceModule(0) {
92c95d8192SDylan Noblesmith   IntrusiveRefCntPtr<DiagnosticIDs> DiagIDs(new DiagnosticIDs);
93c95d8192SDylan Noblesmith   Diags = IntrusiveRefCntPtr<DiagnosticsEngine>(
94811db4eaSDouglas Gregor             new DiagnosticsEngine(DiagIDs, new DiagnosticOptions));
956b930967SDouglas Gregor   Diags->setClient(new ForwardingDiagnosticConsumer(DC),
966b930967SDouglas Gregor                    /*ShouldOwnClient=*/true);
97*1f76c4e8SManuel Klimek   Diags->setSourceManager(&SourceMgr);
98718292f2SDouglas Gregor }
99718292f2SDouglas Gregor 
100718292f2SDouglas Gregor ModuleMap::~ModuleMap() {
1015acdf59eSDouglas Gregor   for (llvm::StringMap<Module *>::iterator I = Modules.begin(),
1025acdf59eSDouglas Gregor                                         IEnd = Modules.end();
1035acdf59eSDouglas Gregor        I != IEnd; ++I) {
1045acdf59eSDouglas Gregor     delete I->getValue();
1055acdf59eSDouglas Gregor   }
106718292f2SDouglas Gregor }
107718292f2SDouglas Gregor 
10889929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) {
10989929282SDouglas Gregor   assert((!this->Target || this->Target == &Target) &&
11089929282SDouglas Gregor          "Improper target override");
11189929282SDouglas Gregor   this->Target = &Target;
11289929282SDouglas Gregor }
11389929282SDouglas Gregor 
114056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier.
115056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
116056396aeSDouglas Gregor                                               SmallVectorImpl<char> &Buffer) {
117056396aeSDouglas Gregor   if (Name.empty())
118056396aeSDouglas Gregor     return Name;
119056396aeSDouglas Gregor 
120a7d03840SJordan Rose   if (!isValidIdentifier(Name)) {
121056396aeSDouglas Gregor     // If we don't already have something with the form of an identifier,
122056396aeSDouglas Gregor     // create a buffer with the sanitized name.
123056396aeSDouglas Gregor     Buffer.clear();
124a7d03840SJordan Rose     if (isDigit(Name[0]))
125056396aeSDouglas Gregor       Buffer.push_back('_');
126056396aeSDouglas Gregor     Buffer.reserve(Buffer.size() + Name.size());
127056396aeSDouglas Gregor     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
128a7d03840SJordan Rose       if (isIdentifierBody(Name[I]))
129056396aeSDouglas Gregor         Buffer.push_back(Name[I]);
130056396aeSDouglas Gregor       else
131056396aeSDouglas Gregor         Buffer.push_back('_');
132056396aeSDouglas Gregor     }
133056396aeSDouglas Gregor 
134056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
135056396aeSDouglas Gregor   }
136056396aeSDouglas Gregor 
137056396aeSDouglas Gregor   while (llvm::StringSwitch<bool>(Name)
138056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
139056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
140056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def"
141056396aeSDouglas Gregor            .Default(false)) {
142056396aeSDouglas Gregor     if (Name.data() != Buffer.data())
143056396aeSDouglas Gregor       Buffer.append(Name.begin(), Name.end());
144056396aeSDouglas Gregor     Buffer.push_back('_');
145056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
146056396aeSDouglas Gregor   }
147056396aeSDouglas Gregor 
148056396aeSDouglas Gregor   return Name;
149056396aeSDouglas Gregor }
150056396aeSDouglas Gregor 
15134d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin
15234d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system
15334d52749SDouglas Gregor /// headers.
15434d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) {
15534d52749SDouglas Gregor   return llvm::StringSwitch<bool>(FileName)
15634d52749SDouglas Gregor            .Case("float.h", true)
15734d52749SDouglas Gregor            .Case("iso646.h", true)
15834d52749SDouglas Gregor            .Case("limits.h", true)
15934d52749SDouglas Gregor            .Case("stdalign.h", true)
16034d52749SDouglas Gregor            .Case("stdarg.h", true)
16134d52749SDouglas Gregor            .Case("stdbool.h", true)
16234d52749SDouglas Gregor            .Case("stddef.h", true)
16334d52749SDouglas Gregor            .Case("stdint.h", true)
16434d52749SDouglas Gregor            .Case("tgmath.h", true)
16534d52749SDouglas Gregor            .Case("unwind.h", true)
16634d52749SDouglas Gregor            .Default(false);
16734d52749SDouglas Gregor }
16834d52749SDouglas Gregor 
16997da9178SDaniel Jasper ModuleMap::KnownHeader
17097da9178SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File,
17197da9178SDaniel Jasper                                Module *RequestingModule) {
17259527666SDouglas Gregor   HeadersMap::iterator Known = Headers.find(File);
1731fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
17497da9178SDaniel Jasper     ModuleMap::KnownHeader Result = KnownHeader();
1751fb5c3a6SDouglas Gregor 
17697da9178SDaniel Jasper     // Iterate over all modules that 'File' is part of to find the best fit.
17797da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
17897da9178SDaniel Jasper                                                 E = Known->second.end();
17997da9178SDaniel Jasper          I != E; ++I) {
18097da9178SDaniel Jasper       // Cannot use a module if the header is excluded or unavailable in it.
18197da9178SDaniel Jasper       if (I->getRole() == ModuleMap::ExcludedHeader ||
18297da9178SDaniel Jasper           !I->getModule()->isAvailable())
18397da9178SDaniel Jasper         continue;
18497da9178SDaniel Jasper 
18597da9178SDaniel Jasper       // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
18697da9178SDaniel Jasper       // module we are looking for.
18797da9178SDaniel Jasper       if (I->getModule() == RequestingModule)
18897da9178SDaniel Jasper         return *I;
18997da9178SDaniel Jasper 
19097da9178SDaniel Jasper       // If uses need to be specified explicitly, we are only allowed to return
19197da9178SDaniel Jasper       // modules that are explicitly used by the requesting module.
19297da9178SDaniel Jasper       if (RequestingModule && LangOpts.ModulesDeclUse &&
19397da9178SDaniel Jasper           std::find(RequestingModule->DirectUses.begin(),
19497da9178SDaniel Jasper                     RequestingModule->DirectUses.end(),
19597da9178SDaniel Jasper                     I->getModule()) == RequestingModule->DirectUses.end())
19697da9178SDaniel Jasper         continue;
19797da9178SDaniel Jasper       Result = *I;
19897da9178SDaniel Jasper       // If 'File' is a public header of this module, this is as good as we
19997da9178SDaniel Jasper       // are going to get.
20097da9178SDaniel Jasper       if (I->getRole() == ModuleMap::NormalHeader)
20197da9178SDaniel Jasper         break;
20297da9178SDaniel Jasper     }
20397da9178SDaniel Jasper     return Result;
2041fb5c3a6SDouglas Gregor   }
205ab0c8a84SDouglas Gregor 
20634d52749SDouglas Gregor   // If we've found a builtin header within Clang's builtin include directory,
20734d52749SDouglas Gregor   // load all of the module maps to see if it will get associated with a
20834d52749SDouglas Gregor   // specific module (e.g., in /usr/include).
20934d52749SDouglas Gregor   if (File->getDir() == BuiltinIncludeDir &&
21034d52749SDouglas Gregor       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
21164a1fa5cSDouglas Gregor     HeaderInfo.loadTopLevelSystemModules();
21234d52749SDouglas Gregor 
21334d52749SDouglas Gregor     // Check again.
21497da9178SDaniel Jasper     if (Headers.find(File) != Headers.end())
21597da9178SDaniel Jasper       return findModuleForHeader(File, RequestingModule);
21634d52749SDouglas Gregor   }
21734d52749SDouglas Gregor 
218b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
219f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
220e00c8b20SDouglas Gregor 
22174260502SDouglas Gregor   // Note: as an egregious but useful hack we use the real path here, because
22274260502SDouglas Gregor   // frameworks moving from top-level frameworks to embedded frameworks tend
22374260502SDouglas Gregor   // to be symlinked from the top-level location to the embedded location,
22474260502SDouglas Gregor   // and we need to resolve lookups as if we had found the embedded location.
225*1f76c4e8SManuel Klimek   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
226a89c5ac4SDouglas Gregor 
227a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
228a89c5ac4SDouglas Gregor   // an umbrella header.
229b65dbfffSDouglas Gregor   do {
230a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
231a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
232a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
233a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
234930a85ccSDouglas Gregor 
235930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
23673141fa9SDouglas Gregor       // directory.
237930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
23873141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
239930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
240930a85ccSDouglas Gregor 
241930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
242a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
243a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
244a89c5ac4SDouglas Gregor         // the actual header is located.
2459458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
2469458f82dSDouglas Gregor 
2477033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
248a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
249056396aeSDouglas Gregor           SmallString<32> NameBuf;
250056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
251056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
252056396aeSDouglas Gregor                              NameBuf);
253a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2549458f82dSDouglas Gregor                                       Explicit).first;
255a89c5ac4SDouglas Gregor 
256a89c5ac4SDouglas Gregor           // Associate the module and the directory.
257a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
258a89c5ac4SDouglas Gregor 
259a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
260a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
261930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
262a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
263a89c5ac4SDouglas Gregor         }
264a89c5ac4SDouglas Gregor 
265a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
266056396aeSDouglas Gregor         SmallString<32> NameBuf;
267056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
268056396aeSDouglas Gregor                            llvm::sys::path::stem(File->getName()), NameBuf);
269a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2709458f82dSDouglas Gregor                                     Explicit).first;
2713c5305c1SArgyrios Kyrtzidis         Result->addTopHeader(File);
272a89c5ac4SDouglas Gregor 
273a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
274a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
275930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
276a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
277a89c5ac4SDouglas Gregor       } else {
278a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
279a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
280a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
281a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
282a89c5ac4SDouglas Gregor       }
283a89c5ac4SDouglas Gregor 
28497da9178SDaniel Jasper       Headers[File].push_back(KnownHeader(Result, NormalHeader));
2851fb5c3a6SDouglas Gregor 
2861fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
2871fb5c3a6SDouglas Gregor       // that it maps to anything.
2881fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
289b53e5483SLawrence Crowl         return KnownHeader();
2901fb5c3a6SDouglas Gregor 
29197da9178SDaniel Jasper       return Headers[File].back();
292a89c5ac4SDouglas Gregor     }
293a89c5ac4SDouglas Gregor 
294a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
295a89c5ac4SDouglas Gregor 
296b65dbfffSDouglas Gregor     // Retrieve our parent path.
297b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
298b65dbfffSDouglas Gregor     if (DirName.empty())
299b65dbfffSDouglas Gregor       break;
300b65dbfffSDouglas Gregor 
301b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
302*1f76c4e8SManuel Klimek     Dir = SourceMgr.getFileManager().getDirectory(DirName);
303a89c5ac4SDouglas Gregor   } while (Dir);
304b65dbfffSDouglas Gregor 
305b53e5483SLawrence Crowl   return KnownHeader();
306ab0c8a84SDouglas Gregor }
307ab0c8a84SDouglas Gregor 
308e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
309e4412640SArgyrios Kyrtzidis   HeadersMap::const_iterator Known = Headers.find(Header);
31097da9178SDaniel Jasper   if (Known != Headers.end()) {
31197da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator
31297da9178SDaniel Jasper              I = Known->second.begin(),
31397da9178SDaniel Jasper              E = Known->second.end();
31497da9178SDaniel Jasper          I != E; ++I) {
31597da9178SDaniel Jasper       if (I->isAvailable())
31697da9178SDaniel Jasper         return false;
31797da9178SDaniel Jasper     }
31897da9178SDaniel Jasper     return true;
31997da9178SDaniel Jasper   }
3201fb5c3a6SDouglas Gregor 
3211fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
322f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
3231fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
3241fb5c3a6SDouglas Gregor 
3251fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
3261fb5c3a6SDouglas Gregor   // an umbrella header.
3271fb5c3a6SDouglas Gregor   do {
328e4412640SArgyrios Kyrtzidis     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
3291fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
3301fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
3311fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
3321fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
3331fb5c3a6SDouglas Gregor         return true;
3341fb5c3a6SDouglas Gregor 
3351fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
3361fb5c3a6SDouglas Gregor       // directory.
3371fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
3381fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
3391fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
3401fb5c3a6SDouglas Gregor 
3411fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
3421fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
3431fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
344056396aeSDouglas Gregor           SmallString<32> NameBuf;
345056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
346056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
347056396aeSDouglas Gregor                              NameBuf);
3481fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
3491fb5c3a6SDouglas Gregor           if (!Found)
3501fb5c3a6SDouglas Gregor             return false;
3511fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
3521fb5c3a6SDouglas Gregor             return true;
3531fb5c3a6SDouglas Gregor         }
3541fb5c3a6SDouglas Gregor 
3551fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
356056396aeSDouglas Gregor         SmallString<32> NameBuf;
357056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
358056396aeSDouglas Gregor                            llvm::sys::path::stem(Header->getName()),
359056396aeSDouglas Gregor                            NameBuf);
3601fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
3611fb5c3a6SDouglas Gregor         if (!Found)
3621fb5c3a6SDouglas Gregor           return false;
3631fb5c3a6SDouglas Gregor       }
3641fb5c3a6SDouglas Gregor 
3651fb5c3a6SDouglas Gregor       return !Found->isAvailable();
3661fb5c3a6SDouglas Gregor     }
3671fb5c3a6SDouglas Gregor 
3681fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
3691fb5c3a6SDouglas Gregor 
3701fb5c3a6SDouglas Gregor     // Retrieve our parent path.
3711fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
3721fb5c3a6SDouglas Gregor     if (DirName.empty())
3731fb5c3a6SDouglas Gregor       break;
3741fb5c3a6SDouglas Gregor 
3751fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
376*1f76c4e8SManuel Klimek     Dir = SourceMgr.getFileManager().getDirectory(DirName);
3771fb5c3a6SDouglas Gregor   } while (Dir);
3781fb5c3a6SDouglas Gregor 
3791fb5c3a6SDouglas Gregor   return false;
3801fb5c3a6SDouglas Gregor }
3811fb5c3a6SDouglas Gregor 
382e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const {
383e4412640SArgyrios Kyrtzidis   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
38488bdfb0eSDouglas Gregor   if (Known != Modules.end())
38588bdfb0eSDouglas Gregor     return Known->getValue();
38688bdfb0eSDouglas Gregor 
38788bdfb0eSDouglas Gregor   return 0;
38888bdfb0eSDouglas Gregor }
38988bdfb0eSDouglas Gregor 
390e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
391e4412640SArgyrios Kyrtzidis                                            Module *Context) const {
3922b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
3932b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
3942b82c2a5SDouglas Gregor       return Sub;
3952b82c2a5SDouglas Gregor   }
3962b82c2a5SDouglas Gregor 
3972b82c2a5SDouglas Gregor   return findModule(Name);
3982b82c2a5SDouglas Gregor }
3992b82c2a5SDouglas Gregor 
400e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
4012b82c2a5SDouglas Gregor   if (!Context)
4022b82c2a5SDouglas Gregor     return findModule(Name);
4032b82c2a5SDouglas Gregor 
404eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
4052b82c2a5SDouglas Gregor }
4062b82c2a5SDouglas Gregor 
407de3ef502SDouglas Gregor std::pair<Module *, bool>
40869021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
40969021974SDouglas Gregor                               bool IsExplicit) {
41069021974SDouglas Gregor   // Try to find an existing module with this name.
411eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
412eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
41369021974SDouglas Gregor 
41469021974SDouglas Gregor   // Create a new module with this name.
41569021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
41669021974SDouglas Gregor                               IsExplicit);
417ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == Name) {
418ba7f2f71SDaniel Jasper     SourceModule = Result;
419ba7f2f71SDaniel Jasper     SourceModuleName = Name;
420ba7f2f71SDaniel Jasper   }
4216f722b4eSArgyrios Kyrtzidis   if (!Parent) {
42269021974SDouglas Gregor     Modules[Name] = Result;
4236f722b4eSArgyrios Kyrtzidis     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
4246f722b4eSArgyrios Kyrtzidis         Name == LangOpts.CurrentModule) {
4256f722b4eSArgyrios Kyrtzidis       CompilingModule = Result;
4266f722b4eSArgyrios Kyrtzidis     }
4276f722b4eSArgyrios Kyrtzidis   }
42869021974SDouglas Gregor   return std::make_pair(Result, true);
42969021974SDouglas Gregor }
43069021974SDouglas Gregor 
4319194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
432e4412640SArgyrios Kyrtzidis                                         StringRef Name, bool &IsSystem) const {
4339194a91dSDouglas Gregor   // Check whether we have already looked into the parent directory
4349194a91dSDouglas Gregor   // for a module map.
435e4412640SArgyrios Kyrtzidis   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4369194a91dSDouglas Gregor     inferred = InferredDirectories.find(ParentDir);
4379194a91dSDouglas Gregor   if (inferred == InferredDirectories.end())
4389194a91dSDouglas Gregor     return false;
4399194a91dSDouglas Gregor 
4409194a91dSDouglas Gregor   if (!inferred->second.InferModules)
4419194a91dSDouglas Gregor     return false;
4429194a91dSDouglas Gregor 
4439194a91dSDouglas Gregor   // We're allowed to infer for this directory, but make sure it's okay
4449194a91dSDouglas Gregor   // to infer this particular module.
4459194a91dSDouglas Gregor   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
4469194a91dSDouglas Gregor                             inferred->second.ExcludedModules.end(),
4479194a91dSDouglas Gregor                             Name) == inferred->second.ExcludedModules.end();
4489194a91dSDouglas Gregor 
4499194a91dSDouglas Gregor   if (canInfer && inferred->second.InferSystemModules)
4509194a91dSDouglas Gregor     IsSystem = true;
4519194a91dSDouglas Gregor 
4529194a91dSDouglas Gregor   return canInfer;
4539194a91dSDouglas Gregor }
4549194a91dSDouglas Gregor 
45511dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we
45611dfe6feSDouglas Gregor /// should link.
45711dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
45811dfe6feSDouglas Gregor                                FileManager &FileMgr) {
45911dfe6feSDouglas Gregor   assert(Mod->IsFramework && "Can only infer linking for framework modules");
46011dfe6feSDouglas Gregor   assert(!Mod->isSubFramework() &&
46111dfe6feSDouglas Gregor          "Can only infer linking for top-level frameworks");
46211dfe6feSDouglas Gregor 
46311dfe6feSDouglas Gregor   SmallString<128> LibName;
46411dfe6feSDouglas Gregor   LibName += FrameworkDir->getName();
46511dfe6feSDouglas Gregor   llvm::sys::path::append(LibName, Mod->Name);
46611dfe6feSDouglas Gregor   if (FileMgr.getFile(LibName)) {
46711dfe6feSDouglas Gregor     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
46811dfe6feSDouglas Gregor                                                      /*IsFramework=*/true));
46911dfe6feSDouglas Gregor   }
47011dfe6feSDouglas Gregor }
47111dfe6feSDouglas Gregor 
472de3ef502SDouglas Gregor Module *
47356c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
474e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
475a686e1b0SDouglas Gregor                                 bool IsSystem,
476e89dbc1dSDouglas Gregor                                 Module *Parent) {
47756c64013SDouglas Gregor   // Check whether we've already found this module.
478e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
479e89dbc1dSDouglas Gregor     return Mod;
480e89dbc1dSDouglas Gregor 
481*1f76c4e8SManuel Klimek   FileManager &FileMgr = SourceMgr.getFileManager();
48256c64013SDouglas Gregor 
4839194a91dSDouglas Gregor   // If the framework has a parent path from which we're allowed to infer
4849194a91dSDouglas Gregor   // a framework module, do so.
4859194a91dSDouglas Gregor   if (!Parent) {
4864ddf2221SDouglas Gregor     // Determine whether we're allowed to infer a module map.
487e00c8b20SDouglas Gregor 
4884ddf2221SDouglas Gregor     // Note: as an egregious but useful hack we use the real path here, because
4894ddf2221SDouglas Gregor     // we might be looking at an embedded framework that symlinks out to a
4904ddf2221SDouglas Gregor     // top-level framework, and we need to infer as if we were naming the
4914ddf2221SDouglas Gregor     // top-level framework.
492e00c8b20SDouglas Gregor     StringRef FrameworkDirName
493*1f76c4e8SManuel Klimek       = SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
4944ddf2221SDouglas Gregor 
4959194a91dSDouglas Gregor     bool canInfer = false;
4964ddf2221SDouglas Gregor     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
4979194a91dSDouglas Gregor       // Figure out the parent path.
4984ddf2221SDouglas Gregor       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
4999194a91dSDouglas Gregor       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
5009194a91dSDouglas Gregor         // Check whether we have already looked into the parent directory
5019194a91dSDouglas Gregor         // for a module map.
502e4412640SArgyrios Kyrtzidis         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
5039194a91dSDouglas Gregor           inferred = InferredDirectories.find(ParentDir);
5049194a91dSDouglas Gregor         if (inferred == InferredDirectories.end()) {
5059194a91dSDouglas Gregor           // We haven't looked here before. Load a module map, if there is
5069194a91dSDouglas Gregor           // one.
5079194a91dSDouglas Gregor           SmallString<128> ModMapPath = Parent;
5089194a91dSDouglas Gregor           llvm::sys::path::append(ModMapPath, "module.map");
5099194a91dSDouglas Gregor           if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
510963c5535SDouglas Gregor             parseModuleMapFile(ModMapFile, IsSystem);
5119194a91dSDouglas Gregor             inferred = InferredDirectories.find(ParentDir);
5129194a91dSDouglas Gregor           }
5139194a91dSDouglas Gregor 
5149194a91dSDouglas Gregor           if (inferred == InferredDirectories.end())
5159194a91dSDouglas Gregor             inferred = InferredDirectories.insert(
5169194a91dSDouglas Gregor                          std::make_pair(ParentDir, InferredDirectory())).first;
5179194a91dSDouglas Gregor         }
5189194a91dSDouglas Gregor 
5199194a91dSDouglas Gregor         if (inferred->second.InferModules) {
5209194a91dSDouglas Gregor           // We're allowed to infer for this directory, but make sure it's okay
5219194a91dSDouglas Gregor           // to infer this particular module.
5224ddf2221SDouglas Gregor           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
5239194a91dSDouglas Gregor           canInfer = std::find(inferred->second.ExcludedModules.begin(),
5249194a91dSDouglas Gregor                                inferred->second.ExcludedModules.end(),
5259194a91dSDouglas Gregor                                Name) == inferred->second.ExcludedModules.end();
5269194a91dSDouglas Gregor 
5279194a91dSDouglas Gregor           if (inferred->second.InferSystemModules)
5289194a91dSDouglas Gregor             IsSystem = true;
5299194a91dSDouglas Gregor         }
5309194a91dSDouglas Gregor       }
5319194a91dSDouglas Gregor     }
5329194a91dSDouglas Gregor 
5339194a91dSDouglas Gregor     // If we're not allowed to infer a framework module, don't.
5349194a91dSDouglas Gregor     if (!canInfer)
5359194a91dSDouglas Gregor       return 0;
5369194a91dSDouglas Gregor   }
5379194a91dSDouglas Gregor 
5389194a91dSDouglas Gregor 
53956c64013SDouglas Gregor   // Look for an umbrella header.
5402c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
54117381a06SBenjamin Kramer   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
542e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
54356c64013SDouglas Gregor 
54456c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
54556c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
54656c64013SDouglas Gregor   // idea.
54756c64013SDouglas Gregor   if (!UmbrellaHeader)
54856c64013SDouglas Gregor     return 0;
54956c64013SDouglas Gregor 
550e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
551e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
552ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == ModuleName) {
553ba7f2f71SDaniel Jasper     SourceModule = Result;
554ba7f2f71SDaniel Jasper     SourceModuleName = ModuleName;
555ba7f2f71SDaniel Jasper   }
556a686e1b0SDouglas Gregor   if (IsSystem)
557a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
558a686e1b0SDouglas Gregor 
559eb90e830SDouglas Gregor   if (!Parent)
560e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
561e89dbc1dSDouglas Gregor 
562322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
56373141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
56497da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
5654dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
566d8bd7537SDouglas Gregor 
567d8bd7537SDouglas Gregor   // export *
568d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
569d8bd7537SDouglas Gregor 
570a89c5ac4SDouglas Gregor   // module * { export * }
571a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
572a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
573a89c5ac4SDouglas Gregor 
574e89dbc1dSDouglas Gregor   // Look for subframeworks.
575e89dbc1dSDouglas Gregor   llvm::error_code EC;
5762c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
577ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
578e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
5792d4d8cb3SBenjamin Kramer   llvm::sys::path::native(SubframeworksDirName);
580ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
5812d4d8cb3SBenjamin Kramer          Dir(SubframeworksDirName.str(), EC), DirEnd;
582e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
583e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
584e89dbc1dSDouglas Gregor       continue;
585f2161a70SDouglas Gregor 
586e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
587e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
58807c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
58907c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
59007c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
59107c22b78SDouglas Gregor       // out to a top-level framework.
592e00c8b20SDouglas Gregor       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
59307c22b78SDouglas Gregor       bool FoundParent = false;
59407c22b78SDouglas Gregor       do {
59507c22b78SDouglas Gregor         // Get the parent directory name.
59607c22b78SDouglas Gregor         SubframeworkDirName
59707c22b78SDouglas Gregor           = llvm::sys::path::parent_path(SubframeworkDirName);
59807c22b78SDouglas Gregor         if (SubframeworkDirName.empty())
59907c22b78SDouglas Gregor           break;
60007c22b78SDouglas Gregor 
60107c22b78SDouglas Gregor         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
60207c22b78SDouglas Gregor           FoundParent = true;
60307c22b78SDouglas Gregor           break;
60407c22b78SDouglas Gregor         }
60507c22b78SDouglas Gregor       } while (true);
60607c22b78SDouglas Gregor 
60707c22b78SDouglas Gregor       if (!FoundParent)
60807c22b78SDouglas Gregor         continue;
60907c22b78SDouglas Gregor 
610e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
611056396aeSDouglas Gregor       SmallString<32> NameBuf;
612056396aeSDouglas Gregor       inferFrameworkModule(sanitizeFilenameAsIdentifier(
613056396aeSDouglas Gregor                              llvm::sys::path::stem(Dir->path()), NameBuf),
614056396aeSDouglas Gregor                            SubframeworkDir, IsSystem, Result);
615e89dbc1dSDouglas Gregor     }
616e89dbc1dSDouglas Gregor   }
617e89dbc1dSDouglas Gregor 
61811dfe6feSDouglas Gregor   // If the module is a top-level framework, automatically link against the
61911dfe6feSDouglas Gregor   // framework.
62011dfe6feSDouglas Gregor   if (!Result->isSubFramework()) {
62111dfe6feSDouglas Gregor     inferFrameworkLink(Result, FrameworkDir, FileMgr);
62211dfe6feSDouglas Gregor   }
62311dfe6feSDouglas Gregor 
62456c64013SDouglas Gregor   return Result;
62556c64013SDouglas Gregor }
62656c64013SDouglas Gregor 
627a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
62897da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
62973141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
6307033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
631a89c5ac4SDouglas Gregor }
632a89c5ac4SDouglas Gregor 
633524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
634524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
635524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
636524e33e1SDouglas Gregor }
637524e33e1SDouglas Gregor 
63859527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
639b53e5483SLawrence Crowl                           ModuleHeaderRole Role) {
640b53e5483SLawrence Crowl   if (Role == ExcludedHeader) {
64159527666SDouglas Gregor     Mod->ExcludedHeaders.push_back(Header);
642b146baabSArgyrios Kyrtzidis   } else {
643b53e5483SLawrence Crowl     if (Role == PrivateHeader)
644b53e5483SLawrence Crowl       Mod->PrivateHeaders.push_back(Header);
645b53e5483SLawrence Crowl     else
646b53e5483SLawrence Crowl       Mod->NormalHeaders.push_back(Header);
6476f722b4eSArgyrios Kyrtzidis     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
648b53e5483SLawrence Crowl     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
649b146baabSArgyrios Kyrtzidis   }
65097da9178SDaniel Jasper   Headers[Header].push_back(KnownHeader(Mod, Role));
651a89c5ac4SDouglas Gregor }
652a89c5ac4SDouglas Gregor 
653514b636aSDouglas Gregor const FileEntry *
654e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const {
655*1f76c4e8SManuel Klimek   if (Module->DefinitionLoc.isInvalid())
656514b636aSDouglas Gregor     return 0;
657514b636aSDouglas Gregor 
658*1f76c4e8SManuel Klimek   return SourceMgr.getFileEntryForID(
659*1f76c4e8SManuel Klimek            SourceMgr.getFileID(Module->DefinitionLoc));
660514b636aSDouglas Gregor }
661514b636aSDouglas Gregor 
662718292f2SDouglas Gregor void ModuleMap::dump() {
663718292f2SDouglas Gregor   llvm::errs() << "Modules:";
664718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
665718292f2SDouglas Gregor                                         MEnd = Modules.end();
666718292f2SDouglas Gregor        M != MEnd; ++M)
667d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
668718292f2SDouglas Gregor 
669718292f2SDouglas Gregor   llvm::errs() << "Headers:";
67059527666SDouglas Gregor   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
671718292f2SDouglas Gregor        H != HEnd; ++H) {
67297da9178SDaniel Jasper     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
67397da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
67497da9178SDaniel Jasper                                                       E = H->second.end();
67597da9178SDaniel Jasper          I != E; ++I) {
67697da9178SDaniel Jasper       if (I != H->second.begin())
67797da9178SDaniel Jasper         llvm::errs() << ",";
67897da9178SDaniel Jasper       llvm::errs() << I->getModule()->getFullModuleName();
67997da9178SDaniel Jasper     }
68097da9178SDaniel Jasper     llvm::errs() << "\n";
681718292f2SDouglas Gregor   }
682718292f2SDouglas Gregor }
683718292f2SDouglas Gregor 
6842b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
6852b82c2a5SDouglas Gregor   bool HadError = false;
6862b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
6872b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
6882b82c2a5SDouglas Gregor                                               Complain);
689f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
6902b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
6912b82c2a5SDouglas Gregor     else
6922b82c2a5SDouglas Gregor       HadError = true;
6932b82c2a5SDouglas Gregor   }
6942b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
6952b82c2a5SDouglas Gregor   return HadError;
6962b82c2a5SDouglas Gregor }
6972b82c2a5SDouglas Gregor 
698ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
699ba7f2f71SDaniel Jasper   bool HadError = false;
700ba7f2f71SDaniel Jasper   for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) {
701ba7f2f71SDaniel Jasper     Module *DirectUse =
702ba7f2f71SDaniel Jasper         resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain);
703ba7f2f71SDaniel Jasper     if (DirectUse)
704ba7f2f71SDaniel Jasper       Mod->DirectUses.push_back(DirectUse);
705ba7f2f71SDaniel Jasper     else
706ba7f2f71SDaniel Jasper       HadError = true;
707ba7f2f71SDaniel Jasper   }
708ba7f2f71SDaniel Jasper   Mod->UnresolvedDirectUses.clear();
709ba7f2f71SDaniel Jasper   return HadError;
710ba7f2f71SDaniel Jasper }
711ba7f2f71SDaniel Jasper 
712fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
713fb912657SDouglas Gregor   bool HadError = false;
714fb912657SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
715fb912657SDouglas Gregor     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
716fb912657SDouglas Gregor                                        Mod, Complain);
717fb912657SDouglas Gregor     if (!OtherMod) {
718fb912657SDouglas Gregor       HadError = true;
719fb912657SDouglas Gregor       continue;
720fb912657SDouglas Gregor     }
721fb912657SDouglas Gregor 
722fb912657SDouglas Gregor     Module::Conflict Conflict;
723fb912657SDouglas Gregor     Conflict.Other = OtherMod;
724fb912657SDouglas Gregor     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
725fb912657SDouglas Gregor     Mod->Conflicts.push_back(Conflict);
726fb912657SDouglas Gregor   }
727fb912657SDouglas Gregor   Mod->UnresolvedConflicts.clear();
728fb912657SDouglas Gregor   return HadError;
729fb912657SDouglas Gregor }
730fb912657SDouglas Gregor 
7310093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
7320093b3c7SDouglas Gregor   if (Loc.isInvalid())
7330093b3c7SDouglas Gregor     return 0;
7340093b3c7SDouglas Gregor 
7350093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
7360093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
7370093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
7380093b3c7SDouglas Gregor     return 0;
7390093b3c7SDouglas Gregor 
7400093b3c7SDouglas Gregor 
7410093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
7420093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
743224d8a74SDouglas Gregor 
744224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
745224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
746224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
747b53e5483SLawrence Crowl     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
748224d8a74SDouglas Gregor       return Mod;
749224d8a74SDouglas Gregor 
750224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
751224d8a74SDouglas Gregor     // any included header has an associated module.
752224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
753224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
7540093b3c7SDouglas Gregor       return 0;
7550093b3c7SDouglas Gregor 
756224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
757224d8a74SDouglas Gregor   }
758224d8a74SDouglas Gregor 
759224d8a74SDouglas Gregor   return 0;
7600093b3c7SDouglas Gregor }
7610093b3c7SDouglas Gregor 
762718292f2SDouglas Gregor //----------------------------------------------------------------------------//
763718292f2SDouglas Gregor // Module map file parser
764718292f2SDouglas Gregor //----------------------------------------------------------------------------//
765718292f2SDouglas Gregor 
766718292f2SDouglas Gregor namespace clang {
767718292f2SDouglas Gregor   /// \brief A token in a module map file.
768718292f2SDouglas Gregor   struct MMToken {
769718292f2SDouglas Gregor     enum TokenKind {
7701fb5c3a6SDouglas Gregor       Comma,
77135b13eceSDouglas Gregor       ConfigMacros,
772fb912657SDouglas Gregor       Conflict,
773718292f2SDouglas Gregor       EndOfFile,
774718292f2SDouglas Gregor       HeaderKeyword,
775718292f2SDouglas Gregor       Identifier,
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 
970718292f2SDouglas Gregor   case tok::string_literal: {
971d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
972d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
973d67aea28SRichard Smith       HadError = true;
974d67aea28SRichard Smith       goto retry;
975d67aea28SRichard Smith     }
976d67aea28SRichard Smith 
977718292f2SDouglas Gregor     // Parse the string literal.
978718292f2SDouglas Gregor     LangOptions LangOpts;
979718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
980718292f2SDouglas Gregor     if (StringLiteral.hadError)
981718292f2SDouglas Gregor       goto retry;
982718292f2SDouglas Gregor 
983718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
984718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
985718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
986718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
987718292f2SDouglas Gregor     Saved[Length] = 0;
988718292f2SDouglas Gregor 
989718292f2SDouglas Gregor     // Form the token.
990718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
991718292f2SDouglas Gregor     Tok.StringData = Saved;
992718292f2SDouglas Gregor     Tok.StringLength = Length;
993718292f2SDouglas Gregor     break;
994718292f2SDouglas Gregor   }
995718292f2SDouglas Gregor 
996718292f2SDouglas Gregor   case tok::comment:
997718292f2SDouglas Gregor     goto retry;
998718292f2SDouglas Gregor 
999718292f2SDouglas Gregor   default:
1000718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
1001718292f2SDouglas Gregor     HadError = true;
1002718292f2SDouglas Gregor     goto retry;
1003718292f2SDouglas Gregor   }
1004718292f2SDouglas Gregor 
1005718292f2SDouglas Gregor   return Result;
1006718292f2SDouglas Gregor }
1007718292f2SDouglas Gregor 
1008718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1009718292f2SDouglas Gregor   unsigned braceDepth = 0;
1010a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
1011718292f2SDouglas Gregor   do {
1012718292f2SDouglas Gregor     switch (Tok.Kind) {
1013718292f2SDouglas Gregor     case MMToken::EndOfFile:
1014718292f2SDouglas Gregor       return;
1015718292f2SDouglas Gregor 
1016718292f2SDouglas Gregor     case MMToken::LBrace:
1017a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1018718292f2SDouglas Gregor         return;
1019718292f2SDouglas Gregor 
1020718292f2SDouglas Gregor       ++braceDepth;
1021718292f2SDouglas Gregor       break;
1022718292f2SDouglas Gregor 
1023a686e1b0SDouglas Gregor     case MMToken::LSquare:
1024a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1025a686e1b0SDouglas Gregor         return;
1026a686e1b0SDouglas Gregor 
1027a686e1b0SDouglas Gregor       ++squareDepth;
1028a686e1b0SDouglas Gregor       break;
1029a686e1b0SDouglas Gregor 
1030718292f2SDouglas Gregor     case MMToken::RBrace:
1031718292f2SDouglas Gregor       if (braceDepth > 0)
1032718292f2SDouglas Gregor         --braceDepth;
1033718292f2SDouglas Gregor       else if (Tok.is(K))
1034718292f2SDouglas Gregor         return;
1035718292f2SDouglas Gregor       break;
1036718292f2SDouglas Gregor 
1037a686e1b0SDouglas Gregor     case MMToken::RSquare:
1038a686e1b0SDouglas Gregor       if (squareDepth > 0)
1039a686e1b0SDouglas Gregor         --squareDepth;
1040a686e1b0SDouglas Gregor       else if (Tok.is(K))
1041a686e1b0SDouglas Gregor         return;
1042a686e1b0SDouglas Gregor       break;
1043a686e1b0SDouglas Gregor 
1044718292f2SDouglas Gregor     default:
1045a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1046718292f2SDouglas Gregor         return;
1047718292f2SDouglas Gregor       break;
1048718292f2SDouglas Gregor     }
1049718292f2SDouglas Gregor 
1050718292f2SDouglas Gregor    consumeToken();
1051718292f2SDouglas Gregor   } while (true);
1052718292f2SDouglas Gregor }
1053718292f2SDouglas Gregor 
1054e7ab3669SDouglas Gregor /// \brief Parse a module-id.
1055e7ab3669SDouglas Gregor ///
1056e7ab3669SDouglas Gregor ///   module-id:
1057e7ab3669SDouglas Gregor ///     identifier
1058e7ab3669SDouglas Gregor ///     identifier '.' module-id
1059e7ab3669SDouglas Gregor ///
1060e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
1061e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1062e7ab3669SDouglas Gregor   Id.clear();
1063e7ab3669SDouglas Gregor   do {
1064e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
1065e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1066e7ab3669SDouglas Gregor       consumeToken();
1067e7ab3669SDouglas Gregor     } else {
1068e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1069e7ab3669SDouglas Gregor       return true;
1070e7ab3669SDouglas Gregor     }
1071e7ab3669SDouglas Gregor 
1072e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
1073e7ab3669SDouglas Gregor       break;
1074e7ab3669SDouglas Gregor 
1075e7ab3669SDouglas Gregor     consumeToken();
1076e7ab3669SDouglas Gregor   } while (true);
1077e7ab3669SDouglas Gregor 
1078e7ab3669SDouglas Gregor   return false;
1079e7ab3669SDouglas Gregor }
1080e7ab3669SDouglas Gregor 
1081a686e1b0SDouglas Gregor namespace {
1082a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
1083a686e1b0SDouglas Gregor   enum AttributeKind {
1084a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
1085a686e1b0SDouglas Gregor     AT_unknown,
1086a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
108735b13eceSDouglas Gregor     AT_system,
108835b13eceSDouglas Gregor     /// \brief The 'exhaustive' attribute.
108935b13eceSDouglas Gregor     AT_exhaustive
1090a686e1b0SDouglas Gregor   };
1091a686e1b0SDouglas Gregor }
1092a686e1b0SDouglas Gregor 
1093718292f2SDouglas Gregor /// \brief Parse a module declaration.
1094718292f2SDouglas Gregor ///
1095718292f2SDouglas Gregor ///   module-declaration:
109697292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
1097a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1098a686e1b0SDouglas Gregor ///       { module-member* }
1099a686e1b0SDouglas Gregor ///
1100718292f2SDouglas Gregor ///   module-member:
11011fb5c3a6SDouglas Gregor ///     requires-declaration
1102718292f2SDouglas Gregor ///     header-declaration
1103e7ab3669SDouglas Gregor ///     submodule-declaration
11042b82c2a5SDouglas Gregor ///     export-declaration
11056ddfca91SDouglas Gregor ///     link-declaration
110673441091SDouglas Gregor ///
110773441091SDouglas Gregor ///   submodule-declaration:
110873441091SDouglas Gregor ///     module-declaration
110973441091SDouglas Gregor ///     inferred-submodule-declaration
1110718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
1111755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
111297292843SDaniel Jasper          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
111397292843SDaniel Jasper   if (Tok.is(MMToken::ExternKeyword)) {
111497292843SDaniel Jasper     parseExternModuleDecl();
111597292843SDaniel Jasper     return;
111697292843SDaniel Jasper   }
111797292843SDaniel Jasper 
1118f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
1119e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
1120718292f2SDouglas Gregor   bool Explicit = false;
1121f2161a70SDouglas Gregor   bool Framework = false;
1122755b2055SDouglas Gregor 
1123f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
1124f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
1125e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
1126f2161a70SDouglas Gregor     Explicit = true;
1127f2161a70SDouglas Gregor   }
1128f2161a70SDouglas Gregor 
1129f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
1130755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
1131755b2055SDouglas Gregor     consumeToken();
1132755b2055SDouglas Gregor     Framework = true;
1133755b2055SDouglas Gregor   }
1134718292f2SDouglas Gregor 
1135718292f2SDouglas Gregor   // Parse 'module' keyword.
1136718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
1137d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1138718292f2SDouglas Gregor     consumeToken();
1139718292f2SDouglas Gregor     HadError = true;
1140718292f2SDouglas Gregor     return;
1141718292f2SDouglas Gregor   }
1142718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
1143718292f2SDouglas Gregor 
114473441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
114573441091SDouglas Gregor   // Parse it.
114673441091SDouglas Gregor   if (Tok.is(MMToken::Star))
11479194a91dSDouglas Gregor     return parseInferredModuleDecl(Framework, Explicit);
114873441091SDouglas Gregor 
1149718292f2SDouglas Gregor   // Parse the module name.
1150e7ab3669SDouglas Gregor   ModuleId Id;
1151e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
1152718292f2SDouglas Gregor     HadError = true;
1153718292f2SDouglas Gregor     return;
1154718292f2SDouglas Gregor   }
1155e7ab3669SDouglas Gregor 
1156e7ab3669SDouglas Gregor   if (ActiveModule) {
1157e7ab3669SDouglas Gregor     if (Id.size() > 1) {
1158e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1159e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
1160e7ab3669SDouglas Gregor 
1161e7ab3669SDouglas Gregor       HadError = true;
1162e7ab3669SDouglas Gregor       return;
1163e7ab3669SDouglas Gregor     }
1164e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
1165e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
1166e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1167e7ab3669SDouglas Gregor     Explicit = false;
1168e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
1169e7ab3669SDouglas Gregor     HadError = true;
1170e7ab3669SDouglas Gregor   }
1171e7ab3669SDouglas Gregor 
1172e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
1173e7ab3669SDouglas Gregor   if (Id.size() > 1) {
1174e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
1175e7ab3669SDouglas Gregor     // is a submodule.
1176e7ab3669SDouglas Gregor     ActiveModule = 0;
1177e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1178e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1179e7ab3669SDouglas Gregor         ActiveModule = Next;
1180e7ab3669SDouglas Gregor         continue;
1181e7ab3669SDouglas Gregor       }
1182e7ab3669SDouglas Gregor 
1183e7ab3669SDouglas Gregor       if (ActiveModule) {
1184e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1185e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
1186e7ab3669SDouglas Gregor       } else {
1187e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1188e7ab3669SDouglas Gregor       }
1189e7ab3669SDouglas Gregor       HadError = true;
1190e7ab3669SDouglas Gregor       return;
1191e7ab3669SDouglas Gregor     }
1192e7ab3669SDouglas Gregor   }
1193e7ab3669SDouglas Gregor 
1194e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
1195e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
1196718292f2SDouglas Gregor 
1197a686e1b0SDouglas Gregor   // Parse the optional attribute list.
11984442605fSBill Wendling   Attributes Attrs;
11999194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
1200a686e1b0SDouglas Gregor 
1201718292f2SDouglas Gregor   // Parse the opening brace.
1202718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
1203718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1204718292f2SDouglas Gregor       << ModuleName;
1205718292f2SDouglas Gregor     HadError = true;
1206718292f2SDouglas Gregor     return;
1207718292f2SDouglas Gregor   }
1208718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
1209718292f2SDouglas Gregor 
1210718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
1211eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1212fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1213fcc54a3bSDouglas Gregor       // Skip the module definition.
1214fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
1215fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
1216fcc54a3bSDouglas Gregor         consumeToken();
1217fcc54a3bSDouglas Gregor       else {
1218fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1219fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1220fcc54a3bSDouglas Gregor         HadError = true;
1221fcc54a3bSDouglas Gregor       }
1222fcc54a3bSDouglas Gregor       return;
1223fcc54a3bSDouglas Gregor     }
1224fcc54a3bSDouglas Gregor 
1225718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1226718292f2SDouglas Gregor       << ModuleName;
1227eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1228718292f2SDouglas Gregor 
1229718292f2SDouglas Gregor     // Skip the module definition.
1230718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
1231718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
1232718292f2SDouglas Gregor       consumeToken();
1233718292f2SDouglas Gregor 
1234718292f2SDouglas Gregor     HadError = true;
1235718292f2SDouglas Gregor     return;
1236718292f2SDouglas Gregor   }
1237718292f2SDouglas Gregor 
1238718292f2SDouglas Gregor   // Start defining this module.
1239eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1240eb90e830SDouglas Gregor                                         Explicit).first;
1241eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
1242963c5535SDouglas Gregor   if (Attrs.IsSystem || IsSystem)
1243a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
1244718292f2SDouglas Gregor 
1245718292f2SDouglas Gregor   bool Done = false;
1246718292f2SDouglas Gregor   do {
1247718292f2SDouglas Gregor     switch (Tok.Kind) {
1248718292f2SDouglas Gregor     case MMToken::EndOfFile:
1249718292f2SDouglas Gregor     case MMToken::RBrace:
1250718292f2SDouglas Gregor       Done = true;
1251718292f2SDouglas Gregor       break;
1252718292f2SDouglas Gregor 
125335b13eceSDouglas Gregor     case MMToken::ConfigMacros:
125435b13eceSDouglas Gregor       parseConfigMacros();
125535b13eceSDouglas Gregor       break;
125635b13eceSDouglas Gregor 
1257fb912657SDouglas Gregor     case MMToken::Conflict:
1258fb912657SDouglas Gregor       parseConflict();
1259fb912657SDouglas Gregor       break;
1260fb912657SDouglas Gregor 
1261718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
126297292843SDaniel Jasper     case MMToken::ExternKeyword:
1263f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
1264718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1265718292f2SDouglas Gregor       parseModuleDecl();
1266718292f2SDouglas Gregor       break;
1267718292f2SDouglas Gregor 
12682b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
12692b82c2a5SDouglas Gregor       parseExportDecl();
12702b82c2a5SDouglas Gregor       break;
12712b82c2a5SDouglas Gregor 
1272ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
1273ba7f2f71SDaniel Jasper       parseUseDecl();
1274ba7f2f71SDaniel Jasper       break;
1275ba7f2f71SDaniel Jasper 
12761fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
12771fb5c3a6SDouglas Gregor       parseRequiresDecl();
12781fb5c3a6SDouglas Gregor       break;
12791fb5c3a6SDouglas Gregor 
1280524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
1281524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
1282524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
1283b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1284524e33e1SDouglas Gregor       else
1285524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
1286718292f2SDouglas Gregor       break;
1287524e33e1SDouglas Gregor     }
1288718292f2SDouglas Gregor 
128959527666SDouglas Gregor     case MMToken::ExcludeKeyword: {
129059527666SDouglas Gregor       SourceLocation ExcludeLoc = consumeToken();
129159527666SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword)) {
1292b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc);
129359527666SDouglas Gregor       } else {
129459527666SDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
129559527666SDouglas Gregor           << "exclude";
129659527666SDouglas Gregor       }
129759527666SDouglas Gregor       break;
129859527666SDouglas Gregor     }
129959527666SDouglas Gregor 
1300b53e5483SLawrence Crowl     case MMToken::PrivateKeyword: {
1301b53e5483SLawrence Crowl       SourceLocation PrivateLoc = consumeToken();
1302b53e5483SLawrence Crowl       if (Tok.is(MMToken::HeaderKeyword)) {
1303b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc);
1304b53e5483SLawrence Crowl       } else {
1305b53e5483SLawrence Crowl         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1306b53e5483SLawrence Crowl           << "private";
1307b53e5483SLawrence Crowl       }
1308b53e5483SLawrence Crowl       break;
1309b53e5483SLawrence Crowl     }
1310b53e5483SLawrence Crowl 
1311322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
1312b53e5483SLawrence Crowl       parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation());
1313718292f2SDouglas Gregor       break;
1314718292f2SDouglas Gregor 
13156ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
13166ddfca91SDouglas Gregor       parseLinkDecl();
13176ddfca91SDouglas Gregor       break;
13186ddfca91SDouglas Gregor 
1319718292f2SDouglas Gregor     default:
1320718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1321718292f2SDouglas Gregor       consumeToken();
1322718292f2SDouglas Gregor       break;
1323718292f2SDouglas Gregor     }
1324718292f2SDouglas Gregor   } while (!Done);
1325718292f2SDouglas Gregor 
1326718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1327718292f2SDouglas Gregor     consumeToken();
1328718292f2SDouglas Gregor   else {
1329718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1330718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1331718292f2SDouglas Gregor     HadError = true;
1332718292f2SDouglas Gregor   }
1333718292f2SDouglas Gregor 
133411dfe6feSDouglas Gregor   // If the active module is a top-level framework, and there are no link
133511dfe6feSDouglas Gregor   // libraries, automatically link against the framework.
133611dfe6feSDouglas Gregor   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
133711dfe6feSDouglas Gregor       ActiveModule->LinkLibraries.empty()) {
133811dfe6feSDouglas Gregor     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
133911dfe6feSDouglas Gregor   }
134011dfe6feSDouglas Gregor 
1341e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1342e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1343718292f2SDouglas Gregor }
1344718292f2SDouglas Gregor 
134597292843SDaniel Jasper /// \brief Parse an extern module declaration.
134697292843SDaniel Jasper ///
134797292843SDaniel Jasper ///   extern module-declaration:
134897292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
134997292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() {
135097292843SDaniel Jasper   assert(Tok.is(MMToken::ExternKeyword));
135197292843SDaniel Jasper   consumeToken(); // 'extern' keyword
135297292843SDaniel Jasper 
135397292843SDaniel Jasper   // Parse 'module' keyword.
135497292843SDaniel Jasper   if (!Tok.is(MMToken::ModuleKeyword)) {
135597292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
135697292843SDaniel Jasper     consumeToken();
135797292843SDaniel Jasper     HadError = true;
135897292843SDaniel Jasper     return;
135997292843SDaniel Jasper   }
136097292843SDaniel Jasper   consumeToken(); // 'module' keyword
136197292843SDaniel Jasper 
136297292843SDaniel Jasper   // Parse the module name.
136397292843SDaniel Jasper   ModuleId Id;
136497292843SDaniel Jasper   if (parseModuleId(Id)) {
136597292843SDaniel Jasper     HadError = true;
136697292843SDaniel Jasper     return;
136797292843SDaniel Jasper   }
136897292843SDaniel Jasper 
136997292843SDaniel Jasper   // Parse the referenced module map file name.
137097292843SDaniel Jasper   if (!Tok.is(MMToken::StringLiteral)) {
137197292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
137297292843SDaniel Jasper     HadError = true;
137397292843SDaniel Jasper     return;
137497292843SDaniel Jasper   }
137597292843SDaniel Jasper   std::string FileName = Tok.getString();
137697292843SDaniel Jasper   consumeToken(); // filename
137797292843SDaniel Jasper 
137897292843SDaniel Jasper   StringRef FileNameRef = FileName;
137997292843SDaniel Jasper   SmallString<128> ModuleMapFileName;
138097292843SDaniel Jasper   if (llvm::sys::path::is_relative(FileNameRef)) {
138197292843SDaniel Jasper     ModuleMapFileName += Directory->getName();
138297292843SDaniel Jasper     llvm::sys::path::append(ModuleMapFileName, FileName);
138397292843SDaniel Jasper     FileNameRef = ModuleMapFileName.str();
138497292843SDaniel Jasper   }
138597292843SDaniel Jasper   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
138697292843SDaniel Jasper     Map.parseModuleMapFile(File, /*IsSystem=*/false);
138797292843SDaniel Jasper }
138897292843SDaniel Jasper 
13891fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
13901fb5c3a6SDouglas Gregor ///
13911fb5c3a6SDouglas Gregor ///   requires-declaration:
13921fb5c3a6SDouglas Gregor ///     'requires' feature-list
13931fb5c3a6SDouglas Gregor ///
13941fb5c3a6SDouglas Gregor ///   feature-list:
13951fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
13961fb5c3a6SDouglas Gregor ///     identifier
13971fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
13981fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
13991fb5c3a6SDouglas Gregor 
14001fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
14011fb5c3a6SDouglas Gregor   consumeToken();
14021fb5c3a6SDouglas Gregor 
14031fb5c3a6SDouglas Gregor   // Parse the feature-list.
14041fb5c3a6SDouglas Gregor   do {
14051fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
14061fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
14071fb5c3a6SDouglas Gregor       HadError = true;
14081fb5c3a6SDouglas Gregor       return;
14091fb5c3a6SDouglas Gregor     }
14101fb5c3a6SDouglas Gregor 
14111fb5c3a6SDouglas Gregor     // Consume the feature name.
14121fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
14131fb5c3a6SDouglas Gregor     consumeToken();
14141fb5c3a6SDouglas Gregor 
14151fb5c3a6SDouglas Gregor     // Add this feature.
141689929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
14171fb5c3a6SDouglas Gregor 
14181fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
14191fb5c3a6SDouglas Gregor       break;
14201fb5c3a6SDouglas Gregor 
14211fb5c3a6SDouglas Gregor     // Consume the comma.
14221fb5c3a6SDouglas Gregor     consumeToken();
14231fb5c3a6SDouglas Gregor   } while (true);
14241fb5c3a6SDouglas Gregor }
14251fb5c3a6SDouglas Gregor 
1426f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1427f2161a70SDouglas Gregor /// subframework in which the given module lives.
1428bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1429f857950dSDmitri Gribenko                                     SmallVectorImpl<char> &Path) {
1430f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1431f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Paths;
1432f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1433f2161a70SDouglas Gregor     if (Mod->IsFramework)
1434f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1435f2161a70SDouglas Gregor   }
1436f2161a70SDouglas Gregor 
1437f2161a70SDouglas Gregor   if (Paths.empty())
1438f2161a70SDouglas Gregor     return;
1439f2161a70SDouglas Gregor 
1440f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
144117381a06SBenjamin Kramer   for (unsigned I = Paths.size() - 1; I != 0; --I)
144217381a06SBenjamin Kramer     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
1443f2161a70SDouglas Gregor }
1444f2161a70SDouglas Gregor 
1445718292f2SDouglas Gregor /// \brief Parse a header declaration.
1446718292f2SDouglas Gregor ///
1447718292f2SDouglas Gregor ///   header-declaration:
1448322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
144959527666SDouglas Gregor ///     'exclude'[opt] 'header' string-literal
1450b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
1451b53e5483SLawrence Crowl                                       SourceLocation LeadingLoc) {
1452718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
14531871ed3dSBenjamin Kramer   consumeToken();
1454718292f2SDouglas Gregor 
1455718292f2SDouglas Gregor   // Parse the header name.
1456718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1457718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1458718292f2SDouglas Gregor       << "header";
1459718292f2SDouglas Gregor     HadError = true;
1460718292f2SDouglas Gregor     return;
1461718292f2SDouglas Gregor   }
1462e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1463718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1464718292f2SDouglas Gregor 
1465524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1466b53e5483SLawrence Crowl   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
1467524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1468524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1469322f633cSDouglas Gregor     HadError = true;
1470322f633cSDouglas Gregor     return;
1471322f633cSDouglas Gregor   }
1472322f633cSDouglas Gregor 
14735257fc63SDouglas Gregor   // Look for this file.
1474e7ab3669SDouglas Gregor   const FileEntry *File = 0;
14753ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
14762c1dd271SDylan Noblesmith   SmallString<128> PathName;
1477e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1478e7ab3669SDouglas Gregor     PathName = FileName;
1479e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
14807033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
14817033127bSDouglas Gregor     PathName = Dir->getName();
14827033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
14837033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1484e7ab3669SDouglas Gregor   } else {
1485e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
14867033127bSDouglas Gregor     PathName = Directory->getName();
1487e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1488755b2055SDouglas Gregor 
1489f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1490f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1491755b2055SDouglas Gregor 
1492e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
149317381a06SBenjamin Kramer       llvm::sys::path::append(PathName, "Headers", FileName);
1494e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1495e7ab3669SDouglas Gregor 
1496e7ab3669SDouglas Gregor       if (!File) {
1497e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1498e7ab3669SDouglas Gregor         PathName.resize(PathLength);
149917381a06SBenjamin Kramer         llvm::sys::path::append(PathName, "PrivateHeaders", FileName);
1500e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1501e7ab3669SDouglas Gregor       }
1502e7ab3669SDouglas Gregor     } else {
1503e7ab3669SDouglas Gregor       // Lookup for normal headers.
1504e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1505e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
15063ec6663bSDouglas Gregor 
15073ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
15083ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
15093ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
1510b53e5483SLawrence Crowl       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
1511b53e5483SLawrence Crowl           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
1512b53e5483SLawrence Crowl           isBuiltinHeader(FileName)) {
15132c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
15143ec6663bSDouglas Gregor         llvm::sys::path::append(BuiltinPathName, FileName);
15153ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
15163ec6663bSDouglas Gregor 
15173ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
15183ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
15193ec6663bSDouglas Gregor         // up adding both (later).
15203ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
15213ec6663bSDouglas Gregor           File = BuiltinFile;
15223ec6663bSDouglas Gregor           BuiltinFile = 0;
15233ec6663bSDouglas Gregor         }
15243ec6663bSDouglas Gregor       }
1525e7ab3669SDouglas Gregor     }
1526e7ab3669SDouglas Gregor   }
15275257fc63SDouglas Gregor 
15285257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
15295257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1530e7ab3669SDouglas Gregor   if (File) {
153197da9178SDaniel Jasper     if (LeadingToken == MMToken::UmbrellaKeyword) {
1532322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
153359527666SDouglas Gregor       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1534b53e5483SLawrence Crowl         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
153559527666SDouglas Gregor           << UmbrellaModule->getFullModuleName();
1536322f633cSDouglas Gregor         HadError = true;
15375257fc63SDouglas Gregor       } else {
1538322f633cSDouglas Gregor         // Record this umbrella header.
1539322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1540322f633cSDouglas Gregor       }
1541322f633cSDouglas Gregor     } else {
1542322f633cSDouglas Gregor       // Record this header.
1543b53e5483SLawrence Crowl       ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
1544b53e5483SLawrence Crowl       if (LeadingToken == MMToken::ExcludeKeyword)
1545b53e5483SLawrence Crowl         Role = ModuleMap::ExcludedHeader;
1546b53e5483SLawrence Crowl       else if (LeadingToken == MMToken::PrivateKeyword)
1547b53e5483SLawrence Crowl         Role = ModuleMap::PrivateHeader;
1548b53e5483SLawrence Crowl       else
1549b53e5483SLawrence Crowl         assert(LeadingToken == MMToken::HeaderKeyword);
1550b53e5483SLawrence Crowl 
1551b53e5483SLawrence Crowl       Map.addHeader(ActiveModule, File, Role);
15523ec6663bSDouglas Gregor 
15533ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
15543ec6663bSDouglas Gregor       if (BuiltinFile)
1555b53e5483SLawrence Crowl         Map.addHeader(ActiveModule, BuiltinFile, Role);
15565257fc63SDouglas Gregor     }
1557b53e5483SLawrence Crowl   } else if (LeadingToken != MMToken::ExcludeKeyword) {
15584b27a64bSDouglas Gregor     // Ignore excluded header files. They're optional anyway.
15594b27a64bSDouglas Gregor 
15605257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1561b53e5483SLawrence Crowl       << (LeadingToken == MMToken::UmbrellaKeyword) << FileName;
15625257fc63SDouglas Gregor     HadError = true;
15635257fc63SDouglas Gregor   }
1564718292f2SDouglas Gregor }
1565718292f2SDouglas Gregor 
1566524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1567524e33e1SDouglas Gregor ///
1568524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1569524e33e1SDouglas Gregor ///     umbrella string-literal
1570524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1571524e33e1SDouglas Gregor   // Parse the directory name.
1572524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1573524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1574524e33e1SDouglas Gregor       << "umbrella";
1575524e33e1SDouglas Gregor     HadError = true;
1576524e33e1SDouglas Gregor     return;
1577524e33e1SDouglas Gregor   }
1578524e33e1SDouglas Gregor 
1579524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1580524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1581524e33e1SDouglas Gregor 
1582524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1583524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1584524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1585524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1586524e33e1SDouglas Gregor     HadError = true;
1587524e33e1SDouglas Gregor     return;
1588524e33e1SDouglas Gregor   }
1589524e33e1SDouglas Gregor 
1590524e33e1SDouglas Gregor   // Look for this file.
1591524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1592524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1593524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1594524e33e1SDouglas Gregor   else {
15952c1dd271SDylan Noblesmith     SmallString<128> PathName;
1596524e33e1SDouglas Gregor     PathName = Directory->getName();
1597524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1598524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1599524e33e1SDouglas Gregor   }
1600524e33e1SDouglas Gregor 
1601524e33e1SDouglas Gregor   if (!Dir) {
1602524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1603524e33e1SDouglas Gregor       << DirName;
1604524e33e1SDouglas Gregor     HadError = true;
1605524e33e1SDouglas Gregor     return;
1606524e33e1SDouglas Gregor   }
1607524e33e1SDouglas Gregor 
1608524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1609524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1610524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1611524e33e1SDouglas Gregor     HadError = true;
1612524e33e1SDouglas Gregor     return;
1613524e33e1SDouglas Gregor   }
1614524e33e1SDouglas Gregor 
1615524e33e1SDouglas Gregor   // Record this umbrella directory.
1616524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1617524e33e1SDouglas Gregor }
1618524e33e1SDouglas Gregor 
16192b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
16202b82c2a5SDouglas Gregor ///
16212b82c2a5SDouglas Gregor ///   export-declaration:
16222b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
16232b82c2a5SDouglas Gregor ///
16242b82c2a5SDouglas Gregor ///   wildcard-module-id:
16252b82c2a5SDouglas Gregor ///     identifier
16262b82c2a5SDouglas Gregor ///     '*'
16272b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
16282b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
16292b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
16302b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
16312b82c2a5SDouglas Gregor 
16322b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
16332b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
16342b82c2a5SDouglas Gregor   bool Wildcard = false;
16352b82c2a5SDouglas Gregor   do {
16362b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
16372b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
16382b82c2a5SDouglas Gregor                                               Tok.getLocation()));
16392b82c2a5SDouglas Gregor       consumeToken();
16402b82c2a5SDouglas Gregor 
16412b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
16422b82c2a5SDouglas Gregor         consumeToken();
16432b82c2a5SDouglas Gregor         continue;
16442b82c2a5SDouglas Gregor       }
16452b82c2a5SDouglas Gregor 
16462b82c2a5SDouglas Gregor       break;
16472b82c2a5SDouglas Gregor     }
16482b82c2a5SDouglas Gregor 
16492b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
16502b82c2a5SDouglas Gregor       Wildcard = true;
1651f5eedd05SDouglas Gregor       consumeToken();
16522b82c2a5SDouglas Gregor       break;
16532b82c2a5SDouglas Gregor     }
16542b82c2a5SDouglas Gregor 
1655ba7f2f71SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
16562b82c2a5SDouglas Gregor     HadError = true;
16572b82c2a5SDouglas Gregor     return;
16582b82c2a5SDouglas Gregor   } while (true);
16592b82c2a5SDouglas Gregor 
16602b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
16612b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
16622b82c2a5SDouglas Gregor   };
16632b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
16642b82c2a5SDouglas Gregor }
16652b82c2a5SDouglas Gregor 
1666ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration.
1667ba7f2f71SDaniel Jasper ///
1668ba7f2f71SDaniel Jasper ///   uses-declaration:
1669ba7f2f71SDaniel Jasper ///     'uses' wildcard-module-id
1670ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() {
1671ba7f2f71SDaniel Jasper   assert(Tok.is(MMToken::UseKeyword));
1672ba7f2f71SDaniel Jasper   consumeToken();
1673ba7f2f71SDaniel Jasper   // Parse the module-id.
1674ba7f2f71SDaniel Jasper   ModuleId ParsedModuleId;
1675ba7f2f71SDaniel Jasper 
1676ba7f2f71SDaniel Jasper   do {
1677ba7f2f71SDaniel Jasper     if (Tok.is(MMToken::Identifier)) {
1678ba7f2f71SDaniel Jasper       ParsedModuleId.push_back(
1679ba7f2f71SDaniel Jasper           std::make_pair(Tok.getString(), Tok.getLocation()));
1680ba7f2f71SDaniel Jasper       consumeToken();
1681ba7f2f71SDaniel Jasper 
1682ba7f2f71SDaniel Jasper       if (Tok.is(MMToken::Period)) {
1683ba7f2f71SDaniel Jasper         consumeToken();
1684ba7f2f71SDaniel Jasper         continue;
1685ba7f2f71SDaniel Jasper       }
1686ba7f2f71SDaniel Jasper 
1687ba7f2f71SDaniel Jasper       break;
1688ba7f2f71SDaniel Jasper     }
1689ba7f2f71SDaniel Jasper 
1690ba7f2f71SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
1691ba7f2f71SDaniel Jasper     HadError = true;
1692ba7f2f71SDaniel Jasper     return;
1693ba7f2f71SDaniel Jasper   } while (true);
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:
208059527666SDouglas Gregor     case MMToken::ExcludeKeyword:
20812b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
2082718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
2083718292f2SDouglas Gregor     case MMToken::Identifier:
2084718292f2SDouglas Gregor     case MMToken::LBrace:
20856ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
2086a686e1b0SDouglas Gregor     case MMToken::LSquare:
20872b82c2a5SDouglas Gregor     case MMToken::Period:
2088b53e5483SLawrence Crowl     case MMToken::PrivateKeyword:
2089718292f2SDouglas Gregor     case MMToken::RBrace:
2090a686e1b0SDouglas Gregor     case MMToken::RSquare:
20911fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
20922b82c2a5SDouglas Gregor     case MMToken::Star:
2093718292f2SDouglas Gregor     case MMToken::StringLiteral:
2094718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
2095ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
2096718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2097718292f2SDouglas Gregor       HadError = true;
2098718292f2SDouglas Gregor       consumeToken();
2099718292f2SDouglas Gregor       break;
2100718292f2SDouglas Gregor     }
2101718292f2SDouglas Gregor   } while (true);
2102718292f2SDouglas Gregor }
2103718292f2SDouglas Gregor 
2104963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) {
21054ddf2221SDouglas Gregor   llvm::DenseMap<const FileEntry *, bool>::iterator Known
21064ddf2221SDouglas Gregor     = ParsedModuleMap.find(File);
21074ddf2221SDouglas Gregor   if (Known != ParsedModuleMap.end())
21084ddf2221SDouglas Gregor     return Known->second;
21094ddf2221SDouglas Gregor 
211089929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
2111*1f76c4e8SManuel Klimek   FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
2112*1f76c4e8SManuel Klimek   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
2113718292f2SDouglas Gregor   if (!Buffer)
21144ddf2221SDouglas Gregor     return ParsedModuleMap[File] = true;
2115718292f2SDouglas Gregor 
2116718292f2SDouglas Gregor   // Parse this module map file.
2117*1f76c4e8SManuel Klimek   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
21181fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
2119*1f76c4e8SManuel Klimek   ModuleMapParser Parser(L, SourceMgr, Target, *Diags, *this, File->getDir(),
2120963c5535SDouglas Gregor                          BuiltinIncludeDir, IsSystem);
2121718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
2122718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
21234ddf2221SDouglas Gregor   ParsedModuleMap[File] = Result;
2124718292f2SDouglas Gregor   return Result;
2125718292f2SDouglas Gregor }
2126