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"
305257fc63SDouglas Gregor #include "llvm/Support/PathV2.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),
90*6f722b4eSArgyrios Kyrtzidis     BuiltinIncludeDir(0), CompilingModule(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 
171de3ef502SDouglas Gregor Module *ModuleMap::findModuleForHeader(const FileEntry *File) {
17259527666SDouglas Gregor   HeadersMap::iterator Known = Headers.find(File);
1731fb5c3a6SDouglas Gregor   if (Known != Headers.end()) {
17459527666SDouglas Gregor     // If a header is not available, don't report that it maps to anything.
17559527666SDouglas Gregor     if (!Known->second.isAvailable())
1761fb5c3a6SDouglas Gregor       return 0;
1771fb5c3a6SDouglas Gregor 
17859527666SDouglas Gregor     return Known->second.getModule();
1791fb5c3a6SDouglas Gregor   }
180ab0c8a84SDouglas Gregor 
18134d52749SDouglas Gregor   // If we've found a builtin header within Clang's builtin include directory,
18234d52749SDouglas Gregor   // load all of the module maps to see if it will get associated with a
18334d52749SDouglas Gregor   // specific module (e.g., in /usr/include).
18434d52749SDouglas Gregor   if (File->getDir() == BuiltinIncludeDir &&
18534d52749SDouglas Gregor       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
18634d52749SDouglas Gregor     SmallVector<Module *, 4> AllModules;
18734d52749SDouglas Gregor     HeaderInfo.collectAllModules(AllModules);
18834d52749SDouglas Gregor 
18934d52749SDouglas Gregor     // Check again.
19034d52749SDouglas Gregor     Known = Headers.find(File);
19134d52749SDouglas Gregor     if (Known != Headers.end()) {
19234d52749SDouglas Gregor       // If a header is not available, don't report that it maps to anything.
19334d52749SDouglas Gregor       if (!Known->second.isAvailable())
19434d52749SDouglas Gregor         return 0;
19534d52749SDouglas Gregor 
19634d52749SDouglas Gregor       return Known->second.getModule();
19734d52749SDouglas Gregor     }
19834d52749SDouglas Gregor   }
19934d52749SDouglas Gregor 
200b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
201f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
202e00c8b20SDouglas Gregor 
20374260502SDouglas Gregor   // Note: as an egregious but useful hack we use the real path here, because
20474260502SDouglas Gregor   // frameworks moving from top-level frameworks to embedded frameworks tend
20574260502SDouglas Gregor   // to be symlinked from the top-level location to the embedded location,
20674260502SDouglas Gregor   // and we need to resolve lookups as if we had found the embedded location.
207e00c8b20SDouglas Gregor   StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir);
208a89c5ac4SDouglas Gregor 
209a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
210a89c5ac4SDouglas Gregor   // an umbrella header.
211b65dbfffSDouglas Gregor   do {
212a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
213a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
214a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
215a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
216930a85ccSDouglas Gregor 
217930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
21873141fa9SDouglas Gregor       // directory.
219930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
22073141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
221930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
222930a85ccSDouglas Gregor 
223930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
224a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
225a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
226a89c5ac4SDouglas Gregor         // the actual header is located.
2279458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
2289458f82dSDouglas Gregor 
2297033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
230a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
231056396aeSDouglas Gregor           SmallString<32> NameBuf;
232056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
233056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
234056396aeSDouglas Gregor                              NameBuf);
235a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2369458f82dSDouglas Gregor                                       Explicit).first;
237a89c5ac4SDouglas Gregor 
238a89c5ac4SDouglas Gregor           // Associate the module and the directory.
239a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
240a89c5ac4SDouglas Gregor 
241a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
242a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
243930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
244a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
245a89c5ac4SDouglas Gregor         }
246a89c5ac4SDouglas Gregor 
247a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
248056396aeSDouglas Gregor         SmallString<32> NameBuf;
249056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
250056396aeSDouglas Gregor                            llvm::sys::path::stem(File->getName()), NameBuf);
251a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2529458f82dSDouglas Gregor                                     Explicit).first;
2533c5305c1SArgyrios Kyrtzidis         Result->addTopHeader(File);
254a89c5ac4SDouglas Gregor 
255a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
256a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
257930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
258a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
259a89c5ac4SDouglas Gregor       } else {
260a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
261a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
262a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
263a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
264a89c5ac4SDouglas Gregor       }
265a89c5ac4SDouglas Gregor 
26659527666SDouglas Gregor       Headers[File] = KnownHeader(Result, /*Excluded=*/false);
2671fb5c3a6SDouglas Gregor 
2681fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
2691fb5c3a6SDouglas Gregor       // that it maps to anything.
2701fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
2711fb5c3a6SDouglas Gregor         return 0;
2721fb5c3a6SDouglas Gregor 
273a89c5ac4SDouglas Gregor       return Result;
274a89c5ac4SDouglas Gregor     }
275a89c5ac4SDouglas Gregor 
276a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
277a89c5ac4SDouglas Gregor 
278b65dbfffSDouglas Gregor     // Retrieve our parent path.
279b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
280b65dbfffSDouglas Gregor     if (DirName.empty())
281b65dbfffSDouglas Gregor       break;
282b65dbfffSDouglas Gregor 
283b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
284b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
285a89c5ac4SDouglas Gregor   } while (Dir);
286b65dbfffSDouglas Gregor 
287ab0c8a84SDouglas Gregor   return 0;
288ab0c8a84SDouglas Gregor }
289ab0c8a84SDouglas Gregor 
290e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
291e4412640SArgyrios Kyrtzidis   HeadersMap::const_iterator Known = Headers.find(Header);
2921fb5c3a6SDouglas Gregor   if (Known != Headers.end())
29359527666SDouglas Gregor     return !Known->second.isAvailable();
2941fb5c3a6SDouglas Gregor 
2951fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
296f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
2971fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
2981fb5c3a6SDouglas Gregor 
2991fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
3001fb5c3a6SDouglas Gregor   // an umbrella header.
3011fb5c3a6SDouglas Gregor   do {
302e4412640SArgyrios Kyrtzidis     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
3031fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
3041fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
3051fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
3061fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
3071fb5c3a6SDouglas Gregor         return true;
3081fb5c3a6SDouglas Gregor 
3091fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
3101fb5c3a6SDouglas Gregor       // directory.
3111fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
3121fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
3131fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
3141fb5c3a6SDouglas Gregor 
3151fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
3161fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
3171fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
318056396aeSDouglas Gregor           SmallString<32> NameBuf;
319056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
320056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
321056396aeSDouglas Gregor                              NameBuf);
3221fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
3231fb5c3a6SDouglas Gregor           if (!Found)
3241fb5c3a6SDouglas Gregor             return false;
3251fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
3261fb5c3a6SDouglas Gregor             return true;
3271fb5c3a6SDouglas Gregor         }
3281fb5c3a6SDouglas Gregor 
3291fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
330056396aeSDouglas Gregor         SmallString<32> NameBuf;
331056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
332056396aeSDouglas Gregor                            llvm::sys::path::stem(Header->getName()),
333056396aeSDouglas Gregor                            NameBuf);
3341fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
3351fb5c3a6SDouglas Gregor         if (!Found)
3361fb5c3a6SDouglas Gregor           return false;
3371fb5c3a6SDouglas Gregor       }
3381fb5c3a6SDouglas Gregor 
3391fb5c3a6SDouglas Gregor       return !Found->isAvailable();
3401fb5c3a6SDouglas Gregor     }
3411fb5c3a6SDouglas Gregor 
3421fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
3431fb5c3a6SDouglas Gregor 
3441fb5c3a6SDouglas Gregor     // Retrieve our parent path.
3451fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
3461fb5c3a6SDouglas Gregor     if (DirName.empty())
3471fb5c3a6SDouglas Gregor       break;
3481fb5c3a6SDouglas Gregor 
3491fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
3501fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
3511fb5c3a6SDouglas Gregor   } while (Dir);
3521fb5c3a6SDouglas Gregor 
3531fb5c3a6SDouglas Gregor   return false;
3541fb5c3a6SDouglas Gregor }
3551fb5c3a6SDouglas Gregor 
356e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const {
357e4412640SArgyrios Kyrtzidis   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
35888bdfb0eSDouglas Gregor   if (Known != Modules.end())
35988bdfb0eSDouglas Gregor     return Known->getValue();
36088bdfb0eSDouglas Gregor 
36188bdfb0eSDouglas Gregor   return 0;
36288bdfb0eSDouglas Gregor }
36388bdfb0eSDouglas Gregor 
364e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
365e4412640SArgyrios Kyrtzidis                                            Module *Context) const {
3662b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
3672b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
3682b82c2a5SDouglas Gregor       return Sub;
3692b82c2a5SDouglas Gregor   }
3702b82c2a5SDouglas Gregor 
3712b82c2a5SDouglas Gregor   return findModule(Name);
3722b82c2a5SDouglas Gregor }
3732b82c2a5SDouglas Gregor 
374e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
3752b82c2a5SDouglas Gregor   if (!Context)
3762b82c2a5SDouglas Gregor     return findModule(Name);
3772b82c2a5SDouglas Gregor 
378eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
3792b82c2a5SDouglas Gregor }
3802b82c2a5SDouglas Gregor 
381de3ef502SDouglas Gregor std::pair<Module *, bool>
38269021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
38369021974SDouglas Gregor                               bool IsExplicit) {
38469021974SDouglas Gregor   // Try to find an existing module with this name.
385eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
386eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
38769021974SDouglas Gregor 
38869021974SDouglas Gregor   // Create a new module with this name.
38969021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
39069021974SDouglas Gregor                               IsExplicit);
391*6f722b4eSArgyrios Kyrtzidis   if (!Parent) {
39269021974SDouglas Gregor     Modules[Name] = Result;
393*6f722b4eSArgyrios Kyrtzidis     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
394*6f722b4eSArgyrios Kyrtzidis         Name == LangOpts.CurrentModule) {
395*6f722b4eSArgyrios Kyrtzidis       CompilingModule = Result;
396*6f722b4eSArgyrios Kyrtzidis     }
397*6f722b4eSArgyrios Kyrtzidis   }
39869021974SDouglas Gregor   return std::make_pair(Result, true);
39969021974SDouglas Gregor }
40069021974SDouglas Gregor 
4019194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
402e4412640SArgyrios Kyrtzidis                                         StringRef Name, bool &IsSystem) const {
4039194a91dSDouglas Gregor   // Check whether we have already looked into the parent directory
4049194a91dSDouglas Gregor   // for a module map.
405e4412640SArgyrios Kyrtzidis   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4069194a91dSDouglas Gregor     inferred = InferredDirectories.find(ParentDir);
4079194a91dSDouglas Gregor   if (inferred == InferredDirectories.end())
4089194a91dSDouglas Gregor     return false;
4099194a91dSDouglas Gregor 
4109194a91dSDouglas Gregor   if (!inferred->second.InferModules)
4119194a91dSDouglas Gregor     return false;
4129194a91dSDouglas Gregor 
4139194a91dSDouglas Gregor   // We're allowed to infer for this directory, but make sure it's okay
4149194a91dSDouglas Gregor   // to infer this particular module.
4159194a91dSDouglas Gregor   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
4169194a91dSDouglas Gregor                             inferred->second.ExcludedModules.end(),
4179194a91dSDouglas Gregor                             Name) == inferred->second.ExcludedModules.end();
4189194a91dSDouglas Gregor 
4199194a91dSDouglas Gregor   if (canInfer && inferred->second.InferSystemModules)
4209194a91dSDouglas Gregor     IsSystem = true;
4219194a91dSDouglas Gregor 
4229194a91dSDouglas Gregor   return canInfer;
4239194a91dSDouglas Gregor }
4249194a91dSDouglas Gregor 
42511dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we
42611dfe6feSDouglas Gregor /// should link.
42711dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
42811dfe6feSDouglas Gregor                                FileManager &FileMgr) {
42911dfe6feSDouglas Gregor   assert(Mod->IsFramework && "Can only infer linking for framework modules");
43011dfe6feSDouglas Gregor   assert(!Mod->isSubFramework() &&
43111dfe6feSDouglas Gregor          "Can only infer linking for top-level frameworks");
43211dfe6feSDouglas Gregor 
43311dfe6feSDouglas Gregor   SmallString<128> LibName;
43411dfe6feSDouglas Gregor   LibName += FrameworkDir->getName();
43511dfe6feSDouglas Gregor   llvm::sys::path::append(LibName, Mod->Name);
43611dfe6feSDouglas Gregor   if (FileMgr.getFile(LibName)) {
43711dfe6feSDouglas Gregor     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
43811dfe6feSDouglas Gregor                                                      /*IsFramework=*/true));
43911dfe6feSDouglas Gregor   }
44011dfe6feSDouglas Gregor }
44111dfe6feSDouglas Gregor 
442de3ef502SDouglas Gregor Module *
44356c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
444e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
445a686e1b0SDouglas Gregor                                 bool IsSystem,
446e89dbc1dSDouglas Gregor                                 Module *Parent) {
44756c64013SDouglas Gregor   // Check whether we've already found this module.
448e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
449e89dbc1dSDouglas Gregor     return Mod;
450e89dbc1dSDouglas Gregor 
451e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
45256c64013SDouglas Gregor 
4539194a91dSDouglas Gregor   // If the framework has a parent path from which we're allowed to infer
4549194a91dSDouglas Gregor   // a framework module, do so.
4559194a91dSDouglas Gregor   if (!Parent) {
4564ddf2221SDouglas Gregor     // Determine whether we're allowed to infer a module map.
457e00c8b20SDouglas Gregor 
4584ddf2221SDouglas Gregor     // Note: as an egregious but useful hack we use the real path here, because
4594ddf2221SDouglas Gregor     // we might be looking at an embedded framework that symlinks out to a
4604ddf2221SDouglas Gregor     // top-level framework, and we need to infer as if we were naming the
4614ddf2221SDouglas Gregor     // top-level framework.
462e00c8b20SDouglas Gregor     StringRef FrameworkDirName
463e00c8b20SDouglas Gregor       = SourceMgr->getFileManager().getCanonicalName(FrameworkDir);
4644ddf2221SDouglas Gregor 
4659194a91dSDouglas Gregor     bool canInfer = false;
4664ddf2221SDouglas Gregor     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
4679194a91dSDouglas Gregor       // Figure out the parent path.
4684ddf2221SDouglas Gregor       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
4699194a91dSDouglas Gregor       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
4709194a91dSDouglas Gregor         // Check whether we have already looked into the parent directory
4719194a91dSDouglas Gregor         // for a module map.
472e4412640SArgyrios Kyrtzidis         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4739194a91dSDouglas Gregor           inferred = InferredDirectories.find(ParentDir);
4749194a91dSDouglas Gregor         if (inferred == InferredDirectories.end()) {
4759194a91dSDouglas Gregor           // We haven't looked here before. Load a module map, if there is
4769194a91dSDouglas Gregor           // one.
4779194a91dSDouglas Gregor           SmallString<128> ModMapPath = Parent;
4789194a91dSDouglas Gregor           llvm::sys::path::append(ModMapPath, "module.map");
4799194a91dSDouglas Gregor           if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
4809194a91dSDouglas Gregor             parseModuleMapFile(ModMapFile);
4819194a91dSDouglas Gregor             inferred = InferredDirectories.find(ParentDir);
4829194a91dSDouglas Gregor           }
4839194a91dSDouglas Gregor 
4849194a91dSDouglas Gregor           if (inferred == InferredDirectories.end())
4859194a91dSDouglas Gregor             inferred = InferredDirectories.insert(
4869194a91dSDouglas Gregor                          std::make_pair(ParentDir, InferredDirectory())).first;
4879194a91dSDouglas Gregor         }
4889194a91dSDouglas Gregor 
4899194a91dSDouglas Gregor         if (inferred->second.InferModules) {
4909194a91dSDouglas Gregor           // We're allowed to infer for this directory, but make sure it's okay
4919194a91dSDouglas Gregor           // to infer this particular module.
4924ddf2221SDouglas Gregor           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
4939194a91dSDouglas Gregor           canInfer = std::find(inferred->second.ExcludedModules.begin(),
4949194a91dSDouglas Gregor                                inferred->second.ExcludedModules.end(),
4959194a91dSDouglas Gregor                                Name) == inferred->second.ExcludedModules.end();
4969194a91dSDouglas Gregor 
4979194a91dSDouglas Gregor           if (inferred->second.InferSystemModules)
4989194a91dSDouglas Gregor             IsSystem = true;
4999194a91dSDouglas Gregor         }
5009194a91dSDouglas Gregor       }
5019194a91dSDouglas Gregor     }
5029194a91dSDouglas Gregor 
5039194a91dSDouglas Gregor     // If we're not allowed to infer a framework module, don't.
5049194a91dSDouglas Gregor     if (!canInfer)
5059194a91dSDouglas Gregor       return 0;
5069194a91dSDouglas Gregor   }
5079194a91dSDouglas Gregor 
5089194a91dSDouglas Gregor 
50956c64013SDouglas Gregor   // Look for an umbrella header.
5102c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
51156c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
51256c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
513e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
51456c64013SDouglas Gregor 
51556c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
51656c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
51756c64013SDouglas Gregor   // idea.
51856c64013SDouglas Gregor   if (!UmbrellaHeader)
51956c64013SDouglas Gregor     return 0;
52056c64013SDouglas Gregor 
521e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
522e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
523a686e1b0SDouglas Gregor   if (IsSystem)
524a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
525a686e1b0SDouglas Gregor 
526eb90e830SDouglas Gregor   if (!Parent)
527e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
528e89dbc1dSDouglas Gregor 
529322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
53073141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
53159527666SDouglas Gregor   Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false);
5324dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
533d8bd7537SDouglas Gregor 
534d8bd7537SDouglas Gregor   // export *
535d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
536d8bd7537SDouglas Gregor 
537a89c5ac4SDouglas Gregor   // module * { export * }
538a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
539a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
540a89c5ac4SDouglas Gregor 
541e89dbc1dSDouglas Gregor   // Look for subframeworks.
542e89dbc1dSDouglas Gregor   llvm::error_code EC;
5432c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
544ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
545e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
5462c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirNameNative;
547ddaa69cbSDouglas Gregor   llvm::sys::path::native(SubframeworksDirName.str(),
548ddaa69cbSDouglas Gregor                           SubframeworksDirNameNative);
549ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
550ddaa69cbSDouglas Gregor          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
551e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
552e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
553e89dbc1dSDouglas Gregor       continue;
554f2161a70SDouglas Gregor 
555e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
556e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
55707c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
55807c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
55907c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
56007c22b78SDouglas Gregor       // out to a top-level framework.
561e00c8b20SDouglas Gregor       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
56207c22b78SDouglas Gregor       bool FoundParent = false;
56307c22b78SDouglas Gregor       do {
56407c22b78SDouglas Gregor         // Get the parent directory name.
56507c22b78SDouglas Gregor         SubframeworkDirName
56607c22b78SDouglas Gregor           = llvm::sys::path::parent_path(SubframeworkDirName);
56707c22b78SDouglas Gregor         if (SubframeworkDirName.empty())
56807c22b78SDouglas Gregor           break;
56907c22b78SDouglas Gregor 
57007c22b78SDouglas Gregor         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
57107c22b78SDouglas Gregor           FoundParent = true;
57207c22b78SDouglas Gregor           break;
57307c22b78SDouglas Gregor         }
57407c22b78SDouglas Gregor       } while (true);
57507c22b78SDouglas Gregor 
57607c22b78SDouglas Gregor       if (!FoundParent)
57707c22b78SDouglas Gregor         continue;
57807c22b78SDouglas Gregor 
579e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
580056396aeSDouglas Gregor       SmallString<32> NameBuf;
581056396aeSDouglas Gregor       inferFrameworkModule(sanitizeFilenameAsIdentifier(
582056396aeSDouglas Gregor                              llvm::sys::path::stem(Dir->path()), NameBuf),
583056396aeSDouglas Gregor                            SubframeworkDir, IsSystem, Result);
584e89dbc1dSDouglas Gregor     }
585e89dbc1dSDouglas Gregor   }
586e89dbc1dSDouglas Gregor 
58711dfe6feSDouglas Gregor   // If the module is a top-level framework, automatically link against the
58811dfe6feSDouglas Gregor   // framework.
58911dfe6feSDouglas Gregor   if (!Result->isSubFramework()) {
59011dfe6feSDouglas Gregor     inferFrameworkLink(Result, FrameworkDir, FileMgr);
59111dfe6feSDouglas Gregor   }
59211dfe6feSDouglas Gregor 
59356c64013SDouglas Gregor   return Result;
59456c64013SDouglas Gregor }
59556c64013SDouglas Gregor 
596a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
59759527666SDouglas Gregor   Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false);
59873141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
5997033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
600a89c5ac4SDouglas Gregor }
601a89c5ac4SDouglas Gregor 
602524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
603524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
604524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
605524e33e1SDouglas Gregor }
606524e33e1SDouglas Gregor 
60759527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
60859527666SDouglas Gregor                           bool Excluded) {
609b146baabSArgyrios Kyrtzidis   if (Excluded) {
61059527666SDouglas Gregor     Mod->ExcludedHeaders.push_back(Header);
611b146baabSArgyrios Kyrtzidis   } else {
612a89c5ac4SDouglas Gregor     Mod->Headers.push_back(Header);
613*6f722b4eSArgyrios Kyrtzidis     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
614*6f722b4eSArgyrios Kyrtzidis     HeaderInfo.MarkFileModuleHeader(Header, isCompilingModuleHeader);
615b146baabSArgyrios Kyrtzidis   }
61659527666SDouglas Gregor   Headers[Header] = KnownHeader(Mod, Excluded);
617a89c5ac4SDouglas Gregor }
618a89c5ac4SDouglas Gregor 
619514b636aSDouglas Gregor const FileEntry *
620e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const {
621514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
622514b636aSDouglas Gregor     return 0;
623514b636aSDouglas Gregor 
624514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
625514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
626514b636aSDouglas Gregor }
627514b636aSDouglas Gregor 
628718292f2SDouglas Gregor void ModuleMap::dump() {
629718292f2SDouglas Gregor   llvm::errs() << "Modules:";
630718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
631718292f2SDouglas Gregor                                         MEnd = Modules.end();
632718292f2SDouglas Gregor        M != MEnd; ++M)
633d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
634718292f2SDouglas Gregor 
635718292f2SDouglas Gregor   llvm::errs() << "Headers:";
63659527666SDouglas Gregor   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
637718292f2SDouglas Gregor        H != HEnd; ++H) {
638718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
63959527666SDouglas Gregor                  << H->second.getModule()->getFullModuleName() << "\n";
640718292f2SDouglas Gregor   }
641718292f2SDouglas Gregor }
642718292f2SDouglas Gregor 
6432b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
6442b82c2a5SDouglas Gregor   bool HadError = false;
6452b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
6462b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
6472b82c2a5SDouglas Gregor                                               Complain);
648f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
6492b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
6502b82c2a5SDouglas Gregor     else
6512b82c2a5SDouglas Gregor       HadError = true;
6522b82c2a5SDouglas Gregor   }
6532b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
6542b82c2a5SDouglas Gregor   return HadError;
6552b82c2a5SDouglas Gregor }
6562b82c2a5SDouglas Gregor 
657fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
658fb912657SDouglas Gregor   bool HadError = false;
659fb912657SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
660fb912657SDouglas Gregor     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
661fb912657SDouglas Gregor                                        Mod, Complain);
662fb912657SDouglas Gregor     if (!OtherMod) {
663fb912657SDouglas Gregor       HadError = true;
664fb912657SDouglas Gregor       continue;
665fb912657SDouglas Gregor     }
666fb912657SDouglas Gregor 
667fb912657SDouglas Gregor     Module::Conflict Conflict;
668fb912657SDouglas Gregor     Conflict.Other = OtherMod;
669fb912657SDouglas Gregor     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
670fb912657SDouglas Gregor     Mod->Conflicts.push_back(Conflict);
671fb912657SDouglas Gregor   }
672fb912657SDouglas Gregor   Mod->UnresolvedConflicts.clear();
673fb912657SDouglas Gregor   return HadError;
674fb912657SDouglas Gregor }
675fb912657SDouglas Gregor 
6760093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
6770093b3c7SDouglas Gregor   if (Loc.isInvalid())
6780093b3c7SDouglas Gregor     return 0;
6790093b3c7SDouglas Gregor 
6800093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
6810093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
6820093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
6830093b3c7SDouglas Gregor     return 0;
6840093b3c7SDouglas Gregor 
6850093b3c7SDouglas Gregor 
6860093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
6870093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
688224d8a74SDouglas Gregor 
689224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
690224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
691224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
692224d8a74SDouglas Gregor     if (Module *Mod = findModuleForHeader(ExpansionFile))
693224d8a74SDouglas Gregor       return Mod;
694224d8a74SDouglas Gregor 
695224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
696224d8a74SDouglas Gregor     // any included header has an associated module.
697224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
698224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
6990093b3c7SDouglas Gregor       return 0;
7000093b3c7SDouglas Gregor 
701224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
702224d8a74SDouglas Gregor   }
703224d8a74SDouglas Gregor 
704224d8a74SDouglas Gregor   return 0;
7050093b3c7SDouglas Gregor }
7060093b3c7SDouglas Gregor 
707718292f2SDouglas Gregor //----------------------------------------------------------------------------//
708718292f2SDouglas Gregor // Module map file parser
709718292f2SDouglas Gregor //----------------------------------------------------------------------------//
710718292f2SDouglas Gregor 
711718292f2SDouglas Gregor namespace clang {
712718292f2SDouglas Gregor   /// \brief A token in a module map file.
713718292f2SDouglas Gregor   struct MMToken {
714718292f2SDouglas Gregor     enum TokenKind {
7151fb5c3a6SDouglas Gregor       Comma,
71635b13eceSDouglas Gregor       ConfigMacros,
717fb912657SDouglas Gregor       Conflict,
718718292f2SDouglas Gregor       EndOfFile,
719718292f2SDouglas Gregor       HeaderKeyword,
720718292f2SDouglas Gregor       Identifier,
72159527666SDouglas Gregor       ExcludeKeyword,
722718292f2SDouglas Gregor       ExplicitKeyword,
7232b82c2a5SDouglas Gregor       ExportKeyword,
724755b2055SDouglas Gregor       FrameworkKeyword,
7256ddfca91SDouglas Gregor       LinkKeyword,
726718292f2SDouglas Gregor       ModuleKeyword,
7272b82c2a5SDouglas Gregor       Period,
728718292f2SDouglas Gregor       UmbrellaKeyword,
7291fb5c3a6SDouglas Gregor       RequiresKeyword,
7302b82c2a5SDouglas Gregor       Star,
731718292f2SDouglas Gregor       StringLiteral,
732718292f2SDouglas Gregor       LBrace,
733a686e1b0SDouglas Gregor       RBrace,
734a686e1b0SDouglas Gregor       LSquare,
735a686e1b0SDouglas Gregor       RSquare
736718292f2SDouglas Gregor     } Kind;
737718292f2SDouglas Gregor 
738718292f2SDouglas Gregor     unsigned Location;
739718292f2SDouglas Gregor     unsigned StringLength;
740718292f2SDouglas Gregor     const char *StringData;
741718292f2SDouglas Gregor 
742718292f2SDouglas Gregor     void clear() {
743718292f2SDouglas Gregor       Kind = EndOfFile;
744718292f2SDouglas Gregor       Location = 0;
745718292f2SDouglas Gregor       StringLength = 0;
746718292f2SDouglas Gregor       StringData = 0;
747718292f2SDouglas Gregor     }
748718292f2SDouglas Gregor 
749718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
750718292f2SDouglas Gregor 
751718292f2SDouglas Gregor     SourceLocation getLocation() const {
752718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
753718292f2SDouglas Gregor     }
754718292f2SDouglas Gregor 
755718292f2SDouglas Gregor     StringRef getString() const {
756718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
757718292f2SDouglas Gregor     }
758718292f2SDouglas Gregor   };
759718292f2SDouglas Gregor 
7609194a91dSDouglas Gregor   /// \brief The set of attributes that can be attached to a module.
7614442605fSBill Wendling   struct Attributes {
76235b13eceSDouglas Gregor     Attributes() : IsSystem(), IsExhaustive() { }
7639194a91dSDouglas Gregor 
7649194a91dSDouglas Gregor     /// \brief Whether this is a system module.
7659194a91dSDouglas Gregor     unsigned IsSystem : 1;
76635b13eceSDouglas Gregor 
76735b13eceSDouglas Gregor     /// \brief Whether this is an exhaustive set of configuration macros.
76835b13eceSDouglas Gregor     unsigned IsExhaustive : 1;
7699194a91dSDouglas Gregor   };
7709194a91dSDouglas Gregor 
7719194a91dSDouglas Gregor 
772718292f2SDouglas Gregor   class ModuleMapParser {
773718292f2SDouglas Gregor     Lexer &L;
774718292f2SDouglas Gregor     SourceManager &SourceMgr;
775bc10b9fbSDouglas Gregor 
776bc10b9fbSDouglas Gregor     /// \brief Default target information, used only for string literal
777bc10b9fbSDouglas Gregor     /// parsing.
778bc10b9fbSDouglas Gregor     const TargetInfo *Target;
779bc10b9fbSDouglas Gregor 
780718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
781718292f2SDouglas Gregor     ModuleMap &Map;
782718292f2SDouglas Gregor 
7835257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
7845257fc63SDouglas Gregor     const DirectoryEntry *Directory;
7855257fc63SDouglas Gregor 
7863ec6663bSDouglas Gregor     /// \brief The directory containing Clang-supplied headers.
7873ec6663bSDouglas Gregor     const DirectoryEntry *BuiltinIncludeDir;
7883ec6663bSDouglas Gregor 
789718292f2SDouglas Gregor     /// \brief Whether an error occurred.
790718292f2SDouglas Gregor     bool HadError;
791718292f2SDouglas Gregor 
792718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
793718292f2SDouglas Gregor     /// during parsing.
794718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
795718292f2SDouglas Gregor 
796718292f2SDouglas Gregor     /// \brief The current token.
797718292f2SDouglas Gregor     MMToken Tok;
798718292f2SDouglas Gregor 
799718292f2SDouglas Gregor     /// \brief The active module.
800de3ef502SDouglas Gregor     Module *ActiveModule;
801718292f2SDouglas Gregor 
802718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
803718292f2SDouglas Gregor     SourceLocation consumeToken();
804718292f2SDouglas Gregor 
805718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
806718292f2SDouglas Gregor     /// (or the end of the file).
807718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
808718292f2SDouglas Gregor 
809f857950dSDmitri Gribenko     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
810e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
811718292f2SDouglas Gregor     void parseModuleDecl();
8121fb5c3a6SDouglas Gregor     void parseRequiresDecl();
81359527666SDouglas Gregor     void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc);
814524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
8152b82c2a5SDouglas Gregor     void parseExportDecl();
8166ddfca91SDouglas Gregor     void parseLinkDecl();
81735b13eceSDouglas Gregor     void parseConfigMacros();
818fb912657SDouglas Gregor     void parseConflict();
8199194a91dSDouglas Gregor     void parseInferredModuleDecl(bool Framework, bool Explicit);
8204442605fSBill Wendling     bool parseOptionalAttributes(Attributes &Attrs);
821718292f2SDouglas Gregor 
8227033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
8237033127bSDouglas Gregor 
824718292f2SDouglas Gregor   public:
825718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
826bc10b9fbSDouglas Gregor                              const TargetInfo *Target,
827718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
8285257fc63SDouglas Gregor                              ModuleMap &Map,
8293ec6663bSDouglas Gregor                              const DirectoryEntry *Directory,
8303ec6663bSDouglas Gregor                              const DirectoryEntry *BuiltinIncludeDir)
831bc10b9fbSDouglas Gregor       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
8323ec6663bSDouglas Gregor         Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
8333ec6663bSDouglas Gregor         HadError(false), ActiveModule(0)
834718292f2SDouglas Gregor     {
835718292f2SDouglas Gregor       Tok.clear();
836718292f2SDouglas Gregor       consumeToken();
837718292f2SDouglas Gregor     }
838718292f2SDouglas Gregor 
839718292f2SDouglas Gregor     bool parseModuleMapFile();
840718292f2SDouglas Gregor   };
841718292f2SDouglas Gregor }
842718292f2SDouglas Gregor 
843718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
844718292f2SDouglas Gregor retry:
845718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
846718292f2SDouglas Gregor   Tok.clear();
847718292f2SDouglas Gregor 
848718292f2SDouglas Gregor   Token LToken;
849718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
850718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
851718292f2SDouglas Gregor   switch (LToken.getKind()) {
852718292f2SDouglas Gregor   case tok::raw_identifier:
853718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
854718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
855718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
85635b13eceSDouglas Gregor                  .Case("config_macros", MMToken::ConfigMacros)
857fb912657SDouglas Gregor                  .Case("conflict", MMToken::Conflict)
85859527666SDouglas Gregor                  .Case("exclude", MMToken::ExcludeKeyword)
859718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
8602b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
861755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
86235b13eceSDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
8636ddfca91SDouglas Gregor                  .Case("link", MMToken::LinkKeyword)
864718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
8651fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
866718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
867718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
868718292f2SDouglas Gregor     break;
869718292f2SDouglas Gregor 
8701fb5c3a6SDouglas Gregor   case tok::comma:
8711fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
8721fb5c3a6SDouglas Gregor     break;
8731fb5c3a6SDouglas Gregor 
874718292f2SDouglas Gregor   case tok::eof:
875718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
876718292f2SDouglas Gregor     break;
877718292f2SDouglas Gregor 
878718292f2SDouglas Gregor   case tok::l_brace:
879718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
880718292f2SDouglas Gregor     break;
881718292f2SDouglas Gregor 
882a686e1b0SDouglas Gregor   case tok::l_square:
883a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
884a686e1b0SDouglas Gregor     break;
885a686e1b0SDouglas Gregor 
8862b82c2a5SDouglas Gregor   case tok::period:
8872b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
8882b82c2a5SDouglas Gregor     break;
8892b82c2a5SDouglas Gregor 
890718292f2SDouglas Gregor   case tok::r_brace:
891718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
892718292f2SDouglas Gregor     break;
893718292f2SDouglas Gregor 
894a686e1b0SDouglas Gregor   case tok::r_square:
895a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
896a686e1b0SDouglas Gregor     break;
897a686e1b0SDouglas Gregor 
8982b82c2a5SDouglas Gregor   case tok::star:
8992b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
9002b82c2a5SDouglas Gregor     break;
9012b82c2a5SDouglas Gregor 
902718292f2SDouglas Gregor   case tok::string_literal: {
903d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
904d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
905d67aea28SRichard Smith       HadError = true;
906d67aea28SRichard Smith       goto retry;
907d67aea28SRichard Smith     }
908d67aea28SRichard Smith 
909718292f2SDouglas Gregor     // Parse the string literal.
910718292f2SDouglas Gregor     LangOptions LangOpts;
911718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
912718292f2SDouglas Gregor     if (StringLiteral.hadError)
913718292f2SDouglas Gregor       goto retry;
914718292f2SDouglas Gregor 
915718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
916718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
917718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
918718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
919718292f2SDouglas Gregor     Saved[Length] = 0;
920718292f2SDouglas Gregor 
921718292f2SDouglas Gregor     // Form the token.
922718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
923718292f2SDouglas Gregor     Tok.StringData = Saved;
924718292f2SDouglas Gregor     Tok.StringLength = Length;
925718292f2SDouglas Gregor     break;
926718292f2SDouglas Gregor   }
927718292f2SDouglas Gregor 
928718292f2SDouglas Gregor   case tok::comment:
929718292f2SDouglas Gregor     goto retry;
930718292f2SDouglas Gregor 
931718292f2SDouglas Gregor   default:
932718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
933718292f2SDouglas Gregor     HadError = true;
934718292f2SDouglas Gregor     goto retry;
935718292f2SDouglas Gregor   }
936718292f2SDouglas Gregor 
937718292f2SDouglas Gregor   return Result;
938718292f2SDouglas Gregor }
939718292f2SDouglas Gregor 
940718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
941718292f2SDouglas Gregor   unsigned braceDepth = 0;
942a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
943718292f2SDouglas Gregor   do {
944718292f2SDouglas Gregor     switch (Tok.Kind) {
945718292f2SDouglas Gregor     case MMToken::EndOfFile:
946718292f2SDouglas Gregor       return;
947718292f2SDouglas Gregor 
948718292f2SDouglas Gregor     case MMToken::LBrace:
949a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
950718292f2SDouglas Gregor         return;
951718292f2SDouglas Gregor 
952718292f2SDouglas Gregor       ++braceDepth;
953718292f2SDouglas Gregor       break;
954718292f2SDouglas Gregor 
955a686e1b0SDouglas Gregor     case MMToken::LSquare:
956a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
957a686e1b0SDouglas Gregor         return;
958a686e1b0SDouglas Gregor 
959a686e1b0SDouglas Gregor       ++squareDepth;
960a686e1b0SDouglas Gregor       break;
961a686e1b0SDouglas Gregor 
962718292f2SDouglas Gregor     case MMToken::RBrace:
963718292f2SDouglas Gregor       if (braceDepth > 0)
964718292f2SDouglas Gregor         --braceDepth;
965718292f2SDouglas Gregor       else if (Tok.is(K))
966718292f2SDouglas Gregor         return;
967718292f2SDouglas Gregor       break;
968718292f2SDouglas Gregor 
969a686e1b0SDouglas Gregor     case MMToken::RSquare:
970a686e1b0SDouglas Gregor       if (squareDepth > 0)
971a686e1b0SDouglas Gregor         --squareDepth;
972a686e1b0SDouglas Gregor       else if (Tok.is(K))
973a686e1b0SDouglas Gregor         return;
974a686e1b0SDouglas Gregor       break;
975a686e1b0SDouglas Gregor 
976718292f2SDouglas Gregor     default:
977a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
978718292f2SDouglas Gregor         return;
979718292f2SDouglas Gregor       break;
980718292f2SDouglas Gregor     }
981718292f2SDouglas Gregor 
982718292f2SDouglas Gregor    consumeToken();
983718292f2SDouglas Gregor   } while (true);
984718292f2SDouglas Gregor }
985718292f2SDouglas Gregor 
986e7ab3669SDouglas Gregor /// \brief Parse a module-id.
987e7ab3669SDouglas Gregor ///
988e7ab3669SDouglas Gregor ///   module-id:
989e7ab3669SDouglas Gregor ///     identifier
990e7ab3669SDouglas Gregor ///     identifier '.' module-id
991e7ab3669SDouglas Gregor ///
992e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
993e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
994e7ab3669SDouglas Gregor   Id.clear();
995e7ab3669SDouglas Gregor   do {
996e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
997e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
998e7ab3669SDouglas Gregor       consumeToken();
999e7ab3669SDouglas Gregor     } else {
1000e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1001e7ab3669SDouglas Gregor       return true;
1002e7ab3669SDouglas Gregor     }
1003e7ab3669SDouglas Gregor 
1004e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
1005e7ab3669SDouglas Gregor       break;
1006e7ab3669SDouglas Gregor 
1007e7ab3669SDouglas Gregor     consumeToken();
1008e7ab3669SDouglas Gregor   } while (true);
1009e7ab3669SDouglas Gregor 
1010e7ab3669SDouglas Gregor   return false;
1011e7ab3669SDouglas Gregor }
1012e7ab3669SDouglas Gregor 
1013a686e1b0SDouglas Gregor namespace {
1014a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
1015a686e1b0SDouglas Gregor   enum AttributeKind {
1016a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
1017a686e1b0SDouglas Gregor     AT_unknown,
1018a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
101935b13eceSDouglas Gregor     AT_system,
102035b13eceSDouglas Gregor     /// \brief The 'exhaustive' attribute.
102135b13eceSDouglas Gregor     AT_exhaustive
1022a686e1b0SDouglas Gregor   };
1023a686e1b0SDouglas Gregor }
1024a686e1b0SDouglas Gregor 
1025718292f2SDouglas Gregor /// \brief Parse a module declaration.
1026718292f2SDouglas Gregor ///
1027718292f2SDouglas Gregor ///   module-declaration:
1028a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1029a686e1b0SDouglas Gregor ///       { module-member* }
1030a686e1b0SDouglas Gregor ///
1031718292f2SDouglas Gregor ///   module-member:
10321fb5c3a6SDouglas Gregor ///     requires-declaration
1033718292f2SDouglas Gregor ///     header-declaration
1034e7ab3669SDouglas Gregor ///     submodule-declaration
10352b82c2a5SDouglas Gregor ///     export-declaration
10366ddfca91SDouglas Gregor ///     link-declaration
103773441091SDouglas Gregor ///
103873441091SDouglas Gregor ///   submodule-declaration:
103973441091SDouglas Gregor ///     module-declaration
104073441091SDouglas Gregor ///     inferred-submodule-declaration
1041718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
1042755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1043755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
1044f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
1045e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
1046718292f2SDouglas Gregor   bool Explicit = false;
1047f2161a70SDouglas Gregor   bool Framework = false;
1048755b2055SDouglas Gregor 
1049f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
1050f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
1051e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
1052f2161a70SDouglas Gregor     Explicit = true;
1053f2161a70SDouglas Gregor   }
1054f2161a70SDouglas Gregor 
1055f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
1056755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
1057755b2055SDouglas Gregor     consumeToken();
1058755b2055SDouglas Gregor     Framework = true;
1059755b2055SDouglas Gregor   }
1060718292f2SDouglas Gregor 
1061718292f2SDouglas Gregor   // Parse 'module' keyword.
1062718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
1063d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1064718292f2SDouglas Gregor     consumeToken();
1065718292f2SDouglas Gregor     HadError = true;
1066718292f2SDouglas Gregor     return;
1067718292f2SDouglas Gregor   }
1068718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
1069718292f2SDouglas Gregor 
107073441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
107173441091SDouglas Gregor   // Parse it.
107273441091SDouglas Gregor   if (Tok.is(MMToken::Star))
10739194a91dSDouglas Gregor     return parseInferredModuleDecl(Framework, Explicit);
107473441091SDouglas Gregor 
1075718292f2SDouglas Gregor   // Parse the module name.
1076e7ab3669SDouglas Gregor   ModuleId Id;
1077e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
1078718292f2SDouglas Gregor     HadError = true;
1079718292f2SDouglas Gregor     return;
1080718292f2SDouglas Gregor   }
1081e7ab3669SDouglas Gregor 
1082e7ab3669SDouglas Gregor   if (ActiveModule) {
1083e7ab3669SDouglas Gregor     if (Id.size() > 1) {
1084e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1085e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
1086e7ab3669SDouglas Gregor 
1087e7ab3669SDouglas Gregor       HadError = true;
1088e7ab3669SDouglas Gregor       return;
1089e7ab3669SDouglas Gregor     }
1090e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
1091e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
1092e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1093e7ab3669SDouglas Gregor     Explicit = false;
1094e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
1095e7ab3669SDouglas Gregor     HadError = true;
1096e7ab3669SDouglas Gregor   }
1097e7ab3669SDouglas Gregor 
1098e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
1099e7ab3669SDouglas Gregor   if (Id.size() > 1) {
1100e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
1101e7ab3669SDouglas Gregor     // is a submodule.
1102e7ab3669SDouglas Gregor     ActiveModule = 0;
1103e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1104e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1105e7ab3669SDouglas Gregor         ActiveModule = Next;
1106e7ab3669SDouglas Gregor         continue;
1107e7ab3669SDouglas Gregor       }
1108e7ab3669SDouglas Gregor 
1109e7ab3669SDouglas Gregor       if (ActiveModule) {
1110e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1111e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
1112e7ab3669SDouglas Gregor       } else {
1113e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1114e7ab3669SDouglas Gregor       }
1115e7ab3669SDouglas Gregor       HadError = true;
1116e7ab3669SDouglas Gregor       return;
1117e7ab3669SDouglas Gregor     }
1118e7ab3669SDouglas Gregor   }
1119e7ab3669SDouglas Gregor 
1120e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
1121e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
1122718292f2SDouglas Gregor 
1123a686e1b0SDouglas Gregor   // Parse the optional attribute list.
11244442605fSBill Wendling   Attributes Attrs;
11259194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
1126a686e1b0SDouglas Gregor 
1127718292f2SDouglas Gregor   // Parse the opening brace.
1128718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
1129718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1130718292f2SDouglas Gregor       << ModuleName;
1131718292f2SDouglas Gregor     HadError = true;
1132718292f2SDouglas Gregor     return;
1133718292f2SDouglas Gregor   }
1134718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
1135718292f2SDouglas Gregor 
1136718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
1137eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1138fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1139fcc54a3bSDouglas Gregor       // Skip the module definition.
1140fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
1141fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
1142fcc54a3bSDouglas Gregor         consumeToken();
1143fcc54a3bSDouglas Gregor       else {
1144fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1145fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1146fcc54a3bSDouglas Gregor         HadError = true;
1147fcc54a3bSDouglas Gregor       }
1148fcc54a3bSDouglas Gregor       return;
1149fcc54a3bSDouglas Gregor     }
1150fcc54a3bSDouglas Gregor 
1151718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1152718292f2SDouglas Gregor       << ModuleName;
1153eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1154718292f2SDouglas Gregor 
1155718292f2SDouglas Gregor     // Skip the module definition.
1156718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
1157718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
1158718292f2SDouglas Gregor       consumeToken();
1159718292f2SDouglas Gregor 
1160718292f2SDouglas Gregor     HadError = true;
1161718292f2SDouglas Gregor     return;
1162718292f2SDouglas Gregor   }
1163718292f2SDouglas Gregor 
1164718292f2SDouglas Gregor   // Start defining this module.
1165eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1166eb90e830SDouglas Gregor                                         Explicit).first;
1167eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
11689194a91dSDouglas Gregor   if (Attrs.IsSystem)
1169a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
1170718292f2SDouglas Gregor 
1171718292f2SDouglas Gregor   bool Done = false;
1172718292f2SDouglas Gregor   do {
1173718292f2SDouglas Gregor     switch (Tok.Kind) {
1174718292f2SDouglas Gregor     case MMToken::EndOfFile:
1175718292f2SDouglas Gregor     case MMToken::RBrace:
1176718292f2SDouglas Gregor       Done = true;
1177718292f2SDouglas Gregor       break;
1178718292f2SDouglas Gregor 
117935b13eceSDouglas Gregor     case MMToken::ConfigMacros:
118035b13eceSDouglas Gregor       parseConfigMacros();
118135b13eceSDouglas Gregor       break;
118235b13eceSDouglas Gregor 
1183fb912657SDouglas Gregor     case MMToken::Conflict:
1184fb912657SDouglas Gregor       parseConflict();
1185fb912657SDouglas Gregor       break;
1186fb912657SDouglas Gregor 
1187718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
1188f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
1189718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1190718292f2SDouglas Gregor       parseModuleDecl();
1191718292f2SDouglas Gregor       break;
1192718292f2SDouglas Gregor 
11932b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
11942b82c2a5SDouglas Gregor       parseExportDecl();
11952b82c2a5SDouglas Gregor       break;
11962b82c2a5SDouglas Gregor 
11971fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
11981fb5c3a6SDouglas Gregor       parseRequiresDecl();
11991fb5c3a6SDouglas Gregor       break;
12001fb5c3a6SDouglas Gregor 
1201524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
1202524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
1203524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
120459527666SDouglas Gregor         parseHeaderDecl(UmbrellaLoc, SourceLocation());
1205524e33e1SDouglas Gregor       else
1206524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
1207718292f2SDouglas Gregor       break;
1208524e33e1SDouglas Gregor     }
1209718292f2SDouglas Gregor 
121059527666SDouglas Gregor     case MMToken::ExcludeKeyword: {
121159527666SDouglas Gregor       SourceLocation ExcludeLoc = consumeToken();
121259527666SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword)) {
121359527666SDouglas Gregor         parseHeaderDecl(SourceLocation(), ExcludeLoc);
121459527666SDouglas Gregor       } else {
121559527666SDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
121659527666SDouglas Gregor           << "exclude";
121759527666SDouglas Gregor       }
121859527666SDouglas Gregor       break;
121959527666SDouglas Gregor     }
122059527666SDouglas Gregor 
1221322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
122259527666SDouglas Gregor       parseHeaderDecl(SourceLocation(), SourceLocation());
1223718292f2SDouglas Gregor       break;
1224718292f2SDouglas Gregor 
12256ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
12266ddfca91SDouglas Gregor       parseLinkDecl();
12276ddfca91SDouglas Gregor       break;
12286ddfca91SDouglas Gregor 
1229718292f2SDouglas Gregor     default:
1230718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1231718292f2SDouglas Gregor       consumeToken();
1232718292f2SDouglas Gregor       break;
1233718292f2SDouglas Gregor     }
1234718292f2SDouglas Gregor   } while (!Done);
1235718292f2SDouglas Gregor 
1236718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1237718292f2SDouglas Gregor     consumeToken();
1238718292f2SDouglas Gregor   else {
1239718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1240718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1241718292f2SDouglas Gregor     HadError = true;
1242718292f2SDouglas Gregor   }
1243718292f2SDouglas Gregor 
124411dfe6feSDouglas Gregor   // If the active module is a top-level framework, and there are no link
124511dfe6feSDouglas Gregor   // libraries, automatically link against the framework.
124611dfe6feSDouglas Gregor   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
124711dfe6feSDouglas Gregor       ActiveModule->LinkLibraries.empty()) {
124811dfe6feSDouglas Gregor     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
124911dfe6feSDouglas Gregor   }
125011dfe6feSDouglas Gregor 
1251e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1252e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1253718292f2SDouglas Gregor }
1254718292f2SDouglas Gregor 
12551fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
12561fb5c3a6SDouglas Gregor ///
12571fb5c3a6SDouglas Gregor ///   requires-declaration:
12581fb5c3a6SDouglas Gregor ///     'requires' feature-list
12591fb5c3a6SDouglas Gregor ///
12601fb5c3a6SDouglas Gregor ///   feature-list:
12611fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
12621fb5c3a6SDouglas Gregor ///     identifier
12631fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
12641fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
12651fb5c3a6SDouglas Gregor 
12661fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
12671fb5c3a6SDouglas Gregor   consumeToken();
12681fb5c3a6SDouglas Gregor 
12691fb5c3a6SDouglas Gregor   // Parse the feature-list.
12701fb5c3a6SDouglas Gregor   do {
12711fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
12721fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
12731fb5c3a6SDouglas Gregor       HadError = true;
12741fb5c3a6SDouglas Gregor       return;
12751fb5c3a6SDouglas Gregor     }
12761fb5c3a6SDouglas Gregor 
12771fb5c3a6SDouglas Gregor     // Consume the feature name.
12781fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
12791fb5c3a6SDouglas Gregor     consumeToken();
12801fb5c3a6SDouglas Gregor 
12811fb5c3a6SDouglas Gregor     // Add this feature.
128289929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
12831fb5c3a6SDouglas Gregor 
12841fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
12851fb5c3a6SDouglas Gregor       break;
12861fb5c3a6SDouglas Gregor 
12871fb5c3a6SDouglas Gregor     // Consume the comma.
12881fb5c3a6SDouglas Gregor     consumeToken();
12891fb5c3a6SDouglas Gregor   } while (true);
12901fb5c3a6SDouglas Gregor }
12911fb5c3a6SDouglas Gregor 
1292f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1293f2161a70SDouglas Gregor /// subframework in which the given module lives.
1294bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1295f857950dSDmitri Gribenko                                     SmallVectorImpl<char> &Path) {
1296f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1297f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Paths;
1298f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1299f2161a70SDouglas Gregor     if (Mod->IsFramework)
1300f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1301f2161a70SDouglas Gregor   }
1302f2161a70SDouglas Gregor 
1303f2161a70SDouglas Gregor   if (Paths.empty())
1304f2161a70SDouglas Gregor     return;
1305f2161a70SDouglas Gregor 
1306f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
1307f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
1308f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
1309f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1310f2161a70SDouglas Gregor   }
1311f2161a70SDouglas Gregor }
1312f2161a70SDouglas Gregor 
1313718292f2SDouglas Gregor /// \brief Parse a header declaration.
1314718292f2SDouglas Gregor ///
1315718292f2SDouglas Gregor ///   header-declaration:
1316322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
131759527666SDouglas Gregor ///     'exclude'[opt] 'header' string-literal
131859527666SDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc,
131959527666SDouglas Gregor                                       SourceLocation ExcludeLoc) {
1320718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
13211871ed3dSBenjamin Kramer   consumeToken();
1322718292f2SDouglas Gregor 
1323322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
132459527666SDouglas Gregor   bool Exclude = ExcludeLoc.isValid();
132559527666SDouglas Gregor   assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'");
1326718292f2SDouglas Gregor   // Parse the header name.
1327718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1328718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1329718292f2SDouglas Gregor       << "header";
1330718292f2SDouglas Gregor     HadError = true;
1331718292f2SDouglas Gregor     return;
1332718292f2SDouglas Gregor   }
1333e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1334718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1335718292f2SDouglas Gregor 
1336524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1337524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
1338524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1339524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1340322f633cSDouglas Gregor     HadError = true;
1341322f633cSDouglas Gregor     return;
1342322f633cSDouglas Gregor   }
1343322f633cSDouglas Gregor 
13445257fc63SDouglas Gregor   // Look for this file.
1345e7ab3669SDouglas Gregor   const FileEntry *File = 0;
13463ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
13472c1dd271SDylan Noblesmith   SmallString<128> PathName;
1348e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1349e7ab3669SDouglas Gregor     PathName = FileName;
1350e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
13517033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
13527033127bSDouglas Gregor     PathName = Dir->getName();
13537033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
13547033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1355e7ab3669SDouglas Gregor   } else {
1356e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
13577033127bSDouglas Gregor     PathName = Directory->getName();
1358e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1359755b2055SDouglas Gregor 
1360f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1361f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1362755b2055SDouglas Gregor 
1363e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
1364e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
13655257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1366e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1367e7ab3669SDouglas Gregor 
1368e7ab3669SDouglas Gregor       if (!File) {
1369e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1370e7ab3669SDouglas Gregor         PathName.resize(PathLength);
1371e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
1372e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
1373e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1374e7ab3669SDouglas Gregor       }
1375e7ab3669SDouglas Gregor     } else {
1376e7ab3669SDouglas Gregor       // Lookup for normal headers.
1377e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1378e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
13793ec6663bSDouglas Gregor 
13803ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
13813ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
13823ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
13833ec6663bSDouglas Gregor       if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
13843ec6663bSDouglas Gregor           BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
13852c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
13863ec6663bSDouglas Gregor         llvm::sys::path::append(BuiltinPathName, FileName);
13873ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
13883ec6663bSDouglas Gregor 
13893ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
13903ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
13913ec6663bSDouglas Gregor         // up adding both (later).
13923ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
13933ec6663bSDouglas Gregor           File = BuiltinFile;
13943ec6663bSDouglas Gregor           BuiltinFile = 0;
13953ec6663bSDouglas Gregor         }
13963ec6663bSDouglas Gregor       }
1397e7ab3669SDouglas Gregor     }
1398e7ab3669SDouglas Gregor   }
13995257fc63SDouglas Gregor 
14005257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
14015257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1402e7ab3669SDouglas Gregor   if (File) {
140359527666SDouglas Gregor     if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) {
14045257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
140559527666SDouglas Gregor         << FileName << OwningModule.getModule()->getFullModuleName();
14065257fc63SDouglas Gregor       HadError = true;
1407322f633cSDouglas Gregor     } else if (Umbrella) {
1408322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
140959527666SDouglas Gregor       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1410322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
141159527666SDouglas Gregor           << UmbrellaModule->getFullModuleName();
1412322f633cSDouglas Gregor         HadError = true;
14135257fc63SDouglas Gregor       } else {
1414322f633cSDouglas Gregor         // Record this umbrella header.
1415322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1416322f633cSDouglas Gregor       }
1417322f633cSDouglas Gregor     } else {
1418322f633cSDouglas Gregor       // Record this header.
141959527666SDouglas Gregor       Map.addHeader(ActiveModule, File, Exclude);
14203ec6663bSDouglas Gregor 
14213ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
14223ec6663bSDouglas Gregor       if (BuiltinFile)
142359527666SDouglas Gregor         Map.addHeader(ActiveModule, BuiltinFile, Exclude);
14245257fc63SDouglas Gregor     }
14254b27a64bSDouglas Gregor   } else if (!Exclude) {
14264b27a64bSDouglas Gregor     // Ignore excluded header files. They're optional anyway.
14274b27a64bSDouglas Gregor 
14285257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1429524e33e1SDouglas Gregor       << Umbrella << FileName;
14305257fc63SDouglas Gregor     HadError = true;
14315257fc63SDouglas Gregor   }
1432718292f2SDouglas Gregor }
1433718292f2SDouglas Gregor 
1434524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1435524e33e1SDouglas Gregor ///
1436524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1437524e33e1SDouglas Gregor ///     umbrella string-literal
1438524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1439524e33e1SDouglas Gregor   // Parse the directory name.
1440524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1441524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1442524e33e1SDouglas Gregor       << "umbrella";
1443524e33e1SDouglas Gregor     HadError = true;
1444524e33e1SDouglas Gregor     return;
1445524e33e1SDouglas Gregor   }
1446524e33e1SDouglas Gregor 
1447524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1448524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1449524e33e1SDouglas Gregor 
1450524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1451524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1452524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1453524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1454524e33e1SDouglas Gregor     HadError = true;
1455524e33e1SDouglas Gregor     return;
1456524e33e1SDouglas Gregor   }
1457524e33e1SDouglas Gregor 
1458524e33e1SDouglas Gregor   // Look for this file.
1459524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1460524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1461524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1462524e33e1SDouglas Gregor   else {
14632c1dd271SDylan Noblesmith     SmallString<128> PathName;
1464524e33e1SDouglas Gregor     PathName = Directory->getName();
1465524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1466524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1467524e33e1SDouglas Gregor   }
1468524e33e1SDouglas Gregor 
1469524e33e1SDouglas Gregor   if (!Dir) {
1470524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1471524e33e1SDouglas Gregor       << DirName;
1472524e33e1SDouglas Gregor     HadError = true;
1473524e33e1SDouglas Gregor     return;
1474524e33e1SDouglas Gregor   }
1475524e33e1SDouglas Gregor 
1476524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1477524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1478524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1479524e33e1SDouglas Gregor     HadError = true;
1480524e33e1SDouglas Gregor     return;
1481524e33e1SDouglas Gregor   }
1482524e33e1SDouglas Gregor 
1483524e33e1SDouglas Gregor   // Record this umbrella directory.
1484524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1485524e33e1SDouglas Gregor }
1486524e33e1SDouglas Gregor 
14872b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
14882b82c2a5SDouglas Gregor ///
14892b82c2a5SDouglas Gregor ///   export-declaration:
14902b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
14912b82c2a5SDouglas Gregor ///
14922b82c2a5SDouglas Gregor ///   wildcard-module-id:
14932b82c2a5SDouglas Gregor ///     identifier
14942b82c2a5SDouglas Gregor ///     '*'
14952b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
14962b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
14972b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
14982b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
14992b82c2a5SDouglas Gregor 
15002b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
15012b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
15022b82c2a5SDouglas Gregor   bool Wildcard = false;
15032b82c2a5SDouglas Gregor   do {
15042b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
15052b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
15062b82c2a5SDouglas Gregor                                               Tok.getLocation()));
15072b82c2a5SDouglas Gregor       consumeToken();
15082b82c2a5SDouglas Gregor 
15092b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
15102b82c2a5SDouglas Gregor         consumeToken();
15112b82c2a5SDouglas Gregor         continue;
15122b82c2a5SDouglas Gregor       }
15132b82c2a5SDouglas Gregor 
15142b82c2a5SDouglas Gregor       break;
15152b82c2a5SDouglas Gregor     }
15162b82c2a5SDouglas Gregor 
15172b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
15182b82c2a5SDouglas Gregor       Wildcard = true;
1519f5eedd05SDouglas Gregor       consumeToken();
15202b82c2a5SDouglas Gregor       break;
15212b82c2a5SDouglas Gregor     }
15222b82c2a5SDouglas Gregor 
15232b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
15242b82c2a5SDouglas Gregor     HadError = true;
15252b82c2a5SDouglas Gregor     return;
15262b82c2a5SDouglas Gregor   } while (true);
15272b82c2a5SDouglas Gregor 
15282b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
15292b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
15302b82c2a5SDouglas Gregor   };
15312b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
15322b82c2a5SDouglas Gregor }
15332b82c2a5SDouglas Gregor 
15346ddfca91SDouglas Gregor /// \brief Parse a link declaration.
15356ddfca91SDouglas Gregor ///
15366ddfca91SDouglas Gregor ///   module-declaration:
15376ddfca91SDouglas Gregor ///     'link' 'framework'[opt] string-literal
15386ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() {
15396ddfca91SDouglas Gregor   assert(Tok.is(MMToken::LinkKeyword));
15406ddfca91SDouglas Gregor   SourceLocation LinkLoc = consumeToken();
15416ddfca91SDouglas Gregor 
15426ddfca91SDouglas Gregor   // Parse the optional 'framework' keyword.
15436ddfca91SDouglas Gregor   bool IsFramework = false;
15446ddfca91SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
15456ddfca91SDouglas Gregor     consumeToken();
15466ddfca91SDouglas Gregor     IsFramework = true;
15476ddfca91SDouglas Gregor   }
15486ddfca91SDouglas Gregor 
15496ddfca91SDouglas Gregor   // Parse the library name
15506ddfca91SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
15516ddfca91SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
15526ddfca91SDouglas Gregor       << IsFramework << SourceRange(LinkLoc);
15536ddfca91SDouglas Gregor     HadError = true;
15546ddfca91SDouglas Gregor     return;
15556ddfca91SDouglas Gregor   }
15566ddfca91SDouglas Gregor 
15576ddfca91SDouglas Gregor   std::string LibraryName = Tok.getString();
15586ddfca91SDouglas Gregor   consumeToken();
15596ddfca91SDouglas Gregor   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
15606ddfca91SDouglas Gregor                                                             IsFramework));
15616ddfca91SDouglas Gregor }
15626ddfca91SDouglas Gregor 
156335b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration.
156435b13eceSDouglas Gregor ///
156535b13eceSDouglas Gregor ///   module-declaration:
156635b13eceSDouglas Gregor ///     'config_macros' attributes[opt] config-macro-list?
156735b13eceSDouglas Gregor ///
156835b13eceSDouglas Gregor ///   config-macro-list:
156935b13eceSDouglas Gregor ///     identifier (',' identifier)?
157035b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() {
157135b13eceSDouglas Gregor   assert(Tok.is(MMToken::ConfigMacros));
157235b13eceSDouglas Gregor   SourceLocation ConfigMacrosLoc = consumeToken();
157335b13eceSDouglas Gregor 
157435b13eceSDouglas Gregor   // Only top-level modules can have configuration macros.
157535b13eceSDouglas Gregor   if (ActiveModule->Parent) {
157635b13eceSDouglas Gregor     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
157735b13eceSDouglas Gregor   }
157835b13eceSDouglas Gregor 
157935b13eceSDouglas Gregor   // Parse the optional attributes.
158035b13eceSDouglas Gregor   Attributes Attrs;
158135b13eceSDouglas Gregor   parseOptionalAttributes(Attrs);
158235b13eceSDouglas Gregor   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
158335b13eceSDouglas Gregor     ActiveModule->ConfigMacrosExhaustive = true;
158435b13eceSDouglas Gregor   }
158535b13eceSDouglas Gregor 
158635b13eceSDouglas Gregor   // If we don't have an identifier, we're done.
158735b13eceSDouglas Gregor   if (!Tok.is(MMToken::Identifier))
158835b13eceSDouglas Gregor     return;
158935b13eceSDouglas Gregor 
159035b13eceSDouglas Gregor   // Consume the first identifier.
159135b13eceSDouglas Gregor   if (!ActiveModule->Parent) {
159235b13eceSDouglas Gregor     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
159335b13eceSDouglas Gregor   }
159435b13eceSDouglas Gregor   consumeToken();
159535b13eceSDouglas Gregor 
159635b13eceSDouglas Gregor   do {
159735b13eceSDouglas Gregor     // If there's a comma, consume it.
159835b13eceSDouglas Gregor     if (!Tok.is(MMToken::Comma))
159935b13eceSDouglas Gregor       break;
160035b13eceSDouglas Gregor     consumeToken();
160135b13eceSDouglas Gregor 
160235b13eceSDouglas Gregor     // We expect to see a macro name here.
160335b13eceSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
160435b13eceSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
160535b13eceSDouglas Gregor       break;
160635b13eceSDouglas Gregor     }
160735b13eceSDouglas Gregor 
160835b13eceSDouglas Gregor     // Consume the macro name.
160935b13eceSDouglas Gregor     if (!ActiveModule->Parent) {
161035b13eceSDouglas Gregor       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
161135b13eceSDouglas Gregor     }
161235b13eceSDouglas Gregor     consumeToken();
161335b13eceSDouglas Gregor   } while (true);
161435b13eceSDouglas Gregor }
161535b13eceSDouglas Gregor 
1616fb912657SDouglas Gregor /// \brief Format a module-id into a string.
1617fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) {
1618fb912657SDouglas Gregor   std::string result;
1619fb912657SDouglas Gregor   {
1620fb912657SDouglas Gregor     llvm::raw_string_ostream OS(result);
1621fb912657SDouglas Gregor 
1622fb912657SDouglas Gregor     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
1623fb912657SDouglas Gregor       if (I)
1624fb912657SDouglas Gregor         OS << ".";
1625fb912657SDouglas Gregor       OS << Id[I].first;
1626fb912657SDouglas Gregor     }
1627fb912657SDouglas Gregor   }
1628fb912657SDouglas Gregor 
1629fb912657SDouglas Gregor   return result;
1630fb912657SDouglas Gregor }
1631fb912657SDouglas Gregor 
1632fb912657SDouglas Gregor /// \brief Parse a conflict declaration.
1633fb912657SDouglas Gregor ///
1634fb912657SDouglas Gregor ///   module-declaration:
1635fb912657SDouglas Gregor ///     'conflict' module-id ',' string-literal
1636fb912657SDouglas Gregor void ModuleMapParser::parseConflict() {
1637fb912657SDouglas Gregor   assert(Tok.is(MMToken::Conflict));
1638fb912657SDouglas Gregor   SourceLocation ConflictLoc = consumeToken();
1639fb912657SDouglas Gregor   Module::UnresolvedConflict Conflict;
1640fb912657SDouglas Gregor 
1641fb912657SDouglas Gregor   // Parse the module-id.
1642fb912657SDouglas Gregor   if (parseModuleId(Conflict.Id))
1643fb912657SDouglas Gregor     return;
1644fb912657SDouglas Gregor 
1645fb912657SDouglas Gregor   // Parse the ','.
1646fb912657SDouglas Gregor   if (!Tok.is(MMToken::Comma)) {
1647fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
1648fb912657SDouglas Gregor       << SourceRange(ConflictLoc);
1649fb912657SDouglas Gregor     return;
1650fb912657SDouglas Gregor   }
1651fb912657SDouglas Gregor   consumeToken();
1652fb912657SDouglas Gregor 
1653fb912657SDouglas Gregor   // Parse the message.
1654fb912657SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1655fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
1656fb912657SDouglas Gregor       << formatModuleId(Conflict.Id);
1657fb912657SDouglas Gregor     return;
1658fb912657SDouglas Gregor   }
1659fb912657SDouglas Gregor   Conflict.Message = Tok.getString().str();
1660fb912657SDouglas Gregor   consumeToken();
1661fb912657SDouglas Gregor 
1662fb912657SDouglas Gregor   // Add this unresolved conflict.
1663fb912657SDouglas Gregor   ActiveModule->UnresolvedConflicts.push_back(Conflict);
1664fb912657SDouglas Gregor }
1665fb912657SDouglas Gregor 
16666ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules).
16679194a91dSDouglas Gregor ///
16689194a91dSDouglas Gregor ///   module-declaration:
16699194a91dSDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
16709194a91dSDouglas Gregor ///       { inferred-module-member* }
16719194a91dSDouglas Gregor ///
16729194a91dSDouglas Gregor ///   inferred-module-member:
16739194a91dSDouglas Gregor ///     'export' '*'
16749194a91dSDouglas Gregor ///     'exclude' identifier
16759194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
167673441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
167773441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
167873441091SDouglas Gregor   bool Failed = false;
167973441091SDouglas Gregor 
168073441091SDouglas Gregor   // Inferred modules must be submodules.
16819194a91dSDouglas Gregor   if (!ActiveModule && !Framework) {
168273441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
168373441091SDouglas Gregor     Failed = true;
168473441091SDouglas Gregor   }
168573441091SDouglas Gregor 
16869194a91dSDouglas Gregor   if (ActiveModule) {
1687524e33e1SDouglas Gregor     // Inferred modules must have umbrella directories.
1688524e33e1SDouglas Gregor     if (!Failed && !ActiveModule->getUmbrellaDir()) {
168973441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
169073441091SDouglas Gregor       Failed = true;
169173441091SDouglas Gregor     }
169273441091SDouglas Gregor 
169373441091SDouglas Gregor     // Check for redefinition of an inferred module.
1694dd005f69SDouglas Gregor     if (!Failed && ActiveModule->InferSubmodules) {
169573441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1696dd005f69SDouglas Gregor       if (ActiveModule->InferredSubmoduleLoc.isValid())
1697dd005f69SDouglas Gregor         Diags.Report(ActiveModule->InferredSubmoduleLoc,
169873441091SDouglas Gregor                      diag::note_mmap_prev_definition);
169973441091SDouglas Gregor       Failed = true;
170073441091SDouglas Gregor     }
170173441091SDouglas Gregor 
17029194a91dSDouglas Gregor     // Check for the 'framework' keyword, which is not permitted here.
17039194a91dSDouglas Gregor     if (Framework) {
17049194a91dSDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
17059194a91dSDouglas Gregor       Framework = false;
17069194a91dSDouglas Gregor     }
17079194a91dSDouglas Gregor   } else if (Explicit) {
17089194a91dSDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
17099194a91dSDouglas Gregor     Explicit = false;
17109194a91dSDouglas Gregor   }
17119194a91dSDouglas Gregor 
171273441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
171373441091SDouglas Gregor   if (Failed) {
171473441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
171573441091SDouglas Gregor       consumeToken();
171673441091SDouglas Gregor       skipUntil(MMToken::RBrace);
171773441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
171873441091SDouglas Gregor         consumeToken();
171973441091SDouglas Gregor     }
172073441091SDouglas Gregor     HadError = true;
172173441091SDouglas Gregor     return;
172273441091SDouglas Gregor   }
172373441091SDouglas Gregor 
17249194a91dSDouglas Gregor   // Parse optional attributes.
17254442605fSBill Wendling   Attributes Attrs;
17269194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
17279194a91dSDouglas Gregor 
17289194a91dSDouglas Gregor   if (ActiveModule) {
172973441091SDouglas Gregor     // Note that we have an inferred submodule.
1730dd005f69SDouglas Gregor     ActiveModule->InferSubmodules = true;
1731dd005f69SDouglas Gregor     ActiveModule->InferredSubmoduleLoc = StarLoc;
1732dd005f69SDouglas Gregor     ActiveModule->InferExplicitSubmodules = Explicit;
17339194a91dSDouglas Gregor   } else {
17349194a91dSDouglas Gregor     // We'll be inferring framework modules for this directory.
17359194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferModules = true;
17369194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
17379194a91dSDouglas Gregor   }
173873441091SDouglas Gregor 
173973441091SDouglas Gregor   // Parse the opening brace.
174073441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
174173441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
174273441091SDouglas Gregor     HadError = true;
174373441091SDouglas Gregor     return;
174473441091SDouglas Gregor   }
174573441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
174673441091SDouglas Gregor 
174773441091SDouglas Gregor   // Parse the body of the inferred submodule.
174873441091SDouglas Gregor   bool Done = false;
174973441091SDouglas Gregor   do {
175073441091SDouglas Gregor     switch (Tok.Kind) {
175173441091SDouglas Gregor     case MMToken::EndOfFile:
175273441091SDouglas Gregor     case MMToken::RBrace:
175373441091SDouglas Gregor       Done = true;
175473441091SDouglas Gregor       break;
175573441091SDouglas Gregor 
17569194a91dSDouglas Gregor     case MMToken::ExcludeKeyword: {
17579194a91dSDouglas Gregor       if (ActiveModule) {
17589194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1759162405daSDouglas Gregor           << (ActiveModule != 0);
17609194a91dSDouglas Gregor         consumeToken();
17619194a91dSDouglas Gregor         break;
17629194a91dSDouglas Gregor       }
17639194a91dSDouglas Gregor 
17649194a91dSDouglas Gregor       consumeToken();
17659194a91dSDouglas Gregor       if (!Tok.is(MMToken::Identifier)) {
17669194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
17679194a91dSDouglas Gregor         break;
17689194a91dSDouglas Gregor       }
17699194a91dSDouglas Gregor 
17709194a91dSDouglas Gregor       Map.InferredDirectories[Directory].ExcludedModules
17719194a91dSDouglas Gregor         .push_back(Tok.getString());
17729194a91dSDouglas Gregor       consumeToken();
17739194a91dSDouglas Gregor       break;
17749194a91dSDouglas Gregor     }
17759194a91dSDouglas Gregor 
17769194a91dSDouglas Gregor     case MMToken::ExportKeyword:
17779194a91dSDouglas Gregor       if (!ActiveModule) {
17789194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1779162405daSDouglas Gregor           << (ActiveModule != 0);
17809194a91dSDouglas Gregor         consumeToken();
17819194a91dSDouglas Gregor         break;
17829194a91dSDouglas Gregor       }
17839194a91dSDouglas Gregor 
178473441091SDouglas Gregor       consumeToken();
178573441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1786dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
178773441091SDouglas Gregor       else
178873441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
178973441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
179073441091SDouglas Gregor       consumeToken();
179173441091SDouglas Gregor       break;
179273441091SDouglas Gregor 
179373441091SDouglas Gregor     case MMToken::ExplicitKeyword:
179473441091SDouglas Gregor     case MMToken::ModuleKeyword:
179573441091SDouglas Gregor     case MMToken::HeaderKeyword:
179673441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
179773441091SDouglas Gregor     default:
17989194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1799162405daSDouglas Gregor           << (ActiveModule != 0);
180073441091SDouglas Gregor       consumeToken();
180173441091SDouglas Gregor       break;
180273441091SDouglas Gregor     }
180373441091SDouglas Gregor   } while (!Done);
180473441091SDouglas Gregor 
180573441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
180673441091SDouglas Gregor     consumeToken();
180773441091SDouglas Gregor   else {
180873441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
180973441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
181073441091SDouglas Gregor     HadError = true;
181173441091SDouglas Gregor   }
181273441091SDouglas Gregor }
181373441091SDouglas Gregor 
18149194a91dSDouglas Gregor /// \brief Parse optional attributes.
18159194a91dSDouglas Gregor ///
18169194a91dSDouglas Gregor ///   attributes:
18179194a91dSDouglas Gregor ///     attribute attributes
18189194a91dSDouglas Gregor ///     attribute
18199194a91dSDouglas Gregor ///
18209194a91dSDouglas Gregor ///   attribute:
18219194a91dSDouglas Gregor ///     [ identifier ]
18229194a91dSDouglas Gregor ///
18239194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes.
18249194a91dSDouglas Gregor ///
18259194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise.
18264442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
18279194a91dSDouglas Gregor   bool HadError = false;
18289194a91dSDouglas Gregor 
18299194a91dSDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
18309194a91dSDouglas Gregor     // Consume the '['.
18319194a91dSDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
18329194a91dSDouglas Gregor 
18339194a91dSDouglas Gregor     // Check whether we have an attribute name here.
18349194a91dSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
18359194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
18369194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
18379194a91dSDouglas Gregor       if (Tok.is(MMToken::RSquare))
18389194a91dSDouglas Gregor         consumeToken();
18399194a91dSDouglas Gregor       HadError = true;
18409194a91dSDouglas Gregor     }
18419194a91dSDouglas Gregor 
18429194a91dSDouglas Gregor     // Decode the attribute name.
18439194a91dSDouglas Gregor     AttributeKind Attribute
18449194a91dSDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
184535b13eceSDouglas Gregor           .Case("exhaustive", AT_exhaustive)
18469194a91dSDouglas Gregor           .Case("system", AT_system)
18479194a91dSDouglas Gregor           .Default(AT_unknown);
18489194a91dSDouglas Gregor     switch (Attribute) {
18499194a91dSDouglas Gregor     case AT_unknown:
18509194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
18519194a91dSDouglas Gregor         << Tok.getString();
18529194a91dSDouglas Gregor       break;
18539194a91dSDouglas Gregor 
18549194a91dSDouglas Gregor     case AT_system:
18559194a91dSDouglas Gregor       Attrs.IsSystem = true;
18569194a91dSDouglas Gregor       break;
185735b13eceSDouglas Gregor 
185835b13eceSDouglas Gregor     case AT_exhaustive:
185935b13eceSDouglas Gregor       Attrs.IsExhaustive = true;
186035b13eceSDouglas Gregor       break;
18619194a91dSDouglas Gregor     }
18629194a91dSDouglas Gregor     consumeToken();
18639194a91dSDouglas Gregor 
18649194a91dSDouglas Gregor     // Consume the ']'.
18659194a91dSDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
18669194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
18679194a91dSDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
18689194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
18699194a91dSDouglas Gregor       HadError = true;
18709194a91dSDouglas Gregor     }
18719194a91dSDouglas Gregor 
18729194a91dSDouglas Gregor     if (Tok.is(MMToken::RSquare))
18739194a91dSDouglas Gregor       consumeToken();
18749194a91dSDouglas Gregor   }
18759194a91dSDouglas Gregor 
18769194a91dSDouglas Gregor   return HadError;
18779194a91dSDouglas Gregor }
18789194a91dSDouglas Gregor 
18797033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
18807033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
18817033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
18827033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
18837033127bSDouglas Gregor     // If we have an umbrella directory, use that.
18847033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
18857033127bSDouglas Gregor       return Mod->getUmbrellaDir();
18867033127bSDouglas Gregor 
18877033127bSDouglas Gregor     // If we have a framework directory, stop looking.
18887033127bSDouglas Gregor     if (Mod->IsFramework)
18897033127bSDouglas Gregor       return 0;
18907033127bSDouglas Gregor   }
18917033127bSDouglas Gregor 
18927033127bSDouglas Gregor   return 0;
18937033127bSDouglas Gregor }
18947033127bSDouglas Gregor 
1895718292f2SDouglas Gregor /// \brief Parse a module map file.
1896718292f2SDouglas Gregor ///
1897718292f2SDouglas Gregor ///   module-map-file:
1898718292f2SDouglas Gregor ///     module-declaration*
1899718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1900718292f2SDouglas Gregor   do {
1901718292f2SDouglas Gregor     switch (Tok.Kind) {
1902718292f2SDouglas Gregor     case MMToken::EndOfFile:
1903718292f2SDouglas Gregor       return HadError;
1904718292f2SDouglas Gregor 
1905e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1906718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1907755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1908718292f2SDouglas Gregor       parseModuleDecl();
1909718292f2SDouglas Gregor       break;
1910718292f2SDouglas Gregor 
19111fb5c3a6SDouglas Gregor     case MMToken::Comma:
191235b13eceSDouglas Gregor     case MMToken::ConfigMacros:
1913fb912657SDouglas Gregor     case MMToken::Conflict:
191459527666SDouglas Gregor     case MMToken::ExcludeKeyword:
19152b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1916718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1917718292f2SDouglas Gregor     case MMToken::Identifier:
1918718292f2SDouglas Gregor     case MMToken::LBrace:
19196ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
1920a686e1b0SDouglas Gregor     case MMToken::LSquare:
19212b82c2a5SDouglas Gregor     case MMToken::Period:
1922718292f2SDouglas Gregor     case MMToken::RBrace:
1923a686e1b0SDouglas Gregor     case MMToken::RSquare:
19241fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
19252b82c2a5SDouglas Gregor     case MMToken::Star:
1926718292f2SDouglas Gregor     case MMToken::StringLiteral:
1927718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1928718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1929718292f2SDouglas Gregor       HadError = true;
1930718292f2SDouglas Gregor       consumeToken();
1931718292f2SDouglas Gregor       break;
1932718292f2SDouglas Gregor     }
1933718292f2SDouglas Gregor   } while (true);
1934718292f2SDouglas Gregor }
1935718292f2SDouglas Gregor 
1936718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
19374ddf2221SDouglas Gregor   llvm::DenseMap<const FileEntry *, bool>::iterator Known
19384ddf2221SDouglas Gregor     = ParsedModuleMap.find(File);
19394ddf2221SDouglas Gregor   if (Known != ParsedModuleMap.end())
19404ddf2221SDouglas Gregor     return Known->second;
19414ddf2221SDouglas Gregor 
194289929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
1943718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1944718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1945718292f2SDouglas Gregor   if (!Buffer)
19464ddf2221SDouglas Gregor     return ParsedModuleMap[File] = true;
1947718292f2SDouglas Gregor 
1948718292f2SDouglas Gregor   // Parse this module map file.
19491fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
19501fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
1951bc10b9fbSDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(),
19523ec6663bSDouglas Gregor                          BuiltinIncludeDir);
1953718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1954718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
19554ddf2221SDouglas Gregor   ParsedModuleMap[File] = Result;
1956718292f2SDouglas Gregor   return Result;
1957718292f2SDouglas Gregor }
1958