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),
906f722b4eSArgyrios 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()))) {
186*64a1fa5cSDouglas Gregor     HeaderInfo.loadTopLevelSystemModules();
18734d52749SDouglas Gregor 
18834d52749SDouglas Gregor     // Check again.
18934d52749SDouglas Gregor     Known = Headers.find(File);
19034d52749SDouglas Gregor     if (Known != Headers.end()) {
19134d52749SDouglas Gregor       // If a header is not available, don't report that it maps to anything.
19234d52749SDouglas Gregor       if (!Known->second.isAvailable())
19334d52749SDouglas Gregor         return 0;
19434d52749SDouglas Gregor 
19534d52749SDouglas Gregor       return Known->second.getModule();
19634d52749SDouglas Gregor     }
19734d52749SDouglas Gregor   }
19834d52749SDouglas Gregor 
199b65dbfffSDouglas Gregor   const DirectoryEntry *Dir = File->getDir();
200f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
201e00c8b20SDouglas Gregor 
20274260502SDouglas Gregor   // Note: as an egregious but useful hack we use the real path here, because
20374260502SDouglas Gregor   // frameworks moving from top-level frameworks to embedded frameworks tend
20474260502SDouglas Gregor   // to be symlinked from the top-level location to the embedded location,
20574260502SDouglas Gregor   // and we need to resolve lookups as if we had found the embedded location.
206e00c8b20SDouglas Gregor   StringRef DirName = SourceMgr->getFileManager().getCanonicalName(Dir);
207a89c5ac4SDouglas Gregor 
208a89c5ac4SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
209a89c5ac4SDouglas Gregor   // an umbrella header.
210b65dbfffSDouglas Gregor   do {
211a89c5ac4SDouglas Gregor     llvm::DenseMap<const DirectoryEntry *, Module *>::iterator KnownDir
212a89c5ac4SDouglas Gregor       = UmbrellaDirs.find(Dir);
213a89c5ac4SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
214a89c5ac4SDouglas Gregor       Module *Result = KnownDir->second;
215930a85ccSDouglas Gregor 
216930a85ccSDouglas Gregor       // Search up the module stack until we find a module with an umbrella
21773141fa9SDouglas Gregor       // directory.
218930a85ccSDouglas Gregor       Module *UmbrellaModule = Result;
21973141fa9SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
220930a85ccSDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
221930a85ccSDouglas Gregor 
222930a85ccSDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
223a89c5ac4SDouglas Gregor         // Infer submodules for each of the directories we found between
224a89c5ac4SDouglas Gregor         // the directory of the umbrella header and the directory where
225a89c5ac4SDouglas Gregor         // the actual header is located.
2269458f82dSDouglas Gregor         bool Explicit = UmbrellaModule->InferExplicitSubmodules;
2279458f82dSDouglas Gregor 
2287033127bSDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
229a89c5ac4SDouglas Gregor           // Find or create the module that corresponds to this directory name.
230056396aeSDouglas Gregor           SmallString<32> NameBuf;
231056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
232056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
233056396aeSDouglas Gregor                              NameBuf);
234a89c5ac4SDouglas Gregor           Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2359458f82dSDouglas Gregor                                       Explicit).first;
236a89c5ac4SDouglas Gregor 
237a89c5ac4SDouglas Gregor           // Associate the module and the directory.
238a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I-1]] = Result;
239a89c5ac4SDouglas Gregor 
240a89c5ac4SDouglas Gregor           // If inferred submodules export everything they import, add a
241a89c5ac4SDouglas Gregor           // wildcard to the set of exports.
242930a85ccSDouglas Gregor           if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
243a89c5ac4SDouglas Gregor             Result->Exports.push_back(Module::ExportDecl(0, true));
244a89c5ac4SDouglas Gregor         }
245a89c5ac4SDouglas Gregor 
246a89c5ac4SDouglas Gregor         // Infer a submodule with the same name as this header file.
247056396aeSDouglas Gregor         SmallString<32> NameBuf;
248056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
249056396aeSDouglas Gregor                            llvm::sys::path::stem(File->getName()), NameBuf);
250a89c5ac4SDouglas Gregor         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
2519458f82dSDouglas Gregor                                     Explicit).first;
2523c5305c1SArgyrios Kyrtzidis         Result->addTopHeader(File);
253a89c5ac4SDouglas Gregor 
254a89c5ac4SDouglas Gregor         // If inferred submodules export everything they import, add a
255a89c5ac4SDouglas Gregor         // wildcard to the set of exports.
256930a85ccSDouglas Gregor         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
257a89c5ac4SDouglas Gregor           Result->Exports.push_back(Module::ExportDecl(0, true));
258a89c5ac4SDouglas Gregor       } else {
259a89c5ac4SDouglas Gregor         // Record each of the directories we stepped through as being part of
260a89c5ac4SDouglas Gregor         // the module we found, since the umbrella header covers them all.
261a89c5ac4SDouglas Gregor         for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
262a89c5ac4SDouglas Gregor           UmbrellaDirs[SkippedDirs[I]] = Result;
263a89c5ac4SDouglas Gregor       }
264a89c5ac4SDouglas Gregor 
26559527666SDouglas Gregor       Headers[File] = KnownHeader(Result, /*Excluded=*/false);
2661fb5c3a6SDouglas Gregor 
2671fb5c3a6SDouglas Gregor       // If a header corresponds to an unavailable module, don't report
2681fb5c3a6SDouglas Gregor       // that it maps to anything.
2691fb5c3a6SDouglas Gregor       if (!Result->isAvailable())
2701fb5c3a6SDouglas Gregor         return 0;
2711fb5c3a6SDouglas Gregor 
272a89c5ac4SDouglas Gregor       return Result;
273a89c5ac4SDouglas Gregor     }
274a89c5ac4SDouglas Gregor 
275a89c5ac4SDouglas Gregor     SkippedDirs.push_back(Dir);
276a89c5ac4SDouglas Gregor 
277b65dbfffSDouglas Gregor     // Retrieve our parent path.
278b65dbfffSDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
279b65dbfffSDouglas Gregor     if (DirName.empty())
280b65dbfffSDouglas Gregor       break;
281b65dbfffSDouglas Gregor 
282b65dbfffSDouglas Gregor     // Resolve the parent path to a directory entry.
283b65dbfffSDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
284a89c5ac4SDouglas Gregor   } while (Dir);
285b65dbfffSDouglas Gregor 
286ab0c8a84SDouglas Gregor   return 0;
287ab0c8a84SDouglas Gregor }
288ab0c8a84SDouglas Gregor 
289e4412640SArgyrios Kyrtzidis bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
290e4412640SArgyrios Kyrtzidis   HeadersMap::const_iterator Known = Headers.find(Header);
2911fb5c3a6SDouglas Gregor   if (Known != Headers.end())
29259527666SDouglas Gregor     return !Known->second.isAvailable();
2931fb5c3a6SDouglas Gregor 
2941fb5c3a6SDouglas Gregor   const DirectoryEntry *Dir = Header->getDir();
295f857950dSDmitri Gribenko   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
2961fb5c3a6SDouglas Gregor   StringRef DirName = Dir->getName();
2971fb5c3a6SDouglas Gregor 
2981fb5c3a6SDouglas Gregor   // Keep walking up the directory hierarchy, looking for a directory with
2991fb5c3a6SDouglas Gregor   // an umbrella header.
3001fb5c3a6SDouglas Gregor   do {
301e4412640SArgyrios Kyrtzidis     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
3021fb5c3a6SDouglas Gregor       = UmbrellaDirs.find(Dir);
3031fb5c3a6SDouglas Gregor     if (KnownDir != UmbrellaDirs.end()) {
3041fb5c3a6SDouglas Gregor       Module *Found = KnownDir->second;
3051fb5c3a6SDouglas Gregor       if (!Found->isAvailable())
3061fb5c3a6SDouglas Gregor         return true;
3071fb5c3a6SDouglas Gregor 
3081fb5c3a6SDouglas Gregor       // Search up the module stack until we find a module with an umbrella
3091fb5c3a6SDouglas Gregor       // directory.
3101fb5c3a6SDouglas Gregor       Module *UmbrellaModule = Found;
3111fb5c3a6SDouglas Gregor       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
3121fb5c3a6SDouglas Gregor         UmbrellaModule = UmbrellaModule->Parent;
3131fb5c3a6SDouglas Gregor 
3141fb5c3a6SDouglas Gregor       if (UmbrellaModule->InferSubmodules) {
3151fb5c3a6SDouglas Gregor         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
3161fb5c3a6SDouglas Gregor           // Find or create the module that corresponds to this directory name.
317056396aeSDouglas Gregor           SmallString<32> NameBuf;
318056396aeSDouglas Gregor           StringRef Name = sanitizeFilenameAsIdentifier(
319056396aeSDouglas Gregor                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
320056396aeSDouglas Gregor                              NameBuf);
3211fb5c3a6SDouglas Gregor           Found = lookupModuleQualified(Name, Found);
3221fb5c3a6SDouglas Gregor           if (!Found)
3231fb5c3a6SDouglas Gregor             return false;
3241fb5c3a6SDouglas Gregor           if (!Found->isAvailable())
3251fb5c3a6SDouglas Gregor             return true;
3261fb5c3a6SDouglas Gregor         }
3271fb5c3a6SDouglas Gregor 
3281fb5c3a6SDouglas Gregor         // Infer a submodule with the same name as this header file.
329056396aeSDouglas Gregor         SmallString<32> NameBuf;
330056396aeSDouglas Gregor         StringRef Name = sanitizeFilenameAsIdentifier(
331056396aeSDouglas Gregor                            llvm::sys::path::stem(Header->getName()),
332056396aeSDouglas Gregor                            NameBuf);
3331fb5c3a6SDouglas Gregor         Found = lookupModuleQualified(Name, Found);
3341fb5c3a6SDouglas Gregor         if (!Found)
3351fb5c3a6SDouglas Gregor           return false;
3361fb5c3a6SDouglas Gregor       }
3371fb5c3a6SDouglas Gregor 
3381fb5c3a6SDouglas Gregor       return !Found->isAvailable();
3391fb5c3a6SDouglas Gregor     }
3401fb5c3a6SDouglas Gregor 
3411fb5c3a6SDouglas Gregor     SkippedDirs.push_back(Dir);
3421fb5c3a6SDouglas Gregor 
3431fb5c3a6SDouglas Gregor     // Retrieve our parent path.
3441fb5c3a6SDouglas Gregor     DirName = llvm::sys::path::parent_path(DirName);
3451fb5c3a6SDouglas Gregor     if (DirName.empty())
3461fb5c3a6SDouglas Gregor       break;
3471fb5c3a6SDouglas Gregor 
3481fb5c3a6SDouglas Gregor     // Resolve the parent path to a directory entry.
3491fb5c3a6SDouglas Gregor     Dir = SourceMgr->getFileManager().getDirectory(DirName);
3501fb5c3a6SDouglas Gregor   } while (Dir);
3511fb5c3a6SDouglas Gregor 
3521fb5c3a6SDouglas Gregor   return false;
3531fb5c3a6SDouglas Gregor }
3541fb5c3a6SDouglas Gregor 
355e4412640SArgyrios Kyrtzidis Module *ModuleMap::findModule(StringRef Name) const {
356e4412640SArgyrios Kyrtzidis   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
35788bdfb0eSDouglas Gregor   if (Known != Modules.end())
35888bdfb0eSDouglas Gregor     return Known->getValue();
35988bdfb0eSDouglas Gregor 
36088bdfb0eSDouglas Gregor   return 0;
36188bdfb0eSDouglas Gregor }
36288bdfb0eSDouglas Gregor 
363e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
364e4412640SArgyrios Kyrtzidis                                            Module *Context) const {
3652b82c2a5SDouglas Gregor   for(; Context; Context = Context->Parent) {
3662b82c2a5SDouglas Gregor     if (Module *Sub = lookupModuleQualified(Name, Context))
3672b82c2a5SDouglas Gregor       return Sub;
3682b82c2a5SDouglas Gregor   }
3692b82c2a5SDouglas Gregor 
3702b82c2a5SDouglas Gregor   return findModule(Name);
3712b82c2a5SDouglas Gregor }
3722b82c2a5SDouglas Gregor 
373e4412640SArgyrios Kyrtzidis Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
3742b82c2a5SDouglas Gregor   if (!Context)
3752b82c2a5SDouglas Gregor     return findModule(Name);
3762b82c2a5SDouglas Gregor 
377eb90e830SDouglas Gregor   return Context->findSubmodule(Name);
3782b82c2a5SDouglas Gregor }
3792b82c2a5SDouglas Gregor 
380de3ef502SDouglas Gregor std::pair<Module *, bool>
38169021974SDouglas Gregor ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
38269021974SDouglas Gregor                               bool IsExplicit) {
38369021974SDouglas Gregor   // Try to find an existing module with this name.
384eb90e830SDouglas Gregor   if (Module *Sub = lookupModuleQualified(Name, Parent))
385eb90e830SDouglas Gregor     return std::make_pair(Sub, false);
38669021974SDouglas Gregor 
38769021974SDouglas Gregor   // Create a new module with this name.
38869021974SDouglas Gregor   Module *Result = new Module(Name, SourceLocation(), Parent, IsFramework,
38969021974SDouglas Gregor                               IsExplicit);
3906f722b4eSArgyrios Kyrtzidis   if (!Parent) {
39169021974SDouglas Gregor     Modules[Name] = Result;
3926f722b4eSArgyrios Kyrtzidis     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
3936f722b4eSArgyrios Kyrtzidis         Name == LangOpts.CurrentModule) {
3946f722b4eSArgyrios Kyrtzidis       CompilingModule = Result;
3956f722b4eSArgyrios Kyrtzidis     }
3966f722b4eSArgyrios Kyrtzidis   }
39769021974SDouglas Gregor   return std::make_pair(Result, true);
39869021974SDouglas Gregor }
39969021974SDouglas Gregor 
4009194a91dSDouglas Gregor bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
401e4412640SArgyrios Kyrtzidis                                         StringRef Name, bool &IsSystem) const {
4029194a91dSDouglas Gregor   // Check whether we have already looked into the parent directory
4039194a91dSDouglas Gregor   // for a module map.
404e4412640SArgyrios Kyrtzidis   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4059194a91dSDouglas Gregor     inferred = InferredDirectories.find(ParentDir);
4069194a91dSDouglas Gregor   if (inferred == InferredDirectories.end())
4079194a91dSDouglas Gregor     return false;
4089194a91dSDouglas Gregor 
4099194a91dSDouglas Gregor   if (!inferred->second.InferModules)
4109194a91dSDouglas Gregor     return false;
4119194a91dSDouglas Gregor 
4129194a91dSDouglas Gregor   // We're allowed to infer for this directory, but make sure it's okay
4139194a91dSDouglas Gregor   // to infer this particular module.
4149194a91dSDouglas Gregor   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
4159194a91dSDouglas Gregor                             inferred->second.ExcludedModules.end(),
4169194a91dSDouglas Gregor                             Name) == inferred->second.ExcludedModules.end();
4179194a91dSDouglas Gregor 
4189194a91dSDouglas Gregor   if (canInfer && inferred->second.InferSystemModules)
4199194a91dSDouglas Gregor     IsSystem = true;
4209194a91dSDouglas Gregor 
4219194a91dSDouglas Gregor   return canInfer;
4229194a91dSDouglas Gregor }
4239194a91dSDouglas Gregor 
42411dfe6feSDouglas Gregor /// \brief For a framework module, infer the framework against which we
42511dfe6feSDouglas Gregor /// should link.
42611dfe6feSDouglas Gregor static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
42711dfe6feSDouglas Gregor                                FileManager &FileMgr) {
42811dfe6feSDouglas Gregor   assert(Mod->IsFramework && "Can only infer linking for framework modules");
42911dfe6feSDouglas Gregor   assert(!Mod->isSubFramework() &&
43011dfe6feSDouglas Gregor          "Can only infer linking for top-level frameworks");
43111dfe6feSDouglas Gregor 
43211dfe6feSDouglas Gregor   SmallString<128> LibName;
43311dfe6feSDouglas Gregor   LibName += FrameworkDir->getName();
43411dfe6feSDouglas Gregor   llvm::sys::path::append(LibName, Mod->Name);
43511dfe6feSDouglas Gregor   if (FileMgr.getFile(LibName)) {
43611dfe6feSDouglas Gregor     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
43711dfe6feSDouglas Gregor                                                      /*IsFramework=*/true));
43811dfe6feSDouglas Gregor   }
43911dfe6feSDouglas Gregor }
44011dfe6feSDouglas Gregor 
441de3ef502SDouglas Gregor Module *
44256c64013SDouglas Gregor ModuleMap::inferFrameworkModule(StringRef ModuleName,
443e89dbc1dSDouglas Gregor                                 const DirectoryEntry *FrameworkDir,
444a686e1b0SDouglas Gregor                                 bool IsSystem,
445e89dbc1dSDouglas Gregor                                 Module *Parent) {
44656c64013SDouglas Gregor   // Check whether we've already found this module.
447e89dbc1dSDouglas Gregor   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
448e89dbc1dSDouglas Gregor     return Mod;
449e89dbc1dSDouglas Gregor 
450e89dbc1dSDouglas Gregor   FileManager &FileMgr = SourceMgr->getFileManager();
45156c64013SDouglas Gregor 
4529194a91dSDouglas Gregor   // If the framework has a parent path from which we're allowed to infer
4539194a91dSDouglas Gregor   // a framework module, do so.
4549194a91dSDouglas Gregor   if (!Parent) {
4554ddf2221SDouglas Gregor     // Determine whether we're allowed to infer a module map.
456e00c8b20SDouglas Gregor 
4574ddf2221SDouglas Gregor     // Note: as an egregious but useful hack we use the real path here, because
4584ddf2221SDouglas Gregor     // we might be looking at an embedded framework that symlinks out to a
4594ddf2221SDouglas Gregor     // top-level framework, and we need to infer as if we were naming the
4604ddf2221SDouglas Gregor     // top-level framework.
461e00c8b20SDouglas Gregor     StringRef FrameworkDirName
462e00c8b20SDouglas Gregor       = SourceMgr->getFileManager().getCanonicalName(FrameworkDir);
4634ddf2221SDouglas Gregor 
4649194a91dSDouglas Gregor     bool canInfer = false;
4654ddf2221SDouglas Gregor     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
4669194a91dSDouglas Gregor       // Figure out the parent path.
4674ddf2221SDouglas Gregor       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
4689194a91dSDouglas Gregor       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
4699194a91dSDouglas Gregor         // Check whether we have already looked into the parent directory
4709194a91dSDouglas Gregor         // for a module map.
471e4412640SArgyrios Kyrtzidis         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
4729194a91dSDouglas Gregor           inferred = InferredDirectories.find(ParentDir);
4739194a91dSDouglas Gregor         if (inferred == InferredDirectories.end()) {
4749194a91dSDouglas Gregor           // We haven't looked here before. Load a module map, if there is
4759194a91dSDouglas Gregor           // one.
4769194a91dSDouglas Gregor           SmallString<128> ModMapPath = Parent;
4779194a91dSDouglas Gregor           llvm::sys::path::append(ModMapPath, "module.map");
4789194a91dSDouglas Gregor           if (const FileEntry *ModMapFile = FileMgr.getFile(ModMapPath)) {
4799194a91dSDouglas Gregor             parseModuleMapFile(ModMapFile);
4809194a91dSDouglas Gregor             inferred = InferredDirectories.find(ParentDir);
4819194a91dSDouglas Gregor           }
4829194a91dSDouglas Gregor 
4839194a91dSDouglas Gregor           if (inferred == InferredDirectories.end())
4849194a91dSDouglas Gregor             inferred = InferredDirectories.insert(
4859194a91dSDouglas Gregor                          std::make_pair(ParentDir, InferredDirectory())).first;
4869194a91dSDouglas Gregor         }
4879194a91dSDouglas Gregor 
4889194a91dSDouglas Gregor         if (inferred->second.InferModules) {
4899194a91dSDouglas Gregor           // We're allowed to infer for this directory, but make sure it's okay
4909194a91dSDouglas Gregor           // to infer this particular module.
4914ddf2221SDouglas Gregor           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
4929194a91dSDouglas Gregor           canInfer = std::find(inferred->second.ExcludedModules.begin(),
4939194a91dSDouglas Gregor                                inferred->second.ExcludedModules.end(),
4949194a91dSDouglas Gregor                                Name) == inferred->second.ExcludedModules.end();
4959194a91dSDouglas Gregor 
4969194a91dSDouglas Gregor           if (inferred->second.InferSystemModules)
4979194a91dSDouglas Gregor             IsSystem = true;
4989194a91dSDouglas Gregor         }
4999194a91dSDouglas Gregor       }
5009194a91dSDouglas Gregor     }
5019194a91dSDouglas Gregor 
5029194a91dSDouglas Gregor     // If we're not allowed to infer a framework module, don't.
5039194a91dSDouglas Gregor     if (!canInfer)
5049194a91dSDouglas Gregor       return 0;
5059194a91dSDouglas Gregor   }
5069194a91dSDouglas Gregor 
5079194a91dSDouglas Gregor 
50856c64013SDouglas Gregor   // Look for an umbrella header.
5092c1dd271SDylan Noblesmith   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
51056c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, "Headers");
51156c64013SDouglas Gregor   llvm::sys::path::append(UmbrellaName, ModuleName + ".h");
512e89dbc1dSDouglas Gregor   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
51356c64013SDouglas Gregor 
51456c64013SDouglas Gregor   // FIXME: If there's no umbrella header, we could probably scan the
51556c64013SDouglas Gregor   // framework to load *everything*. But, it's not clear that this is a good
51656c64013SDouglas Gregor   // idea.
51756c64013SDouglas Gregor   if (!UmbrellaHeader)
51856c64013SDouglas Gregor     return 0;
51956c64013SDouglas Gregor 
520e89dbc1dSDouglas Gregor   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
521e89dbc1dSDouglas Gregor                               /*IsFramework=*/true, /*IsExplicit=*/false);
522a686e1b0SDouglas Gregor   if (IsSystem)
523a686e1b0SDouglas Gregor     Result->IsSystem = IsSystem;
524a686e1b0SDouglas Gregor 
525eb90e830SDouglas Gregor   if (!Parent)
526e89dbc1dSDouglas Gregor     Modules[ModuleName] = Result;
527e89dbc1dSDouglas Gregor 
528322f633cSDouglas Gregor   // umbrella header "umbrella-header-name"
52973141fa9SDouglas Gregor   Result->Umbrella = UmbrellaHeader;
53059527666SDouglas Gregor   Headers[UmbrellaHeader] = KnownHeader(Result, /*Excluded=*/false);
5314dc71835SDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
532d8bd7537SDouglas Gregor 
533d8bd7537SDouglas Gregor   // export *
534d8bd7537SDouglas Gregor   Result->Exports.push_back(Module::ExportDecl(0, true));
535d8bd7537SDouglas Gregor 
536a89c5ac4SDouglas Gregor   // module * { export * }
537a89c5ac4SDouglas Gregor   Result->InferSubmodules = true;
538a89c5ac4SDouglas Gregor   Result->InferExportWildcard = true;
539a89c5ac4SDouglas Gregor 
540e89dbc1dSDouglas Gregor   // Look for subframeworks.
541e89dbc1dSDouglas Gregor   llvm::error_code EC;
5422c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirName
543ddaa69cbSDouglas Gregor     = StringRef(FrameworkDir->getName());
544e89dbc1dSDouglas Gregor   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
5452c1dd271SDylan Noblesmith   SmallString<128> SubframeworksDirNameNative;
546ddaa69cbSDouglas Gregor   llvm::sys::path::native(SubframeworksDirName.str(),
547ddaa69cbSDouglas Gregor                           SubframeworksDirNameNative);
548ddaa69cbSDouglas Gregor   for (llvm::sys::fs::directory_iterator
549ddaa69cbSDouglas Gregor          Dir(SubframeworksDirNameNative.str(), EC), DirEnd;
550e89dbc1dSDouglas Gregor        Dir != DirEnd && !EC; Dir.increment(EC)) {
551e89dbc1dSDouglas Gregor     if (!StringRef(Dir->path()).endswith(".framework"))
552e89dbc1dSDouglas Gregor       continue;
553f2161a70SDouglas Gregor 
554e89dbc1dSDouglas Gregor     if (const DirectoryEntry *SubframeworkDir
555e89dbc1dSDouglas Gregor           = FileMgr.getDirectory(Dir->path())) {
55607c22b78SDouglas Gregor       // Note: as an egregious but useful hack, we use the real path here and
55707c22b78SDouglas Gregor       // check whether it is actually a subdirectory of the parent directory.
55807c22b78SDouglas Gregor       // This will not be the case if the 'subframework' is actually a symlink
55907c22b78SDouglas Gregor       // out to a top-level framework.
560e00c8b20SDouglas Gregor       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
56107c22b78SDouglas Gregor       bool FoundParent = false;
56207c22b78SDouglas Gregor       do {
56307c22b78SDouglas Gregor         // Get the parent directory name.
56407c22b78SDouglas Gregor         SubframeworkDirName
56507c22b78SDouglas Gregor           = llvm::sys::path::parent_path(SubframeworkDirName);
56607c22b78SDouglas Gregor         if (SubframeworkDirName.empty())
56707c22b78SDouglas Gregor           break;
56807c22b78SDouglas Gregor 
56907c22b78SDouglas Gregor         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
57007c22b78SDouglas Gregor           FoundParent = true;
57107c22b78SDouglas Gregor           break;
57207c22b78SDouglas Gregor         }
57307c22b78SDouglas Gregor       } while (true);
57407c22b78SDouglas Gregor 
57507c22b78SDouglas Gregor       if (!FoundParent)
57607c22b78SDouglas Gregor         continue;
57707c22b78SDouglas Gregor 
578e89dbc1dSDouglas Gregor       // FIXME: Do we want to warn about subframeworks without umbrella headers?
579056396aeSDouglas Gregor       SmallString<32> NameBuf;
580056396aeSDouglas Gregor       inferFrameworkModule(sanitizeFilenameAsIdentifier(
581056396aeSDouglas Gregor                              llvm::sys::path::stem(Dir->path()), NameBuf),
582056396aeSDouglas Gregor                            SubframeworkDir, IsSystem, Result);
583e89dbc1dSDouglas Gregor     }
584e89dbc1dSDouglas Gregor   }
585e89dbc1dSDouglas Gregor 
58611dfe6feSDouglas Gregor   // If the module is a top-level framework, automatically link against the
58711dfe6feSDouglas Gregor   // framework.
58811dfe6feSDouglas Gregor   if (!Result->isSubFramework()) {
58911dfe6feSDouglas Gregor     inferFrameworkLink(Result, FrameworkDir, FileMgr);
59011dfe6feSDouglas Gregor   }
59111dfe6feSDouglas Gregor 
59256c64013SDouglas Gregor   return Result;
59356c64013SDouglas Gregor }
59456c64013SDouglas Gregor 
595a89c5ac4SDouglas Gregor void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
59659527666SDouglas Gregor   Headers[UmbrellaHeader] = KnownHeader(Mod, /*Excluded=*/false);
59773141fa9SDouglas Gregor   Mod->Umbrella = UmbrellaHeader;
5987033127bSDouglas Gregor   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
599a89c5ac4SDouglas Gregor }
600a89c5ac4SDouglas Gregor 
601524e33e1SDouglas Gregor void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
602524e33e1SDouglas Gregor   Mod->Umbrella = UmbrellaDir;
603524e33e1SDouglas Gregor   UmbrellaDirs[UmbrellaDir] = Mod;
604524e33e1SDouglas Gregor }
605524e33e1SDouglas Gregor 
60659527666SDouglas Gregor void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
60759527666SDouglas Gregor                           bool Excluded) {
608b146baabSArgyrios Kyrtzidis   if (Excluded) {
60959527666SDouglas Gregor     Mod->ExcludedHeaders.push_back(Header);
610b146baabSArgyrios Kyrtzidis   } else {
611a89c5ac4SDouglas Gregor     Mod->Headers.push_back(Header);
6126f722b4eSArgyrios Kyrtzidis     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
6136f722b4eSArgyrios Kyrtzidis     HeaderInfo.MarkFileModuleHeader(Header, isCompilingModuleHeader);
614b146baabSArgyrios Kyrtzidis   }
61559527666SDouglas Gregor   Headers[Header] = KnownHeader(Mod, Excluded);
616a89c5ac4SDouglas Gregor }
617a89c5ac4SDouglas Gregor 
618514b636aSDouglas Gregor const FileEntry *
619e4412640SArgyrios Kyrtzidis ModuleMap::getContainingModuleMapFile(Module *Module) const {
620514b636aSDouglas Gregor   if (Module->DefinitionLoc.isInvalid() || !SourceMgr)
621514b636aSDouglas Gregor     return 0;
622514b636aSDouglas Gregor 
623514b636aSDouglas Gregor   return SourceMgr->getFileEntryForID(
624514b636aSDouglas Gregor            SourceMgr->getFileID(Module->DefinitionLoc));
625514b636aSDouglas Gregor }
626514b636aSDouglas Gregor 
627718292f2SDouglas Gregor void ModuleMap::dump() {
628718292f2SDouglas Gregor   llvm::errs() << "Modules:";
629718292f2SDouglas Gregor   for (llvm::StringMap<Module *>::iterator M = Modules.begin(),
630718292f2SDouglas Gregor                                         MEnd = Modules.end();
631718292f2SDouglas Gregor        M != MEnd; ++M)
632d28d1b8dSDouglas Gregor     M->getValue()->print(llvm::errs(), 2);
633718292f2SDouglas Gregor 
634718292f2SDouglas Gregor   llvm::errs() << "Headers:";
63559527666SDouglas Gregor   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
636718292f2SDouglas Gregor        H != HEnd; ++H) {
637718292f2SDouglas Gregor     llvm::errs() << "  \"" << H->first->getName() << "\" -> "
63859527666SDouglas Gregor                  << H->second.getModule()->getFullModuleName() << "\n";
639718292f2SDouglas Gregor   }
640718292f2SDouglas Gregor }
641718292f2SDouglas Gregor 
6422b82c2a5SDouglas Gregor bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
6432b82c2a5SDouglas Gregor   bool HadError = false;
6442b82c2a5SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
6452b82c2a5SDouglas Gregor     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I],
6462b82c2a5SDouglas Gregor                                               Complain);
647f5eedd05SDouglas Gregor     if (Export.getPointer() || Export.getInt())
6482b82c2a5SDouglas Gregor       Mod->Exports.push_back(Export);
6492b82c2a5SDouglas Gregor     else
6502b82c2a5SDouglas Gregor       HadError = true;
6512b82c2a5SDouglas Gregor   }
6522b82c2a5SDouglas Gregor   Mod->UnresolvedExports.clear();
6532b82c2a5SDouglas Gregor   return HadError;
6542b82c2a5SDouglas Gregor }
6552b82c2a5SDouglas Gregor 
656fb912657SDouglas Gregor bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
657fb912657SDouglas Gregor   bool HadError = false;
658fb912657SDouglas Gregor   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
659fb912657SDouglas Gregor     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
660fb912657SDouglas Gregor                                        Mod, Complain);
661fb912657SDouglas Gregor     if (!OtherMod) {
662fb912657SDouglas Gregor       HadError = true;
663fb912657SDouglas Gregor       continue;
664fb912657SDouglas Gregor     }
665fb912657SDouglas Gregor 
666fb912657SDouglas Gregor     Module::Conflict Conflict;
667fb912657SDouglas Gregor     Conflict.Other = OtherMod;
668fb912657SDouglas Gregor     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
669fb912657SDouglas Gregor     Mod->Conflicts.push_back(Conflict);
670fb912657SDouglas Gregor   }
671fb912657SDouglas Gregor   Mod->UnresolvedConflicts.clear();
672fb912657SDouglas Gregor   return HadError;
673fb912657SDouglas Gregor }
674fb912657SDouglas Gregor 
6750093b3c7SDouglas Gregor Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
6760093b3c7SDouglas Gregor   if (Loc.isInvalid())
6770093b3c7SDouglas Gregor     return 0;
6780093b3c7SDouglas Gregor 
6790093b3c7SDouglas Gregor   // Use the expansion location to determine which module we're in.
6800093b3c7SDouglas Gregor   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
6810093b3c7SDouglas Gregor   if (!ExpansionLoc.isFileID())
6820093b3c7SDouglas Gregor     return 0;
6830093b3c7SDouglas Gregor 
6840093b3c7SDouglas Gregor 
6850093b3c7SDouglas Gregor   const SourceManager &SrcMgr = Loc.getManager();
6860093b3c7SDouglas Gregor   FileID ExpansionFileID = ExpansionLoc.getFileID();
687224d8a74SDouglas Gregor 
688224d8a74SDouglas Gregor   while (const FileEntry *ExpansionFile
689224d8a74SDouglas Gregor            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
690224d8a74SDouglas Gregor     // Find the module that owns this header (if any).
691224d8a74SDouglas Gregor     if (Module *Mod = findModuleForHeader(ExpansionFile))
692224d8a74SDouglas Gregor       return Mod;
693224d8a74SDouglas Gregor 
694224d8a74SDouglas Gregor     // No module owns this header, so look up the inclusion chain to see if
695224d8a74SDouglas Gregor     // any included header has an associated module.
696224d8a74SDouglas Gregor     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
697224d8a74SDouglas Gregor     if (IncludeLoc.isInvalid())
6980093b3c7SDouglas Gregor       return 0;
6990093b3c7SDouglas Gregor 
700224d8a74SDouglas Gregor     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
701224d8a74SDouglas Gregor   }
702224d8a74SDouglas Gregor 
703224d8a74SDouglas Gregor   return 0;
7040093b3c7SDouglas Gregor }
7050093b3c7SDouglas Gregor 
706718292f2SDouglas Gregor //----------------------------------------------------------------------------//
707718292f2SDouglas Gregor // Module map file parser
708718292f2SDouglas Gregor //----------------------------------------------------------------------------//
709718292f2SDouglas Gregor 
710718292f2SDouglas Gregor namespace clang {
711718292f2SDouglas Gregor   /// \brief A token in a module map file.
712718292f2SDouglas Gregor   struct MMToken {
713718292f2SDouglas Gregor     enum TokenKind {
7141fb5c3a6SDouglas Gregor       Comma,
71535b13eceSDouglas Gregor       ConfigMacros,
716fb912657SDouglas Gregor       Conflict,
717718292f2SDouglas Gregor       EndOfFile,
718718292f2SDouglas Gregor       HeaderKeyword,
719718292f2SDouglas Gregor       Identifier,
72059527666SDouglas Gregor       ExcludeKeyword,
721718292f2SDouglas Gregor       ExplicitKeyword,
7222b82c2a5SDouglas Gregor       ExportKeyword,
723755b2055SDouglas Gregor       FrameworkKeyword,
7246ddfca91SDouglas Gregor       LinkKeyword,
725718292f2SDouglas Gregor       ModuleKeyword,
7262b82c2a5SDouglas Gregor       Period,
727718292f2SDouglas Gregor       UmbrellaKeyword,
7281fb5c3a6SDouglas Gregor       RequiresKeyword,
7292b82c2a5SDouglas Gregor       Star,
730718292f2SDouglas Gregor       StringLiteral,
731718292f2SDouglas Gregor       LBrace,
732a686e1b0SDouglas Gregor       RBrace,
733a686e1b0SDouglas Gregor       LSquare,
734a686e1b0SDouglas Gregor       RSquare
735718292f2SDouglas Gregor     } Kind;
736718292f2SDouglas Gregor 
737718292f2SDouglas Gregor     unsigned Location;
738718292f2SDouglas Gregor     unsigned StringLength;
739718292f2SDouglas Gregor     const char *StringData;
740718292f2SDouglas Gregor 
741718292f2SDouglas Gregor     void clear() {
742718292f2SDouglas Gregor       Kind = EndOfFile;
743718292f2SDouglas Gregor       Location = 0;
744718292f2SDouglas Gregor       StringLength = 0;
745718292f2SDouglas Gregor       StringData = 0;
746718292f2SDouglas Gregor     }
747718292f2SDouglas Gregor 
748718292f2SDouglas Gregor     bool is(TokenKind K) const { return Kind == K; }
749718292f2SDouglas Gregor 
750718292f2SDouglas Gregor     SourceLocation getLocation() const {
751718292f2SDouglas Gregor       return SourceLocation::getFromRawEncoding(Location);
752718292f2SDouglas Gregor     }
753718292f2SDouglas Gregor 
754718292f2SDouglas Gregor     StringRef getString() const {
755718292f2SDouglas Gregor       return StringRef(StringData, StringLength);
756718292f2SDouglas Gregor     }
757718292f2SDouglas Gregor   };
758718292f2SDouglas Gregor 
7599194a91dSDouglas Gregor   /// \brief The set of attributes that can be attached to a module.
7604442605fSBill Wendling   struct Attributes {
76135b13eceSDouglas Gregor     Attributes() : IsSystem(), IsExhaustive() { }
7629194a91dSDouglas Gregor 
7639194a91dSDouglas Gregor     /// \brief Whether this is a system module.
7649194a91dSDouglas Gregor     unsigned IsSystem : 1;
76535b13eceSDouglas Gregor 
76635b13eceSDouglas Gregor     /// \brief Whether this is an exhaustive set of configuration macros.
76735b13eceSDouglas Gregor     unsigned IsExhaustive : 1;
7689194a91dSDouglas Gregor   };
7699194a91dSDouglas Gregor 
7709194a91dSDouglas Gregor 
771718292f2SDouglas Gregor   class ModuleMapParser {
772718292f2SDouglas Gregor     Lexer &L;
773718292f2SDouglas Gregor     SourceManager &SourceMgr;
774bc10b9fbSDouglas Gregor 
775bc10b9fbSDouglas Gregor     /// \brief Default target information, used only for string literal
776bc10b9fbSDouglas Gregor     /// parsing.
777bc10b9fbSDouglas Gregor     const TargetInfo *Target;
778bc10b9fbSDouglas Gregor 
779718292f2SDouglas Gregor     DiagnosticsEngine &Diags;
780718292f2SDouglas Gregor     ModuleMap &Map;
781718292f2SDouglas Gregor 
7825257fc63SDouglas Gregor     /// \brief The directory that this module map resides in.
7835257fc63SDouglas Gregor     const DirectoryEntry *Directory;
7845257fc63SDouglas Gregor 
7853ec6663bSDouglas Gregor     /// \brief The directory containing Clang-supplied headers.
7863ec6663bSDouglas Gregor     const DirectoryEntry *BuiltinIncludeDir;
7873ec6663bSDouglas Gregor 
788718292f2SDouglas Gregor     /// \brief Whether an error occurred.
789718292f2SDouglas Gregor     bool HadError;
790718292f2SDouglas Gregor 
791718292f2SDouglas Gregor     /// \brief Stores string data for the various string literals referenced
792718292f2SDouglas Gregor     /// during parsing.
793718292f2SDouglas Gregor     llvm::BumpPtrAllocator StringData;
794718292f2SDouglas Gregor 
795718292f2SDouglas Gregor     /// \brief The current token.
796718292f2SDouglas Gregor     MMToken Tok;
797718292f2SDouglas Gregor 
798718292f2SDouglas Gregor     /// \brief The active module.
799de3ef502SDouglas Gregor     Module *ActiveModule;
800718292f2SDouglas Gregor 
801718292f2SDouglas Gregor     /// \brief Consume the current token and return its location.
802718292f2SDouglas Gregor     SourceLocation consumeToken();
803718292f2SDouglas Gregor 
804718292f2SDouglas Gregor     /// \brief Skip tokens until we reach the a token with the given kind
805718292f2SDouglas Gregor     /// (or the end of the file).
806718292f2SDouglas Gregor     void skipUntil(MMToken::TokenKind K);
807718292f2SDouglas Gregor 
808f857950dSDmitri Gribenko     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
809e7ab3669SDouglas Gregor     bool parseModuleId(ModuleId &Id);
810718292f2SDouglas Gregor     void parseModuleDecl();
8111fb5c3a6SDouglas Gregor     void parseRequiresDecl();
81259527666SDouglas Gregor     void parseHeaderDecl(SourceLocation UmbrellaLoc, SourceLocation ExcludeLoc);
813524e33e1SDouglas Gregor     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
8142b82c2a5SDouglas Gregor     void parseExportDecl();
8156ddfca91SDouglas Gregor     void parseLinkDecl();
81635b13eceSDouglas Gregor     void parseConfigMacros();
817fb912657SDouglas Gregor     void parseConflict();
8189194a91dSDouglas Gregor     void parseInferredModuleDecl(bool Framework, bool Explicit);
8194442605fSBill Wendling     bool parseOptionalAttributes(Attributes &Attrs);
820718292f2SDouglas Gregor 
8217033127bSDouglas Gregor     const DirectoryEntry *getOverriddenHeaderSearchDir();
8227033127bSDouglas Gregor 
823718292f2SDouglas Gregor   public:
824718292f2SDouglas Gregor     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr,
825bc10b9fbSDouglas Gregor                              const TargetInfo *Target,
826718292f2SDouglas Gregor                              DiagnosticsEngine &Diags,
8275257fc63SDouglas Gregor                              ModuleMap &Map,
8283ec6663bSDouglas Gregor                              const DirectoryEntry *Directory,
8293ec6663bSDouglas Gregor                              const DirectoryEntry *BuiltinIncludeDir)
830bc10b9fbSDouglas Gregor       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map),
8313ec6663bSDouglas Gregor         Directory(Directory), BuiltinIncludeDir(BuiltinIncludeDir),
8323ec6663bSDouglas Gregor         HadError(false), ActiveModule(0)
833718292f2SDouglas Gregor     {
834718292f2SDouglas Gregor       Tok.clear();
835718292f2SDouglas Gregor       consumeToken();
836718292f2SDouglas Gregor     }
837718292f2SDouglas Gregor 
838718292f2SDouglas Gregor     bool parseModuleMapFile();
839718292f2SDouglas Gregor   };
840718292f2SDouglas Gregor }
841718292f2SDouglas Gregor 
842718292f2SDouglas Gregor SourceLocation ModuleMapParser::consumeToken() {
843718292f2SDouglas Gregor retry:
844718292f2SDouglas Gregor   SourceLocation Result = Tok.getLocation();
845718292f2SDouglas Gregor   Tok.clear();
846718292f2SDouglas Gregor 
847718292f2SDouglas Gregor   Token LToken;
848718292f2SDouglas Gregor   L.LexFromRawLexer(LToken);
849718292f2SDouglas Gregor   Tok.Location = LToken.getLocation().getRawEncoding();
850718292f2SDouglas Gregor   switch (LToken.getKind()) {
851718292f2SDouglas Gregor   case tok::raw_identifier:
852718292f2SDouglas Gregor     Tok.StringData = LToken.getRawIdentifierData();
853718292f2SDouglas Gregor     Tok.StringLength = LToken.getLength();
854718292f2SDouglas Gregor     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(Tok.getString())
85535b13eceSDouglas Gregor                  .Case("config_macros", MMToken::ConfigMacros)
856fb912657SDouglas Gregor                  .Case("conflict", MMToken::Conflict)
85759527666SDouglas Gregor                  .Case("exclude", MMToken::ExcludeKeyword)
858718292f2SDouglas Gregor                  .Case("explicit", MMToken::ExplicitKeyword)
8592b82c2a5SDouglas Gregor                  .Case("export", MMToken::ExportKeyword)
860755b2055SDouglas Gregor                  .Case("framework", MMToken::FrameworkKeyword)
86135b13eceSDouglas Gregor                  .Case("header", MMToken::HeaderKeyword)
8626ddfca91SDouglas Gregor                  .Case("link", MMToken::LinkKeyword)
863718292f2SDouglas Gregor                  .Case("module", MMToken::ModuleKeyword)
8641fb5c3a6SDouglas Gregor                  .Case("requires", MMToken::RequiresKeyword)
865718292f2SDouglas Gregor                  .Case("umbrella", MMToken::UmbrellaKeyword)
866718292f2SDouglas Gregor                  .Default(MMToken::Identifier);
867718292f2SDouglas Gregor     break;
868718292f2SDouglas Gregor 
8691fb5c3a6SDouglas Gregor   case tok::comma:
8701fb5c3a6SDouglas Gregor     Tok.Kind = MMToken::Comma;
8711fb5c3a6SDouglas Gregor     break;
8721fb5c3a6SDouglas Gregor 
873718292f2SDouglas Gregor   case tok::eof:
874718292f2SDouglas Gregor     Tok.Kind = MMToken::EndOfFile;
875718292f2SDouglas Gregor     break;
876718292f2SDouglas Gregor 
877718292f2SDouglas Gregor   case tok::l_brace:
878718292f2SDouglas Gregor     Tok.Kind = MMToken::LBrace;
879718292f2SDouglas Gregor     break;
880718292f2SDouglas Gregor 
881a686e1b0SDouglas Gregor   case tok::l_square:
882a686e1b0SDouglas Gregor     Tok.Kind = MMToken::LSquare;
883a686e1b0SDouglas Gregor     break;
884a686e1b0SDouglas Gregor 
8852b82c2a5SDouglas Gregor   case tok::period:
8862b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Period;
8872b82c2a5SDouglas Gregor     break;
8882b82c2a5SDouglas Gregor 
889718292f2SDouglas Gregor   case tok::r_brace:
890718292f2SDouglas Gregor     Tok.Kind = MMToken::RBrace;
891718292f2SDouglas Gregor     break;
892718292f2SDouglas Gregor 
893a686e1b0SDouglas Gregor   case tok::r_square:
894a686e1b0SDouglas Gregor     Tok.Kind = MMToken::RSquare;
895a686e1b0SDouglas Gregor     break;
896a686e1b0SDouglas Gregor 
8972b82c2a5SDouglas Gregor   case tok::star:
8982b82c2a5SDouglas Gregor     Tok.Kind = MMToken::Star;
8992b82c2a5SDouglas Gregor     break;
9002b82c2a5SDouglas Gregor 
901718292f2SDouglas Gregor   case tok::string_literal: {
902d67aea28SRichard Smith     if (LToken.hasUDSuffix()) {
903d67aea28SRichard Smith       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
904d67aea28SRichard Smith       HadError = true;
905d67aea28SRichard Smith       goto retry;
906d67aea28SRichard Smith     }
907d67aea28SRichard Smith 
908718292f2SDouglas Gregor     // Parse the string literal.
909718292f2SDouglas Gregor     LangOptions LangOpts;
910718292f2SDouglas Gregor     StringLiteralParser StringLiteral(&LToken, 1, SourceMgr, LangOpts, *Target);
911718292f2SDouglas Gregor     if (StringLiteral.hadError)
912718292f2SDouglas Gregor       goto retry;
913718292f2SDouglas Gregor 
914718292f2SDouglas Gregor     // Copy the string literal into our string data allocator.
915718292f2SDouglas Gregor     unsigned Length = StringLiteral.GetStringLength();
916718292f2SDouglas Gregor     char *Saved = StringData.Allocate<char>(Length + 1);
917718292f2SDouglas Gregor     memcpy(Saved, StringLiteral.GetString().data(), Length);
918718292f2SDouglas Gregor     Saved[Length] = 0;
919718292f2SDouglas Gregor 
920718292f2SDouglas Gregor     // Form the token.
921718292f2SDouglas Gregor     Tok.Kind = MMToken::StringLiteral;
922718292f2SDouglas Gregor     Tok.StringData = Saved;
923718292f2SDouglas Gregor     Tok.StringLength = Length;
924718292f2SDouglas Gregor     break;
925718292f2SDouglas Gregor   }
926718292f2SDouglas Gregor 
927718292f2SDouglas Gregor   case tok::comment:
928718292f2SDouglas Gregor     goto retry;
929718292f2SDouglas Gregor 
930718292f2SDouglas Gregor   default:
931718292f2SDouglas Gregor     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
932718292f2SDouglas Gregor     HadError = true;
933718292f2SDouglas Gregor     goto retry;
934718292f2SDouglas Gregor   }
935718292f2SDouglas Gregor 
936718292f2SDouglas Gregor   return Result;
937718292f2SDouglas Gregor }
938718292f2SDouglas Gregor 
939718292f2SDouglas Gregor void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
940718292f2SDouglas Gregor   unsigned braceDepth = 0;
941a686e1b0SDouglas Gregor   unsigned squareDepth = 0;
942718292f2SDouglas Gregor   do {
943718292f2SDouglas Gregor     switch (Tok.Kind) {
944718292f2SDouglas Gregor     case MMToken::EndOfFile:
945718292f2SDouglas Gregor       return;
946718292f2SDouglas Gregor 
947718292f2SDouglas Gregor     case MMToken::LBrace:
948a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
949718292f2SDouglas Gregor         return;
950718292f2SDouglas Gregor 
951718292f2SDouglas Gregor       ++braceDepth;
952718292f2SDouglas Gregor       break;
953718292f2SDouglas Gregor 
954a686e1b0SDouglas Gregor     case MMToken::LSquare:
955a686e1b0SDouglas Gregor       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
956a686e1b0SDouglas Gregor         return;
957a686e1b0SDouglas Gregor 
958a686e1b0SDouglas Gregor       ++squareDepth;
959a686e1b0SDouglas Gregor       break;
960a686e1b0SDouglas Gregor 
961718292f2SDouglas Gregor     case MMToken::RBrace:
962718292f2SDouglas Gregor       if (braceDepth > 0)
963718292f2SDouglas Gregor         --braceDepth;
964718292f2SDouglas Gregor       else if (Tok.is(K))
965718292f2SDouglas Gregor         return;
966718292f2SDouglas Gregor       break;
967718292f2SDouglas Gregor 
968a686e1b0SDouglas Gregor     case MMToken::RSquare:
969a686e1b0SDouglas Gregor       if (squareDepth > 0)
970a686e1b0SDouglas Gregor         --squareDepth;
971a686e1b0SDouglas Gregor       else if (Tok.is(K))
972a686e1b0SDouglas Gregor         return;
973a686e1b0SDouglas Gregor       break;
974a686e1b0SDouglas Gregor 
975718292f2SDouglas Gregor     default:
976a686e1b0SDouglas Gregor       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
977718292f2SDouglas Gregor         return;
978718292f2SDouglas Gregor       break;
979718292f2SDouglas Gregor     }
980718292f2SDouglas Gregor 
981718292f2SDouglas Gregor    consumeToken();
982718292f2SDouglas Gregor   } while (true);
983718292f2SDouglas Gregor }
984718292f2SDouglas Gregor 
985e7ab3669SDouglas Gregor /// \brief Parse a module-id.
986e7ab3669SDouglas Gregor ///
987e7ab3669SDouglas Gregor ///   module-id:
988e7ab3669SDouglas Gregor ///     identifier
989e7ab3669SDouglas Gregor ///     identifier '.' module-id
990e7ab3669SDouglas Gregor ///
991e7ab3669SDouglas Gregor /// \returns true if an error occurred, false otherwise.
992e7ab3669SDouglas Gregor bool ModuleMapParser::parseModuleId(ModuleId &Id) {
993e7ab3669SDouglas Gregor   Id.clear();
994e7ab3669SDouglas Gregor   do {
995e7ab3669SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
996e7ab3669SDouglas Gregor       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
997e7ab3669SDouglas Gregor       consumeToken();
998e7ab3669SDouglas Gregor     } else {
999e7ab3669SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
1000e7ab3669SDouglas Gregor       return true;
1001e7ab3669SDouglas Gregor     }
1002e7ab3669SDouglas Gregor 
1003e7ab3669SDouglas Gregor     if (!Tok.is(MMToken::Period))
1004e7ab3669SDouglas Gregor       break;
1005e7ab3669SDouglas Gregor 
1006e7ab3669SDouglas Gregor     consumeToken();
1007e7ab3669SDouglas Gregor   } while (true);
1008e7ab3669SDouglas Gregor 
1009e7ab3669SDouglas Gregor   return false;
1010e7ab3669SDouglas Gregor }
1011e7ab3669SDouglas Gregor 
1012a686e1b0SDouglas Gregor namespace {
1013a686e1b0SDouglas Gregor   /// \brief Enumerates the known attributes.
1014a686e1b0SDouglas Gregor   enum AttributeKind {
1015a686e1b0SDouglas Gregor     /// \brief An unknown attribute.
1016a686e1b0SDouglas Gregor     AT_unknown,
1017a686e1b0SDouglas Gregor     /// \brief The 'system' attribute.
101835b13eceSDouglas Gregor     AT_system,
101935b13eceSDouglas Gregor     /// \brief The 'exhaustive' attribute.
102035b13eceSDouglas Gregor     AT_exhaustive
1021a686e1b0SDouglas Gregor   };
1022a686e1b0SDouglas Gregor }
1023a686e1b0SDouglas Gregor 
1024718292f2SDouglas Gregor /// \brief Parse a module declaration.
1025718292f2SDouglas Gregor ///
1026718292f2SDouglas Gregor ///   module-declaration:
1027a686e1b0SDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt]
1028a686e1b0SDouglas Gregor ///       { module-member* }
1029a686e1b0SDouglas Gregor ///
1030718292f2SDouglas Gregor ///   module-member:
10311fb5c3a6SDouglas Gregor ///     requires-declaration
1032718292f2SDouglas Gregor ///     header-declaration
1033e7ab3669SDouglas Gregor ///     submodule-declaration
10342b82c2a5SDouglas Gregor ///     export-declaration
10356ddfca91SDouglas Gregor ///     link-declaration
103673441091SDouglas Gregor ///
103773441091SDouglas Gregor ///   submodule-declaration:
103873441091SDouglas Gregor ///     module-declaration
103973441091SDouglas Gregor ///     inferred-submodule-declaration
1040718292f2SDouglas Gregor void ModuleMapParser::parseModuleDecl() {
1041755b2055SDouglas Gregor   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
1042755b2055SDouglas Gregor          Tok.is(MMToken::FrameworkKeyword));
1043f2161a70SDouglas Gregor   // Parse 'explicit' or 'framework' keyword, if present.
1044e7ab3669SDouglas Gregor   SourceLocation ExplicitLoc;
1045718292f2SDouglas Gregor   bool Explicit = false;
1046f2161a70SDouglas Gregor   bool Framework = false;
1047755b2055SDouglas Gregor 
1048f2161a70SDouglas Gregor   // Parse 'explicit' keyword, if present.
1049f2161a70SDouglas Gregor   if (Tok.is(MMToken::ExplicitKeyword)) {
1050e7ab3669SDouglas Gregor     ExplicitLoc = consumeToken();
1051f2161a70SDouglas Gregor     Explicit = true;
1052f2161a70SDouglas Gregor   }
1053f2161a70SDouglas Gregor 
1054f2161a70SDouglas Gregor   // Parse 'framework' keyword, if present.
1055755b2055SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
1056755b2055SDouglas Gregor     consumeToken();
1057755b2055SDouglas Gregor     Framework = true;
1058755b2055SDouglas Gregor   }
1059718292f2SDouglas Gregor 
1060718292f2SDouglas Gregor   // Parse 'module' keyword.
1061718292f2SDouglas Gregor   if (!Tok.is(MMToken::ModuleKeyword)) {
1062d6343c99SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1063718292f2SDouglas Gregor     consumeToken();
1064718292f2SDouglas Gregor     HadError = true;
1065718292f2SDouglas Gregor     return;
1066718292f2SDouglas Gregor   }
1067718292f2SDouglas Gregor   consumeToken(); // 'module' keyword
1068718292f2SDouglas Gregor 
106973441091SDouglas Gregor   // If we have a wildcard for the module name, this is an inferred submodule.
107073441091SDouglas Gregor   // Parse it.
107173441091SDouglas Gregor   if (Tok.is(MMToken::Star))
10729194a91dSDouglas Gregor     return parseInferredModuleDecl(Framework, Explicit);
107373441091SDouglas Gregor 
1074718292f2SDouglas Gregor   // Parse the module name.
1075e7ab3669SDouglas Gregor   ModuleId Id;
1076e7ab3669SDouglas Gregor   if (parseModuleId(Id)) {
1077718292f2SDouglas Gregor     HadError = true;
1078718292f2SDouglas Gregor     return;
1079718292f2SDouglas Gregor   }
1080e7ab3669SDouglas Gregor 
1081e7ab3669SDouglas Gregor   if (ActiveModule) {
1082e7ab3669SDouglas Gregor     if (Id.size() > 1) {
1083e7ab3669SDouglas Gregor       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
1084e7ab3669SDouglas Gregor         << SourceRange(Id.front().second, Id.back().second);
1085e7ab3669SDouglas Gregor 
1086e7ab3669SDouglas Gregor       HadError = true;
1087e7ab3669SDouglas Gregor       return;
1088e7ab3669SDouglas Gregor     }
1089e7ab3669SDouglas Gregor   } else if (Id.size() == 1 && Explicit) {
1090e7ab3669SDouglas Gregor     // Top-level modules can't be explicit.
1091e7ab3669SDouglas Gregor     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
1092e7ab3669SDouglas Gregor     Explicit = false;
1093e7ab3669SDouglas Gregor     ExplicitLoc = SourceLocation();
1094e7ab3669SDouglas Gregor     HadError = true;
1095e7ab3669SDouglas Gregor   }
1096e7ab3669SDouglas Gregor 
1097e7ab3669SDouglas Gregor   Module *PreviousActiveModule = ActiveModule;
1098e7ab3669SDouglas Gregor   if (Id.size() > 1) {
1099e7ab3669SDouglas Gregor     // This module map defines a submodule. Go find the module of which it
1100e7ab3669SDouglas Gregor     // is a submodule.
1101e7ab3669SDouglas Gregor     ActiveModule = 0;
1102e7ab3669SDouglas Gregor     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
1103e7ab3669SDouglas Gregor       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
1104e7ab3669SDouglas Gregor         ActiveModule = Next;
1105e7ab3669SDouglas Gregor         continue;
1106e7ab3669SDouglas Gregor       }
1107e7ab3669SDouglas Gregor 
1108e7ab3669SDouglas Gregor       if (ActiveModule) {
1109e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
1110e7ab3669SDouglas Gregor           << Id[I].first << ActiveModule->getTopLevelModule();
1111e7ab3669SDouglas Gregor       } else {
1112e7ab3669SDouglas Gregor         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
1113e7ab3669SDouglas Gregor       }
1114e7ab3669SDouglas Gregor       HadError = true;
1115e7ab3669SDouglas Gregor       return;
1116e7ab3669SDouglas Gregor     }
1117e7ab3669SDouglas Gregor   }
1118e7ab3669SDouglas Gregor 
1119e7ab3669SDouglas Gregor   StringRef ModuleName = Id.back().first;
1120e7ab3669SDouglas Gregor   SourceLocation ModuleNameLoc = Id.back().second;
1121718292f2SDouglas Gregor 
1122a686e1b0SDouglas Gregor   // Parse the optional attribute list.
11234442605fSBill Wendling   Attributes Attrs;
11249194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
1125a686e1b0SDouglas Gregor 
1126718292f2SDouglas Gregor   // Parse the opening brace.
1127718292f2SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
1128718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
1129718292f2SDouglas Gregor       << ModuleName;
1130718292f2SDouglas Gregor     HadError = true;
1131718292f2SDouglas Gregor     return;
1132718292f2SDouglas Gregor   }
1133718292f2SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
1134718292f2SDouglas Gregor 
1135718292f2SDouglas Gregor   // Determine whether this (sub)module has already been defined.
1136eb90e830SDouglas Gregor   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
1137fcc54a3bSDouglas Gregor     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
1138fcc54a3bSDouglas Gregor       // Skip the module definition.
1139fcc54a3bSDouglas Gregor       skipUntil(MMToken::RBrace);
1140fcc54a3bSDouglas Gregor       if (Tok.is(MMToken::RBrace))
1141fcc54a3bSDouglas Gregor         consumeToken();
1142fcc54a3bSDouglas Gregor       else {
1143fcc54a3bSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1144fcc54a3bSDouglas Gregor         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1145fcc54a3bSDouglas Gregor         HadError = true;
1146fcc54a3bSDouglas Gregor       }
1147fcc54a3bSDouglas Gregor       return;
1148fcc54a3bSDouglas Gregor     }
1149fcc54a3bSDouglas Gregor 
1150718292f2SDouglas Gregor     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
1151718292f2SDouglas Gregor       << ModuleName;
1152eb90e830SDouglas Gregor     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
1153718292f2SDouglas Gregor 
1154718292f2SDouglas Gregor     // Skip the module definition.
1155718292f2SDouglas Gregor     skipUntil(MMToken::RBrace);
1156718292f2SDouglas Gregor     if (Tok.is(MMToken::RBrace))
1157718292f2SDouglas Gregor       consumeToken();
1158718292f2SDouglas Gregor 
1159718292f2SDouglas Gregor     HadError = true;
1160718292f2SDouglas Gregor     return;
1161718292f2SDouglas Gregor   }
1162718292f2SDouglas Gregor 
1163718292f2SDouglas Gregor   // Start defining this module.
1164eb90e830SDouglas Gregor   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
1165eb90e830SDouglas Gregor                                         Explicit).first;
1166eb90e830SDouglas Gregor   ActiveModule->DefinitionLoc = ModuleNameLoc;
11679194a91dSDouglas Gregor   if (Attrs.IsSystem)
1168a686e1b0SDouglas Gregor     ActiveModule->IsSystem = true;
1169718292f2SDouglas Gregor 
1170718292f2SDouglas Gregor   bool Done = false;
1171718292f2SDouglas Gregor   do {
1172718292f2SDouglas Gregor     switch (Tok.Kind) {
1173718292f2SDouglas Gregor     case MMToken::EndOfFile:
1174718292f2SDouglas Gregor     case MMToken::RBrace:
1175718292f2SDouglas Gregor       Done = true;
1176718292f2SDouglas Gregor       break;
1177718292f2SDouglas Gregor 
117835b13eceSDouglas Gregor     case MMToken::ConfigMacros:
117935b13eceSDouglas Gregor       parseConfigMacros();
118035b13eceSDouglas Gregor       break;
118135b13eceSDouglas Gregor 
1182fb912657SDouglas Gregor     case MMToken::Conflict:
1183fb912657SDouglas Gregor       parseConflict();
1184fb912657SDouglas Gregor       break;
1185fb912657SDouglas Gregor 
1186718292f2SDouglas Gregor     case MMToken::ExplicitKeyword:
1187f2161a70SDouglas Gregor     case MMToken::FrameworkKeyword:
1188718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1189718292f2SDouglas Gregor       parseModuleDecl();
1190718292f2SDouglas Gregor       break;
1191718292f2SDouglas Gregor 
11922b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
11932b82c2a5SDouglas Gregor       parseExportDecl();
11942b82c2a5SDouglas Gregor       break;
11952b82c2a5SDouglas Gregor 
11961fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
11971fb5c3a6SDouglas Gregor       parseRequiresDecl();
11981fb5c3a6SDouglas Gregor       break;
11991fb5c3a6SDouglas Gregor 
1200524e33e1SDouglas Gregor     case MMToken::UmbrellaKeyword: {
1201524e33e1SDouglas Gregor       SourceLocation UmbrellaLoc = consumeToken();
1202524e33e1SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword))
120359527666SDouglas Gregor         parseHeaderDecl(UmbrellaLoc, SourceLocation());
1204524e33e1SDouglas Gregor       else
1205524e33e1SDouglas Gregor         parseUmbrellaDirDecl(UmbrellaLoc);
1206718292f2SDouglas Gregor       break;
1207524e33e1SDouglas Gregor     }
1208718292f2SDouglas Gregor 
120959527666SDouglas Gregor     case MMToken::ExcludeKeyword: {
121059527666SDouglas Gregor       SourceLocation ExcludeLoc = consumeToken();
121159527666SDouglas Gregor       if (Tok.is(MMToken::HeaderKeyword)) {
121259527666SDouglas Gregor         parseHeaderDecl(SourceLocation(), ExcludeLoc);
121359527666SDouglas Gregor       } else {
121459527666SDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
121559527666SDouglas Gregor           << "exclude";
121659527666SDouglas Gregor       }
121759527666SDouglas Gregor       break;
121859527666SDouglas Gregor     }
121959527666SDouglas Gregor 
1220322f633cSDouglas Gregor     case MMToken::HeaderKeyword:
122159527666SDouglas Gregor       parseHeaderDecl(SourceLocation(), SourceLocation());
1222718292f2SDouglas Gregor       break;
1223718292f2SDouglas Gregor 
12246ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
12256ddfca91SDouglas Gregor       parseLinkDecl();
12266ddfca91SDouglas Gregor       break;
12276ddfca91SDouglas Gregor 
1228718292f2SDouglas Gregor     default:
1229718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
1230718292f2SDouglas Gregor       consumeToken();
1231718292f2SDouglas Gregor       break;
1232718292f2SDouglas Gregor     }
1233718292f2SDouglas Gregor   } while (!Done);
1234718292f2SDouglas Gregor 
1235718292f2SDouglas Gregor   if (Tok.is(MMToken::RBrace))
1236718292f2SDouglas Gregor     consumeToken();
1237718292f2SDouglas Gregor   else {
1238718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
1239718292f2SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
1240718292f2SDouglas Gregor     HadError = true;
1241718292f2SDouglas Gregor   }
1242718292f2SDouglas Gregor 
124311dfe6feSDouglas Gregor   // If the active module is a top-level framework, and there are no link
124411dfe6feSDouglas Gregor   // libraries, automatically link against the framework.
124511dfe6feSDouglas Gregor   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
124611dfe6feSDouglas Gregor       ActiveModule->LinkLibraries.empty()) {
124711dfe6feSDouglas Gregor     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
124811dfe6feSDouglas Gregor   }
124911dfe6feSDouglas Gregor 
1250e7ab3669SDouglas Gregor   // We're done parsing this module. Pop back to the previous module.
1251e7ab3669SDouglas Gregor   ActiveModule = PreviousActiveModule;
1252718292f2SDouglas Gregor }
1253718292f2SDouglas Gregor 
12541fb5c3a6SDouglas Gregor /// \brief Parse a requires declaration.
12551fb5c3a6SDouglas Gregor ///
12561fb5c3a6SDouglas Gregor ///   requires-declaration:
12571fb5c3a6SDouglas Gregor ///     'requires' feature-list
12581fb5c3a6SDouglas Gregor ///
12591fb5c3a6SDouglas Gregor ///   feature-list:
12601fb5c3a6SDouglas Gregor ///     identifier ',' feature-list
12611fb5c3a6SDouglas Gregor ///     identifier
12621fb5c3a6SDouglas Gregor void ModuleMapParser::parseRequiresDecl() {
12631fb5c3a6SDouglas Gregor   assert(Tok.is(MMToken::RequiresKeyword));
12641fb5c3a6SDouglas Gregor 
12651fb5c3a6SDouglas Gregor   // Parse 'requires' keyword.
12661fb5c3a6SDouglas Gregor   consumeToken();
12671fb5c3a6SDouglas Gregor 
12681fb5c3a6SDouglas Gregor   // Parse the feature-list.
12691fb5c3a6SDouglas Gregor   do {
12701fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
12711fb5c3a6SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
12721fb5c3a6SDouglas Gregor       HadError = true;
12731fb5c3a6SDouglas Gregor       return;
12741fb5c3a6SDouglas Gregor     }
12751fb5c3a6SDouglas Gregor 
12761fb5c3a6SDouglas Gregor     // Consume the feature name.
12771fb5c3a6SDouglas Gregor     std::string Feature = Tok.getString();
12781fb5c3a6SDouglas Gregor     consumeToken();
12791fb5c3a6SDouglas Gregor 
12801fb5c3a6SDouglas Gregor     // Add this feature.
128189929282SDouglas Gregor     ActiveModule->addRequirement(Feature, Map.LangOpts, *Map.Target);
12821fb5c3a6SDouglas Gregor 
12831fb5c3a6SDouglas Gregor     if (!Tok.is(MMToken::Comma))
12841fb5c3a6SDouglas Gregor       break;
12851fb5c3a6SDouglas Gregor 
12861fb5c3a6SDouglas Gregor     // Consume the comma.
12871fb5c3a6SDouglas Gregor     consumeToken();
12881fb5c3a6SDouglas Gregor   } while (true);
12891fb5c3a6SDouglas Gregor }
12901fb5c3a6SDouglas Gregor 
1291f2161a70SDouglas Gregor /// \brief Append to \p Paths the set of paths needed to get to the
1292f2161a70SDouglas Gregor /// subframework in which the given module lives.
1293bf8da9d7SBenjamin Kramer static void appendSubframeworkPaths(Module *Mod,
1294f857950dSDmitri Gribenko                                     SmallVectorImpl<char> &Path) {
1295f2161a70SDouglas Gregor   // Collect the framework names from the given module to the top-level module.
1296f857950dSDmitri Gribenko   SmallVector<StringRef, 2> Paths;
1297f2161a70SDouglas Gregor   for (; Mod; Mod = Mod->Parent) {
1298f2161a70SDouglas Gregor     if (Mod->IsFramework)
1299f2161a70SDouglas Gregor       Paths.push_back(Mod->Name);
1300f2161a70SDouglas Gregor   }
1301f2161a70SDouglas Gregor 
1302f2161a70SDouglas Gregor   if (Paths.empty())
1303f2161a70SDouglas Gregor     return;
1304f2161a70SDouglas Gregor 
1305f2161a70SDouglas Gregor   // Add Frameworks/Name.framework for each subframework.
1306f2161a70SDouglas Gregor   for (unsigned I = Paths.size() - 1; I != 0; --I) {
1307f2161a70SDouglas Gregor     llvm::sys::path::append(Path, "Frameworks");
1308f2161a70SDouglas Gregor     llvm::sys::path::append(Path, Paths[I-1] + ".framework");
1309f2161a70SDouglas Gregor   }
1310f2161a70SDouglas Gregor }
1311f2161a70SDouglas Gregor 
1312718292f2SDouglas Gregor /// \brief Parse a header declaration.
1313718292f2SDouglas Gregor ///
1314718292f2SDouglas Gregor ///   header-declaration:
1315322f633cSDouglas Gregor ///     'umbrella'[opt] 'header' string-literal
131659527666SDouglas Gregor ///     'exclude'[opt] 'header' string-literal
131759527666SDouglas Gregor void ModuleMapParser::parseHeaderDecl(SourceLocation UmbrellaLoc,
131859527666SDouglas Gregor                                       SourceLocation ExcludeLoc) {
1319718292f2SDouglas Gregor   assert(Tok.is(MMToken::HeaderKeyword));
13201871ed3dSBenjamin Kramer   consumeToken();
1321718292f2SDouglas Gregor 
1322322f633cSDouglas Gregor   bool Umbrella = UmbrellaLoc.isValid();
132359527666SDouglas Gregor   bool Exclude = ExcludeLoc.isValid();
132459527666SDouglas Gregor   assert(!(Umbrella && Exclude) && "Cannot have both 'umbrella' and 'exclude'");
1325718292f2SDouglas Gregor   // Parse the header name.
1326718292f2SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1327718292f2SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1328718292f2SDouglas Gregor       << "header";
1329718292f2SDouglas Gregor     HadError = true;
1330718292f2SDouglas Gregor     return;
1331718292f2SDouglas Gregor   }
1332e7ab3669SDouglas Gregor   std::string FileName = Tok.getString();
1333718292f2SDouglas Gregor   SourceLocation FileNameLoc = consumeToken();
1334718292f2SDouglas Gregor 
1335524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1336524e33e1SDouglas Gregor   if (Umbrella && ActiveModule->Umbrella) {
1337524e33e1SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_umbrella_clash)
1338524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1339322f633cSDouglas Gregor     HadError = true;
1340322f633cSDouglas Gregor     return;
1341322f633cSDouglas Gregor   }
1342322f633cSDouglas Gregor 
13435257fc63SDouglas Gregor   // Look for this file.
1344e7ab3669SDouglas Gregor   const FileEntry *File = 0;
13453ec6663bSDouglas Gregor   const FileEntry *BuiltinFile = 0;
13462c1dd271SDylan Noblesmith   SmallString<128> PathName;
1347e7ab3669SDouglas Gregor   if (llvm::sys::path::is_absolute(FileName)) {
1348e7ab3669SDouglas Gregor     PathName = FileName;
1349e7ab3669SDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
13507033127bSDouglas Gregor   } else if (const DirectoryEntry *Dir = getOverriddenHeaderSearchDir()) {
13517033127bSDouglas Gregor     PathName = Dir->getName();
13527033127bSDouglas Gregor     llvm::sys::path::append(PathName, FileName);
13537033127bSDouglas Gregor     File = SourceMgr.getFileManager().getFile(PathName);
1354e7ab3669SDouglas Gregor   } else {
1355e7ab3669SDouglas Gregor     // Search for the header file within the search directory.
13567033127bSDouglas Gregor     PathName = Directory->getName();
1357e7ab3669SDouglas Gregor     unsigned PathLength = PathName.size();
1358755b2055SDouglas Gregor 
1359f2161a70SDouglas Gregor     if (ActiveModule->isPartOfFramework()) {
1360f2161a70SDouglas Gregor       appendSubframeworkPaths(ActiveModule, PathName);
1361755b2055SDouglas Gregor 
1362e7ab3669SDouglas Gregor       // Check whether this file is in the public headers.
1363e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, "Headers");
13645257fc63SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1365e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
1366e7ab3669SDouglas Gregor 
1367e7ab3669SDouglas Gregor       if (!File) {
1368e7ab3669SDouglas Gregor         // Check whether this file is in the private headers.
1369e7ab3669SDouglas Gregor         PathName.resize(PathLength);
1370e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, "PrivateHeaders");
1371e7ab3669SDouglas Gregor         llvm::sys::path::append(PathName, FileName);
1372e7ab3669SDouglas Gregor         File = SourceMgr.getFileManager().getFile(PathName);
1373e7ab3669SDouglas Gregor       }
1374e7ab3669SDouglas Gregor     } else {
1375e7ab3669SDouglas Gregor       // Lookup for normal headers.
1376e7ab3669SDouglas Gregor       llvm::sys::path::append(PathName, FileName);
1377e7ab3669SDouglas Gregor       File = SourceMgr.getFileManager().getFile(PathName);
13783ec6663bSDouglas Gregor 
13793ec6663bSDouglas Gregor       // If this is a system module with a top-level header, this header
13803ec6663bSDouglas Gregor       // may have a counterpart (or replacement) in the set of headers
13813ec6663bSDouglas Gregor       // supplied by Clang. Find that builtin header.
13823ec6663bSDouglas Gregor       if (ActiveModule->IsSystem && !Umbrella && BuiltinIncludeDir &&
13833ec6663bSDouglas Gregor           BuiltinIncludeDir != Directory && isBuiltinHeader(FileName)) {
13842c1dd271SDylan Noblesmith         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
13853ec6663bSDouglas Gregor         llvm::sys::path::append(BuiltinPathName, FileName);
13863ec6663bSDouglas Gregor         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
13873ec6663bSDouglas Gregor 
13883ec6663bSDouglas Gregor         // If Clang supplies this header but the underlying system does not,
13893ec6663bSDouglas Gregor         // just silently swap in our builtin version. Otherwise, we'll end
13903ec6663bSDouglas Gregor         // up adding both (later).
13913ec6663bSDouglas Gregor         if (!File && BuiltinFile) {
13923ec6663bSDouglas Gregor           File = BuiltinFile;
13933ec6663bSDouglas Gregor           BuiltinFile = 0;
13943ec6663bSDouglas Gregor         }
13953ec6663bSDouglas Gregor       }
1396e7ab3669SDouglas Gregor     }
1397e7ab3669SDouglas Gregor   }
13985257fc63SDouglas Gregor 
13995257fc63SDouglas Gregor   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
14005257fc63SDouglas Gregor   // Come up with a lazy way to do this.
1401e7ab3669SDouglas Gregor   if (File) {
140259527666SDouglas Gregor     if (ModuleMap::KnownHeader OwningModule = Map.Headers[File]) {
14035257fc63SDouglas Gregor       Diags.Report(FileNameLoc, diag::err_mmap_header_conflict)
140459527666SDouglas Gregor         << FileName << OwningModule.getModule()->getFullModuleName();
14055257fc63SDouglas Gregor       HadError = true;
1406322f633cSDouglas Gregor     } else if (Umbrella) {
1407322f633cSDouglas Gregor       const DirectoryEntry *UmbrellaDir = File->getDir();
140859527666SDouglas Gregor       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
1409322f633cSDouglas Gregor         Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
141059527666SDouglas Gregor           << UmbrellaModule->getFullModuleName();
1411322f633cSDouglas Gregor         HadError = true;
14125257fc63SDouglas Gregor       } else {
1413322f633cSDouglas Gregor         // Record this umbrella header.
1414322f633cSDouglas Gregor         Map.setUmbrellaHeader(ActiveModule, File);
1415322f633cSDouglas Gregor       }
1416322f633cSDouglas Gregor     } else {
1417322f633cSDouglas Gregor       // Record this header.
141859527666SDouglas Gregor       Map.addHeader(ActiveModule, File, Exclude);
14193ec6663bSDouglas Gregor 
14203ec6663bSDouglas Gregor       // If there is a builtin counterpart to this file, add it now.
14213ec6663bSDouglas Gregor       if (BuiltinFile)
142259527666SDouglas Gregor         Map.addHeader(ActiveModule, BuiltinFile, Exclude);
14235257fc63SDouglas Gregor     }
14244b27a64bSDouglas Gregor   } else if (!Exclude) {
14254b27a64bSDouglas Gregor     // Ignore excluded header files. They're optional anyway.
14264b27a64bSDouglas Gregor 
14275257fc63SDouglas Gregor     Diags.Report(FileNameLoc, diag::err_mmap_header_not_found)
1428524e33e1SDouglas Gregor       << Umbrella << FileName;
14295257fc63SDouglas Gregor     HadError = true;
14305257fc63SDouglas Gregor   }
1431718292f2SDouglas Gregor }
1432718292f2SDouglas Gregor 
1433524e33e1SDouglas Gregor /// \brief Parse an umbrella directory declaration.
1434524e33e1SDouglas Gregor ///
1435524e33e1SDouglas Gregor ///   umbrella-dir-declaration:
1436524e33e1SDouglas Gregor ///     umbrella string-literal
1437524e33e1SDouglas Gregor void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
1438524e33e1SDouglas Gregor   // Parse the directory name.
1439524e33e1SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1440524e33e1SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
1441524e33e1SDouglas Gregor       << "umbrella";
1442524e33e1SDouglas Gregor     HadError = true;
1443524e33e1SDouglas Gregor     return;
1444524e33e1SDouglas Gregor   }
1445524e33e1SDouglas Gregor 
1446524e33e1SDouglas Gregor   std::string DirName = Tok.getString();
1447524e33e1SDouglas Gregor   SourceLocation DirNameLoc = consumeToken();
1448524e33e1SDouglas Gregor 
1449524e33e1SDouglas Gregor   // Check whether we already have an umbrella.
1450524e33e1SDouglas Gregor   if (ActiveModule->Umbrella) {
1451524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
1452524e33e1SDouglas Gregor       << ActiveModule->getFullModuleName();
1453524e33e1SDouglas Gregor     HadError = true;
1454524e33e1SDouglas Gregor     return;
1455524e33e1SDouglas Gregor   }
1456524e33e1SDouglas Gregor 
1457524e33e1SDouglas Gregor   // Look for this file.
1458524e33e1SDouglas Gregor   const DirectoryEntry *Dir = 0;
1459524e33e1SDouglas Gregor   if (llvm::sys::path::is_absolute(DirName))
1460524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(DirName);
1461524e33e1SDouglas Gregor   else {
14622c1dd271SDylan Noblesmith     SmallString<128> PathName;
1463524e33e1SDouglas Gregor     PathName = Directory->getName();
1464524e33e1SDouglas Gregor     llvm::sys::path::append(PathName, DirName);
1465524e33e1SDouglas Gregor     Dir = SourceMgr.getFileManager().getDirectory(PathName);
1466524e33e1SDouglas Gregor   }
1467524e33e1SDouglas Gregor 
1468524e33e1SDouglas Gregor   if (!Dir) {
1469524e33e1SDouglas Gregor     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
1470524e33e1SDouglas Gregor       << DirName;
1471524e33e1SDouglas Gregor     HadError = true;
1472524e33e1SDouglas Gregor     return;
1473524e33e1SDouglas Gregor   }
1474524e33e1SDouglas Gregor 
1475524e33e1SDouglas Gregor   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
1476524e33e1SDouglas Gregor     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
1477524e33e1SDouglas Gregor       << OwningModule->getFullModuleName();
1478524e33e1SDouglas Gregor     HadError = true;
1479524e33e1SDouglas Gregor     return;
1480524e33e1SDouglas Gregor   }
1481524e33e1SDouglas Gregor 
1482524e33e1SDouglas Gregor   // Record this umbrella directory.
1483524e33e1SDouglas Gregor   Map.setUmbrellaDir(ActiveModule, Dir);
1484524e33e1SDouglas Gregor }
1485524e33e1SDouglas Gregor 
14862b82c2a5SDouglas Gregor /// \brief Parse a module export declaration.
14872b82c2a5SDouglas Gregor ///
14882b82c2a5SDouglas Gregor ///   export-declaration:
14892b82c2a5SDouglas Gregor ///     'export' wildcard-module-id
14902b82c2a5SDouglas Gregor ///
14912b82c2a5SDouglas Gregor ///   wildcard-module-id:
14922b82c2a5SDouglas Gregor ///     identifier
14932b82c2a5SDouglas Gregor ///     '*'
14942b82c2a5SDouglas Gregor ///     identifier '.' wildcard-module-id
14952b82c2a5SDouglas Gregor void ModuleMapParser::parseExportDecl() {
14962b82c2a5SDouglas Gregor   assert(Tok.is(MMToken::ExportKeyword));
14972b82c2a5SDouglas Gregor   SourceLocation ExportLoc = consumeToken();
14982b82c2a5SDouglas Gregor 
14992b82c2a5SDouglas Gregor   // Parse the module-id with an optional wildcard at the end.
15002b82c2a5SDouglas Gregor   ModuleId ParsedModuleId;
15012b82c2a5SDouglas Gregor   bool Wildcard = false;
15022b82c2a5SDouglas Gregor   do {
15032b82c2a5SDouglas Gregor     if (Tok.is(MMToken::Identifier)) {
15042b82c2a5SDouglas Gregor       ParsedModuleId.push_back(std::make_pair(Tok.getString(),
15052b82c2a5SDouglas Gregor                                               Tok.getLocation()));
15062b82c2a5SDouglas Gregor       consumeToken();
15072b82c2a5SDouglas Gregor 
15082b82c2a5SDouglas Gregor       if (Tok.is(MMToken::Period)) {
15092b82c2a5SDouglas Gregor         consumeToken();
15102b82c2a5SDouglas Gregor         continue;
15112b82c2a5SDouglas Gregor       }
15122b82c2a5SDouglas Gregor 
15132b82c2a5SDouglas Gregor       break;
15142b82c2a5SDouglas Gregor     }
15152b82c2a5SDouglas Gregor 
15162b82c2a5SDouglas Gregor     if(Tok.is(MMToken::Star)) {
15172b82c2a5SDouglas Gregor       Wildcard = true;
1518f5eedd05SDouglas Gregor       consumeToken();
15192b82c2a5SDouglas Gregor       break;
15202b82c2a5SDouglas Gregor     }
15212b82c2a5SDouglas Gregor 
15222b82c2a5SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_export_module_id);
15232b82c2a5SDouglas Gregor     HadError = true;
15242b82c2a5SDouglas Gregor     return;
15252b82c2a5SDouglas Gregor   } while (true);
15262b82c2a5SDouglas Gregor 
15272b82c2a5SDouglas Gregor   Module::UnresolvedExportDecl Unresolved = {
15282b82c2a5SDouglas Gregor     ExportLoc, ParsedModuleId, Wildcard
15292b82c2a5SDouglas Gregor   };
15302b82c2a5SDouglas Gregor   ActiveModule->UnresolvedExports.push_back(Unresolved);
15312b82c2a5SDouglas Gregor }
15322b82c2a5SDouglas Gregor 
15336ddfca91SDouglas Gregor /// \brief Parse a link declaration.
15346ddfca91SDouglas Gregor ///
15356ddfca91SDouglas Gregor ///   module-declaration:
15366ddfca91SDouglas Gregor ///     'link' 'framework'[opt] string-literal
15376ddfca91SDouglas Gregor void ModuleMapParser::parseLinkDecl() {
15386ddfca91SDouglas Gregor   assert(Tok.is(MMToken::LinkKeyword));
15396ddfca91SDouglas Gregor   SourceLocation LinkLoc = consumeToken();
15406ddfca91SDouglas Gregor 
15416ddfca91SDouglas Gregor   // Parse the optional 'framework' keyword.
15426ddfca91SDouglas Gregor   bool IsFramework = false;
15436ddfca91SDouglas Gregor   if (Tok.is(MMToken::FrameworkKeyword)) {
15446ddfca91SDouglas Gregor     consumeToken();
15456ddfca91SDouglas Gregor     IsFramework = true;
15466ddfca91SDouglas Gregor   }
15476ddfca91SDouglas Gregor 
15486ddfca91SDouglas Gregor   // Parse the library name
15496ddfca91SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
15506ddfca91SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
15516ddfca91SDouglas Gregor       << IsFramework << SourceRange(LinkLoc);
15526ddfca91SDouglas Gregor     HadError = true;
15536ddfca91SDouglas Gregor     return;
15546ddfca91SDouglas Gregor   }
15556ddfca91SDouglas Gregor 
15566ddfca91SDouglas Gregor   std::string LibraryName = Tok.getString();
15576ddfca91SDouglas Gregor   consumeToken();
15586ddfca91SDouglas Gregor   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
15596ddfca91SDouglas Gregor                                                             IsFramework));
15606ddfca91SDouglas Gregor }
15616ddfca91SDouglas Gregor 
156235b13eceSDouglas Gregor /// \brief Parse a configuration macro declaration.
156335b13eceSDouglas Gregor ///
156435b13eceSDouglas Gregor ///   module-declaration:
156535b13eceSDouglas Gregor ///     'config_macros' attributes[opt] config-macro-list?
156635b13eceSDouglas Gregor ///
156735b13eceSDouglas Gregor ///   config-macro-list:
156835b13eceSDouglas Gregor ///     identifier (',' identifier)?
156935b13eceSDouglas Gregor void ModuleMapParser::parseConfigMacros() {
157035b13eceSDouglas Gregor   assert(Tok.is(MMToken::ConfigMacros));
157135b13eceSDouglas Gregor   SourceLocation ConfigMacrosLoc = consumeToken();
157235b13eceSDouglas Gregor 
157335b13eceSDouglas Gregor   // Only top-level modules can have configuration macros.
157435b13eceSDouglas Gregor   if (ActiveModule->Parent) {
157535b13eceSDouglas Gregor     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
157635b13eceSDouglas Gregor   }
157735b13eceSDouglas Gregor 
157835b13eceSDouglas Gregor   // Parse the optional attributes.
157935b13eceSDouglas Gregor   Attributes Attrs;
158035b13eceSDouglas Gregor   parseOptionalAttributes(Attrs);
158135b13eceSDouglas Gregor   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
158235b13eceSDouglas Gregor     ActiveModule->ConfigMacrosExhaustive = true;
158335b13eceSDouglas Gregor   }
158435b13eceSDouglas Gregor 
158535b13eceSDouglas Gregor   // If we don't have an identifier, we're done.
158635b13eceSDouglas Gregor   if (!Tok.is(MMToken::Identifier))
158735b13eceSDouglas Gregor     return;
158835b13eceSDouglas Gregor 
158935b13eceSDouglas Gregor   // Consume the first identifier.
159035b13eceSDouglas Gregor   if (!ActiveModule->Parent) {
159135b13eceSDouglas Gregor     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
159235b13eceSDouglas Gregor   }
159335b13eceSDouglas Gregor   consumeToken();
159435b13eceSDouglas Gregor 
159535b13eceSDouglas Gregor   do {
159635b13eceSDouglas Gregor     // If there's a comma, consume it.
159735b13eceSDouglas Gregor     if (!Tok.is(MMToken::Comma))
159835b13eceSDouglas Gregor       break;
159935b13eceSDouglas Gregor     consumeToken();
160035b13eceSDouglas Gregor 
160135b13eceSDouglas Gregor     // We expect to see a macro name here.
160235b13eceSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
160335b13eceSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
160435b13eceSDouglas Gregor       break;
160535b13eceSDouglas Gregor     }
160635b13eceSDouglas Gregor 
160735b13eceSDouglas Gregor     // Consume the macro name.
160835b13eceSDouglas Gregor     if (!ActiveModule->Parent) {
160935b13eceSDouglas Gregor       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
161035b13eceSDouglas Gregor     }
161135b13eceSDouglas Gregor     consumeToken();
161235b13eceSDouglas Gregor   } while (true);
161335b13eceSDouglas Gregor }
161435b13eceSDouglas Gregor 
1615fb912657SDouglas Gregor /// \brief Format a module-id into a string.
1616fb912657SDouglas Gregor static std::string formatModuleId(const ModuleId &Id) {
1617fb912657SDouglas Gregor   std::string result;
1618fb912657SDouglas Gregor   {
1619fb912657SDouglas Gregor     llvm::raw_string_ostream OS(result);
1620fb912657SDouglas Gregor 
1621fb912657SDouglas Gregor     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
1622fb912657SDouglas Gregor       if (I)
1623fb912657SDouglas Gregor         OS << ".";
1624fb912657SDouglas Gregor       OS << Id[I].first;
1625fb912657SDouglas Gregor     }
1626fb912657SDouglas Gregor   }
1627fb912657SDouglas Gregor 
1628fb912657SDouglas Gregor   return result;
1629fb912657SDouglas Gregor }
1630fb912657SDouglas Gregor 
1631fb912657SDouglas Gregor /// \brief Parse a conflict declaration.
1632fb912657SDouglas Gregor ///
1633fb912657SDouglas Gregor ///   module-declaration:
1634fb912657SDouglas Gregor ///     'conflict' module-id ',' string-literal
1635fb912657SDouglas Gregor void ModuleMapParser::parseConflict() {
1636fb912657SDouglas Gregor   assert(Tok.is(MMToken::Conflict));
1637fb912657SDouglas Gregor   SourceLocation ConflictLoc = consumeToken();
1638fb912657SDouglas Gregor   Module::UnresolvedConflict Conflict;
1639fb912657SDouglas Gregor 
1640fb912657SDouglas Gregor   // Parse the module-id.
1641fb912657SDouglas Gregor   if (parseModuleId(Conflict.Id))
1642fb912657SDouglas Gregor     return;
1643fb912657SDouglas Gregor 
1644fb912657SDouglas Gregor   // Parse the ','.
1645fb912657SDouglas Gregor   if (!Tok.is(MMToken::Comma)) {
1646fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
1647fb912657SDouglas Gregor       << SourceRange(ConflictLoc);
1648fb912657SDouglas Gregor     return;
1649fb912657SDouglas Gregor   }
1650fb912657SDouglas Gregor   consumeToken();
1651fb912657SDouglas Gregor 
1652fb912657SDouglas Gregor   // Parse the message.
1653fb912657SDouglas Gregor   if (!Tok.is(MMToken::StringLiteral)) {
1654fb912657SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
1655fb912657SDouglas Gregor       << formatModuleId(Conflict.Id);
1656fb912657SDouglas Gregor     return;
1657fb912657SDouglas Gregor   }
1658fb912657SDouglas Gregor   Conflict.Message = Tok.getString().str();
1659fb912657SDouglas Gregor   consumeToken();
1660fb912657SDouglas Gregor 
1661fb912657SDouglas Gregor   // Add this unresolved conflict.
1662fb912657SDouglas Gregor   ActiveModule->UnresolvedConflicts.push_back(Conflict);
1663fb912657SDouglas Gregor }
1664fb912657SDouglas Gregor 
16656ddfca91SDouglas Gregor /// \brief Parse an inferred module declaration (wildcard modules).
16669194a91dSDouglas Gregor ///
16679194a91dSDouglas Gregor ///   module-declaration:
16689194a91dSDouglas Gregor ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
16699194a91dSDouglas Gregor ///       { inferred-module-member* }
16709194a91dSDouglas Gregor ///
16719194a91dSDouglas Gregor ///   inferred-module-member:
16729194a91dSDouglas Gregor ///     'export' '*'
16739194a91dSDouglas Gregor ///     'exclude' identifier
16749194a91dSDouglas Gregor void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
167573441091SDouglas Gregor   assert(Tok.is(MMToken::Star));
167673441091SDouglas Gregor   SourceLocation StarLoc = consumeToken();
167773441091SDouglas Gregor   bool Failed = false;
167873441091SDouglas Gregor 
167973441091SDouglas Gregor   // Inferred modules must be submodules.
16809194a91dSDouglas Gregor   if (!ActiveModule && !Framework) {
168173441091SDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
168273441091SDouglas Gregor     Failed = true;
168373441091SDouglas Gregor   }
168473441091SDouglas Gregor 
16859194a91dSDouglas Gregor   if (ActiveModule) {
1686524e33e1SDouglas Gregor     // Inferred modules must have umbrella directories.
1687524e33e1SDouglas Gregor     if (!Failed && !ActiveModule->getUmbrellaDir()) {
168873441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
168973441091SDouglas Gregor       Failed = true;
169073441091SDouglas Gregor     }
169173441091SDouglas Gregor 
169273441091SDouglas Gregor     // Check for redefinition of an inferred module.
1693dd005f69SDouglas Gregor     if (!Failed && ActiveModule->InferSubmodules) {
169473441091SDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
1695dd005f69SDouglas Gregor       if (ActiveModule->InferredSubmoduleLoc.isValid())
1696dd005f69SDouglas Gregor         Diags.Report(ActiveModule->InferredSubmoduleLoc,
169773441091SDouglas Gregor                      diag::note_mmap_prev_definition);
169873441091SDouglas Gregor       Failed = true;
169973441091SDouglas Gregor     }
170073441091SDouglas Gregor 
17019194a91dSDouglas Gregor     // Check for the 'framework' keyword, which is not permitted here.
17029194a91dSDouglas Gregor     if (Framework) {
17039194a91dSDouglas Gregor       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
17049194a91dSDouglas Gregor       Framework = false;
17059194a91dSDouglas Gregor     }
17069194a91dSDouglas Gregor   } else if (Explicit) {
17079194a91dSDouglas Gregor     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
17089194a91dSDouglas Gregor     Explicit = false;
17099194a91dSDouglas Gregor   }
17109194a91dSDouglas Gregor 
171173441091SDouglas Gregor   // If there were any problems with this inferred submodule, skip its body.
171273441091SDouglas Gregor   if (Failed) {
171373441091SDouglas Gregor     if (Tok.is(MMToken::LBrace)) {
171473441091SDouglas Gregor       consumeToken();
171573441091SDouglas Gregor       skipUntil(MMToken::RBrace);
171673441091SDouglas Gregor       if (Tok.is(MMToken::RBrace))
171773441091SDouglas Gregor         consumeToken();
171873441091SDouglas Gregor     }
171973441091SDouglas Gregor     HadError = true;
172073441091SDouglas Gregor     return;
172173441091SDouglas Gregor   }
172273441091SDouglas Gregor 
17239194a91dSDouglas Gregor   // Parse optional attributes.
17244442605fSBill Wendling   Attributes Attrs;
17259194a91dSDouglas Gregor   parseOptionalAttributes(Attrs);
17269194a91dSDouglas Gregor 
17279194a91dSDouglas Gregor   if (ActiveModule) {
172873441091SDouglas Gregor     // Note that we have an inferred submodule.
1729dd005f69SDouglas Gregor     ActiveModule->InferSubmodules = true;
1730dd005f69SDouglas Gregor     ActiveModule->InferredSubmoduleLoc = StarLoc;
1731dd005f69SDouglas Gregor     ActiveModule->InferExplicitSubmodules = Explicit;
17329194a91dSDouglas Gregor   } else {
17339194a91dSDouglas Gregor     // We'll be inferring framework modules for this directory.
17349194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferModules = true;
17359194a91dSDouglas Gregor     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
17369194a91dSDouglas Gregor   }
173773441091SDouglas Gregor 
173873441091SDouglas Gregor   // Parse the opening brace.
173973441091SDouglas Gregor   if (!Tok.is(MMToken::LBrace)) {
174073441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
174173441091SDouglas Gregor     HadError = true;
174273441091SDouglas Gregor     return;
174373441091SDouglas Gregor   }
174473441091SDouglas Gregor   SourceLocation LBraceLoc = consumeToken();
174573441091SDouglas Gregor 
174673441091SDouglas Gregor   // Parse the body of the inferred submodule.
174773441091SDouglas Gregor   bool Done = false;
174873441091SDouglas Gregor   do {
174973441091SDouglas Gregor     switch (Tok.Kind) {
175073441091SDouglas Gregor     case MMToken::EndOfFile:
175173441091SDouglas Gregor     case MMToken::RBrace:
175273441091SDouglas Gregor       Done = true;
175373441091SDouglas Gregor       break;
175473441091SDouglas Gregor 
17559194a91dSDouglas Gregor     case MMToken::ExcludeKeyword: {
17569194a91dSDouglas Gregor       if (ActiveModule) {
17579194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1758162405daSDouglas Gregor           << (ActiveModule != 0);
17599194a91dSDouglas Gregor         consumeToken();
17609194a91dSDouglas Gregor         break;
17619194a91dSDouglas Gregor       }
17629194a91dSDouglas Gregor 
17639194a91dSDouglas Gregor       consumeToken();
17649194a91dSDouglas Gregor       if (!Tok.is(MMToken::Identifier)) {
17659194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
17669194a91dSDouglas Gregor         break;
17679194a91dSDouglas Gregor       }
17689194a91dSDouglas Gregor 
17699194a91dSDouglas Gregor       Map.InferredDirectories[Directory].ExcludedModules
17709194a91dSDouglas Gregor         .push_back(Tok.getString());
17719194a91dSDouglas Gregor       consumeToken();
17729194a91dSDouglas Gregor       break;
17739194a91dSDouglas Gregor     }
17749194a91dSDouglas Gregor 
17759194a91dSDouglas Gregor     case MMToken::ExportKeyword:
17769194a91dSDouglas Gregor       if (!ActiveModule) {
17779194a91dSDouglas Gregor         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1778162405daSDouglas Gregor           << (ActiveModule != 0);
17799194a91dSDouglas Gregor         consumeToken();
17809194a91dSDouglas Gregor         break;
17819194a91dSDouglas Gregor       }
17829194a91dSDouglas Gregor 
178373441091SDouglas Gregor       consumeToken();
178473441091SDouglas Gregor       if (Tok.is(MMToken::Star))
1785dd005f69SDouglas Gregor         ActiveModule->InferExportWildcard = true;
178673441091SDouglas Gregor       else
178773441091SDouglas Gregor         Diags.Report(Tok.getLocation(),
178873441091SDouglas Gregor                      diag::err_mmap_expected_export_wildcard);
178973441091SDouglas Gregor       consumeToken();
179073441091SDouglas Gregor       break;
179173441091SDouglas Gregor 
179273441091SDouglas Gregor     case MMToken::ExplicitKeyword:
179373441091SDouglas Gregor     case MMToken::ModuleKeyword:
179473441091SDouglas Gregor     case MMToken::HeaderKeyword:
179573441091SDouglas Gregor     case MMToken::UmbrellaKeyword:
179673441091SDouglas Gregor     default:
17979194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
1798162405daSDouglas Gregor           << (ActiveModule != 0);
179973441091SDouglas Gregor       consumeToken();
180073441091SDouglas Gregor       break;
180173441091SDouglas Gregor     }
180273441091SDouglas Gregor   } while (!Done);
180373441091SDouglas Gregor 
180473441091SDouglas Gregor   if (Tok.is(MMToken::RBrace))
180573441091SDouglas Gregor     consumeToken();
180673441091SDouglas Gregor   else {
180773441091SDouglas Gregor     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
180873441091SDouglas Gregor     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
180973441091SDouglas Gregor     HadError = true;
181073441091SDouglas Gregor   }
181173441091SDouglas Gregor }
181273441091SDouglas Gregor 
18139194a91dSDouglas Gregor /// \brief Parse optional attributes.
18149194a91dSDouglas Gregor ///
18159194a91dSDouglas Gregor ///   attributes:
18169194a91dSDouglas Gregor ///     attribute attributes
18179194a91dSDouglas Gregor ///     attribute
18189194a91dSDouglas Gregor ///
18199194a91dSDouglas Gregor ///   attribute:
18209194a91dSDouglas Gregor ///     [ identifier ]
18219194a91dSDouglas Gregor ///
18229194a91dSDouglas Gregor /// \param Attrs Will be filled in with the parsed attributes.
18239194a91dSDouglas Gregor ///
18249194a91dSDouglas Gregor /// \returns true if an error occurred, false otherwise.
18254442605fSBill Wendling bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
18269194a91dSDouglas Gregor   bool HadError = false;
18279194a91dSDouglas Gregor 
18289194a91dSDouglas Gregor   while (Tok.is(MMToken::LSquare)) {
18299194a91dSDouglas Gregor     // Consume the '['.
18309194a91dSDouglas Gregor     SourceLocation LSquareLoc = consumeToken();
18319194a91dSDouglas Gregor 
18329194a91dSDouglas Gregor     // Check whether we have an attribute name here.
18339194a91dSDouglas Gregor     if (!Tok.is(MMToken::Identifier)) {
18349194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
18359194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
18369194a91dSDouglas Gregor       if (Tok.is(MMToken::RSquare))
18379194a91dSDouglas Gregor         consumeToken();
18389194a91dSDouglas Gregor       HadError = true;
18399194a91dSDouglas Gregor     }
18409194a91dSDouglas Gregor 
18419194a91dSDouglas Gregor     // Decode the attribute name.
18429194a91dSDouglas Gregor     AttributeKind Attribute
18439194a91dSDouglas Gregor       = llvm::StringSwitch<AttributeKind>(Tok.getString())
184435b13eceSDouglas Gregor           .Case("exhaustive", AT_exhaustive)
18459194a91dSDouglas Gregor           .Case("system", AT_system)
18469194a91dSDouglas Gregor           .Default(AT_unknown);
18479194a91dSDouglas Gregor     switch (Attribute) {
18489194a91dSDouglas Gregor     case AT_unknown:
18499194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
18509194a91dSDouglas Gregor         << Tok.getString();
18519194a91dSDouglas Gregor       break;
18529194a91dSDouglas Gregor 
18539194a91dSDouglas Gregor     case AT_system:
18549194a91dSDouglas Gregor       Attrs.IsSystem = true;
18559194a91dSDouglas Gregor       break;
185635b13eceSDouglas Gregor 
185735b13eceSDouglas Gregor     case AT_exhaustive:
185835b13eceSDouglas Gregor       Attrs.IsExhaustive = true;
185935b13eceSDouglas Gregor       break;
18609194a91dSDouglas Gregor     }
18619194a91dSDouglas Gregor     consumeToken();
18629194a91dSDouglas Gregor 
18639194a91dSDouglas Gregor     // Consume the ']'.
18649194a91dSDouglas Gregor     if (!Tok.is(MMToken::RSquare)) {
18659194a91dSDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
18669194a91dSDouglas Gregor       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
18679194a91dSDouglas Gregor       skipUntil(MMToken::RSquare);
18689194a91dSDouglas Gregor       HadError = true;
18699194a91dSDouglas Gregor     }
18709194a91dSDouglas Gregor 
18719194a91dSDouglas Gregor     if (Tok.is(MMToken::RSquare))
18729194a91dSDouglas Gregor       consumeToken();
18739194a91dSDouglas Gregor   }
18749194a91dSDouglas Gregor 
18759194a91dSDouglas Gregor   return HadError;
18769194a91dSDouglas Gregor }
18779194a91dSDouglas Gregor 
18787033127bSDouglas Gregor /// \brief If there is a specific header search directory due the presence
18797033127bSDouglas Gregor /// of an umbrella directory, retrieve that directory. Otherwise, returns null.
18807033127bSDouglas Gregor const DirectoryEntry *ModuleMapParser::getOverriddenHeaderSearchDir() {
18817033127bSDouglas Gregor   for (Module *Mod = ActiveModule; Mod; Mod = Mod->Parent) {
18827033127bSDouglas Gregor     // If we have an umbrella directory, use that.
18837033127bSDouglas Gregor     if (Mod->hasUmbrellaDir())
18847033127bSDouglas Gregor       return Mod->getUmbrellaDir();
18857033127bSDouglas Gregor 
18867033127bSDouglas Gregor     // If we have a framework directory, stop looking.
18877033127bSDouglas Gregor     if (Mod->IsFramework)
18887033127bSDouglas Gregor       return 0;
18897033127bSDouglas Gregor   }
18907033127bSDouglas Gregor 
18917033127bSDouglas Gregor   return 0;
18927033127bSDouglas Gregor }
18937033127bSDouglas Gregor 
1894718292f2SDouglas Gregor /// \brief Parse a module map file.
1895718292f2SDouglas Gregor ///
1896718292f2SDouglas Gregor ///   module-map-file:
1897718292f2SDouglas Gregor ///     module-declaration*
1898718292f2SDouglas Gregor bool ModuleMapParser::parseModuleMapFile() {
1899718292f2SDouglas Gregor   do {
1900718292f2SDouglas Gregor     switch (Tok.Kind) {
1901718292f2SDouglas Gregor     case MMToken::EndOfFile:
1902718292f2SDouglas Gregor       return HadError;
1903718292f2SDouglas Gregor 
1904e7ab3669SDouglas Gregor     case MMToken::ExplicitKeyword:
1905718292f2SDouglas Gregor     case MMToken::ModuleKeyword:
1906755b2055SDouglas Gregor     case MMToken::FrameworkKeyword:
1907718292f2SDouglas Gregor       parseModuleDecl();
1908718292f2SDouglas Gregor       break;
1909718292f2SDouglas Gregor 
19101fb5c3a6SDouglas Gregor     case MMToken::Comma:
191135b13eceSDouglas Gregor     case MMToken::ConfigMacros:
1912fb912657SDouglas Gregor     case MMToken::Conflict:
191359527666SDouglas Gregor     case MMToken::ExcludeKeyword:
19142b82c2a5SDouglas Gregor     case MMToken::ExportKeyword:
1915718292f2SDouglas Gregor     case MMToken::HeaderKeyword:
1916718292f2SDouglas Gregor     case MMToken::Identifier:
1917718292f2SDouglas Gregor     case MMToken::LBrace:
19186ddfca91SDouglas Gregor     case MMToken::LinkKeyword:
1919a686e1b0SDouglas Gregor     case MMToken::LSquare:
19202b82c2a5SDouglas Gregor     case MMToken::Period:
1921718292f2SDouglas Gregor     case MMToken::RBrace:
1922a686e1b0SDouglas Gregor     case MMToken::RSquare:
19231fb5c3a6SDouglas Gregor     case MMToken::RequiresKeyword:
19242b82c2a5SDouglas Gregor     case MMToken::Star:
1925718292f2SDouglas Gregor     case MMToken::StringLiteral:
1926718292f2SDouglas Gregor     case MMToken::UmbrellaKeyword:
1927718292f2SDouglas Gregor       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
1928718292f2SDouglas Gregor       HadError = true;
1929718292f2SDouglas Gregor       consumeToken();
1930718292f2SDouglas Gregor       break;
1931718292f2SDouglas Gregor     }
1932718292f2SDouglas Gregor   } while (true);
1933718292f2SDouglas Gregor }
1934718292f2SDouglas Gregor 
1935718292f2SDouglas Gregor bool ModuleMap::parseModuleMapFile(const FileEntry *File) {
19364ddf2221SDouglas Gregor   llvm::DenseMap<const FileEntry *, bool>::iterator Known
19374ddf2221SDouglas Gregor     = ParsedModuleMap.find(File);
19384ddf2221SDouglas Gregor   if (Known != ParsedModuleMap.end())
19394ddf2221SDouglas Gregor     return Known->second;
19404ddf2221SDouglas Gregor 
194189929282SDouglas Gregor   assert(Target != 0 && "Missing target information");
1942718292f2SDouglas Gregor   FileID ID = SourceMgr->createFileID(File, SourceLocation(), SrcMgr::C_User);
1943718292f2SDouglas Gregor   const llvm::MemoryBuffer *Buffer = SourceMgr->getBuffer(ID);
1944718292f2SDouglas Gregor   if (!Buffer)
19454ddf2221SDouglas Gregor     return ParsedModuleMap[File] = true;
1946718292f2SDouglas Gregor 
1947718292f2SDouglas Gregor   // Parse this module map file.
19481fb5c3a6SDouglas Gregor   Lexer L(ID, SourceMgr->getBuffer(ID), *SourceMgr, MMapLangOpts);
19491fb5c3a6SDouglas Gregor   Diags->getClient()->BeginSourceFile(MMapLangOpts);
1950bc10b9fbSDouglas Gregor   ModuleMapParser Parser(L, *SourceMgr, Target, *Diags, *this, File->getDir(),
19513ec6663bSDouglas Gregor                          BuiltinIncludeDir);
1952718292f2SDouglas Gregor   bool Result = Parser.parseModuleMapFile();
1953718292f2SDouglas Gregor   Diags->getClient()->EndSourceFile();
19544ddf2221SDouglas Gregor   ParsedModuleMap[File] = Result;
1955718292f2SDouglas Gregor   return Result;
1956718292f2SDouglas Gregor }
1957