HeaderMap.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the HeaderMap interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/HeaderMap.h"
  14. #include "clang/Basic/CharInfo.h"
  15. #include "clang/Basic/FileManager.h"
  16. #include "llvm/ADT/SmallString.h"
  17. #include "llvm/Support/DataTypes.h"
  18. #include "llvm/Support/MathExtras.h"
  19. #include "llvm/Support/MemoryBuffer.h"
  20. #include <cstdio>
  21. #include <memory>
  22. using namespace clang;
  23. //===----------------------------------------------------------------------===//
  24. // Data Structures and Manifest Constants
  25. //===----------------------------------------------------------------------===//
  26. enum {
  27. HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
  28. HMAP_HeaderVersion = 1,
  29. HMAP_EmptyBucketKey = 0
  30. };
  31. namespace clang {
  32. struct HMapBucket {
  33. uint32_t Key; // Offset (into strings) of key.
  34. uint32_t Prefix; // Offset (into strings) of value prefix.
  35. uint32_t Suffix; // Offset (into strings) of value suffix.
  36. };
  37. struct HMapHeader {
  38. uint32_t Magic; // Magic word, also indicates byte order.
  39. uint16_t Version; // Version number -- currently 1.
  40. uint16_t Reserved; // Reserved for future use - zero for now.
  41. uint32_t StringsOffset; // Offset to start of string pool.
  42. uint32_t NumEntries; // Number of entries in the string table.
  43. uint32_t NumBuckets; // Number of buckets (always a power of 2).
  44. uint32_t MaxValueLength; // Length of longest result path (excluding nul).
  45. // An array of 'NumBuckets' HMapBucket objects follows this header.
  46. // Strings follow the buckets, at StringsOffset.
  47. };
  48. } // end namespace clang.
  49. /// HashHMapKey - This is the 'well known' hash function required by the file
  50. /// format, used to look up keys in the hash table. The hash table uses simple
  51. /// linear probing based on this function.
  52. static inline unsigned HashHMapKey(StringRef Str) {
  53. unsigned Result = 0;
  54. const char *S = Str.begin(), *End = Str.end();
  55. for (; S != End; S++)
  56. Result += toLowercase(*S) * 13;
  57. return Result;
  58. }
  59. //===----------------------------------------------------------------------===//
  60. // Verification and Construction
  61. //===----------------------------------------------------------------------===//
  62. /// HeaderMap::Create - This attempts to load the specified file as a header
  63. /// map. If it doesn't look like a HeaderMap, it gives up and returns null.
  64. /// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
  65. /// into the string error argument and returns null.
  66. const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
  67. // If the file is too small to be a header map, ignore it.
  68. unsigned FileSize = FE->getSize();
  69. if (FileSize <= sizeof(HMapHeader)) return nullptr;
  70. auto FileBuffer = FM.getBufferForFile(FE);
  71. if (!FileBuffer) return nullptr; // Unreadable file?
  72. const char *FileStart = (*FileBuffer)->getBufferStart();
  73. // We know the file is at least as big as the header, check it now.
  74. const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
  75. // Sniff it to see if it's a headermap by checking the magic number and
  76. // version.
  77. bool NeedsByteSwap;
  78. if (Header->Magic == HMAP_HeaderMagicNumber &&
  79. Header->Version == HMAP_HeaderVersion)
  80. NeedsByteSwap = false;
  81. else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
  82. Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
  83. NeedsByteSwap = true; // Mixed endianness headermap.
  84. else
  85. return nullptr; // Not a header map.
  86. if (Header->Reserved != 0) return nullptr;
  87. // Okay, everything looks good, create the header map.
  88. return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap);
  89. }
  90. //===----------------------------------------------------------------------===//
  91. // Utility Methods
  92. //===----------------------------------------------------------------------===//
  93. /// getFileName - Return the filename of the headermap.
  94. const char *HeaderMap::getFileName() const {
  95. return FileBuffer->getBufferIdentifier();
  96. }
  97. unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
  98. if (!NeedsBSwap) return X;
  99. return llvm::ByteSwap_32(X);
  100. }
  101. /// getHeader - Return a reference to the file header, in unbyte-swapped form.
  102. /// This method cannot fail.
  103. const HMapHeader &HeaderMap::getHeader() const {
  104. // We know the file is at least as big as the header. Return it.
  105. return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
  106. }
  107. /// getBucket - Return the specified hash table bucket from the header map,
  108. /// bswap'ing its fields as appropriate. If the bucket number is not valid,
  109. /// this return a bucket with an empty key (0).
  110. HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
  111. HMapBucket Result;
  112. Result.Key = HMAP_EmptyBucketKey;
  113. const HMapBucket *BucketArray =
  114. reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
  115. sizeof(HMapHeader));
  116. const HMapBucket *BucketPtr = BucketArray+BucketNo;
  117. if ((const char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
  118. Result.Prefix = 0;
  119. Result.Suffix = 0;
  120. return Result; // Invalid buffer, corrupt hmap.
  121. }
  122. // Otherwise, the bucket is valid. Load the values, bswapping as needed.
  123. Result.Key = getEndianAdjustedWord(BucketPtr->Key);
  124. Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
  125. Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
  126. return Result;
  127. }
  128. /// getString - Look up the specified string in the string table. If the string
  129. /// index is not valid, it returns an empty string.
  130. const char *HeaderMap::getString(unsigned StrTabIdx) const {
  131. // Add the start of the string table to the idx.
  132. StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
  133. // Check for invalid index.
  134. if (StrTabIdx >= FileBuffer->getBufferSize())
  135. return nullptr;
  136. // Otherwise, we have a valid pointer into the file. Just return it. We know
  137. // that the "string" can not overrun the end of the file, because the buffer
  138. // is nul terminated by virtue of being a MemoryBuffer.
  139. return FileBuffer->getBufferStart()+StrTabIdx;
  140. }
  141. //===----------------------------------------------------------------------===//
  142. // The Main Drivers
  143. //===----------------------------------------------------------------------===//
  144. /// dump - Print the contents of this headermap to stderr.
  145. void HeaderMap::dump() const {
  146. const HMapHeader &Hdr = getHeader();
  147. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  148. fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
  149. getFileName(), NumBuckets,
  150. getEndianAdjustedWord(Hdr.NumEntries));
  151. for (unsigned i = 0; i != NumBuckets; ++i) {
  152. HMapBucket B = getBucket(i);
  153. if (B.Key == HMAP_EmptyBucketKey) continue;
  154. const char *Key = getString(B.Key);
  155. const char *Prefix = getString(B.Prefix);
  156. const char *Suffix = getString(B.Suffix);
  157. fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
  158. }
  159. }
  160. /// LookupFile - Check to see if the specified relative filename is located in
  161. /// this HeaderMap. If so, open it and return its FileEntry.
  162. const FileEntry *HeaderMap::LookupFile(
  163. StringRef Filename, FileManager &FM) const {
  164. SmallString<1024> Path;
  165. StringRef Dest = lookupFilename(Filename, Path);
  166. if (Dest.empty())
  167. return nullptr;
  168. return FM.getFile(Dest);
  169. }
  170. StringRef HeaderMap::lookupFilename(StringRef Filename,
  171. SmallVectorImpl<char> &DestPath) const {
  172. const HMapHeader &Hdr = getHeader();
  173. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  174. // If the number of buckets is not a power of two, the headermap is corrupt.
  175. // Don't probe infinitely.
  176. if (NumBuckets & (NumBuckets-1))
  177. return StringRef();
  178. // Linearly probe the hash table.
  179. for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
  180. HMapBucket B = getBucket(Bucket & (NumBuckets-1));
  181. if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
  182. // See if the key matches. If not, probe on.
  183. if (!Filename.equals_lower(getString(B.Key)))
  184. continue;
  185. // If so, we have a match in the hash table. Construct the destination
  186. // path.
  187. StringRef Prefix = getString(B.Prefix);
  188. StringRef Suffix = getString(B.Suffix);
  189. DestPath.clear();
  190. DestPath.append(Prefix.begin(), Prefix.end());
  191. DestPath.append(Suffix.begin(), Suffix.end());
  192. return StringRef(DestPath.begin(), DestPath.size());
  193. }
  194. }