1 //===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DirectoryLookup and HeaderSearch interfaces.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/HeaderSearch.h"
15 #include "clang/Lex/HeaderMap.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/IdentifierTable.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/ADT/SmallString.h"
22 using namespace clang;
24 const IdentifierInfo *
25 HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
27 return ControllingMacro;
29 if (!ControllingMacroID || !External)
32 ControllingMacro = External->GetIdentifier(ControllingMacroID);
33 return ControllingMacro;
36 ExternalHeaderFileInfoSource::~ExternalHeaderFileInfoSource() {}
38 HeaderSearch::HeaderSearch(FileManager &FM)
39 : FileMgr(FM), FrameworkMap(64) {
41 NoCurDirSearch = false;
46 NumMultiIncludeFileOptzn = 0;
47 NumFrameworkLookups = NumSubFrameworkLookups = 0;
50 HeaderSearch::~HeaderSearch() {
52 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
53 delete HeaderMaps[i].second;
56 void HeaderSearch::PrintStats() {
57 fprintf(stderr, "\n*** HeaderSearch Stats:\n");
58 fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
59 unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
60 for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
61 NumOnceOnlyFiles += FileInfo[i].isImport;
62 if (MaxNumIncludes < FileInfo[i].NumIncludes)
63 MaxNumIncludes = FileInfo[i].NumIncludes;
64 NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
66 fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
67 fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
68 fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
70 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
71 fprintf(stderr, " %d #includes skipped due to"
72 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
74 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
75 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
78 /// CreateHeaderMap - This method returns a HeaderMap for the specified
79 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
80 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
81 // We expect the number of headermaps to be small, and almost always empty.
82 // If it ever grows, use of a linear search should be re-evaluated.
83 if (!HeaderMaps.empty()) {
84 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
85 // Pointer equality comparison of FileEntries works because they are
86 // already uniqued by inode.
87 if (HeaderMaps[i].first == FE)
88 return HeaderMaps[i].second;
91 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
92 HeaderMaps.push_back(std::make_pair(FE, HM));
99 //===----------------------------------------------------------------------===//
100 // File lookup within a DirectoryLookup scope
101 //===----------------------------------------------------------------------===//
103 /// getName - Return the directory or filename corresponding to this lookup
105 const char *DirectoryLookup::getName() const {
107 return getDir()->getName();
109 return getFrameworkDir()->getName();
110 assert(isHeaderMap() && "Unknown DirectoryLookup");
111 return getHeaderMap()->getFileName();
115 /// LookupFile - Lookup the specified file in this search path, returning it
116 /// if it exists or returning null if not.
117 const FileEntry *DirectoryLookup::LookupFile(
118 llvm::StringRef Filename,
120 llvm::SmallVectorImpl<char> *SearchPath,
121 llvm::SmallVectorImpl<char> *RelativePath) const {
122 llvm::SmallString<1024> TmpDir;
124 // Concatenate the requested file onto the directory.
125 // FIXME: Portability. Filename concatenation should be in sys::Path.
126 TmpDir += getDir()->getName();
127 TmpDir.push_back('/');
128 TmpDir.append(Filename.begin(), Filename.end());
129 if (SearchPath != NULL) {
130 llvm::StringRef SearchPathRef(getDir()->getName());
132 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
134 if (RelativePath != NULL) {
135 RelativePath->clear();
136 RelativePath->append(Filename.begin(), Filename.end());
138 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
142 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
144 assert(isHeaderMap() && "Unknown directory lookup");
145 const FileEntry * const Result = getHeaderMap()->LookupFile(
146 Filename, HS.getFileMgr());
148 if (SearchPath != NULL) {
149 llvm::StringRef SearchPathRef(getName());
151 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
153 if (RelativePath != NULL) {
154 RelativePath->clear();
155 RelativePath->append(Filename.begin(), Filename.end());
162 /// DoFrameworkLookup - Do a lookup of the specified file in the current
163 /// DirectoryLookup, which is a framework directory.
164 const FileEntry *DirectoryLookup::DoFrameworkLookup(
165 llvm::StringRef Filename,
167 llvm::SmallVectorImpl<char> *SearchPath,
168 llvm::SmallVectorImpl<char> *RelativePath) const {
169 FileManager &FileMgr = HS.getFileMgr();
171 // Framework names must have a '/' in the filename.
172 size_t SlashPos = Filename.find('/');
173 if (SlashPos == llvm::StringRef::npos) return 0;
175 // Find out if this is the home for the specified framework, by checking
176 // HeaderSearch. Possible answer are yes/no and unknown.
177 const DirectoryEntry *&FrameworkDirCache =
178 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
180 // If it is known and in some other directory, fail.
181 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
184 // Otherwise, construct the path to this framework dir.
186 // FrameworkName = "/System/Library/Frameworks/"
187 llvm::SmallString<1024> FrameworkName;
188 FrameworkName += getFrameworkDir()->getName();
189 if (FrameworkName.empty() || FrameworkName.back() != '/')
190 FrameworkName.push_back('/');
192 // FrameworkName = "/System/Library/Frameworks/Cocoa"
193 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
195 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
196 FrameworkName += ".framework/";
198 // If the cache entry is still unresolved, query to see if the cache entry is
199 // still unresolved. If so, check its existence now.
200 if (FrameworkDirCache == 0) {
201 HS.IncrementFrameworkLookupCount();
203 // If the framework dir doesn't exist, we fail.
204 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
206 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
209 // Otherwise, if it does, remember that this is the right direntry for this
211 FrameworkDirCache = getFrameworkDir();
214 if (RelativePath != NULL) {
215 RelativePath->clear();
216 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
219 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
220 unsigned OrigSize = FrameworkName.size();
222 FrameworkName += "Headers/";
224 if (SearchPath != NULL) {
226 // Without trailing '/'.
227 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
230 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
231 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
232 /*openFile=*/true)) {
236 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
237 const char *Private = "Private";
238 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
239 Private+strlen(Private));
240 if (SearchPath != NULL)
241 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
242 Private+strlen(Private));
244 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
248 //===----------------------------------------------------------------------===//
249 // Header File Location.
250 //===----------------------------------------------------------------------===//
253 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
254 /// return null on failure. isAngled indicates whether the file reference is
255 /// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
256 /// non-null, indicates where the #including file is, in case a relative search
258 const FileEntry *HeaderSearch::LookupFile(
259 llvm::StringRef Filename,
261 const DirectoryLookup *FromDir,
262 const DirectoryLookup *&CurDir,
263 const FileEntry *CurFileEnt,
264 llvm::SmallVectorImpl<char> *SearchPath,
265 llvm::SmallVectorImpl<char> *RelativePath) {
266 // If 'Filename' is absolute, check to see if it exists and no searching.
267 if (llvm::sys::path::is_absolute(Filename)) {
270 // If this was an #include_next "/absolute/file", fail.
271 if (FromDir) return 0;
273 if (SearchPath != NULL)
275 if (RelativePath != NULL) {
276 RelativePath->clear();
277 RelativePath->append(Filename.begin(), Filename.end());
279 // Otherwise, just return the file.
280 return FileMgr.getFile(Filename, /*openFile=*/true);
283 // Step #0, unless disabled, check to see if the file is in the #includer's
284 // directory. This has to be based on CurFileEnt, not CurDir, because
285 // CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
286 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
287 // This search is not done for <> headers.
288 if (CurFileEnt && !isAngled && !NoCurDirSearch) {
289 llvm::SmallString<1024> TmpDir;
290 // Concatenate the requested file onto the directory.
291 // FIXME: Portability. Filename concatenation should be in sys::Path.
292 TmpDir += CurFileEnt->getDir()->getName();
293 TmpDir.push_back('/');
294 TmpDir.append(Filename.begin(), Filename.end());
295 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
296 // Leave CurDir unset.
297 // This file is a system header or C++ unfriendly if the old file is.
299 // Note that the temporary 'DirInfo' is required here, as either call to
300 // getFileInfo could resize the vector and we don't want to rely on order
302 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
303 getFileInfo(FE).DirInfo = DirInfo;
304 if (SearchPath != NULL) {
305 llvm::StringRef SearchPathRef(CurFileEnt->getDir()->getName());
307 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
309 if (RelativePath != NULL) {
310 RelativePath->clear();
311 RelativePath->append(Filename.begin(), Filename.end());
319 // If this is a system #include, ignore the user #include locs.
320 unsigned i = isAngled ? SystemDirIdx : 0;
322 // If this is a #include_next request, start searching after the directory the
323 // file was found in.
325 i = FromDir-&SearchDirs[0];
327 // Cache all of the lookups performed by this method. Many headers are
328 // multiply included, and the "pragma once" optimization prevents them from
329 // being relex/pp'd, but they would still have to search through a
330 // (potentially huge) series of SearchDirs to find it.
331 std::pair<unsigned, unsigned> &CacheLookup =
332 LookupFileCache.GetOrCreateValue(Filename).getValue();
334 // If the entry has been previously looked up, the first value will be
335 // non-zero. If the value is equal to i (the start point of our search), then
336 // this is a matching hit.
337 if (CacheLookup.first == i+1) {
338 // Skip querying potentially lots of directories for this lookup.
339 i = CacheLookup.second;
341 // Otherwise, this is the first query, or the previous query didn't match
342 // our search start. We will fill in our found location below, so prime the
343 // start point value.
344 CacheLookup.first = i+1;
347 // Check each directory in sequence to see if it contains this file.
348 for (; i != SearchDirs.size(); ++i) {
349 const FileEntry *FE =
350 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
353 CurDir = &SearchDirs[i];
355 // This file is a system header or C++ unfriendly if the dir is.
356 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
358 // Remember this location for the next lookup we do.
359 CacheLookup.second = i;
363 // Otherwise, didn't find it. Remember we didn't find this.
364 CacheLookup.second = SearchDirs.size();
368 /// LookupSubframeworkHeader - Look up a subframework for the specified
369 /// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
370 /// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
371 /// is a subframework within Carbon.framework. If so, return the FileEntry
372 /// for the designated file, otherwise return null.
373 const FileEntry *HeaderSearch::
374 LookupSubframeworkHeader(llvm::StringRef Filename,
375 const FileEntry *ContextFileEnt,
376 llvm::SmallVectorImpl<char> *SearchPath,
377 llvm::SmallVectorImpl<char> *RelativePath) {
378 assert(ContextFileEnt && "No context file?");
380 // Framework names must have a '/' in the filename. Find it.
381 size_t SlashPos = Filename.find('/');
382 if (SlashPos == llvm::StringRef::npos) return 0;
384 // Look up the base framework name of the ContextFileEnt.
385 const char *ContextName = ContextFileEnt->getName();
387 // If the context info wasn't a framework, couldn't be a subframework.
388 const char *FrameworkPos = strstr(ContextName, ".framework/");
389 if (FrameworkPos == 0)
392 llvm::SmallString<1024> FrameworkName(ContextName,
393 FrameworkPos+strlen(".framework/"));
395 // Append Frameworks/HIToolbox.framework/
396 FrameworkName += "Frameworks/";
397 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
398 FrameworkName += ".framework/";
400 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
401 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
403 // Some other location?
404 if (CacheLookup.getValue() &&
405 CacheLookup.getKeyLength() == FrameworkName.size() &&
406 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
407 CacheLookup.getKeyLength()) != 0)
410 // Cache subframework.
411 if (CacheLookup.getValue() == 0) {
412 ++NumSubFrameworkLookups;
414 // If the framework dir doesn't exist, we fail.
415 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
416 if (Dir == 0) return 0;
418 // Otherwise, if it does, remember that this is the right direntry for this
420 CacheLookup.setValue(Dir);
423 const FileEntry *FE = 0;
425 if (RelativePath != NULL) {
426 RelativePath->clear();
427 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
430 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
431 llvm::SmallString<1024> HeadersFilename(FrameworkName);
432 HeadersFilename += "Headers/";
433 if (SearchPath != NULL) {
435 // Without trailing '/'.
436 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
439 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
440 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
442 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
443 HeadersFilename = FrameworkName;
444 HeadersFilename += "PrivateHeaders/";
445 if (SearchPath != NULL) {
447 // Without trailing '/'.
448 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
451 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
452 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
456 // This file is a system header or C++ unfriendly if the old file is.
458 // Note that the temporary 'DirInfo' is required here, as either call to
459 // getFileInfo could resize the vector and we don't want to rely on order
461 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
462 getFileInfo(FE).DirInfo = DirInfo;
466 //===----------------------------------------------------------------------===//
467 // File Info Management.
468 //===----------------------------------------------------------------------===//
471 /// getFileInfo - Return the HeaderFileInfo structure for the specified
473 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
474 if (FE->getUID() >= FileInfo.size())
475 FileInfo.resize(FE->getUID()+1);
477 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
478 if (ExternalSource && !HFI.Resolved) {
479 HFI = ExternalSource->GetHeaderFileInfo(FE);
485 void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
486 if (UID >= FileInfo.size())
487 FileInfo.resize(UID+1);
492 /// ShouldEnterIncludeFile - Mark the specified file as a target of of a
493 /// #include, #include_next, or #import directive. Return false if #including
494 /// the file will have no effect or true if we should include it.
495 bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
496 ++NumIncluded; // Count # of attempted #includes.
498 // Get information about this file.
499 HeaderFileInfo &FileInfo = getFileInfo(File);
501 // If this is a #import directive, check that we have not already imported
504 // If this has already been imported, don't import it again.
505 FileInfo.isImport = true;
507 // Has this already been #import'ed or #include'd?
508 if (FileInfo.NumIncludes) return false;
510 // Otherwise, if this is a #include of a file that was previously #import'd
511 // or if this is the second #include of a #pragma once file, ignore it.
512 if (FileInfo.isImport)
516 // Next, check to see if the file is wrapped with #ifndef guards. If so, and
517 // if the macro that guards it is defined, we know the #include has no effect.
518 if (const IdentifierInfo *ControllingMacro
519 = FileInfo.getControllingMacro(ExternalLookup))
520 if (ControllingMacro->hasMacroDefinition()) {
521 ++NumMultiIncludeFileOptzn;
525 // Increment the number of times this file has been included.
526 ++FileInfo.NumIncludes;