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/StringMap.h"
     21 #include "llvm/ADT/StringSet.h"
     22 #include "llvm/Support/Allocator.h"
     23 #include "llvm/ADT/OwningPtr.h"
     24 #include <vector>
     25 
     26 namespace clang {
     27 
     28 class DiagnosticsEngine;
     29 class ExternalIdentifierLookup;
     30 class FileEntry;
     31 class FileManager;
     32 class IdentifierInfo;
     33 
     34 /// \brief The preprocessor keeps track of this information for each
     35 /// file that is \#included.
     36 struct HeaderFileInfo {
     37   /// \brief True if this is a \#import'd or \#pragma once file.
     38   unsigned isImport : 1;
     39 
     40   /// \brief True if this is a \#pragma once file.
     41   unsigned isPragmaOnce : 1;
     42 
     43   /// DirInfo - Keep track of whether this is a system header, and if so,
     44   /// whether it is C++ clean or not.  This can be set by the include paths or
     45   /// by \#pragma gcc system_header.  This is an instance of
     46   /// SrcMgr::CharacteristicKind.
     47   unsigned DirInfo : 2;
     48 
     49   /// \brief Whether this header file info was supplied by an external source.
     50   unsigned External : 1;
     51 
     52   /// \brief Whether this structure is considered to already have been
     53   /// "resolved", meaning that it was loaded from the external source.
     54   unsigned Resolved : 1;
     55 
     56   /// \brief Whether this is a header inside a framework that is currently
     57   /// being built.
     58   ///
     59   /// When a framework is being built, the headers have not yet been placed
     60   /// into the appropriate framework subdirectories, and therefore are
     61   /// provided via a header map. This bit indicates when this is one of
     62   /// those framework headers.
     63   unsigned IndexHeaderMapHeader : 1;
     64 
     65   /// \brief The number of times the file has been included already.
     66   unsigned short NumIncludes;
     67 
     68   /// \brief The ID number of the controlling macro.
     69   ///
     70   /// This ID number will be non-zero when there is a controlling
     71   /// macro whose IdentifierInfo may not yet have been loaded from
     72   /// external storage.
     73   unsigned ControllingMacroID;
     74 
     75   /// If this file has a \#ifndef XXX (or equivalent) guard that
     76   /// protects the entire contents of the file, this is the identifier
     77   /// for the macro that controls whether or not it has any effect.
     78   ///
     79   /// Note: Most clients should use getControllingMacro() to access
     80   /// the controlling macro of this header, since
     81   /// getControllingMacro() is able to load a controlling macro from
     82   /// external storage.
     83   const IdentifierInfo *ControllingMacro;
     84 
     85   /// \brief If this header came from a framework include, this is the name
     86   /// of the framework.
     87   StringRef Framework;
     88 
     89   HeaderFileInfo()
     90     : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User),
     91       External(false), Resolved(false), IndexHeaderMapHeader(false),
     92       NumIncludes(0), ControllingMacroID(0), ControllingMacro(0)  {}
     93 
     94   /// \brief Retrieve the controlling macro for this header file, if
     95   /// any.
     96   const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External);
     97 
     98   /// \brief Determine whether this is a non-default header file info, e.g.,
     99   /// it corresponds to an actual header we've included or tried to include.
    100   bool isNonDefault() const {
    101     return isImport || isPragmaOnce || NumIncludes || ControllingMacro ||
    102       ControllingMacroID;
    103   }
    104 };
    105 
    106 /// \brief An external source of header file information, which may supply
    107 /// information about header files already included.
    108 class ExternalHeaderFileInfoSource {
    109 public:
    110   virtual ~ExternalHeaderFileInfoSource();
    111 
    112   /// \brief Retrieve the header file information for the given file entry.
    113   ///
    114   /// \returns Header file information for the given file entry, with the
    115   /// \c External bit set. If the file entry is not known, return a
    116   /// default-constructed \c HeaderFileInfo.
    117   virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0;
    118 };
    119 
    120 /// \brief Encapsulates the information needed to find the file referenced
    121 /// by a \#include or \#include_next, (sub-)framework lookup, etc.
    122 class HeaderSearch {
    123   /// This structure is used to record entries in our framework cache.
    124   struct FrameworkCacheEntry {
    125     /// The directory entry which should be used for the cached framework.
    126     const DirectoryEntry *Directory;
    127 
    128     /// Whether this framework has been "user-specified" to be treated as if it
    129     /// were a system framework (even if it was found outside a system framework
    130     /// directory).
    131     bool IsUserSpecifiedSystemFramework;
    132   };
    133 
    134   FileManager &FileMgr;
    135   DiagnosticsEngine &Diags;
    136   /// \#include search path information.  Requests for \#include "x" search the
    137   /// directory of the \#including file first, then each directory in SearchDirs
    138   /// consecutively. Requests for <x> search the current dir first, then each
    139   /// directory in SearchDirs, starting at AngledDirIdx, consecutively.  If
    140   /// NoCurDirSearch is true, then the check for the file in the current
    141   /// directory is suppressed.
    142   std::vector<DirectoryLookup> SearchDirs;
    143   unsigned AngledDirIdx;
    144   unsigned SystemDirIdx;
    145   bool NoCurDirSearch;
    146 
    147   /// \brief \#include prefixes for which the 'system header' property is
    148   /// overridden.
    149   ///
    150   /// For a \#include "x" or \#include \<x> directive, the last string in this
    151   /// list which is a prefix of 'x' determines whether the file is treated as
    152   /// a system header.
    153   std::vector<std::pair<std::string, bool> > SystemHeaderPrefixes;
    154 
    155   /// \brief The path to the module cache.
    156   std::string ModuleCachePath;
    157 
    158   /// \brief All of the preprocessor-specific data about files that are
    159   /// included, indexed by the FileEntry's UID.
    160   std::vector<HeaderFileInfo> FileInfo;
    161 
    162   /// \brief Keeps track of each lookup performed by LookupFile.
    163   ///
    164   /// The first part of the value is the starting index in SearchDirs
    165   /// that the cached search was performed from.  If there is a hit and
    166   /// this value doesn't match the current query, the cache has to be
    167   /// ignored.  The second value is the entry in SearchDirs that satisfied
    168   /// the query.
    169   llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator>
    170     LookupFileCache;
    171 
    172   /// \brief Collection mapping a framework or subframework
    173   /// name like "Carbon" to the Carbon.framework directory.
    174   llvm::StringMap<FrameworkCacheEntry, llvm::BumpPtrAllocator> FrameworkMap;
    175 
    176   /// IncludeAliases - maps include file names (including the quotes or
    177   /// angle brackets) to other include file names.  This is used to support the
    178   /// include_alias pragma for Microsoft compatibility.
    179   typedef llvm::StringMap<std::string, llvm::BumpPtrAllocator>
    180     IncludeAliasMap;
    181   OwningPtr<IncludeAliasMap> IncludeAliases;
    182 
    183   /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing
    184   /// headermaps.  This vector owns the headermap.
    185   std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps;
    186 
    187   /// \brief The mapping between modules and headers.
    188   ModuleMap ModMap;
    189 
    190   /// \brief Describes whether a given directory has a module map in it.
    191   llvm::DenseMap<const DirectoryEntry *, bool> DirectoryHasModuleMap;
    192 
    193   /// \brief Uniqued set of framework names, which is used to track which
    194   /// headers were included as framework headers.
    195   llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames;
    196 
    197   /// \brief Entity used to resolve the identifier IDs of controlling
    198   /// macros into IdentifierInfo pointers, as needed.
    199   ExternalIdentifierLookup *ExternalLookup;
    200 
    201   /// \brief Entity used to look up stored header file information.
    202   ExternalHeaderFileInfoSource *ExternalSource;
    203 
    204   // Various statistics we track for performance analysis.
    205   unsigned NumIncluded;
    206   unsigned NumMultiIncludeFileOptzn;
    207   unsigned NumFrameworkLookups, NumSubFrameworkLookups;
    208 
    209   // HeaderSearch doesn't support default or copy construction.
    210   explicit HeaderSearch();
    211   explicit HeaderSearch(const HeaderSearch&);
    212   void operator=(const HeaderSearch&);
    213 
    214   friend class DirectoryLookup;
    215 
    216 public:
    217   HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags,
    218                const LangOptions &LangOpts, const TargetInfo *Target);
    219   ~HeaderSearch();
    220 
    221   FileManager &getFileMgr() const { return FileMgr; }
    222 
    223   /// \brief Interface for setting the file search paths.
    224   void SetSearchPaths(const std::vector<DirectoryLookup> &dirs,
    225                       unsigned angledDirIdx, unsigned systemDirIdx,
    226                       bool noCurDirSearch) {
    227     assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() &&
    228         "Directory indicies are unordered");
    229     SearchDirs = dirs;
    230     AngledDirIdx = angledDirIdx;
    231     SystemDirIdx = systemDirIdx;
    232     NoCurDirSearch = noCurDirSearch;
    233     //LookupFileCache.clear();
    234   }
    235 
    236   /// \brief Add an additional search path.
    237   void AddSearchPath(const DirectoryLookup &dir, bool isAngled) {
    238     unsigned idx = isAngled ? SystemDirIdx : AngledDirIdx;
    239     SearchDirs.insert(SearchDirs.begin() + idx, dir);
    240     if (!isAngled)
    241       AngledDirIdx++;
    242     SystemDirIdx++;
    243   }
    244 
    245   /// \brief Set the list of system header prefixes.
    246   void SetSystemHeaderPrefixes(ArrayRef<std::pair<std::string, bool> > P) {
    247     SystemHeaderPrefixes.assign(P.begin(), P.end());
    248   }
    249 
    250   /// \brief Checks whether the map exists or not.
    251   bool HasIncludeAliasMap() const {
    252     return IncludeAliases;
    253   }
    254 
    255   /// \brief Map the source include name to the dest include name.
    256   ///
    257   /// The Source should include the angle brackets or quotes, the dest
    258   /// should not.  This allows for distinction between <> and "" headers.
    259   void AddIncludeAlias(StringRef Source, StringRef Dest) {
    260     if (!IncludeAliases)
    261       IncludeAliases.reset(new IncludeAliasMap);
    262     (*IncludeAliases)[Source] = Dest;
    263   }
    264 
    265   /// MapHeaderToIncludeAlias - Maps one header file name to a different header
    266   /// file name, for use with the include_alias pragma.  Note that the source
    267   /// file name should include the angle brackets or quotes.  Returns StringRef
    268   /// as null if the header cannot be mapped.
    269   StringRef MapHeaderToIncludeAlias(StringRef Source) {
    270     assert(IncludeAliases && "Trying to map headers when there's no map");
    271 
    272     // Do any filename replacements before anything else
    273     IncludeAliasMap::const_iterator Iter = IncludeAliases->find(Source);
    274     if (Iter != IncludeAliases->end())
    275       return Iter->second;
    276     return StringRef();
    277   }
    278 
    279   /// \brief Set the path to the module cache.
    280   void setModuleCachePath(StringRef CachePath) {
    281     ModuleCachePath = CachePath;
    282   }
    283 
    284   /// \brief Retrieve the path to the module cache.
    285   StringRef getModuleCachePath() const { return ModuleCachePath; }
    286 
    287   /// \brief Forget everything we know about headers so far.
    288   void ClearFileInfo() {
    289     FileInfo.clear();
    290   }
    291 
    292   void SetExternalLookup(ExternalIdentifierLookup *EIL) {
    293     ExternalLookup = EIL;
    294   }
    295 
    296   ExternalIdentifierLookup *getExternalLookup() const {
    297     return ExternalLookup;
    298   }
    299 
    300   /// \brief Set the external source of header information.
    301   void SetExternalSource(ExternalHeaderFileInfoSource *ES) {
    302     ExternalSource = ES;
    303   }
    304 
    305   /// \brief Set the target information for the header search, if not
    306   /// already known.
    307   void setTarget(const TargetInfo &Target);
    308 
    309   /// \brief Given a "foo" or \<foo> reference, look up the indicated file,
    310   /// return null on failure.
    311   ///
    312   /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member
    313   /// the file was found in, or null if not applicable.
    314   ///
    315   /// \param isAngled indicates whether the file reference is a <> reference.
    316   ///
    317   /// \param CurDir If non-null, the file was found in the specified directory
    318   /// search location.  This is used to implement \#include_next.
    319   ///
    320   /// \param CurFileEnt If non-null, indicates where the \#including file is, in
    321   /// case a relative search is needed.
    322   ///
    323   /// \param SearchPath If non-null, will be set to the search path relative
    324   /// to which the file was found. If the include path is absolute, SearchPath
    325   /// will be set to an empty string.
    326   ///
    327   /// \param RelativePath If non-null, will be set to the path relative to
    328   /// SearchPath at which the file was found. This only differs from the
    329   /// Filename for framework includes.
    330   ///
    331   /// \param SuggestedModule If non-null, and the file found is semantically
    332   /// part of a known module, this will be set to the module that should
    333   /// be imported instead of preprocessing/parsing the file found.
    334   const FileEntry *LookupFile(StringRef Filename, bool isAngled,
    335                               const DirectoryLookup *FromDir,
    336                               const DirectoryLookup *&CurDir,
    337                               const FileEntry *CurFileEnt,
    338                               SmallVectorImpl<char> *SearchPath,
    339                               SmallVectorImpl<char> *RelativePath,
    340                               Module **SuggestedModule,
    341                               bool SkipCache = false);
    342 
    343   /// \brief Look up a subframework for the specified \#include file.
    344   ///
    345   /// For example, if \#include'ing <HIToolbox/HIToolbox.h> from
    346   /// within ".../Carbon.framework/Headers/Carbon.h", check to see if
    347   /// HIToolbox is a subframework within Carbon.framework.  If so, return
    348   /// the FileEntry for the designated file, otherwise return null.
    349   const FileEntry *LookupSubframeworkHeader(
    350       StringRef Filename,
    351       const FileEntry *RelativeFileEnt,
    352       SmallVectorImpl<char> *SearchPath,
    353       SmallVectorImpl<char> *RelativePath);
    354 
    355   /// \brief Look up the specified framework name in our framework cache.
    356   /// \returns The DirectoryEntry it is in if we know, null otherwise.
    357   FrameworkCacheEntry &LookupFrameworkCache(StringRef FWName) {
    358     return FrameworkMap.GetOrCreateValue(FWName).getValue();
    359   }
    360 
    361   /// \brief Mark the specified file as a target of of a \#include,
    362   /// \#include_next, or \#import directive.
    363   ///
    364   /// \return false if \#including the file will have no effect or true
    365   /// if we should include it.
    366   bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport);
    367 
    368 
    369   /// \brief Return whether the specified file is a normal header,
    370   /// a system header, or a C++ friendly system header.
    371   SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) {
    372     return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo;
    373   }
    374 
    375   /// \brief Mark the specified file as a "once only" file, e.g. due to
    376   /// \#pragma once.
    377   void MarkFileIncludeOnce(const FileEntry *File) {
    378     HeaderFileInfo &FI = getFileInfo(File);
    379     FI.isImport = true;
    380     FI.isPragmaOnce = true;
    381   }
    382 
    383   /// \brief Mark the specified file as a system header, e.g. due to
    384   /// \#pragma GCC system_header.
    385   void MarkFileSystemHeader(const FileEntry *File) {
    386     getFileInfo(File).DirInfo = SrcMgr::C_System;
    387   }
    388 
    389   /// \brief Increment the count for the number of times the specified
    390   /// FileEntry has been entered.
    391   void IncrementIncludeCount(const FileEntry *File) {
    392     ++getFileInfo(File).NumIncludes;
    393   }
    394 
    395   /// \brief Mark the specified file as having a controlling macro.
    396   ///
    397   /// This is used by the multiple-include optimization to eliminate
    398   /// no-op \#includes.
    399   void SetFileControllingMacro(const FileEntry *File,
    400                                const IdentifierInfo *ControllingMacro) {
    401     getFileInfo(File).ControllingMacro = ControllingMacro;
    402   }
    403 
    404   /// \brief Determine whether this file is intended to be safe from
    405   /// multiple inclusions, e.g., it has \#pragma once or a controlling
    406   /// macro.
    407   ///
    408   /// This routine does not consider the effect of \#import
    409   bool isFileMultipleIncludeGuarded(const FileEntry *File);
    410 
    411   /// CreateHeaderMap - This method returns a HeaderMap for the specified
    412   /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
    413   const HeaderMap *CreateHeaderMap(const FileEntry *FE);
    414 
    415   /// \brief Retrieve the name of the module file that should be used to
    416   /// load the given module.
    417   ///
    418   /// \param Module The module whose module file name will be returned.
    419   ///
    420   /// \returns The name of the module file that corresponds to this module,
    421   /// or an empty string if this module does not correspond to any module file.
    422   std::string getModuleFileName(Module *Module);
    423 
    424   /// \brief Retrieve the name of the module file that should be used to
    425   /// load a module with the given name.
    426   ///
    427   /// \param ModuleName The module whose module file name will be returned.
    428   ///
    429   /// \returns The name of the module file that corresponds to this module,
    430   /// or an empty string if this module does not correspond to any module file.
    431   std::string getModuleFileName(StringRef ModuleName);
    432 
    433   /// \brief Lookup a module Search for a module with the given name.
    434   ///
    435   /// \param ModuleName The name of the module we're looking for.
    436   ///
    437   /// \param AllowSearch Whether we are allowed to search in the various
    438   /// search directories to produce a module definition. If not, this lookup
    439   /// will only return an already-known module.
    440   ///
    441   /// \returns The module with the given name.
    442   Module *lookupModule(StringRef ModuleName, bool AllowSearch = true);
    443 
    444   void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; }
    445 
    446   /// \brief Determine whether there is a module map that may map the header
    447   /// with the given file name to a (sub)module.
    448   ///
    449   /// \param Filename The name of the file.
    450   ///
    451   /// \param Root The "root" directory, at which we should stop looking for
    452   /// module maps.
    453   bool hasModuleMap(StringRef Filename, const DirectoryEntry *Root);
    454 
    455   /// \brief Retrieve the module that corresponds to the given file, if any.
    456   ///
    457   /// \param File The header that we wish to map to a module.
    458   Module *findModuleForHeader(const FileEntry *File);
    459 
    460   /// \brief Read the contents of the given module map file.
    461   ///
    462   /// \param File The module map file.
    463   ///
    464   /// \returns true if an error occurred, false otherwise.
    465   bool loadModuleMapFile(const FileEntry *File);
    466 
    467   /// \brief Collect the set of all known, top-level modules.
    468   ///
    469   /// \param Modules Will be filled with the set of known, top-level modules.
    470   void collectAllModules(llvm::SmallVectorImpl<Module *> &Modules);
    471 
    472 private:
    473   /// \brief Retrieve a module with the given name, which may be part of the
    474   /// given framework.
    475   ///
    476   /// \param Name The name of the module to retrieve.
    477   ///
    478   /// \param Dir The framework directory (e.g., ModuleName.framework).
    479   ///
    480   /// \param IsSystem Whether the framework directory is part of the system
    481   /// frameworks.
    482   ///
    483   /// \returns The module, if found; otherwise, null.
    484   Module *loadFrameworkModule(StringRef Name,
    485                               const DirectoryEntry *Dir,
    486                               bool IsSystem);
    487 
    488 public:
    489   /// \brief Retrieve the module map.
    490   ModuleMap &getModuleMap() { return ModMap; }
    491 
    492   unsigned header_file_size() const { return FileInfo.size(); }
    493 
    494   // Used by ASTReader.
    495   void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID);
    496 
    497   /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
    498   const HeaderFileInfo &getFileInfo(const FileEntry *FE) const {
    499     return const_cast<HeaderSearch*>(this)->getFileInfo(FE);
    500   }
    501 
    502   // Used by external tools
    503   typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator;
    504   search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); }
    505   search_dir_iterator search_dir_end() const { return SearchDirs.end(); }
    506   unsigned search_dir_size() const { return SearchDirs.size(); }
    507 
    508   search_dir_iterator quoted_dir_begin() const {
    509     return SearchDirs.begin();
    510   }
    511   search_dir_iterator quoted_dir_end() const {
    512     return SearchDirs.begin() + AngledDirIdx;
    513   }
    514 
    515   search_dir_iterator angled_dir_begin() const {
    516     return SearchDirs.begin() + AngledDirIdx;
    517   }
    518   search_dir_iterator angled_dir_end() const {
    519     return SearchDirs.begin() + SystemDirIdx;
    520   }
    521 
    522   search_dir_iterator system_dir_begin() const {
    523     return SearchDirs.begin() + SystemDirIdx;
    524   }
    525   search_dir_iterator system_dir_end() const { return SearchDirs.end(); }
    526 
    527   /// \brief Retrieve a uniqued framework name.
    528   StringRef getUniqueFrameworkName(StringRef Framework);
    529 
    530   void PrintStats();
    531 
    532   size_t getTotalMemory() const;
    533 
    534   static std::string NormalizeDashIncludePath(StringRef File,
    535                                               FileManager &FileMgr);
    536 
    537 private:
    538   /// \brief Describes what happened when we tried to load a module map file.
    539   enum LoadModuleMapResult {
    540     /// \brief The module map file had already been loaded.
    541     LMM_AlreadyLoaded,
    542     /// \brief The module map file was loaded by this invocation.
    543     LMM_NewlyLoaded,
    544     /// \brief There is was directory with the given name.
    545     LMM_NoDirectory,
    546     /// \brief There was either no module map file or the module map file was
    547     /// invalid.
    548     LMM_InvalidModuleMap
    549   };
    550 
    551   /// \brief Try to load the module map file in the given directory.
    552   ///
    553   /// \param DirName The name of the directory where we will look for a module
    554   /// map file.
    555   ///
    556   /// \returns The result of attempting to load the module map file from the
    557   /// named directory.
    558   LoadModuleMapResult loadModuleMapFile(StringRef DirName);
    559 
    560   /// \brief Try to load the module map file in the given directory.
    561   ///
    562   /// \param Dir The directory where we will look for a module map file.
    563   ///
    564   /// \returns The result of attempting to load the module map file from the
    565   /// named directory.
    566   LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir);
    567 
    568   /// \brief Return the HeaderFileInfo structure for the specified FileEntry.
    569   HeaderFileInfo &getFileInfo(const FileEntry *FE);
    570 };
    571 
    572 }  // end namespace clang
    573 
    574 #endif
    575