blob: 0ba76327421e52c2c7d16771e7871007a3c4ea80 [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 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000284 // directory. This has to be based on CurFileEnt, not CurDir, because
285 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000286 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
287 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000288 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
289 llvm::SmallString<1024> TmpDir;
290 // Concatenate the requested file onto the directory.
291 // FIXME: Portability. Filename concatenation should be in sys::Path.
292 TmpDir += CurFileEnt->getDir()->getName();
293 TmpDir.push_back('/');
294 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000295 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000297 // This file is a system header or C++ unfriendly if the old file is.
298 //
299 // Note that the temporary 'DirInfo' is required here, as either call to
300 // getFileInfo could resize the vector and we don't want to rely on order
301 // of evaluation.
302 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000303 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000304 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000305 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000306 SearchPath->clear();
307 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
308 }
309 if (RelativePath != NULL) {
310 RelativePath->clear();
311 RelativePath->append(Filename.begin(), Filename.end());
312 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000313 return FE;
314 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316
Reid Spencer5f016e22007-07-11 17:01:13 +0000317 CurDir = 0;
318
319 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000320 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Reid Spencer5f016e22007-07-11 17:01:13 +0000322 // If this is a #include_next request, start searching after the directory the
323 // file was found in.
324 if (FromDir)
325 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattner9960ae82007-07-22 07:28:00 +0000327 // Cache all of the lookups performed by this method. Many headers are
328 // multiply included, and the "pragma once" optimization prevents them from
329 // being relex/pp'd, but they would still have to search through a
330 // (potentially huge) series of SearchDirs to find it.
331 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000332 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000333
334 // If the entry has been previously looked up, the first value will be
335 // non-zero. If the value is equal to i (the start point of our search), then
336 // this is a matching hit.
337 if (CacheLookup.first == i+1) {
338 // Skip querying potentially lots of directories for this lookup.
339 i = CacheLookup.second;
340 } else {
341 // Otherwise, this is the first query, or the previous query didn't match
342 // our search start. We will fill in our found location below, so prime the
343 // start point value.
344 CacheLookup.first = i+1;
345 }
Mike Stump1eb44332009-09-09 15:08:12 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347 // Check each directory in sequence to see if it contains this file.
348 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000349 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000350 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000351 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Chris Lattnerafded5b2007-12-17 08:13:48 +0000353 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattnerafded5b2007-12-17 08:13:48 +0000355 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000356 HeaderFileInfo &HFI = getFileInfo(FE);
357 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000359 // If this file is found in a header map and uses the framework style of
360 // includes, then this header is part of a framework we're building.
361 if (CurDir->isIndexHeaderMap()) {
362 size_t SlashPos = Filename.find('/');
363 if (SlashPos != StringRef::npos) {
364 HFI.IndexHeaderMapHeader = 1;
365 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
366 SlashPos));
367 }
368 }
369
Chris Lattnerafded5b2007-12-17 08:13:48 +0000370 // Remember this location for the next lookup we do.
371 CacheLookup.second = i;
372 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 }
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000375 // If we are including a file with a quoted include "foo.h" from inside
376 // a header in a framework that is currently being built, and we couldn't
377 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
378 // "Foo" is the name of the framework in which the including header was found.
379 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
380 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
381 if (IncludingHFI.IndexHeaderMapHeader) {
382 llvm::SmallString<128> ScratchFilename;
383 ScratchFilename += IncludingHFI.Framework;
384 ScratchFilename += '/';
385 ScratchFilename += Filename;
386
387 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
388 FromDir, CurDir, CurFileEnt,
389 SearchPath, RelativePath);
390 std::pair<unsigned, unsigned> &CacheLookup
391 = LookupFileCache.GetOrCreateValue(Filename).getValue();
392 CacheLookup.second
393 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
394 return Result;
395 }
396 }
397
Chris Lattner9960ae82007-07-22 07:28:00 +0000398 // Otherwise, didn't find it. Remember we didn't find this.
399 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 return 0;
401}
402
403/// LookupSubframeworkHeader - Look up a subframework for the specified
404/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
405/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
406/// is a subframework within Carbon.framework. If so, return the FileEntry
407/// for the designated file, otherwise return null.
408const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000409LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000410 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000411 SmallVectorImpl<char> *SearchPath,
412 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000413 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000416 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000417 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 // Look up the base framework name of the ContextFileEnt.
420 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 // If the context info wasn't a framework, couldn't be a subframework.
423 const char *FrameworkPos = strstr(ContextName, ".framework/");
424 if (FrameworkPos == 0)
425 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000426
427 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 FrameworkPos+strlen(".framework/"));
429
430 // Append Frameworks/HIToolbox.framework/
431 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000432 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 FrameworkName += ".framework/";
434
435 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000436 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 // Some other location?
439 if (CacheLookup.getValue() &&
440 CacheLookup.getKeyLength() == FrameworkName.size() &&
441 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
442 CacheLookup.getKeyLength()) != 0)
443 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Reid Spencer5f016e22007-07-11 17:01:13 +0000445 // Cache subframework.
446 if (CacheLookup.getValue() == 0) {
447 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000450 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 // Otherwise, if it does, remember that this is the right direntry for this
454 // framework.
455 CacheLookup.setValue(Dir);
456 }
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 const FileEntry *FE = 0;
459
Manuel Klimek74124942011-04-26 21:50:03 +0000460 if (RelativePath != NULL) {
461 RelativePath->clear();
462 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
463 }
464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
466 llvm::SmallString<1024> HeadersFilename(FrameworkName);
467 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000468 if (SearchPath != NULL) {
469 SearchPath->clear();
470 // Without trailing '/'.
471 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
472 }
473
Chris Lattnera1394812010-01-10 01:35:12 +0000474 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000475 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Reid Spencer5f016e22007-07-11 17:01:13 +0000477 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
478 HeadersFilename = FrameworkName;
479 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000480 if (SearchPath != NULL) {
481 SearchPath->clear();
482 // Without trailing '/'.
483 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
484 }
485
Chris Lattnera1394812010-01-10 01:35:12 +0000486 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000487 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000488 return 0;
489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Reid Spencer5f016e22007-07-11 17:01:13 +0000491 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000492 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000493 // Note that the temporary 'DirInfo' is required here, as either call to
494 // getFileInfo could resize the vector and we don't want to rely on order
495 // of evaluation.
496 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
497 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 return FE;
499}
500
501//===----------------------------------------------------------------------===//
502// File Info Management.
503//===----------------------------------------------------------------------===//
504
505
Steve Naroff83d63c72009-04-24 20:03:17 +0000506/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000507/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000508HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000509 if (FE->getUID() >= FileInfo.size())
510 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000511
512 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
513 if (ExternalSource && !HFI.Resolved) {
514 HFI = ExternalSource->GetHeaderFileInfo(FE);
515 HFI.Resolved = true;
516 }
517 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000518}
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
Douglas Gregordd3e5542011-05-04 00:14:37 +0000520bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
521 // Check if we've ever seen this file as a header.
522 if (File->getUID() >= FileInfo.size())
523 return false;
524
525 // Resolve header file info from the external source, if needed.
526 HeaderFileInfo &HFI = FileInfo[File->getUID()];
527 if (ExternalSource && !HFI.Resolved) {
528 HFI = ExternalSource->GetHeaderFileInfo(File);
529 HFI.Resolved = true;
530 }
531
532 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
533}
534
Steve Naroff83d63c72009-04-24 20:03:17 +0000535void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
536 if (UID >= FileInfo.size())
537 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000538 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000539 FileInfo[UID] = HFI;
540}
541
Reid Spencer5f016e22007-07-11 17:01:13 +0000542/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
543/// #include, #include_next, or #import directive. Return false if #including
544/// the file will have no effect or true if we should include it.
545bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
546 ++NumIncluded; // Count # of attempted #includes.
547
548 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000549 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Reid Spencer5f016e22007-07-11 17:01:13 +0000551 // If this is a #import directive, check that we have not already imported
552 // this header.
553 if (isImport) {
554 // If this has already been imported, don't import it again.
555 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 // Has this already been #import'ed or #include'd?
558 if (FileInfo.NumIncludes) return false;
559 } else {
560 // Otherwise, if this is a #include of a file that was previously #import'd
561 // or if this is the second #include of a #pragma once file, ignore it.
562 if (FileInfo.isImport)
563 return false;
564 }
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
567 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000568 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000569 = FileInfo.getControllingMacro(ExternalLookup))
570 if (ControllingMacro->hasMacroDefinition()) {
571 ++NumMultiIncludeFileOptzn;
572 return false;
573 }
Mike Stump1eb44332009-09-09 15:08:12 +0000574
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 // Increment the number of times this file has been included.
576 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 return true;
579}
580
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000581size_t HeaderSearch::getTotalMemory() const {
582 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000583 + llvm::capacity_in_bytes(FileInfo)
584 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000585 + LookupFileCache.getAllocator().getTotalMemory()
586 + FrameworkMap.getAllocator().getTotalMemory();
587}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000588
589StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
590 return FrameworkNames.GetOrCreateValue(Framework).getKey();
591}