Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 1 | //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DirectoryLookup and HeaderSearch interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/FileManager.h" |
| 15 | #include "clang/Lex/HeaderSearch.h" |
| 16 | #include "clang/Lex/IdentifierTable.h" |
| 17 | #include "llvm/System/Path.h" |
| 18 | #include <iostream> |
| 19 | using namespace llvm; |
| 20 | using namespace clang; |
| 21 | |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 22 | HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM) { |
| 23 | SystemDirIdx = 0; |
| 24 | NoCurDirSearch = false; |
| 25 | |
| 26 | NumIncluded = 0; |
| 27 | NumMultiIncludeFileOptzn = 0; |
| 28 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 29 | } |
| 30 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 31 | void HeaderSearch::PrintStats() { |
| 32 | std::cerr << "\n*** HeaderSearch Stats:\n"; |
| 33 | std::cerr << FileInfo.size() << " files tracked.\n"; |
| 34 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 35 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 36 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 37 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 38 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 39 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 40 | } |
| 41 | std::cerr << " " << NumOnceOnlyFiles << " #import/#pragma once files.\n"; |
| 42 | std::cerr << " " << NumSingleIncludedFiles << " included exactly once.\n"; |
| 43 | std::cerr << " " << MaxNumIncludes << " max times a file is included.\n"; |
| 44 | |
| 45 | std::cerr << " " << NumIncluded << " #include/#include_next/#import.\n"; |
| 46 | std::cerr << " " << NumMultiIncludeFileOptzn << " #includes skipped due to" |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 47 | << " the multi-include optimization.\n"; |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 48 | |
| 49 | std::cerr << NumFrameworkLookups << " framework lookups.\n"; |
| 50 | std::cerr << NumSubFrameworkLookups << " subframework lookups.\n"; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // Header File Location. |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 57 | const FileEntry *HeaderSearch::DoFrameworkLookup(const DirectoryEntry *Dir, |
| 58 | const std::string &Filename) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 59 | // Framework names must have a '/' in the filename. |
| 60 | std::string::size_type SlashPos = Filename.find('/'); |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 61 | if (SlashPos == std::string::npos) return 0; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame^] | 63 | const DirectoryEntry *&CacheLookup = |
| 64 | FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)]; |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame^] | 66 | // If it is some other directory, fail. |
| 67 | if (CacheLookup && CacheLookup != Dir) |
| 68 | return 0; |
| 69 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 70 | // FrameworkName = "/System/Library/Frameworks/" |
| 71 | std::string FrameworkName = Dir->getName(); |
| 72 | if (FrameworkName.empty() || FrameworkName[FrameworkName.size()-1] != '/') |
| 73 | FrameworkName += '/'; |
| 74 | |
| 75 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
| 76 | FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos); |
| 77 | |
| 78 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 79 | FrameworkName += ".framework/"; |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame^] | 80 | |
| 81 | if (CacheLookup == 0) { |
| 82 | ++NumFrameworkLookups; |
| 83 | |
| 84 | // If the framework dir doesn't exist, we fail. |
| 85 | if (!sys::Path(FrameworkName).exists()) |
| 86 | return 0; |
| 87 | |
| 88 | // Otherwise, if it does, remember that this is the right direntry for this |
| 89 | // framework. |
| 90 | CacheLookup = Dir; |
| 91 | } |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 93 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 94 | std::string HeadersFilename = FrameworkName + "Headers/" + |
| 95 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 96 | if (const FileEntry *FE = FileMgr.getFile(HeadersFilename)) |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 97 | return FE; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 98 | |
| 99 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 100 | std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" + |
| 101 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 102 | return FileMgr.getFile(PrivateHeadersFilename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 106 | /// return null on failure. isAngled indicates whether the file reference is |
| 107 | /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 108 | /// non-null, indicates where the #including file is, in case a relative search |
| 109 | /// is needed. |
| 110 | const FileEntry *HeaderSearch::LookupFile(const std::string &Filename, |
| 111 | bool isAngled, |
| 112 | const DirectoryLookup *FromDir, |
| 113 | const DirectoryLookup *&CurDir, |
| 114 | const FileEntry *CurFileEnt) { |
| 115 | // If 'Filename' is absolute, check to see if it exists and no searching. |
| 116 | // FIXME: Portability. This should be a sys::Path interface, this doesn't |
| 117 | // handle things like C:\foo.txt right, nor win32 \\network\device\blah. |
| 118 | if (Filename[0] == '/') { |
| 119 | CurDir = 0; |
| 120 | |
| 121 | // If this was an #include_next "/absolute/file", fail. |
| 122 | if (FromDir) return 0; |
| 123 | |
| 124 | // Otherwise, just return the file. |
| 125 | return FileMgr.getFile(Filename); |
| 126 | } |
| 127 | |
| 128 | // Step #0, unless disabled, check to see if the file is in the #includer's |
| 129 | // directory. This search is not done for <> headers. |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 130 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 131 | // Concatenate the requested file onto the directory. |
| 132 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 133 | if (const FileEntry *FE = |
| 134 | FileMgr.getFile(CurFileEnt->getDir()->getName()+"/"+Filename)) { |
| 135 | // Leave CurDir unset. |
| 136 | |
| 137 | // This file is a system header or C++ unfriendly if the old file is. |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 138 | getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 139 | return FE; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | CurDir = 0; |
| 144 | |
| 145 | // If this is a system #include, ignore the user #include locs. |
| 146 | unsigned i = isAngled ? SystemDirIdx : 0; |
| 147 | |
| 148 | // If this is a #include_next request, start searching after the directory the |
| 149 | // file was found in. |
| 150 | if (FromDir) |
| 151 | i = FromDir-&SearchDirs[0]; |
| 152 | |
| 153 | // Check each directory in sequence to see if it contains this file. |
| 154 | for (; i != SearchDirs.size(); ++i) { |
| 155 | // Concatenate the requested file onto the directory. |
| 156 | std::string SearchDir; |
| 157 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 158 | const FileEntry *FE = 0; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 159 | if (!SearchDirs[i].isFramework()) { |
| 160 | // FIXME: Portability. Adding file to dir should be in sys::Path. |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 161 | FE = FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 162 | } else { |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 163 | FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 166 | if (FE) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 167 | CurDir = &SearchDirs[i]; |
| 168 | |
| 169 | // This file is a system header or C++ unfriendly if the dir is. |
| 170 | getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic(); |
| 171 | return FE; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Otherwise, didn't find it. |
| 176 | return 0; |
| 177 | } |
| 178 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 179 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
| 180 | /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from |
| 181 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 182 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 183 | /// for the designated file, otherwise return null. |
| 184 | const FileEntry *HeaderSearch:: |
| 185 | LookupSubframeworkHeader(const std::string &Filename, |
| 186 | const FileEntry *ContextFileEnt) { |
| 187 | // Framework names must have a '/' in the filename. Find it. |
| 188 | std::string::size_type SlashPos = Filename.find('/'); |
| 189 | if (SlashPos == std::string::npos) return 0; |
| 190 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 191 | // Look up the base framework name of the ContextFileEnt. |
| 192 | const std::string &ContextName = ContextFileEnt->getName(); |
| 193 | std::string::size_type FrameworkPos = ContextName.find(".framework/"); |
| 194 | // If the context info wasn't a framework, couldn't be a subframework. |
| 195 | if (FrameworkPos == std::string::npos) |
| 196 | return 0; |
| 197 | |
| 198 | std::string FrameworkName(ContextName.begin(), |
| 199 | ContextName.begin()+FrameworkPos+strlen(".framework/")); |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame^] | 200 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 201 | // Append Frameworks/HIToolbox.framework/ |
| 202 | FrameworkName += "Frameworks/"; |
| 203 | FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos); |
| 204 | FrameworkName += ".framework/"; |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 5ed76da | 2006-10-22 07:24:13 +0000 | [diff] [blame^] | 206 | const DirectoryEntry *&CacheLookup = |
| 207 | FrameworkMap[std::string(Filename.begin(), Filename.begin()+SlashPos)]; |
| 208 | |
| 209 | // Some other location? |
| 210 | if (CacheLookup && CacheLookup->getName() != FrameworkName) |
| 211 | return 0; |
| 212 | |
| 213 | // Cache subframework. |
| 214 | if (CacheLookup == 0) { |
| 215 | ++NumSubFrameworkLookups; |
| 216 | |
| 217 | // If the framework dir doesn't exist, we fail. |
| 218 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName); |
| 219 | if (Dir == 0) return 0; |
| 220 | |
| 221 | // Otherwise, if it does, remember that this is the right direntry for this |
| 222 | // framework. |
| 223 | CacheLookup = Dir; |
| 224 | } |
| 225 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 226 | const FileEntry *FE = 0; |
| 227 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 228 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
| 229 | std::string HeadersFilename = FrameworkName + "Headers/" + |
| 230 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 231 | if (!(FE = FileMgr.getFile(HeadersFilename))) { |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 232 | |
| 233 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 234 | std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" + |
| 235 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 236 | if (!(FE = FileMgr.getFile(PrivateHeadersFilename))) |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 237 | return 0; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 240 | // This file is a system header or C++ unfriendly if the old file is. |
| 241 | getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 242 | return FE; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 245 | //===----------------------------------------------------------------------===// |
| 246 | // File Info Management. |
| 247 | //===----------------------------------------------------------------------===// |
| 248 | |
| 249 | |
| 250 | /// getFileInfo - Return the PerFileInfo structure for the specified |
| 251 | /// FileEntry. |
| 252 | HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
| 253 | if (FE->getUID() >= FileInfo.size()) |
| 254 | FileInfo.resize(FE->getUID()+1); |
| 255 | return FileInfo[FE->getUID()]; |
| 256 | } |
| 257 | |
| 258 | /// ShouldEnterIncludeFile - Mark the specified file as a target of of a |
| 259 | /// #include, #include_next, or #import directive. Return false if #including |
| 260 | /// the file will have no effect or true if we should include it. |
| 261 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 262 | ++NumIncluded; // Count # of attempted #includes. |
| 263 | |
| 264 | // Get information about this file. |
| 265 | PerFileInfo &FileInfo = getFileInfo(File); |
| 266 | |
| 267 | // If this is a #import directive, check that we have not already imported |
| 268 | // this header. |
| 269 | if (isImport) { |
| 270 | // If this has already been imported, don't import it again. |
| 271 | FileInfo.isImport = true; |
| 272 | |
| 273 | // Has this already been #import'ed or #include'd? |
| 274 | if (FileInfo.NumIncludes) return false; |
| 275 | } else { |
| 276 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 277 | // or if this is the second #include of a #pragma once file, ignore it. |
| 278 | if (FileInfo.isImport) |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 283 | // if the macro that guards it is defined, we know the #include has no effect. |
| 284 | if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) { |
| 285 | ++NumMultiIncludeFileOptzn; |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | // Increment the number of times this file has been included. |
| 290 | ++FileInfo.NumIncludes; |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | |