HeaderSearch.h revision 49c1f4aa2a6c360d25d605004ec3c4affd62db77
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; 92 93 // HeaderSearch doesn't support default or copy construction. 94 explicit HeaderSearch(); 95 explicit HeaderSearch(const HeaderSearch&); 96 void operator=(const HeaderSearch&); 97public: 98 HeaderSearch(FileManager &FM); 99 ~HeaderSearch(); 100 101 FileManager &getFileMgr() const { return FileMgr; } 102 103 /// SetSearchPaths - Interface for setting the file search paths. 104 /// 105 void SetSearchPaths(const std::vector<DirectoryLookup> &dirs, 106 unsigned systemDirIdx, bool noCurDirSearch) { 107 SearchDirs = dirs; 108 SystemDirIdx = systemDirIdx; 109 NoCurDirSearch = noCurDirSearch; 110 //LookupFileCache.clear(); 111 } 112 113 /// ClearFileInfo - Forget everything we know about headers so far. 114 void ClearFileInfo() { 115 FileInfo.clear(); 116 } 117 118 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, 119 /// return null on failure. isAngled indicates whether the file reference is 120 /// a <> reference. If successful, this returns 'UsedDir', the 121 /// DirectoryLookup member the file was found in, or null if not applicable. 122 /// If CurDir is non-null, the file was found in the specified directory 123 /// search location. This is used to implement #include_next. CurFileEnt, if 124 /// non-null, indicates where the #including file is, in case a relative 125 /// search is needed. 126 const FileEntry *LookupFile(const char *FilenameStart, 127 const char *FilenameEnd, bool isAngled, 128 const DirectoryLookup *FromDir, 129 const DirectoryLookup *&CurDir, 130 const FileEntry *CurFileEnt); 131 132 /// LookupSubframeworkHeader - Look up a subframework for the specified 133 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from 134 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox 135 /// is a subframework within Carbon.framework. If so, return the FileEntry 136 /// for the designated file, otherwise return null. 137 const FileEntry *LookupSubframeworkHeader(const char *FilenameStart, 138 const char *FilenameEnd, 139 const FileEntry *RelativeFileEnt); 140 141 /// LookupFrameworkCache - Look up the specified framework name in our 142 /// framework cache, returning the DirectoryEntry it is in if we know, 143 /// otherwise, return null. 144 const DirectoryEntry *&LookupFrameworkCache(const char *FWNameStart, 145 const char *FWNameEnd) { 146 return FrameworkMap.GetOrCreateValue(FWNameStart, FWNameEnd).getValue(); 147 } 148 149 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a 150 /// #include, #include_next, or #import directive. Return false if #including 151 /// the file will have no effect or true if we should include it. 152 bool ShouldEnterIncludeFile(const FileEntry *File, bool isImport); 153 154 155 /// getFileDirFlavor - Return whether the specified file is a normal header, 156 /// a system header, or a C++ friendly system header. 157 DirectoryLookup::DirType getFileDirFlavor(const FileEntry *File) { 158 return getFileInfo(File).DirInfo; 159 } 160 161 /// MarkFileIncludeOnce - Mark the specified file as a "once only" file, e.g. 162 /// due to #pragma once. 163 void MarkFileIncludeOnce(const FileEntry *File) { 164 getFileInfo(File).isImport = true; 165 } 166 167 /// MarkFileSystemHeader - Mark the specified fiel as a system header, e.g. 168 /// due to #pragma GCC system_header. 169 void MarkFileSystemHeader(const FileEntry *File) { 170 getFileInfo(File).DirInfo = DirectoryLookup::SystemHeaderDir; 171 } 172 173 /// IncrementIncludeCount - Increment the count for the number of times the 174 /// specified FileEntry has been entered. 175 void IncrementIncludeCount(const FileEntry *File) { 176 ++getFileInfo(File).NumIncludes; 177 } 178 179 /// SetFileControllingMacro - Mark the specified file as having a controlling 180 /// macro. This is used by the multiple-include optimization to eliminate 181 /// no-op #includes. 182 void SetFileControllingMacro(const FileEntry *File, 183 const IdentifierInfo *ControllingMacro) { 184 getFileInfo(File).ControllingMacro = ControllingMacro; 185 } 186 187 /// CreateHeaderMap - This method returns a HeaderMap for the specified 188 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. 189 const HeaderMap *CreateHeaderMap(const FileEntry *FE); 190 191 void IncrementFrameworkLookupCount() { ++NumFrameworkLookups; } 192 193 void PrintStats(); 194private: 195 196 /// getFileInfo - Return the PerFileInfo structure for the specified 197 /// FileEntry. 198 PerFileInfo &getFileInfo(const FileEntry *FE); 199}; 200 201} // end namespace clang 202 203#endif 204