Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- HeaderSearch.cpp - Resolve Header File Locations ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the DirectoryLookup and HeaderSearch interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 15 | #include "clang/Lex/HeaderMap.h" |
Chris Lattner | c7229c3 | 2007-10-07 08:58:51 +0000 | [diff] [blame] | 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/IdentifierTable.h" |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 3daed52 | 2009-03-02 22:20:04 +0000 | [diff] [blame] | 21 | #include <cstdio> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 24 | const IdentifierInfo * |
| 25 | HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) { |
| 26 | if (ControllingMacro) |
| 27 | return ControllingMacro; |
| 28 | |
| 29 | if (!ControllingMacroID || !External) |
| 30 | return 0; |
| 31 | |
| 32 | ControllingMacro = External->GetIdentifier(ControllingMacroID); |
| 33 | return ControllingMacro; |
| 34 | } |
| 35 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 36 | HeaderSearch::HeaderSearch(FileManager &FM) |
| 37 | : FileMgr(FM), FrameworkMap(64) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | SystemDirIdx = 0; |
| 39 | NoCurDirSearch = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 41 | ExternalLookup = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | NumIncluded = 0; |
| 43 | NumMultiIncludeFileOptzn = 0; |
| 44 | NumFrameworkLookups = NumSubFrameworkLookups = 0; |
| 45 | } |
| 46 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 47 | HeaderSearch::~HeaderSearch() { |
| 48 | // Delete headermaps. |
| 49 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
| 50 | delete HeaderMaps[i].second; |
| 51 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | void HeaderSearch::PrintStats() { |
| 54 | fprintf(stderr, "\n*** HeaderSearch Stats:\n"); |
| 55 | fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size()); |
| 56 | unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0; |
| 57 | for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) { |
| 58 | NumOnceOnlyFiles += FileInfo[i].isImport; |
| 59 | if (MaxNumIncludes < FileInfo[i].NumIncludes) |
| 60 | MaxNumIncludes = FileInfo[i].NumIncludes; |
| 61 | NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1; |
| 62 | } |
| 63 | fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles); |
| 64 | fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles); |
| 65 | fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 67 | fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded); |
| 68 | fprintf(stderr, " %d #includes skipped due to" |
| 69 | " the multi-include optimization.\n", NumMultiIncludeFileOptzn); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups); |
| 72 | fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups); |
| 73 | } |
| 74 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 75 | /// CreateHeaderMap - This method returns a HeaderMap for the specified |
| 76 | /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure. |
Chris Lattner | 1bfd4a6 | 2007-12-17 18:34:53 +0000 | [diff] [blame] | 77 | const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 78 | // We expect the number of headermaps to be small, and almost always empty. |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 79 | // If it ever grows, use of a linear search should be re-evaluated. |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 80 | if (!HeaderMaps.empty()) { |
| 81 | for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i) |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 82 | // Pointer equality comparison of FileEntries works because they are |
| 83 | // already uniqued by inode. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | if (HeaderMaps[i].first == FE) |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 85 | return HeaderMaps[i].second; |
| 86 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 88 | if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) { |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 89 | HeaderMaps.push_back(std::make_pair(FE, HM)); |
| 90 | return HM; |
| 91 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 822da61 | 2007-12-17 06:36:45 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 96 | //===----------------------------------------------------------------------===// |
| 97 | // File lookup within a DirectoryLookup scope |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
Chris Lattner | 3af66a9 | 2007-12-17 17:57:27 +0000 | [diff] [blame] | 100 | /// getName - Return the directory or filename corresponding to this lookup |
| 101 | /// object. |
| 102 | const char *DirectoryLookup::getName() const { |
| 103 | if (isNormalDir()) |
| 104 | return getDir()->getName(); |
| 105 | if (isFramework()) |
| 106 | return getFrameworkDir()->getName(); |
| 107 | assert(isHeaderMap() && "Unknown DirectoryLookup"); |
| 108 | return getHeaderMap()->getFileName(); |
| 109 | } |
| 110 | |
| 111 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 112 | /// LookupFile - Lookup the specified file in this search path, returning it |
| 113 | /// if it exists or returning null if not. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 114 | const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename, |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 115 | HeaderSearch &HS) const { |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 116 | llvm::SmallString<1024> TmpDir; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 117 | if (isNormalDir()) { |
| 118 | // Concatenate the requested file onto the directory. |
| 119 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 120 | TmpDir += getDir()->getName(); |
| 121 | TmpDir.push_back('/'); |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 122 | TmpDir.append(Filename.begin(), Filename.end()); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 123 | return HS.getFileMgr().getFile(TmpDir.str()); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 125 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 126 | if (isFramework()) |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 127 | return DoFrameworkLookup(Filename, HS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Chris Lattner | b09e71f | 2007-12-17 08:17:39 +0000 | [diff] [blame] | 129 | assert(isHeaderMap() && "Unknown directory lookup"); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 130 | return getHeaderMap()->LookupFile(Filename, HS.getFileMgr()); |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 134 | /// DoFrameworkLookup - Do a lookup of the specified file in the current |
| 135 | /// DirectoryLookup, which is a framework directory. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 136 | const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename, |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 137 | HeaderSearch &HS) const { |
| 138 | FileManager &FileMgr = HS.getFileMgr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 140 | // Framework names must have a '/' in the filename. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 141 | size_t SlashPos = Filename.find('/'); |
| 142 | if (SlashPos == llvm::StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 144 | // Find out if this is the home for the specified framework, by checking |
| 145 | // HeaderSearch. Possible answer are yes/no and unknown. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | const DirectoryEntry *&FrameworkDirCache = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 147 | HS.LookupFrameworkCache(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 148 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 149 | // If it is known and in some other directory, fail. |
| 150 | if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 153 | // Otherwise, construct the path to this framework dir. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | // FrameworkName = "/System/Library/Frameworks/" |
| 156 | llvm::SmallString<1024> FrameworkName; |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 157 | FrameworkName += getFrameworkDir()->getName(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | if (FrameworkName.empty() || FrameworkName.back() != '/') |
| 159 | FrameworkName.push_back('/'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | // FrameworkName = "/System/Library/Frameworks/Cocoa" |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 162 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 163 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/" |
| 165 | FrameworkName += ".framework/"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 167 | // If the cache entry is still unresolved, query to see if the cache entry is |
| 168 | // still unresolved. If so, check its existence now. |
| 169 | if (FrameworkDirCache == 0) { |
| 170 | HS.IncrementFrameworkLookupCount(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 173 | // FIXME: It's probably more efficient to query this with FileMgr.getDir. |
Michael J. Spencer | 32bef4e | 2011-01-10 02:34:13 +0000 | [diff] [blame] | 174 | bool Exists; |
| 175 | if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | // Otherwise, if it does, remember that this is the right direntry for this |
| 179 | // framework. |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 180 | FrameworkDirCache = getFrameworkDir(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h" |
| 184 | unsigned OrigSize = FrameworkName.size(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | FrameworkName += "Headers/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 187 | FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 188 | if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | return FE; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h" |
| 192 | const char *Private = "Private"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | FrameworkName.insert(FrameworkName.begin()+OrigSize, Private, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | Private+strlen(Private)); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 195 | return FileMgr.getFile(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 198 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
| 200 | // Header File Location. |
| 201 | //===----------------------------------------------------------------------===// |
| 202 | |
| 203 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file, |
| 205 | /// return null on failure. isAngled indicates whether the file reference is |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 206 | /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if |
| 207 | /// non-null, indicates where the #including file is, in case a relative search |
| 208 | /// is needed. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 209 | const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | bool isAngled, |
| 211 | const DirectoryLookup *FromDir, |
| 212 | const DirectoryLookup *&CurDir, |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 213 | const FileEntry *CurFileEnt) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | // If 'Filename' is absolute, check to see if it exists and no searching. |
Michael J. Spencer | 256053b | 2010-12-17 21:22:22 +0000 | [diff] [blame] | 215 | if (llvm::sys::path::is_absolute(Filename)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | CurDir = 0; |
| 217 | |
| 218 | // If this was an #include_next "/absolute/file", fail. |
| 219 | if (FromDir) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 221 | // Otherwise, just return the file. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 222 | return FileMgr.getFile(Filename); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | // Step #0, unless disabled, check to see if the file is in the #includer's |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 226 | // directory. This has to be based on CurFileEnt, not CurDir, because |
| 227 | // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and |
Chris Lattner | df77233 | 2007-12-17 07:52:39 +0000 | [diff] [blame] | 228 | // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h". |
| 229 | // This search is not done for <> headers. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 230 | if (CurFileEnt && !isAngled && !NoCurDirSearch) { |
| 231 | llvm::SmallString<1024> TmpDir; |
| 232 | // Concatenate the requested file onto the directory. |
| 233 | // FIXME: Portability. Filename concatenation should be in sys::Path. |
| 234 | TmpDir += CurFileEnt->getDir()->getName(); |
| 235 | TmpDir.push_back('/'); |
| 236 | TmpDir.append(Filename.begin(), Filename.end()); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 237 | if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | // Leave CurDir unset. |
Douglas Gregor | 10fe93d | 2010-08-08 07:49:23 +0000 | [diff] [blame] | 239 | // This file is a system header or C++ unfriendly if the old file is. |
| 240 | // |
| 241 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 242 | // getFileInfo could resize the vector and we don't want to rely on order |
| 243 | // of evaluation. |
| 244 | unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo; |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 245 | getFileInfo(FE).DirInfo = DirInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 246 | return FE; |
| 247 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | CurDir = 0; |
| 251 | |
| 252 | // If this is a system #include, ignore the user #include locs. |
| 253 | unsigned i = isAngled ? SystemDirIdx : 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 255 | // If this is a #include_next request, start searching after the directory the |
| 256 | // file was found in. |
| 257 | if (FromDir) |
| 258 | i = FromDir-&SearchDirs[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 260 | // Cache all of the lookups performed by this method. Many headers are |
| 261 | // multiply included, and the "pragma once" optimization prevents them from |
| 262 | // being relex/pp'd, but they would still have to search through a |
| 263 | // (potentially huge) series of SearchDirs to find it. |
| 264 | std::pair<unsigned, unsigned> &CacheLookup = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 265 | LookupFileCache.GetOrCreateValue(Filename).getValue(); |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 266 | |
| 267 | // If the entry has been previously looked up, the first value will be |
| 268 | // non-zero. If the value is equal to i (the start point of our search), then |
| 269 | // this is a matching hit. |
| 270 | if (CacheLookup.first == i+1) { |
| 271 | // Skip querying potentially lots of directories for this lookup. |
| 272 | i = CacheLookup.second; |
| 273 | } else { |
| 274 | // Otherwise, this is the first query, or the previous query didn't match |
| 275 | // our search start. We will fill in our found location below, so prime the |
| 276 | // start point value. |
| 277 | CacheLookup.first = i+1; |
| 278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | // Check each directory in sequence to see if it contains this file. |
| 281 | for (; i != SearchDirs.size(); ++i) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | const FileEntry *FE = |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 283 | SearchDirs[i].LookupFile(Filename, *this); |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 284 | if (!FE) continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 286 | CurDir = &SearchDirs[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 288 | // This file is a system header or C++ unfriendly if the dir is. |
| 289 | getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Chris Lattner | afded5b | 2007-12-17 08:13:48 +0000 | [diff] [blame] | 291 | // Remember this location for the next lookup we do. |
| 292 | CacheLookup.second = i; |
| 293 | return FE; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Chris Lattner | 9960ae8 | 2007-07-22 07:28:00 +0000 | [diff] [blame] | 296 | // Otherwise, didn't find it. Remember we didn't find this. |
| 297 | CacheLookup.second = SearchDirs.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | /// LookupSubframeworkHeader - Look up a subframework for the specified |
| 302 | /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from |
| 303 | /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox |
| 304 | /// is a subframework within Carbon.framework. If so, return the FileEntry |
| 305 | /// for the designated file, otherwise return null. |
| 306 | const FileEntry *HeaderSearch:: |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 307 | LookupSubframeworkHeader(llvm::StringRef Filename, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | const FileEntry *ContextFileEnt) { |
Chris Lattner | 9415a0c | 2008-02-01 05:34:02 +0000 | [diff] [blame] | 309 | assert(ContextFileEnt && "No context file?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | // Framework names must have a '/' in the filename. Find it. |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 312 | size_t SlashPos = Filename.find('/'); |
| 313 | if (SlashPos == llvm::StringRef::npos) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | // Look up the base framework name of the ContextFileEnt. |
| 316 | const char *ContextName = ContextFileEnt->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 318 | // If the context info wasn't a framework, couldn't be a subframework. |
| 319 | const char *FrameworkPos = strstr(ContextName, ".framework/"); |
| 320 | if (FrameworkPos == 0) |
| 321 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | |
| 323 | llvm::SmallString<1024> FrameworkName(ContextName, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 324 | FrameworkPos+strlen(".framework/")); |
| 325 | |
| 326 | // Append Frameworks/HIToolbox.framework/ |
| 327 | FrameworkName += "Frameworks/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 328 | FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 329 | FrameworkName += ".framework/"; |
| 330 | |
| 331 | llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup = |
Chris Lattner | 6538227 | 2010-11-21 09:55:08 +0000 | [diff] [blame] | 332 | FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 333 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 334 | // Some other location? |
| 335 | if (CacheLookup.getValue() && |
| 336 | CacheLookup.getKeyLength() == FrameworkName.size() && |
| 337 | memcmp(CacheLookup.getKeyData(), &FrameworkName[0], |
| 338 | CacheLookup.getKeyLength()) != 0) |
| 339 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 341 | // Cache subframework. |
| 342 | if (CacheLookup.getValue() == 0) { |
| 343 | ++NumSubFrameworkLookups; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | // If the framework dir doesn't exist, we fail. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 346 | const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | if (Dir == 0) return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | // Otherwise, if it does, remember that this is the right direntry for this |
| 350 | // framework. |
| 351 | CacheLookup.setValue(Dir); |
| 352 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 353 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 354 | const FileEntry *FE = 0; |
| 355 | |
| 356 | // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h" |
| 357 | llvm::SmallString<1024> HeadersFilename(FrameworkName); |
| 358 | HeadersFilename += "Headers/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 359 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 360 | if (!(FE = FileMgr.getFile(HeadersFilename.str()))) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h" |
| 363 | HeadersFilename = FrameworkName; |
| 364 | HeadersFilename += "PrivateHeaders/"; |
Chris Lattner | a139481 | 2010-01-10 01:35:12 +0000 | [diff] [blame] | 365 | HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end()); |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 366 | if (!(FE = FileMgr.getFile(HeadersFilename.str()))) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | return 0; |
| 368 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 370 | // This file is a system header or C++ unfriendly if the old file is. |
Ted Kremenek | ca63fa0 | 2008-02-24 03:55:14 +0000 | [diff] [blame] | 371 | // |
Chris Lattner | c9dde4f | 2008-02-25 21:38:21 +0000 | [diff] [blame] | 372 | // Note that the temporary 'DirInfo' is required here, as either call to |
| 373 | // getFileInfo could resize the vector and we don't want to rely on order |
| 374 | // of evaluation. |
| 375 | unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo; |
| 376 | getFileInfo(FE).DirInfo = DirInfo; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 377 | return FE; |
| 378 | } |
| 379 | |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | // File Info Management. |
| 382 | //===----------------------------------------------------------------------===// |
| 383 | |
| 384 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 385 | /// getFileInfo - Return the HeaderFileInfo structure for the specified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | /// FileEntry. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 387 | HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | if (FE->getUID() >= FileInfo.size()) |
| 389 | FileInfo.resize(FE->getUID()+1); |
| 390 | return FileInfo[FE->getUID()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 392 | |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 393 | void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) { |
| 394 | if (UID >= FileInfo.size()) |
| 395 | FileInfo.resize(UID+1); |
| 396 | FileInfo[UID] = HFI; |
| 397 | } |
| 398 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | /// ShouldEnterIncludeFile - Mark the specified file as a target of of a |
| 400 | /// #include, #include_next, or #import directive. Return false if #including |
| 401 | /// the file will have no effect or true if we should include it. |
| 402 | bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ |
| 403 | ++NumIncluded; // Count # of attempted #includes. |
| 404 | |
| 405 | // Get information about this file. |
Steve Naroff | 83d63c7 | 2009-04-24 20:03:17 +0000 | [diff] [blame] | 406 | HeaderFileInfo &FileInfo = getFileInfo(File); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 408 | // If this is a #import directive, check that we have not already imported |
| 409 | // this header. |
| 410 | if (isImport) { |
| 411 | // If this has already been imported, don't import it again. |
| 412 | FileInfo.isImport = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | // Has this already been #import'ed or #include'd? |
| 415 | if (FileInfo.NumIncludes) return false; |
| 416 | } else { |
| 417 | // Otherwise, if this is a #include of a file that was previously #import'd |
| 418 | // or if this is the second #include of a #pragma once file, ignore it. |
| 419 | if (FileInfo.isImport) |
| 420 | return false; |
| 421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | // Next, check to see if the file is wrapped with #ifndef guards. If so, and |
| 424 | // if the macro that guards it is defined, we know the #include has no effect. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | if (const IdentifierInfo *ControllingMacro |
Douglas Gregor | 8c5a760 | 2009-04-25 23:30:02 +0000 | [diff] [blame] | 426 | = FileInfo.getControllingMacro(ExternalLookup)) |
| 427 | if (ControllingMacro->hasMacroDefinition()) { |
| 428 | ++NumMultiIncludeFileOptzn; |
| 429 | return false; |
| 430 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 432 | // Increment the number of times this file has been included. |
| 433 | ++FileInfo.NumIncludes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | return true; |
| 436 | } |
| 437 | |
| 438 | |