Home | History | Annotate | Download | only in Lex
      1 //===--- HeaderSearch.h - Resolve Header File Locations ---------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the HeaderSearch interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_LEX_HEADERSEARCH_H
     15 #define LLVM_CLANG_LEX_HEADERSEARCH_H
     16 
     17 #include "clang/Lex/DirectoryLookup.h"
     18 #include "clang/Lex/ModuleMap.h"
     19 #include "llvm/ADT/ArrayRef.h"
     20 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     21 #include "llvm/ADT/StringMap.h"
     22 #include "llvm/ADT/StringSet.h"
     23 #include "llvm/Support/Allocator.h"
     24 #include <memory>
     25 #include <vector>
     26 
     27 namespace clang {
     28 
     29 class DiagnosticsEngine;
     30 class ExternalPreprocessorSource;
     31 class FileEntry;
     32 class FileManager;
     33 class HeaderSearchOptions;
     34 class IdentifierInfo;
     35 class Preprocessor;
     36 
     37 /// \brief The preprocessor keeps track of this information for each
     38 /// file that is \#included.
     39 struct HeaderFileInfo {
     40   /// \brief True if this is a \#import'd or \#pragma once file.
     41   unsigned isImport : 1;
     42 
     43   /// \brief True if this is a \#pragma once file.
     44   unsigned isPragmaOnce : 1;
     45 
     46   /// DirInfo - Keep track of whether this is a system header, and if so,
     47   /// whether it is C++ clean or not.  This can be set by the include paths or
     48   /// by \#pragma gcc system_header.  This is an instance of
     49   /// SrcMgr::CharacteristicKind.
     50   unsigned DirInfo : 2;
     51 
     52   /// \brief Whether this header file info was supplied by an external source,
     53   /// and has not changed since.
     54   unsigned External : 1;
     55 
     56   /// \brief Whether this header is part of a module.
     57   unsigned isModuleHeader : 1;
     58 
     59   /// \brief Whether this header is part of the module that we are building.
     60   unsigned isCompilingModuleHeader : 1;
     61 
     62   /// \brief Whether this structure is considered to already have been
     63   /// "resolved", meaning that it was loaded from the external source.
     64   unsigned Resolved : 1;
     65 
     66   /// \brief Whether this is a header inside a framework that is currently
     67   /// being built.
     68   ///
     69   /// When a framework is being built, the headers have not yet been placed
     70   /// into the appropriate framework subdirectories, and therefore are
     71   /// provided via a header map. This bit indicates when this is one of
     72   /// those framework headers.
     73   unsigned IndexHeaderMapHeader : 1;
     74 
     75   /// \brief Whether this file has been looked up as a header.
     76   unsigned IsValid : 1;
     77 
     78   /// \brief The number of times the file has been included already.
     79   unsigned short NumIncludes;
     80 
     81   /// \brief The ID number of the controlling macro.
     82   ///
     83   /// This ID number will be non-zero when there is a controlling
     84   /// macro whose IdentifierInfo may not yet have been loaded from
     85   /// external storage.
     86   unsigned ControllingMacroID;
     87 
     88   /// If this file has a \#ifndef XXX (or equivalent) guard that
     89   /// protects the entire contents of the file, this is the identifier
     90   /// for the macro that controls whether or not it has any effect.
     91   ///
     92   /// Note: Most clients should use getControllingMacro() to access
     93   /// the controlling macro of this header, since
     94   /// getControllingMacro() is able to load a controlling macro from
     95   /// external storage.
     96   const IdentifierInfo *ControllingMacro;
     97 
     98   /// \brief If this header came from a framework include, this is the name
     99   /// of the framework.
    100   StringRef Framework;
    101 
    102   HeaderFileInfo()
    103     : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
    104       External(false), isModuleHeader(false), isCompilingModuleHeader(false),
    105       Resolved(false), IndexHeaderMapHeader(false), IsValid(0),
    106       NumIncludes(0), ControllingMacroID(0), ControllingMacro(nullptr)  {}
    107 
    108   /// \brief Retrieve the controlling macro for this header file, if
    109   /// any.
    110   const IdentifierInfo *
    111   getControllingMacro(ExternalPreprocessorSource *External);
    112 
    113   /// \brief Determine whether this is a non-default header file info, e.g.,
    114   /// it corresponds to an actual header we've included or tried to include.
    115   bool isNonDefault() const {
    116     return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
    117       ControllingMacroID;
    118   }
    119 };
    120 
    121 /// \brief An external source of header file information, which may supply
    122 /// information about header files already included.
    123 class ExternalHeaderFileInfoSource {
    124 public:
    125   virtual ~ExternalHeaderFileInfoSource();
    126 
    127   /// \brief Retrieve the header file information for the given file entry.
    128   ///
    129   /// \returns Header file information for the given file entry, with the
    130   /// \c External bit set. If the file entry is not known, return a
    131   /// default-constructed \c HeaderFileInfo.
    132   virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
    133 };
    134 
    135 /// \brief Encapsulates the information needed to find the file referenced
    136 /// by a \#include or \#include_next, (sub-)framework lookup, etc.
    137 class HeaderSearch {
    138   /// This structure is used to record entries in our framework cache.
    139   struct FrameworkCacheEntry {
    140     /// The directory entry which should be used for the cached framework.
    141     const DirectoryEntry *Directory;
    142 
    143     /// Whether this framework has been "user-specified" to be treated as if it
    144     /// were a system framework (even if it was found outside a system framework
    145     /// directory).
    146     bool IsUserSpecifiedSystemFramework;
    147   };
    148 
    149   /// \brief Header-search options used to initialize this header search.
    150   std::shared_ptr<HeaderSearchOptions> HSOpts;
    151 
    152   DiagnosticsEngine &Diags;
    153   FileManager &FileMgr;
    154   /// \#include search path information.  Requests for \#include "x" search the
    155   /// directory of the \#including file first, then each directory in SearchDirs
    156   /// consecutively. Requests for <x> search the current dir first, then each
    157   /// directory in SearchDirs, starting at AngledDirIdx, consecutively.  If
    158   /// NoCurDirSearch is true, then the check for the file in the current
    159   /// directory is suppressed.
    160   std::vector<DirectoryLookup> SearchDirs;
    161   unsigned AngledDirIdx;
    162   unsigned SystemDirIdx;
    163   bool NoCurDirSearch;
    164 
    165   /// \brief \#include prefixes for which the 'system header' property is
    166   /// overridden.
    167   ///
    168   /// For a \#include "x" or \#include \<x> directive, the last string in this
    169   /// list which is a prefix of 'x' determines whether the file is treated as
    170   /// a system header.
    171   std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
    172 
    173   /// \brief The path to the module cache.
    174   std::string ModuleCachePath;
    175 
    176   /// \brief All of the preprocessor-specific data about files that are
    177   /// included, indexed by the FileEntry's UID.
    178   mutable std::vector<HeaderFileInfo> FileInfo;
    179 
    180   /// Keeps track of each lookup performed by LookupFile.
    181   struct LookupFileCacheInfo {
    182     /// Starting index in SearchDirs that the cached search was performed from.
    183     /// If there is a hit and this value doesn't match the current query, the
    184     /// cache has to be ignored.
    185     unsigned StartIdx;
    186     /// The entry in SearchDirs that satisfied the query.
    187     unsigned HitIdx;
    188     /// This is non-null if the original filename was mapped to a framework
    189     /// include via a headermap.
    190     const char *MappedName;
    191 
    192     /// Default constructor -- Initialize all members with zero.
    193     LookupFileCacheInfo(): StartIdx(0), HitIdx(0), MappedName(nullptr) {}
    194 
    195     void reset(unsigned StartIdx) {
    196       this->StartIdx = StartIdx;
    197       this->MappedName = nullptr;
    198     }
    199   };
    200   llvm::StringMap<LookupFileCacheInfo, llvm::BumpPtrAllocator> LookupFileCache;
    201 
    202   /// \brief Collection mapping a framework or subframework
    203   /// name like "Carbon" to the Carbon.framework directory.
    204   llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
    205 
    206   /// IncludeAliases - maps include file names (including the quotes or
    207   /// angle brackets) to other include file names.  This is used to support the
    208   /// include_alias pragma for Microsoft compatibility.
    209   typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator>
    210     IncludeAliasMap;
    211   std::unique_ptr<IncludeAliasMap> IncludeAliases;
    212 
    213   /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
    214   /// headermaps.  This vector owns the headermap.
    215   std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
    216 
    217   /// \brief The mapping between modules and headers.
    218   mutable ModuleMap ModMap;
    219 
    220   /// \brief Describes whether a given directory has a module map in it.
    221   llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
    222 
    223   /// \brief Set of module map files we've already loaded, and a flag indicating
    224   /// whether they were valid or not.
    225   llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
    226 
    227   /// \brief Uniqued set of framework names, which is used to track which
    228   /// headers were included as framework headers.
    229   llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
    230 
    231   /// \brief Entity used to resolve the identifier IDs of controlling
    232   /// macros into IdentifierInfo pointers, and keep the identifire up to date,
    233   /// as needed.
    234   ExternalPreprocessorSource *ExternalLookup;
    235 
    236   /// \brief Entity used to look up stored header file information.
    237   ExternalHeaderFileInfoSource *ExternalSource;
    238 
    239   // Various statistics we track for performance analysis.
    240   unsigned NumIncluded;
    241   unsigned NumMultiIncludeFileOptzn;
    242   unsigned NumFrameworkLookups, NumSubFrameworkLookups;
    243 
    244   // HeaderSearch doesn't support default or copy construction.
    245   HeaderSearch(const HeaderSearch&) = delete;
    246   void operator=(const HeaderSearch&) = delete;
    247 
    248   friend class DirectoryLookup;
    249 
    250 public:
    251   HeaderSearch(std::shared_ptr<HeaderSearchOptions> HSOpts,
    252                SourceManager &SourceMgr, DiagnosticsEngine &Diags,
    253                const LangOptions &LangOpts, const TargetInfo *Target);
    254   ~HeaderSearch();
    255 
    256   /// \brief Retrieve the header-search options with which this header search
    257   /// was initialized.
    258   HeaderSearchOptions &getHeaderSearchOpts() const { return *HSOpts; }
    259 
    260   FileManager &getFileMgr() const { return FileMgr; }
    261 
    262   /// \brief Interface for setting the file search paths.
    263   void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
    264                       unsigned angledDirIdx, unsigned systemDirIdx,
    265                       bool noCurDirSearch) {
    266     assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
    267         "Directory indicies are unordered");
    268     SearchDirs = dirs;
    269     AngledDirIdx = angledDirIdx;
    270     SystemDirIdx = systemDirIdx;
    271     NoCurDirSearch = noCurDirSearch;
    272     //LookupFileCache.clear();
    273   }
    274 
    275   /// \brief Add an additional search path.
    276   void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
    277     unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
    278     SearchDirs.insert(SearchDirs.begin() + idx, dir);
    279     if (!isAngled)
    280       AngledDirIdx++;
    281     SystemDirIdx++;
    282   }
    283 
    284   /// \brief Set the list of system header prefixes.
    285   void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) {
    286     SystemHeaderPrefixes.assign(P.begin(), P.end());
    287   }
    288 
    289   /// \brief Checks whether the map exists or not.
    290   bool HasIncludeAliasMap() const { return (bool)IncludeAliases; }
    291 
    292   /// \brief Map the source include name to the dest include name.
    293   ///
    294   /// The Source should include the angle brackets or quotes, the dest
    295   /// should not.  This allows for distinction between <> and "" headers.
    296   void AddIncludeAlias(StringRef Source, StringRef Dest) {
    297     if (!IncludeAliases)
    298       IncludeAliases.reset(new IncludeAliasMap);
    299     (*IncludeAliases)[Source] = Dest;
    300   }
    301 
    302   /// MapHeaderToIncludeAlias - Maps one header file name to a different header
    303   /// file name, for use with the include_alias pragma.  Note that the source
    304   /// file name should include the angle brackets or quotes.  Returns StringRef
    305   /// as null if the header cannot be mapped.
    306   StringRef MapHeaderToIncludeAlias(StringRef Source) {
    307     assert(IncludeAliases && "Trying to map headers when there's no map");
    308 
    309     // Do any filename replacements before anything else
    310     IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source);
    311     if (Iter != IncludeAliases->end())
    312       return Iter->second;
    313     return StringRef();
    314   }
    315 
    316   /// \brief Set the path to the module cache.
    317   void setModuleCachePath(StringRef CachePath) {
    318     ModuleCachePath = CachePath;
    319   }
    320 
    321   /// \brief Retrieve the path to the module cache.
    322   StringRef getModuleCachePath() const { return ModuleCachePath; }
    323 
    324   /// \brief Consider modules when including files from this directory.
    325   void setDirectoryHasModuleMap(const DirectoryEntry* Dir) {
    326     DirectoryHasModuleMap[Dir] = true;
    327   }
    328 
    329   /// \brief Forget everything we know about headers so far.
    330   void ClearFileInfo() {
    331     FileInfo.clear();
    332   }
    333 
    334   void SetExternalLookup(ExternalPreprocessorSource *EPS) {
    335     ExternalLookup = EPS;
    336   }
    337 
    338   ExternalPreprocessorSource *getExternalLookup() const {
    339     return ExternalLookup;
    340   }
    341 
    342   /// \brief Set the external source of header information.
    343   void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
    344     ExternalSource = ES;
    345   }
    346 
    347   /// \brief Set the target information for the header search, if not
    348   /// already known.
    349   void setTarget(const TargetInfo &Target);
    350 
    351   /// \brief Given a "foo" or \<foo> reference, look up the indicated file,
    352   /// return null on failure.
    353   ///
    354   /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
    355   /// the file was found in, or null if not applicable.
    356   ///
    357   /// \param IncludeLoc Used for diagnostics if valid.
    358   ///
    359   /// \param isAngled indicates whether the file reference is a <> reference.
    360   ///
    361   /// \param CurDir If non-null, the file was found in the specified directory
    362   /// search location.  This is used to implement \#include_next.
    363   ///
    364   /// \param Includers Indicates where the \#including file(s) are, in case
    365   /// relative searches are needed. In reverse order of inclusion.
    366   ///
    367   /// \param SearchPath If non-null, will be set to the search path relative
    368   /// to which the file was found. If the include path is absolute, SearchPath
    369   /// will be set to an empty string.
    370   ///
    371   /// \param RelativePath If non-null, will be set to the path relative to
    372   /// SearchPath at which the file was found. This only differs from the
    373   /// Filename for framework includes.
    374   ///
    375   /// \param SuggestedModule If non-null, and the file found is semantically
    376   /// part of a known module, this will be set to the module that should
    377   /// be imported instead of preprocessing/parsing the file found.
    378   const FileEntry *LookupFile(
    379       StringRef Filename, SourceLocation IncludeLoc, bool isAngled,
    380       const DirectoryLookup *FromDir, const DirectoryLookup *&CurDir,
    381       ArrayRef<std::pair<const FileEntry *, const DirectoryEntry *>> Includers,
    382       SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
    383       Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
    384       bool SkipCache = false, bool BuildSystemModule = false);
    385 
    386   /// \brief Look up a subframework for the specified \#include file.
    387   ///
    388   /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
    389   /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
    390   /// HIToolbox is a subframework within Carbon.framework.  If so, return
    391   /// the FileEntry for the designated file, otherwise return null.
    392   const FileEntry *LookupSubframeworkHeader(
    393       StringRef Filename, const FileEntry *RelativeFileEnt,
    394       SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
    395       Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule);
    396 
    397   /// \brief Look up the specified framework name in our framework cache.
    398   /// \returns The DirectoryEntry it is in if we know, null otherwise.
    399   FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
    400     return FrameworkMap[FWName];
    401   }
    402 
    403   /// \brief Mark the specified file as a target of of a \#include,
    404   /// \#include_next, or \#import directive.
    405   ///
    406   /// \return false if \#including the file will have no effect or true
    407   /// if we should include it.
    408   bool ShouldEnterIncludeFile(Preprocessor &PP, const FileEntry *File,
    409                               bool isImport, bool ModulesEnabled,
    410                               Module *CorrespondingModule);
    411 
    412   /// \brief Return whether the specified file is a normal header,
    413   /// a system header, or a C++ friendly system header.
    414   SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
    415     return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
    416   }
    417 
    418   /// \brief Mark the specified file as a "once only" file, e.g. due to
    419   /// \#pragma once.
    420   void MarkFileIncludeOnce(const FileEntry *File) {
    421     HeaderFileInfo &FI = getFileInfo(File);
    422     FI.isImport = true;
    423     FI.isPragmaOnce = true;
    424   }
    425 
    426   /// \brief Mark the specified file as a system header, e.g. due to
    427   /// \#pragma GCC system_header.
    428   void MarkFileSystemHeader(const FileEntry *File) {
    429     getFileInfo(File).DirInfo = SrcMgr::C_System;
    430   }
    431 
    432   /// \brief Mark the specified file as part of a module.
    433   void MarkFileModuleHeader(const FileEntry *File,
    434                             ModuleMap::ModuleHeaderRole Role,
    435                             bool IsCompiledModuleHeader);
    436 
    437   /// \brief Increment the count for the number of times the specified
    438   /// FileEntry has been entered.
    439   void IncrementIncludeCount(const FileEntry *File) {
    440     ++getFileInfo(File).NumIncludes;
    441   }
    442 
    443   /// \brief Mark the specified file as having a controlling macro.
    444   ///
    445   /// This is used by the multiple-include optimization to eliminate
    446   /// no-op \#includes.
    447   void SetFileControllingMacro(const FileEntry *File,
    448                                const IdentifierInfo *ControllingMacro) {
    449     getFileInfo(File).ControllingMacro = ControllingMacro;
    450   }
    451 
    452   /// \brief Return true if this is the first time encountering this header.
    453   bool FirstTimeLexingFile(const FileEntry *File) {
    454     return getFileInfo(File).NumIncludes == 1;
    455   }
    456 
    457   /// \brief Determine whether this file is intended to be safe from
    458   /// multiple inclusions, e.g., it has \#pragma once or a controlling
    459   /// macro.
    460   ///
    461   /// This routine does not consider the effect of \#import
    462   bool isFileMultipleIncludeGuarded(const FileEntry *File);
    463 
    464   /// CreateHeaderMap - This method returns a HeaderMap for the specified
    465   /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
    466   const HeaderMap *CreateHeaderMap(const FileEntry *FE);
    467 
    468   /// \brief Get filenames for all registered header maps.
    469   void getHeaderMapFileNames(SmallVectorImpl<std::string> &Names) const;
    470 
    471   /// \brief Retrieve the name of the module file that should be used to
    472   /// load the given module.
    473   ///
    474   /// \param Module The module whose module file name will be returned.
    475   ///
    476   /// \returns The name of the module file that corresponds to this module,
    477   /// or an empty string if this module does not correspond to any module file.
    478   std::string getModuleFileName(Module *Module);
    479 
    480   /// \brief Retrieve the name of the module file that should be used to
    481   /// load a module with the given name.
    482   ///
    483   /// \param ModuleName The module whose module file name will be returned.
    484   ///
    485   /// \param ModuleMapPath A path that when combined with \c ModuleName
    486   /// uniquely identifies this module. See Module::ModuleMap.
    487   ///
    488   /// \param UsePrebuiltPath Whether we should use the prebuilt module path.
    489   ///
    490   /// \returns The name of the module file that corresponds to this module,
    491   /// or an empty string if this module does not correspond to any module file.
    492   std::string getModuleFileName(StringRef ModuleName, StringRef ModuleMapPath,
    493                                 bool UsePrebuiltPath);
    494 
    495   /// \brief Lookup a module Search for a module with the given name.
    496   ///
    497   /// \param ModuleName The name of the module we're looking for.
    498   ///
    499   /// \param AllowSearch Whether we are allowed to search in the various
    500   /// search directories to produce a module definition. If not, this lookup
    501   /// will only return an already-known module.
    502   ///
    503   /// \returns The module with the given name.
    504   Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
    505 
    506   /// \brief Try to find a module map file in the given directory, returning
    507   /// \c nullptr if none is found.
    508   const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
    509                                        bool IsFramework);
    510 
    511   void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
    512 
    513   /// \brief Determine whether there is a module map that may map the header
    514   /// with the given file name to a (sub)module.
    515   /// Always returns false if modules are disabled.
    516   ///
    517   /// \param Filename The name of the file.
    518   ///
    519   /// \param Root The "root" directory, at which we should stop looking for
    520   /// module maps.
    521   ///
    522   /// \param IsSystem Whether the directories we're looking at are system
    523   /// header directories.
    524   bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root,
    525                     bool IsSystem);
    526 
    527   /// \brief Retrieve the module that corresponds to the given file, if any.
    528   ///
    529   /// \param File The header that we wish to map to a module.
    530   /// \param AllowTextual Whether we want to find textual headers too.
    531   ModuleMap::KnownHeader findModuleForHeader(const FileEntry *File,
    532                                              bool AllowTextual = false) const;
    533 
    534   /// \brief Read the contents of the given module map file.
    535   ///
    536   /// \param File The module map file.
    537   /// \param IsSystem Whether this file is in a system header directory.
    538   ///
    539   /// \returns true if an error occurred, false otherwise.
    540   bool loadModuleMapFile(const FileEntry *File, bool IsSystem);
    541 
    542   /// \brief Collect the set of all known, top-level modules.
    543   ///
    544   /// \param Modules Will be filled with the set of known, top-level modules.
    545   void collectAllModules(SmallVectorImpl<Module *> &Modules);
    546 
    547   /// \brief Load all known, top-level system modules.
    548   void loadTopLevelSystemModules();
    549 
    550 private:
    551 
    552   /// \brief Lookup a module with the given module name and search-name.
    553   ///
    554   /// \param ModuleName The name of the module we're looking for.
    555   ///
    556   /// \param SearchName The "search-name" to derive filesystem paths from
    557   /// when looking for the module map; this is usually equal to ModuleName,
    558   /// but for compatibility with some buggy frameworks, additional attempts
    559   /// may be made to find the module under a related-but-different search-name.
    560   ///
    561   /// \returns The module named ModuleName.
    562   Module *lookupModule(StringRef ModuleName, StringRef SearchName);
    563 
    564   /// \brief Retrieve a module with the given name, which may be part of the
    565   /// given framework.
    566   ///
    567   /// \param Name The name of the module to retrieve.
    568   ///
    569   /// \param Dir The framework directory (e.g., ModuleName.framework).
    570   ///
    571   /// \param IsSystem Whether the framework directory is part of the system
    572   /// frameworks.
    573   ///
    574   /// \returns The module, if found; otherwise, null.
    575   Module *loadFrameworkModule(StringRef Name,
    576                               const DirectoryEntry *Dir,
    577                               bool IsSystem);
    578 
    579   /// \brief Load all of the module maps within the immediate subdirectories
    580   /// of the given search directory.
    581   void loadSubdirectoryModuleMaps(DirectoryLookup &SearchDir);
    582 
    583   /// \brief Find and suggest a usable module for the given file.
    584   ///
    585   /// \return \c true if the file can be used, \c false if we are not permitted to
    586   ///         find this file due to requirements from \p RequestingModule.
    587   bool findUsableModuleForHeader(const FileEntry *File,
    588                                  const DirectoryEntry *Root,
    589                                  Module *RequestingModule,
    590                                  ModuleMap::KnownHeader *SuggestedModule,
    591                                  bool IsSystemHeaderDir);
    592 
    593   /// \brief Find and suggest a usable module for the given file, which is part of
    594   /// the specified framework.
    595   ///
    596   /// \return \c true if the file can be used, \c false if we are not permitted to
    597   ///         find this file due to requirements from \p RequestingModule.
    598   bool findUsableModuleForFrameworkHeader(
    599       const FileEntry *File, StringRef FrameworkDir, Module *RequestingModule,
    600       ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework);
    601 
    602   /// \brief Look up the file with the specified name and determine its owning
    603   /// module.
    604   const FileEntry *
    605   getFileAndSuggestModule(StringRef FileName, SourceLocation IncludeLoc,
    606                           const DirectoryEntry *Dir, bool IsSystemHeaderDir,
    607                           Module *RequestingModule,
    608                           ModuleMap::KnownHeader *SuggestedModule);
    609 
    610 public:
    611   /// \brief Retrieve the module map.
    612   ModuleMap &getModuleMap() { return ModMap; }
    613 
    614   /// \brief Retrieve the module map.
    615   const ModuleMap &getModuleMap() const { return ModMap; }
    616 
    617   unsigned header_file_size() const { return FileInfo.size(); }
    618 
    619   /// \brief Return the HeaderFileInfo structure for the specified FileEntry,
    620   /// in preparation for updating it in some way.
    621   HeaderFileInfo &getFileInfo(const FileEntry *FE);
    622 
    623   /// \brief Return the HeaderFileInfo structure for the specified FileEntry,
    624   /// if it has ever been filled in.
    625   /// \param WantExternal Whether the caller wants purely-external header file
    626   ///        info (where \p External is true).
    627   const HeaderFileInfo *getExistingFileInfo(const FileEntry *FE,
    628                                             bool WantExternal = true) const;
    629 
    630   // Used by external tools
    631   typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
    632   search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
    633   search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
    634   unsigned search_dir_size() const { return SearchDirs.size(); }
    635 
    636   search_dir_iterator quoted_dir_begin() const {
    637     return SearchDirs.begin();
    638   }
    639   search_dir_iterator quoted_dir_end() const {
    640     return SearchDirs.begin() + AngledDirIdx;
    641   }
    642 
    643   search_dir_iterator angled_dir_begin() const {
    644     return SearchDirs.begin() + AngledDirIdx;
    645   }
    646   search_dir_iterator angled_dir_end() const {
    647     return SearchDirs.begin() + SystemDirIdx;
    648   }
    649 
    650   search_dir_iterator system_dir_begin() const {
    651     return SearchDirs.begin() + SystemDirIdx;
    652   }
    653   search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
    654 
    655   /// \brief Retrieve a uniqued framework name.
    656   StringRef getUniqueFrameworkName(StringRef Framework);
    657 
    658   /// \brief Suggest a path by which the specified file could be found, for
    659   /// use in diagnostics to suggest a #include.
    660   ///
    661   /// \param IsSystem If non-null, filled in to indicate whether the suggested
    662   ///        path is relative to a system header directory.
    663   std::string suggestPathToFileForDiagnostics(const FileEntry *File,
    664                                               bool *IsSystem = nullptr);
    665 
    666   void PrintStats();
    667 
    668   size_t getTotalMemory() const;
    669 
    670 private:
    671   /// \brief Describes what happened when we tried to load a module map file.
    672   enum LoadModuleMapResult {
    673     /// \brief The module map file had already been loaded.
    674     LMM_AlreadyLoaded,
    675     /// \brief The module map file was loaded by this invocation.
    676     LMM_NewlyLoaded,
    677     /// \brief There is was directory with the given name.
    678     LMM_NoDirectory,
    679     /// \brief There was either no module map file or the module map file was
    680     /// invalid.
    681     LMM_InvalidModuleMap
    682   };
    683 
    684   LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
    685                                             bool IsSystem,
    686                                             const DirectoryEntry *Dir);
    687 
    688   /// \brief Try to load the module map file in the given directory.
    689   ///
    690   /// \param DirName The name of the directory where we will look for a module
    691   /// map file.
    692   /// \param IsSystem Whether this is a system header directory.
    693   /// \param IsFramework Whether this is a framework directory.
    694   ///
    695   /// \returns The result of attempting to load the module map file from the
    696   /// named directory.
    697   LoadModuleMapResult loadModuleMapFile(StringRef DirName, bool IsSystem,
    698                                         bool IsFramework);
    699 
    700   /// \brief Try to load the module map file in the given directory.
    701   ///
    702   /// \param Dir The directory where we will look for a module map file.
    703   /// \param IsSystem Whether this is a system header directory.
    704   /// \param IsFramework Whether this is a framework directory.
    705   ///
    706   /// \returns The result of attempting to load the module map file from the
    707   /// named directory.
    708   LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir,
    709                                         bool IsSystem, bool IsFramework);
    710 };
    711 
    712 }  // end namespace clang
    713 
    714 #endif
    715