blob: 5cf65cbb0dfec1fd27a1069b31a08c05c7738928 [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 Gregor21cae202011-09-12 23:31:24 +0000101const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
102 std::string *UmbrellaHeader) {
Douglas Gregor9a6da692011-09-12 20:41:59 +0000103 // If we don't have a module cache path, we can't do anything.
104 if (ModuleCachePath.empty())
105 return 0;
Douglas Gregor21cae202011-09-12 23:31:24 +0000106
107 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000108 llvm::SmallString<256> FileName(ModuleCachePath);
109 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor21cae202011-09-12 23:31:24 +0000110 if (const FileEntry *ModuleFile = getFileMgr().getFile(FileName))
111 return ModuleFile;
112
113 // We didn't find the module. If we're not supposed to look for an
114 // umbrella header, this is the end of the road.
115 if (!UmbrellaHeader)
116 return 0;
117
118 // Look in each of the framework directories for an umbrella header with
119 // the same name as the module.
120 // FIXME: We need a way for non-frameworks to provide umbrella headers.
121 llvm::SmallString<128> UmbrellaHeaderName;
122 UmbrellaHeaderName = ModuleName;
123 UmbrellaHeaderName += '/';
124 UmbrellaHeaderName += ModuleName;
125 UmbrellaHeaderName += ".h";
126 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
127 // Skip non-framework include paths
128 if (!SearchDirs[Idx].isFramework())
129 continue;
130
131 // Look for the umbrella header in this directory.
132 if (const FileEntry *HeaderFile
133 = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0)) {
134 *UmbrellaHeader = HeaderFile->getName();
135 return 0;
136 }
137 }
138
139 // We did not find an umbrella header. Clear out the UmbrellaHeader pointee
140 // so our caller knows that we failed.
141 UmbrellaHeader->clear();
142 return 0;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000143}
144
Chris Lattnerdf772332007-12-17 07:52:39 +0000145//===----------------------------------------------------------------------===//
146// File lookup within a DirectoryLookup scope
147//===----------------------------------------------------------------------===//
148
Chris Lattner3af66a92007-12-17 17:57:27 +0000149/// getName - Return the directory or filename corresponding to this lookup
150/// object.
151const char *DirectoryLookup::getName() const {
152 if (isNormalDir())
153 return getDir()->getName();
154 if (isFramework())
155 return getFrameworkDir()->getName();
156 assert(isHeaderMap() && "Unknown DirectoryLookup");
157 return getHeaderMap()->getFileName();
158}
159
160
Chris Lattnerdf772332007-12-17 07:52:39 +0000161/// LookupFile - Lookup the specified file in this search path, returning it
162/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000163const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000165 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166 SmallVectorImpl<char> *SearchPath,
167 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000168 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000169 if (isNormalDir()) {
170 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000171 TmpDir = getDir()->getName();
172 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000173 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000175 SearchPath->clear();
176 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
177 }
178 if (RelativePath != NULL) {
179 RelativePath->clear();
180 RelativePath->append(Filename.begin(), Filename.end());
181 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000182 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000183 }
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnerafded5b2007-12-17 08:13:48 +0000185 if (isFramework())
Manuel Klimek74124942011-04-26 21:50:03 +0000186 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000188 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000189 const FileEntry * const Result = getHeaderMap()->LookupFile(
190 Filename, HS.getFileMgr());
191 if (Result) {
192 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000193 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000194 SearchPath->clear();
195 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
196 }
197 if (RelativePath != NULL) {
198 RelativePath->clear();
199 RelativePath->append(Filename.begin(), Filename.end());
200 }
201 }
202 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000203}
204
205
Chris Lattnerafded5b2007-12-17 08:13:48 +0000206/// DoFrameworkLookup - Do a lookup of the specified file in the current
207/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000208const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000209 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000210 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000211 SmallVectorImpl<char> *SearchPath,
212 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000213 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Reid Spencer5f016e22007-07-11 17:01:13 +0000215 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000216 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000217 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000218
Chris Lattnerafded5b2007-12-17 08:13:48 +0000219 // Find out if this is the home for the specified framework, by checking
220 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000221 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000222 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattnerafded5b2007-12-17 08:13:48 +0000224 // If it is known and in some other directory, fail.
225 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000226 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattnerafded5b2007-12-17 08:13:48 +0000228 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Reid Spencer5f016e22007-07-11 17:01:13 +0000230 // FrameworkName = "/System/Library/Frameworks/"
231 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000232 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 if (FrameworkName.empty() || FrameworkName.back() != '/')
234 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000237 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
240 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnerafded5b2007-12-17 08:13:48 +0000242 // If the cache entry is still unresolved, query to see if the cache entry is
243 // still unresolved. If so, check its existence now.
244 if (FrameworkDirCache == 0) {
245 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000248 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000249 bool Exists;
250 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000251 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // Otherwise, if it does, remember that this is the right direntry for this
254 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000255 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 }
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Manuel Klimek74124942011-04-26 21:50:03 +0000258 if (RelativePath != NULL) {
259 RelativePath->clear();
260 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
261 }
262
Reid Spencer5f016e22007-07-11 17:01:13 +0000263 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
264 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000267
268 if (SearchPath != NULL) {
269 SearchPath->clear();
270 // Without trailing '/'.
271 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
272 }
273
Chris Lattnera1394812010-01-10 01:35:12 +0000274 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000275 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
276 /*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000277 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
281 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000282 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000284 if (SearchPath != NULL)
285 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
286 Private+strlen(Private));
287
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000288 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000289}
290
Chris Lattnerdf772332007-12-17 07:52:39 +0000291
Chris Lattnerafded5b2007-12-17 08:13:48 +0000292//===----------------------------------------------------------------------===//
293// Header File Location.
294//===----------------------------------------------------------------------===//
295
296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
298/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000299/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
300/// non-null, indicates where the #including file is, in case a relative search
301/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000302const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000303 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000304 bool isAngled,
305 const DirectoryLookup *FromDir,
306 const DirectoryLookup *&CurDir,
307 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000308 SmallVectorImpl<char> *SearchPath,
309 SmallVectorImpl<char> *RelativePath) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000310 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000311 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 CurDir = 0;
313
314 // If this was an #include_next "/absolute/file", fail.
315 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Manuel Klimek74124942011-04-26 21:50:03 +0000317 if (SearchPath != NULL)
318 SearchPath->clear();
319 if (RelativePath != NULL) {
320 RelativePath->clear();
321 RelativePath->append(Filename.begin(), Filename.end());
322 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000324 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 }
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000327 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000328 // directory. This has to be based on CurFileEnt, not CurDir, because
329 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000330 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
331 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000332 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
333 llvm::SmallString<1024> TmpDir;
334 // Concatenate the requested file onto the directory.
335 // FIXME: Portability. Filename concatenation should be in sys::Path.
336 TmpDir += CurFileEnt->getDir()->getName();
337 TmpDir.push_back('/');
338 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000339 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000341 // This file is a system header or C++ unfriendly if the old file is.
342 //
343 // Note that the temporary 'DirInfo' is required here, as either call to
344 // getFileInfo could resize the vector and we don't want to rely on order
345 // of evaluation.
346 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000347 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000348 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000349 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000350 SearchPath->clear();
351 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
352 }
353 if (RelativePath != NULL) {
354 RelativePath->clear();
355 RelativePath->append(Filename.begin(), Filename.end());
356 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000357 return FE;
358 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000359 }
Mike Stump1eb44332009-09-09 15:08:12 +0000360
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 CurDir = 0;
362
363 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000364 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000365
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 // If this is a #include_next request, start searching after the directory the
367 // file was found in.
368 if (FromDir)
369 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattner9960ae82007-07-22 07:28:00 +0000371 // Cache all of the lookups performed by this method. Many headers are
372 // multiply included, and the "pragma once" optimization prevents them from
373 // being relex/pp'd, but they would still have to search through a
374 // (potentially huge) series of SearchDirs to find it.
375 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000376 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000377
378 // If the entry has been previously looked up, the first value will be
379 // non-zero. If the value is equal to i (the start point of our search), then
380 // this is a matching hit.
381 if (CacheLookup.first == i+1) {
382 // Skip querying potentially lots of directories for this lookup.
383 i = CacheLookup.second;
384 } else {
385 // Otherwise, this is the first query, or the previous query didn't match
386 // our search start. We will fill in our found location below, so prime the
387 // start point value.
388 CacheLookup.first = i+1;
389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 // Check each directory in sequence to see if it contains this file.
392 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000393 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000394 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000395 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Chris Lattnerafded5b2007-12-17 08:13:48 +0000397 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Chris Lattnerafded5b2007-12-17 08:13:48 +0000399 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000400 HeaderFileInfo &HFI = getFileInfo(FE);
401 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000403 // If this file is found in a header map and uses the framework style of
404 // includes, then this header is part of a framework we're building.
405 if (CurDir->isIndexHeaderMap()) {
406 size_t SlashPos = Filename.find('/');
407 if (SlashPos != StringRef::npos) {
408 HFI.IndexHeaderMapHeader = 1;
409 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
410 SlashPos));
411 }
412 }
413
Chris Lattnerafded5b2007-12-17 08:13:48 +0000414 // Remember this location for the next lookup we do.
415 CacheLookup.second = i;
416 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 }
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000419 // If we are including a file with a quoted include "foo.h" from inside
420 // a header in a framework that is currently being built, and we couldn't
421 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
422 // "Foo" is the name of the framework in which the including header was found.
423 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
424 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
425 if (IncludingHFI.IndexHeaderMapHeader) {
426 llvm::SmallString<128> ScratchFilename;
427 ScratchFilename += IncludingHFI.Framework;
428 ScratchFilename += '/';
429 ScratchFilename += Filename;
430
431 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
432 FromDir, CurDir, CurFileEnt,
433 SearchPath, RelativePath);
434 std::pair<unsigned, unsigned> &CacheLookup
435 = LookupFileCache.GetOrCreateValue(Filename).getValue();
436 CacheLookup.second
437 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
438 return Result;
439 }
440 }
441
Chris Lattner9960ae82007-07-22 07:28:00 +0000442 // Otherwise, didn't find it. Remember we didn't find this.
443 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 return 0;
445}
446
447/// LookupSubframeworkHeader - Look up a subframework for the specified
448/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
449/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
450/// is a subframework within Carbon.framework. If so, return the FileEntry
451/// for the designated file, otherwise return null.
452const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000453LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000454 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000455 SmallVectorImpl<char> *SearchPath,
456 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000457 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000460 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000461 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 // Look up the base framework name of the ContextFileEnt.
464 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000465
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 // If the context info wasn't a framework, couldn't be a subframework.
467 const char *FrameworkPos = strstr(ContextName, ".framework/");
468 if (FrameworkPos == 0)
469 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
471 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 FrameworkPos+strlen(".framework/"));
473
474 // Append Frameworks/HIToolbox.framework/
475 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000476 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 FrameworkName += ".framework/";
478
479 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000480 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Reid Spencer5f016e22007-07-11 17:01:13 +0000482 // Some other location?
483 if (CacheLookup.getValue() &&
484 CacheLookup.getKeyLength() == FrameworkName.size() &&
485 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
486 CacheLookup.getKeyLength()) != 0)
487 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 // Cache subframework.
490 if (CacheLookup.getValue() == 0) {
491 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000494 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000495 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 // Otherwise, if it does, remember that this is the right direntry for this
498 // framework.
499 CacheLookup.setValue(Dir);
500 }
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 const FileEntry *FE = 0;
503
Manuel Klimek74124942011-04-26 21:50:03 +0000504 if (RelativePath != NULL) {
505 RelativePath->clear();
506 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
507 }
508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
510 llvm::SmallString<1024> HeadersFilename(FrameworkName);
511 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000512 if (SearchPath != NULL) {
513 SearchPath->clear();
514 // Without trailing '/'.
515 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
516 }
517
Chris Lattnera1394812010-01-10 01:35:12 +0000518 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000519 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
522 HeadersFilename = FrameworkName;
523 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000524 if (SearchPath != NULL) {
525 SearchPath->clear();
526 // Without trailing '/'.
527 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
528 }
529
Chris Lattnera1394812010-01-10 01:35:12 +0000530 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000531 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 return 0;
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000536 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000537 // Note that the temporary 'DirInfo' is required here, as either call to
538 // getFileInfo could resize the vector and we don't want to rely on order
539 // of evaluation.
540 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
541 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 return FE;
543}
544
545//===----------------------------------------------------------------------===//
546// File Info Management.
547//===----------------------------------------------------------------------===//
548
549
Steve Naroff83d63c72009-04-24 20:03:17 +0000550/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000551/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000552HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000553 if (FE->getUID() >= FileInfo.size())
554 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000555
556 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
557 if (ExternalSource && !HFI.Resolved) {
558 HFI = ExternalSource->GetHeaderFileInfo(FE);
559 HFI.Resolved = true;
560 }
561 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000562}
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
Douglas Gregordd3e5542011-05-04 00:14:37 +0000564bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
565 // Check if we've ever seen this file as a header.
566 if (File->getUID() >= FileInfo.size())
567 return false;
568
569 // Resolve header file info from the external source, if needed.
570 HeaderFileInfo &HFI = FileInfo[File->getUID()];
571 if (ExternalSource && !HFI.Resolved) {
572 HFI = ExternalSource->GetHeaderFileInfo(File);
573 HFI.Resolved = true;
574 }
575
576 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
577}
578
Steve Naroff83d63c72009-04-24 20:03:17 +0000579void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
580 if (UID >= FileInfo.size())
581 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000582 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000583 FileInfo[UID] = HFI;
584}
585
Reid Spencer5f016e22007-07-11 17:01:13 +0000586/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
587/// #include, #include_next, or #import directive. Return false if #including
588/// the file will have no effect or true if we should include it.
589bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
590 ++NumIncluded; // Count # of attempted #includes.
591
592 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000593 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 // If this is a #import directive, check that we have not already imported
596 // this header.
597 if (isImport) {
598 // If this has already been imported, don't import it again.
599 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Reid Spencer5f016e22007-07-11 17:01:13 +0000601 // Has this already been #import'ed or #include'd?
602 if (FileInfo.NumIncludes) return false;
603 } else {
604 // Otherwise, if this is a #include of a file that was previously #import'd
605 // or if this is the second #include of a #pragma once file, ignore it.
606 if (FileInfo.isImport)
607 return false;
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
611 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000612 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000613 = FileInfo.getControllingMacro(ExternalLookup))
614 if (ControllingMacro->hasMacroDefinition()) {
615 ++NumMultiIncludeFileOptzn;
616 return false;
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 // Increment the number of times this file has been included.
620 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Reid Spencer5f016e22007-07-11 17:01:13 +0000622 return true;
623}
624
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000625size_t HeaderSearch::getTotalMemory() const {
626 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000627 + llvm::capacity_in_bytes(FileInfo)
628 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000629 + LookupFileCache.getAllocator().getTotalMemory()
630 + FrameworkMap.getAllocator().getTotalMemory();
631}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000632
633StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
634 return FrameworkNames.GetOrCreateValue(Framework).getKey();
635}