blob: 64a2c8e71bf3c7d822d7e68f8d791f976a74655a [file] [log] [blame]
Chris Lattner0f441ab2007-12-17 08:22:46 +00001//===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the HeaderMap interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/HeaderMap.h"
Chris Lattner1bfd4a62007-12-17 18:34:53 +000015#include "clang/Basic/FileManager.h"
16#include "llvm/ADT/scoped_ptr.h"
Chris Lattner5fb125c2007-12-17 21:38:04 +000017#include "llvm/ADT/SmallString.h"
Chris Lattner1bfd4a62007-12-17 18:34:53 +000018#include "llvm/Support/DataTypes.h"
19#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/MemoryBuffer.h"
Chris Lattner0f441ab2007-12-17 08:22:46 +000021using namespace clang;
22
Chris Lattner1adbf632007-12-17 21:06:11 +000023//===----------------------------------------------------------------------===//
24// Data Structures and Manifest Constants
25//===----------------------------------------------------------------------===//
26
Chris Lattner1bfd4a62007-12-17 18:34:53 +000027enum {
Chris Lattner1adbf632007-12-17 21:06:11 +000028 HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
29 HMAP_HeaderVersion = 1,
30
31 HMAP_EmptyBucketKey = 0
32};
33
34namespace clang {
35struct HMapBucket {
36 uint32_t Key; // Offset (into strings) of key.
37
38 uint32_t Prefix; // Offset (into strings) of value prefix.
39 uint32_t Suffix; // Offset (into strings) of value suffix.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000040};
41
42struct HMapHeader {
43 uint32_t Magic; // Magic word, also indicates byte order.
44 uint16_t Version; // Version number -- currently 1.
45 uint16_t Reserved; // Reserved for future use - zero for now.
46 uint32_t StringsOffset; // Offset to start of string pool.
Chris Lattner1adbf632007-12-17 21:06:11 +000047 uint32_t NumEntries; // Number of entries in the string table.
48 uint32_t NumBuckets; // Number of buckets (always a power of 2).
Chris Lattner1bfd4a62007-12-17 18:34:53 +000049 uint32_t MaxValueLength; // Length of longest result path (excluding nul).
Chris Lattner1adbf632007-12-17 21:06:11 +000050 // An array of 'NumBuckets' HMapBucket objects follows this header.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000051 // Strings follow the buckets, at StringsOffset.
52};
Chris Lattner1adbf632007-12-17 21:06:11 +000053} // end namespace clang.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000054
Chris Lattner5fb125c2007-12-17 21:38:04 +000055/// HashHMapKey - This is the 'well known' hash function required by the file
56/// format, used to look up keys in the hash table. The hash table uses simple
57/// linear probing based on this function.
58static inline unsigned HashHMapKey(const char *S, const char *End) {
59 unsigned Result = 0;
60
61 for (; S != End; S++)
62 Result += tolower(*S) * 13;
63 return Result;
64}
65
66
67
Chris Lattner1adbf632007-12-17 21:06:11 +000068//===----------------------------------------------------------------------===//
69// Verification and Construction
70//===----------------------------------------------------------------------===//
Chris Lattner1bfd4a62007-12-17 18:34:53 +000071
72/// HeaderMap::Create - This attempts to load the specified file as a header
73/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
74/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
75/// into the string error argument and returns null.
76const HeaderMap *HeaderMap::Create(const FileEntry *FE) {
77 // If the file is too small to be a header map, ignore it.
78 unsigned FileSize = FE->getSize();
79 if (FileSize <= sizeof(HMapHeader)) return 0;
80
Chris Lattner98751312007-12-17 18:44:09 +000081 llvm::scoped_ptr<const llvm::MemoryBuffer> FileBuffer(
Chris Lattner1bfd4a62007-12-17 18:34:53 +000082 llvm::MemoryBuffer::getFile(FE->getName(), strlen(FE->getName()), 0,
83 FE->getSize()));
Chris Lattner98751312007-12-17 18:44:09 +000084 if (FileBuffer == 0) return 0; // Unreadable file?
85 const char *FileStart = FileBuffer->getBufferStart();
Chris Lattner1bfd4a62007-12-17 18:34:53 +000086
87 // We know the file is at least as big as the header, check it now.
88 const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
89
Chris Lattnerba832652007-12-17 18:59:44 +000090 // Sniff it to see if it's a headermap by checking the magic number and
91 // version.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000092 bool NeedsByteSwap;
Chris Lattner1adbf632007-12-17 21:06:11 +000093 if (Header->Magic == HMAP_HeaderMagicNumber &&
94 Header->Version == HMAP_HeaderVersion)
Chris Lattner1bfd4a62007-12-17 18:34:53 +000095 NeedsByteSwap = false;
Chris Lattner1adbf632007-12-17 21:06:11 +000096 else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
97 Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
Chris Lattner1bfd4a62007-12-17 18:34:53 +000098 NeedsByteSwap = true; // Mixed endianness headermap.
99 else
100 return 0; // Not a header map.
Chris Lattnerba832652007-12-17 18:59:44 +0000101
102 if (Header->Reserved != 0) return 0;
Chris Lattner98751312007-12-17 18:44:09 +0000103
104 // Okay, everything looks good, create the header map.
Chris Lattnerba832652007-12-17 18:59:44 +0000105 return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
Chris Lattner98751312007-12-17 18:44:09 +0000106}
107
108HeaderMap::~HeaderMap() {
109 delete FileBuffer;
110}
111
Chris Lattner1adbf632007-12-17 21:06:11 +0000112//===----------------------------------------------------------------------===//
113// Utility Methods
114//===----------------------------------------------------------------------===//
115
Chris Lattner98751312007-12-17 18:44:09 +0000116
117/// getFileName - Return the filename of the headermap.
118const char *HeaderMap::getFileName() const {
119 return FileBuffer->getBufferIdentifier();
Chris Lattner0f441ab2007-12-17 08:22:46 +0000120}
121
Chris Lattner1adbf632007-12-17 21:06:11 +0000122unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
123 if (!NeedsBSwap) return X;
124 return llvm::ByteSwap_32(X);
125}
126
127/// getHeader - Return a reference to the file header, in unbyte-swapped form.
128/// This method cannot fail.
129const HMapHeader &HeaderMap::getHeader() const {
130 // We know the file is at least as big as the header. Return it.
131 return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
132}
133
134/// getBucket - Return the specified hash table bucket from the header map,
135/// bswap'ing its fields as appropriate. If the bucket number is not valid,
136/// this return a bucket with an empty key (0).
137HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
138 HMapBucket Result;
139 Result.Key = HMAP_EmptyBucketKey;
140
141 const HMapBucket *BucketArray =
142 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
143 sizeof(HMapHeader));
144
145 const HMapBucket *BucketPtr = BucketArray+BucketNo;
146 if ((char*)(BucketPtr+1) > FileBuffer->getBufferEnd())
147 return Result; // Invalid buffer, corrupt hmap.
148
149 // Otherwise, the bucket is valid. Load the values, bswapping as needed.
150 Result.Key = getEndianAdjustedWord(BucketPtr->Key);
151 Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
152 Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
153 return Result;
154}
155
156/// getString - Look up the specified string in the string table. If the string
157/// index is not valid, it returns an empty string.
158const char *HeaderMap::getString(unsigned StrTabIdx) const {
159 // Add the start of the string table to the idx.
160 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
161
162 // Check for invalid index.
163 if (StrTabIdx >= FileBuffer->getBufferSize())
164 return 0;
165
166 // Otherwise, we have a valid pointer into the file. Just return it. We know
167 // that the "string" can not overrun the end of the file, because the buffer
168 // is nul terminated by virtue of being a MemoryBuffer.
169 return FileBuffer->getBufferStart()+StrTabIdx;
170}
171
Chris Lattner5fb125c2007-12-17 21:38:04 +0000172/// StringsEqualWithoutCase - Compare the specified two strings for case-
173/// insensitive equality, returning true if they are equal. Both strings are
174/// known to have the same length.
175static bool StringsEqualWithoutCase(const char *S1, const char *S2,
176 unsigned Len) {
177 for (; Len; ++S1, ++S2, --Len)
178 if (tolower(*S1) != tolower(*S2))
179 return false;
180 return true;
181}
182
Chris Lattner1adbf632007-12-17 21:06:11 +0000183//===----------------------------------------------------------------------===//
184// The Main Drivers
185//===----------------------------------------------------------------------===//
186
187/// dump - Print the contents of this headermap to stderr.
188void HeaderMap::dump() const {
189 const HMapHeader &Hdr = getHeader();
190 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
191
192 fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
193 getFileName(), NumBuckets,
194 getEndianAdjustedWord(Hdr.NumEntries));
195
196 for (unsigned i = 0; i != NumBuckets; ++i) {
197 HMapBucket B = getBucket(i);
198 if (B.Key == HMAP_EmptyBucketKey) continue;
199
200 const char *Key = getString(B.Key);
201 const char *Prefix = getString(B.Prefix);
202 const char *Suffix = getString(B.Suffix);
203 fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
204 }
205}
206
Chris Lattner0f441ab2007-12-17 08:22:46 +0000207/// LookupFile - Check to see if the specified relative filename is located in
208/// this HeaderMap. If so, open it and return its FileEntry.
209const FileEntry *HeaderMap::LookupFile(const char *FilenameStart,
210 const char *FilenameEnd,
211 FileManager &FM) const {
Chris Lattner5fb125c2007-12-17 21:38:04 +0000212 const HMapHeader &Hdr = getHeader();
213 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
214
215 // If the number of buckets is not a power of two, the headermap is corrupt.
216 // Don't probe infinitely.
217 if (NumBuckets & (NumBuckets-1))
218 return 0;
219
220 // Linearly probe the hash table.
221 for (unsigned Bucket = HashHMapKey(FilenameStart, FilenameEnd);; ++Bucket) {
222 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
223 if (B.Key == HMAP_EmptyBucketKey) return 0; // Hash miss.
224
225 // See if the key matches. If not, probe on.
226 const char *Key = getString(B.Key);
227 unsigned BucketKeyLen = strlen(Key);
228 if (BucketKeyLen != unsigned(FilenameEnd-FilenameStart))
229 continue;
230
231 // See if the actual strings equal.
232 if (!StringsEqualWithoutCase(FilenameStart, Key, BucketKeyLen))
233 continue;
234
235 // If so, we have a match in the hash table. Construct the destination
236 // path.
237 llvm::SmallString<1024> DestPath;
238 DestPath += getString(B.Prefix);
239 DestPath += getString(B.Suffix);
240 return FM.getFile(DestPath.begin(), DestPath.end());
241 }
Chris Lattner0f441ab2007-12-17 08:22:46 +0000242}