blob: 2592ada676a1a0dfaf2b8bfd70d9cea68964157c [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
Chris Lattnerdf772332007-12-17 07:52:39 +0000101//===----------------------------------------------------------------------===//
102// File lookup within a DirectoryLookup scope
103//===----------------------------------------------------------------------===//
104
Chris Lattner3af66a92007-12-17 17:57:27 +0000105/// getName - Return the directory or filename corresponding to this lookup
106/// object.
107const char *DirectoryLookup::getName() const {
108 if (isNormalDir())
109 return getDir()->getName();
110 if (isFramework())
111 return getFrameworkDir()->getName();
112 assert(isHeaderMap() && "Unknown DirectoryLookup");
113 return getHeaderMap()->getFileName();
114}
115
116
Chris Lattnerdf772332007-12-17 07:52:39 +0000117/// LookupFile - Lookup the specified file in this search path, returning it
118/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000119const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000120 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000121 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000122 SmallVectorImpl<char> *SearchPath,
123 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000124 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000125 if (isNormalDir()) {
126 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000127 TmpDir = getDir()->getName();
128 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000129 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000130 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000131 SearchPath->clear();
132 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
133 }
134 if (RelativePath != NULL) {
135 RelativePath->clear();
136 RelativePath->append(Filename.begin(), Filename.end());
137 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000138 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattnerafded5b2007-12-17 08:13:48 +0000141 if (isFramework())
Manuel Klimek74124942011-04-26 21:50:03 +0000142 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump1eb44332009-09-09 15:08:12 +0000143
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000144 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000145 const FileEntry * const Result = getHeaderMap()->LookupFile(
146 Filename, HS.getFileMgr());
147 if (Result) {
148 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000149 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000150 SearchPath->clear();
151 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
152 }
153 if (RelativePath != NULL) {
154 RelativePath->clear();
155 RelativePath->append(Filename.begin(), Filename.end());
156 }
157 }
158 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000159}
160
161
Chris Lattnerafded5b2007-12-17 08:13:48 +0000162/// DoFrameworkLookup - Do a lookup of the specified file in the current
163/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000164const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000165 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000166 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000167 SmallVectorImpl<char> *SearchPath,
168 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000169 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000172 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000173 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chris Lattnerafded5b2007-12-17 08:13:48 +0000175 // Find out if this is the home for the specified framework, by checking
176 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000177 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000178 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Chris Lattnerafded5b2007-12-17 08:13:48 +0000180 // If it is known and in some other directory, fail.
181 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000182 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000183
Chris Lattnerafded5b2007-12-17 08:13:48 +0000184 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 // FrameworkName = "/System/Library/Frameworks/"
187 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000188 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000189 if (FrameworkName.empty() || FrameworkName.back() != '/')
190 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000193 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
196 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattnerafded5b2007-12-17 08:13:48 +0000198 // If the cache entry is still unresolved, query to see if the cache entry is
199 // still unresolved. If so, check its existence now.
200 if (FrameworkDirCache == 0) {
201 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Reid Spencer5f016e22007-07-11 17:01:13 +0000203 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000204 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000205 bool Exists;
206 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 // Otherwise, if it does, remember that this is the right direntry for this
210 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000211 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Manuel Klimek74124942011-04-26 21:50:03 +0000214 if (RelativePath != NULL) {
215 RelativePath->clear();
216 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
217 }
218
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
220 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000223
224 if (SearchPath != NULL) {
225 SearchPath->clear();
226 // Without trailing '/'.
227 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
228 }
229
Chris Lattnera1394812010-01-10 01:35:12 +0000230 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000231 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
232 /*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000233 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
237 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000238 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000239 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000240 if (SearchPath != NULL)
241 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
242 Private+strlen(Private));
243
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000244 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
Chris Lattnerdf772332007-12-17 07:52:39 +0000247
Chris Lattnerafded5b2007-12-17 08:13:48 +0000248//===----------------------------------------------------------------------===//
249// Header File Location.
250//===----------------------------------------------------------------------===//
251
252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
254/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000255/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
256/// non-null, indicates where the #including file is, in case a relative search
257/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000258const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000259 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000260 bool isAngled,
261 const DirectoryLookup *FromDir,
262 const DirectoryLookup *&CurDir,
263 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264 SmallVectorImpl<char> *SearchPath,
265 SmallVectorImpl<char> *RelativePath) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000267 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 CurDir = 0;
269
270 // If this was an #include_next "/absolute/file", fail.
271 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Manuel Klimek74124942011-04-26 21:50:03 +0000273 if (SearchPath != NULL)
274 SearchPath->clear();
275 if (RelativePath != NULL) {
276 RelativePath->clear();
277 RelativePath->append(Filename.begin(), Filename.end());
278 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000279 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000280 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 }
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000283 // If we are including a file with a quoted include "foo.h" from inside
284 // a header in a framework that is currently being built, change the include
285 // to <Foo/foo.h>, where "Foo" is the name of the framework in which the
286 // including header was found.
287 llvm::SmallString<128> ScratchFilename;
288 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
289 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
290 if (IncludingHFI.IndexHeaderMapHeader) {
291 isAngled = true;
292 ScratchFilename += IncludingHFI.Framework;
293 ScratchFilename += '/';
294 ScratchFilename += Filename;
295 Filename = ScratchFilename;
296 }
297 }
298
299 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000300 // directory. This has to be based on CurFileEnt, not CurDir, because
301 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000302 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
303 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000304 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
305 llvm::SmallString<1024> TmpDir;
306 // Concatenate the requested file onto the directory.
307 // FIXME: Portability. Filename concatenation should be in sys::Path.
308 TmpDir += CurFileEnt->getDir()->getName();
309 TmpDir.push_back('/');
310 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000311 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000313 // This file is a system header or C++ unfriendly if the old file is.
314 //
315 // Note that the temporary 'DirInfo' is required here, as either call to
316 // getFileInfo could resize the vector and we don't want to rely on order
317 // of evaluation.
318 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000319 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000320 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000321 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000322 SearchPath->clear();
323 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
324 }
325 if (RelativePath != NULL) {
326 RelativePath->clear();
327 RelativePath->append(Filename.begin(), Filename.end());
328 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return FE;
330 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000331 }
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Reid Spencer5f016e22007-07-11 17:01:13 +0000333 CurDir = 0;
334
335 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000336 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000337
Reid Spencer5f016e22007-07-11 17:01:13 +0000338 // If this is a #include_next request, start searching after the directory the
339 // file was found in.
340 if (FromDir)
341 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Chris Lattner9960ae82007-07-22 07:28:00 +0000343 // Cache all of the lookups performed by this method. Many headers are
344 // multiply included, and the "pragma once" optimization prevents them from
345 // being relex/pp'd, but they would still have to search through a
346 // (potentially huge) series of SearchDirs to find it.
347 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000348 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000349
350 // If the entry has been previously looked up, the first value will be
351 // non-zero. If the value is equal to i (the start point of our search), then
352 // this is a matching hit.
353 if (CacheLookup.first == i+1) {
354 // Skip querying potentially lots of directories for this lookup.
355 i = CacheLookup.second;
356 } else {
357 // Otherwise, this is the first query, or the previous query didn't match
358 // our search start. We will fill in our found location below, so prime the
359 // start point value.
360 CacheLookup.first = i+1;
361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363 // Check each directory in sequence to see if it contains this file.
364 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000365 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000366 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000367 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnerafded5b2007-12-17 08:13:48 +0000369 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000370
Chris Lattnerafded5b2007-12-17 08:13:48 +0000371 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000372 HeaderFileInfo &HFI = getFileInfo(FE);
373 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000375 // If this file is found in a header map and uses the framework style of
376 // includes, then this header is part of a framework we're building.
377 if (CurDir->isIndexHeaderMap()) {
378 size_t SlashPos = Filename.find('/');
379 if (SlashPos != StringRef::npos) {
380 HFI.IndexHeaderMapHeader = 1;
381 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
382 SlashPos));
383 }
384 }
385
Chris Lattnerafded5b2007-12-17 08:13:48 +0000386 // Remember this location for the next lookup we do.
387 CacheLookup.second = i;
388 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner9960ae82007-07-22 07:28:00 +0000391 // Otherwise, didn't find it. Remember we didn't find this.
392 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 return 0;
394}
395
396/// LookupSubframeworkHeader - Look up a subframework for the specified
397/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
398/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
399/// is a subframework within Carbon.framework. If so, return the FileEntry
400/// for the designated file, otherwise return null.
401const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000402LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000403 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000404 SmallVectorImpl<char> *SearchPath,
405 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000406 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000407
Reid Spencer5f016e22007-07-11 17:01:13 +0000408 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000409 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000410 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 // Look up the base framework name of the ContextFileEnt.
413 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 // If the context info wasn't a framework, couldn't be a subframework.
416 const char *FrameworkPos = strstr(ContextName, ".framework/");
417 if (FrameworkPos == 0)
418 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000419
420 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 FrameworkPos+strlen(".framework/"));
422
423 // Append Frameworks/HIToolbox.framework/
424 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000425 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 FrameworkName += ".framework/";
427
428 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000429 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 // Some other location?
432 if (CacheLookup.getValue() &&
433 CacheLookup.getKeyLength() == FrameworkName.size() &&
434 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
435 CacheLookup.getKeyLength()) != 0)
436 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // Cache subframework.
439 if (CacheLookup.getValue() == 0) {
440 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000443 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 // Otherwise, if it does, remember that this is the right direntry for this
447 // framework.
448 CacheLookup.setValue(Dir);
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 const FileEntry *FE = 0;
452
Manuel Klimek74124942011-04-26 21:50:03 +0000453 if (RelativePath != NULL) {
454 RelativePath->clear();
455 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
456 }
457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
459 llvm::SmallString<1024> HeadersFilename(FrameworkName);
460 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000461 if (SearchPath != NULL) {
462 SearchPath->clear();
463 // Without trailing '/'.
464 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
465 }
466
Chris Lattnera1394812010-01-10 01:35:12 +0000467 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000468 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
471 HeadersFilename = FrameworkName;
472 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000473 if (SearchPath != NULL) {
474 SearchPath->clear();
475 // Without trailing '/'.
476 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
477 }
478
Chris Lattnera1394812010-01-10 01:35:12 +0000479 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000480 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000481 return 0;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000485 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000486 // Note that the temporary 'DirInfo' is required here, as either call to
487 // getFileInfo could resize the vector and we don't want to rely on order
488 // of evaluation.
489 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
490 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 return FE;
492}
493
494//===----------------------------------------------------------------------===//
495// File Info Management.
496//===----------------------------------------------------------------------===//
497
498
Steve Naroff83d63c72009-04-24 20:03:17 +0000499/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000500/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000501HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 if (FE->getUID() >= FileInfo.size())
503 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000504
505 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
506 if (ExternalSource && !HFI.Resolved) {
507 HFI = ExternalSource->GetHeaderFileInfo(FE);
508 HFI.Resolved = true;
509 }
510 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000511}
Reid Spencer5f016e22007-07-11 17:01:13 +0000512
Douglas Gregordd3e5542011-05-04 00:14:37 +0000513bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
514 // Check if we've ever seen this file as a header.
515 if (File->getUID() >= FileInfo.size())
516 return false;
517
518 // Resolve header file info from the external source, if needed.
519 HeaderFileInfo &HFI = FileInfo[File->getUID()];
520 if (ExternalSource && !HFI.Resolved) {
521 HFI = ExternalSource->GetHeaderFileInfo(File);
522 HFI.Resolved = true;
523 }
524
525 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
526}
527
Steve Naroff83d63c72009-04-24 20:03:17 +0000528void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
529 if (UID >= FileInfo.size())
530 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000531 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000532 FileInfo[UID] = HFI;
533}
534
Reid Spencer5f016e22007-07-11 17:01:13 +0000535/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
536/// #include, #include_next, or #import directive. Return false if #including
537/// the file will have no effect or true if we should include it.
538bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
539 ++NumIncluded; // Count # of attempted #includes.
540
541 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000542 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 // If this is a #import directive, check that we have not already imported
545 // this header.
546 if (isImport) {
547 // If this has already been imported, don't import it again.
548 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000549
Reid Spencer5f016e22007-07-11 17:01:13 +0000550 // Has this already been #import'ed or #include'd?
551 if (FileInfo.NumIncludes) return false;
552 } else {
553 // Otherwise, if this is a #include of a file that was previously #import'd
554 // or if this is the second #include of a #pragma once file, ignore it.
555 if (FileInfo.isImport)
556 return false;
557 }
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
560 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000561 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000562 = FileInfo.getControllingMacro(ExternalLookup))
563 if (ControllingMacro->hasMacroDefinition()) {
564 ++NumMultiIncludeFileOptzn;
565 return false;
566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Reid Spencer5f016e22007-07-11 17:01:13 +0000568 // Increment the number of times this file has been included.
569 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Reid Spencer5f016e22007-07-11 17:01:13 +0000571 return true;
572}
573
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000574size_t HeaderSearch::getTotalMemory() const {
575 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000576 + llvm::capacity_in_bytes(FileInfo)
577 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000578 + LookupFileCache.getAllocator().getTotalMemory()
579 + FrameworkMap.getAllocator().getTotalMemory();
580}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000581
582StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
583 return FrameworkNames.GetOrCreateValue(Framework).getKey();
584}