2006-10-18 13:34:33 +08:00
|
|
|
//===--- HeaderSearch.cpp - Resolve Header File Locations ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-10-18 13:34:33 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the DirectoryLookup and HeaderSearch interfaces.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2007-12-17 14:36:45 +08:00
|
|
|
#include "clang/Lex/HeaderMap.h"
|
2007-10-07 16:58:51 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
2006-10-18 13:34:33 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2006-10-30 11:40:58 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2006-10-18 13:34:33 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2009-04-26 07:30:02 +08:00
|
|
|
const IdentifierInfo *
|
|
|
|
HeaderFileInfo::getControllingMacro(ExternalIdentifierLookup *External) {
|
|
|
|
if (ControllingMacro)
|
|
|
|
return ControllingMacro;
|
|
|
|
|
|
|
|
if (!ControllingMacroID || !External)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ControllingMacro = External->GetIdentifier(ControllingMacroID);
|
|
|
|
return ControllingMacro;
|
|
|
|
}
|
|
|
|
|
2010-11-23 16:35:12 +08:00
|
|
|
HeaderSearch::HeaderSearch(FileManager &FM)
|
|
|
|
: FileMgr(FM), FrameworkMap(64) {
|
2006-10-20 14:23:14 +08:00
|
|
|
SystemDirIdx = 0;
|
|
|
|
NoCurDirSearch = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-26 07:30:02 +08:00
|
|
|
ExternalLookup = 0;
|
2006-10-20 14:23:14 +08:00
|
|
|
NumIncluded = 0;
|
|
|
|
NumMultiIncludeFileOptzn = 0;
|
|
|
|
NumFrameworkLookups = NumSubFrameworkLookups = 0;
|
|
|
|
}
|
|
|
|
|
2007-12-17 14:36:45 +08:00
|
|
|
HeaderSearch::~HeaderSearch() {
|
|
|
|
// Delete headermaps.
|
|
|
|
for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
|
|
|
|
delete HeaderMaps[i].second;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
void HeaderSearch::PrintStats() {
|
2007-06-16 07:05:46 +08:00
|
|
|
fprintf(stderr, "\n*** HeaderSearch Stats:\n");
|
|
|
|
fprintf(stderr, "%d files tracked.\n", (int)FileInfo.size());
|
2006-10-18 13:34:33 +08:00
|
|
|
unsigned NumOnceOnlyFiles = 0, MaxNumIncludes = 0, NumSingleIncludedFiles = 0;
|
|
|
|
for (unsigned i = 0, e = FileInfo.size(); i != e; ++i) {
|
|
|
|
NumOnceOnlyFiles += FileInfo[i].isImport;
|
|
|
|
if (MaxNumIncludes < FileInfo[i].NumIncludes)
|
|
|
|
MaxNumIncludes = FileInfo[i].NumIncludes;
|
|
|
|
NumSingleIncludedFiles += FileInfo[i].NumIncludes == 1;
|
|
|
|
}
|
2007-06-16 07:05:46 +08:00
|
|
|
fprintf(stderr, " %d #import/#pragma once files.\n", NumOnceOnlyFiles);
|
|
|
|
fprintf(stderr, " %d included exactly once.\n", NumSingleIncludedFiles);
|
|
|
|
fprintf(stderr, " %d max times a file is included.\n", MaxNumIncludes);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
fprintf(stderr, " %d #include/#include_next/#import.\n", NumIncluded);
|
|
|
|
fprintf(stderr, " %d #includes skipped due to"
|
|
|
|
" the multi-include optimization.\n", NumMultiIncludeFileOptzn);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
fprintf(stderr, "%d framework lookups.\n", NumFrameworkLookups);
|
|
|
|
fprintf(stderr, "%d subframework lookups.\n", NumSubFrameworkLookups);
|
2006-10-18 13:34:33 +08:00
|
|
|
}
|
|
|
|
|
2007-12-17 14:36:45 +08:00
|
|
|
/// CreateHeaderMap - This method returns a HeaderMap for the specified
|
|
|
|
/// FileEntry, uniquing them through the the 'HeaderMaps' datastructure.
|
2007-12-18 02:34:53 +08:00
|
|
|
const HeaderMap *HeaderSearch::CreateHeaderMap(const FileEntry *FE) {
|
2007-12-17 14:36:45 +08:00
|
|
|
// We expect the number of headermaps to be small, and almost always empty.
|
2007-12-17 15:52:39 +08:00
|
|
|
// If it ever grows, use of a linear search should be re-evaluated.
|
2007-12-17 14:36:45 +08:00
|
|
|
if (!HeaderMaps.empty()) {
|
|
|
|
for (unsigned i = 0, e = HeaderMaps.size(); i != e; ++i)
|
2007-12-17 15:52:39 +08:00
|
|
|
// Pointer equality comparison of FileEntries works because they are
|
|
|
|
// already uniqued by inode.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (HeaderMaps[i].first == FE)
|
2007-12-17 14:36:45 +08:00
|
|
|
return HeaderMaps[i].second;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-11-23 16:35:12 +08:00
|
|
|
if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr)) {
|
2007-12-17 14:36:45 +08:00
|
|
|
HeaderMaps.push_back(std::make_pair(FE, HM));
|
|
|
|
return HM;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 14:36:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-17 15:52:39 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// File lookup within a DirectoryLookup scope
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-18 01:57:27 +08:00
|
|
|
/// getName - Return the directory or filename corresponding to this lookup
|
|
|
|
/// object.
|
|
|
|
const char *DirectoryLookup::getName() const {
|
|
|
|
if (isNormalDir())
|
|
|
|
return getDir()->getName();
|
|
|
|
if (isFramework())
|
|
|
|
return getFrameworkDir()->getName();
|
|
|
|
assert(isHeaderMap() && "Unknown DirectoryLookup");
|
|
|
|
return getHeaderMap()->getFileName();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-17 15:52:39 +08:00
|
|
|
/// LookupFile - Lookup the specified file in this search path, returning it
|
|
|
|
/// if it exists or returning null if not.
|
2010-01-10 09:35:12 +08:00
|
|
|
const FileEntry *DirectoryLookup::LookupFile(llvm::StringRef Filename,
|
2007-12-17 16:13:48 +08:00
|
|
|
HeaderSearch &HS) const {
|
2007-12-17 15:52:39 +08:00
|
|
|
llvm::SmallString<1024> TmpDir;
|
2007-12-17 16:13:48 +08:00
|
|
|
if (isNormalDir()) {
|
|
|
|
// Concatenate the requested file onto the directory.
|
|
|
|
// FIXME: Portability. Filename concatenation should be in sys::Path.
|
|
|
|
TmpDir += getDir()->getName();
|
|
|
|
TmpDir.push_back('/');
|
2010-01-10 09:35:12 +08:00
|
|
|
TmpDir.append(Filename.begin(), Filename.end());
|
2010-11-23 16:35:12 +08:00
|
|
|
return HS.getFileMgr().getFile(TmpDir.str());
|
2007-12-17 16:13:48 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
if (isFramework())
|
2010-01-10 09:35:12 +08:00
|
|
|
return DoFrameworkLookup(Filename, HS);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:17:39 +08:00
|
|
|
assert(isHeaderMap() && "Unknown directory lookup");
|
2010-11-23 16:35:12 +08:00
|
|
|
return getHeaderMap()->LookupFile(Filename, HS.getFileMgr());
|
2007-12-17 15:52:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
/// DoFrameworkLookup - Do a lookup of the specified file in the current
|
|
|
|
/// DirectoryLookup, which is a framework directory.
|
2010-01-10 09:35:12 +08:00
|
|
|
const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
|
2007-12-17 16:13:48 +08:00
|
|
|
HeaderSearch &HS) const {
|
|
|
|
FileManager &FileMgr = HS.getFileMgr();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Framework names must have a '/' in the filename.
|
2010-01-10 09:35:12 +08:00
|
|
|
size_t SlashPos = Filename.find('/');
|
|
|
|
if (SlashPos == llvm::StringRef::npos) return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// Find out if this is the home for the specified framework, by checking
|
|
|
|
// HeaderSearch. Possible answer are yes/no and unknown.
|
2009-09-09 23:08:12 +08:00
|
|
|
const DirectoryEntry *&FrameworkDirCache =
|
2010-01-10 09:35:12 +08:00
|
|
|
HS.LookupFrameworkCache(Filename.substr(0, SlashPos));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// If it is known and in some other directory, fail.
|
|
|
|
if (FrameworkDirCache && FrameworkDirCache != getFrameworkDir())
|
2006-10-22 15:24:13 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// Otherwise, construct the path to this framework dir.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// FrameworkName = "/System/Library/Frameworks/"
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallString<1024> FrameworkName;
|
2007-12-17 16:13:48 +08:00
|
|
|
FrameworkName += getFrameworkDir()->getName();
|
2006-10-30 13:09:49 +08:00
|
|
|
if (FrameworkName.empty() || FrameworkName.back() != '/')
|
|
|
|
FrameworkName.push_back('/');
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// FrameworkName = "/System/Library/Frameworks/Cocoa"
|
2010-01-10 09:35:12 +08:00
|
|
|
FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// FrameworkName = "/System/Library/Frameworks/Cocoa.framework/"
|
|
|
|
FrameworkName += ".framework/";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// If the cache entry is still unresolved, query to see if the cache entry is
|
|
|
|
// still unresolved. If so, check its existence now.
|
|
|
|
if (FrameworkDirCache == 0) {
|
|
|
|
HS.IncrementFrameworkLookupCount();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// If the framework dir doesn't exist, we fail.
|
2007-12-17 16:13:48 +08:00
|
|
|
// FIXME: It's probably more efficient to query this with FileMgr.getDir.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!llvm::sys::Path(std::string(FrameworkName.begin(),
|
2007-06-16 07:05:46 +08:00
|
|
|
FrameworkName.end())).exists())
|
2006-10-22 15:24:13 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// Otherwise, if it does, remember that this is the right direntry for this
|
|
|
|
// framework.
|
2007-12-17 16:13:48 +08:00
|
|
|
FrameworkDirCache = getFrameworkDir();
|
2006-10-22 15:24:13 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Check "/System/Library/Frameworks/Cocoa.framework/Headers/file.h"
|
2006-10-30 13:09:49 +08:00
|
|
|
unsigned OrigSize = FrameworkName.size();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-30 13:09:49 +08:00
|
|
|
FrameworkName += "Headers/";
|
2010-01-10 09:35:12 +08:00
|
|
|
FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
|
2010-11-23 16:35:12 +08:00
|
|
|
if (const FileEntry *FE = FileMgr.getFile(FrameworkName.str()))
|
2006-10-20 12:55:45 +08:00
|
|
|
return FE;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Check "/System/Library/Frameworks/Cocoa.framework/PrivateHeaders/file.h"
|
2006-10-30 13:09:49 +08:00
|
|
|
const char *Private = "Private";
|
2009-09-09 23:08:12 +08:00
|
|
|
FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
|
2006-10-30 13:09:49 +08:00
|
|
|
Private+strlen(Private));
|
2010-11-23 16:35:12 +08:00
|
|
|
return FileMgr.getFile(FrameworkName.str());
|
2006-10-18 13:34:33 +08:00
|
|
|
}
|
|
|
|
|
2007-12-17 15:52:39 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Header File Location.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
/// LookupFile - Given a "foo" or <foo> reference, look up the indicated file,
|
|
|
|
/// return null on failure. isAngled indicates whether the file reference is
|
2010-08-08 15:49:23 +08:00
|
|
|
/// for system #include's or not (i.e. using <> instead of ""). CurFileEnt, if
|
|
|
|
/// non-null, indicates where the #including file is, in case a relative search
|
|
|
|
/// is needed.
|
2010-01-10 09:35:12 +08:00
|
|
|
const FileEntry *HeaderSearch::LookupFile(llvm::StringRef Filename,
|
2006-10-18 13:34:33 +08:00
|
|
|
bool isAngled,
|
|
|
|
const DirectoryLookup *FromDir,
|
|
|
|
const DirectoryLookup *&CurDir,
|
2010-08-08 15:49:23 +08:00
|
|
|
const FileEntry *CurFileEnt) {
|
2006-10-18 13:34:33 +08:00
|
|
|
// If 'Filename' is absolute, check to see if it exists and no searching.
|
2010-01-10 09:35:12 +08:00
|
|
|
if (llvm::sys::Path::isAbsolute(Filename.begin(), Filename.size())) {
|
2006-10-18 13:34:33 +08:00
|
|
|
CurDir = 0;
|
|
|
|
|
|
|
|
// If this was an #include_next "/absolute/file", fail.
|
|
|
|
if (FromDir) return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Otherwise, just return the file.
|
2010-11-23 16:35:12 +08:00
|
|
|
return FileMgr.getFile(Filename);
|
2006-10-18 13:34:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Step #0, unless disabled, check to see if the file is in the #includer's
|
2010-08-08 15:49:23 +08:00
|
|
|
// directory. This has to be based on CurFileEnt, not CurDir, because
|
|
|
|
// CurFileEnt could be a #include of a subdirectory (#include "foo/bar.h") and
|
2007-12-17 15:52:39 +08:00
|
|
|
// a subsequent include of "baz.h" should resolve to "whatever/foo/baz.h".
|
|
|
|
// This search is not done for <> headers.
|
2010-08-08 15:49:23 +08:00
|
|
|
if (CurFileEnt && !isAngled && !NoCurDirSearch) {
|
|
|
|
llvm::SmallString<1024> TmpDir;
|
|
|
|
// Concatenate the requested file onto the directory.
|
|
|
|
// FIXME: Portability. Filename concatenation should be in sys::Path.
|
|
|
|
TmpDir += CurFileEnt->getDir()->getName();
|
|
|
|
TmpDir.push_back('/');
|
|
|
|
TmpDir.append(Filename.begin(), Filename.end());
|
2010-11-23 16:35:12 +08:00
|
|
|
if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) {
|
2006-10-18 13:34:33 +08:00
|
|
|
// Leave CurDir unset.
|
2010-08-08 15:49:23 +08:00
|
|
|
// This file is a system header or C++ unfriendly if the old file is.
|
|
|
|
//
|
|
|
|
// Note that the temporary 'DirInfo' is required here, as either call to
|
|
|
|
// getFileInfo could resize the vector and we don't want to rely on order
|
|
|
|
// of evaluation.
|
|
|
|
unsigned DirInfo = getFileInfo(CurFileEnt).DirInfo;
|
2008-02-26 05:38:21 +08:00
|
|
|
getFileInfo(FE).DirInfo = DirInfo;
|
2006-10-18 13:34:33 +08:00
|
|
|
return FE;
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
CurDir = 0;
|
|
|
|
|
|
|
|
// If this is a system #include, ignore the user #include locs.
|
|
|
|
unsigned i = isAngled ? SystemDirIdx : 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// If this is a #include_next request, start searching after the directory the
|
|
|
|
// file was found in.
|
|
|
|
if (FromDir)
|
|
|
|
i = FromDir-&SearchDirs[0];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-22 15:28:00 +08:00
|
|
|
// Cache all of the lookups performed by this method. Many headers are
|
|
|
|
// multiply included, and the "pragma once" optimization prevents them from
|
|
|
|
// being relex/pp'd, but they would still have to search through a
|
|
|
|
// (potentially huge) series of SearchDirs to find it.
|
|
|
|
std::pair<unsigned, unsigned> &CacheLookup =
|
2010-01-10 09:35:12 +08:00
|
|
|
LookupFileCache.GetOrCreateValue(Filename).getValue();
|
2007-07-22 15:28:00 +08:00
|
|
|
|
|
|
|
// If the entry has been previously looked up, the first value will be
|
|
|
|
// non-zero. If the value is equal to i (the start point of our search), then
|
|
|
|
// this is a matching hit.
|
|
|
|
if (CacheLookup.first == i+1) {
|
|
|
|
// Skip querying potentially lots of directories for this lookup.
|
|
|
|
i = CacheLookup.second;
|
|
|
|
} else {
|
|
|
|
// Otherwise, this is the first query, or the previous query didn't match
|
|
|
|
// our search start. We will fill in our found location below, so prime the
|
|
|
|
// start point value.
|
|
|
|
CacheLookup.first = i+1;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Check each directory in sequence to see if it contains this file.
|
|
|
|
for (; i != SearchDirs.size(); ++i) {
|
2009-09-09 23:08:12 +08:00
|
|
|
const FileEntry *FE =
|
2010-01-10 09:35:12 +08:00
|
|
|
SearchDirs[i].LookupFile(Filename, *this);
|
2007-12-17 16:13:48 +08:00
|
|
|
if (!FE) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
CurDir = &SearchDirs[i];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// This file is a system header or C++ unfriendly if the dir is.
|
|
|
|
getFileInfo(FE).DirInfo = CurDir->getDirCharacteristic();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-17 16:13:48 +08:00
|
|
|
// Remember this location for the next lookup we do.
|
|
|
|
CacheLookup.second = i;
|
|
|
|
return FE;
|
2006-10-18 13:34:33 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-07-22 15:28:00 +08:00
|
|
|
// Otherwise, didn't find it. Remember we didn't find this.
|
|
|
|
CacheLookup.second = SearchDirs.size();
|
2006-10-18 13:34:33 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
/// LookupSubframeworkHeader - Look up a subframework for the specified
|
|
|
|
/// #include file. For example, if #include'ing <HIToolbox/HIToolbox.h> from
|
|
|
|
/// within ".../Carbon.framework/Headers/Carbon.h", check to see if HIToolbox
|
|
|
|
/// is a subframework within Carbon.framework. If so, return the FileEntry
|
|
|
|
/// for the designated file, otherwise return null.
|
|
|
|
const FileEntry *HeaderSearch::
|
2010-01-10 09:35:12 +08:00
|
|
|
LookupSubframeworkHeader(llvm::StringRef Filename,
|
2006-10-20 12:42:40 +08:00
|
|
|
const FileEntry *ContextFileEnt) {
|
2008-02-01 13:34:02 +08:00
|
|
|
assert(ContextFileEnt && "No context file?");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// Framework names must have a '/' in the filename. Find it.
|
2010-01-10 09:35:12 +08:00
|
|
|
size_t SlashPos = Filename.find('/');
|
|
|
|
if (SlashPos == llvm::StringRef::npos) return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// Look up the base framework name of the ContextFileEnt.
|
2006-10-27 13:12:36 +08:00
|
|
|
const char *ContextName = ContextFileEnt->getName();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// If the context info wasn't a framework, couldn't be a subframework.
|
2006-10-27 13:12:36 +08:00
|
|
|
const char *FrameworkPos = strstr(ContextName, ".framework/");
|
|
|
|
if (FrameworkPos == 0)
|
2006-10-20 12:42:40 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
llvm::SmallString<1024> FrameworkName(ContextName,
|
2007-06-16 07:05:46 +08:00
|
|
|
FrameworkPos+strlen(".framework/"));
|
2006-10-22 15:24:13 +08:00
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// Append Frameworks/HIToolbox.framework/
|
|
|
|
FrameworkName += "Frameworks/";
|
2010-01-10 09:35:12 +08:00
|
|
|
FrameworkName.append(Filename.begin(), Filename.begin()+SlashPos);
|
2006-10-20 12:42:40 +08:00
|
|
|
FrameworkName += ".framework/";
|
2006-10-20 12:55:45 +08:00
|
|
|
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::StringMapEntry<const DirectoryEntry *> &CacheLookup =
|
2010-11-21 17:55:08 +08:00
|
|
|
FrameworkMap.GetOrCreateValue(Filename.substr(0, SlashPos));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// Some other location?
|
2007-02-09 03:08:49 +08:00
|
|
|
if (CacheLookup.getValue() &&
|
|
|
|
CacheLookup.getKeyLength() == FrameworkName.size() &&
|
|
|
|
memcmp(CacheLookup.getKeyData(), &FrameworkName[0],
|
|
|
|
CacheLookup.getKeyLength()) != 0)
|
2006-10-22 15:24:13 +08:00
|
|
|
return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// Cache subframework.
|
2007-02-09 03:08:49 +08:00
|
|
|
if (CacheLookup.getValue() == 0) {
|
2006-10-22 15:24:13 +08:00
|
|
|
++NumSubFrameworkLookups;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// If the framework dir doesn't exist, we fail.
|
2010-11-23 16:35:12 +08:00
|
|
|
const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.str());
|
2006-10-22 15:24:13 +08:00
|
|
|
if (Dir == 0) return 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-22 15:24:13 +08:00
|
|
|
// Otherwise, if it does, remember that this is the right direntry for this
|
|
|
|
// framework.
|
2007-02-09 03:08:49 +08:00
|
|
|
CacheLookup.setValue(Dir);
|
2006-10-22 15:24:13 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:55:45 +08:00
|
|
|
const FileEntry *FE = 0;
|
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// Check ".../Frameworks/HIToolbox.framework/Headers/HIToolbox.h"
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallString<1024> HeadersFilename(FrameworkName);
|
2006-10-30 11:40:58 +08:00
|
|
|
HeadersFilename += "Headers/";
|
2010-01-10 09:35:12 +08:00
|
|
|
HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
|
2010-11-23 16:35:12 +08:00
|
|
|
if (!(FE = FileMgr.getFile(HeadersFilename.str()))) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:42:40 +08:00
|
|
|
// Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
|
2006-10-30 11:40:58 +08:00
|
|
|
HeadersFilename = FrameworkName;
|
|
|
|
HeadersFilename += "PrivateHeaders/";
|
2010-01-10 09:35:12 +08:00
|
|
|
HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
|
2010-11-23 16:35:12 +08:00
|
|
|
if (!(FE = FileMgr.getFile(HeadersFilename.str())))
|
2006-10-20 12:42:40 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-20 12:55:45 +08:00
|
|
|
// This file is a system header or C++ unfriendly if the old file is.
|
2008-02-24 11:55:14 +08:00
|
|
|
//
|
2008-02-26 05:38:21 +08:00
|
|
|
// Note that the temporary 'DirInfo' is required here, as either call to
|
|
|
|
// getFileInfo could resize the vector and we don't want to rely on order
|
|
|
|
// of evaluation.
|
|
|
|
unsigned DirInfo = getFileInfo(ContextFileEnt).DirInfo;
|
|
|
|
getFileInfo(FE).DirInfo = DirInfo;
|
2006-10-20 12:55:45 +08:00
|
|
|
return FE;
|
2006-10-20 12:42:40 +08:00
|
|
|
}
|
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// File Info Management.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2009-04-25 04:03:17 +08:00
|
|
|
/// getFileInfo - Return the HeaderFileInfo structure for the specified
|
2006-10-18 13:34:33 +08:00
|
|
|
/// FileEntry.
|
2009-04-25 04:03:17 +08:00
|
|
|
HeaderFileInfo &HeaderSearch::getFileInfo(const FileEntry *FE) {
|
2006-10-18 13:34:33 +08:00
|
|
|
if (FE->getUID() >= FileInfo.size())
|
|
|
|
FileInfo.resize(FE->getUID()+1);
|
|
|
|
return FileInfo[FE->getUID()];
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2006-10-18 13:34:33 +08:00
|
|
|
|
2009-04-25 04:03:17 +08:00
|
|
|
void HeaderSearch::setHeaderFileInfoForUID(HeaderFileInfo HFI, unsigned UID) {
|
|
|
|
if (UID >= FileInfo.size())
|
|
|
|
FileInfo.resize(UID+1);
|
|
|
|
FileInfo[UID] = HFI;
|
|
|
|
}
|
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
/// ShouldEnterIncludeFile - Mark the specified file as a target of of a
|
|
|
|
/// #include, #include_next, or #import directive. Return false if #including
|
|
|
|
/// the file will have no effect or true if we should include it.
|
|
|
|
bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){
|
|
|
|
++NumIncluded; // Count # of attempted #includes.
|
|
|
|
|
|
|
|
// Get information about this file.
|
2009-04-25 04:03:17 +08:00
|
|
|
HeaderFileInfo &FileInfo = getFileInfo(File);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// If this is a #import directive, check that we have not already imported
|
|
|
|
// this header.
|
|
|
|
if (isImport) {
|
|
|
|
// If this has already been imported, don't import it again.
|
|
|
|
FileInfo.isImport = true;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Has this already been #import'ed or #include'd?
|
|
|
|
if (FileInfo.NumIncludes) return false;
|
|
|
|
} else {
|
|
|
|
// Otherwise, if this is a #include of a file that was previously #import'd
|
|
|
|
// or if this is the second #include of a #pragma once file, ignore it.
|
|
|
|
if (FileInfo.isImport)
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Next, check to see if the file is wrapped with #ifndef guards. If so, and
|
|
|
|
// if the macro that guards it is defined, we know the #include has no effect.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (const IdentifierInfo *ControllingMacro
|
2009-04-26 07:30:02 +08:00
|
|
|
= FileInfo.getControllingMacro(ExternalLookup))
|
|
|
|
if (ControllingMacro->hasMacroDefinition()) {
|
|
|
|
++NumMultiIncludeFileOptzn;
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
// Increment the number of times this file has been included.
|
|
|
|
++FileInfo.NumIncludes;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-10-18 13:34:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|