2007-12-17 16:22:46 +08:00
|
|
|
//===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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"
|
2016-02-23 08:48:16 +08:00
|
|
|
#include "llvm/Support/Compiler.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"
|
2016-02-21 07:09:14 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2016-02-21 08:14:36 +08:00
|
|
|
#include <cstring>
|
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.
|
2018-08-21 03:15:02 +08:00
|
|
|
std::unique_ptr<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;
|
2018-08-21 03:15:02 +08:00
|
|
|
return std::unique_ptr<HeaderMap>(new HeaderMap(std::move(*FileBuffer), NeedsByteSwap));
|
2016-02-21 04:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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:24:31 +08:00
|
|
|
// Check the number of buckets. It should be a power of two, and there
|
|
|
|
// should be enough space in the file for all of them.
|
2016-02-23 06:24:22 +08:00
|
|
|
uint32_t NumBuckets = NeedsByteSwap
|
|
|
|
? llvm::sys::getSwappedBytes(Header->NumBuckets)
|
|
|
|
: Header->NumBuckets;
|
|
|
|
if (!llvm::isPowerOf2_32(NumBuckets))
|
2016-02-21 05:00:58 +08:00
|
|
|
return false;
|
2016-02-21 05:24:31 +08:00
|
|
|
if (File.getBufferSize() <
|
|
|
|
sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
|
|
|
|
return false;
|
2016-02-21 05:00:58 +08:00
|
|
|
|
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-10-02 00:38:28 +08:00
|
|
|
StringRef 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 {
|
2016-02-21 05:24:31 +08:00
|
|
|
assert(FileBuffer->getBufferSize() >=
|
|
|
|
sizeof(HMapHeader) + sizeof(HMapBucket) * BucketNo &&
|
|
|
|
"Expected bucket to be in range");
|
|
|
|
|
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));
|
|
|
|
const HMapBucket *BucketPtr = BucketArray+BucketNo;
|
|
|
|
|
2016-02-21 05:24:31 +08:00
|
|
|
// Load the values, bswapping as needed.
|
2007-12-18 05:06:11 +08:00
|
|
|
Result.Key = getEndianAdjustedWord(BucketPtr->Key);
|
|
|
|
Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
|
|
|
|
Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2016-02-23 08:48:16 +08:00
|
|
|
Optional<StringRef> 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())
|
2016-02-23 08:48:16 +08:00
|
|
|
return None;
|
2016-02-21 08:14:36 +08:00
|
|
|
|
|
|
|
const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
|
|
|
|
unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
|
|
|
|
unsigned Len = strnlen(Data, MaxLen);
|
|
|
|
|
|
|
|
// Check whether the buffer is null-terminated.
|
|
|
|
if (Len == MaxLen && Data[Len - 1])
|
2016-02-23 08:48:16 +08:00
|
|
|
return None;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-02-21 08:14:36 +08:00
|
|
|
return StringRef(Data, Len);
|
2007-12-18 05:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// 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
|
|
|
|
2016-02-21 07:09:14 +08:00
|
|
|
llvm::dbgs() << "Header Map " << getFileName() << ":\n " << NumBuckets
|
|
|
|
<< ", " << getEndianAdjustedWord(Hdr.NumEntries) << "\n";
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2016-02-23 08:48:16 +08:00
|
|
|
auto getStringOrInvalid = [this](unsigned Id) -> StringRef {
|
|
|
|
if (Optional<StringRef> S = getString(Id))
|
|
|
|
return *S;
|
|
|
|
return "<invalid>";
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2016-02-23 08:48:16 +08:00
|
|
|
StringRef Key = getStringOrInvalid(B.Key);
|
|
|
|
StringRef Prefix = getStringOrInvalid(B.Prefix);
|
|
|
|
StringRef Suffix = getStringOrInvalid(B.Suffix);
|
2016-02-21 07:09:14 +08:00
|
|
|
llvm::dbgs() << " " << i << ". " << Key << " -> '" << Prefix << "' '"
|
|
|
|
<< Suffix << "'\n";
|
2007-12-18 05:06:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
Optional<FileEntryRef> HeaderMap::LookupFile(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())
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
return None;
|
2014-02-14 22:58:28 +08:00
|
|
|
|
2019-08-27 02:29:51 +08:00
|
|
|
return FM.getOptionalFileRef(Dest);
|
2014-02-14 22:58:28 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2016-02-23 06:24:22 +08:00
|
|
|
assert(llvm::isPowerOf2_32(NumBuckets) && "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.
|
2016-02-23 08:48:16 +08:00
|
|
|
Optional<StringRef> Key = getString(B.Key);
|
|
|
|
if (LLVM_UNLIKELY(!Key))
|
|
|
|
continue;
|
|
|
|
if (!Filename.equals_lower(*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.
|
2016-02-23 08:48:16 +08:00
|
|
|
Optional<StringRef> Prefix = getString(B.Prefix);
|
|
|
|
Optional<StringRef> Suffix = getString(B.Suffix);
|
|
|
|
|
2014-02-14 22:58:28 +08:00
|
|
|
DestPath.clear();
|
2016-02-23 08:48:16 +08:00
|
|
|
if (LLVM_LIKELY(Prefix && Suffix)) {
|
|
|
|
DestPath.append(Prefix->begin(), Prefix->end());
|
|
|
|
DestPath.append(Suffix->begin(), Suffix->end());
|
|
|
|
}
|
2014-02-14 22:58:28 +08:00
|
|
|
return StringRef(DestPath.begin(), DestPath.size());
|
2007-12-18 05:38:04 +08:00
|
|
|
}
|
2007-12-17 16:22:46 +08:00
|
|
|
}
|