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