blob: 11cb1ecd8cd83e052c5c6220d49295df3b1bad94 [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
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 // Step #0, 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.
356 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattnerafded5b2007-12-17 08:13:48 +0000358 // Remember this location for the next lookup we do.
359 CacheLookup.second = i;
360 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361 }
Mike Stump1eb44332009-09-09 15:08:12 +0000362
Chris Lattner9960ae82007-07-22 07:28:00 +0000363 // Otherwise, didn't find it. Remember we didn't find this.
364 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000365 return 0;
366}
367
368/// LookupSubframeworkHeader - Look up a subframework for the specified
369/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
370/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
371/// is a subframework within Carbon.framework. If so, return the FileEntry
372/// for the designated file, otherwise return null.
373const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000374LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000375 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000376 SmallVectorImpl<char> *SearchPath,
377 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000378 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000379
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000381 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000382 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Reid Spencer5f016e22007-07-11 17:01:13 +0000384 // Look up the base framework name of the ContextFileEnt.
385 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000386
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 // If the context info wasn't a framework, couldn't be a subframework.
388 const char *FrameworkPos = strstr(ContextName, ".framework/");
389 if (FrameworkPos == 0)
390 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000391
392 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 FrameworkPos+strlen(".framework/"));
394
395 // Append Frameworks/HIToolbox.framework/
396 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000397 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 FrameworkName += ".framework/";
399
400 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000401 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 // Some other location?
404 if (CacheLookup.getValue() &&
405 CacheLookup.getKeyLength() == FrameworkName.size() &&
406 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
407 CacheLookup.getKeyLength()) != 0)
408 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 // Cache subframework.
411 if (CacheLookup.getValue() == 0) {
412 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000413
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000415 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 // Otherwise, if it does, remember that this is the right direntry for this
419 // framework.
420 CacheLookup.setValue(Dir);
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 const FileEntry *FE = 0;
424
Manuel Klimek74124942011-04-26 21:50:03 +0000425 if (RelativePath != NULL) {
426 RelativePath->clear();
427 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
428 }
429
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
431 llvm::SmallString<1024> HeadersFilename(FrameworkName);
432 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000433 if (SearchPath != NULL) {
434 SearchPath->clear();
435 // Without trailing '/'.
436 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
437 }
438
Chris Lattnera1394812010-01-10 01:35:12 +0000439 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000440 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
443 HeadersFilename = FrameworkName;
444 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000445 if (SearchPath != NULL) {
446 SearchPath->clear();
447 // Without trailing '/'.
448 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
449 }
450
Chris Lattnera1394812010-01-10 01:35:12 +0000451 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000452 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 return 0;
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Reid Spencer5f016e22007-07-11 17:01:13 +0000456 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000457 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000458 // Note that the temporary 'DirInfo' is required here, as either call to
459 // getFileInfo could resize the vector and we don't want to rely on order
460 // of evaluation.
461 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
462 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000463 return FE;
464}
465
466//===----------------------------------------------------------------------===//
467// File Info Management.
468//===----------------------------------------------------------------------===//
469
470
Steve Naroff83d63c72009-04-24 20:03:17 +0000471/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000472/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000473HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 if (FE->getUID() >= FileInfo.size())
475 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000476
477 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
478 if (ExternalSource && !HFI.Resolved) {
479 HFI = ExternalSource->GetHeaderFileInfo(FE);
480 HFI.Resolved = true;
481 }
482 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000483}
Reid Spencer5f016e22007-07-11 17:01:13 +0000484
Douglas Gregordd3e5542011-05-04 00:14:37 +0000485bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
486 // Check if we've ever seen this file as a header.
487 if (File->getUID() >= FileInfo.size())
488 return false;
489
490 // Resolve header file info from the external source, if needed.
491 HeaderFileInfo &HFI = FileInfo[File->getUID()];
492 if (ExternalSource && !HFI.Resolved) {
493 HFI = ExternalSource->GetHeaderFileInfo(File);
494 HFI.Resolved = true;
495 }
496
497 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
498}
499
Steve Naroff83d63c72009-04-24 20:03:17 +0000500void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
501 if (UID >= FileInfo.size())
502 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000503 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000504 FileInfo[UID] = HFI;
505}
506
Reid Spencer5f016e22007-07-11 17:01:13 +0000507/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
508/// #include, #include_next, or #import directive. Return false if #including
509/// the file will have no effect or true if we should include it.
510bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
511 ++NumIncluded; // Count # of attempted #includes.
512
513 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000514 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Reid Spencer5f016e22007-07-11 17:01:13 +0000516 // If this is a #import directive, check that we have not already imported
517 // this header.
518 if (isImport) {
519 // If this has already been imported, don't import it again.
520 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 // Has this already been #import'ed or #include'd?
523 if (FileInfo.NumIncludes) return false;
524 } else {
525 // Otherwise, if this is a #include of a file that was previously #import'd
526 // or if this is the second #include of a #pragma once file, ignore it.
527 if (FileInfo.isImport)
528 return false;
529 }
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
532 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000533 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000534 = FileInfo.getControllingMacro(ExternalLookup))
535 if (ControllingMacro->hasMacroDefinition()) {
536 ++NumMultiIncludeFileOptzn;
537 return false;
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 // Increment the number of times this file has been included.
541 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Reid Spencer5f016e22007-07-11 17:01:13 +0000543 return true;
544}
545
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000546size_t HeaderSearch::getTotalMemory() const {
547 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000548 + llvm::capacity_in_bytes(FileInfo)
549 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000550 + LookupFileCache.getAllocator().getTotalMemory()
551 + FrameworkMap.getAllocator().getTotalMemory();
552}