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