blob: 837b913d475103f34a07bc82c3a50411e8b78c83 [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
Douglas Gregor8e238062011-11-11 00:35:06 +000039HeaderSearch::HeaderSearch(FileManager &FM, DiagnosticsEngine &Diags)
40 : FileMgr(FM), Diags(Diags), FrameworkMap(64)
41{
Nico Weber74a5fd82011-05-24 04:31:14 +000042 AngledDirIdx = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000043 SystemDirIdx = 0;
44 NoCurDirSearch = false;
Mike Stump1eb44332009-09-09 15:08:12 +000045
Douglas Gregor8c5a7602009-04-25 23:30:02 +000046 ExternalLookup = 0;
Douglas Gregorcfbf1c72011-02-10 17:09:37 +000047 ExternalSource = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048 NumIncluded = 0;
49 NumMultiIncludeFileOptzn = 0;
50 NumFrameworkLookups = NumSubFrameworkLookups = 0;
51}
52
Chris Lattner822da612007-12-17 06:36:45 +000053HeaderSearch::~HeaderSearch() {
54 // Delete headermaps.
55 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
56 delete HeaderMaps[i].second;
57}
Mike Stump1eb44332009-09-09 15:08:12 +000058
Reid Spencer5f016e22007-07-11 17:01:13 +000059void HeaderSearch::PrintStats() {
60 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
61 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
62 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
63 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
64 NumOnceOnlyFiles += FileInfo[i].isImport;
65 if (MaxNumIncludes < FileInfo[i].NumIncludes)
66 MaxNumIncludes = FileInfo[i].NumIncludes;
67 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
68 }
69 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
70 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
71 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Reid Spencer5f016e22007-07-11 17:01:13 +000073 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
74 fprintf(stderr, " %d #includes skipped due to"
75 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
Mike Stump1eb44332009-09-09 15:08:12 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
78 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
79}
80
Chris Lattner822da612007-12-17 06:36:45 +000081/// CreateHeaderMap - This method returns a HeaderMap for the specified
82/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000083const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
Chris Lattner822da612007-12-17 06:36:45 +000084 // We expect the number of headermaps to be small, and almost always empty.
Chris Lattnerdf772332007-12-17 07:52:39 +000085 // If it ever grows, use of a linear search should be re-evaluated.
Chris Lattner822da612007-12-17 06:36:45 +000086 if (!HeaderMaps.empty()) {
87 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
Chris Lattnerdf772332007-12-17 07:52:39 +000088 // Pointer equality comparison of FileEntries works because they are
89 // already uniqued by inode.
Mike Stump1eb44332009-09-09 15:08:12 +000090 if (HeaderMaps[i].first == FE)
Chris Lattner822da612007-12-17 06:36:45 +000091 return HeaderMaps[i].second;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner39b49bc2010-11-23 08:35:12 +000094 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
Chris Lattner822da612007-12-17 06:36:45 +000095 HeaderMaps.push_back(std::make_pair(FE, HM));
96 return HM;
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chris Lattner822da612007-12-17 06:36:45 +000099 return 0;
100}
101
Douglas Gregor21cae202011-09-12 23:31:24 +0000102const FileEntry *HeaderSearch::lookupModule(StringRef ModuleName,
Douglas Gregor6e975c42011-09-13 23:15:45 +0000103 std::string *ModuleFileName,
Douglas Gregor21cae202011-09-12 23:31:24 +0000104 std::string *UmbrellaHeader) {
Douglas Gregor9a6da692011-09-12 20:41:59 +0000105 // If we don't have a module cache path, we can't do anything.
Douglas Gregor6e975c42011-09-13 23:15:45 +0000106 if (ModuleCachePath.empty()) {
107 if (ModuleFileName)
108 ModuleFileName->clear();
Douglas Gregor9a6da692011-09-12 20:41:59 +0000109 return 0;
Douglas Gregor6e975c42011-09-13 23:15:45 +0000110 }
111
Douglas Gregor21cae202011-09-12 23:31:24 +0000112 // Try to find the module path.
Douglas Gregor9a6da692011-09-12 20:41:59 +0000113 llvm::SmallString<256> FileName(ModuleCachePath);
114 llvm::sys::path::append(FileName, ModuleName + ".pcm");
Douglas Gregor6e975c42011-09-13 23:15:45 +0000115 if (ModuleFileName)
116 *ModuleFileName = FileName.str();
117
118 if (const FileEntry *ModuleFile
119 = getFileMgr().getFile(FileName, /*OpenFile=*/false,
120 /*CacheFailure=*/false))
Douglas Gregor21cae202011-09-12 23:31:24 +0000121 return ModuleFile;
122
123 // We didn't find the module. If we're not supposed to look for an
124 // umbrella header, this is the end of the road.
125 if (!UmbrellaHeader)
126 return 0;
127
128 // Look in each of the framework directories for an umbrella header with
129 // the same name as the module.
130 // FIXME: We need a way for non-frameworks to provide umbrella headers.
131 llvm::SmallString<128> UmbrellaHeaderName;
132 UmbrellaHeaderName = ModuleName;
133 UmbrellaHeaderName += '/';
134 UmbrellaHeaderName += ModuleName;
135 UmbrellaHeaderName += ".h";
136 for (unsigned Idx = 0, N = SearchDirs.size(); Idx != N; ++Idx) {
137 // Skip non-framework include paths
138 if (!SearchDirs[Idx].isFramework())
139 continue;
140
141 // Look for the umbrella header in this directory.
142 if (const FileEntry *HeaderFile
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000143 = SearchDirs[Idx].LookupFile(UmbrellaHeaderName, *this, 0, 0,
144 StringRef(), 0)) {
Douglas Gregor21cae202011-09-12 23:31:24 +0000145 *UmbrellaHeader = HeaderFile->getName();
146 return 0;
147 }
148 }
149
150 // We did not find an umbrella header. Clear out the UmbrellaHeader pointee
151 // so our caller knows that we failed.
152 UmbrellaHeader->clear();
153 return 0;
Douglas Gregor9a6da692011-09-12 20:41:59 +0000154}
155
Chris Lattnerdf772332007-12-17 07:52:39 +0000156//===----------------------------------------------------------------------===//
157// File lookup within a DirectoryLookup scope
158//===----------------------------------------------------------------------===//
159
Chris Lattner3af66a92007-12-17 17:57:27 +0000160/// getName - Return the directory or filename corresponding to this lookup
161/// object.
162const char *DirectoryLookup::getName() const {
163 if (isNormalDir())
164 return getDir()->getName();
165 if (isFramework())
166 return getFrameworkDir()->getName();
167 assert(isHeaderMap() && "Unknown DirectoryLookup");
168 return getHeaderMap()->getFileName();
169}
170
171
Chris Lattnerdf772332007-12-17 07:52:39 +0000172/// LookupFile - Lookup the specified file in this search path, returning it
173/// if it exists or returning null if not.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000174const FileEntry *DirectoryLookup::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000175 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000176 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000177 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000178 SmallVectorImpl<char> *RelativePath,
179 StringRef BuildingModule,
180 StringRef *SuggestedModule) const {
Chris Lattnerdf772332007-12-17 07:52:39 +0000181 llvm::SmallString<1024> TmpDir;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000182 if (isNormalDir()) {
183 // Concatenate the requested file onto the directory.
Eli Friedmana6e023c2011-07-08 20:17:28 +0000184 TmpDir = getDir()->getName();
185 llvm::sys::path::append(TmpDir, Filename);
Manuel Klimek74124942011-04-26 21:50:03 +0000186 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000187 StringRef SearchPathRef(getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000188 SearchPath->clear();
189 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
190 }
191 if (RelativePath != NULL) {
192 RelativePath->clear();
193 RelativePath->append(Filename.begin(), Filename.end());
194 }
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000195 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000196 }
Mike Stump1eb44332009-09-09 15:08:12 +0000197
Chris Lattnerafded5b2007-12-17 08:13:48 +0000198 if (isFramework())
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000199 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath,
200 BuildingModule, SuggestedModule);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattnerb09e71f2007-12-17 08:17:39 +0000202 assert(isHeaderMap() && "Unknown directory lookup");
Manuel Klimek74124942011-04-26 21:50:03 +0000203 const FileEntry * const Result = getHeaderMap()->LookupFile(
204 Filename, HS.getFileMgr());
205 if (Result) {
206 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000207 StringRef SearchPathRef(getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000208 SearchPath->clear();
209 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
210 }
211 if (RelativePath != NULL) {
212 RelativePath->clear();
213 RelativePath->append(Filename.begin(), Filename.end());
214 }
215 }
216 return Result;
Chris Lattnerdf772332007-12-17 07:52:39 +0000217}
218
219
Chris Lattnerafded5b2007-12-17 08:13:48 +0000220/// DoFrameworkLookup - Do a lookup of the specified file in the current
221/// DirectoryLookup, which is a framework directory.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000222const FileEntry *DirectoryLookup::DoFrameworkLookup(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000223 StringRef Filename,
Manuel Klimek74124942011-04-26 21:50:03 +0000224 HeaderSearch &HS,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000226 SmallVectorImpl<char> *RelativePath,
227 StringRef BuildingModule,
228 StringRef *SuggestedModule) const
229{
Chris Lattnerafded5b2007-12-17 08:13:48 +0000230 FileManager &FileMgr = HS.getFileMgr();
Mike Stump1eb44332009-09-09 15:08:12 +0000231
Reid Spencer5f016e22007-07-11 17:01:13 +0000232 // Framework names must have a '/' in the filename.
Chris Lattnera1394812010-01-10 01:35:12 +0000233 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000234 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnerafded5b2007-12-17 08:13:48 +0000236 // Find out if this is the home for the specified framework, by checking
237 // HeaderSearch. Possible answer are yes/no and unknown.
Mike Stump1eb44332009-09-09 15:08:12 +0000238 const DirectoryEntry *&FrameworkDirCache =
Chris Lattnera1394812010-01-10 01:35:12 +0000239 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000240
Chris Lattnerafded5b2007-12-17 08:13:48 +0000241 // If it is known and in some other directory, fail.
242 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
Reid Spencer5f016e22007-07-11 17:01:13 +0000243 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Chris Lattnerafded5b2007-12-17 08:13:48 +0000245 // Otherwise, construct the path to this framework dir.
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // FrameworkName = "/System/Library/Frameworks/"
248 llvm::SmallString<1024> FrameworkName;
Chris Lattnerafded5b2007-12-17 08:13:48 +0000249 FrameworkName += getFrameworkDir()->getName();
Reid Spencer5f016e22007-07-11 17:01:13 +0000250 if (FrameworkName.empty() || FrameworkName.back() != '/')
251 FrameworkName.push_back('/');
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Reid Spencer5f016e22007-07-11 17:01:13 +0000253 // FrameworkName = "/System/Library/Frameworks/Cocoa"
Chris Lattnera1394812010-01-10 01:35:12 +0000254 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
257 FrameworkName += ".framework/";
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattnerafded5b2007-12-17 08:13:48 +0000259 // If the cache entry is still unresolved, query to see if the cache entry is
260 // still unresolved. If so, check its existence now.
261 if (FrameworkDirCache == 0) {
262 HS.IncrementFrameworkLookupCount();
Mike Stump1eb44332009-09-09 15:08:12 +0000263
Reid Spencer5f016e22007-07-11 17:01:13 +0000264 // If the framework dir doesn't exist, we fail.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000265 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
Michael J. Spencer32bef4e2011-01-10 02:34:13 +0000266 bool Exists;
267 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
Reid Spencer5f016e22007-07-11 17:01:13 +0000268 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 // Otherwise, if it does, remember that this is the right direntry for this
271 // framework.
Chris Lattnerafded5b2007-12-17 08:13:48 +0000272 FrameworkDirCache = getFrameworkDir();
Reid Spencer5f016e22007-07-11 17:01:13 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Manuel Klimek74124942011-04-26 21:50:03 +0000275 if (RelativePath != NULL) {
276 RelativePath->clear();
277 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
278 }
279
Reid Spencer5f016e22007-07-11 17:01:13 +0000280 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
281 unsigned OrigSize = FrameworkName.size();
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Reid Spencer5f016e22007-07-11 17:01:13 +0000283 FrameworkName += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000284
285 if (SearchPath != NULL) {
286 SearchPath->clear();
287 // Without trailing '/'.
288 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
289 }
290
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000291 /// Determine whether this is the module we're building or not.
292 bool AutomaticImport = SuggestedModule &&
Douglas Gregor0fd787b2011-09-16 00:22:46 +0000293 (BuildingModule != StringRef(Filename.begin(), SlashPos)) &&
294 !Filename.substr(SlashPos + 1).startswith("..");
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000295
Chris Lattnera1394812010-01-10 01:35:12 +0000296 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000297 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000298 /*openFile=*/!AutomaticImport)) {
299 if (AutomaticImport)
300 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000301 return FE;
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000302 }
Mike Stump1eb44332009-09-09 15:08:12 +0000303
Reid Spencer5f016e22007-07-11 17:01:13 +0000304 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
305 const char *Private = "Private";
Mike Stump1eb44332009-09-09 15:08:12 +0000306 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Reid Spencer5f016e22007-07-11 17:01:13 +0000307 Private+strlen(Private));
Manuel Klimek74124942011-04-26 21:50:03 +0000308 if (SearchPath != NULL)
309 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
310 Private+strlen(Private));
311
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000312 const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
313 /*openFile=*/!AutomaticImport);
314 if (FE && AutomaticImport)
315 *SuggestedModule = StringRef(Filename.begin(), SlashPos);
316 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}
318
Chris Lattnerdf772332007-12-17 07:52:39 +0000319
Chris Lattnerafded5b2007-12-17 08:13:48 +0000320//===----------------------------------------------------------------------===//
321// Header File Location.
322//===----------------------------------------------------------------------===//
323
324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
326/// return null on failure. isAngled indicates whether the file reference is
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000327/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
328/// non-null, indicates where the #including file is, in case a relative search
329/// is needed.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000330const FileEntry *HeaderSearch::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000331 StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000332 bool isAngled,
333 const DirectoryLookup *FromDir,
334 const DirectoryLookup *&CurDir,
335 const FileEntry *CurFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000336 SmallVectorImpl<char> *SearchPath,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000337 SmallVectorImpl<char> *RelativePath,
338 StringRef *SuggestedModule)
339{
340 if (SuggestedModule)
341 *SuggestedModule = StringRef();
342
Reid Spencer5f016e22007-07-11 17:01:13 +0000343 // If 'Filename' is absolute, check to see if it exists and no searching.
Michael J. Spencer256053b2010-12-17 21:22:22 +0000344 if (llvm::sys::path::is_absolute(Filename)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 CurDir = 0;
346
347 // If this was an #include_next "/absolute/file", fail.
348 if (FromDir) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000349
Manuel Klimek74124942011-04-26 21:50:03 +0000350 if (SearchPath != NULL)
351 SearchPath->clear();
352 if (RelativePath != NULL) {
353 RelativePath->clear();
354 RelativePath->append(Filename.begin(), Filename.end());
355 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 // Otherwise, just return the file.
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000357 return FileMgr.getFile(Filename, /*openFile=*/true);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 }
Mike Stump1eb44332009-09-09 15:08:12 +0000359
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000360 // Unless disabled, check to see if the file is in the #includer's
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000361 // directory. This has to be based on CurFileEnt, not CurDir, because
362 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
Chris Lattnerdf772332007-12-17 07:52:39 +0000363 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
364 // This search is not done for <> headers.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000365 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
366 llvm::SmallString<1024> TmpDir;
367 // Concatenate the requested file onto the directory.
368 // FIXME: Portability. Filename concatenation should be in sys::Path.
369 TmpDir += CurFileEnt->getDir()->getName();
370 TmpDir.push_back('/');
371 TmpDir.append(Filename.begin(), Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000372 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 // Leave CurDir unset.
Douglas Gregor10fe93d2010-08-08 07:49:23 +0000374 // This file is a system header or C++ unfriendly if the old file is.
375 //
376 // Note that the temporary 'DirInfo' is required here, as either call to
377 // getFileInfo could resize the vector and we don't want to rely on order
378 // of evaluation.
379 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000380 getFileInfo(FE).DirInfo = DirInfo;
Manuel Klimek74124942011-04-26 21:50:03 +0000381 if (SearchPath != NULL) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000382 StringRef SearchPathRef(CurFileEnt->getDir()->getName());
Manuel Klimek74124942011-04-26 21:50:03 +0000383 SearchPath->clear();
384 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
385 }
386 if (RelativePath != NULL) {
387 RelativePath->clear();
388 RelativePath->append(Filename.begin(), Filename.end());
389 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 return FE;
391 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 }
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394 CurDir = 0;
395
396 // If this is a system #include, ignore the user #include locs.
Nico Weber74a5fd82011-05-24 04:31:14 +0000397 unsigned i = isAngled ? AngledDirIdx : 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000398
Reid Spencer5f016e22007-07-11 17:01:13 +0000399 // If this is a #include_next request, start searching after the directory the
400 // file was found in.
401 if (FromDir)
402 i = FromDir-&SearchDirs[0];
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner9960ae82007-07-22 07:28:00 +0000404 // Cache all of the lookups performed by this method. Many headers are
405 // multiply included, and the "pragma once" optimization prevents them from
406 // being relex/pp'd, but they would still have to search through a
407 // (potentially huge) series of SearchDirs to find it.
408 std::pair<unsigned, unsigned> &CacheLookup =
Chris Lattnera1394812010-01-10 01:35:12 +0000409 LookupFileCache.GetOrCreateValue(Filename).getValue();
Chris Lattner9960ae82007-07-22 07:28:00 +0000410
411 // If the entry has been previously looked up, the first value will be
412 // non-zero. If the value is equal to i (the start point of our search), then
413 // this is a matching hit.
414 if (CacheLookup.first == i+1) {
415 // Skip querying potentially lots of directories for this lookup.
416 i = CacheLookup.second;
417 } else {
418 // Otherwise, this is the first query, or the previous query didn't match
419 // our search start. We will fill in our found location below, so prime the
420 // start point value.
421 CacheLookup.first = i+1;
422 }
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 // Check each directory in sequence to see if it contains this file.
425 for (; i != SearchDirs.size(); ++i) {
Mike Stump1eb44332009-09-09 15:08:12 +0000426 const FileEntry *FE =
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000427 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath,
428 BuildingModule, SuggestedModule);
Chris Lattnerafded5b2007-12-17 08:13:48 +0000429 if (!FE) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattnerafded5b2007-12-17 08:13:48 +0000431 CurDir = &SearchDirs[i];
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Chris Lattnerafded5b2007-12-17 08:13:48 +0000433 // This file is a system header or C++ unfriendly if the dir is.
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000434 HeaderFileInfo &HFI = getFileInfo(FE);
435 HFI.DirInfo = CurDir->getDirCharacteristic();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000437 // If this file is found in a header map and uses the framework style of
438 // includes, then this header is part of a framework we're building.
439 if (CurDir->isIndexHeaderMap()) {
440 size_t SlashPos = Filename.find('/');
441 if (SlashPos != StringRef::npos) {
442 HFI.IndexHeaderMapHeader = 1;
443 HFI.Framework = getUniqueFrameworkName(StringRef(Filename.begin(),
444 SlashPos));
445 }
446 }
447
Chris Lattnerafded5b2007-12-17 08:13:48 +0000448 // Remember this location for the next lookup we do.
449 CacheLookup.second = i;
450 return FE;
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 }
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000453 // If we are including a file with a quoted include "foo.h" from inside
454 // a header in a framework that is currently being built, and we couldn't
455 // resolve "foo.h" any other way, change the include to <Foo/foo.h>, where
456 // "Foo" is the name of the framework in which the including header was found.
457 if (CurFileEnt && !isAngled && Filename.find('/') == StringRef::npos) {
458 HeaderFileInfo &IncludingHFI = getFileInfo(CurFileEnt);
459 if (IncludingHFI.IndexHeaderMapHeader) {
460 llvm::SmallString<128> ScratchFilename;
461 ScratchFilename += IncludingHFI.Framework;
462 ScratchFilename += '/';
463 ScratchFilename += Filename;
464
465 const FileEntry *Result = LookupFile(ScratchFilename, /*isAngled=*/true,
466 FromDir, CurDir, CurFileEnt,
Douglas Gregorfba18aa2011-09-15 22:00:41 +0000467 SearchPath, RelativePath,
468 SuggestedModule);
Douglas Gregor2c7b7802011-07-30 06:28:34 +0000469 std::pair<unsigned, unsigned> &CacheLookup
470 = LookupFileCache.GetOrCreateValue(Filename).getValue();
471 CacheLookup.second
472 = LookupFileCache.GetOrCreateValue(ScratchFilename).getValue().second;
473 return Result;
474 }
475 }
476
Chris Lattner9960ae82007-07-22 07:28:00 +0000477 // Otherwise, didn't find it. Remember we didn't find this.
478 CacheLookup.second = SearchDirs.size();
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 return 0;
480}
481
482/// LookupSubframeworkHeader - Look up a subframework for the specified
483/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
484/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
485/// is a subframework within Carbon.framework. If so, return the FileEntry
486/// for the designated file, otherwise return null.
487const FileEntry *HeaderSearch::
Chris Lattner5f9e2722011-07-23 10:55:15 +0000488LookupSubframeworkHeader(StringRef Filename,
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000489 const FileEntry *ContextFileEnt,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000490 SmallVectorImpl<char> *SearchPath,
491 SmallVectorImpl<char> *RelativePath) {
Chris Lattner9415a0c2008-02-01 05:34:02 +0000492 assert(ContextFileEnt && "No context file?");
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 // Framework names must have a '/' in the filename. Find it.
Chris Lattnera1394812010-01-10 01:35:12 +0000495 size_t SlashPos = Filename.find('/');
Chris Lattner5f9e2722011-07-23 10:55:15 +0000496 if (SlashPos == StringRef::npos) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 // Look up the base framework name of the ContextFileEnt.
499 const char *ContextName = ContextFileEnt->getName();
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 // If the context info wasn't a framework, couldn't be a subframework.
502 const char *FrameworkPos = strstr(ContextName, ".framework/");
503 if (FrameworkPos == 0)
504 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
506 llvm::SmallString<1024> FrameworkName(ContextName,
Reid Spencer5f016e22007-07-11 17:01:13 +0000507 FrameworkPos+strlen(".framework/"));
508
509 // Append Frameworks/HIToolbox.framework/
510 FrameworkName += "Frameworks/";
Chris Lattnera1394812010-01-10 01:35:12 +0000511 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000512 FrameworkName += ".framework/";
513
514 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
Chris Lattner65382272010-11-21 09:55:08 +0000515 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 // Some other location?
518 if (CacheLookup.getValue() &&
519 CacheLookup.getKeyLength() == FrameworkName.size() &&
520 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
521 CacheLookup.getKeyLength()) != 0)
522 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Reid Spencer5f016e22007-07-11 17:01:13 +0000524 // Cache subframework.
525 if (CacheLookup.getValue() == 0) {
526 ++NumSubFrameworkLookups;
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Reid Spencer5f016e22007-07-11 17:01:13 +0000528 // If the framework dir doesn't exist, we fail.
Chris Lattner39b49bc2010-11-23 08:35:12 +0000529 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 if (Dir == 0) return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 // Otherwise, if it does, remember that this is the right direntry for this
533 // framework.
534 CacheLookup.setValue(Dir);
535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 const FileEntry *FE = 0;
538
Manuel Klimek74124942011-04-26 21:50:03 +0000539 if (RelativePath != NULL) {
540 RelativePath->clear();
541 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
542 }
543
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
545 llvm::SmallString<1024> HeadersFilename(FrameworkName);
546 HeadersFilename += "Headers/";
Manuel Klimek74124942011-04-26 21:50:03 +0000547 if (SearchPath != NULL) {
548 SearchPath->clear();
549 // Without trailing '/'.
550 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
551 }
552
Chris Lattnera1394812010-01-10 01:35:12 +0000553 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000554 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
Mike Stump1eb44332009-09-09 15:08:12 +0000555
Reid Spencer5f016e22007-07-11 17:01:13 +0000556 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
557 HeadersFilename = FrameworkName;
558 HeadersFilename += "PrivateHeaders/";
Manuel Klimek74124942011-04-26 21:50:03 +0000559 if (SearchPath != NULL) {
560 SearchPath->clear();
561 // Without trailing '/'.
562 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
563 }
564
Chris Lattnera1394812010-01-10 01:35:12 +0000565 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
Argyrios Kyrtzidis3cd01282011-03-16 19:17:25 +0000566 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 return 0;
568 }
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Reid Spencer5f016e22007-07-11 17:01:13 +0000570 // This file is a system header or C++ unfriendly if the old file is.
Ted Kremenekca63fa02008-02-24 03:55:14 +0000571 //
Chris Lattnerc9dde4f2008-02-25 21:38:21 +0000572 // Note that the temporary 'DirInfo' is required here, as either call to
573 // getFileInfo could resize the vector and we don't want to rely on order
574 // of evaluation.
575 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
576 getFileInfo(FE).DirInfo = DirInfo;
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 return FE;
578}
579
580//===----------------------------------------------------------------------===//
581// File Info Management.
582//===----------------------------------------------------------------------===//
583
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000584/// \brief Merge the header file info provided by \p OtherHFI into the current
585/// header file info (\p HFI)
586static void mergeHeaderFileInfo(HeaderFileInfo &HFI,
587 const HeaderFileInfo &OtherHFI) {
588 HFI.isImport |= OtherHFI.isImport;
589 HFI.isPragmaOnce |= OtherHFI.isPragmaOnce;
590 HFI.NumIncludes += OtherHFI.NumIncludes;
591
592 if (!HFI.ControllingMacro && !HFI.ControllingMacroID) {
593 HFI.ControllingMacro = OtherHFI.ControllingMacro;
594 HFI.ControllingMacroID = OtherHFI.ControllingMacroID;
595 }
596
597 if (OtherHFI.External) {
598 HFI.DirInfo = OtherHFI.DirInfo;
599 HFI.External = OtherHFI.External;
600 HFI.IndexHeaderMapHeader = OtherHFI.IndexHeaderMapHeader;
601 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000602
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000603 if (HFI.Framework.empty())
604 HFI.Framework = OtherHFI.Framework;
605
606 HFI.Resolved = true;
607}
608
Steve Naroff83d63c72009-04-24 20:03:17 +0000609/// getFileInfo - Return the HeaderFileInfo structure for the specified
Reid Spencer5f016e22007-07-11 17:01:13 +0000610/// FileEntry.
Steve Naroff83d63c72009-04-24 20:03:17 +0000611HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000612 if (FE->getUID() >= FileInfo.size())
613 FileInfo.resize(FE->getUID()+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000614
615 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000616 if (ExternalSource && !HFI.Resolved)
617 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(FE));
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000618 return HFI;
Mike Stump1eb44332009-09-09 15:08:12 +0000619}
Reid Spencer5f016e22007-07-11 17:01:13 +0000620
Douglas Gregordd3e5542011-05-04 00:14:37 +0000621bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
622 // Check if we've ever seen this file as a header.
623 if (File->getUID() >= FileInfo.size())
624 return false;
625
626 // Resolve header file info from the external source, if needed.
627 HeaderFileInfo &HFI = FileInfo[File->getUID()];
Douglas Gregor8f8d5812011-09-17 05:35:18 +0000628 if (ExternalSource && !HFI.Resolved)
629 mergeHeaderFileInfo(HFI, ExternalSource->GetHeaderFileInfo(File));
Douglas Gregordd3e5542011-05-04 00:14:37 +0000630
631 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
632}
633
Steve Naroff83d63c72009-04-24 20:03:17 +0000634void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
635 if (UID >= FileInfo.size())
636 FileInfo.resize(UID+1);
Douglas Gregorcfbf1c72011-02-10 17:09:37 +0000637 HFI.Resolved = true;
Steve Naroff83d63c72009-04-24 20:03:17 +0000638 FileInfo[UID] = HFI;
639}
640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
642/// #include, #include_next, or #import directive. Return false if #including
643/// the file will have no effect or true if we should include it.
644bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
645 ++NumIncluded; // Count # of attempted #includes.
646
647 // Get information about this file.
Steve Naroff83d63c72009-04-24 20:03:17 +0000648 HeaderFileInfo &FileInfo = getFileInfo(File);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650 // If this is a #import directive, check that we have not already imported
651 // this header.
652 if (isImport) {
653 // If this has already been imported, don't import it again.
654 FileInfo.isImport = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 // Has this already been #import'ed or #include'd?
657 if (FileInfo.NumIncludes) return false;
658 } else {
659 // Otherwise, if this is a #include of a file that was previously #import'd
660 // or if this is the second #include of a #pragma once file, ignore it.
661 if (FileInfo.isImport)
662 return false;
663 }
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
666 // if the macro that guards it is defined, we know the #include has no effect.
Mike Stump1eb44332009-09-09 15:08:12 +0000667 if (const IdentifierInfo *ControllingMacro
Douglas Gregor8c5a7602009-04-25 23:30:02 +0000668 = FileInfo.getControllingMacro(ExternalLookup))
669 if (ControllingMacro->hasMacroDefinition()) {
670 ++NumMultiIncludeFileOptzn;
671 return false;
672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // Increment the number of times this file has been included.
675 ++FileInfo.NumIncludes;
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 return true;
678}
679
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000680size_t HeaderSearch::getTotalMemory() const {
681 return SearchDirs.capacity()
Ted Kremenekeabea452011-07-27 18:41:18 +0000682 + llvm::capacity_in_bytes(FileInfo)
683 + llvm::capacity_in_bytes(HeaderMaps)
Ted Kremenekd1194fb2011-07-26 23:46:11 +0000684 + LookupFileCache.getAllocator().getTotalMemory()
685 + FrameworkMap.getAllocator().getTotalMemory();
686}
Douglas Gregor65e02fa2011-07-28 04:45:53 +0000687
688StringRef HeaderSearch::getUniqueFrameworkName(StringRef Framework) {
689 return FrameworkNames.GetOrCreateValue(Framework).getKey();
690}