HeaderSearch.h revision fc8f0e14ad142ed811e90fbd9a30e419e301c717
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 "llvm/ADT/StringMap.h" 19#include <vector> 20 21namespace clang { 22 23class ExternalIdentifierLookup; 24class FileEntry; 25class FileManager; 26class IdentifierInfo; 27 28/// HeaderFileInfo - The preprocessor keeps track of this information for each 29/// file that is #included. 30struct HeaderFileInfo { 31 /// isImport - True if this is a #import'd or #pragma once file. 32 unsigned isImport : 1; 33 34 /// DirInfo - Keep track of whether this is a system header, and if so, 35 /// whether it is C++ clean or not. This can be set by the include paths or 36 /// by #pragma gcc system_header. This is an instance of 37 /// SrcMgr::CharacteristicKind. 38 unsigned DirInfo : 2; 39 40 /// \brief Whether this header file info was supplied by an external source. 41 unsigned External : 1; 42 43 /// \brief Whether this structure is considered to already have been 44 /// "resolved", meaning that it was loaded from the external source. 45 unsigned Resolved : 1; 46 47 /// NumIncludes - This is the number of times the file has been included 48 /// already. 49 unsigned short NumIncludes; 50 51 /// \brief The ID number of the controlling macro. 52 /// 53 /// This ID number will be non-zero when there is a controlling 54 /// macro whose IdentifierInfo may not yet have been loaded from 55 /// external storage. 56 unsigned ControllingMacroID; 57 58 /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard 59 /// that protects the entire contents of the file, this is the identifier 60 /// for the macro that controls whether or not it has any effect. 61 /// 62 /// Note: Most clients should use getControllingMacro() to access 63 /// the controlling macro of this header, since 64 /// getControllingMacro() is able to load a controlling macro from 65 /// external storage. 66 const IdentifierInfo *ControllingMacro; 67 68 HeaderFileInfo() 69 : isImport(false), DirInfo(SrcMgr::C_User), External(false), 70 Resolved(false), NumIncludes(0), ControllingMacroID(0), 71 ControllingMacro(0) {} 72 73 /// \brief Retrieve the controlling macro for this header file, if 74 /// any. 75 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External); 76 77 /// \brief Determine whether this is a non-default header file info, e.g., 78 /// it corresponds to an actual header we've included or tried to include. 79 bool isNonDefault() const { 80 return isImport || NumIncludes || ControllingMacro || ControllingMacroID; 81 } 82}; 83 84/// \brief An external source of header file information, which may supply 85/// information about header files already included. 86class ExternalHeaderFileInfoSource { 87public: 88 virtual ~ExternalHeaderFileInfoSource(); 89 90 /// \brief Retrieve the header file information for the given file entry. 91 /// 92 /// \returns Header file information for the given file entry, with the 93 /// \c External bit set. If the file entry is not known, return a 94 /// default-constructed \c HeaderFileInfo. 95 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0; 96}; 97 98/// HeaderSearch - This class encapsulates the information needed to find the 99/// file referenced by a #include or #include_next, (sub-)framework lookup, etc. 100class HeaderSearch { 101 FileManager &FileMgr; 102 /// #include search path information. Requests for #include "x" search the 103 /// directory of the #including file first, then each directory in SearchDirs 104 /// consequtively. Requests for <x> search the current dir first, then each 105 /// directory in SearchDirs, starting at SystemDirIdx, consequtively. If 106 /// NoCurDirSearch is true, then the check for the file in the current 107 /// directory is suppressed. 108 std::vector<DirectoryLookup> SearchDirs; 109 unsigned SystemDirIdx; 110 bool NoCurDirSearch; 111 112 /// FileInfo - This contains all of the preprocessor-specific data about files 113 /// that are included. The vector is indexed by the FileEntry's UID. 114 /// 115 std::vector<HeaderFileInfo> FileInfo; 116 117 /// LookupFileCache - This is keeps track of each lookup performed by 118 /// LookupFile. The first part of the value is the starting index in 119 /// SearchDirs that the cached search was performed from. If there is a hit 120 /// and this value doesn't match the current query, the cache has to be 121 /// ignored. The second value is the entry in SearchDirs that satisfied the 122 /// query. 123 llvm::StringMap<std::pair<unsigned, unsigned> > LookupFileCache; 124 125 126 /// FrameworkMap - This is a collection mapping a framework or subframework 127 /// name like "Carbon" to the Carbon.framework directory. 128 llvm::StringMap<const DirectoryEntry *> FrameworkMap; 129 130 /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing 131 /// headermaps. This vector owns the headermap. 132 std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; 133 134 /// \brief Entity used to resolve the identifier IDs of controlling 135 /// macros into IdentifierInfo pointers, as needed. 136 ExternalIdentifierLookup *ExternalLookup; 137 138 /// \brief Entity used to look up stored header file information. 139 ExternalHeaderFileInfoSource *ExternalSource; 140 141 // Various statistics we track for performance analysis. 142 unsigned NumIncluded; 143 unsigned NumMultiIncludeFileOptzn; 144 unsigned NumFrameworkLookups, NumSubFrameworkLookups; 145 146 // HeaderSearch doesn't support default or copy construction. 147 explicit HeaderSearch(); 148 explicit HeaderSearch(const HeaderSearch&); 149 void operator=(const HeaderSearch&); 150public: 151 HeaderSearch(FileManager &FM); 152 ~HeaderSearch(); 153 154 FileManager &getFileMgr() const { return FileMgr; } 155 156 /// SetSearchPaths - Interface for setting the file search paths. 157 /// 158 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 159 unsigned systemDirIdx, bool noCurDirSearch) { 160 SearchDirs = dirs; 161 SystemDirIdx = systemDirIdx; 162 NoCurDirSearch = noCurDirSearch; 163 //LookupFileCache.clear(); 164 } 165 166 /// ClearFileInfo - Forget everything we know about headers so far. 167 void ClearFileInfo() { 168 FileInfo.clear(); 169 } 170 171 void SetExternalLookup(ExternalIdentifierLookup *EIL) { 172 ExternalLookup = EIL; 173 } 174 175 ExternalIdentifierLookup *getExternalLookup() const { 176 return ExternalLookup; 177 } 178 179 /// \brief Set the external source of header information. 180 void SetExternalSource(ExternalHeaderFileInfoSource *ES) { 181 ExternalSource = ES; 182 } 183 184 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 185 /// return null on failure. isAngled indicates whether the file reference is 186 /// a <> reference. If successful, this returns 'UsedDir', the 187 /// DirectoryLookup member the file was found in, or null if not applicable. 188 /// If CurDir is non-null, the file was found in the specified directory 189 /// search location. This is used to implement #include_next. CurFileEnt, if 190 /// non-null, indicates where the #including file is, in case a relative 191 /// search is needed. 192 const FileEntry *LookupFile(llvm::StringRef Filename, bool isAngled, 193 const DirectoryLookup *FromDir, 194 const DirectoryLookup *&CurDir, 195 const FileEntry *CurFileEnt, 196 llvm::SmallVectorImpl<char> *RawPath); 197 198 /// LookupSubframeworkHeader - Look up a subframework for the specified 199 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 200 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 201 /// is a subframework within Carbon.framework. If so, return the FileEntry 202 /// for the designated file, otherwise return null. 203 const FileEntry *LookupSubframeworkHeader( 204 llvm::StringRef Filename, 205 const FileEntry *RelativeFileEnt, 206 llvm::SmallVectorImpl<char> *RawPath); 207 208 /// LookupFrameworkCache - Look up the specified framework name in our 209 /// framework cache, returning the DirectoryEntry it is in if we know, 210 /// otherwise, return null. 211 const DirectoryEntry *&LookupFrameworkCache(llvm::StringRef FWName) { 212 return FrameworkMap.GetOrCreateValue(FWName).getValue(); 213 } 214 215 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 216 /// #include, #include_next, or #import directive. Return false if #including 217 /// the file will have no effect or true if we should include it. 218 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 219 220 221 /// getFileDirFlavor - Return whether the specified file is a normal header, 222 /// a system header, or a C++ friendly system header. 223 SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) { 224 return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo; 225 } 226 227 /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g. 228 /// due to #pragma once. 229 void MarkFileIncludeOnce(const FileEntry *File) { 230 getFileInfo(File).isImport = true; 231 } 232 233 /// MarkFileSystemHeader - Mark the specified file as a system header, e.g. 234 /// due to #pragma GCC system_header. 235 void MarkFileSystemHeader(const FileEntry *File) { 236 getFileInfo(File).DirInfo = SrcMgr::C_System; 237 } 238 239 /// IncrementIncludeCount - Increment the count for the number of times the 240 /// specified FileEntry has been entered. 241 void IncrementIncludeCount(const FileEntry *File) { 242 ++getFileInfo(File).NumIncludes; 243 } 244 245 /// SetFileControllingMacro - Mark the specified file as having a controlling 246 /// macro. This is used by the multiple-include optimization to eliminate 247 /// no-op #includes. 248 void SetFileControllingMacro(const FileEntry *File, 249 const IdentifierInfo *ControllingMacro) { 250 getFileInfo(File).ControllingMacro = ControllingMacro; 251 } 252 253 /// CreateHeaderMap - This method returns a HeaderMap for the specified 254 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 255 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 256 257 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 258 259 typedef std::vector<HeaderFileInfo>::const_iterator header_file_iterator; 260 header_file_iterator header_file_begin() const { return FileInfo.begin(); } 261 header_file_iterator header_file_end() const { return FileInfo.end(); } 262 unsigned header_file_size() const { return FileInfo.size(); } 263 264 // Used by ASTReader. 265 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID); 266 267 // Used by external tools 268 typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator; 269 search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); } 270 search_dir_iterator search_dir_end() const { return SearchDirs.end(); } 271 unsigned search_dir_size() const { return SearchDirs.size(); } 272 273 search_dir_iterator system_dir_begin() const { 274 return SearchDirs.begin() + SystemDirIdx; 275 } 276 search_dir_iterator system_dir_end() const { return SearchDirs.end(); } 277 278 void PrintStats(); 279private: 280 281 /// getFileInfo - Return the HeaderFileInfo structure for the specified 282 /// FileEntry. 283 HeaderFileInfo &getFileInfo(const FileEntry *FE); 284}; 285 286} // end namespace clang 287 288#endif 289