blob: bb4388195a325ba667a0a54d443f0e0fc7745adb [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(
119 llvm::StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000120 HeaderSearch &HS,
121 llvm::SmallVectorImpl<char> *SearchPath,
122 llvm::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.
126 // FIXME: Portability. Filename concatenation should be in sys::Path.
127 TmpDir += getDir()->getName();
128 TmpDir.push_back('/');
Chris Lattnera1394812010-01-10 01:35:12 +0000129 TmpDir.append(Filename.begin(), Filename.end());
Manuel Klimek74124942011-04-26 21:50:03 +0000130 if (SearchPath != NULL) {
131 llvm::StringRef SearchPathRef(getDir()->getName());
132 SearchPath->clear();
133 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
134 }
135 if (RelativePath != NULL) {
136 RelativePath->clear();
137 RelativePath->append(Filename.begin(), Filename.end());
138 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000139 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000140 }
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Chris Lattnerafded5b2007-12-17 08:13:48 +0000142 if (isFramework())
Manuel Klimek74124942011-04-26 21:50:03 +0000143 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
Mike Stump1eb44332009-09-09 15:08:12 +0000144
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000145 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000146 const FileEntry * const Result = getHeaderMap()->LookupFile(
147 Filename, HS.getFileMgr());
148 if (Result) {
149 if (SearchPath != NULL) {
Douglas Gregor520a99f2011-04-29 00:45:09 +0000150 llvm::StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000151 SearchPath->clear();
152 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
153 }
154 if (RelativePath != NULL) {
155 RelativePath->clear();
156 RelativePath->append(Filename.begin(), Filename.end());
157 }
158 }
159 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000160}
161
162
Chris Lattnerafded5b2007-12-17 08:13:48 +0000163/// DoFrameworkLookup - Do a lookup of the specified file in the current
164/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000165const FileEntry *DirectoryLookup::DoFrameworkLookup(
166 llvm::StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000167 HeaderSearch &HS,
168 llvm::SmallVectorImpl<char> *SearchPath,
169 llvm::SmallVectorImpl<char> *RelativePath) const {
Chris Lattnerafded5b2007-12-17 08:13:48 +0000170 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Reid Spencer5f016e22007-07-11 17:01:13 +0000172 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000173 size_t SlashPos = Filename.find('/');
174 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000175
Chris Lattnerafded5b2007-12-17 08:13:48 +0000176 // Find out if this is the home for the specified framework, by checking
177 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000178 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000179 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattnerafded5b2007-12-17 08:13:48 +0000181 // If it is known and in some other directory, fail.
182 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnerafded5b2007-12-17 08:13:48 +0000185 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 // FrameworkName = "/System/Library/Frameworks/"
188 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000189 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 if (FrameworkName.empty() || FrameworkName.back() != '/')
191 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000194 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Reid Spencer5f016e22007-07-11 17:01:13 +0000196 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
197 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Chris Lattnerafded5b2007-12-17 08:13:48 +0000199 // If the cache entry is still unresolved, query to see if the cache entry is
200 // still unresolved. If so, check its existence now.
201 if (FrameworkDirCache == 0) {
202 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000205 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000206 bool Exists;
207 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000208 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 // Otherwise, if it does, remember that this is the right direntry for this
211 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000212 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000213 }
Mike Stump1eb44332009-09-09 15:08:12 +0000214
Manuel Klimek74124942011-04-26 21:50:03 +0000215 if (RelativePath != NULL) {
216 RelativePath->clear();
217 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
218 }
219
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
221 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Reid Spencer5f016e22007-07-11 17:01:13 +0000223 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000224
225 if (SearchPath != NULL) {
226 SearchPath->clear();
227 // Without trailing '/'.
228 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
229 }
230
Chris Lattnera1394812010-01-10 01:35:12 +0000231 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000232 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
233 /*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000234 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
238 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000239 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000240 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000241 if (SearchPath != NULL)
242 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
243 Private+strlen(Private));
244
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000245 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000246}
247
Chris Lattnerdf772332007-12-17 07:52:39 +0000248
Chris Lattnerafded5b2007-12-17 08:13:48 +0000249//===----------------------------------------------------------------------===//
250// Header File Location.
251//===----------------------------------------------------------------------===//
252
253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
255/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000256/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
257/// non-null, indicates where the #including file is, in case a relative search
258/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000259const FileEntry *HeaderSearch::LookupFile(
260 llvm::StringRef Filename,
261 bool isAngled,
262 const DirectoryLookup *FromDir,
263 const DirectoryLookup *&CurDir,
264 const FileEntry *CurFileEnt,
Manuel Klimek74124942011-04-26 21:50:03 +0000265 llvm::SmallVectorImpl<char> *SearchPath,
266 llvm::SmallVectorImpl<char> *RelativePath) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000268 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 CurDir = 0;
270
271 // If this was an #include_next "/absolute/file", fail.
272 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Manuel Klimek74124942011-04-26 21:50:03 +0000274 if (SearchPath != NULL)
275 SearchPath->clear();
276 if (RelativePath != NULL) {
277 RelativePath->clear();
278 RelativePath->append(Filename.begin(), Filename.end());
279 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000281 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000282 }
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 // Step #0, unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000285 // directory. This has to be based on CurFileEnt, not CurDir, because
286 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000287 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
288 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000289 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
290 llvm::SmallString<1024> TmpDir;
291 // Concatenate the requested file onto the directory.
292 // FIXME: Portability. Filename concatenation should be in sys::Path.
293 TmpDir += CurFileEnt->getDir()->getName();
294 TmpDir.push_back('/');
295 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000296 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000298 // This file is a system header or C++ unfriendly if the old file is.
299 //
300 // Note that the temporary 'DirInfo' is required here, as either call to
301 // getFileInfo could resize the vector and we don't want to rely on order
302 // of evaluation.
303 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000304 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000305 if (SearchPath != NULL) {
306 llvm::StringRef SearchPathRef(CurFileEnt->getDir()->getName());
307 SearchPath->clear();
308 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
309 }
310 if (RelativePath != NULL) {
311 RelativePath->clear();
312 RelativePath->append(Filename.begin(), Filename.end());
313 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 return FE;
315 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000316 }
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318 CurDir = 0;
319
320 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000321 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Reid Spencer5f016e22007-07-11 17:01:13 +0000323 // If this is a #include_next request, start searching after the directory the
324 // file was found in.
325 if (FromDir)
326 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000327
Chris Lattner9960ae82007-07-22 07:28:00 +0000328 // Cache all of the lookups performed by this method. Many headers are
329 // multiply included, and the "pragma once" optimization prevents them from
330 // being relex/pp'd, but they would still have to search through a
331 // (potentially huge) series of SearchDirs to find it.
332 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000333 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000334
335 // If the entry has been previously looked up, the first value will be
336 // non-zero. If the value is equal to i (the start point of our search), then
337 // this is a matching hit.
338 if (CacheLookup.first == i+1) {
339 // Skip querying potentially lots of directories for this lookup.
340 i = CacheLookup.second;
341 } else {
342 // Otherwise, this is the first query, or the previous query didn't match
343 // our search start. We will fill in our found location below, so prime the
344 // start point value.
345 CacheLookup.first = i+1;
346 }
Mike Stump1eb44332009-09-09 15:08:12 +0000347
Reid Spencer5f016e22007-07-11 17:01:13 +0000348 // Check each directory in sequence to see if it contains this file.
349 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000350 const FileEntry *FE =
Manuel Klimek74124942011-04-26 21:50:03 +0000351 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000352 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000353
Chris Lattnerafded5b2007-12-17 08:13:48 +0000354 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chris Lattnerafded5b2007-12-17 08:13:48 +0000356 // This file is a system header or C++ unfriendly if the dir is.
357 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattnerafded5b2007-12-17 08:13:48 +0000359 // Remember this location for the next lookup we do.
360 CacheLookup.second = i;
361 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000362 }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattner9960ae82007-07-22 07:28:00 +0000364 // Otherwise, didn't find it. Remember we didn't find this.
365 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000366 return 0;
367}
368
369/// LookupSubframeworkHeader - Look up a subframework for the specified
370/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
371/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
372/// is a subframework within Carbon.framework. If so, return the FileEntry
373/// for the designated file, otherwise return null.
374const FileEntry *HeaderSearch::
Chris Lattnera1394812010-01-10 01:35:12 +0000375LookupSubframeworkHeader(llvm::StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000376 const FileEntry *ContextFileEnt,
Manuel Klimek74124942011-04-26 21:50:03 +0000377 llvm::SmallVectorImpl<char> *SearchPath,
378 llvm::SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000379 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000382 size_t SlashPos = Filename.find('/');
383 if (SlashPos == llvm::StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 // Look up the base framework name of the ContextFileEnt.
386 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 // If the context info wasn't a framework, couldn't be a subframework.
389 const char *FrameworkPos = strstr(ContextName, ".framework/");
390 if (FrameworkPos == 0)
391 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000392
393 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 FrameworkPos+strlen(".framework/"));
395
396 // Append Frameworks/HIToolbox.framework/
397 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000398 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 FrameworkName += ".framework/";
400
401 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000402 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 // Some other location?
405 if (CacheLookup.getValue() &&
406 CacheLookup.getKeyLength() == FrameworkName.size() &&
407 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
408 CacheLookup.getKeyLength()) != 0)
409 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // Cache subframework.
412 if (CacheLookup.getValue() == 0) {
413 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000416 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 // Otherwise, if it does, remember that this is the right direntry for this
420 // framework.
421 CacheLookup.setValue(Dir);
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 const FileEntry *FE = 0;
425
Manuel Klimek74124942011-04-26 21:50:03 +0000426 if (RelativePath != NULL) {
427 RelativePath->clear();
428 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
429 }
430
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
432 llvm::SmallString<1024> HeadersFilename(FrameworkName);
433 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000434 if (SearchPath != NULL) {
435 SearchPath->clear();
436 // Without trailing '/'.
437 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
438 }
439
Chris Lattnera1394812010-01-10 01:35:12 +0000440 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000441 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
444 HeadersFilename = FrameworkName;
445 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000446 if (SearchPath != NULL) {
447 SearchPath->clear();
448 // Without trailing '/'.
449 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
450 }
451
Chris Lattnera1394812010-01-10 01:35:12 +0000452 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000453 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 return 0;
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000458 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000459 // Note that the temporary 'DirInfo' is required here, as either call to
460 // getFileInfo could resize the vector and we don't want to rely on order
461 // of evaluation.
462 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
463 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 return FE;
465}
466
467//===----------------------------------------------------------------------===//
468// File Info Management.
469//===----------------------------------------------------------------------===//
470
471
Steve Naroff83d63c72009-04-24 20:03:17 +0000472/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000473/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000474HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 if (FE->getUID() >= FileInfo.size())
476 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000477
478 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
479 if (ExternalSource && !HFI.Resolved) {
480 HFI = ExternalSource->GetHeaderFileInfo(FE);
481 HFI.Resolved = true;
482 }
483 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000484}
Reid Spencer5f016e22007-07-11 17:01:13 +0000485
Douglas Gregordd3e5542011-05-04 00:14:37 +0000486bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
487 // Check if we've ever seen this file as a header.
488 if (File->getUID() >= FileInfo.size())
489 return false;
490
491 // Resolve header file info from the external source, if needed.
492 HeaderFileInfo &HFI = FileInfo[File->getUID()];
493 if (ExternalSource && !HFI.Resolved) {
494 HFI = ExternalSource->GetHeaderFileInfo(File);
495 HFI.Resolved = true;
496 }
497
498 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
499}
500
Steve Naroff83d63c72009-04-24 20:03:17 +0000501void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
502 if (UID >= FileInfo.size())
503 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000504 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000505 FileInfo[UID] = HFI;
506}
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
509/// #include, #include_next, or #import directive. Return false if #including
510/// the file will have no effect or true if we should include it.
511bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
512 ++NumIncluded; // Count # of attempted #includes.
513
514 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000515 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 // If this is a #import directive, check that we have not already imported
518 // this header.
519 if (isImport) {
520 // If this has already been imported, don't import it again.
521 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 // Has this already been #import'ed or #include'd?
524 if (FileInfo.NumIncludes) return false;
525 } else {
526 // Otherwise, if this is a #include of a file that was previously #import'd
527 // or if this is the second #include of a #pragma once file, ignore it.
528 if (FileInfo.isImport)
529 return false;
530 }
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
533 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000534 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000535 = FileInfo.getControllingMacro(ExternalLookup))
536 if (ControllingMacro->hasMacroDefinition()) {
537 ++NumMultiIncludeFileOptzn;
538 return false;
539 }
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Reid Spencer5f016e22007-07-11 17:01:13 +0000541 // Increment the number of times this file has been included.
542 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 return true;
545}
546
547