HeaderMap.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/FileManager.h"
  15. #include "llvm/ADT/OwningPtr.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 <cctype>
  21. #include <cstdio>
  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 += tolower(*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 0;
  70. OwningPtr<const llvm::MemoryBuffer> FileBuffer(FM.getBufferForFile(FE));
  71. if (FileBuffer == 0) return 0; // 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 0; // Not a header map.
  86. if (Header->Reserved != 0) return 0;
  87. // Okay, everything looks good, create the header map.
  88. return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
  89. }
  90. HeaderMap::~HeaderMap() {
  91. delete FileBuffer;
  92. }
  93. //===----------------------------------------------------------------------===//
  94. // Utility Methods
  95. //===----------------------------------------------------------------------===//
  96. /// getFileName - Return the filename of the headermap.
  97. const char *HeaderMap::getFileName() const {
  98. return FileBuffer->getBufferIdentifier();
  99. }
  100. unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
  101. if (!NeedsBSwap) return X;
  102. return llvm::ByteSwap_32(X);
  103. }
  104. /// getHeader - Return a reference to the file header, in unbyte-swapped form.
  105. /// This method cannot fail.
  106. const HMapHeader &HeaderMap::getHeader() const {
  107. // We know the file is at least as big as the header. Return it.
  108. return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
  109. }
  110. /// getBucket - Return the specified hash table bucket from the header map,
  111. /// bswap'ing its fields as appropriate. If the bucket number is not valid,
  112. /// this return a bucket with an empty key (0).
  113. HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
  114. HMapBucket Result;
  115. Result.Key = HMAP_EmptyBucketKey;
  116. const HMapBucket *BucketArray =
  117. reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
  118. sizeof(HMapHeader));
  119. const HMapBucket *BucketPtr = BucketArray+BucketNo;
  120. if ((char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
  121. Result.Prefix = 0;
  122. Result.Suffix = 0;
  123. return Result; // Invalid buffer, corrupt hmap.
  124. }
  125. // Otherwise, the bucket is valid. Load the values, bswapping as needed.
  126. Result.Key = getEndianAdjustedWord(BucketPtr->Key);
  127. Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
  128. Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
  129. return Result;
  130. }
  131. /// getString - Look up the specified string in the string table. If the string
  132. /// index is not valid, it returns an empty string.
  133. const char *HeaderMap::getString(unsigned StrTabIdx) const {
  134. // Add the start of the string table to the idx.
  135. StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
  136. // Check for invalid index.
  137. if (StrTabIdx >= FileBuffer->getBufferSize())
  138. return 0;
  139. // Otherwise, we have a valid pointer into the file. Just return it. We know
  140. // that the "string" can not overrun the end of the file, because the buffer
  141. // is nul terminated by virtue of being a MemoryBuffer.
  142. return FileBuffer->getBufferStart()+StrTabIdx;
  143. }
  144. //===----------------------------------------------------------------------===//
  145. // The Main Drivers
  146. //===----------------------------------------------------------------------===//
  147. /// dump - Print the contents of this headermap to stderr.
  148. void HeaderMap::dump() const {
  149. const HMapHeader &Hdr = getHeader();
  150. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  151. fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
  152. getFileName(), NumBuckets,
  153. getEndianAdjustedWord(Hdr.NumEntries));
  154. for (unsigned i = 0; i != NumBuckets; ++i) {
  155. HMapBucket B = getBucket(i);
  156. if (B.Key == HMAP_EmptyBucketKey) continue;
  157. const char *Key = getString(B.Key);
  158. const char *Prefix = getString(B.Prefix);
  159. const char *Suffix = getString(B.Suffix);
  160. fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
  161. }
  162. }
  163. /// LookupFile - Check to see if the specified relative filename is located in
  164. /// this HeaderMap. If so, open it and return its FileEntry.
  165. const FileEntry *HeaderMap::LookupFile(
  166. StringRef Filename, FileManager &FM) const {
  167. const HMapHeader &Hdr = getHeader();
  168. unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
  169. // If the number of buckets is not a power of two, the headermap is corrupt.
  170. // Don't probe infinitely.
  171. if (NumBuckets & (NumBuckets-1))
  172. return 0;
  173. // Linearly probe the hash table.
  174. for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
  175. HMapBucket B = getBucket(Bucket & (NumBuckets-1));
  176. if (B.Key == HMAP_EmptyBucketKey) return 0; // Hash miss.
  177. // See if the key matches. If not, probe on.
  178. if (!Filename.equals_lower(getString(B.Key)))
  179. continue;
  180. // If so, we have a match in the hash table. Construct the destination
  181. // path.
  182. SmallString<1024> DestPath;
  183. DestPath += getString(B.Prefix);
  184. DestPath += getString(B.Suffix);
  185. return FM.getFile(DestPath.str());
  186. }
  187. }