blob: 36826756b8b8a6fcedb6ba65f58530a4a6b9ea37 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the DirectoryLookup and HeaderSearch interfaces.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer5f016e22007-07-11 17:01:13 +000014#include "clang/Lex/HeaderSearch.h"
Chris Lattner822da612007-12-17 06:36:45 +000015#include "clang/Lex/HeaderMap.h"
Chris Lattnerc7229c32007-10-07 08:58:51 +000016#include "clang/Basic/FileManager.h"
17#include "clang/Basic/IdentifierTable.h"
Michael J. Spencer32bef4e2011-01-10 02:34:13 +000018#include "llvm/Support/FileSystem.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000019#include "llvm/Support/Path.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "llvm/ADT/SmallString.h"
Ted Kremenekeabea452011-07-27 18:41:18 +000021#include "llvm/Support/Capacity.h"
Chris Lattner3daed522009-03-02 22:20:04 +000022#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
Douglas Gregor8c5a7602009-04-25 23:30:02 +000025const IdentifierInfo *
26HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
27 if (ControllingMacro)
28 return ControllingMacro;
29
30 if (!ControllingMacroID || !External)
31 return 0;
32
33 ControllingMacro = External->GetIdentifier(ControllingMacroID);
34 return ControllingMacro;
35}
36
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000037ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
38
Chris Lattner39b49bc2010-11-23 08:35:12 +000039HeaderSearch::HeaderSearch(FileManager &FM)
40 : FileMgr(FM), FrameworkMap(64) {
Nico Weber74a5fd82011-05-24 04:31:14 +000041 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000042 SystemDirIdx = 0;
43 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Douglas Gregor8c5a7602009-04-25 23:30:02 +000045 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000046 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000047 NumIncluded = 0;
48 NumMultiIncludeFileOptzn = 0;
49 NumFrameworkLookups = NumSubFrameworkLookups = 0;
50}
51
Chris Lattner822da612007-12-17 06:36:45 +000052HeaderSearch::~HeaderSearch() {
53 // Delete headermaps.
54 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
55 delete HeaderMaps[i].second;
56}
Mike Stump1eb44332009-09-09 15:08:12 +000057
Reid Spencer5f016e22007-07-11 17:01:13 +000058void HeaderSearch::PrintStats() {
59 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
60 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
61 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
62 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
63 NumOnceOnlyFiles += FileInfo[i].isImport;
64 if (MaxNumIncludes < FileInfo[i].NumIncludes)
65 MaxNumIncludes = FileInfo[i].NumIncludes;
66 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
67 }
68 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
69 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
70 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Reid Spencer5f016e22007-07-11 17:01:13 +000072 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
73 fprintf(stderr, " %d #includes skipped due to"
74 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Reid Spencer5f016e22007-07-11 17:01:13 +000076 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
77 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
78}
79
Chris Lattner822da612007-12-17 06:36:45 +000080/// CreateHeaderMap - This method returns a HeaderMap for the specified
81/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000082const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000083 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000084 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000085 if (!HeaderMaps.empty()) {
86 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000087 // Pointer equality comparison of FileEntries works because they are
88 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000089 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000090 return HeaderMaps[i].second;
91 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner39b49bc2010-11-23 08:35:12 +000093 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000094 HeaderMaps.push_back(std::make_pair(FE, HM));
95 return HM;
96 }
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner822da612007-12-17 06:36:45 +000098 return 0;
99}
100
Douglas Gregor9a6da692011-09-12 20:41:59 +0000101const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName) {
102 // If we don't have a module cache path, we can't do anything.
103 if (ModuleCachePath.empty())
104 return 0;
105
106 llvm::SmallString<256> FileName(ModuleCachePath);
107 llvm::sys::path::append(FileName, ModuleName + ".pcm");
108 return getFileMgr().getFile(FileName);
109}
110
Chris Lattnerdf772332007-12-17 07:52:39 +0000111//===----------------------------------------------------------------------===//
112// File lookup within a DirectoryLookup scope
113//===----------------------------------------------------------------------===//
114
Chris Lattner3af66a92007-12-17 17:57:27 +0000115/// getName - Return the directory or filename corresponding to this lookup
116/// object.
117const char *DirectoryLookup::getName() const {
118 if (isNormalDir())
119 return getDir()->getName();
120 if (isFramework())
121 return getFrameworkDir()->getName();
122 assert(isHeaderMap() && "Unknown DirectoryLookup");
123 return getHeaderMap()->getFileName();
124}
125
126
Chris Lattnerdf772332007-12-17 07:52:39 +0000127/// LookupFile - Lookup the specified file in this search path, returning it
128/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000129const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000131 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000132 SmallVectorImpl<char> *SearchPath,
133 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000134 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000135 if (isNormalDir()) {
136 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000137 TmpDir = getDir()->getName();
138 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000139 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000140 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000141 SearchPath->clear();
142 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
143 }
144 if (RelativePath != NULL) {
145 RelativePath->clear();
146 RelativePath->append(Filename.begin(), Filename.end());
147 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000148 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000149 }
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Chris Lattnerafded5b2007-12-17 08:13:48 +0000151 if (isFramework())
Manuel Klimek74124942011-04-26 21:50:03 +0000152 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000154 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000155 const FileEntry * const Result = getHeaderMap()->LookupFile(
156 Filename, HS.getFileMgr());
157 if (Result) {
158 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000159 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000160 SearchPath->clear();
161 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
162 }
163 if (RelativePath != NULL) {
164 RelativePath->clear();
165 RelativePath->append(Filename.begin(), Filename.end());
166 }
167 }
168 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000169}
170
171
Chris Lattnerafded5b2007-12-17 08:13:48 +0000172/// DoFrameworkLookup - Do a lookup of the specified file in the current
173/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000174const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000176 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 SmallVectorImpl<char> *SearchPath,
178 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000179 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000182 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000183 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnerafded5b2007-12-17 08:13:48 +0000185 // Find out if this is the home for the specified framework, by checking
186 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000187 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000188 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000189
Chris Lattnerafded5b2007-12-17 08:13:48 +0000190 // If it is known and in some other directory, fail.
191 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Chris Lattnerafded5b2007-12-17 08:13:48 +0000194 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // FrameworkName = "/System/Library/Frameworks/"
197 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000198 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000199 if (FrameworkName.empty() || FrameworkName.back() != '/')
200 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000203 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
206 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Chris Lattnerafded5b2007-12-17 08:13:48 +0000208 // If the cache entry is still unresolved, query to see if the cache entry is
209 // still unresolved. If so, check its existence now.
210 if (FrameworkDirCache == 0) {
211 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000214 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000215 bool Exists;
216 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000217 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 // Otherwise, if it does, remember that this is the right direntry for this
220 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000221 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 }
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Manuel Klimek74124942011-04-26 21:50:03 +0000224 if (RelativePath != NULL) {
225 RelativePath->clear();
226 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
227 }
228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
230 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000233
234 if (SearchPath != NULL) {
235 SearchPath->clear();
236 // Without trailing '/'.
237 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
238 }
239
Chris Lattnera1394812010-01-10 01:35:12 +0000240 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000241 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
242 /*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
247 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000248 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000249 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000250 if (SearchPath != NULL)
251 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
252 Private+strlen(Private));
253
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000254 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000255}
256
Chris Lattnerdf772332007-12-17 07:52:39 +0000257
Chris Lattnerafded5b2007-12-17 08:13:48 +0000258//===----------------------------------------------------------------------===//
259// Header File Location.
260//===----------------------------------------------------------------------===//
261
262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
264/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000265/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
266/// non-null, indicates where the #including file is, in case a relative search
267/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000268const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000269 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000270 bool isAngled,
271 const DirectoryLookup *FromDir,
272 const DirectoryLookup *&CurDir,
273 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000274 SmallVectorImpl<char> *SearchPath,
275 SmallVectorImpl<char> *RelativePath) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000277 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 CurDir = 0;
279
280 // If this was an #include_next "/absolute/file", fail.
281 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Manuel Klimek74124942011-04-26 21:50:03 +0000283 if (SearchPath != NULL)
284 SearchPath->clear();
285 if (RelativePath != NULL) {
286 RelativePath->clear();
287 RelativePath->append(Filename.begin(), Filename.end());
288 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000289 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000290 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000293 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000294 // directory. This has to be based on CurFileEnt, not CurDir, because
295 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000296 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
297 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000298 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
299 llvm::SmallString<1024> TmpDir;
300 // Concatenate the requested file onto the directory.
301 // FIXME: Portability. Filename concatenation should be in sys::Path.
302 TmpDir += CurFileEnt->getDir()->getName();
303 TmpDir.push_back('/');
304 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000305 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000306 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000307 // This file is a system header or C++ unfriendly if the old file is.
308 //
309 // Note that the temporary 'DirInfo' is required here, as either call to
310 // getFileInfo could resize the vector and we don't want to rely on order
311 // of evaluation.
312 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000313 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000314 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000315 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000316 SearchPath->clear();
317 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
318 }
319 if (RelativePath != NULL) {
320 RelativePath->clear();
321 RelativePath->append(Filename.begin(), Filename.end());
322 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 return FE;
324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327 CurDir = 0;
328
329 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000330 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 // If this is a #include_next request, start searching after the directory the
333 // file was found in.
334 if (FromDir)
335 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Chris Lattner9960ae82007-07-22 07:28:00 +0000337 // Cache all of the lookups performed by this method. Many headers are
338 // multiply included, and the "pragma once" optimization prevents them from
339 // being relex/pp'd, but they would still have to search through a
340 // (potentially huge) series of SearchDirs to find it.
341 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000342 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000343
344 // If the entry has been previously looked up, the first value will be
345 // non-zero. If the value is equal to i (the start point of our search), then
346 // this is a matching hit.
347 if (CacheLookup.first == i+1) {
348 // Skip querying potentially lots of directories for this lookup.
349 i = CacheLookup.second;
350 } else {
351 // Otherwise, this is the first query, or the previous query didn't match
352 // our search start. We will fill in our found location below, so prime the
353 // start point value.
354 CacheLookup.first = i+1;
355 }
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 // Check each directory in sequence to see if it contains this file.
358 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000359 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000360 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000361 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattnerafded5b2007-12-17 08:13:48 +0000363 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000364
Chris Lattnerafded5b2007-12-17 08:13:48 +0000365 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000366 HeaderFileInfo &HFI = getFileInfo(FE);
367 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000369 // If this file is found in a header map and uses the framework style of
370 // includes, then this header is part of a framework we're building.
371 if (CurDir->isIndexHeaderMap()) {
372 size_t SlashPos = Filename.find('/');
373 if (SlashPos != StringRef::npos) {
374 HFI.IndexHeaderMapHeader = 1;
375 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
376 SlashPos));
377 }
378 }
379
Chris Lattnerafded5b2007-12-17 08:13:48 +0000380 // Remember this location for the next lookup we do.
381 CacheLookup.second = i;
382 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 }
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000385 // If we are including a file with a quoted include "foo.h" from inside
386 // a header in a framework that is currently being built, and we couldn't
387 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
388 // "Foo" is the name of the framework in which the including header was found.
389 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
390 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
391 if (IncludingHFI.IndexHeaderMapHeader) {
392 llvm::SmallString<128> ScratchFilename;
393 ScratchFilename += IncludingHFI.Framework;
394 ScratchFilename += '/';
395 ScratchFilename += Filename;
396
397 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
398 FromDir, CurDir, CurFileEnt,
399 SearchPath, RelativePath);
400 std::pair<unsigned, unsigned> &CacheLookup
401 = LookupFileCache.GetOrCreateValue(Filename).getValue();
402 CacheLookup.second
403 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
404 return Result;
405 }
406 }
407
Chris Lattner9960ae82007-07-22 07:28:00 +0000408 // Otherwise, didn't find it. Remember we didn't find this.
409 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 return 0;
411}
412
413/// LookupSubframeworkHeader - Look up a subframework for the specified
414/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
415/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
416/// is a subframework within Carbon.framework. If so, return the FileEntry
417/// for the designated file, otherwise return null.
418const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000419LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000420 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000421 SmallVectorImpl<char> *SearchPath,
422 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000423 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000426 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000427 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 // Look up the base framework name of the ContextFileEnt.
430 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 // If the context info wasn't a framework, couldn't be a subframework.
433 const char *FrameworkPos = strstr(ContextName, ".framework/");
434 if (FrameworkPos == 0)
435 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
437 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 FrameworkPos+strlen(".framework/"));
439
440 // Append Frameworks/HIToolbox.framework/
441 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000442 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 FrameworkName += ".framework/";
444
445 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000446 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 // Some other location?
449 if (CacheLookup.getValue() &&
450 CacheLookup.getKeyLength() == FrameworkName.size() &&
451 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
452 CacheLookup.getKeyLength()) != 0)
453 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 // Cache subframework.
456 if (CacheLookup.getValue() == 0) {
457 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000460 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 // Otherwise, if it does, remember that this is the right direntry for this
464 // framework.
465 CacheLookup.setValue(Dir);
466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468 const FileEntry *FE = 0;
469
Manuel Klimek74124942011-04-26 21:50:03 +0000470 if (RelativePath != NULL) {
471 RelativePath->clear();
472 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
473 }
474
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
476 llvm::SmallString<1024> HeadersFilename(FrameworkName);
477 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000478 if (SearchPath != NULL) {
479 SearchPath->clear();
480 // Without trailing '/'.
481 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
482 }
483
Chris Lattnera1394812010-01-10 01:35:12 +0000484 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000485 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000486
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
488 HeadersFilename = FrameworkName;
489 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000490 if (SearchPath != NULL) {
491 SearchPath->clear();
492 // Without trailing '/'.
493 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
494 }
495
Chris Lattnera1394812010-01-10 01:35:12 +0000496 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000497 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return 0;
499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000502 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000503 // Note that the temporary 'DirInfo' is required here, as either call to
504 // getFileInfo could resize the vector and we don't want to rely on order
505 // of evaluation.
506 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
507 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000508 return FE;
509}
510
511//===----------------------------------------------------------------------===//
512// File Info Management.
513//===----------------------------------------------------------------------===//
514
515
Steve Naroff83d63c72009-04-24 20:03:17 +0000516/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000517/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000518HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000519 if (FE->getUID() >= FileInfo.size())
520 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000521
522 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
523 if (ExternalSource && !HFI.Resolved) {
524 HFI = ExternalSource->GetHeaderFileInfo(FE);
525 HFI.Resolved = true;
526 }
527 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000528}
Reid Spencer5f016e22007-07-11 17:01:13 +0000529
Douglas Gregordd3e5542011-05-04 00:14:37 +0000530bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
531 // Check if we've ever seen this file as a header.
532 if (File->getUID() >= FileInfo.size())
533 return false;
534
535 // Resolve header file info from the external source, if needed.
536 HeaderFileInfo &HFI = FileInfo[File->getUID()];
537 if (ExternalSource && !HFI.Resolved) {
538 HFI = ExternalSource->GetHeaderFileInfo(File);
539 HFI.Resolved = true;
540 }
541
542 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
543}
544
Steve Naroff83d63c72009-04-24 20:03:17 +0000545void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
546 if (UID >= FileInfo.size())
547 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000548 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000549 FileInfo[UID] = HFI;
550}
551
Reid Spencer5f016e22007-07-11 17:01:13 +0000552/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
553/// #include, #include_next, or #import directive. Return false if #including
554/// the file will have no effect or true if we should include it.
555bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
556 ++NumIncluded; // Count # of attempted #includes.
557
558 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000559 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Reid Spencer5f016e22007-07-11 17:01:13 +0000561 // If this is a #import directive, check that we have not already imported
562 // this header.
563 if (isImport) {
564 // If this has already been imported, don't import it again.
565 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 // Has this already been #import'ed or #include'd?
568 if (FileInfo.NumIncludes) return false;
569 } else {
570 // Otherwise, if this is a #include of a file that was previously #import'd
571 // or if this is the second #include of a #pragma once file, ignore it.
572 if (FileInfo.isImport)
573 return false;
574 }
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
577 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000578 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000579 = FileInfo.getControllingMacro(ExternalLookup))
580 if (ControllingMacro->hasMacroDefinition()) {
581 ++NumMultiIncludeFileOptzn;
582 return false;
583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Reid Spencer5f016e22007-07-11 17:01:13 +0000585 // Increment the number of times this file has been included.
586 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 return true;
589}
590
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000591size_t HeaderSearch::getTotalMemory() const {
592 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000593 + llvm::capacity_in_bytes(FileInfo)
594 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000595 + LookupFileCache.getAllocator().getTotalMemory()
596 + FrameworkMap.getAllocator().getTotalMemory();
597}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000598
599StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
600 return FrameworkNames.GetOrCreateValue(Framework).getKey();
601}