HeaderMap.cpp 9.2 KB

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