clang API Documentation

ModuleMap.cpp
Go to the documentation of this file.
00001 //===--- ModuleMap.cpp - Describe the layout of modules ---------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the ModuleMap implementation, which describes the layout
00011 // of a module as it relates to headers.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #include "clang/Lex/ModuleMap.h"
00015 #include "clang/Basic/CharInfo.h"
00016 #include "clang/Basic/Diagnostic.h"
00017 #include "clang/Basic/DiagnosticOptions.h"
00018 #include "clang/Basic/FileManager.h"
00019 #include "clang/Basic/TargetInfo.h"
00020 #include "clang/Basic/TargetOptions.h"
00021 #include "clang/Lex/HeaderSearch.h"
00022 #include "clang/Lex/LexDiagnostic.h"
00023 #include "clang/Lex/Lexer.h"
00024 #include "clang/Lex/LiteralSupport.h"
00025 #include "llvm/ADT/StringRef.h"
00026 #include "llvm/ADT/StringSwitch.h"
00027 #include "llvm/Support/Allocator.h"
00028 #include "llvm/Support/FileSystem.h"
00029 #include "llvm/Support/Host.h"
00030 #include "llvm/Support/Path.h"
00031 #include "llvm/Support/raw_ostream.h"
00032 #include <stdlib.h>
00033 #if defined(LLVM_ON_UNIX)
00034 #include <limits.h>
00035 #endif
00036 using namespace clang;
00037 
00038 Module::ExportDecl 
00039 ModuleMap::resolveExport(Module *Mod, 
00040                          const Module::UnresolvedExportDecl &Unresolved,
00041                          bool Complain) const {
00042   // We may have just a wildcard.
00043   if (Unresolved.Id.empty()) {
00044     assert(Unresolved.Wildcard && "Invalid unresolved export");
00045     return Module::ExportDecl(nullptr, true);
00046   }
00047   
00048   // Resolve the module-id.
00049   Module *Context = resolveModuleId(Unresolved.Id, Mod, Complain);
00050   if (!Context)
00051     return Module::ExportDecl();
00052 
00053   return Module::ExportDecl(Context, Unresolved.Wildcard);
00054 }
00055 
00056 Module *ModuleMap::resolveModuleId(const ModuleId &Id, Module *Mod,
00057                                    bool Complain) const {
00058   // Find the starting module.
00059   Module *Context = lookupModuleUnqualified(Id[0].first, Mod);
00060   if (!Context) {
00061     if (Complain)
00062       Diags.Report(Id[0].second, diag::err_mmap_missing_module_unqualified)
00063       << Id[0].first << Mod->getFullModuleName();
00064 
00065     return nullptr;
00066   }
00067 
00068   // Dig into the module path.
00069   for (unsigned I = 1, N = Id.size(); I != N; ++I) {
00070     Module *Sub = lookupModuleQualified(Id[I].first, Context);
00071     if (!Sub) {
00072       if (Complain)
00073         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
00074         << Id[I].first << Context->getFullModuleName()
00075         << SourceRange(Id[0].second, Id[I-1].second);
00076 
00077       return nullptr;
00078     }
00079 
00080     Context = Sub;
00081   }
00082 
00083   return Context;
00084 }
00085 
00086 ModuleMap::ModuleMap(SourceManager &SourceMgr, DiagnosticsEngine &Diags,
00087                      const LangOptions &LangOpts, const TargetInfo *Target,
00088                      HeaderSearch &HeaderInfo)
00089     : SourceMgr(SourceMgr), Diags(Diags), LangOpts(LangOpts), Target(Target),
00090       HeaderInfo(HeaderInfo), BuiltinIncludeDir(nullptr),
00091       CompilingModule(nullptr), SourceModule(nullptr) {}
00092 
00093 ModuleMap::~ModuleMap() {
00094   for (llvm::StringMap<Module *>::iterator I = Modules.begin(), 
00095                                         IEnd = Modules.end();
00096        I != IEnd; ++I) {
00097     delete I->getValue();
00098   }
00099 }
00100 
00101 void ModuleMap::setTarget(const TargetInfo &Target) {
00102   assert((!this->Target || this->Target == &Target) && 
00103          "Improper target override");
00104   this->Target = &Target;
00105 }
00106 
00107 /// \brief "Sanitize" a filename so that it can be used as an identifier.
00108 static StringRef sanitizeFilenameAsIdentifier(StringRef Name,
00109                                               SmallVectorImpl<char> &Buffer) {
00110   if (Name.empty())
00111     return Name;
00112 
00113   if (!isValidIdentifier(Name)) {
00114     // If we don't already have something with the form of an identifier,
00115     // create a buffer with the sanitized name.
00116     Buffer.clear();
00117     if (isDigit(Name[0]))
00118       Buffer.push_back('_');
00119     Buffer.reserve(Buffer.size() + Name.size());
00120     for (unsigned I = 0, N = Name.size(); I != N; ++I) {
00121       if (isIdentifierBody(Name[I]))
00122         Buffer.push_back(Name[I]);
00123       else
00124         Buffer.push_back('_');
00125     }
00126 
00127     Name = StringRef(Buffer.data(), Buffer.size());
00128   }
00129 
00130   while (llvm::StringSwitch<bool>(Name)
00131 #define KEYWORD(Keyword,Conditions) .Case(#Keyword, true)
00132 #define ALIAS(Keyword, AliasOf, Conditions) .Case(Keyword, true)
00133 #include "clang/Basic/TokenKinds.def"
00134            .Default(false)) {
00135     if (Name.data() != Buffer.data())
00136       Buffer.append(Name.begin(), Name.end());
00137     Buffer.push_back('_');
00138     Name = StringRef(Buffer.data(), Buffer.size());
00139   }
00140 
00141   return Name;
00142 }
00143 
00144 /// \brief Determine whether the given file name is the name of a builtin
00145 /// header, supplied by Clang to replace, override, or augment existing system
00146 /// headers.
00147 static bool isBuiltinHeader(StringRef FileName) {
00148   return llvm::StringSwitch<bool>(FileName)
00149            .Case("float.h", true)
00150            .Case("iso646.h", true)
00151            .Case("limits.h", true)
00152            .Case("stdalign.h", true)
00153            .Case("stdarg.h", true)
00154            .Case("stdbool.h", true)
00155            .Case("stddef.h", true)
00156            .Case("stdint.h", true)
00157            .Case("tgmath.h", true)
00158            .Case("unwind.h", true)
00159            .Default(false);
00160 }
00161 
00162 ModuleMap::HeadersMap::iterator
00163 ModuleMap::findKnownHeader(const FileEntry *File) {
00164   HeadersMap::iterator Known = Headers.find(File);
00165   if (Known == Headers.end() && File->getDir() == BuiltinIncludeDir &&
00166       isBuiltinHeader(llvm::sys::path::filename(File->getName()))) {
00167     HeaderInfo.loadTopLevelSystemModules();
00168     return Headers.find(File);
00169   }
00170   return Known;
00171 }
00172 
00173 ModuleMap::KnownHeader
00174 ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
00175                     SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
00176   const DirectoryEntry *Dir = File->getDir();
00177   assert(Dir && "file in no directory");
00178 
00179   // Note: as an egregious but useful hack we use the real path here, because
00180   // frameworks moving from top-level frameworks to embedded frameworks tend
00181   // to be symlinked from the top-level location to the embedded location,
00182   // and we need to resolve lookups as if we had found the embedded location.
00183   StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
00184 
00185   // Keep walking up the directory hierarchy, looking for a directory with
00186   // an umbrella header.
00187   do {
00188     auto KnownDir = UmbrellaDirs.find(Dir);
00189     if (KnownDir != UmbrellaDirs.end())
00190       return KnownHeader(KnownDir->second, NormalHeader);
00191 
00192     IntermediateDirs.push_back(Dir);
00193 
00194     // Retrieve our parent path.
00195     DirName = llvm::sys::path::parent_path(DirName);
00196     if (DirName.empty())
00197       break;
00198 
00199     // Resolve the parent path to a directory entry.
00200     Dir = SourceMgr.getFileManager().getDirectory(DirName);
00201   } while (Dir);
00202   return KnownHeader();
00203 }
00204 
00205 // Returns true if RequestingModule directly uses RequestedModule.
00206 static bool directlyUses(const Module *RequestingModule,
00207                          const Module *RequestedModule) {
00208   return std::find(RequestingModule->DirectUses.begin(),
00209                    RequestingModule->DirectUses.end(),
00210                    RequestedModule) != RequestingModule->DirectUses.end();
00211 }
00212 
00213 static bool violatesPrivateInclude(Module *RequestingModule,
00214                                    const FileEntry *IncFileEnt,
00215                                    ModuleMap::ModuleHeaderRole Role,
00216                                    Module *RequestedModule) {
00217   bool IsPrivateRole = Role & ModuleMap::PrivateHeader;
00218 #ifndef NDEBUG
00219   // Check for consistency between the module header role
00220   // as obtained from the lookup and as obtained from the module.
00221   // This check is not cheap, so enable it only for debugging.
00222   bool IsPrivate = false;
00223   SmallVectorImpl<const FileEntry *> *HeaderList[] =
00224       {&RequestedModule->PrivateHeaders,
00225        &RequestedModule->PrivateTextualHeaders};
00226   for (auto *Hdrs : HeaderList)
00227     IsPrivate |=
00228         std::find(Hdrs->begin(), Hdrs->end(), IncFileEnt) != Hdrs->end();
00229   assert(IsPrivate == IsPrivateRole && "inconsistent headers and roles");
00230 #endif
00231   return IsPrivateRole &&
00232          RequestedModule->getTopLevelModule() != RequestingModule;
00233 }
00234 
00235 static Module *getTopLevelOrNull(Module *M) {
00236   return M ? M->getTopLevelModule() : nullptr;
00237 }
00238 
00239 void ModuleMap::diagnoseHeaderInclusion(Module *RequestingModule,
00240                                         SourceLocation FilenameLoc,
00241                                         StringRef Filename,
00242                                         const FileEntry *File) {
00243   // No errors for indirect modules. This may be a bit of a problem for modules
00244   // with no source files.
00245   if (getTopLevelOrNull(RequestingModule) != getTopLevelOrNull(SourceModule))
00246     return;
00247 
00248   if (RequestingModule)
00249     resolveUses(RequestingModule, /*Complain=*/false);
00250 
00251   bool Excluded = false;
00252   Module *Private = nullptr;
00253   Module *NotUsed = nullptr;
00254 
00255   HeadersMap::iterator Known = findKnownHeader(File);
00256   if (Known != Headers.end()) {
00257     for (const KnownHeader &Header : Known->second) {
00258       // If 'File' is part of 'RequestingModule' we can definitely include it.
00259       if (Header.getModule() == RequestingModule)
00260         return;
00261 
00262       // Remember private headers for later printing of a diagnostic.
00263       if (violatesPrivateInclude(RequestingModule, File, Header.getRole(),
00264                                  Header.getModule())) {
00265         Private = Header.getModule();
00266         continue;
00267       }
00268 
00269       // If uses need to be specified explicitly, we are only allowed to return
00270       // modules that are explicitly used by the requesting module.
00271       if (RequestingModule && LangOpts.ModulesDeclUse &&
00272           !directlyUses(RequestingModule, Header.getModule())) {
00273         NotUsed = Header.getModule();
00274         continue;
00275       }
00276 
00277       // We have found a module that we can happily use.
00278       return;
00279     }
00280 
00281     Excluded = true;
00282   }
00283 
00284   // We have found a header, but it is private.
00285   if (Private) {
00286     Diags.Report(FilenameLoc, diag::error_use_of_private_header_outside_module)
00287         << Filename;
00288     return;
00289   }
00290 
00291   // We have found a module, but we don't use it.
00292   if (NotUsed) {
00293     Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module)
00294         << RequestingModule->getFullModuleName() << Filename;
00295     return;
00296   }
00297 
00298   if (Excluded || isHeaderInUmbrellaDirs(File))
00299     return;
00300 
00301   // At this point, only non-modular includes remain.
00302 
00303   if (LangOpts.ModulesStrictDeclUse) {
00304     Diags.Report(FilenameLoc, diag::error_undeclared_use_of_module)
00305         << RequestingModule->getFullModuleName() << Filename;
00306   } else if (RequestingModule) {
00307     diag::kind DiagID = RequestingModule->getTopLevelModule()->IsFramework ?
00308         diag::warn_non_modular_include_in_framework_module :
00309         diag::warn_non_modular_include_in_module;
00310     Diags.Report(FilenameLoc, DiagID) << RequestingModule->getFullModuleName();
00311   }
00312 }
00313 
00314 ModuleMap::KnownHeader
00315 ModuleMap::findModuleForHeader(const FileEntry *File,
00316                                Module *RequestingModule,
00317                                bool IncludeTextualHeaders) {
00318   HeadersMap::iterator Known = findKnownHeader(File);
00319 
00320   auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
00321     if (!IncludeTextualHeaders && (R.getRole() & ModuleMap::TextualHeader))
00322       return ModuleMap::KnownHeader();
00323     return R;
00324   };
00325 
00326   if (Known != Headers.end()) {
00327     ModuleMap::KnownHeader Result;
00328 
00329     // Iterate over all modules that 'File' is part of to find the best fit.
00330     for (SmallVectorImpl<KnownHeader>::iterator I = Known->second.begin(),
00331                                                 E = Known->second.end();
00332          I != E; ++I) {
00333       // Cannot use a module if it is unavailable.
00334       if (!I->getModule()->isAvailable())
00335         continue;
00336 
00337       // If 'File' is part of 'RequestingModule', 'RequestingModule' is the
00338       // module we are looking for.
00339       if (I->getModule() == RequestingModule)
00340         return MakeResult(*I);
00341 
00342       // If uses need to be specified explicitly, we are only allowed to return
00343       // modules that are explicitly used by the requesting module.
00344       if (RequestingModule && LangOpts.ModulesDeclUse &&
00345           !directlyUses(RequestingModule, I->getModule()))
00346         continue;
00347 
00348       // Prefer a public header over a private header.
00349       if (!Result || (Result.getRole() & ModuleMap::PrivateHeader))
00350         Result = *I;
00351     }
00352     return MakeResult(Result);
00353   }
00354 
00355   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
00356   KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
00357   if (H) {
00358     Module *Result = H.getModule();
00359 
00360     // Search up the module stack until we find a module with an umbrella
00361     // directory.
00362     Module *UmbrellaModule = Result;
00363     while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
00364       UmbrellaModule = UmbrellaModule->Parent;
00365 
00366     if (UmbrellaModule->InferSubmodules) {
00367       const FileEntry *UmbrellaModuleMap =
00368           getModuleMapFileForUniquing(UmbrellaModule);
00369 
00370       // Infer submodules for each of the directories we found between
00371       // the directory of the umbrella header and the directory where
00372       // the actual header is located.
00373       bool Explicit = UmbrellaModule->InferExplicitSubmodules;
00374 
00375       for (unsigned I = SkippedDirs.size(); I != 0; --I) {
00376         // Find or create the module that corresponds to this directory name.
00377         SmallString<32> NameBuf;
00378         StringRef Name = sanitizeFilenameAsIdentifier(
00379             llvm::sys::path::stem(SkippedDirs[I-1]->getName()), NameBuf);
00380         Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
00381                                     Explicit).first;
00382         InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
00383         Result->IsInferred = true;
00384 
00385         // Associate the module and the directory.
00386         UmbrellaDirs[SkippedDirs[I-1]] = Result;
00387 
00388         // If inferred submodules export everything they import, add a
00389         // wildcard to the set of exports.
00390         if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
00391           Result->Exports.push_back(Module::ExportDecl(nullptr, true));
00392       }
00393 
00394       // Infer a submodule with the same name as this header file.
00395       SmallString<32> NameBuf;
00396       StringRef Name = sanitizeFilenameAsIdentifier(
00397                          llvm::sys::path::stem(File->getName()), NameBuf);
00398       Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
00399                                   Explicit).first;
00400       InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
00401       Result->IsInferred = true;
00402       Result->addTopHeader(File);
00403 
00404       // If inferred submodules export everything they import, add a
00405       // wildcard to the set of exports.
00406       if (UmbrellaModule->InferExportWildcard && Result->Exports.empty())
00407         Result->Exports.push_back(Module::ExportDecl(nullptr, true));
00408     } else {
00409       // Record each of the directories we stepped through as being part of
00410       // the module we found, since the umbrella header covers them all.
00411       for (unsigned I = 0, N = SkippedDirs.size(); I != N; ++I)
00412         UmbrellaDirs[SkippedDirs[I]] = Result;
00413     }
00414 
00415     Headers[File].push_back(KnownHeader(Result, NormalHeader));
00416 
00417     // If a header corresponds to an unavailable module, don't report
00418     // that it maps to anything.
00419     if (!Result->isAvailable())
00420       return KnownHeader();
00421 
00422     return MakeResult(Headers[File].back());
00423   }
00424 
00425   return KnownHeader();
00426 }
00427 
00428 bool ModuleMap::isHeaderInUnavailableModule(const FileEntry *Header) const {
00429   return isHeaderUnavailableInModule(Header, nullptr);
00430 }
00431 
00432 bool
00433 ModuleMap::isHeaderUnavailableInModule(const FileEntry *Header,
00434                                        const Module *RequestingModule) const {
00435   HeadersMap::const_iterator Known = Headers.find(Header);
00436   if (Known != Headers.end()) {
00437     for (SmallVectorImpl<KnownHeader>::const_iterator
00438              I = Known->second.begin(),
00439              E = Known->second.end();
00440          I != E; ++I) {
00441       if (I->isAvailable() && (!RequestingModule ||
00442                                I->getModule()->isSubModuleOf(RequestingModule)))
00443         return false;
00444     }
00445     return true;
00446   }
00447 
00448   const DirectoryEntry *Dir = Header->getDir();
00449   SmallVector<const DirectoryEntry *, 2> SkippedDirs;
00450   StringRef DirName = Dir->getName();
00451 
00452   auto IsUnavailable = [&](const Module *M) {
00453     return !M->isAvailable() && (!RequestingModule ||
00454                                  M->isSubModuleOf(RequestingModule));
00455   };
00456 
00457   // Keep walking up the directory hierarchy, looking for a directory with
00458   // an umbrella header.
00459   do {
00460     llvm::DenseMap<const DirectoryEntry *, Module *>::const_iterator KnownDir
00461       = UmbrellaDirs.find(Dir);
00462     if (KnownDir != UmbrellaDirs.end()) {
00463       Module *Found = KnownDir->second;
00464       if (IsUnavailable(Found))
00465         return true;
00466 
00467       // Search up the module stack until we find a module with an umbrella
00468       // directory.
00469       Module *UmbrellaModule = Found;
00470       while (!UmbrellaModule->getUmbrellaDir() && UmbrellaModule->Parent)
00471         UmbrellaModule = UmbrellaModule->Parent;
00472 
00473       if (UmbrellaModule->InferSubmodules) {
00474         for (unsigned I = SkippedDirs.size(); I != 0; --I) {
00475           // Find or create the module that corresponds to this directory name.
00476           SmallString<32> NameBuf;
00477           StringRef Name = sanitizeFilenameAsIdentifier(
00478                              llvm::sys::path::stem(SkippedDirs[I-1]->getName()),
00479                              NameBuf);
00480           Found = lookupModuleQualified(Name, Found);
00481           if (!Found)
00482             return false;
00483           if (IsUnavailable(Found))
00484             return true;
00485         }
00486         
00487         // Infer a submodule with the same name as this header file.
00488         SmallString<32> NameBuf;
00489         StringRef Name = sanitizeFilenameAsIdentifier(
00490                            llvm::sys::path::stem(Header->getName()),
00491                            NameBuf);
00492         Found = lookupModuleQualified(Name, Found);
00493         if (!Found)
00494           return false;
00495       }
00496 
00497       return IsUnavailable(Found);
00498     }
00499     
00500     SkippedDirs.push_back(Dir);
00501     
00502     // Retrieve our parent path.
00503     DirName = llvm::sys::path::parent_path(DirName);
00504     if (DirName.empty())
00505       break;
00506     
00507     // Resolve the parent path to a directory entry.
00508     Dir = SourceMgr.getFileManager().getDirectory(DirName);
00509   } while (Dir);
00510   
00511   return false;
00512 }
00513 
00514 Module *ModuleMap::findModule(StringRef Name) const {
00515   llvm::StringMap<Module *>::const_iterator Known = Modules.find(Name);
00516   if (Known != Modules.end())
00517     return Known->getValue();
00518 
00519   return nullptr;
00520 }
00521 
00522 Module *ModuleMap::lookupModuleUnqualified(StringRef Name,
00523                                            Module *Context) const {
00524   for(; Context; Context = Context->Parent) {
00525     if (Module *Sub = lookupModuleQualified(Name, Context))
00526       return Sub;
00527   }
00528   
00529   return findModule(Name);
00530 }
00531 
00532 Module *ModuleMap::lookupModuleQualified(StringRef Name, Module *Context) const{
00533   if (!Context)
00534     return findModule(Name);
00535   
00536   return Context->findSubmodule(Name);
00537 }
00538 
00539 std::pair<Module *, bool> 
00540 ModuleMap::findOrCreateModule(StringRef Name, Module *Parent, bool IsFramework,
00541                               bool IsExplicit) {
00542   // Try to find an existing module with this name.
00543   if (Module *Sub = lookupModuleQualified(Name, Parent))
00544     return std::make_pair(Sub, false);
00545   
00546   // Create a new module with this name.
00547   Module *Result = new Module(Name, SourceLocation(), Parent,
00548                               IsFramework, IsExplicit);
00549   if (LangOpts.CurrentModule == Name) {
00550     SourceModule = Result;
00551     SourceModuleName = Name;
00552   }
00553   if (!Parent) {
00554     Modules[Name] = Result;
00555     if (!LangOpts.CurrentModule.empty() && !CompilingModule &&
00556         Name == LangOpts.CurrentModule) {
00557       CompilingModule = Result;
00558     }
00559   }
00560   return std::make_pair(Result, true);
00561 }
00562 
00563 bool ModuleMap::canInferFrameworkModule(const DirectoryEntry *ParentDir,
00564                                         StringRef Name, bool &IsSystem) const {
00565   // Check whether we have already looked into the parent directory
00566   // for a module map.
00567   llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
00568     inferred = InferredDirectories.find(ParentDir);
00569   if (inferred == InferredDirectories.end())
00570     return false;
00571 
00572   if (!inferred->second.InferModules)
00573     return false;
00574 
00575   // We're allowed to infer for this directory, but make sure it's okay
00576   // to infer this particular module.
00577   bool canInfer = std::find(inferred->second.ExcludedModules.begin(),
00578                             inferred->second.ExcludedModules.end(),
00579                             Name) == inferred->second.ExcludedModules.end();
00580 
00581   if (canInfer && inferred->second.InferSystemModules)
00582     IsSystem = true;
00583 
00584   return canInfer;
00585 }
00586 
00587 /// \brief For a framework module, infer the framework against which we
00588 /// should link.
00589 static void inferFrameworkLink(Module *Mod, const DirectoryEntry *FrameworkDir,
00590                                FileManager &FileMgr) {
00591   assert(Mod->IsFramework && "Can only infer linking for framework modules");
00592   assert(!Mod->isSubFramework() &&
00593          "Can only infer linking for top-level frameworks");
00594 
00595   SmallString<128> LibName;
00596   LibName += FrameworkDir->getName();
00597   llvm::sys::path::append(LibName, Mod->Name);
00598   if (FileMgr.getFile(LibName)) {
00599     Mod->LinkLibraries.push_back(Module::LinkLibrary(Mod->Name,
00600                                                      /*IsFramework=*/true));
00601   }
00602 }
00603 
00604 Module *
00605 ModuleMap::inferFrameworkModule(StringRef ModuleName,
00606                                 const DirectoryEntry *FrameworkDir,
00607                                 bool IsSystem,
00608                                 Module *Parent) {
00609   // Check whether we've already found this module.
00610   if (Module *Mod = lookupModuleQualified(ModuleName, Parent))
00611     return Mod;
00612   
00613   FileManager &FileMgr = SourceMgr.getFileManager();
00614 
00615   // If the framework has a parent path from which we're allowed to infer
00616   // a framework module, do so.
00617   const FileEntry *ModuleMapFile = nullptr;
00618   if (!Parent) {
00619     // Determine whether we're allowed to infer a module map.
00620 
00621     // Note: as an egregious but useful hack we use the real path here, because
00622     // we might be looking at an embedded framework that symlinks out to a
00623     // top-level framework, and we need to infer as if we were naming the
00624     // top-level framework.
00625     StringRef FrameworkDirName
00626       = SourceMgr.getFileManager().getCanonicalName(FrameworkDir);
00627 
00628     // In case this is a case-insensitive filesystem, make sure the canonical
00629     // directory name matches ModuleName exactly. Modules are case-sensitive.
00630     // FIXME: we should be able to give a fix-it hint for the correct spelling.
00631     if (llvm::sys::path::stem(FrameworkDirName) != ModuleName)
00632       return nullptr;
00633 
00634     bool canInfer = false;
00635     if (llvm::sys::path::has_parent_path(FrameworkDirName)) {
00636       // Figure out the parent path.
00637       StringRef Parent = llvm::sys::path::parent_path(FrameworkDirName);
00638       if (const DirectoryEntry *ParentDir = FileMgr.getDirectory(Parent)) {
00639         // Check whether we have already looked into the parent directory
00640         // for a module map.
00641         llvm::DenseMap<const DirectoryEntry *, InferredDirectory>::const_iterator
00642           inferred = InferredDirectories.find(ParentDir);
00643         if (inferred == InferredDirectories.end()) {
00644           // We haven't looked here before. Load a module map, if there is
00645           // one.
00646           bool IsFrameworkDir = Parent.endswith(".framework");
00647           if (const FileEntry *ModMapFile =
00648                 HeaderInfo.lookupModuleMapFile(ParentDir, IsFrameworkDir)) {
00649             parseModuleMapFile(ModMapFile, IsSystem);
00650             inferred = InferredDirectories.find(ParentDir);
00651           }
00652 
00653           if (inferred == InferredDirectories.end())
00654             inferred = InferredDirectories.insert(
00655                          std::make_pair(ParentDir, InferredDirectory())).first;
00656         }
00657 
00658         if (inferred->second.InferModules) {
00659           // We're allowed to infer for this directory, but make sure it's okay
00660           // to infer this particular module.
00661           StringRef Name = llvm::sys::path::stem(FrameworkDirName);
00662           canInfer = std::find(inferred->second.ExcludedModules.begin(),
00663                                inferred->second.ExcludedModules.end(),
00664                                Name) == inferred->second.ExcludedModules.end();
00665 
00666           if (inferred->second.InferSystemModules)
00667             IsSystem = true;
00668           ModuleMapFile = inferred->second.ModuleMapFile;
00669         }
00670       }
00671     }
00672 
00673     // If we're not allowed to infer a framework module, don't.
00674     if (!canInfer)
00675       return nullptr;
00676   } else
00677     ModuleMapFile = getModuleMapFileForUniquing(Parent);
00678 
00679 
00680   // Look for an umbrella header.
00681   SmallString<128> UmbrellaName = StringRef(FrameworkDir->getName());
00682   llvm::sys::path::append(UmbrellaName, "Headers", ModuleName + ".h");
00683   const FileEntry *UmbrellaHeader = FileMgr.getFile(UmbrellaName);
00684   
00685   // FIXME: If there's no umbrella header, we could probably scan the
00686   // framework to load *everything*. But, it's not clear that this is a good
00687   // idea.
00688   if (!UmbrellaHeader)
00689     return nullptr;
00690 
00691   Module *Result = new Module(ModuleName, SourceLocation(), Parent,
00692                               /*IsFramework=*/true, /*IsExplicit=*/false);
00693   InferredModuleAllowedBy[Result] = ModuleMapFile;
00694   Result->IsInferred = true;
00695   if (LangOpts.CurrentModule == ModuleName) {
00696     SourceModule = Result;
00697     SourceModuleName = ModuleName;
00698   }
00699   if (IsSystem)
00700     Result->IsSystem = IsSystem;
00701   
00702   if (!Parent)
00703     Modules[ModuleName] = Result;
00704   
00705   // umbrella header "umbrella-header-name"
00706   Result->Umbrella = UmbrellaHeader;
00707   Headers[UmbrellaHeader].push_back(KnownHeader(Result, NormalHeader));
00708   UmbrellaDirs[UmbrellaHeader->getDir()] = Result;
00709   
00710   // export *
00711   Result->Exports.push_back(Module::ExportDecl(nullptr, true));
00712 
00713   // module * { export * }
00714   Result->InferSubmodules = true;
00715   Result->InferExportWildcard = true;
00716   
00717   // Look for subframeworks.
00718   std::error_code EC;
00719   SmallString<128> SubframeworksDirName
00720     = StringRef(FrameworkDir->getName());
00721   llvm::sys::path::append(SubframeworksDirName, "Frameworks");
00722   llvm::sys::path::native(SubframeworksDirName);
00723   for (llvm::sys::fs::directory_iterator 
00724          Dir(SubframeworksDirName.str(), EC), DirEnd;
00725        Dir != DirEnd && !EC; Dir.increment(EC)) {
00726     if (!StringRef(Dir->path()).endswith(".framework"))
00727       continue;
00728 
00729     if (const DirectoryEntry *SubframeworkDir
00730           = FileMgr.getDirectory(Dir->path())) {
00731       // Note: as an egregious but useful hack, we use the real path here and
00732       // check whether it is actually a subdirectory of the parent directory.
00733       // This will not be the case if the 'subframework' is actually a symlink
00734       // out to a top-level framework.
00735       StringRef SubframeworkDirName = FileMgr.getCanonicalName(SubframeworkDir);
00736       bool FoundParent = false;
00737       do {
00738         // Get the parent directory name.
00739         SubframeworkDirName
00740           = llvm::sys::path::parent_path(SubframeworkDirName);
00741         if (SubframeworkDirName.empty())
00742           break;
00743 
00744         if (FileMgr.getDirectory(SubframeworkDirName) == FrameworkDir) {
00745           FoundParent = true;
00746           break;
00747         }
00748       } while (true);
00749 
00750       if (!FoundParent)
00751         continue;
00752 
00753       // FIXME: Do we want to warn about subframeworks without umbrella headers?
00754       SmallString<32> NameBuf;
00755       inferFrameworkModule(sanitizeFilenameAsIdentifier(
00756                              llvm::sys::path::stem(Dir->path()), NameBuf),
00757                            SubframeworkDir, IsSystem, Result);
00758     }
00759   }
00760 
00761   // If the module is a top-level framework, automatically link against the
00762   // framework.
00763   if (!Result->isSubFramework()) {
00764     inferFrameworkLink(Result, FrameworkDir, FileMgr);
00765   }
00766 
00767   return Result;
00768 }
00769 
00770 void ModuleMap::setUmbrellaHeader(Module *Mod, const FileEntry *UmbrellaHeader){
00771   Headers[UmbrellaHeader].push_back(KnownHeader(Mod, NormalHeader));
00772   Mod->Umbrella = UmbrellaHeader;
00773   UmbrellaDirs[UmbrellaHeader->getDir()] = Mod;
00774 }
00775 
00776 void ModuleMap::setUmbrellaDir(Module *Mod, const DirectoryEntry *UmbrellaDir) {
00777   Mod->Umbrella = UmbrellaDir;
00778   UmbrellaDirs[UmbrellaDir] = Mod;
00779 }
00780 
00781 void ModuleMap::addHeader(Module *Mod, const FileEntry *Header,
00782                           ModuleHeaderRole Role) {
00783   switch ((int)Role) {
00784   default:
00785     llvm_unreachable("unknown header role");
00786   case NormalHeader:
00787     Mod->NormalHeaders.push_back(Header);
00788     break;
00789   case PrivateHeader:
00790     Mod->PrivateHeaders.push_back(Header);
00791     break;
00792   case TextualHeader:
00793     Mod->TextualHeaders.push_back(Header);
00794     break;
00795   case PrivateHeader | TextualHeader:
00796     Mod->PrivateTextualHeaders.push_back(Header);
00797     break;
00798   }
00799 
00800   if (!(Role & TextualHeader)) {
00801     bool isCompilingModuleHeader = Mod->getTopLevelModule() == CompilingModule;
00802     HeaderInfo.MarkFileModuleHeader(Header, Role, isCompilingModuleHeader);
00803   }
00804   Headers[Header].push_back(KnownHeader(Mod, Role));
00805 }
00806 
00807 void ModuleMap::excludeHeader(Module *Mod, const FileEntry *Header) {
00808   Mod->ExcludedHeaders.push_back(Header);
00809 
00810   // Add this as a known header so we won't implicitly add it to any
00811   // umbrella directory module.
00812   // FIXME: Should we only exclude it from umbrella modules within the
00813   // specified module?
00814   (void) Headers[Header];
00815 }
00816 
00817 const FileEntry *
00818 ModuleMap::getContainingModuleMapFile(const Module *Module) const {
00819   if (Module->DefinitionLoc.isInvalid())
00820     return nullptr;
00821 
00822   return SourceMgr.getFileEntryForID(
00823            SourceMgr.getFileID(Module->DefinitionLoc));
00824 }
00825 
00826 const FileEntry *ModuleMap::getModuleMapFileForUniquing(const Module *M) const {
00827   if (M->IsInferred) {
00828     assert(InferredModuleAllowedBy.count(M) && "missing inferred module map");
00829     return InferredModuleAllowedBy.find(M)->second;
00830   }
00831   return getContainingModuleMapFile(M);
00832 }
00833 
00834 void ModuleMap::setInferredModuleAllowedBy(Module *M, const FileEntry *ModMap) {
00835   assert(M->IsInferred && "module not inferred");
00836   InferredModuleAllowedBy[M] = ModMap;
00837 }
00838 
00839 void ModuleMap::dump() {
00840   llvm::errs() << "Modules:";
00841   for (llvm::StringMap<Module *>::iterator M = Modules.begin(), 
00842                                         MEnd = Modules.end(); 
00843        M != MEnd; ++M)
00844     M->getValue()->print(llvm::errs(), 2);
00845   
00846   llvm::errs() << "Headers:";
00847   for (HeadersMap::iterator H = Headers.begin(), HEnd = Headers.end();
00848        H != HEnd; ++H) {
00849     llvm::errs() << "  \"" << H->first->getName() << "\" -> ";
00850     for (SmallVectorImpl<KnownHeader>::const_iterator I = H->second.begin(),
00851                                                       E = H->second.end();
00852          I != E; ++I) {
00853       if (I != H->second.begin())
00854         llvm::errs() << ",";
00855       llvm::errs() << I->getModule()->getFullModuleName();
00856     }
00857     llvm::errs() << "\n";
00858   }
00859 }
00860 
00861 bool ModuleMap::resolveExports(Module *Mod, bool Complain) {
00862   bool HadError = false;
00863   for (unsigned I = 0, N = Mod->UnresolvedExports.size(); I != N; ++I) {
00864     Module::ExportDecl Export = resolveExport(Mod, Mod->UnresolvedExports[I], 
00865                                               Complain);
00866     if (Export.getPointer() || Export.getInt())
00867       Mod->Exports.push_back(Export);
00868     else
00869       HadError = true;
00870   }
00871   Mod->UnresolvedExports.clear();
00872   return HadError;
00873 }
00874 
00875 bool ModuleMap::resolveUses(Module *Mod, bool Complain) {
00876   bool HadError = false;
00877   for (unsigned I = 0, N = Mod->UnresolvedDirectUses.size(); I != N; ++I) {
00878     Module *DirectUse =
00879         resolveModuleId(Mod->UnresolvedDirectUses[I], Mod, Complain);
00880     if (DirectUse)
00881       Mod->DirectUses.push_back(DirectUse);
00882     else
00883       HadError = true;
00884   }
00885   Mod->UnresolvedDirectUses.clear();
00886   return HadError;
00887 }
00888 
00889 bool ModuleMap::resolveConflicts(Module *Mod, bool Complain) {
00890   bool HadError = false;
00891   for (unsigned I = 0, N = Mod->UnresolvedConflicts.size(); I != N; ++I) {
00892     Module *OtherMod = resolveModuleId(Mod->UnresolvedConflicts[I].Id,
00893                                        Mod, Complain);
00894     if (!OtherMod) {
00895       HadError = true;
00896       continue;
00897     }
00898 
00899     Module::Conflict Conflict;
00900     Conflict.Other = OtherMod;
00901     Conflict.Message = Mod->UnresolvedConflicts[I].Message;
00902     Mod->Conflicts.push_back(Conflict);
00903   }
00904   Mod->UnresolvedConflicts.clear();
00905   return HadError;
00906 }
00907 
00908 Module *ModuleMap::inferModuleFromLocation(FullSourceLoc Loc) {
00909   if (Loc.isInvalid())
00910     return nullptr;
00911 
00912   // Use the expansion location to determine which module we're in.
00913   FullSourceLoc ExpansionLoc = Loc.getExpansionLoc();
00914   if (!ExpansionLoc.isFileID())
00915     return nullptr;
00916 
00917   const SourceManager &SrcMgr = Loc.getManager();
00918   FileID ExpansionFileID = ExpansionLoc.getFileID();
00919   
00920   while (const FileEntry *ExpansionFile
00921            = SrcMgr.getFileEntryForID(ExpansionFileID)) {
00922     // Find the module that owns this header (if any).
00923     if (Module *Mod = findModuleForHeader(ExpansionFile).getModule())
00924       return Mod;
00925     
00926     // No module owns this header, so look up the inclusion chain to see if
00927     // any included header has an associated module.
00928     SourceLocation IncludeLoc = SrcMgr.getIncludeLoc(ExpansionFileID);
00929     if (IncludeLoc.isInvalid())
00930       return nullptr;
00931 
00932     ExpansionFileID = SrcMgr.getFileID(IncludeLoc);
00933   }
00934 
00935   return nullptr;
00936 }
00937 
00938 //----------------------------------------------------------------------------//
00939 // Module map file parser
00940 //----------------------------------------------------------------------------//
00941 
00942 namespace clang {
00943   /// \brief A token in a module map file.
00944   struct MMToken {
00945     enum TokenKind {
00946       Comma,
00947       ConfigMacros,
00948       Conflict,
00949       EndOfFile,
00950       HeaderKeyword,
00951       Identifier,
00952       Exclaim,
00953       ExcludeKeyword,
00954       ExplicitKeyword,
00955       ExportKeyword,
00956       ExternKeyword,
00957       FrameworkKeyword,
00958       LinkKeyword,
00959       ModuleKeyword,
00960       Period,
00961       PrivateKeyword,
00962       UmbrellaKeyword,
00963       UseKeyword,
00964       RequiresKeyword,
00965       Star,
00966       StringLiteral,
00967       TextualKeyword,
00968       LBrace,
00969       RBrace,
00970       LSquare,
00971       RSquare
00972     } Kind;
00973     
00974     unsigned Location;
00975     unsigned StringLength;
00976     const char *StringData;
00977     
00978     void clear() {
00979       Kind = EndOfFile;
00980       Location = 0;
00981       StringLength = 0;
00982       StringData = nullptr;
00983     }
00984     
00985     bool is(TokenKind K) const { return Kind == K; }
00986     
00987     SourceLocation getLocation() const {
00988       return SourceLocation::getFromRawEncoding(Location);
00989     }
00990     
00991     StringRef getString() const {
00992       return StringRef(StringData, StringLength);
00993     }
00994   };
00995 
00996   /// \brief The set of attributes that can be attached to a module.
00997   struct Attributes {
00998     Attributes() : IsSystem(), IsExternC(), IsExhaustive() { }
00999 
01000     /// \brief Whether this is a system module.
01001     unsigned IsSystem : 1;
01002 
01003     /// \brief Whether this is an extern "C" module.
01004     unsigned IsExternC : 1;
01005 
01006     /// \brief Whether this is an exhaustive set of configuration macros.
01007     unsigned IsExhaustive : 1;
01008   };
01009   
01010 
01011   class ModuleMapParser {
01012     Lexer &L;
01013     SourceManager &SourceMgr;
01014 
01015     /// \brief Default target information, used only for string literal
01016     /// parsing.
01017     const TargetInfo *Target;
01018 
01019     DiagnosticsEngine &Diags;
01020     ModuleMap &Map;
01021 
01022     /// \brief The current module map file.
01023     const FileEntry *ModuleMapFile;
01024     
01025     /// \brief The directory that this module map resides in.
01026     const DirectoryEntry *Directory;
01027 
01028     /// \brief The directory containing Clang-supplied headers.
01029     const DirectoryEntry *BuiltinIncludeDir;
01030 
01031     /// \brief Whether this module map is in a system header directory.
01032     bool IsSystem;
01033     
01034     /// \brief Whether an error occurred.
01035     bool HadError;
01036         
01037     /// \brief Stores string data for the various string literals referenced
01038     /// during parsing.
01039     llvm::BumpPtrAllocator StringData;
01040     
01041     /// \brief The current token.
01042     MMToken Tok;
01043     
01044     /// \brief The active module.
01045     Module *ActiveModule;
01046     
01047     /// \brief Consume the current token and return its location.
01048     SourceLocation consumeToken();
01049     
01050     /// \brief Skip tokens until we reach the a token with the given kind
01051     /// (or the end of the file).
01052     void skipUntil(MMToken::TokenKind K);
01053 
01054     typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
01055     bool parseModuleId(ModuleId &Id);
01056     void parseModuleDecl();
01057     void parseExternModuleDecl();
01058     void parseRequiresDecl();
01059     void parseHeaderDecl(clang::MMToken::TokenKind,
01060                          SourceLocation LeadingLoc);
01061     void parseUmbrellaDirDecl(SourceLocation UmbrellaLoc);
01062     void parseExportDecl();
01063     void parseUseDecl();
01064     void parseLinkDecl();
01065     void parseConfigMacros();
01066     void parseConflict();
01067     void parseInferredModuleDecl(bool Framework, bool Explicit);
01068     bool parseOptionalAttributes(Attributes &Attrs);
01069     
01070   public:
01071     explicit ModuleMapParser(Lexer &L, SourceManager &SourceMgr, 
01072                              const TargetInfo *Target,
01073                              DiagnosticsEngine &Diags,
01074                              ModuleMap &Map,
01075                              const FileEntry *ModuleMapFile,
01076                              const DirectoryEntry *Directory,
01077                              const DirectoryEntry *BuiltinIncludeDir,
01078                              bool IsSystem)
01079       : L(L), SourceMgr(SourceMgr), Target(Target), Diags(Diags), Map(Map), 
01080         ModuleMapFile(ModuleMapFile), Directory(Directory),
01081         BuiltinIncludeDir(BuiltinIncludeDir), IsSystem(IsSystem),
01082         HadError(false), ActiveModule(nullptr)
01083     {
01084       Tok.clear();
01085       consumeToken();
01086     }
01087     
01088     bool parseModuleMapFile();
01089   };
01090 }
01091 
01092 SourceLocation ModuleMapParser::consumeToken() {
01093 retry:
01094   SourceLocation Result = Tok.getLocation();
01095   Tok.clear();
01096   
01097   Token LToken;
01098   L.LexFromRawLexer(LToken);
01099   Tok.Location = LToken.getLocation().getRawEncoding();
01100   switch (LToken.getKind()) {
01101   case tok::raw_identifier: {
01102     StringRef RI = LToken.getRawIdentifier();
01103     Tok.StringData = RI.data();
01104     Tok.StringLength = RI.size();
01105     Tok.Kind = llvm::StringSwitch<MMToken::TokenKind>(RI)
01106                  .Case("config_macros", MMToken::ConfigMacros)
01107                  .Case("conflict", MMToken::Conflict)
01108                  .Case("exclude", MMToken::ExcludeKeyword)
01109                  .Case("explicit", MMToken::ExplicitKeyword)
01110                  .Case("export", MMToken::ExportKeyword)
01111                  .Case("extern", MMToken::ExternKeyword)
01112                  .Case("framework", MMToken::FrameworkKeyword)
01113                  .Case("header", MMToken::HeaderKeyword)
01114                  .Case("link", MMToken::LinkKeyword)
01115                  .Case("module", MMToken::ModuleKeyword)
01116                  .Case("private", MMToken::PrivateKeyword)
01117                  .Case("requires", MMToken::RequiresKeyword)
01118                  .Case("textual", MMToken::TextualKeyword)
01119                  .Case("umbrella", MMToken::UmbrellaKeyword)
01120                  .Case("use", MMToken::UseKeyword)
01121                  .Default(MMToken::Identifier);
01122     break;
01123   }
01124 
01125   case tok::comma:
01126     Tok.Kind = MMToken::Comma;
01127     break;
01128 
01129   case tok::eof:
01130     Tok.Kind = MMToken::EndOfFile;
01131     break;
01132       
01133   case tok::l_brace:
01134     Tok.Kind = MMToken::LBrace;
01135     break;
01136 
01137   case tok::l_square:
01138     Tok.Kind = MMToken::LSquare;
01139     break;
01140       
01141   case tok::period:
01142     Tok.Kind = MMToken::Period;
01143     break;
01144       
01145   case tok::r_brace:
01146     Tok.Kind = MMToken::RBrace;
01147     break;
01148       
01149   case tok::r_square:
01150     Tok.Kind = MMToken::RSquare;
01151     break;
01152       
01153   case tok::star:
01154     Tok.Kind = MMToken::Star;
01155     break;
01156       
01157   case tok::exclaim:
01158     Tok.Kind = MMToken::Exclaim;
01159     break;
01160       
01161   case tok::string_literal: {
01162     if (LToken.hasUDSuffix()) {
01163       Diags.Report(LToken.getLocation(), diag::err_invalid_string_udl);
01164       HadError = true;
01165       goto retry;
01166     }
01167 
01168     // Parse the string literal.
01169     LangOptions LangOpts;
01170     StringLiteralParser StringLiteral(LToken, SourceMgr, LangOpts, *Target);
01171     if (StringLiteral.hadError)
01172       goto retry;
01173     
01174     // Copy the string literal into our string data allocator.
01175     unsigned Length = StringLiteral.GetStringLength();
01176     char *Saved = StringData.Allocate<char>(Length + 1);
01177     memcpy(Saved, StringLiteral.GetString().data(), Length);
01178     Saved[Length] = 0;
01179     
01180     // Form the token.
01181     Tok.Kind = MMToken::StringLiteral;
01182     Tok.StringData = Saved;
01183     Tok.StringLength = Length;
01184     break;
01185   }
01186       
01187   case tok::comment:
01188     goto retry;
01189       
01190   default:
01191     Diags.Report(LToken.getLocation(), diag::err_mmap_unknown_token);
01192     HadError = true;
01193     goto retry;
01194   }
01195   
01196   return Result;
01197 }
01198 
01199 void ModuleMapParser::skipUntil(MMToken::TokenKind K) {
01200   unsigned braceDepth = 0;
01201   unsigned squareDepth = 0;
01202   do {
01203     switch (Tok.Kind) {
01204     case MMToken::EndOfFile:
01205       return;
01206 
01207     case MMToken::LBrace:
01208       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
01209         return;
01210         
01211       ++braceDepth;
01212       break;
01213 
01214     case MMToken::LSquare:
01215       if (Tok.is(K) && braceDepth == 0 && squareDepth == 0)
01216         return;
01217       
01218       ++squareDepth;
01219       break;
01220 
01221     case MMToken::RBrace:
01222       if (braceDepth > 0)
01223         --braceDepth;
01224       else if (Tok.is(K))
01225         return;
01226       break;
01227 
01228     case MMToken::RSquare:
01229       if (squareDepth > 0)
01230         --squareDepth;
01231       else if (Tok.is(K))
01232         return;
01233       break;
01234 
01235     default:
01236       if (braceDepth == 0 && squareDepth == 0 && Tok.is(K))
01237         return;
01238       break;
01239     }
01240     
01241    consumeToken();
01242   } while (true);
01243 }
01244 
01245 /// \brief Parse a module-id.
01246 ///
01247 ///   module-id:
01248 ///     identifier
01249 ///     identifier '.' module-id
01250 ///
01251 /// \returns true if an error occurred, false otherwise.
01252 bool ModuleMapParser::parseModuleId(ModuleId &Id) {
01253   Id.clear();
01254   do {
01255     if (Tok.is(MMToken::Identifier) || Tok.is(MMToken::StringLiteral)) {
01256       Id.push_back(std::make_pair(Tok.getString(), Tok.getLocation()));
01257       consumeToken();
01258     } else {
01259       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module_name);
01260       return true;
01261     }
01262     
01263     if (!Tok.is(MMToken::Period))
01264       break;
01265     
01266     consumeToken();
01267   } while (true);
01268   
01269   return false;
01270 }
01271 
01272 namespace {
01273   /// \brief Enumerates the known attributes.
01274   enum AttributeKind {
01275     /// \brief An unknown attribute.
01276     AT_unknown,
01277     /// \brief The 'system' attribute.
01278     AT_system,
01279     /// \brief The 'extern_c' attribute.
01280     AT_extern_c,
01281     /// \brief The 'exhaustive' attribute.
01282     AT_exhaustive
01283   };
01284 }
01285 
01286 /// \brief Parse a module declaration.
01287 ///
01288 ///   module-declaration:
01289 ///     'extern' 'module' module-id string-literal
01290 ///     'explicit'[opt] 'framework'[opt] 'module' module-id attributes[opt] 
01291 ///       { module-member* }
01292 ///
01293 ///   module-member:
01294 ///     requires-declaration
01295 ///     header-declaration
01296 ///     submodule-declaration
01297 ///     export-declaration
01298 ///     link-declaration
01299 ///
01300 ///   submodule-declaration:
01301 ///     module-declaration
01302 ///     inferred-submodule-declaration
01303 void ModuleMapParser::parseModuleDecl() {
01304   assert(Tok.is(MMToken::ExplicitKeyword) || Tok.is(MMToken::ModuleKeyword) ||
01305          Tok.is(MMToken::FrameworkKeyword) || Tok.is(MMToken::ExternKeyword));
01306   if (Tok.is(MMToken::ExternKeyword)) {
01307     parseExternModuleDecl();
01308     return;
01309   }
01310 
01311   // Parse 'explicit' or 'framework' keyword, if present.
01312   SourceLocation ExplicitLoc;
01313   bool Explicit = false;
01314   bool Framework = false;
01315 
01316   // Parse 'explicit' keyword, if present.
01317   if (Tok.is(MMToken::ExplicitKeyword)) {
01318     ExplicitLoc = consumeToken();
01319     Explicit = true;
01320   }
01321 
01322   // Parse 'framework' keyword, if present.
01323   if (Tok.is(MMToken::FrameworkKeyword)) {
01324     consumeToken();
01325     Framework = true;
01326   } 
01327   
01328   // Parse 'module' keyword.
01329   if (!Tok.is(MMToken::ModuleKeyword)) {
01330     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
01331     consumeToken();
01332     HadError = true;
01333     return;
01334   }
01335   consumeToken(); // 'module' keyword
01336 
01337   // If we have a wildcard for the module name, this is an inferred submodule.
01338   // Parse it. 
01339   if (Tok.is(MMToken::Star))
01340     return parseInferredModuleDecl(Framework, Explicit);
01341   
01342   // Parse the module name.
01343   ModuleId Id;
01344   if (parseModuleId(Id)) {
01345     HadError = true;
01346     return;
01347   }
01348 
01349   if (ActiveModule) {
01350     if (Id.size() > 1) {
01351       Diags.Report(Id.front().second, diag::err_mmap_nested_submodule_id)
01352         << SourceRange(Id.front().second, Id.back().second);
01353       
01354       HadError = true;
01355       return;
01356     }
01357   } else if (Id.size() == 1 && Explicit) {
01358     // Top-level modules can't be explicit.
01359     Diags.Report(ExplicitLoc, diag::err_mmap_explicit_top_level);
01360     Explicit = false;
01361     ExplicitLoc = SourceLocation();
01362     HadError = true;
01363   }
01364   
01365   Module *PreviousActiveModule = ActiveModule;  
01366   if (Id.size() > 1) {
01367     // This module map defines a submodule. Go find the module of which it
01368     // is a submodule.
01369     ActiveModule = nullptr;
01370     const Module *TopLevelModule = nullptr;
01371     for (unsigned I = 0, N = Id.size() - 1; I != N; ++I) {
01372       if (Module *Next = Map.lookupModuleQualified(Id[I].first, ActiveModule)) {
01373         if (I == 0)
01374           TopLevelModule = Next;
01375         ActiveModule = Next;
01376         continue;
01377       }
01378       
01379       if (ActiveModule) {
01380         Diags.Report(Id[I].second, diag::err_mmap_missing_module_qualified)
01381           << Id[I].first
01382           << ActiveModule->getTopLevelModule()->getFullModuleName();
01383       } else {
01384         Diags.Report(Id[I].second, diag::err_mmap_expected_module_name);
01385       }
01386       HadError = true;
01387       return;
01388     }
01389 
01390     if (ModuleMapFile != Map.getContainingModuleMapFile(TopLevelModule)) {
01391       assert(ModuleMapFile != Map.getModuleMapFileForUniquing(TopLevelModule) &&
01392              "submodule defined in same file as 'module *' that allowed its "
01393              "top-level module");
01394       Map.addAdditionalModuleMapFile(TopLevelModule, ModuleMapFile);
01395     }
01396   }
01397   
01398   StringRef ModuleName = Id.back().first;
01399   SourceLocation ModuleNameLoc = Id.back().second;
01400   
01401   // Parse the optional attribute list.
01402   Attributes Attrs;
01403   parseOptionalAttributes(Attrs);
01404   
01405   // Parse the opening brace.
01406   if (!Tok.is(MMToken::LBrace)) {
01407     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace)
01408       << ModuleName;
01409     HadError = true;
01410     return;
01411   }  
01412   SourceLocation LBraceLoc = consumeToken();
01413   
01414   // Determine whether this (sub)module has already been defined.
01415   if (Module *Existing = Map.lookupModuleQualified(ModuleName, ActiveModule)) {
01416     if (Existing->DefinitionLoc.isInvalid() && !ActiveModule) {
01417       // Skip the module definition.
01418       skipUntil(MMToken::RBrace);
01419       if (Tok.is(MMToken::RBrace))
01420         consumeToken();
01421       else {
01422         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
01423         Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
01424         HadError = true;        
01425       }
01426       return;
01427     }
01428     
01429     Diags.Report(ModuleNameLoc, diag::err_mmap_module_redefinition)
01430       << ModuleName;
01431     Diags.Report(Existing->DefinitionLoc, diag::note_mmap_prev_definition);
01432     
01433     // Skip the module definition.
01434     skipUntil(MMToken::RBrace);
01435     if (Tok.is(MMToken::RBrace))
01436       consumeToken();
01437     
01438     HadError = true;
01439     return;
01440   }
01441 
01442   // Start defining this module.
01443   ActiveModule = Map.findOrCreateModule(ModuleName, ActiveModule, Framework,
01444                                         Explicit).first;
01445   ActiveModule->DefinitionLoc = ModuleNameLoc;
01446   if (Attrs.IsSystem || IsSystem)
01447     ActiveModule->IsSystem = true;
01448   if (Attrs.IsExternC)
01449     ActiveModule->IsExternC = true;
01450 
01451   bool Done = false;
01452   do {
01453     switch (Tok.Kind) {
01454     case MMToken::EndOfFile:
01455     case MMToken::RBrace:
01456       Done = true;
01457       break;
01458 
01459     case MMToken::ConfigMacros:
01460       parseConfigMacros();
01461       break;
01462 
01463     case MMToken::Conflict:
01464       parseConflict();
01465       break;
01466 
01467     case MMToken::ExplicitKeyword:
01468     case MMToken::ExternKeyword:
01469     case MMToken::FrameworkKeyword:
01470     case MMToken::ModuleKeyword:
01471       parseModuleDecl();
01472       break;
01473 
01474     case MMToken::ExportKeyword:
01475       parseExportDecl();
01476       break;
01477 
01478     case MMToken::UseKeyword:
01479       parseUseDecl();
01480       break;
01481         
01482     case MMToken::RequiresKeyword:
01483       parseRequiresDecl();
01484       break;
01485 
01486     case MMToken::TextualKeyword:
01487       parseHeaderDecl(MMToken::TextualKeyword, consumeToken());
01488       break;
01489 
01490     case MMToken::UmbrellaKeyword: {
01491       SourceLocation UmbrellaLoc = consumeToken();
01492       if (Tok.is(MMToken::HeaderKeyword))
01493         parseHeaderDecl(MMToken::UmbrellaKeyword, UmbrellaLoc);
01494       else
01495         parseUmbrellaDirDecl(UmbrellaLoc);
01496       break;
01497     }
01498 
01499     case MMToken::ExcludeKeyword:
01500       parseHeaderDecl(MMToken::ExcludeKeyword, consumeToken());
01501       break;
01502 
01503     case MMToken::PrivateKeyword:
01504       parseHeaderDecl(MMToken::PrivateKeyword, consumeToken());
01505       break;
01506 
01507     case MMToken::HeaderKeyword:
01508       parseHeaderDecl(MMToken::HeaderKeyword, consumeToken());
01509       break;
01510 
01511     case MMToken::LinkKeyword:
01512       parseLinkDecl();
01513       break;
01514 
01515     default:
01516       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_member);
01517       consumeToken();
01518       break;        
01519     }
01520   } while (!Done);
01521 
01522   if (Tok.is(MMToken::RBrace))
01523     consumeToken();
01524   else {
01525     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
01526     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
01527     HadError = true;
01528   }
01529 
01530   // If the active module is a top-level framework, and there are no link
01531   // libraries, automatically link against the framework.
01532   if (ActiveModule->IsFramework && !ActiveModule->isSubFramework() &&
01533       ActiveModule->LinkLibraries.empty()) {
01534     inferFrameworkLink(ActiveModule, Directory, SourceMgr.getFileManager());
01535   }
01536 
01537   // If the module meets all requirements but is still unavailable, mark the
01538   // whole tree as unavailable to prevent it from building.
01539   if (!ActiveModule->IsAvailable && !ActiveModule->IsMissingRequirement &&
01540       ActiveModule->Parent) {
01541     ActiveModule->getTopLevelModule()->markUnavailable();
01542     ActiveModule->getTopLevelModule()->MissingHeaders.append(
01543       ActiveModule->MissingHeaders.begin(), ActiveModule->MissingHeaders.end());
01544   }
01545 
01546   // We're done parsing this module. Pop back to the previous module.
01547   ActiveModule = PreviousActiveModule;
01548 }
01549 
01550 /// \brief Parse an extern module declaration.
01551 ///
01552 ///   extern module-declaration:
01553 ///     'extern' 'module' module-id string-literal
01554 void ModuleMapParser::parseExternModuleDecl() {
01555   assert(Tok.is(MMToken::ExternKeyword));
01556   consumeToken(); // 'extern' keyword
01557 
01558   // Parse 'module' keyword.
01559   if (!Tok.is(MMToken::ModuleKeyword)) {
01560     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
01561     consumeToken();
01562     HadError = true;
01563     return;
01564   }
01565   consumeToken(); // 'module' keyword
01566 
01567   // Parse the module name.
01568   ModuleId Id;
01569   if (parseModuleId(Id)) {
01570     HadError = true;
01571     return;
01572   }
01573 
01574   // Parse the referenced module map file name.
01575   if (!Tok.is(MMToken::StringLiteral)) {
01576     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_mmap_file);
01577     HadError = true;
01578     return;
01579   }
01580   std::string FileName = Tok.getString();
01581   consumeToken(); // filename
01582 
01583   StringRef FileNameRef = FileName;
01584   SmallString<128> ModuleMapFileName;
01585   if (llvm::sys::path::is_relative(FileNameRef)) {
01586     ModuleMapFileName += Directory->getName();
01587     llvm::sys::path::append(ModuleMapFileName, FileName);
01588     FileNameRef = ModuleMapFileName.str();
01589   }
01590   if (const FileEntry *File = SourceMgr.getFileManager().getFile(FileNameRef))
01591     Map.parseModuleMapFile(File, /*IsSystem=*/false);
01592 }
01593 
01594 /// \brief Parse a requires declaration.
01595 ///
01596 ///   requires-declaration:
01597 ///     'requires' feature-list
01598 ///
01599 ///   feature-list:
01600 ///     feature ',' feature-list
01601 ///     feature
01602 ///
01603 ///   feature:
01604 ///     '!'[opt] identifier
01605 void ModuleMapParser::parseRequiresDecl() {
01606   assert(Tok.is(MMToken::RequiresKeyword));
01607 
01608   // Parse 'requires' keyword.
01609   consumeToken();
01610 
01611   // Parse the feature-list.
01612   do {
01613     bool RequiredState = true;
01614     if (Tok.is(MMToken::Exclaim)) {
01615       RequiredState = false;
01616       consumeToken();
01617     }
01618 
01619     if (!Tok.is(MMToken::Identifier)) {
01620       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_feature);
01621       HadError = true;
01622       return;
01623     }
01624 
01625     // Consume the feature name.
01626     std::string Feature = Tok.getString();
01627     consumeToken();
01628 
01629     // Add this feature.
01630     ActiveModule->addRequirement(Feature, RequiredState,
01631                                  Map.LangOpts, *Map.Target);
01632 
01633     if (!Tok.is(MMToken::Comma))
01634       break;
01635 
01636     // Consume the comma.
01637     consumeToken();
01638   } while (true);
01639 }
01640 
01641 /// \brief Append to \p Paths the set of paths needed to get to the 
01642 /// subframework in which the given module lives.
01643 static void appendSubframeworkPaths(Module *Mod,
01644                                     SmallVectorImpl<char> &Path) {
01645   // Collect the framework names from the given module to the top-level module.
01646   SmallVector<StringRef, 2> Paths;
01647   for (; Mod; Mod = Mod->Parent) {
01648     if (Mod->IsFramework)
01649       Paths.push_back(Mod->Name);
01650   }
01651   
01652   if (Paths.empty())
01653     return;
01654   
01655   // Add Frameworks/Name.framework for each subframework.
01656   for (unsigned I = Paths.size() - 1; I != 0; --I)
01657     llvm::sys::path::append(Path, "Frameworks", Paths[I-1] + ".framework");
01658 }
01659 
01660 /// \brief Parse a header declaration.
01661 ///
01662 ///   header-declaration:
01663 ///     'textual'[opt] 'header' string-literal
01664 ///     'private' 'textual'[opt] 'header' string-literal
01665 ///     'exclude' 'header' string-literal
01666 ///     'umbrella' 'header' string-literal
01667 ///
01668 /// FIXME: Support 'private textual header'.
01669 void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
01670                                       SourceLocation LeadingLoc) {
01671   // We've already consumed the first token.
01672   ModuleMap::ModuleHeaderRole Role = ModuleMap::NormalHeader;
01673   if (LeadingToken == MMToken::PrivateKeyword) {
01674     Role = ModuleMap::PrivateHeader;
01675     // 'private' may optionally be followed by 'textual'.
01676     if (Tok.is(MMToken::TextualKeyword)) {
01677       LeadingToken = Tok.Kind;
01678       consumeToken();
01679     }
01680   }
01681   if (LeadingToken == MMToken::TextualKeyword)
01682     Role = ModuleMap::ModuleHeaderRole(Role | ModuleMap::TextualHeader);
01683 
01684   if (LeadingToken != MMToken::HeaderKeyword) {
01685     if (!Tok.is(MMToken::HeaderKeyword)) {
01686       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header)
01687           << (LeadingToken == MMToken::PrivateKeyword ? "private" :
01688               LeadingToken == MMToken::ExcludeKeyword ? "exclude" :
01689               LeadingToken == MMToken::TextualKeyword ? "textual" : "umbrella");
01690       return;
01691     }
01692     consumeToken();
01693   }
01694 
01695   // Parse the header name.
01696   if (!Tok.is(MMToken::StringLiteral)) {
01697     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 
01698       << "header";
01699     HadError = true;
01700     return;
01701   }
01702   Module::HeaderDirective Header;
01703   Header.FileName = Tok.getString();
01704   Header.FileNameLoc = consumeToken();
01705   
01706   // Check whether we already have an umbrella.
01707   if (LeadingToken == MMToken::UmbrellaKeyword && ActiveModule->Umbrella) {
01708     Diags.Report(Header.FileNameLoc, diag::err_mmap_umbrella_clash)
01709       << ActiveModule->getFullModuleName();
01710     HadError = true;
01711     return;
01712   }
01713 
01714   // Look for this file.
01715   const FileEntry *File = nullptr;
01716   const FileEntry *BuiltinFile = nullptr;
01717   SmallString<128> PathName;
01718   if (llvm::sys::path::is_absolute(Header.FileName)) {
01719     PathName = Header.FileName;
01720     File = SourceMgr.getFileManager().getFile(PathName);
01721   } else {
01722     // Search for the header file within the search directory.
01723     PathName = Directory->getName();
01724     unsigned PathLength = PathName.size();
01725     
01726     if (ActiveModule->isPartOfFramework()) {
01727       appendSubframeworkPaths(ActiveModule, PathName);
01728       
01729       // Check whether this file is in the public headers.
01730       llvm::sys::path::append(PathName, "Headers", Header.FileName);
01731       File = SourceMgr.getFileManager().getFile(PathName);
01732       
01733       if (!File) {
01734         // Check whether this file is in the private headers.
01735         PathName.resize(PathLength);
01736         llvm::sys::path::append(PathName, "PrivateHeaders", Header.FileName);
01737         File = SourceMgr.getFileManager().getFile(PathName);
01738       }
01739     } else {
01740       // Lookup for normal headers.
01741       llvm::sys::path::append(PathName, Header.FileName);
01742       File = SourceMgr.getFileManager().getFile(PathName);
01743       
01744       // If this is a system module with a top-level header, this header
01745       // may have a counterpart (or replacement) in the set of headers
01746       // supplied by Clang. Find that builtin header.
01747       if (ActiveModule->IsSystem && LeadingToken != MMToken::UmbrellaKeyword &&
01748           BuiltinIncludeDir && BuiltinIncludeDir != Directory &&
01749           isBuiltinHeader(Header.FileName)) {
01750         SmallString<128> BuiltinPathName(BuiltinIncludeDir->getName());
01751         llvm::sys::path::append(BuiltinPathName, Header.FileName);
01752         BuiltinFile = SourceMgr.getFileManager().getFile(BuiltinPathName);
01753         
01754         // If Clang supplies this header but the underlying system does not,
01755         // just silently swap in our builtin version. Otherwise, we'll end
01756         // up adding both (later).
01757         if (!File && BuiltinFile) {
01758           File = BuiltinFile;
01759           BuiltinFile = nullptr;
01760         }
01761       }
01762     }
01763   }
01764   
01765   // FIXME: We shouldn't be eagerly stat'ing every file named in a module map.
01766   // Come up with a lazy way to do this.
01767   if (File) {
01768     if (LeadingToken == MMToken::UmbrellaKeyword) {
01769       const DirectoryEntry *UmbrellaDir = File->getDir();
01770       if (Module *UmbrellaModule = Map.UmbrellaDirs[UmbrellaDir]) {
01771         Diags.Report(LeadingLoc, diag::err_mmap_umbrella_clash)
01772           << UmbrellaModule->getFullModuleName();
01773         HadError = true;
01774       } else {
01775         // Record this umbrella header.
01776         Map.setUmbrellaHeader(ActiveModule, File);
01777       }
01778     } else if (LeadingToken == MMToken::ExcludeKeyword) {
01779       Map.excludeHeader(ActiveModule, File);
01780     } else {
01781       // If there is a builtin counterpart to this file, add it now, before
01782       // the "real" header, so we build the built-in one first when building
01783       // the module.
01784       if (BuiltinFile)
01785         Map.addHeader(ActiveModule, BuiltinFile, Role);
01786 
01787       // Record this header.
01788       Map.addHeader(ActiveModule, File, Role);
01789     }
01790   } else if (LeadingToken != MMToken::ExcludeKeyword) {
01791     // Ignore excluded header files. They're optional anyway.
01792 
01793     // If we find a module that has a missing header, we mark this module as
01794     // unavailable and store the header directive for displaying diagnostics.
01795     Header.IsUmbrella = LeadingToken == MMToken::UmbrellaKeyword;
01796     ActiveModule->markUnavailable();
01797     ActiveModule->MissingHeaders.push_back(Header);
01798   }
01799 }
01800 
01801 /// \brief Parse an umbrella directory declaration.
01802 ///
01803 ///   umbrella-dir-declaration:
01804 ///     umbrella string-literal
01805 void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
01806   // Parse the directory name.
01807   if (!Tok.is(MMToken::StringLiteral)) {
01808     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_header) 
01809       << "umbrella";
01810     HadError = true;
01811     return;
01812   }
01813 
01814   std::string DirName = Tok.getString();
01815   SourceLocation DirNameLoc = consumeToken();
01816   
01817   // Check whether we already have an umbrella.
01818   if (ActiveModule->Umbrella) {
01819     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_clash)
01820       << ActiveModule->getFullModuleName();
01821     HadError = true;
01822     return;
01823   }
01824 
01825   // Look for this file.
01826   const DirectoryEntry *Dir = nullptr;
01827   if (llvm::sys::path::is_absolute(DirName))
01828     Dir = SourceMgr.getFileManager().getDirectory(DirName);
01829   else {
01830     SmallString<128> PathName;
01831     PathName = Directory->getName();
01832     llvm::sys::path::append(PathName, DirName);
01833     Dir = SourceMgr.getFileManager().getDirectory(PathName);
01834   }
01835   
01836   if (!Dir) {
01837     Diags.Report(DirNameLoc, diag::err_mmap_umbrella_dir_not_found)
01838       << DirName;
01839     HadError = true;
01840     return;
01841   }
01842   
01843   if (Module *OwningModule = Map.UmbrellaDirs[Dir]) {
01844     Diags.Report(UmbrellaLoc, diag::err_mmap_umbrella_clash)
01845       << OwningModule->getFullModuleName();
01846     HadError = true;
01847     return;
01848   } 
01849   
01850   // Record this umbrella directory.
01851   Map.setUmbrellaDir(ActiveModule, Dir);
01852 }
01853 
01854 /// \brief Parse a module export declaration.
01855 ///
01856 ///   export-declaration:
01857 ///     'export' wildcard-module-id
01858 ///
01859 ///   wildcard-module-id:
01860 ///     identifier
01861 ///     '*'
01862 ///     identifier '.' wildcard-module-id
01863 void ModuleMapParser::parseExportDecl() {
01864   assert(Tok.is(MMToken::ExportKeyword));
01865   SourceLocation ExportLoc = consumeToken();
01866   
01867   // Parse the module-id with an optional wildcard at the end.
01868   ModuleId ParsedModuleId;
01869   bool Wildcard = false;
01870   do {
01871     // FIXME: Support string-literal module names here.
01872     if (Tok.is(MMToken::Identifier)) {
01873       ParsedModuleId.push_back(std::make_pair(Tok.getString(), 
01874                                               Tok.getLocation()));
01875       consumeToken();
01876       
01877       if (Tok.is(MMToken::Period)) {
01878         consumeToken();
01879         continue;
01880       } 
01881       
01882       break;
01883     }
01884     
01885     if(Tok.is(MMToken::Star)) {
01886       Wildcard = true;
01887       consumeToken();
01888       break;
01889     }
01890     
01891     Diags.Report(Tok.getLocation(), diag::err_mmap_module_id);
01892     HadError = true;
01893     return;
01894   } while (true);
01895   
01896   Module::UnresolvedExportDecl Unresolved = { 
01897     ExportLoc, ParsedModuleId, Wildcard 
01898   };
01899   ActiveModule->UnresolvedExports.push_back(Unresolved);
01900 }
01901 
01902 /// \brief Parse a module uses declaration.
01903 ///
01904 ///   uses-declaration:
01905 ///     'uses' wildcard-module-id
01906 void ModuleMapParser::parseUseDecl() {
01907   assert(Tok.is(MMToken::UseKeyword));
01908   consumeToken();
01909   // Parse the module-id.
01910   ModuleId ParsedModuleId;
01911   parseModuleId(ParsedModuleId);
01912 
01913   ActiveModule->UnresolvedDirectUses.push_back(ParsedModuleId);
01914 }
01915 
01916 /// \brief Parse a link declaration.
01917 ///
01918 ///   module-declaration:
01919 ///     'link' 'framework'[opt] string-literal
01920 void ModuleMapParser::parseLinkDecl() {
01921   assert(Tok.is(MMToken::LinkKeyword));
01922   SourceLocation LinkLoc = consumeToken();
01923 
01924   // Parse the optional 'framework' keyword.
01925   bool IsFramework = false;
01926   if (Tok.is(MMToken::FrameworkKeyword)) {
01927     consumeToken();
01928     IsFramework = true;
01929   }
01930 
01931   // Parse the library name
01932   if (!Tok.is(MMToken::StringLiteral)) {
01933     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_library_name)
01934       << IsFramework << SourceRange(LinkLoc);
01935     HadError = true;
01936     return;
01937   }
01938 
01939   std::string LibraryName = Tok.getString();
01940   consumeToken();
01941   ActiveModule->LinkLibraries.push_back(Module::LinkLibrary(LibraryName,
01942                                                             IsFramework));
01943 }
01944 
01945 /// \brief Parse a configuration macro declaration.
01946 ///
01947 ///   module-declaration:
01948 ///     'config_macros' attributes[opt] config-macro-list?
01949 ///
01950 ///   config-macro-list:
01951 ///     identifier (',' identifier)?
01952 void ModuleMapParser::parseConfigMacros() {
01953   assert(Tok.is(MMToken::ConfigMacros));
01954   SourceLocation ConfigMacrosLoc = consumeToken();
01955 
01956   // Only top-level modules can have configuration macros.
01957   if (ActiveModule->Parent) {
01958     Diags.Report(ConfigMacrosLoc, diag::err_mmap_config_macro_submodule);
01959   }
01960 
01961   // Parse the optional attributes.
01962   Attributes Attrs;
01963   parseOptionalAttributes(Attrs);
01964   if (Attrs.IsExhaustive && !ActiveModule->Parent) {
01965     ActiveModule->ConfigMacrosExhaustive = true;
01966   }
01967 
01968   // If we don't have an identifier, we're done.
01969   // FIXME: Support macros with the same name as a keyword here.
01970   if (!Tok.is(MMToken::Identifier))
01971     return;
01972 
01973   // Consume the first identifier.
01974   if (!ActiveModule->Parent) {
01975     ActiveModule->ConfigMacros.push_back(Tok.getString().str());
01976   }
01977   consumeToken();
01978 
01979   do {
01980     // If there's a comma, consume it.
01981     if (!Tok.is(MMToken::Comma))
01982       break;
01983     consumeToken();
01984 
01985     // We expect to see a macro name here.
01986     // FIXME: Support macros with the same name as a keyword here.
01987     if (!Tok.is(MMToken::Identifier)) {
01988       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_config_macro);
01989       break;
01990     }
01991 
01992     // Consume the macro name.
01993     if (!ActiveModule->Parent) {
01994       ActiveModule->ConfigMacros.push_back(Tok.getString().str());
01995     }
01996     consumeToken();
01997   } while (true);
01998 }
01999 
02000 /// \brief Format a module-id into a string.
02001 static std::string formatModuleId(const ModuleId &Id) {
02002   std::string result;
02003   {
02004     llvm::raw_string_ostream OS(result);
02005 
02006     for (unsigned I = 0, N = Id.size(); I != N; ++I) {
02007       if (I)
02008         OS << ".";
02009       OS << Id[I].first;
02010     }
02011   }
02012 
02013   return result;
02014 }
02015 
02016 /// \brief Parse a conflict declaration.
02017 ///
02018 ///   module-declaration:
02019 ///     'conflict' module-id ',' string-literal
02020 void ModuleMapParser::parseConflict() {
02021   assert(Tok.is(MMToken::Conflict));
02022   SourceLocation ConflictLoc = consumeToken();
02023   Module::UnresolvedConflict Conflict;
02024 
02025   // Parse the module-id.
02026   if (parseModuleId(Conflict.Id))
02027     return;
02028 
02029   // Parse the ','.
02030   if (!Tok.is(MMToken::Comma)) {
02031     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_comma)
02032       << SourceRange(ConflictLoc);
02033     return;
02034   }
02035   consumeToken();
02036 
02037   // Parse the message.
02038   if (!Tok.is(MMToken::StringLiteral)) {
02039     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_conflicts_message)
02040       << formatModuleId(Conflict.Id);
02041     return;
02042   }
02043   Conflict.Message = Tok.getString().str();
02044   consumeToken();
02045 
02046   // Add this unresolved conflict.
02047   ActiveModule->UnresolvedConflicts.push_back(Conflict);
02048 }
02049 
02050 /// \brief Parse an inferred module declaration (wildcard modules).
02051 ///
02052 ///   module-declaration:
02053 ///     'explicit'[opt] 'framework'[opt] 'module' * attributes[opt]
02054 ///       { inferred-module-member* }
02055 ///
02056 ///   inferred-module-member:
02057 ///     'export' '*'
02058 ///     'exclude' identifier
02059 void ModuleMapParser::parseInferredModuleDecl(bool Framework, bool Explicit) {
02060   assert(Tok.is(MMToken::Star));
02061   SourceLocation StarLoc = consumeToken();
02062   bool Failed = false;
02063 
02064   // Inferred modules must be submodules.
02065   if (!ActiveModule && !Framework) {
02066     Diags.Report(StarLoc, diag::err_mmap_top_level_inferred_submodule);
02067     Failed = true;
02068   }
02069 
02070   if (ActiveModule) {
02071     // Inferred modules must have umbrella directories.
02072     if (!Failed && ActiveModule->IsAvailable &&
02073         !ActiveModule->getUmbrellaDir()) {
02074       Diags.Report(StarLoc, diag::err_mmap_inferred_no_umbrella);
02075       Failed = true;
02076     }
02077     
02078     // Check for redefinition of an inferred module.
02079     if (!Failed && ActiveModule->InferSubmodules) {
02080       Diags.Report(StarLoc, diag::err_mmap_inferred_redef);
02081       if (ActiveModule->InferredSubmoduleLoc.isValid())
02082         Diags.Report(ActiveModule->InferredSubmoduleLoc,
02083                      diag::note_mmap_prev_definition);
02084       Failed = true;
02085     }
02086 
02087     // Check for the 'framework' keyword, which is not permitted here.
02088     if (Framework) {
02089       Diags.Report(StarLoc, diag::err_mmap_inferred_framework_submodule);
02090       Framework = false;
02091     }
02092   } else if (Explicit) {
02093     Diags.Report(StarLoc, diag::err_mmap_explicit_inferred_framework);
02094     Explicit = false;
02095   }
02096 
02097   // If there were any problems with this inferred submodule, skip its body.
02098   if (Failed) {
02099     if (Tok.is(MMToken::LBrace)) {
02100       consumeToken();
02101       skipUntil(MMToken::RBrace);
02102       if (Tok.is(MMToken::RBrace))
02103         consumeToken();
02104     }
02105     HadError = true;
02106     return;
02107   }
02108 
02109   // Parse optional attributes.
02110   Attributes Attrs;
02111   parseOptionalAttributes(Attrs);
02112 
02113   if (ActiveModule) {
02114     // Note that we have an inferred submodule.
02115     ActiveModule->InferSubmodules = true;
02116     ActiveModule->InferredSubmoduleLoc = StarLoc;
02117     ActiveModule->InferExplicitSubmodules = Explicit;
02118   } else {
02119     // We'll be inferring framework modules for this directory.
02120     Map.InferredDirectories[Directory].InferModules = true;
02121     Map.InferredDirectories[Directory].InferSystemModules = Attrs.IsSystem;
02122     Map.InferredDirectories[Directory].ModuleMapFile = ModuleMapFile;
02123     // FIXME: Handle the 'framework' keyword.
02124   }
02125 
02126   // Parse the opening brace.
02127   if (!Tok.is(MMToken::LBrace)) {
02128     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_lbrace_wildcard);
02129     HadError = true;
02130     return;
02131   }  
02132   SourceLocation LBraceLoc = consumeToken();
02133 
02134   // Parse the body of the inferred submodule.
02135   bool Done = false;
02136   do {
02137     switch (Tok.Kind) {
02138     case MMToken::EndOfFile:
02139     case MMToken::RBrace:
02140       Done = true;
02141       break;
02142 
02143     case MMToken::ExcludeKeyword: {
02144       if (ActiveModule) {
02145         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
02146           << (ActiveModule != nullptr);
02147         consumeToken();
02148         break;
02149       }
02150 
02151       consumeToken();
02152       // FIXME: Support string-literal module names here.
02153       if (!Tok.is(MMToken::Identifier)) {
02154         Diags.Report(Tok.getLocation(), diag::err_mmap_missing_exclude_name);
02155         break;
02156       }
02157 
02158       Map.InferredDirectories[Directory].ExcludedModules
02159         .push_back(Tok.getString());
02160       consumeToken();
02161       break;
02162     }
02163 
02164     case MMToken::ExportKeyword:
02165       if (!ActiveModule) {
02166         Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
02167           << (ActiveModule != nullptr);
02168         consumeToken();
02169         break;
02170       }
02171 
02172       consumeToken();
02173       if (Tok.is(MMToken::Star)) 
02174         ActiveModule->InferExportWildcard = true;
02175       else
02176         Diags.Report(Tok.getLocation(), 
02177                      diag::err_mmap_expected_export_wildcard);
02178       consumeToken();
02179       break;
02180 
02181     case MMToken::ExplicitKeyword:
02182     case MMToken::ModuleKeyword:
02183     case MMToken::HeaderKeyword:
02184     case MMToken::PrivateKeyword:
02185     case MMToken::UmbrellaKeyword:
02186     default:
02187       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_inferred_member)
02188           << (ActiveModule != nullptr);
02189       consumeToken();
02190       break;        
02191     }
02192   } while (!Done);
02193   
02194   if (Tok.is(MMToken::RBrace))
02195     consumeToken();
02196   else {
02197     Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rbrace);
02198     Diags.Report(LBraceLoc, diag::note_mmap_lbrace_match);
02199     HadError = true;
02200   }
02201 }
02202 
02203 /// \brief Parse optional attributes.
02204 ///
02205 ///   attributes:
02206 ///     attribute attributes
02207 ///     attribute
02208 ///
02209 ///   attribute:
02210 ///     [ identifier ]
02211 ///
02212 /// \param Attrs Will be filled in with the parsed attributes.
02213 ///
02214 /// \returns true if an error occurred, false otherwise.
02215 bool ModuleMapParser::parseOptionalAttributes(Attributes &Attrs) {
02216   bool HadError = false;
02217   
02218   while (Tok.is(MMToken::LSquare)) {
02219     // Consume the '['.
02220     SourceLocation LSquareLoc = consumeToken();
02221 
02222     // Check whether we have an attribute name here.
02223     if (!Tok.is(MMToken::Identifier)) {
02224       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_attribute);
02225       skipUntil(MMToken::RSquare);
02226       if (Tok.is(MMToken::RSquare))
02227         consumeToken();
02228       HadError = true;
02229     }
02230 
02231     // Decode the attribute name.
02232     AttributeKind Attribute
02233       = llvm::StringSwitch<AttributeKind>(Tok.getString())
02234           .Case("exhaustive", AT_exhaustive)
02235           .Case("extern_c", AT_extern_c)
02236           .Case("system", AT_system)
02237           .Default(AT_unknown);
02238     switch (Attribute) {
02239     case AT_unknown:
02240       Diags.Report(Tok.getLocation(), diag::warn_mmap_unknown_attribute)
02241         << Tok.getString();
02242       break;
02243 
02244     case AT_system:
02245       Attrs.IsSystem = true;
02246       break;
02247 
02248     case AT_extern_c:
02249       Attrs.IsExternC = true;
02250       break;
02251 
02252     case AT_exhaustive:
02253       Attrs.IsExhaustive = true;
02254       break;
02255     }
02256     consumeToken();
02257 
02258     // Consume the ']'.
02259     if (!Tok.is(MMToken::RSquare)) {
02260       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_rsquare);
02261       Diags.Report(LSquareLoc, diag::note_mmap_lsquare_match);
02262       skipUntil(MMToken::RSquare);
02263       HadError = true;
02264     }
02265 
02266     if (Tok.is(MMToken::RSquare))
02267       consumeToken();
02268   }
02269 
02270   return HadError;
02271 }
02272 
02273 /// \brief Parse a module map file.
02274 ///
02275 ///   module-map-file:
02276 ///     module-declaration*
02277 bool ModuleMapParser::parseModuleMapFile() {
02278   do {
02279     switch (Tok.Kind) {
02280     case MMToken::EndOfFile:
02281       return HadError;
02282       
02283     case MMToken::ExplicitKeyword:
02284     case MMToken::ExternKeyword:
02285     case MMToken::ModuleKeyword:
02286     case MMToken::FrameworkKeyword:
02287       parseModuleDecl();
02288       break;
02289 
02290     case MMToken::Comma:
02291     case MMToken::ConfigMacros:
02292     case MMToken::Conflict:
02293     case MMToken::Exclaim:
02294     case MMToken::ExcludeKeyword:
02295     case MMToken::ExportKeyword:
02296     case MMToken::HeaderKeyword:
02297     case MMToken::Identifier:
02298     case MMToken::LBrace:
02299     case MMToken::LinkKeyword:
02300     case MMToken::LSquare:
02301     case MMToken::Period:
02302     case MMToken::PrivateKeyword:
02303     case MMToken::RBrace:
02304     case MMToken::RSquare:
02305     case MMToken::RequiresKeyword:
02306     case MMToken::Star:
02307     case MMToken::StringLiteral:
02308     case MMToken::TextualKeyword:
02309     case MMToken::UmbrellaKeyword:
02310     case MMToken::UseKeyword:
02311       Diags.Report(Tok.getLocation(), diag::err_mmap_expected_module);
02312       HadError = true;
02313       consumeToken();
02314       break;
02315     }
02316   } while (true);
02317 }
02318 
02319 bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem) {
02320   llvm::DenseMap<const FileEntry *, bool>::iterator Known
02321     = ParsedModuleMap.find(File);
02322   if (Known != ParsedModuleMap.end())
02323     return Known->second;
02324 
02325   assert(Target && "Missing target information");
02326   auto FileCharacter = IsSystem ? SrcMgr::C_System : SrcMgr::C_User;
02327   FileID ID = SourceMgr.createFileID(File, SourceLocation(), FileCharacter);
02328   const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(ID);
02329   if (!Buffer)
02330     return ParsedModuleMap[File] = true;
02331 
02332   // Find the directory for the module. For frameworks, that may require going
02333   // up from the 'Modules' directory.
02334   const DirectoryEntry *Dir = File->getDir();
02335   StringRef DirName(Dir->getName());
02336   if (llvm::sys::path::filename(DirName) == "Modules") {
02337     DirName = llvm::sys::path::parent_path(DirName);
02338     if (DirName.endswith(".framework"))
02339       Dir = SourceMgr.getFileManager().getDirectory(DirName);
02340     assert(Dir && "parent must exist");
02341   }
02342   
02343   // Parse this module map file.
02344   Lexer L(ID, SourceMgr.getBuffer(ID), SourceMgr, MMapLangOpts);
02345   ModuleMapParser Parser(L, SourceMgr, Target, Diags, *this, File, Dir,
02346                          BuiltinIncludeDir, IsSystem);
02347   bool Result = Parser.parseModuleMapFile();
02348   ParsedModuleMap[File] = Result;
02349   return Result;
02350 }