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