blob: e2fd02983c4d58e73149747e0512d4009c61ec24 [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"
Chris Lattner3daed522009-03-02 22:20:04 +000021#include <cstdio>
Reid Spencer5f016e22007-07-11 17:01:13 +000022using namespace clang;
23
Douglas Gregor8c5a7602009-04-25 23:30:02 +000024const IdentifierInfo *
25HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
26 if (ControllingMacro)
27 return ControllingMacro;
28
29 if (!ControllingMacroID || !External)
30 return 0;
31
32 ControllingMacro = External->GetIdentifier(ControllingMacroID);
33 return ControllingMacro;
34}
35
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000036ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
37
Chris Lattner39b49bc2010-11-23 08:35:12 +000038HeaderSearch::HeaderSearch(FileManager &FM)
39 : FileMgr(FM), FrameworkMap(64) {
Nico Weber74a5fd82011-05-24 04:31:14 +000040 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000041 SystemDirIdx = 0;
42 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000043
Douglas Gregor8c5a7602009-04-25 23:30:02 +000044 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000045 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046 NumIncluded = 0;
47 NumMultiIncludeFileOptzn = 0;
48 NumFrameworkLookups = NumSubFrameworkLookups = 0;
49}
50
Chris Lattner822da612007-12-17 06:36:45 +000051HeaderSearch::~HeaderSearch() {
52 // Delete headermaps.
53 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
54 delete HeaderMaps[i].second;
55}
Mike Stump1eb44332009-09-09 15:08:12 +000056
Reid Spencer5f016e22007-07-11 17:01:13 +000057void HeaderSearch::PrintStats() {
58 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
59 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
60 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
61 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
62 NumOnceOnlyFiles += FileInfo[i].isImport;
63 if (MaxNumIncludes < FileInfo[i].NumIncludes)
64 MaxNumIncludes = FileInfo[i].NumIncludes;
65 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
66 }
67 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
68 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
69 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000070
Reid Spencer5f016e22007-07-11 17:01:13 +000071 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
72 fprintf(stderr, " %d #includes skipped due to"
73 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Reid Spencer5f016e22007-07-11 17:01:13 +000075 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
76 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
77}
78
Chris Lattner822da612007-12-17 06:36:45 +000079/// CreateHeaderMap - This method returns a HeaderMap for the specified
80/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000081const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000082 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000083 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000084 if (!HeaderMaps.empty()) {
85 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000086 // Pointer equality comparison of FileEntries works because they are
87 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000088 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000089 return HeaderMaps[i].second;
90 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chris Lattner39b49bc2010-11-23 08:35:12 +000092 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000093 HeaderMaps.push_back(std::make_pair(FE, HM));
94 return HM;
95 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner822da612007-12-17 06:36:45 +000097 return 0;
98}
99
Chris Lattnerdf772332007-12-17 07:52:39 +0000100//===----------------------------------------------------------------------===//
101// File lookup within a DirectoryLookup scope
102//===----------------------------------------------------------------------===//
103
Chris Lattner3af66a92007-12-17 17:57:27 +0000104/// getName - Return the directory or filename corresponding to this lookup
105/// object.
106const char *DirectoryLookup::getName() const {
107 if (isNormalDir())
108 return getDir()->getName();
109 if (isFramework())
110 return getFrameworkDir()->getName();
111 assert(isHeaderMap() && "Unknown DirectoryLookup");
112 return getHeaderMap()->getFileName();
113}
114
115
Chris Lattnerdf772332007-12-17 07:52:39 +0000116/// LookupFile - Lookup the specified file in this search path, returning it
117/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000118const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000119 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000120 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000121 SmallVectorImpl<char> *SearchPath,
122 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000123 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000124 if (isNormalDir()) {
125 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000126 TmpDir = getDir()->getName();
127 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000128 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000130 SearchPath->clear();
131 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
132 }
133 if (RelativePath != NULL) {
134 RelativePath->clear();
135 RelativePath->append(Filename.begin(), Filename.end());
136 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000137 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000138 }
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Chris Lattnerafded5b2007-12-17 08:13:48 +0000140 if (isFramework())
Manuel Klimek74124942011-04-26 21:50:03 +0000141 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump1eb44332009-09-09 15:08:12 +0000142
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000143 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000144 const FileEntry * const Result = getHeaderMap()->LookupFile(
145 Filename, HS.getFileMgr());
146 if (Result) {
147 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000148 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000149 SearchPath->clear();
150 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
151 }
152 if (RelativePath != NULL) {
153 RelativePath->clear();
154 RelativePath->append(Filename.begin(), Filename.end());
155 }
156 }
157 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000158}
159
160
Chris Lattnerafded5b2007-12-17 08:13:48 +0000161/// DoFrameworkLookup - Do a lookup of the specified file in the current
162/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000163const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000164 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000165 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000166 SmallVectorImpl<char> *SearchPath,
167 SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000168 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000171 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000172 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattnerafded5b2007-12-17 08:13:48 +0000174 // Find out if this is the home for the specified framework, by checking
175 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000176 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000177 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnerafded5b2007-12-17 08:13:48 +0000179 // If it is known and in some other directory, fail.
180 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattnerafded5b2007-12-17 08:13:48 +0000183 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Reid Spencer5f016e22007-07-11 17:01:13 +0000185 // FrameworkName = "/System/Library/Frameworks/"
186 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000187 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 if (FrameworkName.empty() || FrameworkName.back() != '/')
189 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000190
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000192 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000193
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
195 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Chris Lattnerafded5b2007-12-17 08:13:48 +0000197 // If the cache entry is still unresolved, query to see if the cache entry is
198 // still unresolved. If so, check its existence now.
199 if (FrameworkDirCache == 0) {
200 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Reid Spencer5f016e22007-07-11 17:01:13 +0000202 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000203 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000204 bool Exists;
205 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000206 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000207
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 // Otherwise, if it does, remember that this is the right direntry for this
209 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000210 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000211 }
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Manuel Klimek74124942011-04-26 21:50:03 +0000213 if (RelativePath != NULL) {
214 RelativePath->clear();
215 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
216 }
217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
219 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000222
223 if (SearchPath != NULL) {
224 SearchPath->clear();
225 // Without trailing '/'.
226 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
227 }
228
Chris Lattnera1394812010-01-10 01:35:12 +0000229 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000230 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
231 /*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000233 }
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Reid Spencer5f016e22007-07-11 17:01:13 +0000235 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
236 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000237 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000239 if (SearchPath != NULL)
240 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
241 Private+strlen(Private));
242
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000243 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattnerdf772332007-12-17 07:52:39 +0000246
Chris Lattnerafded5b2007-12-17 08:13:48 +0000247//===----------------------------------------------------------------------===//
248// Header File Location.
249//===----------------------------------------------------------------------===//
250
251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
253/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000254/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
255/// non-null, indicates where the #including file is, in case a relative search
256/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000257const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000258 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000259 bool isAngled,
260 const DirectoryLookup *FromDir,
261 const DirectoryLookup *&CurDir,
262 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000263 SmallVectorImpl<char> *SearchPath,
264 SmallVectorImpl<char> *RelativePath) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000266 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 CurDir = 0;
268
269 // If this was an #include_next "/absolute/file", fail.
270 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Manuel Klimek74124942011-04-26 21:50:03 +0000272 if (SearchPath != NULL)
273 SearchPath->clear();
274 if (RelativePath != NULL) {
275 RelativePath->clear();
276 RelativePath->append(Filename.begin(), Filename.end());
277 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000278 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000279 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000283 // directory. This has to be based on CurFileEnt, not CurDir, because
284 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000285 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
286 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000287 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
288 llvm::SmallString<1024> TmpDir;
289 // Concatenate the requested file onto the directory.
290 // FIXME: Portability. Filename concatenation should be in sys::Path.
291 TmpDir += CurFileEnt->getDir()->getName();
292 TmpDir.push_back('/');
293 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000294 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000295 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000296 // This file is a system header or C++ unfriendly if the old file is.
297 //
298 // Note that the temporary 'DirInfo' is required here, as either call to
299 // getFileInfo could resize the vector and we don't want to rely on order
300 // of evaluation.
301 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000302 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000303 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000304 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000305 SearchPath->clear();
306 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
307 }
308 if (RelativePath != NULL) {
309 RelativePath->clear();
310 RelativePath->append(Filename.begin(), Filename.end());
311 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 return FE;
313 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 }
Mike Stump1eb44332009-09-09 15:08:12 +0000315
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 CurDir = 0;
317
318 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000319 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Reid Spencer5f016e22007-07-11 17:01:13 +0000321 // If this is a #include_next request, start searching after the directory the
322 // file was found in.
323 if (FromDir)
324 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner9960ae82007-07-22 07:28:00 +0000326 // Cache all of the lookups performed by this method. Many headers are
327 // multiply included, and the "pragma once" optimization prevents them from
328 // being relex/pp'd, but they would still have to search through a
329 // (potentially huge) series of SearchDirs to find it.
330 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000331 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000332
333 // If the entry has been previously looked up, the first value will be
334 // non-zero. If the value is equal to i (the start point of our search), then
335 // this is a matching hit.
336 if (CacheLookup.first == i+1) {
337 // Skip querying potentially lots of directories for this lookup.
338 i = CacheLookup.second;
339 } else {
340 // Otherwise, this is the first query, or the previous query didn't match
341 // our search start. We will fill in our found location below, so prime the
342 // start point value.
343 CacheLookup.first = i+1;
344 }
Mike Stump1eb44332009-09-09 15:08:12 +0000345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346 // Check each directory in sequence to see if it contains this file.
347 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000348 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000349 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000350 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnerafded5b2007-12-17 08:13:48 +0000352 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattnerafded5b2007-12-17 08:13:48 +0000354 // This file is a system header or C++ unfriendly if the dir is.
355 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattnerafded5b2007-12-17 08:13:48 +0000357 // Remember this location for the next lookup we do.
358 CacheLookup.second = i;
359 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 }
Mike Stump1eb44332009-09-09 15:08:12 +0000361
Chris Lattner9960ae82007-07-22 07:28:00 +0000362 // Otherwise, didn't find it. Remember we didn't find this.
363 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000364 return 0;
365}
366
367/// LookupSubframeworkHeader - Look up a subframework for the specified
368/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
369/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
370/// is a subframework within Carbon.framework. If so, return the FileEntry
371/// for the designated file, otherwise return null.
372const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000373LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000374 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000375 SmallVectorImpl<char> *SearchPath,
376 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000377 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000378
Reid Spencer5f016e22007-07-11 17:01:13 +0000379 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000380 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000381 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 // Look up the base framework name of the ContextFileEnt.
384 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Reid Spencer5f016e22007-07-11 17:01:13 +0000386 // If the context info wasn't a framework, couldn't be a subframework.
387 const char *FrameworkPos = strstr(ContextName, ".framework/");
388 if (FrameworkPos == 0)
389 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
391 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 FrameworkPos+strlen(".framework/"));
393
394 // Append Frameworks/HIToolbox.framework/
395 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000396 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000397 FrameworkName += ".framework/";
398
399 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000400 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000401
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 // Some other location?
403 if (CacheLookup.getValue() &&
404 CacheLookup.getKeyLength() == FrameworkName.size() &&
405 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
406 CacheLookup.getKeyLength()) != 0)
407 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409 // Cache subframework.
410 if (CacheLookup.getValue() == 0) {
411 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000414 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 // Otherwise, if it does, remember that this is the right direntry for this
418 // framework.
419 CacheLookup.setValue(Dir);
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 const FileEntry *FE = 0;
423
Manuel Klimek74124942011-04-26 21:50:03 +0000424 if (RelativePath != NULL) {
425 RelativePath->clear();
426 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
427 }
428
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
430 llvm::SmallString<1024> HeadersFilename(FrameworkName);
431 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000432 if (SearchPath != NULL) {
433 SearchPath->clear();
434 // Without trailing '/'.
435 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
436 }
437
Chris Lattnera1394812010-01-10 01:35:12 +0000438 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000439 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
442 HeadersFilename = FrameworkName;
443 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000444 if (SearchPath != NULL) {
445 SearchPath->clear();
446 // Without trailing '/'.
447 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
448 }
449
Chris Lattnera1394812010-01-10 01:35:12 +0000450 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000451 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 return 0;
453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000456 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000457 // Note that the temporary 'DirInfo' is required here, as either call to
458 // getFileInfo could resize the vector and we don't want to rely on order
459 // of evaluation.
460 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
461 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 return FE;
463}
464
465//===----------------------------------------------------------------------===//
466// File Info Management.
467//===----------------------------------------------------------------------===//
468
469
Steve Naroff83d63c72009-04-24 20:03:17 +0000470/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000471/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000472HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 if (FE->getUID() >= FileInfo.size())
474 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000475
476 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
477 if (ExternalSource && !HFI.Resolved) {
478 HFI = ExternalSource->GetHeaderFileInfo(FE);
479 HFI.Resolved = true;
480 }
481 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000482}
Reid Spencer5f016e22007-07-11 17:01:13 +0000483
Douglas Gregordd3e5542011-05-04 00:14:37 +0000484bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
485 // Check if we've ever seen this file as a header.
486 if (File->getUID() >= FileInfo.size())
487 return false;
488
489 // Resolve header file info from the external source, if needed.
490 HeaderFileInfo &HFI = FileInfo[File->getUID()];
491 if (ExternalSource && !HFI.Resolved) {
492 HFI = ExternalSource->GetHeaderFileInfo(File);
493 HFI.Resolved = true;
494 }
495
496 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
497}
498
Steve Naroff83d63c72009-04-24 20:03:17 +0000499void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
500 if (UID >= FileInfo.size())
501 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000502 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000503 FileInfo[UID] = HFI;
504}
505
Reid Spencer5f016e22007-07-11 17:01:13 +0000506/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
507/// #include, #include_next, or #import directive. Return false if #including
508/// the file will have no effect or true if we should include it.
509bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
510 ++NumIncluded; // Count # of attempted #includes.
511
512 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000513 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000514
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 // If this is a #import directive, check that we have not already imported
516 // this header.
517 if (isImport) {
518 // If this has already been imported, don't import it again.
519 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Reid Spencer5f016e22007-07-11 17:01:13 +0000521 // Has this already been #import'ed or #include'd?
522 if (FileInfo.NumIncludes) return false;
523 } else {
524 // Otherwise, if this is a #include of a file that was previously #import'd
525 // or if this is the second #include of a #pragma once file, ignore it.
526 if (FileInfo.isImport)
527 return false;
528 }
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
531 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000532 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000533 = FileInfo.getControllingMacro(ExternalLookup))
534 if (ControllingMacro->hasMacroDefinition()) {
535 ++NumMultiIncludeFileOptzn;
536 return false;
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Reid Spencer5f016e22007-07-11 17:01:13 +0000539 // Increment the number of times this file has been included.
540 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Reid Spencer5f016e22007-07-11 17:01:13 +0000542 return true;
543}
544
545