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 
866b930967SDouglas Gregor ModuleMap::ModuleMap(FileManager &FileMgr, DiagnosticConsumer &DC,
87b146baabSArgyrios Kyrtzidis                      const LangOptions &LangOpts, const TargetInfo *Target,
88b146baabSArgyrios Kyrtzidis                      HeaderSearch &HeaderInfo)
89b146baabSArgyrios Kyrtzidis   : LangOpts(LangOpts), Target(Target), HeaderInfo(HeaderInfo),
909a59e2c6SDaniel Jasper     BuiltinIncludeDir(0), CompilingModule(0), SourceModule(0)
911fb5c3a6SDouglas Gregor {
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);
97718292f2SDouglas Gregor   SourceMgr = new SourceManager(*Diags, FileMgr);
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   }
1065acdf59eSDouglas Gregor 
107718292f2SDouglas Gregor   delete SourceMgr;
108718292f2SDouglas Gregor }
109718292f2SDouglas Gregor 
11089929282SDouglas Gregor void ModuleMap::setTarget(const TargetInfo &Target) {
11189929282SDouglas Gregor   assert((!this->Target || this->Target == &Target) &&
11289929282SDouglas Gregor          "Improper target override");
11389929282SDouglas Gregor   this->Target = &Target;
11489929282SDouglas Gregor }
11589929282SDouglas Gregor 
116056396aeSDouglas Gregor /// \brief "Sanitize" a filename so that it can be used as an identifier.
117056396aeSDouglas Gregor static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
118056396aeSDouglas Gregor                                               SmallVectorImpl<char> &Buffer) {
119056396aeSDouglas Gregor   if (Name.empty())
120056396aeSDouglas Gregor     return Name;
121056396aeSDouglas Gregor 
122a7d03840SJordan Rose   if (!isValidIdentifier(Name)) {
123056396aeSDouglas Gregor     // If we don't already have something with the form of an identifier,
124056396aeSDouglas Gregor     // create a buffer with the sanitized name.
125056396aeSDouglas Gregor     Buffer.clear();
126a7d03840SJordan Rose     if (isDigit(Name[0]))
127056396aeSDouglas Gregor       Buffer.push_back('_');
128056396aeSDouglas Gregor     Buffer.reserve(Buffer.size() + Name.size());
129056396aeSDouglas Gregor     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
130a7d03840SJordan Rose       if (isIdentifierBody(Name[I]))
131056396aeSDouglas Gregor         Buffer.push_back(Name[I]);
132056396aeSDouglas Gregor       else
133056396aeSDouglas Gregor         Buffer.push_back('_');
134056396aeSDouglas Gregor     }
135056396aeSDouglas Gregor 
136056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
137056396aeSDouglas Gregor   }
138056396aeSDouglas Gregor 
139056396aeSDouglas Gregor   while (llvm::StringSwitch<bool>(Name)
140056396aeSDouglas Gregor #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
141056396aeSDouglas Gregor #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
142056396aeSDouglas Gregor #include "clang/Basic/TokenKinds.def"
143056396aeSDouglas Gregor            .Default(false)) {
144056396aeSDouglas Gregor     if (Name.data() != Buffer.data())
145056396aeSDouglas Gregor       Buffer.append(Name.begin(), Name.end());
146056396aeSDouglas Gregor     Buffer.push_back('_');
147056396aeSDouglas Gregor     Name = StringRef(Buffer.data(), Buffer.size());
148056396aeSDouglas Gregor   }
149056396aeSDouglas Gregor 
150056396aeSDouglas Gregor   return Name;
151056396aeSDouglas Gregor }
152056396aeSDouglas Gregor 
15334d52749SDouglas Gregor /// \brief Determine whether the given file name is the name of a builtin
15434d52749SDouglas Gregor /// header, supplied by Clang to replace, override, or augment existing system
15534d52749SDouglas Gregor /// headers.
15634d52749SDouglas Gregor static bool isBuiltinHeader(StringRef FileName) {
15734d52749SDouglas Gregor   return llvm::StringSwitch<bool>(FileName)
15834d52749SDouglas Gregor            .Case("float.h", true)
15934d52749SDouglas Gregor            .Case("iso646.h", true)
16034d52749SDouglas Gregor            .Case("limits.h", true)
16134d52749SDouglas Gregor            .Case("stdalign.h", true)
16234d52749SDouglas Gregor            .Case("stdarg.h", true)
16334d52749SDouglas Gregor            .Case("stdbool.h", true)
16434d52749SDouglas Gregor            .Case("stddef.h", true)
16534d52749SDouglas Gregor            .Case("stdint.h", true)
16634d52749SDouglas Gregor            .Case("tgmath.h", true)
16734d52749SDouglas Gregor            .Case("unwind.h", true)
16834d52749SDouglas Gregor            .Default(false);
16934d52749SDouglas Gregor }
17034d52749SDouglas Gregor 
171*97da9178SDaniel Jasper ModuleMap::KnownHeader
172*97da9178SDaniel Jasper ModuleMap::findModuleForHeader(const FileEntry *File,
173*97da9178SDaniel Jasper                                Module *RequestingModule) {
17459527666SDouglas Gregor   HeadersMap::iterator Known = Headers.find(File);
1751fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
176*97da9178SDaniel Jasper     ModuleMap::KnownHeader Result = KnownHeader();
1771fb5c3a6SDouglas Gregor 
178*97da9178SDaniel Jasper     // Iterate over all modules that 'File' is part of to find the best fit.
179*97da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
180*97da9178SDaniel Jasper                                                 E = Known->second.end();
181*97da9178SDaniel Jasper          I != E; ++I) {
182*97da9178SDaniel Jasper       // Cannot use a module if the header is excluded or unavailable in it.
183*97da9178SDaniel Jasper       if (I->getRole() == ModuleMap::ExcludedHeader ||
184*97da9178SDaniel Jasper           !I->getModule()->isAvailable())
185*97da9178SDaniel Jasper         continue;
186*97da9178SDaniel Jasper 
187*97da9178SDaniel Jasper       // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
188*97da9178SDaniel Jasper       // module we are looking for.
189*97da9178SDaniel Jasper       if (I->getModule() == RequestingModule)
190*97da9178SDaniel Jasper         return *I;
191*97da9178SDaniel Jasper 
192*97da9178SDaniel Jasper       // If uses need to be specified explicitly, we are only allowed to return
193*97da9178SDaniel Jasper       // modules that are explicitly used by the requesting module.
194*97da9178SDaniel Jasper       if (RequestingModule && LangOpts.ModulesDeclUse &&
195*97da9178SDaniel Jasper           std::find(RequestingModule->DirectUses.begin(),
196*97da9178SDaniel Jasper                     RequestingModule->DirectUses.end(),
197*97da9178SDaniel Jasper                     I->getModule()) == RequestingModule->DirectUses.end())
198*97da9178SDaniel Jasper         continue;
199*97da9178SDaniel Jasper       Result = *I;
200*97da9178SDaniel Jasper       // If 'File' is a public header of this module, this is as good as we
201*97da9178SDaniel Jasper       // are going to get.
202*97da9178SDaniel Jasper       if (I->getRole() == ModuleMap::NormalHeader)
203*97da9178SDaniel Jasper         break;
204*97da9178SDaniel Jasper     }
205*97da9178SDaniel Jasper     return Result;
2061fb5c3a6SDouglas Gregor   }
207ab0c8a84SDouglas Gregor 
20834d52749SDouglas Gregor   // If we've found a builtin header within Clang's builtin include directory,
20934d52749SDouglas Gregor   // load all of the module maps to see if it will get associated with a
21034d52749SDouglas Gregor   // specific module (e.g., in /usr/include).
21134d52749SDouglas Gregor   if (File->getDir() == BuiltinIncludeDir &&
21234d52749SDouglas Gregor       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
21364a1fa5cSDouglas Gregor     HeaderInfo.loadTopLevelSystemModules();
21434d52749SDouglas Gregor 
21534d52749SDouglas Gregor     // Check again.
216*97da9178SDaniel Jasper     if (Headers.find(File) != Headers.end())
217*97da9178SDaniel Jasper       return findModuleForHeader(File, RequestingModule);
21834d52749SDouglas Gregor   }
21934d52749SDouglas Gregor 
220b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
221f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
222e00c8b20SDouglas Gregor 
22374260502SDouglas Gregor   // Note: as an egregious but useful hack we use the real path here, because
22474260502SDouglas Gregor   // frameworks moving from top-level frameworks to embedded frameworks tend
22574260502SDouglas Gregor   // to be symlinked from the top-level location to the embedded location,
22674260502SDouglas Gregor   // and we need to resolve lookups as if we had found the embedded location.
227e00c8b20SDouglas Gregor   StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir);
228a89c5ac4SDouglas Gregor 
229a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
230a89c5ac4SDouglas Gregor   // an umbrella header.
231b65dbfffSDouglas Gregor   do {
232a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
233a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
234a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
235a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
236930a85ccSDouglas Gregor 
237930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
23873141fa9SDouglas Gregor       // directory.
239930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
24073141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
241930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
242930a85ccSDouglas Gregor 
243930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
244a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
245a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
246a89c5ac4SDouglas Gregor         // the actual header is located.
2479458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
2489458f82dSDouglas Gregor 
2497033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
250a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
251056396aeSDouglas Gregor           SmallString<32> NameBuf;
252056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
253056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
254056396aeSDouglas Gregor                              NameBuf);
255a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2569458f82dSDouglas Gregor                                       Explicit).first;
257a89c5ac4SDouglas Gregor 
258a89c5ac4SDouglas Gregor           // Associate the module and the directory.
259a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
260a89c5ac4SDouglas Gregor 
261a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
262a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
263930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
264a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
265a89c5ac4SDouglas Gregor         }
266a89c5ac4SDouglas Gregor 
267a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
268056396aeSDouglas Gregor         SmallString<32> NameBuf;
269056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
270056396aeSDouglas Gregor                            llvm::sys::path::stem(File->getName()), NameBuf);
271a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2729458f82dSDouglas Gregor                                     Explicit).first;
2733c5305c1SArgyrios Kyrtzidis         Result->addTopHeader(File);
274a89c5ac4SDouglas Gregor 
275a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
276a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
277930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
278a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
279a89c5ac4SDouglas Gregor       } else {
280a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
281a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
282a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
283a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
284a89c5ac4SDouglas Gregor       }
285a89c5ac4SDouglas Gregor 
286*97da9178SDaniel Jasper       Headers[File].push_back(KnownHeader(Result, NormalHeader));
2871fb5c3a6SDouglas Gregor 
2881fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
2891fb5c3a6SDouglas Gregor       // that it maps to anything.
2901fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
291b53e5483SLawrence Crowl         return KnownHeader();
2921fb5c3a6SDouglas Gregor 
293*97da9178SDaniel Jasper       return Headers[File].back();
294a89c5ac4SDouglas Gregor     }
295a89c5ac4SDouglas Gregor 
296a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
297a89c5ac4SDouglas Gregor 
298b65dbfffSDouglas Gregor     // Retrieve our parent path.
299b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
300b65dbfffSDouglas Gregor     if (DirName.empty())
301b65dbfffSDouglas Gregor       break;
302b65dbfffSDouglas Gregor 
303b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
304b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
305a89c5ac4SDouglas Gregor   } while (Dir);
306b65dbfffSDouglas Gregor 
307b53e5483SLawrence Crowl   return KnownHeader();
308ab0c8a84SDouglas Gregor }
309ab0c8a84SDouglas Gregor 
310e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
311e4412640SArgyrios Kyrtzidis   HeadersMap::const_iterator Known = Headers.find(Header);
312*97da9178SDaniel Jasper   if (Known != Headers.end()) {
313*97da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator
314*97da9178SDaniel Jasper              I = Known->second.begin(),
315*97da9178SDaniel Jasper              E = Known->second.end();
316*97da9178SDaniel Jasper          I != E; ++I) {
317*97da9178SDaniel Jasper       if (I->isAvailable())
318*97da9178SDaniel Jasper         return false;
319*97da9178SDaniel Jasper     }
320*97da9178SDaniel Jasper     return true;
321*97da9178SDaniel Jasper   }
3221fb5c3a6SDouglas Gregor 
3231fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
324f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
3251fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
3261fb5c3a6SDouglas Gregor 
3271fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
3281fb5c3a6SDouglas Gregor   // an umbrella header.
3291fb5c3a6SDouglas Gregor   do {
330e4412640SArgyrios Kyrtzidis     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
3311fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
3321fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
3331fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
3341fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
3351fb5c3a6SDouglas Gregor         return true;
3361fb5c3a6SDouglas Gregor 
3371fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
3381fb5c3a6SDouglas Gregor       // directory.
3391fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
3401fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
3411fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
3421fb5c3a6SDouglas Gregor 
3431fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
3441fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
3451fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
346056396aeSDouglas Gregor           SmallString<32> NameBuf;
347056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
348056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
349056396aeSDouglas Gregor                              NameBuf);
3501fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
3511fb5c3a6SDouglas Gregor           if (!Found)
3521fb5c3a6SDouglas Gregor             return false;
3531fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
3541fb5c3a6SDouglas Gregor             return true;
3551fb5c3a6SDouglas Gregor         }
3561fb5c3a6SDouglas Gregor 
3571fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
358056396aeSDouglas Gregor         SmallString<32> NameBuf;
359056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
360056396aeSDouglas Gregor                            llvm::sys::path::stem(Header->getName()),
361056396aeSDouglas Gregor                            NameBuf);
3621fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
3631fb5c3a6SDouglas Gregor         if (!Found)
3641fb5c3a6SDouglas Gregor           return false;
3651fb5c3a6SDouglas Gregor       }
3661fb5c3a6SDouglas Gregor 
3671fb5c3a6SDouglas Gregor       return !Found->isAvailable();
3681fb5c3a6SDouglas Gregor     }
3691fb5c3a6SDouglas Gregor 
3701fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
3711fb5c3a6SDouglas Gregor 
3721fb5c3a6SDouglas Gregor     // Retrieve our parent path.
3731fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
3741fb5c3a6SDouglas Gregor     if (DirName.empty())
3751fb5c3a6SDouglas Gregor       break;
3761fb5c3a6SDouglas Gregor 
3771fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
3781fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
3791fb5c3a6SDouglas Gregor   } while (Dir);
3801fb5c3a6SDouglas Gregor 
3811fb5c3a6SDouglas Gregor   return false;
3821fb5c3a6SDouglas Gregor }
3831fb5c3a6SDouglas Gregor 
384e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const {
385e4412640SArgyrios Kyrtzidis   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
38688bdfb0eSDouglas Gregor   if (Known != Modules.end())
38788bdfb0eSDouglas Gregor     return Known->getValue();
38888bdfb0eSDouglas Gregor 
38988bdfb0eSDouglas Gregor   return 0;
39088bdfb0eSDouglas Gregor }
39188bdfb0eSDouglas Gregor 
392e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
393e4412640SArgyrios Kyrtzidis                                            Module *Context) const {
3942b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
3952b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
3962b82c2a5SDouglas Gregor       return Sub;
3972b82c2a5SDouglas Gregor   }
3982b82c2a5SDouglas Gregor 
3992b82c2a5SDouglas Gregor   return findModule(Name);
4002b82c2a5SDouglas Gregor }
4012b82c2a5SDouglas Gregor 
402e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
4032b82c2a5SDouglas Gregor   if (!Context)
4042b82c2a5SDouglas Gregor     return findModule(Name);
4052b82c2a5SDouglas Gregor 
406eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
4072b82c2a5SDouglas Gregor }
4082b82c2a5SDouglas Gregor 
409de3ef502SDouglas Gregor std::pair<Module *, bool>
41069021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
41169021974SDouglas Gregor                               bool IsExplicit) {
41269021974SDouglas Gregor   // Try to find an existing module with this name.
413eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
414eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
41569021974SDouglas Gregor 
41669021974SDouglas Gregor   // Create a new module with this name.
41769021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
41869021974SDouglas Gregor                               IsExplicit);
419ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == Name) {
420ba7f2f71SDaniel Jasper     SourceModule = Result;
421ba7f2f71SDaniel Jasper     SourceModuleName = Name;
422ba7f2f71SDaniel Jasper   }
4236f722b4eSArgyrios Kyrtzidis   if (!Parent) {
42469021974SDouglas Gregor     Modules[Name] = Result;
4256f722b4eSArgyrios Kyrtzidis     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
4266f722b4eSArgyrios Kyrtzidis         Name == LangOpts.CurrentModule) {
4276f722b4eSArgyrios Kyrtzidis       CompilingModule = Result;
4286f722b4eSArgyrios Kyrtzidis     }
4296f722b4eSArgyrios Kyrtzidis   }
43069021974SDouglas Gregor   return std::make_pair(Result, true);
43169021974SDouglas Gregor }
43269021974SDouglas Gregor 
4339194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
434e4412640SArgyrios Kyrtzidis                                         StringRef Name, bool &IsSystem) const {
4359194a91dSDouglas Gregor   // Check whether we have already looked into the parent directory
4369194a91dSDouglas Gregor   // for a module map.
437e4412640SArgyrios Kyrtzidis   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4389194a91dSDouglas Gregor     inferred = InferredDirectories.find(ParentDir);
4399194a91dSDouglas Gregor   if (inferred == InferredDirectories.end())
4409194a91dSDouglas Gregor     return false;
4419194a91dSDouglas Gregor 
4429194a91dSDouglas Gregor   if (!inferred->second.InferModules)
4439194a91dSDouglas Gregor     return false;
4449194a91dSDouglas Gregor 
4459194a91dSDouglas Gregor   // We're allowed to infer for this directory, but make sure it's okay
4469194a91dSDouglas Gregor   // to infer this particular module.
4479194a91dSDouglas Gregor   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
4489194a91dSDouglas Gregor                             inferred->second.ExcludedModules.end(),
4499194a91dSDouglas Gregor                             Name) == inferred->second.ExcludedModules.end();
4509194a91dSDouglas Gregor 
4519194a91dSDouglas Gregor   if (canInfer && inferred->second.InferSystemModules)
4529194a91dSDouglas Gregor     IsSystem = true;
4539194a91dSDouglas Gregor 
4549194a91dSDouglas Gregor   return canInfer;
4559194a91dSDouglas Gregor }
4569194a91dSDouglas Gregor 
45711dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we
45811dfe6feSDouglas Gregor /// should link.
45911dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
46011dfe6feSDouglas Gregor                                FileManager &FileMgr) {
46111dfe6feSDouglas Gregor   assert(Mod->IsFramework && "Can only infer linking for framework modules");
46211dfe6feSDouglas Gregor   assert(!Mod->isSubFramework() &&
46311dfe6feSDouglas Gregor          "Can only infer linking for top-level frameworks");
46411dfe6feSDouglas Gregor 
46511dfe6feSDouglas Gregor   SmallString<128> LibName;
46611dfe6feSDouglas Gregor   LibName += FrameworkDir->getName();
46711dfe6feSDouglas Gregor   llvm::sys::path::append(LibName, Mod->Name);
46811dfe6feSDouglas Gregor   if (FileMgr.getFile(LibName)) {
46911dfe6feSDouglas Gregor     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
47011dfe6feSDouglas Gregor                                                      /*IsFramework=*/true));
47111dfe6feSDouglas Gregor   }
47211dfe6feSDouglas Gregor }
47311dfe6feSDouglas Gregor 
474de3ef502SDouglas Gregor Module *
47556c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
476e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
477a686e1b0SDouglas Gregor                                 bool IsSystem,
478e89dbc1dSDouglas Gregor                                 Module *Parent) {
47956c64013SDouglas Gregor   // Check whether we've already found this module.
480e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
481e89dbc1dSDouglas Gregor     return Mod;
482e89dbc1dSDouglas Gregor 
483e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
48456c64013SDouglas Gregor 
4859194a91dSDouglas Gregor   // If the framework has a parent path from which we're allowed to infer
4869194a91dSDouglas Gregor   // a framework module, do so.
4879194a91dSDouglas Gregor   if (!Parent) {
4884ddf2221SDouglas Gregor     // Determine whether we're allowed to infer a module map.
489e00c8b20SDouglas Gregor 
4904ddf2221SDouglas Gregor     // Note: as an egregious but useful hack we use the real path here, because
4914ddf2221SDouglas Gregor     // we might be looking at an embedded framework that symlinks out to a
4924ddf2221SDouglas Gregor     // top-level framework, and we need to infer as if we were naming the
4934ddf2221SDouglas Gregor     // top-level framework.
494e00c8b20SDouglas Gregor     StringRef FrameworkDirName
495e00c8b20SDouglas Gregor       = SourceMgr->getFileManager().getCanonicalName(FrameworkDir);
4964ddf2221SDouglas Gregor 
4979194a91dSDouglas Gregor     bool canInfer = false;
4984ddf2221SDouglas Gregor     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
4999194a91dSDouglas Gregor       // Figure out the parent path.
5004ddf2221SDouglas Gregor       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
5019194a91dSDouglas Gregor       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
5029194a91dSDouglas Gregor         // Check whether we have already looked into the parent directory
5039194a91dSDouglas Gregor         // for a module map.
504e4412640SArgyrios Kyrtzidis         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
5059194a91dSDouglas Gregor           inferred = InferredDirectories.find(ParentDir);
5069194a91dSDouglas Gregor         if (inferred == InferredDirectories.end()) {
5079194a91dSDouglas Gregor           // We haven't looked here before. Load a module map, if there is
5089194a91dSDouglas Gregor           // one.
5099194a91dSDouglas Gregor           SmallString<128> ModMapPath = Parent;
5109194a91dSDouglas Gregor           llvm::sys::path::append(ModMapPath, "module.map");
5119194a91dSDouglas Gregor           if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
512963c5535SDouglas Gregor             parseModuleMapFile(ModMapFile, IsSystem);
5139194a91dSDouglas Gregor             inferred = InferredDirectories.find(ParentDir);
5149194a91dSDouglas Gregor           }
5159194a91dSDouglas Gregor 
5169194a91dSDouglas Gregor           if (inferred == InferredDirectories.end())
5179194a91dSDouglas Gregor             inferred = InferredDirectories.insert(
5189194a91dSDouglas Gregor                          std::make_pair(ParentDir, InferredDirectory())).first;
5199194a91dSDouglas Gregor         }
5209194a91dSDouglas Gregor 
5219194a91dSDouglas Gregor         if (inferred->second.InferModules) {
5229194a91dSDouglas Gregor           // We're allowed to infer for this directory, but make sure it's okay
5239194a91dSDouglas Gregor           // to infer this particular module.
5244ddf2221SDouglas Gregor           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
5259194a91dSDouglas Gregor           canInfer = std::find(inferred->second.ExcludedModules.begin(),
5269194a91dSDouglas Gregor                                inferred->second.ExcludedModules.end(),
5279194a91dSDouglas Gregor                                Name) == inferred->second.ExcludedModules.end();
5289194a91dSDouglas Gregor 
5299194a91dSDouglas Gregor           if (inferred->second.InferSystemModules)
5309194a91dSDouglas Gregor             IsSystem = true;
5319194a91dSDouglas Gregor         }
5329194a91dSDouglas Gregor       }
5339194a91dSDouglas Gregor     }
5349194a91dSDouglas Gregor 
5359194a91dSDouglas Gregor     // If we're not allowed to infer a framework module, don't.
5369194a91dSDouglas Gregor     if (!canInfer)
5379194a91dSDouglas Gregor       return 0;
5389194a91dSDouglas Gregor   }
5399194a91dSDouglas Gregor 
5409194a91dSDouglas Gregor 
54156c64013SDouglas Gregor   // Look for an umbrella header.
5422c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
54317381a06SBenjamin Kramer   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
544e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
54556c64013SDouglas Gregor 
54656c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
54756c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
54856c64013SDouglas Gregor   // idea.
54956c64013SDouglas Gregor   if (!UmbrellaHeader)
55056c64013SDouglas Gregor     return 0;
55156c64013SDouglas Gregor 
552e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
553e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
554ba7f2f71SDaniel Jasper   if (LangOpts.CurrentModule == ModuleName) {
555ba7f2f71SDaniel Jasper     SourceModule = Result;
556ba7f2f71SDaniel Jasper     SourceModuleName = ModuleName;
557ba7f2f71SDaniel Jasper   }
558a686e1b0SDouglas Gregor   if (IsSystem)
559a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
560a686e1b0SDouglas Gregor 
561eb90e830SDouglas Gregor   if (!Parent)
562e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
563e89dbc1dSDouglas Gregor 
564322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
56573141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
566*97da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
5674dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
568d8bd7537SDouglas Gregor 
569d8bd7537SDouglas Gregor   // export *
570d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
571d8bd7537SDouglas Gregor 
572a89c5ac4SDouglas Gregor   // module * { export * }
573a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
574a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
575a89c5ac4SDouglas Gregor 
576e89dbc1dSDouglas Gregor   // Look for subframeworks.
577e89dbc1dSDouglas Gregor   llvm::error_code EC;
5782c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
579ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
580e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
5812d4d8cb3SBenjamin Kramer   llvm::sys::path::native(SubframeworksDirName);
582ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
5832d4d8cb3SBenjamin Kramer          Dir(SubframeworksDirName.str(), EC), DirEnd;
584e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
585e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
586e89dbc1dSDouglas Gregor       continue;
587f2161a70SDouglas Gregor 
588e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
589e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
59007c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
59107c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
59207c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
59307c22b78SDouglas Gregor       // out to a top-level framework.
594e00c8b20SDouglas Gregor       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
59507c22b78SDouglas Gregor       bool FoundParent = false;
59607c22b78SDouglas Gregor       do {
59707c22b78SDouglas Gregor         // Get the parent directory name.
59807c22b78SDouglas Gregor         SubframeworkDirName
59907c22b78SDouglas Gregor           = llvm::sys::path::parent_path(SubframeworkDirName);
60007c22b78SDouglas Gregor         if (SubframeworkDirName.empty())
60107c22b78SDouglas Gregor           break;
60207c22b78SDouglas Gregor 
60307c22b78SDouglas Gregor         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
60407c22b78SDouglas Gregor           FoundParent = true;
60507c22b78SDouglas Gregor           break;
60607c22b78SDouglas Gregor         }
60707c22b78SDouglas Gregor       } while (true);
60807c22b78SDouglas Gregor 
60907c22b78SDouglas Gregor       if (!FoundParent)
61007c22b78SDouglas Gregor         continue;
61107c22b78SDouglas Gregor 
612e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
613056396aeSDouglas Gregor       SmallString<32> NameBuf;
614056396aeSDouglas Gregor       inferFrameworkModule(sanitizeFilenameAsIdentifier(
615056396aeSDouglas Gregor                              llvm::sys::path::stem(Dir->path()), NameBuf),
616056396aeSDouglas Gregor                            SubframeworkDir, IsSystem, Result);
617e89dbc1dSDouglas Gregor     }
618e89dbc1dSDouglas Gregor   }
619e89dbc1dSDouglas Gregor 
62011dfe6feSDouglas Gregor   // If the module is a top-level framework, automatically link against the
62111dfe6feSDouglas Gregor   // framework.
62211dfe6feSDouglas Gregor   if (!Result->isSubFramework()) {
62311dfe6feSDouglas Gregor     inferFrameworkLink(Result, FrameworkDir, FileMgr);
62411dfe6feSDouglas Gregor   }
62511dfe6feSDouglas Gregor 
62656c64013SDouglas Gregor   return Result;
62756c64013SDouglas Gregor }
62856c64013SDouglas Gregor 
629a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
630*97da9178SDaniel Jasper   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
63173141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
6327033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
633a89c5ac4SDouglas Gregor }
634a89c5ac4SDouglas Gregor 
635524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
636524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
637524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
638524e33e1SDouglas Gregor }
639524e33e1SDouglas Gregor 
64059527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
641b53e5483SLawrence Crowl                           ModuleHeaderRole Role) {
642b53e5483SLawrence Crowl   if (Role == ExcludedHeader) {
64359527666SDouglas Gregor     Mod->ExcludedHeaders.push_back(Header);
644b146baabSArgyrios Kyrtzidis   } else {
645b53e5483SLawrence Crowl     if (Role == PrivateHeader)
646b53e5483SLawrence Crowl       Mod->PrivateHeaders.push_back(Header);
647b53e5483SLawrence Crowl     else
648b53e5483SLawrence Crowl       Mod->NormalHeaders.push_back(Header);
6496f722b4eSArgyrios Kyrtzidis     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
650b53e5483SLawrence Crowl     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
651b146baabSArgyrios Kyrtzidis   }
652*97da9178SDaniel Jasper   Headers[Header].push_back(KnownHeader(Mod, Role));
653a89c5ac4SDouglas Gregor }
654a89c5ac4SDouglas Gregor 
655514b636aSDouglas Gregor const FileEntry *
656e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const {
657514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
658514b636aSDouglas Gregor     return 0;
659514b636aSDouglas Gregor 
660514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
661514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
662514b636aSDouglas Gregor }
663514b636aSDouglas Gregor 
664718292f2SDouglas Gregor void ModuleMap::dump() {
665718292f2SDouglas Gregor   llvm::errs() << "Modules:";
666718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
667718292f2SDouglas Gregor                                         MEnd = Modules.end();
668718292f2SDouglas Gregor        M != MEnd; ++M)
669d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
670718292f2SDouglas Gregor 
671718292f2SDouglas Gregor   llvm::errs() << "Headers:";
67259527666SDouglas Gregor   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
673718292f2SDouglas Gregor        H != HEnd; ++H) {
674*97da9178SDaniel Jasper     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
675*97da9178SDaniel Jasper     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
676*97da9178SDaniel Jasper                                                       E = H->second.end();
677*97da9178SDaniel Jasper          I != E; ++I) {
678*97da9178SDaniel Jasper       if (I != H->second.begin())
679*97da9178SDaniel Jasper         llvm::errs() << ",";
680*97da9178SDaniel Jasper       llvm::errs() << I->getModule()->getFullModuleName();
681*97da9178SDaniel Jasper     }
682*97da9178SDaniel Jasper     llvm::errs() << "\n";
683718292f2SDouglas Gregor   }
684718292f2SDouglas Gregor }
685718292f2SDouglas Gregor 
6862b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
6872b82c2a5SDouglas Gregor   bool HadError = false;
6882b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
6892b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
6902b82c2a5SDouglas Gregor                                               Complain);
691f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
6922b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
6932b82c2a5SDouglas Gregor     else
6942b82c2a5SDouglas Gregor       HadError = true;
6952b82c2a5SDouglas Gregor   }
6962b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
6972b82c2a5SDouglas Gregor   return HadError;
6982b82c2a5SDouglas Gregor }
6992b82c2a5SDouglas Gregor 
700ba7f2f71SDaniel Jasper bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
701ba7f2f71SDaniel Jasper   bool HadError = false;
702ba7f2f71SDaniel Jasper   for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) {
703ba7f2f71SDaniel Jasper     Module *DirectUse =
704ba7f2f71SDaniel Jasper         resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain);
705ba7f2f71SDaniel Jasper     if (DirectUse)
706ba7f2f71SDaniel Jasper       Mod->DirectUses.push_back(DirectUse);
707ba7f2f71SDaniel Jasper     else
708ba7f2f71SDaniel Jasper       HadError = true;
709ba7f2f71SDaniel Jasper   }
710ba7f2f71SDaniel Jasper   Mod->UnresolvedDirectUses.clear();
711ba7f2f71SDaniel Jasper   return HadError;
712ba7f2f71SDaniel Jasper }
713ba7f2f71SDaniel Jasper 
714fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
715fb912657SDouglas Gregor   bool HadError = false;
716fb912657SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
717fb912657SDouglas Gregor     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
718fb912657SDouglas Gregor                                        Mod, Complain);
719fb912657SDouglas Gregor     if (!OtherMod) {
720fb912657SDouglas Gregor       HadError = true;
721fb912657SDouglas Gregor       continue;
722fb912657SDouglas Gregor     }
723fb912657SDouglas Gregor 
724fb912657SDouglas Gregor     Module::Conflict Conflict;
725fb912657SDouglas Gregor     Conflict.Other = OtherMod;
726fb912657SDouglas Gregor     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
727fb912657SDouglas Gregor     Mod->Conflicts.push_back(Conflict);
728fb912657SDouglas Gregor   }
729fb912657SDouglas Gregor   Mod->UnresolvedConflicts.clear();
730fb912657SDouglas Gregor   return HadError;
731fb912657SDouglas Gregor }
732fb912657SDouglas Gregor 
7330093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
7340093b3c7SDouglas Gregor   if (Loc.isInvalid())
7350093b3c7SDouglas Gregor     return 0;
7360093b3c7SDouglas Gregor 
7370093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
7380093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
7390093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
7400093b3c7SDouglas Gregor     return 0;
7410093b3c7SDouglas Gregor 
7420093b3c7SDouglas Gregor 
7430093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
7440093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
745224d8a74SDouglas Gregor 
746224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
747224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
748224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
749b53e5483SLawrence Crowl     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
750224d8a74SDouglas Gregor       return Mod;
751224d8a74SDouglas Gregor 
752224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
753224d8a74SDouglas Gregor     // any included header has an associated module.
754224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
755224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
7560093b3c7SDouglas Gregor       return 0;
7570093b3c7SDouglas Gregor 
758224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
759224d8a74SDouglas Gregor   }
760224d8a74SDouglas Gregor 
761224d8a74SDouglas Gregor   return 0;
7620093b3c7SDouglas Gregor }
7630093b3c7SDouglas Gregor 
764718292f2SDouglas Gregor //----------------------------------------------------------------------------//
765718292f2SDouglas Gregor // Module map file parser
766718292f2SDouglas Gregor //----------------------------------------------------------------------------//
767718292f2SDouglas Gregor 
768718292f2SDouglas Gregor namespace clang {
769718292f2SDouglas Gregor   /// \brief A token in a module map file.
770718292f2SDouglas Gregor   struct MMToken {
771718292f2SDouglas Gregor     enum TokenKind {
7721fb5c3a6SDouglas Gregor       Comma,
77335b13eceSDouglas Gregor       ConfigMacros,
774fb912657SDouglas Gregor       Conflict,
775718292f2SDouglas Gregor       EndOfFile,
776718292f2SDouglas Gregor       HeaderKeyword,
777718292f2SDouglas Gregor       Identifier,
77859527666SDouglas Gregor       ExcludeKeyword,
779718292f2SDouglas Gregor       ExplicitKeyword,
7802b82c2a5SDouglas Gregor       ExportKeyword,
78197292843SDaniel Jasper       ExternKeyword,
782755b2055SDouglas Gregor       FrameworkKeyword,
7836ddfca91SDouglas Gregor       LinkKeyword,
784718292f2SDouglas Gregor       ModuleKeyword,
7852b82c2a5SDouglas Gregor       Period,
786b53e5483SLawrence Crowl       PrivateKeyword,
787718292f2SDouglas Gregor       UmbrellaKeyword,
788ba7f2f71SDaniel Jasper       UseKeyword,
7891fb5c3a6SDouglas Gregor       RequiresKeyword,
7902b82c2a5SDouglas Gregor       Star,
791718292f2SDouglas Gregor       StringLiteral,
792718292f2SDouglas Gregor       LBrace,
793a686e1b0SDouglas Gregor       RBrace,
794a686e1b0SDouglas Gregor       LSquare,
795a686e1b0SDouglas Gregor       RSquare
796718292f2SDouglas Gregor     } Kind;
797718292f2SDouglas Gregor 
798718292f2SDouglas Gregor     unsigned Location;
799718292f2SDouglas Gregor     unsigned StringLength;
800718292f2SDouglas Gregor     const char *StringData;
801718292f2SDouglas Gregor 
802718292f2SDouglas Gregor     void clear() {
803718292f2SDouglas Gregor       Kind = EndOfFile;
804718292f2SDouglas Gregor       Location = 0;
805718292f2SDouglas Gregor       StringLength = 0;
806718292f2SDouglas Gregor       StringData = 0;
807718292f2SDouglas Gregor     }
808718292f2SDouglas Gregor 
809718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
810718292f2SDouglas Gregor 
811718292f2SDouglas Gregor     SourceLocation getLocation() const {
812718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
813718292f2SDouglas Gregor     }
814718292f2SDouglas Gregor 
815718292f2SDouglas Gregor     StringRef getString() const {
816718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
817718292f2SDouglas Gregor     }
818718292f2SDouglas Gregor   };
819718292f2SDouglas Gregor 
8209194a91dSDouglas Gregor   /// \brief The set of attributes that can be attached to a module.
8214442605fSBill Wendling   struct Attributes {
82235b13eceSDouglas Gregor     Attributes() : IsSystem(), IsExhaustive() { }
8239194a91dSDouglas Gregor 
8249194a91dSDouglas Gregor     /// \brief Whether this is a system module.
8259194a91dSDouglas Gregor     unsigned IsSystem : 1;
82635b13eceSDouglas Gregor 
82735b13eceSDouglas Gregor     /// \brief Whether this is an exhaustive set of configuration macros.
82835b13eceSDouglas Gregor     unsigned IsExhaustive : 1;
8299194a91dSDouglas Gregor   };
8309194a91dSDouglas Gregor 
8319194a91dSDouglas Gregor 
832718292f2SDouglas Gregor   class ModuleMapParser {
833718292f2SDouglas Gregor     Lexer &L;
834718292f2SDouglas Gregor     SourceManager &SourceMgr;
835bc10b9fbSDouglas Gregor 
836bc10b9fbSDouglas Gregor     /// \brief Default target information, used only for string literal
837bc10b9fbSDouglas Gregor     /// parsing.
838bc10b9fbSDouglas Gregor     const TargetInfo *Target;
839bc10b9fbSDouglas Gregor 
840718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
841718292f2SDouglas Gregor     ModuleMap &Map;
842718292f2SDouglas Gregor 
8435257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
8445257fc63SDouglas Gregor     const DirectoryEntry *Directory;
8455257fc63SDouglas Gregor 
8463ec6663bSDouglas Gregor     /// \brief The directory containing Clang-supplied headers.
8473ec6663bSDouglas Gregor     const DirectoryEntry *BuiltinIncludeDir;
8483ec6663bSDouglas Gregor 
849963c5535SDouglas Gregor     /// \brief Whether this module map is in a system header directory.
850963c5535SDouglas Gregor     bool IsSystem;
851963c5535SDouglas Gregor 
852718292f2SDouglas Gregor     /// \brief Whether an error occurred.
853718292f2SDouglas Gregor     bool HadError;
854718292f2SDouglas Gregor 
855718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
856718292f2SDouglas Gregor     /// during parsing.
857718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
858718292f2SDouglas Gregor 
859718292f2SDouglas Gregor     /// \brief The current token.
860718292f2SDouglas Gregor     MMToken Tok;
861718292f2SDouglas Gregor 
862718292f2SDouglas Gregor     /// \brief The active module.
863de3ef502SDouglas Gregor     Module *ActiveModule;
864718292f2SDouglas Gregor 
865718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
866718292f2SDouglas Gregor     SourceLocation consumeToken();
867718292f2SDouglas Gregor 
868718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
869718292f2SDouglas Gregor     /// (or the end of the file).
870718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
871718292f2SDouglas Gregor 
872f857950dSDmitri Gribenko     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
873e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
874718292f2SDouglas Gregor     void parseModuleDecl();
87597292843SDaniel Jasper     void parseExternModuleDecl();
8761fb5c3a6SDouglas Gregor     void parseRequiresDecl();
877b53e5483SLawrence Crowl     void parseHeaderDecl(clang::MMToken::TokenKind,
878b53e5483SLawrence Crowl                          SourceLocation LeadingLoc);
879524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
8802b82c2a5SDouglas Gregor     void parseExportDecl();
881ba7f2f71SDaniel Jasper     void parseUseDecl();
8826ddfca91SDouglas Gregor     void parseLinkDecl();
88335b13eceSDouglas Gregor     void parseConfigMacros();
884fb912657SDouglas Gregor     void parseConflict();
8859194a91dSDouglas Gregor     void parseInferredModuleDecl(bool Framework, bool Explicit);
8864442605fSBill Wendling     bool parseOptionalAttributes(Attributes &Attrs);
887718292f2SDouglas Gregor 
8887033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
8897033127bSDouglas Gregor 
890718292f2SDouglas Gregor   public:
891718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
892bc10b9fbSDouglas Gregor                              const TargetInfo *Target,
893718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
8945257fc63SDouglas Gregor                              ModuleMap &Map,
8953ec6663bSDouglas Gregor                              const DirectoryEntry *Directory,
896963c5535SDouglas Gregor                              const DirectoryEntry *BuiltinIncludeDir,
897963c5535SDouglas Gregor                              bool IsSystem)
898bc10b9fbSDouglas Gregor       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
8993ec6663bSDouglas Gregor         Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
900963c5535SDouglas Gregor         IsSystem(IsSystem), HadError(false), ActiveModule(0)
901718292f2SDouglas Gregor     {
902718292f2SDouglas Gregor       Tok.clear();
903718292f2SDouglas Gregor       consumeToken();
904718292f2SDouglas Gregor     }
905718292f2SDouglas Gregor 
906718292f2SDouglas Gregor     bool parseModuleMapFile();
907718292f2SDouglas Gregor   };
908718292f2SDouglas Gregor }
909718292f2SDouglas Gregor 
910718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
911718292f2SDouglas Gregor retry:
912718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
913718292f2SDouglas Gregor   Tok.clear();
914718292f2SDouglas Gregor 
915718292f2SDouglas Gregor   Token LToken;
916718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
917718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
918718292f2SDouglas Gregor   switch (LToken.getKind()) {
919718292f2SDouglas Gregor   case tok::raw_identifier:
920718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
921718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
922718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
92335b13eceSDouglas Gregor                  .Case("config_macros", MMToken::ConfigMacros)
924fb912657SDouglas Gregor                  .Case("conflict", MMToken::Conflict)
92559527666SDouglas Gregor                  .Case("exclude", MMToken::ExcludeKeyword)
926718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
9272b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
92897292843SDaniel Jasper                  .Case("extern", MMToken::ExternKeyword)
929755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
93035b13eceSDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
9316ddfca91SDouglas Gregor                  .Case("link", MMToken::LinkKeyword)
932718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
933b53e5483SLawrence Crowl                  .Case("private", MMToken::PrivateKeyword)
9341fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
935718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
936ba7f2f71SDaniel Jasper                  .Case("use", MMToken::UseKeyword)
937718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
938718292f2SDouglas Gregor     break;
939718292f2SDouglas Gregor 
9401fb5c3a6SDouglas Gregor   case tok::comma:
9411fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
9421fb5c3a6SDouglas Gregor     break;
9431fb5c3a6SDouglas Gregor 
944718292f2SDouglas Gregor   case tok::eof:
945718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
946718292f2SDouglas Gregor     break;
947718292f2SDouglas Gregor 
948718292f2SDouglas Gregor   case tok::l_brace:
949718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
950718292f2SDouglas Gregor     break;
951718292f2SDouglas Gregor 
952a686e1b0SDouglas Gregor   case tok::l_square:
953a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
954a686e1b0SDouglas Gregor     break;
955a686e1b0SDouglas Gregor 
9562b82c2a5SDouglas Gregor   case tok::period:
9572b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
9582b82c2a5SDouglas Gregor     break;
9592b82c2a5SDouglas Gregor 
960718292f2SDouglas Gregor   case tok::r_brace:
961718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
962718292f2SDouglas Gregor     break;
963718292f2SDouglas Gregor 
964a686e1b0SDouglas Gregor   case tok::r_square:
965a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
966a686e1b0SDouglas Gregor     break;
967a686e1b0SDouglas Gregor 
9682b82c2a5SDouglas Gregor   case tok::star:
9692b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
9702b82c2a5SDouglas Gregor     break;
9712b82c2a5SDouglas Gregor 
972718292f2SDouglas Gregor   case tok::string_literal: {
973d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
974d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
975d67aea28SRichard Smith       HadError = true;
976d67aea28SRichard Smith       goto retry;
977d67aea28SRichard Smith     }
978d67aea28SRichard Smith 
979718292f2SDouglas Gregor     // Parse the string literal.
980718292f2SDouglas Gregor     LangOptions LangOpts;
981718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
982718292f2SDouglas Gregor     if (StringLiteral.hadError)
983718292f2SDouglas Gregor       goto retry;
984718292f2SDouglas Gregor 
985718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
986718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
987718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
988718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
989718292f2SDouglas Gregor     Saved[Length] = 0;
990718292f2SDouglas Gregor 
991718292f2SDouglas Gregor     // Form the token.
992718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
993718292f2SDouglas Gregor     Tok.StringData = Saved;
994718292f2SDouglas Gregor     Tok.StringLength = Length;
995718292f2SDouglas Gregor     break;
996718292f2SDouglas Gregor   }
997718292f2SDouglas Gregor 
998718292f2SDouglas Gregor   case tok::comment:
999718292f2SDouglas Gregor     goto retry;
1000718292f2SDouglas Gregor 
1001718292f2SDouglas Gregor   default:
1002718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
1003718292f2SDouglas Gregor     HadError = true;
1004718292f2SDouglas Gregor     goto retry;
1005718292f2SDouglas Gregor   }
1006718292f2SDouglas Gregor 
1007718292f2SDouglas Gregor   return Result;
1008718292f2SDouglas Gregor }
1009718292f2SDouglas Gregor 
1010718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
1011718292f2SDouglas Gregor   unsigned braceDepth = 0;
1012a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
1013718292f2SDouglas Gregor   do {
1014718292f2SDouglas Gregor     switch (Tok.Kind) {
1015718292f2SDouglas Gregor     case MMToken::EndOfFile:
1016718292f2SDouglas Gregor       return;
1017718292f2SDouglas Gregor 
1018718292f2SDouglas Gregor     case MMToken::LBrace:
1019a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1020718292f2SDouglas Gregor         return;
1021718292f2SDouglas Gregor 
1022718292f2SDouglas Gregor       ++braceDepth;
1023718292f2SDouglas Gregor       break;
1024718292f2SDouglas Gregor 
1025a686e1b0SDouglas Gregor     case MMToken::LSquare:
1026a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
1027a686e1b0SDouglas Gregor         return;
1028a686e1b0SDouglas Gregor 
1029a686e1b0SDouglas Gregor       ++squareDepth;
1030a686e1b0SDouglas Gregor       break;
1031a686e1b0SDouglas Gregor 
1032718292f2SDouglas Gregor     case MMToken::RBrace:
1033718292f2SDouglas Gregor       if (braceDepth > 0)
1034718292f2SDouglas Gregor         --braceDepth;
1035718292f2SDouglas Gregor       else if (Tok.is(K))
1036718292f2SDouglas Gregor         return;
1037718292f2SDouglas Gregor       break;
1038718292f2SDouglas Gregor 
1039a686e1b0SDouglas Gregor     case MMToken::RSquare:
1040a686e1b0SDouglas Gregor       if (squareDepth > 0)
1041a686e1b0SDouglas Gregor         --squareDepth;
1042a686e1b0SDouglas Gregor       else if (Tok.is(K))
1043a686e1b0SDouglas Gregor         return;
1044a686e1b0SDouglas Gregor       break;
1045a686e1b0SDouglas Gregor 
1046718292f2SDouglas Gregor     default:
1047a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
1048718292f2SDouglas Gregor         return;
1049718292f2SDouglas Gregor       break;
1050718292f2SDouglas Gregor     }
1051718292f2SDouglas Gregor 
1052718292f2SDouglas Gregor    consumeToken();
1053718292f2SDouglas Gregor   } while (true);
1054718292f2SDouglas Gregor }
1055718292f2SDouglas Gregor 
1056e7ab3669SDouglas Gregor /// \brief Parse a module-id.
1057e7ab3669SDouglas Gregor ///
1058e7ab3669SDouglas Gregor ///   module-id:
1059e7ab3669SDouglas Gregor ///     identifier
1060e7ab3669SDouglas Gregor ///     identifier '.' module-id
1061e7ab3669SDouglas Gregor ///
1062e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
1063e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
1064e7ab3669SDouglas Gregor   Id.clear();
1065e7ab3669SDouglas Gregor   do {
1066e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
1067e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
1068e7ab3669SDouglas Gregor       consumeToken();
1069e7ab3669SDouglas Gregor     } else {
1070e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1071e7ab3669SDouglas Gregor       return true;
1072e7ab3669SDouglas Gregor     }
1073e7ab3669SDouglas Gregor 
1074e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
1075e7ab3669SDouglas Gregor       break;
1076e7ab3669SDouglas Gregor 
1077e7ab3669SDouglas Gregor     consumeToken();
1078e7ab3669SDouglas Gregor   } while (true);
1079e7ab3669SDouglas Gregor 
1080e7ab3669SDouglas Gregor   return false;
1081e7ab3669SDouglas Gregor }
1082e7ab3669SDouglas Gregor 
1083a686e1b0SDouglas Gregor namespace {
1084a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
1085a686e1b0SDouglas Gregor   enum AttributeKind {
1086a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
1087a686e1b0SDouglas Gregor     AT_unknown,
1088a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
108935b13eceSDouglas Gregor     AT_system,
109035b13eceSDouglas Gregor     /// \brief The 'exhaustive' attribute.
109135b13eceSDouglas Gregor     AT_exhaustive
1092a686e1b0SDouglas Gregor   };
1093a686e1b0SDouglas Gregor }
1094a686e1b0SDouglas Gregor 
1095718292f2SDouglas Gregor /// \brief Parse a module declaration.
1096718292f2SDouglas Gregor ///
1097718292f2SDouglas Gregor ///   module-declaration:
109897292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
1099a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1100a686e1b0SDouglas Gregor ///       { module-member* }
1101a686e1b0SDouglas Gregor ///
1102718292f2SDouglas Gregor ///   module-member:
11031fb5c3a6SDouglas Gregor ///     requires-declaration
1104718292f2SDouglas Gregor ///     header-declaration
1105e7ab3669SDouglas Gregor ///     submodule-declaration
11062b82c2a5SDouglas Gregor ///     export-declaration
11076ddfca91SDouglas Gregor ///     link-declaration
110873441091SDouglas Gregor ///
110973441091SDouglas Gregor ///   submodule-declaration:
111073441091SDouglas Gregor ///     module-declaration
111173441091SDouglas Gregor ///     inferred-submodule-declaration
1112718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
1113755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
111497292843SDaniel Jasper          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
111597292843SDaniel Jasper   if (Tok.is(MMToken::ExternKeyword)) {
111697292843SDaniel Jasper     parseExternModuleDecl();
111797292843SDaniel Jasper     return;
111897292843SDaniel Jasper   }
111997292843SDaniel Jasper 
1120f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
1121e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
1122718292f2SDouglas Gregor   bool Explicit = false;
1123f2161a70SDouglas Gregor   bool Framework = false;
1124755b2055SDouglas Gregor 
1125f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
1126f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
1127e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
1128f2161a70SDouglas Gregor     Explicit = true;
1129f2161a70SDouglas Gregor   }
1130f2161a70SDouglas Gregor 
1131f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
1132755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
1133755b2055SDouglas Gregor     consumeToken();
1134755b2055SDouglas Gregor     Framework = true;
1135755b2055SDouglas Gregor   }
1136718292f2SDouglas Gregor 
1137718292f2SDouglas Gregor   // Parse 'module' keyword.
1138718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
1139d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1140718292f2SDouglas Gregor     consumeToken();
1141718292f2SDouglas Gregor     HadError = true;
1142718292f2SDouglas Gregor     return;
1143718292f2SDouglas Gregor   }
1144718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
1145718292f2SDouglas Gregor 
114673441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
114773441091SDouglas Gregor   // Parse it.
114873441091SDouglas Gregor   if (Tok.is(MMToken::Star))
11499194a91dSDouglas Gregor     return parseInferredModuleDecl(Framework, Explicit);
115073441091SDouglas Gregor 
1151718292f2SDouglas Gregor   // Parse the module name.
1152e7ab3669SDouglas Gregor   ModuleId Id;
1153e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
1154718292f2SDouglas Gregor     HadError = true;
1155718292f2SDouglas Gregor     return;
1156718292f2SDouglas Gregor   }
1157e7ab3669SDouglas Gregor 
1158e7ab3669SDouglas Gregor   if (ActiveModule) {
1159e7ab3669SDouglas Gregor     if (Id.size() > 1) {
1160e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1161e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
1162e7ab3669SDouglas Gregor 
1163e7ab3669SDouglas Gregor       HadError = true;
1164e7ab3669SDouglas Gregor       return;
1165e7ab3669SDouglas Gregor     }
1166e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
1167e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
1168e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1169e7ab3669SDouglas Gregor     Explicit = false;
1170e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
1171e7ab3669SDouglas Gregor     HadError = true;
1172e7ab3669SDouglas Gregor   }
1173e7ab3669SDouglas Gregor 
1174e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
1175e7ab3669SDouglas Gregor   if (Id.size() > 1) {
1176e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
1177e7ab3669SDouglas Gregor     // is a submodule.
1178e7ab3669SDouglas Gregor     ActiveModule = 0;
1179e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1180e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1181e7ab3669SDouglas Gregor         ActiveModule = Next;
1182e7ab3669SDouglas Gregor         continue;
1183e7ab3669SDouglas Gregor       }
1184e7ab3669SDouglas Gregor 
1185e7ab3669SDouglas Gregor       if (ActiveModule) {
1186e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1187e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
1188e7ab3669SDouglas Gregor       } else {
1189e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1190e7ab3669SDouglas Gregor       }
1191e7ab3669SDouglas Gregor       HadError = true;
1192e7ab3669SDouglas Gregor       return;
1193e7ab3669SDouglas Gregor     }
1194e7ab3669SDouglas Gregor   }
1195e7ab3669SDouglas Gregor 
1196e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
1197e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
1198718292f2SDouglas Gregor 
1199a686e1b0SDouglas Gregor   // Parse the optional attribute list.
12004442605fSBill Wendling   Attributes Attrs;
12019194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
1202a686e1b0SDouglas Gregor 
1203718292f2SDouglas Gregor   // Parse the opening brace.
1204718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
1205718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1206718292f2SDouglas Gregor       << ModuleName;
1207718292f2SDouglas Gregor     HadError = true;
1208718292f2SDouglas Gregor     return;
1209718292f2SDouglas Gregor   }
1210718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
1211718292f2SDouglas Gregor 
1212718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
1213eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1214fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1215fcc54a3bSDouglas Gregor       // Skip the module definition.
1216fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
1217fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
1218fcc54a3bSDouglas Gregor         consumeToken();
1219fcc54a3bSDouglas Gregor       else {
1220fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1221fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1222fcc54a3bSDouglas Gregor         HadError = true;
1223fcc54a3bSDouglas Gregor       }
1224fcc54a3bSDouglas Gregor       return;
1225fcc54a3bSDouglas Gregor     }
1226fcc54a3bSDouglas Gregor 
1227718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1228718292f2SDouglas Gregor       << ModuleName;
1229eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1230718292f2SDouglas Gregor 
1231718292f2SDouglas Gregor     // Skip the module definition.
1232718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
1233718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
1234718292f2SDouglas Gregor       consumeToken();
1235718292f2SDouglas Gregor 
1236718292f2SDouglas Gregor     HadError = true;
1237718292f2SDouglas Gregor     return;
1238718292f2SDouglas Gregor   }
1239718292f2SDouglas Gregor 
1240718292f2SDouglas Gregor   // Start defining this module.
1241eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1242eb90e830SDouglas Gregor                                         Explicit).first;
1243eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
1244963c5535SDouglas Gregor   if (Attrs.IsSystem || IsSystem)
1245a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
1246718292f2SDouglas Gregor 
1247718292f2SDouglas Gregor   bool Done = false;
1248718292f2SDouglas Gregor   do {
1249718292f2SDouglas Gregor     switch (Tok.Kind) {
1250718292f2SDouglas Gregor     case MMToken::EndOfFile:
1251718292f2SDouglas Gregor     case MMToken::RBrace:
1252718292f2SDouglas Gregor       Done = true;
1253718292f2SDouglas Gregor       break;
1254718292f2SDouglas Gregor 
125535b13eceSDouglas Gregor     case MMToken::ConfigMacros:
125635b13eceSDouglas Gregor       parseConfigMacros();
125735b13eceSDouglas Gregor       break;
125835b13eceSDouglas Gregor 
1259fb912657SDouglas Gregor     case MMToken::Conflict:
1260fb912657SDouglas Gregor       parseConflict();
1261fb912657SDouglas Gregor       break;
1262fb912657SDouglas Gregor 
1263718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
126497292843SDaniel Jasper     case MMToken::ExternKeyword:
1265f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
1266718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1267718292f2SDouglas Gregor       parseModuleDecl();
1268718292f2SDouglas Gregor       break;
1269718292f2SDouglas Gregor 
12702b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
12712b82c2a5SDouglas Gregor       parseExportDecl();
12722b82c2a5SDouglas Gregor       break;
12732b82c2a5SDouglas Gregor 
1274ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
1275ba7f2f71SDaniel Jasper       parseUseDecl();
1276ba7f2f71SDaniel Jasper       break;
1277ba7f2f71SDaniel Jasper 
12781fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
12791fb5c3a6SDouglas Gregor       parseRequiresDecl();
12801fb5c3a6SDouglas Gregor       break;
12811fb5c3a6SDouglas Gregor 
1282524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
1283524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
1284524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
1285b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
1286524e33e1SDouglas Gregor       else
1287524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
1288718292f2SDouglas Gregor       break;
1289524e33e1SDouglas Gregor     }
1290718292f2SDouglas Gregor 
129159527666SDouglas Gregor     case MMToken::ExcludeKeyword: {
129259527666SDouglas Gregor       SourceLocation ExcludeLoc = consumeToken();
129359527666SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword)) {
1294b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::ExcludeKeyword, ExcludeLoc);
129559527666SDouglas Gregor       } else {
129659527666SDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
129759527666SDouglas Gregor           << "exclude";
129859527666SDouglas Gregor       }
129959527666SDouglas Gregor       break;
130059527666SDouglas Gregor     }
130159527666SDouglas Gregor 
1302b53e5483SLawrence Crowl     case MMToken::PrivateKeyword: {
1303b53e5483SLawrence Crowl       SourceLocation PrivateLoc = consumeToken();
1304b53e5483SLawrence Crowl       if (Tok.is(MMToken::HeaderKeyword)) {
1305b53e5483SLawrence Crowl         parseHeaderDecl(MMToken::PrivateKeyword, PrivateLoc);
1306b53e5483SLawrence Crowl       } else {
1307b53e5483SLawrence Crowl         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1308b53e5483SLawrence Crowl           << "private";
1309b53e5483SLawrence Crowl       }
1310b53e5483SLawrence Crowl       break;
1311b53e5483SLawrence Crowl     }
1312b53e5483SLawrence Crowl 
1313322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
1314b53e5483SLawrence Crowl       parseHeaderDecl(MMToken::HeaderKeyword, SourceLocation());
1315718292f2SDouglas Gregor       break;
1316718292f2SDouglas Gregor 
13176ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
13186ddfca91SDouglas Gregor       parseLinkDecl();
13196ddfca91SDouglas Gregor       break;
13206ddfca91SDouglas Gregor 
1321718292f2SDouglas Gregor     default:
1322718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1323718292f2SDouglas Gregor       consumeToken();
1324718292f2SDouglas Gregor       break;
1325718292f2SDouglas Gregor     }
1326718292f2SDouglas Gregor   } while (!Done);
1327718292f2SDouglas Gregor 
1328718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1329718292f2SDouglas Gregor     consumeToken();
1330718292f2SDouglas Gregor   else {
1331718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1332718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1333718292f2SDouglas Gregor     HadError = true;
1334718292f2SDouglas Gregor   }
1335718292f2SDouglas Gregor 
133611dfe6feSDouglas Gregor   // If the active module is a top-level framework, and there are no link
133711dfe6feSDouglas Gregor   // libraries, automatically link against the framework.
133811dfe6feSDouglas Gregor   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
133911dfe6feSDouglas Gregor       ActiveModule->LinkLibraries.empty()) {
134011dfe6feSDouglas Gregor     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
134111dfe6feSDouglas Gregor   }
134211dfe6feSDouglas Gregor 
1343e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1344e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1345718292f2SDouglas Gregor }
1346718292f2SDouglas Gregor 
134797292843SDaniel Jasper /// \brief Parse an extern module declaration.
134897292843SDaniel Jasper ///
134997292843SDaniel Jasper ///   extern module-declaration:
135097292843SDaniel Jasper ///     'extern' 'module' module-id string-literal
135197292843SDaniel Jasper void ModuleMapParser::parseExternModuleDecl() {
135297292843SDaniel Jasper   assert(Tok.is(MMToken::ExternKeyword));
135397292843SDaniel Jasper   consumeToken(); // 'extern' keyword
135497292843SDaniel Jasper 
135597292843SDaniel Jasper   // Parse 'module' keyword.
135697292843SDaniel Jasper   if (!Tok.is(MMToken::ModuleKeyword)) {
135797292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
135897292843SDaniel Jasper     consumeToken();
135997292843SDaniel Jasper     HadError = true;
136097292843SDaniel Jasper     return;
136197292843SDaniel Jasper   }
136297292843SDaniel Jasper   consumeToken(); // 'module' keyword
136397292843SDaniel Jasper 
136497292843SDaniel Jasper   // Parse the module name.
136597292843SDaniel Jasper   ModuleId Id;
136697292843SDaniel Jasper   if (parseModuleId(Id)) {
136797292843SDaniel Jasper     HadError = true;
136897292843SDaniel Jasper     return;
136997292843SDaniel Jasper   }
137097292843SDaniel Jasper 
137197292843SDaniel Jasper   // Parse the referenced module map file name.
137297292843SDaniel Jasper   if (!Tok.is(MMToken::StringLiteral)) {
137397292843SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
137497292843SDaniel Jasper     HadError = true;
137597292843SDaniel Jasper     return;
137697292843SDaniel Jasper   }
137797292843SDaniel Jasper   std::string FileName = Tok.getString();
137897292843SDaniel Jasper   consumeToken(); // filename
137997292843SDaniel Jasper 
138097292843SDaniel Jasper   StringRef FileNameRef = FileName;
138197292843SDaniel Jasper   SmallString<128> ModuleMapFileName;
138297292843SDaniel Jasper   if (llvm::sys::path::is_relative(FileNameRef)) {
138397292843SDaniel Jasper     ModuleMapFileName += Directory->getName();
138497292843SDaniel Jasper     llvm::sys::path::append(ModuleMapFileName, FileName);
138597292843SDaniel Jasper     FileNameRef = ModuleMapFileName.str();
138697292843SDaniel Jasper   }
138797292843SDaniel Jasper   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
138897292843SDaniel Jasper     Map.parseModuleMapFile(File, /*IsSystem=*/false);
138997292843SDaniel Jasper }
139097292843SDaniel Jasper 
13911fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
13921fb5c3a6SDouglas Gregor ///
13931fb5c3a6SDouglas Gregor ///   requires-declaration:
13941fb5c3a6SDouglas Gregor ///     'requires' feature-list
13951fb5c3a6SDouglas Gregor ///
13961fb5c3a6SDouglas Gregor ///   feature-list:
13971fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
13981fb5c3a6SDouglas Gregor ///     identifier
13991fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
14001fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
14011fb5c3a6SDouglas Gregor 
14021fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
14031fb5c3a6SDouglas Gregor   consumeToken();
14041fb5c3a6SDouglas Gregor 
14051fb5c3a6SDouglas Gregor   // Parse the feature-list.
14061fb5c3a6SDouglas Gregor   do {
14071fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
14081fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
14091fb5c3a6SDouglas Gregor       HadError = true;
14101fb5c3a6SDouglas Gregor       return;
14111fb5c3a6SDouglas Gregor     }
14121fb5c3a6SDouglas Gregor 
14131fb5c3a6SDouglas Gregor     // Consume the feature name.
14141fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
14151fb5c3a6SDouglas Gregor     consumeToken();
14161fb5c3a6SDouglas Gregor 
14171fb5c3a6SDouglas Gregor     // Add this feature.
141889929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
14191fb5c3a6SDouglas Gregor 
14201fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
14211fb5c3a6SDouglas Gregor       break;
14221fb5c3a6SDouglas Gregor 
14231fb5c3a6SDouglas Gregor     // Consume the comma.
14241fb5c3a6SDouglas Gregor     consumeToken();
14251fb5c3a6SDouglas Gregor   } while (true);
14261fb5c3a6SDouglas Gregor }
14271fb5c3a6SDouglas Gregor 
1428f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1429f2161a70SDouglas Gregor /// subframework in which the given module lives.
1430bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1431f857950dSDmitri Gribenko                                     SmallVectorImpl<char> &Path) {
1432f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1433f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Paths;
1434f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1435f2161a70SDouglas Gregor     if (Mod->IsFramework)
1436f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1437f2161a70SDouglas Gregor   }
1438f2161a70SDouglas Gregor 
1439f2161a70SDouglas Gregor   if (Paths.empty())
1440f2161a70SDouglas Gregor     return;
1441f2161a70SDouglas Gregor 
1442f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
144317381a06SBenjamin Kramer   for (unsigned I = Paths.size() - 1; I != 0; --I)
144417381a06SBenjamin Kramer     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
1445f2161a70SDouglas Gregor }
1446f2161a70SDouglas Gregor 
1447718292f2SDouglas Gregor /// \brief Parse a header declaration.
1448718292f2SDouglas Gregor ///
1449718292f2SDouglas Gregor ///   header-declaration:
1450322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
145159527666SDouglas Gregor ///     'exclude'[opt] 'header' string-literal
1452b53e5483SLawrence Crowl void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
1453b53e5483SLawrence Crowl                                       SourceLocation LeadingLoc) {
1454718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
14551871ed3dSBenjamin Kramer   consumeToken();
1456718292f2SDouglas Gregor 
1457718292f2SDouglas Gregor   // Parse the header name.
1458718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1459718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1460718292f2SDouglas Gregor       << "header";
1461718292f2SDouglas Gregor     HadError = true;
1462718292f2SDouglas Gregor     return;
1463718292f2SDouglas Gregor   }
1464e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1465718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1466718292f2SDouglas Gregor 
1467524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1468b53e5483SLawrence Crowl   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
1469524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1470524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1471322f633cSDouglas Gregor     HadError = true;
1472322f633cSDouglas Gregor     return;
1473322f633cSDouglas Gregor   }
1474322f633cSDouglas Gregor 
14755257fc63SDouglas Gregor   // Look for this file.
1476e7ab3669SDouglas Gregor   const FileEntry *File = 0;
14773ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
14782c1dd271SDylan Noblesmith   SmallString<128> PathName;
1479e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1480e7ab3669SDouglas Gregor     PathName = FileName;
1481e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
14827033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
14837033127bSDouglas Gregor     PathName = Dir->getName();
14847033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
14857033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1486e7ab3669SDouglas Gregor   } else {
1487e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
14887033127bSDouglas Gregor     PathName = Directory->getName();
1489e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1490755b2055SDouglas Gregor 
1491f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1492f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1493755b2055SDouglas Gregor 
1494e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
149517381a06SBenjamin Kramer       llvm::sys::path::append(PathName, "Headers", FileName);
1496e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1497e7ab3669SDouglas Gregor 
1498e7ab3669SDouglas Gregor       if (!File) {
1499e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1500e7ab3669SDouglas Gregor         PathName.resize(PathLength);
150117381a06SBenjamin Kramer         llvm::sys::path::append(PathName, "PrivateHeaders", FileName);
1502e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1503e7ab3669SDouglas Gregor       }
1504e7ab3669SDouglas Gregor     } else {
1505e7ab3669SDouglas Gregor       // Lookup for normal headers.
1506e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1507e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
15083ec6663bSDouglas Gregor 
15093ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
15103ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
15113ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
1512b53e5483SLawrence Crowl       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
1513b53e5483SLawrence Crowl           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
1514b53e5483SLawrence Crowl           isBuiltinHeader(FileName)) {
15152c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
15163ec6663bSDouglas Gregor         llvm::sys::path::append(BuiltinPathName, FileName);
15173ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
15183ec6663bSDouglas Gregor 
15193ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
15203ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
15213ec6663bSDouglas Gregor         // up adding both (later).
15223ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
15233ec6663bSDouglas Gregor           File = BuiltinFile;
15243ec6663bSDouglas Gregor           BuiltinFile = 0;
15253ec6663bSDouglas Gregor         }
15263ec6663bSDouglas Gregor       }
1527e7ab3669SDouglas Gregor     }
1528e7ab3669SDouglas Gregor   }
15295257fc63SDouglas Gregor 
15305257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
15315257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1532e7ab3669SDouglas Gregor   if (File) {
1533*97da9178SDaniel Jasper     if (LeadingToken == MMToken::UmbrellaKeyword) {
1534322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
153559527666SDouglas Gregor       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1536b53e5483SLawrence Crowl         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
153759527666SDouglas Gregor           << UmbrellaModule->getFullModuleName();
1538322f633cSDouglas Gregor         HadError = true;
15395257fc63SDouglas Gregor       } else {
1540322f633cSDouglas Gregor         // Record this umbrella header.
1541322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1542322f633cSDouglas Gregor       }
1543322f633cSDouglas Gregor     } else {
1544322f633cSDouglas Gregor       // Record this header.
1545b53e5483SLawrence Crowl       ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
1546b53e5483SLawrence Crowl       if (LeadingToken == MMToken::ExcludeKeyword)
1547b53e5483SLawrence Crowl         Role = ModuleMap::ExcludedHeader;
1548b53e5483SLawrence Crowl       else if (LeadingToken == MMToken::PrivateKeyword)
1549b53e5483SLawrence Crowl         Role = ModuleMap::PrivateHeader;
1550b53e5483SLawrence Crowl       else
1551b53e5483SLawrence Crowl         assert(LeadingToken == MMToken::HeaderKeyword);
1552b53e5483SLawrence Crowl 
1553b53e5483SLawrence Crowl       Map.addHeader(ActiveModule, File, Role);
15543ec6663bSDouglas Gregor 
15553ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
15563ec6663bSDouglas Gregor       if (BuiltinFile)
1557b53e5483SLawrence Crowl         Map.addHeader(ActiveModule, BuiltinFile, Role);
15585257fc63SDouglas Gregor     }
1559b53e5483SLawrence Crowl   } else if (LeadingToken != MMToken::ExcludeKeyword) {
15604b27a64bSDouglas Gregor     // Ignore excluded header files. They're optional anyway.
15614b27a64bSDouglas Gregor 
15625257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1563b53e5483SLawrence Crowl       << (LeadingToken == MMToken::UmbrellaKeyword) << FileName;
15645257fc63SDouglas Gregor     HadError = true;
15655257fc63SDouglas Gregor   }
1566718292f2SDouglas Gregor }
1567718292f2SDouglas Gregor 
1568524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1569524e33e1SDouglas Gregor ///
1570524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1571524e33e1SDouglas Gregor ///     umbrella string-literal
1572524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1573524e33e1SDouglas Gregor   // Parse the directory name.
1574524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1575524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1576524e33e1SDouglas Gregor       << "umbrella";
1577524e33e1SDouglas Gregor     HadError = true;
1578524e33e1SDouglas Gregor     return;
1579524e33e1SDouglas Gregor   }
1580524e33e1SDouglas Gregor 
1581524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1582524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1583524e33e1SDouglas Gregor 
1584524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1585524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1586524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1587524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1588524e33e1SDouglas Gregor     HadError = true;
1589524e33e1SDouglas Gregor     return;
1590524e33e1SDouglas Gregor   }
1591524e33e1SDouglas Gregor 
1592524e33e1SDouglas Gregor   // Look for this file.
1593524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1594524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1595524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1596524e33e1SDouglas Gregor   else {
15972c1dd271SDylan Noblesmith     SmallString<128> PathName;
1598524e33e1SDouglas Gregor     PathName = Directory->getName();
1599524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1600524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1601524e33e1SDouglas Gregor   }
1602524e33e1SDouglas Gregor 
1603524e33e1SDouglas Gregor   if (!Dir) {
1604524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1605524e33e1SDouglas Gregor       << DirName;
1606524e33e1SDouglas Gregor     HadError = true;
1607524e33e1SDouglas Gregor     return;
1608524e33e1SDouglas Gregor   }
1609524e33e1SDouglas Gregor 
1610524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1611524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1612524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1613524e33e1SDouglas Gregor     HadError = true;
1614524e33e1SDouglas Gregor     return;
1615524e33e1SDouglas Gregor   }
1616524e33e1SDouglas Gregor 
1617524e33e1SDouglas Gregor   // Record this umbrella directory.
1618524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1619524e33e1SDouglas Gregor }
1620524e33e1SDouglas Gregor 
16212b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
16222b82c2a5SDouglas Gregor ///
16232b82c2a5SDouglas Gregor ///   export-declaration:
16242b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
16252b82c2a5SDouglas Gregor ///
16262b82c2a5SDouglas Gregor ///   wildcard-module-id:
16272b82c2a5SDouglas Gregor ///     identifier
16282b82c2a5SDouglas Gregor ///     '*'
16292b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
16302b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
16312b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
16322b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
16332b82c2a5SDouglas Gregor 
16342b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
16352b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
16362b82c2a5SDouglas Gregor   bool Wildcard = false;
16372b82c2a5SDouglas Gregor   do {
16382b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
16392b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
16402b82c2a5SDouglas Gregor                                               Tok.getLocation()));
16412b82c2a5SDouglas Gregor       consumeToken();
16422b82c2a5SDouglas Gregor 
16432b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
16442b82c2a5SDouglas Gregor         consumeToken();
16452b82c2a5SDouglas Gregor         continue;
16462b82c2a5SDouglas Gregor       }
16472b82c2a5SDouglas Gregor 
16482b82c2a5SDouglas Gregor       break;
16492b82c2a5SDouglas Gregor     }
16502b82c2a5SDouglas Gregor 
16512b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
16522b82c2a5SDouglas Gregor       Wildcard = true;
1653f5eedd05SDouglas Gregor       consumeToken();
16542b82c2a5SDouglas Gregor       break;
16552b82c2a5SDouglas Gregor     }
16562b82c2a5SDouglas Gregor 
1657ba7f2f71SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
16582b82c2a5SDouglas Gregor     HadError = true;
16592b82c2a5SDouglas Gregor     return;
16602b82c2a5SDouglas Gregor   } while (true);
16612b82c2a5SDouglas Gregor 
16622b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
16632b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
16642b82c2a5SDouglas Gregor   };
16652b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
16662b82c2a5SDouglas Gregor }
16672b82c2a5SDouglas Gregor 
1668ba7f2f71SDaniel Jasper /// \brief Parse a module uses declaration.
1669ba7f2f71SDaniel Jasper ///
1670ba7f2f71SDaniel Jasper ///   uses-declaration:
1671ba7f2f71SDaniel Jasper ///     'uses' wildcard-module-id
1672ba7f2f71SDaniel Jasper void ModuleMapParser::parseUseDecl() {
1673ba7f2f71SDaniel Jasper   assert(Tok.is(MMToken::UseKeyword));
1674ba7f2f71SDaniel Jasper   consumeToken();
1675ba7f2f71SDaniel Jasper   // Parse the module-id.
1676ba7f2f71SDaniel Jasper   ModuleId ParsedModuleId;
1677ba7f2f71SDaniel Jasper 
1678ba7f2f71SDaniel Jasper   do {
1679ba7f2f71SDaniel Jasper     if (Tok.is(MMToken::Identifier)) {
1680ba7f2f71SDaniel Jasper       ParsedModuleId.push_back(
1681ba7f2f71SDaniel Jasper           std::make_pair(Tok.getString(), Tok.getLocation()));
1682ba7f2f71SDaniel Jasper       consumeToken();
1683ba7f2f71SDaniel Jasper 
1684ba7f2f71SDaniel Jasper       if (Tok.is(MMToken::Period)) {
1685ba7f2f71SDaniel Jasper         consumeToken();
1686ba7f2f71SDaniel Jasper         continue;
1687ba7f2f71SDaniel Jasper       }
1688ba7f2f71SDaniel Jasper 
1689ba7f2f71SDaniel Jasper       break;
1690ba7f2f71SDaniel Jasper     }
1691ba7f2f71SDaniel Jasper 
1692ba7f2f71SDaniel Jasper     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
1693ba7f2f71SDaniel Jasper     HadError = true;
1694ba7f2f71SDaniel Jasper     return;
1695ba7f2f71SDaniel Jasper   } while (true);
1696ba7f2f71SDaniel Jasper 
1697ba7f2f71SDaniel Jasper   ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
1698ba7f2f71SDaniel Jasper }
1699ba7f2f71SDaniel Jasper 
17006ddfca91SDouglas Gregor /// \brief Parse a link declaration.
17016ddfca91SDouglas Gregor ///
17026ddfca91SDouglas Gregor ///   module-declaration:
17036ddfca91SDouglas Gregor ///     'link' 'framework'[opt] string-literal
17046ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() {
17056ddfca91SDouglas Gregor   assert(Tok.is(MMToken::LinkKeyword));
17066ddfca91SDouglas Gregor   SourceLocation LinkLoc = consumeToken();
17076ddfca91SDouglas Gregor 
17086ddfca91SDouglas Gregor   // Parse the optional 'framework' keyword.
17096ddfca91SDouglas Gregor   bool IsFramework = false;
17106ddfca91SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
17116ddfca91SDouglas Gregor     consumeToken();
17126ddfca91SDouglas Gregor     IsFramework = true;
17136ddfca91SDouglas Gregor   }
17146ddfca91SDouglas Gregor 
17156ddfca91SDouglas Gregor   // Parse the library name
17166ddfca91SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
17176ddfca91SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
17186ddfca91SDouglas Gregor       << IsFramework << SourceRange(LinkLoc);
17196ddfca91SDouglas Gregor     HadError = true;
17206ddfca91SDouglas Gregor     return;
17216ddfca91SDouglas Gregor   }
17226ddfca91SDouglas Gregor 
17236ddfca91SDouglas Gregor   std::string LibraryName = Tok.getString();
17246ddfca91SDouglas Gregor   consumeToken();
17256ddfca91SDouglas Gregor   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
17266ddfca91SDouglas Gregor                                                             IsFramework));
17276ddfca91SDouglas Gregor }
17286ddfca91SDouglas Gregor 
172935b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration.
173035b13eceSDouglas Gregor ///
173135b13eceSDouglas Gregor ///   module-declaration:
173235b13eceSDouglas Gregor ///     'config_macros' attributes[opt] config-macro-list?
173335b13eceSDouglas Gregor ///
173435b13eceSDouglas Gregor ///   config-macro-list:
173535b13eceSDouglas Gregor ///     identifier (',' identifier)?
173635b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() {
173735b13eceSDouglas Gregor   assert(Tok.is(MMToken::ConfigMacros));
173835b13eceSDouglas Gregor   SourceLocation ConfigMacrosLoc = consumeToken();
173935b13eceSDouglas Gregor 
174035b13eceSDouglas Gregor   // Only top-level modules can have configuration macros.
174135b13eceSDouglas Gregor   if (ActiveModule->Parent) {
174235b13eceSDouglas Gregor     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
174335b13eceSDouglas Gregor   }
174435b13eceSDouglas Gregor 
174535b13eceSDouglas Gregor   // Parse the optional attributes.
174635b13eceSDouglas Gregor   Attributes Attrs;
174735b13eceSDouglas Gregor   parseOptionalAttributes(Attrs);
174835b13eceSDouglas Gregor   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
174935b13eceSDouglas Gregor     ActiveModule->ConfigMacrosExhaustive = true;
175035b13eceSDouglas Gregor   }
175135b13eceSDouglas Gregor 
175235b13eceSDouglas Gregor   // If we don't have an identifier, we're done.
175335b13eceSDouglas Gregor   if (!Tok.is(MMToken::Identifier))
175435b13eceSDouglas Gregor     return;
175535b13eceSDouglas Gregor 
175635b13eceSDouglas Gregor   // Consume the first identifier.
175735b13eceSDouglas Gregor   if (!ActiveModule->Parent) {
175835b13eceSDouglas Gregor     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
175935b13eceSDouglas Gregor   }
176035b13eceSDouglas Gregor   consumeToken();
176135b13eceSDouglas Gregor 
176235b13eceSDouglas Gregor   do {
176335b13eceSDouglas Gregor     // If there's a comma, consume it.
176435b13eceSDouglas Gregor     if (!Tok.is(MMToken::Comma))
176535b13eceSDouglas Gregor       break;
176635b13eceSDouglas Gregor     consumeToken();
176735b13eceSDouglas Gregor 
176835b13eceSDouglas Gregor     // We expect to see a macro name here.
176935b13eceSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
177035b13eceSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
177135b13eceSDouglas Gregor       break;
177235b13eceSDouglas Gregor     }
177335b13eceSDouglas Gregor 
177435b13eceSDouglas Gregor     // Consume the macro name.
177535b13eceSDouglas Gregor     if (!ActiveModule->Parent) {
177635b13eceSDouglas Gregor       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
177735b13eceSDouglas Gregor     }
177835b13eceSDouglas Gregor     consumeToken();
177935b13eceSDouglas Gregor   } while (true);
178035b13eceSDouglas Gregor }
178135b13eceSDouglas Gregor 
1782fb912657SDouglas Gregor /// \brief Format a module-id into a string.
1783fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) {
1784fb912657SDouglas Gregor   std::string result;
1785fb912657SDouglas Gregor   {
1786fb912657SDouglas Gregor     llvm::raw_string_ostream OS(result);
1787fb912657SDouglas Gregor 
1788fb912657SDouglas Gregor     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
1789fb912657SDouglas Gregor       if (I)
1790fb912657SDouglas Gregor         OS << ".";
1791fb912657SDouglas Gregor       OS << Id[I].first;
1792fb912657SDouglas Gregor     }
1793fb912657SDouglas Gregor   }
1794fb912657SDouglas Gregor 
1795fb912657SDouglas Gregor   return result;
1796fb912657SDouglas Gregor }
1797fb912657SDouglas Gregor 
1798fb912657SDouglas Gregor /// \brief Parse a conflict declaration.
1799fb912657SDouglas Gregor ///
1800fb912657SDouglas Gregor ///   module-declaration:
1801fb912657SDouglas Gregor ///     'conflict' module-id ',' string-literal
1802fb912657SDouglas Gregor void ModuleMapParser::parseConflict() {
1803fb912657SDouglas Gregor   assert(Tok.is(MMToken::Conflict));
1804fb912657SDouglas Gregor   SourceLocation ConflictLoc = consumeToken();
1805fb912657SDouglas Gregor   Module::UnresolvedConflict Conflict;
1806fb912657SDouglas Gregor 
1807fb912657SDouglas Gregor   // Parse the module-id.
1808fb912657SDouglas Gregor   if (parseModuleId(Conflict.Id))
1809fb912657SDouglas Gregor     return;
1810fb912657SDouglas Gregor 
1811fb912657SDouglas Gregor   // Parse the ','.
1812fb912657SDouglas Gregor   if (!Tok.is(MMToken::Comma)) {
1813fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
1814fb912657SDouglas Gregor       << SourceRange(ConflictLoc);
1815fb912657SDouglas Gregor     return;
1816fb912657SDouglas Gregor   }
1817fb912657SDouglas Gregor   consumeToken();
1818fb912657SDouglas Gregor 
1819fb912657SDouglas Gregor   // Parse the message.
1820fb912657SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1821fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
1822fb912657SDouglas Gregor       << formatModuleId(Conflict.Id);
1823fb912657SDouglas Gregor     return;
1824fb912657SDouglas Gregor   }
1825fb912657SDouglas Gregor   Conflict.Message = Tok.getString().str();
1826fb912657SDouglas Gregor   consumeToken();
1827fb912657SDouglas Gregor 
1828fb912657SDouglas Gregor   // Add this unresolved conflict.
1829fb912657SDouglas Gregor   ActiveModule->UnresolvedConflicts.push_back(Conflict);
1830fb912657SDouglas Gregor }
1831fb912657SDouglas Gregor 
18326ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules).
18339194a91dSDouglas Gregor ///
18349194a91dSDouglas Gregor ///   module-declaration:
18359194a91dSDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
18369194a91dSDouglas Gregor ///       { inferred-module-member* }
18379194a91dSDouglas Gregor ///
18389194a91dSDouglas Gregor ///   inferred-module-member:
18399194a91dSDouglas Gregor ///     'export' '*'
18409194a91dSDouglas Gregor ///     'exclude' identifier
18419194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
184273441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
184373441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
184473441091SDouglas Gregor   bool Failed = false;
184573441091SDouglas Gregor 
184673441091SDouglas Gregor   // Inferred modules must be submodules.
18479194a91dSDouglas Gregor   if (!ActiveModule && !Framework) {
184873441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
184973441091SDouglas Gregor     Failed = true;
185073441091SDouglas Gregor   }
185173441091SDouglas Gregor 
18529194a91dSDouglas Gregor   if (ActiveModule) {
1853524e33e1SDouglas Gregor     // Inferred modules must have umbrella directories.
1854524e33e1SDouglas Gregor     if (!Failed && !ActiveModule->getUmbrellaDir()) {
185573441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
185673441091SDouglas Gregor       Failed = true;
185773441091SDouglas Gregor     }
185873441091SDouglas Gregor 
185973441091SDouglas Gregor     // Check for redefinition of an inferred module.
1860dd005f69SDouglas Gregor     if (!Failed && ActiveModule->InferSubmodules) {
186173441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1862dd005f69SDouglas Gregor       if (ActiveModule->InferredSubmoduleLoc.isValid())
1863dd005f69SDouglas Gregor         Diags.Report(ActiveModule->InferredSubmoduleLoc,
186473441091SDouglas Gregor                      diag::note_mmap_prev_definition);
186573441091SDouglas Gregor       Failed = true;
186673441091SDouglas Gregor     }
186773441091SDouglas Gregor 
18689194a91dSDouglas Gregor     // Check for the 'framework' keyword, which is not permitted here.
18699194a91dSDouglas Gregor     if (Framework) {
18709194a91dSDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
18719194a91dSDouglas Gregor       Framework = false;
18729194a91dSDouglas Gregor     }
18739194a91dSDouglas Gregor   } else if (Explicit) {
18749194a91dSDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
18759194a91dSDouglas Gregor     Explicit = false;
18769194a91dSDouglas Gregor   }
18779194a91dSDouglas Gregor 
187873441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
187973441091SDouglas Gregor   if (Failed) {
188073441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
188173441091SDouglas Gregor       consumeToken();
188273441091SDouglas Gregor       skipUntil(MMToken::RBrace);
188373441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
188473441091SDouglas Gregor         consumeToken();
188573441091SDouglas Gregor     }
188673441091SDouglas Gregor     HadError = true;
188773441091SDouglas Gregor     return;
188873441091SDouglas Gregor   }
188973441091SDouglas Gregor 
18909194a91dSDouglas Gregor   // Parse optional attributes.
18914442605fSBill Wendling   Attributes Attrs;
18929194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
18939194a91dSDouglas Gregor 
18949194a91dSDouglas Gregor   if (ActiveModule) {
189573441091SDouglas Gregor     // Note that we have an inferred submodule.
1896dd005f69SDouglas Gregor     ActiveModule->InferSubmodules = true;
1897dd005f69SDouglas Gregor     ActiveModule->InferredSubmoduleLoc = StarLoc;
1898dd005f69SDouglas Gregor     ActiveModule->InferExplicitSubmodules = Explicit;
18999194a91dSDouglas Gregor   } else {
19009194a91dSDouglas Gregor     // We'll be inferring framework modules for this directory.
19019194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferModules = true;
19029194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
19039194a91dSDouglas Gregor   }
190473441091SDouglas Gregor 
190573441091SDouglas Gregor   // Parse the opening brace.
190673441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
190773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
190873441091SDouglas Gregor     HadError = true;
190973441091SDouglas Gregor     return;
191073441091SDouglas Gregor   }
191173441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
191273441091SDouglas Gregor 
191373441091SDouglas Gregor   // Parse the body of the inferred submodule.
191473441091SDouglas Gregor   bool Done = false;
191573441091SDouglas Gregor   do {
191673441091SDouglas Gregor     switch (Tok.Kind) {
191773441091SDouglas Gregor     case MMToken::EndOfFile:
191873441091SDouglas Gregor     case MMToken::RBrace:
191973441091SDouglas Gregor       Done = true;
192073441091SDouglas Gregor       break;
192173441091SDouglas Gregor 
19229194a91dSDouglas Gregor     case MMToken::ExcludeKeyword: {
19239194a91dSDouglas Gregor       if (ActiveModule) {
19249194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1925162405daSDouglas Gregor           << (ActiveModule != 0);
19269194a91dSDouglas Gregor         consumeToken();
19279194a91dSDouglas Gregor         break;
19289194a91dSDouglas Gregor       }
19299194a91dSDouglas Gregor 
19309194a91dSDouglas Gregor       consumeToken();
19319194a91dSDouglas Gregor       if (!Tok.is(MMToken::Identifier)) {
19329194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
19339194a91dSDouglas Gregor         break;
19349194a91dSDouglas Gregor       }
19359194a91dSDouglas Gregor 
19369194a91dSDouglas Gregor       Map.InferredDirectories[Directory].ExcludedModules
19379194a91dSDouglas Gregor         .push_back(Tok.getString());
19389194a91dSDouglas Gregor       consumeToken();
19399194a91dSDouglas Gregor       break;
19409194a91dSDouglas Gregor     }
19419194a91dSDouglas Gregor 
19429194a91dSDouglas Gregor     case MMToken::ExportKeyword:
19439194a91dSDouglas Gregor       if (!ActiveModule) {
19449194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1945162405daSDouglas Gregor           << (ActiveModule != 0);
19469194a91dSDouglas Gregor         consumeToken();
19479194a91dSDouglas Gregor         break;
19489194a91dSDouglas Gregor       }
19499194a91dSDouglas Gregor 
195073441091SDouglas Gregor       consumeToken();
195173441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1952dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
195373441091SDouglas Gregor       else
195473441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
195573441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
195673441091SDouglas Gregor       consumeToken();
195773441091SDouglas Gregor       break;
195873441091SDouglas Gregor 
195973441091SDouglas Gregor     case MMToken::ExplicitKeyword:
196073441091SDouglas Gregor     case MMToken::ModuleKeyword:
196173441091SDouglas Gregor     case MMToken::HeaderKeyword:
1962b53e5483SLawrence Crowl     case MMToken::PrivateKeyword:
196373441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
196473441091SDouglas Gregor     default:
19659194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1966162405daSDouglas Gregor           << (ActiveModule != 0);
196773441091SDouglas Gregor       consumeToken();
196873441091SDouglas Gregor       break;
196973441091SDouglas Gregor     }
197073441091SDouglas Gregor   } while (!Done);
197173441091SDouglas Gregor 
197273441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
197373441091SDouglas Gregor     consumeToken();
197473441091SDouglas Gregor   else {
197573441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
197673441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
197773441091SDouglas Gregor     HadError = true;
197873441091SDouglas Gregor   }
197973441091SDouglas Gregor }
198073441091SDouglas Gregor 
19819194a91dSDouglas Gregor /// \brief Parse optional attributes.
19829194a91dSDouglas Gregor ///
19839194a91dSDouglas Gregor ///   attributes:
19849194a91dSDouglas Gregor ///     attribute attributes
19859194a91dSDouglas Gregor ///     attribute
19869194a91dSDouglas Gregor ///
19879194a91dSDouglas Gregor ///   attribute:
19889194a91dSDouglas Gregor ///     [ identifier ]
19899194a91dSDouglas Gregor ///
19909194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes.
19919194a91dSDouglas Gregor ///
19929194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise.
19934442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
19949194a91dSDouglas Gregor   bool HadError = false;
19959194a91dSDouglas Gregor 
19969194a91dSDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
19979194a91dSDouglas Gregor     // Consume the '['.
19989194a91dSDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
19999194a91dSDouglas Gregor 
20009194a91dSDouglas Gregor     // Check whether we have an attribute name here.
20019194a91dSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
20029194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
20039194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
20049194a91dSDouglas Gregor       if (Tok.is(MMToken::RSquare))
20059194a91dSDouglas Gregor         consumeToken();
20069194a91dSDouglas Gregor       HadError = true;
20079194a91dSDouglas Gregor     }
20089194a91dSDouglas Gregor 
20099194a91dSDouglas Gregor     // Decode the attribute name.
20109194a91dSDouglas Gregor     AttributeKind Attribute
20119194a91dSDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
201235b13eceSDouglas Gregor           .Case("exhaustive", AT_exhaustive)
20139194a91dSDouglas Gregor           .Case("system", AT_system)
20149194a91dSDouglas Gregor           .Default(AT_unknown);
20159194a91dSDouglas Gregor     switch (Attribute) {
20169194a91dSDouglas Gregor     case AT_unknown:
20179194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
20189194a91dSDouglas Gregor         << Tok.getString();
20199194a91dSDouglas Gregor       break;
20209194a91dSDouglas Gregor 
20219194a91dSDouglas Gregor     case AT_system:
20229194a91dSDouglas Gregor       Attrs.IsSystem = true;
20239194a91dSDouglas Gregor       break;
202435b13eceSDouglas Gregor 
202535b13eceSDouglas Gregor     case AT_exhaustive:
202635b13eceSDouglas Gregor       Attrs.IsExhaustive = true;
202735b13eceSDouglas Gregor       break;
20289194a91dSDouglas Gregor     }
20299194a91dSDouglas Gregor     consumeToken();
20309194a91dSDouglas Gregor 
20319194a91dSDouglas Gregor     // Consume the ']'.
20329194a91dSDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
20339194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
20349194a91dSDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
20359194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
20369194a91dSDouglas Gregor       HadError = true;
20379194a91dSDouglas Gregor     }
20389194a91dSDouglas Gregor 
20399194a91dSDouglas Gregor     if (Tok.is(MMToken::RSquare))
20409194a91dSDouglas Gregor       consumeToken();
20419194a91dSDouglas Gregor   }
20429194a91dSDouglas Gregor 
20439194a91dSDouglas Gregor   return HadError;
20449194a91dSDouglas Gregor }
20459194a91dSDouglas Gregor 
20467033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
20477033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
20487033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
20497033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
20507033127bSDouglas Gregor     // If we have an umbrella directory, use that.
20517033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
20527033127bSDouglas Gregor       return Mod->getUmbrellaDir();
20537033127bSDouglas Gregor 
20547033127bSDouglas Gregor     // If we have a framework directory, stop looking.
20557033127bSDouglas Gregor     if (Mod->IsFramework)
20567033127bSDouglas Gregor       return 0;
20577033127bSDouglas Gregor   }
20587033127bSDouglas Gregor 
20597033127bSDouglas Gregor   return 0;
20607033127bSDouglas Gregor }
20617033127bSDouglas Gregor 
2062718292f2SDouglas Gregor /// \brief Parse a module map file.
2063718292f2SDouglas Gregor ///
2064718292f2SDouglas Gregor ///   module-map-file:
2065718292f2SDouglas Gregor ///     module-declaration*
2066718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
2067718292f2SDouglas Gregor   do {
2068718292f2SDouglas Gregor     switch (Tok.Kind) {
2069718292f2SDouglas Gregor     case MMToken::EndOfFile:
2070718292f2SDouglas Gregor       return HadError;
2071718292f2SDouglas Gregor 
2072e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
207397292843SDaniel Jasper     case MMToken::ExternKeyword:
2074718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
2075755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
2076718292f2SDouglas Gregor       parseModuleDecl();
2077718292f2SDouglas Gregor       break;
2078718292f2SDouglas Gregor 
20791fb5c3a6SDouglas Gregor     case MMToken::Comma:
208035b13eceSDouglas Gregor     case MMToken::ConfigMacros:
2081fb912657SDouglas Gregor     case MMToken::Conflict:
208259527666SDouglas Gregor     case MMToken::ExcludeKeyword:
20832b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
2084718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
2085718292f2SDouglas Gregor     case MMToken::Identifier:
2086718292f2SDouglas Gregor     case MMToken::LBrace:
20876ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
2088a686e1b0SDouglas Gregor     case MMToken::LSquare:
20892b82c2a5SDouglas Gregor     case MMToken::Period:
2090b53e5483SLawrence Crowl     case MMToken::PrivateKeyword:
2091718292f2SDouglas Gregor     case MMToken::RBrace:
2092a686e1b0SDouglas Gregor     case MMToken::RSquare:
20931fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
20942b82c2a5SDouglas Gregor     case MMToken::Star:
2095718292f2SDouglas Gregor     case MMToken::StringLiteral:
2096718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
2097ba7f2f71SDaniel Jasper     case MMToken::UseKeyword:
2098718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
2099718292f2SDouglas Gregor       HadError = true;
2100718292f2SDouglas Gregor       consumeToken();
2101718292f2SDouglas Gregor       break;
2102718292f2SDouglas Gregor     }
2103718292f2SDouglas Gregor   } while (true);
2104718292f2SDouglas Gregor }
2105718292f2SDouglas Gregor 
2106963c5535SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) {
21074ddf2221SDouglas Gregor   llvm::DenseMap<const FileEntry *, bool>::iterator Known
21084ddf2221SDouglas Gregor     = ParsedModuleMap.find(File);
21094ddf2221SDouglas Gregor   if (Known != ParsedModuleMap.end())
21104ddf2221SDouglas Gregor     return Known->second;
21114ddf2221SDouglas Gregor 
211289929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
2113718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
2114718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
2115718292f2SDouglas Gregor   if (!Buffer)
21164ddf2221SDouglas Gregor     return ParsedModuleMap[File] = true;
2117718292f2SDouglas Gregor 
2118718292f2SDouglas Gregor   // Parse this module map file.
21191fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
21201fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
2121bc10b9fbSDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(),
2122963c5535SDouglas Gregor                          BuiltinIncludeDir, IsSystem);
2123718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
2124718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
21254ddf2221SDouglas Gregor   ParsedModuleMap[File] = Result;
2126718292f2SDouglas Gregor   return Result;
2127718292f2SDouglas Gregor }
2128