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 | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 63 | // TODO: caching. |
| 64 | ++NumFrameworkLookups; |
| 65 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 66 | // FrameworkName = "/System/Library/Frameworks/" |
| 67 | std::string FrameworkName = Dir->getName(); |
| 68 | if (FrameworkName.empty() || FrameworkName[FrameworkName.size()-1] != '/') |
| 69 | FrameworkName += '/'; |
| 70 | |
| 71 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
| 72 | FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos); |
| 73 | |
| 74 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 75 | FrameworkName += ".framework/"; |
| 76 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 77 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 78 | std::string HeadersFilename = FrameworkName + "Headers/" + |
| 79 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 80 | if (const FileEntry *FE = FileMgr.getFile(HeadersFilename)) |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 81 | return FE; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 82 | |
| 83 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 84 | std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" + |
| 85 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 86 | return FileMgr.getFile(PrivateHeadersFilename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 90 | /// return null on failure. isAngled indicates whether the file reference is |
| 91 | /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 92 | /// non-null, indicates where the #including file is, in case a relative search |
| 93 | /// is needed. |
| 94 | const FileEntry *HeaderSearch::LookupFile(const std::string &Filename, |
| 95 | bool isAngled, |
| 96 | const DirectoryLookup *FromDir, |
| 97 | const DirectoryLookup *&CurDir, |
| 98 | const FileEntry *CurFileEnt) { |
| 99 | // If 'Filename' is absolute, check to see if it exists and no searching. |
| 100 | // FIXME: Portability. This should be a sys::Path interface, this doesn't |
| 101 | // handle things like C:\foo.txt right, nor win32 \\network\device\blah. |
| 102 | if (Filename[0] == '/') { |
| 103 | CurDir = 0; |
| 104 | |
| 105 | // If this was an #include_next "/absolute/file", fail. |
| 106 | if (FromDir) return 0; |
| 107 | |
| 108 | // Otherwise, just return the file. |
| 109 | return FileMgr.getFile(Filename); |
| 110 | } |
| 111 | |
| 112 | // Step #0, unless disabled, check to see if the file is in the #includer's |
| 113 | // directory. This search is not done for <> headers. |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 114 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 115 | // Concatenate the requested file onto the directory. |
| 116 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 117 | if (const FileEntry *FE = |
| 118 | FileMgr.getFile(CurFileEnt->getDir()->getName()+"/"+Filename)) { |
| 119 | // Leave CurDir unset. |
| 120 | |
| 121 | // 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] | 122 | getFileInfo(FE).DirInfo = getFileInfo(CurFileEnt).DirInfo; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 123 | return FE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | CurDir = 0; |
| 128 | |
| 129 | // If this is a system #include, ignore the user #include locs. |
| 130 | unsigned i = isAngled ? SystemDirIdx : 0; |
| 131 | |
| 132 | // If this is a #include_next request, start searching after the directory the |
| 133 | // file was found in. |
| 134 | if (FromDir) |
| 135 | i = FromDir-&SearchDirs[0]; |
| 136 | |
| 137 | // Check each directory in sequence to see if it contains this file. |
| 138 | for (; i != SearchDirs.size(); ++i) { |
| 139 | // Concatenate the requested file onto the directory. |
| 140 | std::string SearchDir; |
| 141 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 142 | const FileEntry *FE = 0; |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 143 | if (!SearchDirs[i].isFramework()) { |
| 144 | // FIXME: Portability. Adding file to dir should be in sys::Path. |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 145 | FE = FileMgr.getFile(SearchDirs[i].getDir()->getName()+"/"+Filename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 146 | } else { |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 147 | FE = DoFrameworkLookup(SearchDirs[i].getDir(), Filename); |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 150 | if (FE) { |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 151 | CurDir = &SearchDirs[i]; |
| 152 | |
| 153 | // This file is a system header or C++ unfriendly if the dir is. |
| 154 | getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic(); |
| 155 | return FE; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Otherwise, didn't find it. |
| 160 | return 0; |
| 161 | } |
| 162 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 163 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
| 164 | /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from |
| 165 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 166 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 167 | /// for the designated file, otherwise return null. |
| 168 | const FileEntry *HeaderSearch:: |
| 169 | LookupSubframeworkHeader(const std::string &Filename, |
| 170 | const FileEntry *ContextFileEnt) { |
| 171 | // Framework names must have a '/' in the filename. Find it. |
| 172 | std::string::size_type SlashPos = Filename.find('/'); |
| 173 | if (SlashPos == std::string::npos) return 0; |
| 174 | |
| 175 | // TODO: Cache subframework. |
Chris Lattner | 641a0be | 2006-10-20 06:23:14 +0000 | [diff] [blame] | 176 | ++NumSubFrameworkLookups; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 177 | |
| 178 | // Look up the base framework name of the ContextFileEnt. |
| 179 | const std::string &ContextName = ContextFileEnt->getName(); |
| 180 | std::string::size_type FrameworkPos = ContextName.find(".framework/"); |
| 181 | // If the context info wasn't a framework, couldn't be a subframework. |
| 182 | if (FrameworkPos == std::string::npos) |
| 183 | return 0; |
| 184 | |
| 185 | std::string FrameworkName(ContextName.begin(), |
| 186 | ContextName.begin()+FrameworkPos+strlen(".framework/")); |
| 187 | // Append Frameworks/HIToolbox.framework/ |
| 188 | FrameworkName += "Frameworks/"; |
| 189 | FrameworkName += std::string(Filename.begin(), Filename.begin()+SlashPos); |
| 190 | FrameworkName += ".framework/"; |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 191 | |
| 192 | const FileEntry *FE = 0; |
| 193 | |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 194 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
| 195 | std::string HeadersFilename = FrameworkName + "Headers/" + |
| 196 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 197 | if (!(FE = FileMgr.getFile(HeadersFilename))) { |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 198 | |
| 199 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 200 | std::string PrivateHeadersFilename = FrameworkName + "PrivateHeaders/" + |
| 201 | std::string(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 202 | if (!(FE = FileMgr.getFile(PrivateHeadersFilename))) |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 203 | return 0; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | 577377e | 2006-10-20 04:55:45 +0000 | [diff] [blame] | 206 | // This file is a system header or C++ unfriendly if the old file is. |
| 207 | getFileInfo(FE).DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 208 | return FE; |
Chris Lattner | 63dd32b | 2006-10-20 04:42:40 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Chris Lattner | 59a9ebd | 2006-10-18 05:34:33 +0000 | [diff] [blame] | 211 | //===----------------------------------------------------------------------===// |
| 212 | // File Info Management. |
| 213 | //===----------------------------------------------------------------------===// |
| 214 | |
| 215 | |
| 216 | /// getFileInfo - Return the PerFileInfo structure for the specified |
| 217 | /// FileEntry. |
| 218 | HeaderSearch::PerFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
| 219 | if (FE->getUID() >= FileInfo.size()) |
| 220 | FileInfo.resize(FE->getUID()+1); |
| 221 | return FileInfo[FE->getUID()]; |
| 222 | } |
| 223 | |
| 224 | /// ShouldEnterIncludeFile - Mark the specified file as a target of of a |
| 225 | /// #include, #include_next, or #import directive. Return false if #including |
| 226 | /// the file will have no effect or true if we should include it. |
| 227 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 228 | ++NumIncluded; // Count # of attempted #includes. |
| 229 | |
| 230 | // Get information about this file. |
| 231 | PerFileInfo &FileInfo = getFileInfo(File); |
| 232 | |
| 233 | // If this is a #import directive, check that we have not already imported |
| 234 | // this header. |
| 235 | if (isImport) { |
| 236 | // If this has already been imported, don't import it again. |
| 237 | FileInfo.isImport = true; |
| 238 | |
| 239 | // Has this already been #import'ed or #include'd? |
| 240 | if (FileInfo.NumIncludes) return false; |
| 241 | } else { |
| 242 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 243 | // or if this is the second #include of a #pragma once file, ignore it. |
| 244 | if (FileInfo.isImport) |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 249 | // if the macro that guards it is defined, we know the #include has no effect. |
| 250 | if (FileInfo.ControllingMacro && FileInfo.ControllingMacro->getMacroInfo()) { |
| 251 | ++NumMultiIncludeFileOptzn; |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | // Increment the number of times this file has been included. |
| 256 | ++FileInfo.NumIncludes; |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | |