HeaderSearch.h revision 0bc735ffcfb223c0186419547abaa5c84482663e
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 { 22class FileEntry; 23class FileManager; 24class IdentifierInfo; 25 26 27/// HeaderSearch - This class encapsulates the information needed to find the 28/// file referenced by a #include or #include_next, (sub-)framework lookup, etc. 29class HeaderSearch { 30 FileManager &FileMgr; 31 32 /// #include search path information. Requests for #include "x" search the 33 /// directory of the #including file first, then each directory in SearchDirs 34 /// consequtively. Requests for <x> search the current dir first, then each 35 /// directory in SearchDirs, starting at SystemDirIdx, consequtively. If 36 /// NoCurDirSearch is true, then the check for the file in the current 37 /// directory is supressed. 38 std::vector<DirectoryLookup> SearchDirs; 39 unsigned SystemDirIdx; 40 bool NoCurDirSearch; 41 42 /// PreFileInfo - The preprocessor keeps track of this information for each 43 /// file that is #included. 44 struct PerFileInfo { 45 /// isImport - True if this is a #import'd or #pragma once file. 46 bool isImport : 1; 47 48 /// DirInfo - Keep track of whether this is a system header, and if so, 49 /// whether it is C++ clean or not. This can be set by the include paths or 50 /// by #pragma gcc system_header. 51 DirectoryLookup::DirType DirInfo : 2; 52 53 /// NumIncludes - This is the number of times the file has been included 54 /// already. 55 unsigned short NumIncludes; 56 57 /// ControllingMacro - If this file has a #ifndef XXX (or equivalent) guard 58 /// that protects the entire contents of the file, this is the identifier 59 /// for the macro that controls whether or not it has any effect. 60 const IdentifierInfo *ControllingMacro; 61 62 PerFileInfo() : isImport(false), DirInfo(DirectoryLookup::NormalHeaderDir), 63 NumIncludes(0), ControllingMacro(0) {} 64 }; 65 66 /// FileInfo - This contains all of the preprocessor-specific data about files 67 /// that are included. The vector is indexed by the FileEntry's UID. 68 /// 69 std::vector<PerFileInfo> FileInfo; 70 71 /// LookupFileCache - This is keeps track of each lookup performed by 72 /// LookupFile. The first part of the value is the starting index in 73 /// SearchDirs that the cached search was performed from. If there is a hit 74 /// and this value doesn't match the current query, the cache has to be 75 /// ignored. The second value is the entry in SearchDirs that satisfied the 76 /// query. 77 llvm::StringMap<std::pair<unsigned, unsigned> > LookupFileCache; 78 79 80 /// FrameworkMap - This is a collection mapping a framework or subframework 81 /// name like "Carbon" to the Carbon.framework directory. 82 llvm::StringMap<const DirectoryEntry *> FrameworkMap; 83 84 /// HeaderMaps - This is a mapping from FileEntry -> HeaderMap, uniquing 85 /// headermaps. This vector owns the headermap. 86 std::vector<std::pair<const FileEntry*, const HeaderMap*> > HeaderMaps; 87 88 // Various statistics we track for performance analysis. 89 unsigned NumIncluded; 90 unsigned NumMultiIncludeFileOptzn; 91 unsigned NumFrameworkLookups, NumSubFrameworkLookups; 92public: 93 HeaderSearch(FileManager &FM); 94 ~HeaderSearch(); 95 96 FileManager &getFileMgr() const { return FileMgr; } 97 98 /// SetSearchPaths - Interface for setting the file search paths. 99 /// 100 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 101 unsigned systemDirIdx, bool noCurDirSearch) { 102 SearchDirs = dirs; 103 SystemDirIdx = systemDirIdx; 104 NoCurDirSearch = noCurDirSearch; 105 //LookupFileCache.clear(); 106 } 107 108 /// ClearFileInfo - Forget everything we know about headers so far. 109 void ClearFileInfo() { 110 FileInfo.clear(); 111 } 112 113 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 114 /// return null on failure. isAngled indicates whether the file reference is 115 /// a <> reference. If successful, this returns 'UsedDir', the 116 /// DirectoryLookup member the file was found in, or null if not applicable. 117 /// If CurDir is non-null, the file was found in the specified directory 118 /// search location. This is used to implement #include_next. CurFileEnt, if 119 /// non-null, indicates where the #including file is, in case a relative 120 /// search is needed. 121 const FileEntry *LookupFile(const char *FilenameStart, 122 const char *FilenameEnd, bool isAngled, 123 const DirectoryLookup *FromDir, 124 const DirectoryLookup *&CurDir, 125 const FileEntry *CurFileEnt); 126 127 /// LookupSubframeworkHeader - Look up a subframework for the specified 128 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 129 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 130 /// is a subframework within Carbon.framework. If so, return the FileEntry 131 /// for the designated file, otherwise return null. 132 const FileEntry *LookupSubframeworkHeader(const char *FilenameStart, 133 const char *FilenameEnd, 134 const FileEntry *RelativeFileEnt); 135 136 /// LookupFrameworkCache - Look up the specified framework name in our 137 /// framework cache, returning the DirectoryEntry it is in if we know, 138 /// otherwise, return null. 139 const DirectoryEntry *&LookupFrameworkCache(const char *FWNameStart, 140 const char *FWNameEnd) { 141 return FrameworkMap.GetOrCreateValue(FWNameStart, FWNameEnd).getValue(); 142 } 143 144 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 145 /// #include, #include_next, or #import directive. Return false if #including 146 /// the file will have no effect or true if we should include it. 147 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 148 149 150 /// getFileDirFlavor - Return whether the specified file is a normal header, 151 /// a system header, or a C++ friendly system header. 152 DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) { 153 return getFileInfo(File).DirInfo; 154 } 155 156 /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g. 157 /// due to #pragma once. 158 void MarkFileIncludeOnce(const FileEntry *File) { 159 getFileInfo(File).isImport = true; 160 } 161 162 /// MarkFileSystemHeader - Mark the specified fiel as a system header, e.g. 163 /// due to #pragma GCC system_header. 164 void MarkFileSystemHeader(const FileEntry *File) { 165 getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir; 166 } 167 168 /// IncrementIncludeCount - Increment the count for the number of times the 169 /// specified FileEntry has been entered. 170 void IncrementIncludeCount(const FileEntry *File) { 171 ++getFileInfo(File).NumIncludes; 172 } 173 174 /// SetFileControllingMacro - Mark the specified file as having a controlling 175 /// macro. This is used by the multiple-include optimization to eliminate 176 /// no-op #includes. 177 void SetFileControllingMacro(const FileEntry *File, 178 const IdentifierInfo *ControllingMacro) { 179 getFileInfo(File).ControllingMacro = ControllingMacro; 180 } 181 182 /// CreateHeaderMap - This method returns a HeaderMap for the specified 183 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 184 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 185 186 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 187 188 void PrintStats(); 189private: 190 191 /// getFileInfo - Return the PerFileInfo structure for the specified 192 /// FileEntry. 193 PerFileInfo &getFileInfo(const FileEntry *FE); 194}; 195 196} // end namespace clang 197 198#endif 199