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) {
42 NoCurDirSearch = false;
47 NumMultiIncludeFileOptzn = 0;
48 NumFrameworkLookups = NumSubFrameworkLookups = 0;
51 HeaderSearch::~HeaderSearch() {
53 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
54 delete HeaderMaps[i].second;
57 void 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;
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);
71 fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
72 fprintf(stderr, " %d #includes skipped due to"
73 " the multi-include optimization.\n", NumMultiIncludeFileOptzn);
75 fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
76 fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
79 /// CreateHeaderMap - This method returns a HeaderMap for the specified
80 /// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
81 const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
82 // We expect the number of headermaps to be small, and almost always empty.
83 // If it ever grows, use of a linear search should be re-evaluated.
84 if (!HeaderMaps.empty()) {
85 for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
86 // Pointer equality comparison of FileEntries works because they are
87 // already uniqued by inode.
88 if (HeaderMaps[i].first == FE)
89 return HeaderMaps[i].second;
92 if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
93 HeaderMaps.push_back(std::make_pair(FE, HM));
100 //===----------------------------------------------------------------------===//
101 // File lookup within a DirectoryLookup scope
102 //===----------------------------------------------------------------------===//
104 /// getName - Return the directory or filename corresponding to this lookup
106 const char *DirectoryLookup::getName() const {
108 return getDir()->getName();
110 return getFrameworkDir()->getName();
111 assert(isHeaderMap() && "Unknown DirectoryLookup");
112 return getHeaderMap()->getFileName();
116 /// LookupFile - Lookup the specified file in this search path, returning it
117 /// if it exists or returning null if not.
118 const FileEntry *DirectoryLookup::LookupFile(
119 llvm::StringRef Filename,
121 llvm::SmallVectorImpl<char> *SearchPath,
122 llvm::SmallVectorImpl<char> *RelativePath) const {
123 llvm::SmallString<1024> TmpDir;
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('/');
129 TmpDir.append(Filename.begin(), Filename.end());
130 if (SearchPath != NULL) {
131 llvm::StringRef SearchPathRef(getDir()->getName());
133 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
135 if (RelativePath != NULL) {
136 RelativePath->clear();
137 RelativePath->append(Filename.begin(), Filename.end());
139 return HS.getFileMgr().getFile(TmpDir.str(), /*openFile=*/true);
143 return DoFrameworkLookup(Filename, HS, SearchPath, RelativePath);
145 assert(isHeaderMap() && "Unknown directory lookup");
146 const FileEntry * const Result = getHeaderMap()->LookupFile(
147 Filename, HS.getFileMgr());
149 if (SearchPath != NULL) {
150 llvm::StringRef SearchPathRef(getName());
152 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
154 if (RelativePath != NULL) {
155 RelativePath->clear();
156 RelativePath->append(Filename.begin(), Filename.end());
163 /// DoFrameworkLookup - Do a lookup of the specified file in the current
164 /// DirectoryLookup, which is a framework directory.
165 const FileEntry *DirectoryLookup::DoFrameworkLookup(
166 llvm::StringRef Filename,
168 llvm::SmallVectorImpl<char> *SearchPath,
169 llvm::SmallVectorImpl<char> *RelativePath) const {
170 FileManager &FileMgr = HS.getFileMgr();
172 // Framework names must have a '/' in the filename.
173 size_t SlashPos = Filename.find('/');
174 if (SlashPos == llvm::StringRef::npos) return 0;
176 // Find out if this is the home for the specified framework, by checking
177 // HeaderSearch. Possible answer are yes/no and unknown.
178 const DirectoryEntry *&FrameworkDirCache =
179 HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
181 // If it is known and in some other directory, fail.
182 if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
185 // Otherwise, construct the path to this framework dir.
187 // FrameworkName = "/System/Library/Frameworks/"
188 llvm::SmallString<1024> FrameworkName;
189 FrameworkName += getFrameworkDir()->getName();
190 if (FrameworkName.empty() || FrameworkName.back() != '/')
191 FrameworkName.push_back('/');
193 // FrameworkName = "/System/Library/Frameworks/Cocoa"
194 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
196 // FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
197 FrameworkName += ".framework/";
199 // 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();
204 // If the framework dir doesn't exist, we fail.
205 // FIXME: It's probably more efficient to query this with FileMgr.getDir.
207 if (llvm::sys::fs::exists(FrameworkName.str(), Exists) || !Exists)
210 // Otherwise, if it does, remember that this is the right direntry for this
212 FrameworkDirCache = getFrameworkDir();
215 if (RelativePath != NULL) {
216 RelativePath->clear();
217 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
220 // Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
221 unsigned OrigSize = FrameworkName.size();
223 FrameworkName += "Headers/";
225 if (SearchPath != NULL) {
227 // Without trailing '/'.
228 SearchPath->append(FrameworkName.begin(), FrameworkName.end()-1);
231 FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
232 if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str(),
233 /*openFile=*/true)) {
237 // Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
238 const char *Private = "Private";
239 FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
240 Private+strlen(Private));
241 if (SearchPath != NULL)
242 SearchPath->insert(SearchPath->begin()+OrigSize, Private,
243 Private+strlen(Private));
245 return FileMgr.getFile(FrameworkName.str(), /*openFile=*/true);
249 //===----------------------------------------------------------------------===//
250 // Header File Location.
251 //===----------------------------------------------------------------------===//
254 /// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
255 /// return null on failure. isAngled indicates whether the file reference is
256 /// 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
259 const FileEntry *HeaderSearch::LookupFile(
260 llvm::StringRef Filename,
262 const DirectoryLookup *FromDir,
263 const DirectoryLookup *&CurDir,
264 const FileEntry *CurFileEnt,
265 llvm::SmallVectorImpl<char> *SearchPath,
266 llvm::SmallVectorImpl<char> *RelativePath) {
267 // If 'Filename' is absolute, check to see if it exists and no searching.
268 if (llvm::sys::path::is_absolute(Filename)) {
271 // If this was an #include_next "/absolute/file", fail.
272 if (FromDir) return 0;
274 if (SearchPath != NULL)
276 if (RelativePath != NULL) {
277 RelativePath->clear();
278 RelativePath->append(Filename.begin(), Filename.end());
280 // Otherwise, just return the file.
281 return FileMgr.getFile(Filename, /*openFile=*/true);
284 // Step #0, unless disabled, check to see if the file is in the #includer's
285 // 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
287 // a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
288 // This search is not done for <> headers.
289 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());
296 if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(),/*openFile=*/true)) {
297 // Leave CurDir unset.
298 // This file is a system header or C++ unfriendly if the old file is.
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
303 unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
304 getFileInfo(FE).DirInfo = DirInfo;
305 if (SearchPath != NULL) {
306 llvm::StringRef SearchPathRef(CurFileEnt->getDir()->getName());
308 SearchPath->append(SearchPathRef.begin(), SearchPathRef.end());
310 if (RelativePath != NULL) {
311 RelativePath->clear();
312 RelativePath->append(Filename.begin(), Filename.end());
320 // If this is a system #include, ignore the user #include locs.
321 unsigned i = isAngled ? AngledDirIdx : 0;
323 // If this is a #include_next request, start searching after the directory the
324 // file was found in.
326 i = FromDir-&SearchDirs[0];
328 // 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 =
333 LookupFileCache.GetOrCreateValue(Filename).getValue();
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;
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;
348 // Check each directory in sequence to see if it contains this file.
349 for (; i != SearchDirs.size(); ++i) {
350 const FileEntry *FE =
351 SearchDirs[i].LookupFile(Filename, *this, SearchPath, RelativePath);
354 CurDir = &SearchDirs[i];
356 // This file is a system header or C++ unfriendly if the dir is.
357 getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
359 // Remember this location for the next lookup we do.
360 CacheLookup.second = i;
364 // Otherwise, didn't find it. Remember we didn't find this.
365 CacheLookup.second = SearchDirs.size();
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.
374 const FileEntry *HeaderSearch::
375 LookupSubframeworkHeader(llvm::StringRef Filename,
376 const FileEntry *ContextFileEnt,
377 llvm::SmallVectorImpl<char> *SearchPath,
378 llvm::SmallVectorImpl<char> *RelativePath) {
379 assert(ContextFileEnt && "No context file?");
381 // Framework names must have a '/' in the filename. Find it.
382 size_t SlashPos = Filename.find('/');
383 if (SlashPos == llvm::StringRef::npos) return 0;
385 // Look up the base framework name of the ContextFileEnt.
386 const char *ContextName = ContextFileEnt->getName();
388 // If the context info wasn't a framework, couldn't be a subframework.
389 const char *FrameworkPos = strstr(ContextName, ".framework/");
390 if (FrameworkPos == 0)
393 llvm::SmallString<1024> FrameworkName(ContextName,
394 FrameworkPos+strlen(".framework/"));
396 // Append Frameworks/HIToolbox.framework/
397 FrameworkName += "Frameworks/";
398 FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
399 FrameworkName += ".framework/";
401 llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
402 FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
404 // Some other location?
405 if (CacheLookup.getValue() &&
406 CacheLookup.getKeyLength() == FrameworkName.size() &&
407 memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
408 CacheLookup.getKeyLength()) != 0)
411 // Cache subframework.
412 if (CacheLookup.getValue() == 0) {
413 ++NumSubFrameworkLookups;
415 // If the framework dir doesn't exist, we fail.
416 const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
417 if (Dir == 0) return 0;
419 // Otherwise, if it does, remember that this is the right direntry for this
421 CacheLookup.setValue(Dir);
424 const FileEntry *FE = 0;
426 if (RelativePath != NULL) {
427 RelativePath->clear();
428 RelativePath->append(Filename.begin()+SlashPos+1, Filename.end());
431 // Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
432 llvm::SmallString<1024> HeadersFilename(FrameworkName);
433 HeadersFilename += "Headers/";
434 if (SearchPath != NULL) {
436 // Without trailing '/'.
437 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
440 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
441 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true))) {
443 // Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
444 HeadersFilename = FrameworkName;
445 HeadersFilename += "PrivateHeaders/";
446 if (SearchPath != NULL) {
448 // Without trailing '/'.
449 SearchPath->append(HeadersFilename.begin(), HeadersFilename.end()-1);
452 HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
453 if (!(FE = FileMgr.getFile(HeadersFilename.str(), /*openFile=*/true)))
457 // This file is a system header or C++ unfriendly if the old file is.
459 // 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
462 unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
463 getFileInfo(FE).DirInfo = DirInfo;
467 //===----------------------------------------------------------------------===//
468 // File Info Management.
469 //===----------------------------------------------------------------------===//
472 /// getFileInfo - Return the HeaderFileInfo structure for the specified
474 HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
475 if (FE->getUID() >= FileInfo.size())
476 FileInfo.resize(FE->getUID()+1);
478 HeaderFileInfo &HFI = FileInfo[FE->getUID()];
479 if (ExternalSource && !HFI.Resolved) {
480 HFI = ExternalSource->GetHeaderFileInfo(FE);
486 bool HeaderSearch::isFileMultipleIncludeGuarded(const FileEntry *File) {
487 // Check if we've ever seen this file as a header.
488 if (File->getUID() >= FileInfo.size())
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);
498 return HFI.isPragmaOnce || HFI.ControllingMacro || HFI.ControllingMacroID;
501 void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
502 if (UID >= FileInfo.size())
503 FileInfo.resize(UID+1);
508 /// 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.
511 bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
512 ++NumIncluded; // Count # of attempted #includes.
514 // Get information about this file.
515 HeaderFileInfo &FileInfo = getFileInfo(File);
517 // If this is a #import directive, check that we have not already imported
520 // If this has already been imported, don't import it again.
521 FileInfo.isImport = true;
523 // Has this already been #import'ed or #include'd?
524 if (FileInfo.NumIncludes) return false;
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)
532 // 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.
534 if (const IdentifierInfo *ControllingMacro
535 = FileInfo.getControllingMacro(ExternalLookup))
536 if (ControllingMacro->hasMacroDefinition()) {
537 ++NumMultiIncludeFileOptzn;
541 // Increment the number of times this file has been included.
542 ++FileInfo.NumIncludes;