HeaderMap.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the HeaderMap interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Lex/HeaderMap.h"
  13. #include "clang/Lex/HeaderMapTypes.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/DataTypes.h"
  19. #include "llvm/Support/MathExtras.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/SwapByteOrder.h"
  22. #include "llvm/Support/Debug.h"
  23. #include <cstring>
  24. #include <memory>
  25. using namespace clang;
  26. /// HashHMapKey - This is the 'well known' hash function required by the file
  27. /// format, used to look up keys in the hash table. The hash table uses simple
  28. /// linear probing based on this function.
  29. static inline unsigned HashHMapKey(StringRef Str) {
  30. unsigned Result = 0;
  31. const char *S = Str.begin(), *End = Str.end();
  32. for (; S != End; S++)
  33. Result += toLowercase(*S) * 13;
  34. return Result;
  35. }
  36. //===----------------------------------------------------------------------===//
  37. // Verification and Construction
  38. //===----------------------------------------------------------------------===//
  39. /// HeaderMap::Create - This attempts to load the specified file as a header
  40. /// map. If it doesn't look like a HeaderMap, it gives up and returns null.
  41. /// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
  42. /// into the string error argument and returns null.
  43. std::unique_ptr<HeaderMap> HeaderMap::Create(const FileEntry *FE,
  44. FileManager &FM) {
  45. // If the file is too small to be a header map, ignore it.
  46. unsigned FileSize = FE->getSize();
  47. if (FileSize <= sizeof(HMapHeader)) return nullptr;
  48. auto FileBuffer = FM.getBufferForFile(FE);
  49. if (!FileBuffer || !*FileBuffer)
  50. return nullptr;
  51. bool NeedsByteSwap;
  52. if (!checkHeader(**FileBuffer, NeedsByteSwap))
  53. return nullptr;
  54. return std::unique_ptr<HeaderMap>(new HeaderMap(std::move(*FileBuffer), NeedsByteSwap));
  55. }
  56. bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
  57. bool &NeedsByteSwap) {
  58. if (File.getBufferSize() <= sizeof(HMapHeader))
  59. return false;
  60. const char *FileStart = File.getBufferStart();
  61. // We know the file is at least as big as the header, check it now.
  62. const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
  63. // Sniff it to see if it's a headermap by checking the magic number and
  64. // version.
  65. if (Header->Magic == HMAP_HeaderMagicNumber &&
  66. Header->Version == HMAP_HeaderVersion)
  67. NeedsByteSwap = false;
  68. else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
  69. Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
  70. NeedsByteSwap = true; // Mixed endianness headermap.
  71. else
  72. return false; // Not a header map.
  73. if (Header->Reserved != 0)
  74. return false;
  75. // Check the number of buckets. It should be a power of two, and there
  76. // should be enough space in the file for all of them.
  77. uint32_t NumBuckets = NeedsByteSwap
  78. ? llvm::sys::getSwappedBytes(Header->NumBuckets)
  79. : Header->NumBuckets;
  80. if (!llvm::isPowerOf2_32(NumBuckets))
  81. return false;
  82. if (File.getBufferSize() <
  83. sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
  84. return false;
  85. // Okay, everything looks good.
  86. return true;
  87. }
  88. //===----------------------------------------------------------------------===//
  89. // Utility Methods
  90. //===----------------------------------------------------------------------===//
  91. /// getFileName - Return the filename of the headermap.
  92. StringRef HeaderMapImpl::getFileName() const {
  93. return FileBuffer->getBufferIdentifier();
  94. }
  95. unsigned HeaderMapImpl::getEndianAdjustedWord(unsigned X) const {
  96. if (!NeedsBSwap) return X;
  97. return llvm::ByteSwap_32(X);
  98. }
  99. /// getHeader - Return a reference to the file header, in unbyte-swapped form.
  100. /// This method cannot fail.
  101. const HMapHeader &HeaderMapImpl::getHeader() const {
  102. // We know the file is at least as big as the header. Return it.
  103. return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
  104. }
  105. /// getBucket - Return the specified hash table bucket from the header map,
  106. /// bswap'ing its fields as appropriate. If the bucket number is not valid,
  107. /// this return a bucket with an empty key (0).
  108. HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
  109. assert(FileBuffer->getBufferSize() >=
  110. sizeof(HMapHeader) + sizeof(HMapBucket) * BucketNo &&
  111. "Expected bucket to be in range");
  112. HMapBucket Result;
  113. Result.Key = HMAP_EmptyBucketKey;
  114. const HMapBucket *BucketArray =
  115. reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
  116. sizeof(HMapHeader));
  117. const HMapBucket *BucketPtr = BucketArray+BucketNo;
  118. // Load the values, bswapping as needed.
  119. Result.Key = getEndianAdjustedWord(BucketPtr->Key);
  120. Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
  121. Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
  122. return Result;
  123. }
  124. Optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
  125. // Add the start of the string table to the idx.
  126. StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
  127. // Check for invalid index.
  128. if (StrTabIdx >= FileBuffer->getBufferSize())
  129. return None;
  130. const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
  131. unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
  132. unsigned Len = strnlen(Data, MaxLen);
  133. // Check whether the buffer is null-terminated.
  134. if (Len == MaxLen && Data[Len - 1])
  135. return None;
  136. return StringRef(Data, Len);
  137. }
  138. //===----------------------------------------------------------------------===//
  139. // The Main Drivers
  140. //===----------------------------------------------------------------------===//
  141. /// dump - Print the contents of this headermap to stderr.
  142. LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
  143. const HMapHeader &Hdr = getHeader();
  144. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  145. llvm::dbgs() << "Header Map " << getFileName() << ":\n " << NumBuckets
  146. << ", " << getEndianAdjustedWord(Hdr.NumEntries) << "\n";
  147. auto getStringOrInvalid = [this](unsigned Id) -> StringRef {
  148. if (Optional<StringRef> S = getString(Id))
  149. return *S;
  150. return "<invalid>";
  151. };
  152. for (unsigned i = 0; i != NumBuckets; ++i) {
  153. HMapBucket B = getBucket(i);
  154. if (B.Key == HMAP_EmptyBucketKey) continue;
  155. StringRef Key = getStringOrInvalid(B.Key);
  156. StringRef Prefix = getStringOrInvalid(B.Prefix);
  157. StringRef Suffix = getStringOrInvalid(B.Suffix);
  158. llvm::dbgs() << " " << i << ". " << Key << " -> '" << Prefix << "' '"
  159. << Suffix << "'\n";
  160. }
  161. }
  162. /// LookupFile - Check to see if the specified relative filename is located in
  163. /// this HeaderMap. If so, open it and return its FileEntry.
  164. Optional<FileEntryRef> HeaderMap::LookupFile(StringRef Filename,
  165. FileManager &FM) const {
  166. SmallString<1024> Path;
  167. StringRef Dest = HeaderMapImpl::lookupFilename(Filename, Path);
  168. if (Dest.empty())
  169. return None;
  170. if (auto File = FM.getFileRef(Dest))
  171. return *File;
  172. return None;
  173. }
  174. StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
  175. SmallVectorImpl<char> &DestPath) const {
  176. const HMapHeader &Hdr = getHeader();
  177. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  178. // Don't probe infinitely. This should be checked before constructing.
  179. assert(llvm::isPowerOf2_32(NumBuckets) && "Expected power of 2");
  180. // Linearly probe the hash table.
  181. for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
  182. HMapBucket B = getBucket(Bucket & (NumBuckets-1));
  183. if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
  184. // See if the key matches. If not, probe on.
  185. Optional<StringRef> Key = getString(B.Key);
  186. if (LLVM_UNLIKELY(!Key))
  187. continue;
  188. if (!Filename.equals_lower(*Key))
  189. continue;
  190. // If so, we have a match in the hash table. Construct the destination
  191. // path.
  192. Optional<StringRef> Prefix = getString(B.Prefix);
  193. Optional<StringRef> Suffix = getString(B.Suffix);
  194. DestPath.clear();
  195. if (LLVM_LIKELY(Prefix && Suffix)) {
  196. DestPath.append(Prefix->begin(), Prefix->end());
  197. DestPath.append(Suffix->begin(), Suffix->end());
  198. }
  199. return StringRef(DestPath.begin(), DestPath.size());
  200. }
  201. }