2007-12-17 16:22:46 +08:00
|
|
|
//===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
|
|
|
|
//
|
|
|
|
// 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.
|
2007-12-17 16:22:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the HeaderMap interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/HeaderMap.h"
|
2016-02-21 04:39:51 +08:00
|
|
|
#include "clang/Lex/HeaderMapTypes.h"
|
2013-02-09 18:09:43 +08:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2007-12-18 02:34:53 +08:00
|
|
|
#include "clang/Basic/FileManager.h"
|
2007-12-18 05:38:04 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-11-30 02:12:39 +08:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2007-12-18 02:34:53 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-02-21 05:00:58 +08:00
|
|
|
#include "llvm/Support/SwapByteOrder.h"
|
2009-03-03 06:20:04 +08:00
|
|
|
#include <cstdio>
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2007-12-17 16:22:46 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-12-18 05:38:04 +08:00
|
|
|
/// HashHMapKey - This is the 'well known' hash function required by the file
|
|
|
|
/// format, used to look up keys in the hash table. The hash table uses simple
|
|
|
|
/// linear probing based on this function.
|
2011-07-23 18:55:15 +08:00
|
|
|
static inline unsigned HashHMapKey(StringRef Str) {
|
2007-12-18 05:38:04 +08:00
|
|
|
unsigned Result = 0;
|
2010-01-10 09:35:12 +08:00
|
|
|
const char *S = Str.begin(), *End = Str.end();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:38:04 +08:00
|
|
|
for (; S != End; S++)
|
2013-02-09 18:09:43 +08:00
|
|
|
Result += toLowercase(*S) * 13;
|
2007-12-18 05:38:04 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Verification and Construction
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-12-18 02:34:53 +08:00
|
|
|
|
|
|
|
/// HeaderMap::Create - This attempts to load the specified file as a header
|
|
|
|
/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
|
|
|
|
/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
|
|
|
|
/// into the string error argument and returns null.
|
2010-11-23 16:35:12 +08:00
|
|
|
const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
|
2007-12-18 02:34:53 +08:00
|
|
|
// If the file is too small to be a header map, ignore it.
|
|
|
|
unsigned FileSize = FE->getSize();
|
2014-05-18 07:10:59 +08:00
|
|
|
if (FileSize <= sizeof(HMapHeader)) return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-10-27 06:44:13 +08:00
|
|
|
auto FileBuffer = FM.getBufferForFile(FE);
|
2016-02-21 04:39:51 +08:00
|
|
|
if (!FileBuffer || !*FileBuffer)
|
|
|
|
return nullptr;
|
|
|
|
bool NeedsByteSwap;
|
|
|
|
if (!checkHeader(**FileBuffer, NeedsByteSwap))
|
|
|
|
return nullptr;
|
|
|
|
return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
|
|
|
|
bool &NeedsByteSwap) {
|
|
|
|
if (File.getBufferSize() <= sizeof(HMapHeader))
|
|
|
|
return false;
|
|
|
|
const char *FileStart = File.getBufferStart();
|
2007-12-18 02:34:53 +08:00
|
|
|
|
|
|
|
// We know the file is at least as big as the header, check it now.
|
|
|
|
const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 02:59:44 +08:00
|
|
|
// Sniff it to see if it's a headermap by checking the magic number and
|
|
|
|
// version.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (Header->Magic == HMAP_HeaderMagicNumber &&
|
2007-12-18 05:06:11 +08:00
|
|
|
Header->Version == HMAP_HeaderVersion)
|
2007-12-18 02:34:53 +08:00
|
|
|
NeedsByteSwap = false;
|
2007-12-18 05:06:11 +08:00
|
|
|
else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
|
|
|
|
Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
|
2007-12-18 02:34:53 +08:00
|
|
|
NeedsByteSwap = true; // Mixed endianness headermap.
|
2009-09-09 23:08:12 +08:00
|
|
|
else
|
2016-02-21 04:39:51 +08:00
|
|
|
return false; // Not a header map.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-02-21 04:39:51 +08:00
|
|
|
if (Header->Reserved != 0)
|
|
|
|
return false;
|
2007-12-18 02:44:09 +08:00
|
|
|
|
2016-02-21 05:00:58 +08:00
|
|
|
// Check the number of buckets.
|
|
|
|
auto NumBuckets = NeedsByteSwap
|
|
|
|
? llvm::sys::getSwappedBytes(Header->NumBuckets)
|
|
|
|
: Header->NumBuckets;
|
|
|
|
|
|
|
|
// If the number of buckets is not a power of two, the headermap is corrupt.
|
|
|
|
if (NumBuckets & (NumBuckets - 1))
|
|
|
|
return false;
|
|
|
|
|
2016-02-21 04:39:51 +08:00
|
|
|
// Okay, everything looks good.
|
|
|
|
return true;
|
2007-12-18 02:44:09 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Utility Methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-12-18 02:44:09 +08:00
|
|
|
|
|
|
|
/// getFileName - Return the filename of the headermap.
|
2016-02-21 04:39:51 +08:00
|
|
|
const char *HeaderMapImpl::getFileName() const {
|
2007-12-18 02:44:09 +08:00
|
|
|
return FileBuffer->getBufferIdentifier();
|
2007-12-17 16:22:46 +08:00
|
|
|
}
|
|
|
|
|
2016-02-21 04:39:51 +08:00
|
|
|
unsigned HeaderMapImpl::getEndianAdjustedWord(unsigned X) const {
|
2007-12-18 05:06:11 +08:00
|
|
|
if (!NeedsBSwap) return X;
|
|
|
|
return llvm::ByteSwap_32(X);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getHeader - Return a reference to the file header, in unbyte-swapped form.
|
|
|
|
/// This method cannot fail.
|
2016-02-21 04:39:51 +08:00
|
|
|
const HMapHeader &HeaderMapImpl::getHeader() const {
|
2007-12-18 05:06:11 +08:00
|
|
|
// We know the file is at least as big as the header. Return it.
|
|
|
|
return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getBucket - Return the specified hash table bucket from the header map,
|
|
|
|
/// bswap'ing its fields as appropriate. If the bucket number is not valid,
|
|
|
|
/// this return a bucket with an empty key (0).
|
2016-02-21 04:39:51 +08:00
|
|
|
HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
|
2007-12-18 05:06:11 +08:00
|
|
|
HMapBucket Result;
|
|
|
|
Result.Key = HMAP_EmptyBucketKey;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
const HMapBucket *BucketArray =
|
2007-12-18 05:06:11 +08:00
|
|
|
reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
|
|
|
|
sizeof(HMapHeader));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
const HMapBucket *BucketPtr = BucketArray+BucketNo;
|
2012-09-06 23:59:27 +08:00
|
|
|
if ((const char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
|
2008-10-28 08:18:42 +08:00
|
|
|
Result.Prefix = 0;
|
|
|
|
Result.Suffix = 0;
|
2007-12-18 05:06:11 +08:00
|
|
|
return Result; // Invalid buffer, corrupt hmap.
|
2008-10-28 08:18:42 +08:00
|
|
|
}
|
2007-12-18 05:06:11 +08:00
|
|
|
|
|
|
|
// Otherwise, the bucket is valid. Load the values, bswapping as needed.
|
|
|
|
Result.Key = getEndianAdjustedWord(BucketPtr->Key);
|
|
|
|
Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
|
|
|
|
Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getString - Look up the specified string in the string table. If the string
|
|
|
|
/// index is not valid, it returns an empty string.
|
2016-02-21 04:39:51 +08:00
|
|
|
const char *HeaderMapImpl::getString(unsigned StrTabIdx) const {
|
2007-12-18 05:06:11 +08:00
|
|
|
// Add the start of the string table to the idx.
|
|
|
|
StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
// Check for invalid index.
|
|
|
|
if (StrTabIdx >= FileBuffer->getBufferSize())
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
// Otherwise, we have a valid pointer into the file. Just return it. We know
|
|
|
|
// that the "string" can not overrun the end of the file, because the buffer
|
|
|
|
// is nul terminated by virtue of being a MemoryBuffer.
|
|
|
|
return FileBuffer->getBufferStart()+StrTabIdx;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// The Main Drivers
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// dump - Print the contents of this headermap to stderr.
|
2016-02-21 04:39:51 +08:00
|
|
|
LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
|
2007-12-18 05:06:11 +08:00
|
|
|
const HMapHeader &Hdr = getHeader();
|
|
|
|
unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
|
2007-12-18 05:06:11 +08:00
|
|
|
getFileName(), NumBuckets,
|
|
|
|
getEndianAdjustedWord(Hdr.NumEntries));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
for (unsigned i = 0; i != NumBuckets; ++i) {
|
|
|
|
HMapBucket B = getBucket(i);
|
|
|
|
if (B.Key == HMAP_EmptyBucketKey) continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:06:11 +08:00
|
|
|
const char *Key = getString(B.Key);
|
|
|
|
const char *Prefix = getString(B.Prefix);
|
|
|
|
const char *Suffix = getString(B.Suffix);
|
|
|
|
fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-17 16:22:46 +08:00
|
|
|
/// LookupFile - Check to see if the specified relative filename is located in
|
|
|
|
/// this HeaderMap. If so, open it and return its FileEntry.
|
2011-03-17 02:34:36 +08:00
|
|
|
const FileEntry *HeaderMap::LookupFile(
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Filename, FileManager &FM) const {
|
2014-02-14 22:58:28 +08:00
|
|
|
|
|
|
|
SmallString<1024> Path;
|
2016-02-21 04:39:51 +08:00
|
|
|
StringRef Dest = HeaderMapImpl::lookupFilename(Filename, Path);
|
2014-02-14 22:58:28 +08:00
|
|
|
if (Dest.empty())
|
2014-05-18 07:10:59 +08:00
|
|
|
return nullptr;
|
2014-02-14 22:58:28 +08:00
|
|
|
|
|
|
|
return FM.getFile(Dest);
|
|
|
|
}
|
|
|
|
|
2016-02-21 04:39:51 +08:00
|
|
|
StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
|
|
|
|
SmallVectorImpl<char> &DestPath) const {
|
2007-12-18 05:38:04 +08:00
|
|
|
const HMapHeader &Hdr = getHeader();
|
|
|
|
unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
|
|
|
|
|
2016-02-21 05:00:58 +08:00
|
|
|
// Don't probe infinitely. This should be checked before constructing.
|
|
|
|
assert(!(NumBuckets & (NumBuckets - 1)) && "Expected power of 2");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:38:04 +08:00
|
|
|
// Linearly probe the hash table.
|
2010-01-10 09:35:12 +08:00
|
|
|
for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
|
2007-12-18 05:38:04 +08:00
|
|
|
HMapBucket B = getBucket(Bucket & (NumBuckets-1));
|
2014-02-14 22:58:28 +08:00
|
|
|
if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:38:04 +08:00
|
|
|
// See if the key matches. If not, probe on.
|
2010-01-10 17:51:00 +08:00
|
|
|
if (!Filename.equals_lower(getString(B.Key)))
|
2007-12-18 05:38:04 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-18 05:38:04 +08:00
|
|
|
// If so, we have a match in the hash table. Construct the destination
|
|
|
|
// path.
|
2014-02-14 22:58:28 +08:00
|
|
|
StringRef Prefix = getString(B.Prefix);
|
|
|
|
StringRef Suffix = getString(B.Suffix);
|
|
|
|
DestPath.clear();
|
|
|
|
DestPath.append(Prefix.begin(), Prefix.end());
|
|
|
|
DestPath.append(Suffix.begin(), Suffix.end());
|
|
|
|
return StringRef(DestPath.begin(), DestPath.size());
|
2007-12-18 05:38:04 +08:00
|
|
|
}
|
2007-12-17 16:22:46 +08:00
|
|
|
}
|