HeaderSearch.h revision 8e23806863721495f9e1f84aed614f7afba774a3
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 "llvm/ADT/StringSet.h" 20#include "llvm/Support/Allocator.h" 21#include <vector> 22 23namespace clang { 24 25class DiagnosticsEngine; 26class ExternalIdentifierLookup; 27class FileEntry; 28class FileManager; 29class IdentifierInfo; 30 31/// HeaderFileInfo - The preprocessor keeps track of this information for each 32/// file that is #included. 33struct HeaderFileInfo { 34 /// isImport - True if this is a #import'd or #pragma once file. 35 unsigned isImport : 1; 36 37 /// isPragmaOnce - True if this is #pragma once file. 38 unsigned isPragmaOnce : 1; 39 40 /// DirInfo - Keep track of whether this is a system header, and if so, 41 /// whether it is C++ clean or not. This can be set by the include paths or 42 /// by #pragma gcc system_header. This is an instance of 43 /// SrcMgr::CharacteristicKind. 44 unsigned DirInfo : 2; 45 46 /// \brief Whether this header file info was supplied by an external source. 47 unsigned External : 1; 48 49 /// \brief Whether this structure is considered to already have been 50 /// "resolved", meaning that it was loaded from the external source. 51 unsigned Resolved : 1; 52 53 /// \brief Whether this is a header inside a framework that is currently 54 /// being built. 55 /// 56 /// When a framework is being built, the headers have not yet been placed 57 /// into the appropriate framework subdirectories, and therefore are 58 /// provided via a header map. This bit indicates when this is one of 59 /// those framework headers. 60 unsigned IndexHeaderMapHeader : 1; 61 62 /// NumIncludes - This is the number of times the file has been included 63 /// already. 64 unsigned short NumIncludes; 65 66 /// \brief The ID number of the controlling macro. 67 /// 68 /// This ID number will be non-zero when there is a controlling 69 /// macro whose IdentifierInfo may not yet have been loaded from 70 /// external storage. 71 unsigned ControllingMacroID; 72 73 /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard 74 /// that protects the entire contents of the file, this is the identifier 75 /// for the macro that controls whether or not it has any effect. 76 /// 77 /// Note: Most clients should use getControllingMacro() to access 78 /// the controlling macro of this header, since 79 /// getControllingMacro() is able to load a controlling macro from 80 /// external storage. 81 const IdentifierInfo *ControllingMacro; 82 83 /// \brief If this header came from a framework include, this is the name 84 /// of the framework. 85 StringRef Framework; 86 87 HeaderFileInfo() 88 : isImport(false), isPragmaOnce(false), DirInfo(SrcMgr::C_User), 89 External(false), Resolved(false), IndexHeaderMapHeader(false), 90 NumIncludes(0), ControllingMacroID(0), ControllingMacro(0) {} 91 92 /// \brief Retrieve the controlling macro for this header file, if 93 /// any. 94 const IdentifierInfo *getControllingMacro(ExternalIdentifierLookup *External); 95 96 /// \brief Determine whether this is a non-default header file info, e.g., 97 /// it corresponds to an actual header we've included or tried to include. 98 bool isNonDefault() const { 99 return isImport || isPragmaOnce || NumIncludes || ControllingMacro || 100 ControllingMacroID; 101 } 102}; 103 104/// \brief An external source of header file information, which may supply 105/// information about header files already included. 106class ExternalHeaderFileInfoSource { 107public: 108 virtual ~ExternalHeaderFileInfoSource(); 109 110 /// \brief Retrieve the header file information for the given file entry. 111 /// 112 /// \returns Header file information for the given file entry, with the 113 /// \c External bit set. If the file entry is not known, return a 114 /// default-constructed \c HeaderFileInfo. 115 virtual HeaderFileInfo GetHeaderFileInfo(const FileEntry *FE) = 0; 116}; 117 118/// HeaderSearch - This class encapsulates the information needed to find the 119/// file referenced by a #include or #include_next, (sub-)framework lookup, etc. 120class HeaderSearch { 121 FileManager &FileMgr; 122 DiagnosticsEngine &Diags; 123 /// #include search path information. Requests for #include "x" search the 124 /// directory of the #including file first, then each directory in SearchDirs 125 /// consecutively. Requests for <x> search the current dir first, then each 126 /// directory in SearchDirs, starting at AngledDirIdx, consecutively. If 127 /// NoCurDirSearch is true, then the check for the file in the current 128 /// directory is suppressed. 129 std::vector<DirectoryLookup> SearchDirs; 130 unsigned AngledDirIdx; 131 unsigned SystemDirIdx; 132 bool NoCurDirSearch; 133 134 /// \brief The path to the module cache. 135 std::string ModuleCachePath; 136 137 /// \brief The name of the module we're building. 138 std::string BuildingModule; 139 140 /// FileInfo - This contains all of the preprocessor-specific data about files 141 /// that are included. The vector is indexed by the FileEntry's UID. 142 /// 143 std::vector<HeaderFileInfo> FileInfo; 144 145 /// LookupFileCache - This is keeps track of each lookup performed by 146 /// LookupFile. The first part of the value is the starting index in 147 /// SearchDirs that the cached search was performed from. If there is a hit 148 /// and this value doesn't match the current query, the cache has to be 149 /// ignored. The second value is the entry in SearchDirs that satisfied the 150 /// query. 151 llvm::StringMap<std::pair<unsigned, unsigned>, llvm::BumpPtrAllocator> 152 LookupFileCache; 153 154 155 /// FrameworkMap - This is a collection mapping a framework or subframework 156 /// name like "Carbon" to the Carbon.framework directory. 157 llvm::StringMap<const DirectoryEntry *, llvm::BumpPtrAllocator> 158 FrameworkMap; 159 160 /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing 161 /// headermaps. This vector owns the headermap. 162 std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; 163 164 /// \brief Uniqued set of framework names, which is used to track which 165 /// headers were included as framework headers. 166 llvm::StringSet<llvm::BumpPtrAllocator> FrameworkNames; 167 168 /// \brief Entity used to resolve the identifier IDs of controlling 169 /// macros into IdentifierInfo pointers, as needed. 170 ExternalIdentifierLookup *ExternalLookup; 171 172 /// \brief Entity used to look up stored header file information. 173 ExternalHeaderFileInfoSource *ExternalSource; 174 175 // Various statistics we track for performance analysis. 176 unsigned NumIncluded; 177 unsigned NumMultiIncludeFileOptzn; 178 unsigned NumFrameworkLookups, NumSubFrameworkLookups; 179 180 // HeaderSearch doesn't support default or copy construction. 181 explicit HeaderSearch(); 182 explicit HeaderSearch(const HeaderSearch&); 183 void operator=(const HeaderSearch&); 184public: 185 HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags); 186 ~HeaderSearch(); 187 188 FileManager &getFileMgr() const { return FileMgr; } 189 190 /// SetSearchPaths - Interface for setting the file search paths. 191 /// 192 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 193 unsigned angledDirIdx, unsigned systemDirIdx, 194 bool noCurDirSearch) { 195 assert(angledDirIdx <= systemDirIdx && systemDirIdx <= dirs.size() && 196 "Directory indicies are unordered"); 197 SearchDirs = dirs; 198 AngledDirIdx = angledDirIdx; 199 SystemDirIdx = systemDirIdx; 200 NoCurDirSearch = noCurDirSearch; 201 //LookupFileCache.clear(); 202 } 203 204 /// \brief Set the path to the module cache and the name of the module 205 /// we're building 206 void configureModules(StringRef CachePath, StringRef BuildingModule) { 207 ModuleCachePath = CachePath; 208 this->BuildingModule = BuildingModule; 209 } 210 211 /// ClearFileInfo - Forget everything we know about headers so far. 212 void ClearFileInfo() { 213 FileInfo.clear(); 214 } 215 216 void SetExternalLookup(ExternalIdentifierLookup *EIL) { 217 ExternalLookup = EIL; 218 } 219 220 ExternalIdentifierLookup *getExternalLookup() const { 221 return ExternalLookup; 222 } 223 224 /// \brief Set the external source of header information. 225 void SetExternalSource(ExternalHeaderFileInfoSource *ES) { 226 ExternalSource = ES; 227 } 228 229 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 230 /// return null on failure. 231 /// 232 /// \returns If successful, this returns 'UsedDir', the DirectoryLookup member 233 /// the file was found in, or null if not applicable. 234 /// 235 /// \param isAngled indicates whether the file reference is a <> reference. 236 /// 237 /// \param CurDir If non-null, the file was found in the specified directory 238 /// search location. This is used to implement #include_next. 239 /// 240 /// \param CurFileEnt If non-null, indicates where the #including file is, in 241 /// case a relative search is needed. 242 /// 243 /// \param SearchPath If non-null, will be set to the search path relative 244 /// to which the file was found. If the include path is absolute, SearchPath 245 /// will be set to an empty string. 246 /// 247 /// \param RelativePath If non-null, will be set to the path relative to 248 /// SearchPath at which the file was found. This only differs from the 249 /// Filename for framework includes. 250 /// 251 /// \param SuggestedModule If non-null, and the file found is semantically 252 /// part of a known module, this will be set to the name of the module that 253 /// could be imported instead of preprocessing/parsing the file found. 254 const FileEntry *LookupFile(StringRef Filename, bool isAngled, 255 const DirectoryLookup *FromDir, 256 const DirectoryLookup *&CurDir, 257 const FileEntry *CurFileEnt, 258 SmallVectorImpl<char> *SearchPath, 259 SmallVectorImpl<char> *RelativePath, 260 StringRef *SuggestedModule); 261 262 /// LookupSubframeworkHeader - Look up a subframework for the specified 263 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 264 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 265 /// is a subframework within Carbon.framework. If so, return the FileEntry 266 /// for the designated file, otherwise return null. 267 const FileEntry *LookupSubframeworkHeader( 268 StringRef Filename, 269 const FileEntry *RelativeFileEnt, 270 SmallVectorImpl<char> *SearchPath, 271 SmallVectorImpl<char> *RelativePath); 272 273 /// LookupFrameworkCache - Look up the specified framework name in our 274 /// framework cache, returning the DirectoryEntry it is in if we know, 275 /// otherwise, return null. 276 const DirectoryEntry *&LookupFrameworkCache(StringRef FWName) { 277 return FrameworkMap.GetOrCreateValue(FWName).getValue(); 278 } 279 280 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 281 /// #include, #include_next, or #import directive. Return false if #including 282 /// the file will have no effect or true if we should include it. 283 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 284 285 286 /// getFileDirFlavor - Return whether the specified file is a normal header, 287 /// a system header, or a C++ friendly system header. 288 SrcMgr::CharacteristicKind getFileDirFlavor(const FileEntry *File) { 289 return (SrcMgr::CharacteristicKind)getFileInfo(File).DirInfo; 290 } 291 292 /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g. 293 /// due to #pragma once. 294 void MarkFileIncludeOnce(const FileEntry *File) { 295 HeaderFileInfo &FI = getFileInfo(File); 296 FI.isImport = true; 297 FI.isPragmaOnce = true; 298 } 299 300 /// MarkFileSystemHeader - Mark the specified file as a system header, e.g. 301 /// due to #pragma GCC system_header. 302 void MarkFileSystemHeader(const FileEntry *File) { 303 getFileInfo(File).DirInfo = SrcMgr::C_System; 304 } 305 306 /// IncrementIncludeCount - Increment the count for the number of times the 307 /// specified FileEntry has been entered. 308 void IncrementIncludeCount(const FileEntry *File) { 309 ++getFileInfo(File).NumIncludes; 310 } 311 312 /// SetFileControllingMacro - Mark the specified file as having a controlling 313 /// macro. This is used by the multiple-include optimization to eliminate 314 /// no-op #includes. 315 void SetFileControllingMacro(const FileEntry *File, 316 const IdentifierInfo *ControllingMacro) { 317 getFileInfo(File).ControllingMacro = ControllingMacro; 318 } 319 320 /// \brief Determine whether this file is intended to be safe from 321 /// multiple inclusions, e.g., it has #pragma once or a controlling 322 /// macro. 323 /// 324 /// This routine does not consider the effect of #import 325 bool isFileMultipleIncludeGuarded(const FileEntry *File); 326 327 /// CreateHeaderMap - This method returns a HeaderMap for the specified 328 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 329 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 330 331 /// \brief Search in the module cache path for a module with the given 332 /// name. 333 /// 334 /// \param If non-NULL, will be set to the module file name we expected to 335 /// find (regardless of whether it was actually found or not). 336 /// 337 /// \param UmbrellaHeader If non-NULL, and no module was found in the module 338 /// cache, this routine will search in the framework paths to determine 339 /// whether a module can be built from an umbrella header. If so, the pointee 340 /// will be set to the path of the umbrella header. 341 /// 342 /// \returns A file describing the named module, if available, or NULL to 343 /// indicate that the module could not be found. 344 const FileEntry *lookupModule(StringRef ModuleName, 345 std::string *ModuleFileName = 0, 346 std::string *UmbrellaHeader = 0); 347 348 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 349 350 typedef std::vector<HeaderFileInfo>::const_iterator header_file_iterator; 351 header_file_iterator header_file_begin() const { return FileInfo.begin(); } 352 header_file_iterator header_file_end() const { return FileInfo.end(); } 353 unsigned header_file_size() const { return FileInfo.size(); } 354 355 // Used by ASTReader. 356 void setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID); 357 358 // Used by external tools 359 typedef std::vector<DirectoryLookup>::const_iterator search_dir_iterator; 360 search_dir_iterator search_dir_begin() const { return SearchDirs.begin(); } 361 search_dir_iterator search_dir_end() const { return SearchDirs.end(); } 362 unsigned search_dir_size() const { return SearchDirs.size(); } 363 364 search_dir_iterator quoted_dir_begin() const { 365 return SearchDirs.begin(); 366 } 367 search_dir_iterator quoted_dir_end() const { 368 return SearchDirs.begin() + AngledDirIdx; 369 } 370 371 search_dir_iterator angled_dir_begin() const { 372 return SearchDirs.begin() + AngledDirIdx; 373 } 374 search_dir_iterator angled_dir_end() const { 375 return SearchDirs.begin() + SystemDirIdx; 376 } 377 378 search_dir_iterator system_dir_begin() const { 379 return SearchDirs.begin() + SystemDirIdx; 380 } 381 search_dir_iterator system_dir_end() const { return SearchDirs.end(); } 382 383 /// \brief Retrieve a uniqued framework name. 384 StringRef getUniqueFrameworkName(StringRef Framework); 385 386 void PrintStats(); 387 388 size_t getTotalMemory() const; 389 390private: 391 392 /// getFileInfo - Return the HeaderFileInfo structure for the specified 393 /// FileEntry. 394 HeaderFileInfo &getFileInfo(const FileEntry *FE); 395}; 396 397} // end namespace clang 398 399#endif 400