blob: d5d7e4b9bb26f14272d783f3f52cdcb76adb68fa [file] [log] [blame]
Chris Lattner1587e6d2007-12-17 08:22:46 +00001//===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1587e6d2007-12-17 08:22:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the HeaderMap interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/HeaderMap.h"
Chris Lattner4ffe46c2007-12-17 18:34:53 +000015#include "clang/Basic/FileManager.h"
Ted Kremenek71d643f2007-12-20 19:47:16 +000016#include "llvm/ADT/OwningPtr.h"
Chris Lattner619e18c2007-12-17 21:38:04 +000017#include "llvm/ADT/SmallString.h"
Chandler Carrutha3f084c2009-10-26 01:37:10 +000018#include "llvm/System/DataTypes.h"
Chris Lattner4ffe46c2007-12-17 18:34:53 +000019#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000021#include <cstdio>
Chris Lattner1587e6d2007-12-17 08:22:46 +000022using namespace clang;
23
Chris Lattner9f9a6192007-12-17 21:06:11 +000024//===----------------------------------------------------------------------===//
25// Data Structures and Manifest Constants
26//===----------------------------------------------------------------------===//
27
Chris Lattner4ffe46c2007-12-17 18:34:53 +000028enum {
Chris Lattner9f9a6192007-12-17 21:06:11 +000029 HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
30 HMAP_HeaderVersion = 1,
Mike Stump11289f42009-09-09 15:08:12 +000031
32 HMAP_EmptyBucketKey = 0
Chris Lattner9f9a6192007-12-17 21:06:11 +000033};
34
35namespace clang {
36struct 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 Lattner4ffe46c2007-12-17 18:34:53 +000041};
42
43struct 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 Lattner9f9a6192007-12-17 21:06:11 +000048 uint32_t NumEntries; // Number of entries in the string table.
49 uint32_t NumBuckets; // Number of buckets (always a power of 2).
Chris Lattner4ffe46c2007-12-17 18:34:53 +000050 uint32_t MaxValueLength; // Length of longest result path (excluding nul).
Chris Lattner9f9a6192007-12-17 21:06:11 +000051 // An array of 'NumBuckets' HMapBucket objects follows this header.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000052 // Strings follow the buckets, at StringsOffset.
53};
Chris Lattner9f9a6192007-12-17 21:06:11 +000054} // end namespace clang.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000055
Chris Lattner619e18c2007-12-17 21:38:04 +000056/// 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 Lattnerd081f8c2010-01-10 01:35:12 +000059static inline unsigned HashHMapKey(llvm::StringRef Str) {
Chris Lattner619e18c2007-12-17 21:38:04 +000060 unsigned Result = 0;
Chris Lattnerd081f8c2010-01-10 01:35:12 +000061 const char *S = Str.begin(), *End = Str.end();
Mike Stump11289f42009-09-09 15:08:12 +000062
Chris Lattner619e18c2007-12-17 21:38:04 +000063 for (; S != End; S++)
64 Result += tolower(*S) * 13;
65 return Result;
66}
67
68
69
Chris Lattner9f9a6192007-12-17 21:06:11 +000070//===----------------------------------------------------------------------===//
71// Verification and Construction
72//===----------------------------------------------------------------------===//
Chris Lattner4ffe46c2007-12-17 18:34:53 +000073
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.
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000078const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM,
79 const FileSystemOptions &FSOpts) {
Chris Lattner4ffe46c2007-12-17 18:34:53 +000080 // If the file is too small to be a header map, ignore it.
81 unsigned FileSize = FE->getSize();
82 if (FileSize <= sizeof(HMapHeader)) return 0;
Mike Stump11289f42009-09-09 15:08:12 +000083
84 llvm::OwningPtr<const llvm::MemoryBuffer> FileBuffer(
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000085 FM.getBufferForFile(FE, FSOpts));
Chris Lattner79764a62007-12-17 18:44:09 +000086 if (FileBuffer == 0) return 0; // Unreadable file?
87 const char *FileStart = FileBuffer->getBufferStart();
Chris Lattner4ffe46c2007-12-17 18:34:53 +000088
89 // We know the file is at least as big as the header, check it now.
90 const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnerd39b8c02007-12-17 18:59:44 +000092 // Sniff it to see if it's a headermap by checking the magic number and
93 // version.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000094 bool NeedsByteSwap;
Mike Stump11289f42009-09-09 15:08:12 +000095 if (Header->Magic == HMAP_HeaderMagicNumber &&
Chris Lattner9f9a6192007-12-17 21:06:11 +000096 Header->Version == HMAP_HeaderVersion)
Chris Lattner4ffe46c2007-12-17 18:34:53 +000097 NeedsByteSwap = false;
Chris Lattner9f9a6192007-12-17 21:06:11 +000098 else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
99 Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
Chris Lattner4ffe46c2007-12-17 18:34:53 +0000100 NeedsByteSwap = true; // Mixed endianness headermap.
Mike Stump11289f42009-09-09 15:08:12 +0000101 else
Chris Lattner4ffe46c2007-12-17 18:34:53 +0000102 return 0; // Not a header map.
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattnerd39b8c02007-12-17 18:59:44 +0000104 if (Header->Reserved != 0) return 0;
Chris Lattner79764a62007-12-17 18:44:09 +0000105
106 // Okay, everything looks good, create the header map.
Chris Lattnerd39b8c02007-12-17 18:59:44 +0000107 return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
Chris Lattner79764a62007-12-17 18:44:09 +0000108}
109
110HeaderMap::~HeaderMap() {
111 delete FileBuffer;
112}
113
Chris Lattner9f9a6192007-12-17 21:06:11 +0000114//===----------------------------------------------------------------------===//
115// Utility Methods
116//===----------------------------------------------------------------------===//
117
Chris Lattner79764a62007-12-17 18:44:09 +0000118
119/// getFileName - Return the filename of the headermap.
120const char *HeaderMap::getFileName() const {
121 return FileBuffer->getBufferIdentifier();
Chris Lattner1587e6d2007-12-17 08:22:46 +0000122}
123
Chris Lattner9f9a6192007-12-17 21:06:11 +0000124unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
125 if (!NeedsBSwap) return X;
126 return llvm::ByteSwap_32(X);
127}
128
129/// getHeader - Return a reference to the file header, in unbyte-swapped form.
130/// This method cannot fail.
131const HMapHeader &HeaderMap::getHeader() const {
132 // We know the file is at least as big as the header. Return it.
133 return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
134}
135
136/// getBucket - Return the specified hash table bucket from the header map,
137/// bswap'ing its fields as appropriate. If the bucket number is not valid,
138/// this return a bucket with an empty key (0).
139HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
140 HMapBucket Result;
141 Result.Key = HMAP_EmptyBucketKey;
Mike Stump11289f42009-09-09 15:08:12 +0000142
143 const HMapBucket *BucketArray =
Chris Lattner9f9a6192007-12-17 21:06:11 +0000144 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
145 sizeof(HMapHeader));
Mike Stump11289f42009-09-09 15:08:12 +0000146
Chris Lattner9f9a6192007-12-17 21:06:11 +0000147 const HMapBucket *BucketPtr = BucketArray+BucketNo;
Ted Kremenek100f87d2008-10-28 00:18:42 +0000148 if ((char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
149 Result.Prefix = 0;
150 Result.Suffix = 0;
Chris Lattner9f9a6192007-12-17 21:06:11 +0000151 return Result; // Invalid buffer, corrupt hmap.
Ted Kremenek100f87d2008-10-28 00:18:42 +0000152 }
Chris Lattner9f9a6192007-12-17 21:06:11 +0000153
154 // Otherwise, the bucket is valid. Load the values, bswapping as needed.
155 Result.Key = getEndianAdjustedWord(BucketPtr->Key);
156 Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
157 Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
158 return Result;
159}
160
161/// getString - Look up the specified string in the string table. If the string
162/// index is not valid, it returns an empty string.
163const char *HeaderMap::getString(unsigned StrTabIdx) const {
164 // Add the start of the string table to the idx.
165 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000166
Chris Lattner9f9a6192007-12-17 21:06:11 +0000167 // Check for invalid index.
168 if (StrTabIdx >= FileBuffer->getBufferSize())
169 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000170
Chris Lattner9f9a6192007-12-17 21:06:11 +0000171 // Otherwise, we have a valid pointer into the file. Just return it. We know
172 // that the "string" can not overrun the end of the file, because the buffer
173 // is nul terminated by virtue of being a MemoryBuffer.
174 return FileBuffer->getBufferStart()+StrTabIdx;
175}
176
177//===----------------------------------------------------------------------===//
178// The Main Drivers
179//===----------------------------------------------------------------------===//
180
181/// dump - Print the contents of this headermap to stderr.
182void HeaderMap::dump() const {
183 const HMapHeader &Hdr = getHeader();
184 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
Mike Stump11289f42009-09-09 15:08:12 +0000185
186 fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
Chris Lattner9f9a6192007-12-17 21:06:11 +0000187 getFileName(), NumBuckets,
188 getEndianAdjustedWord(Hdr.NumEntries));
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattner9f9a6192007-12-17 21:06:11 +0000190 for (unsigned i = 0; i != NumBuckets; ++i) {
191 HMapBucket B = getBucket(i);
192 if (B.Key == HMAP_EmptyBucketKey) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000193
Chris Lattner9f9a6192007-12-17 21:06:11 +0000194 const char *Key = getString(B.Key);
195 const char *Prefix = getString(B.Prefix);
196 const char *Suffix = getString(B.Suffix);
197 fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
198 }
199}
200
Chris Lattner1587e6d2007-12-17 08:22:46 +0000201/// LookupFile - Check to see if the specified relative filename is located in
202/// this HeaderMap. If so, open it and return its FileEntry.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000203const FileEntry *HeaderMap::LookupFile(llvm::StringRef Filename,
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +0000204 FileManager &FM,
205 const FileSystemOptions &FileSystemOpts) const {
Chris Lattner619e18c2007-12-17 21:38:04 +0000206 const HMapHeader &Hdr = getHeader();
207 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
208
209 // If the number of buckets is not a power of two, the headermap is corrupt.
210 // Don't probe infinitely.
211 if (NumBuckets & (NumBuckets-1))
212 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000213
Chris Lattner619e18c2007-12-17 21:38:04 +0000214 // Linearly probe the hash table.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000215 for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
Chris Lattner619e18c2007-12-17 21:38:04 +0000216 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
217 if (B.Key == HMAP_EmptyBucketKey) return 0; // Hash miss.
Mike Stump11289f42009-09-09 15:08:12 +0000218
Chris Lattner619e18c2007-12-17 21:38:04 +0000219 // See if the key matches. If not, probe on.
Benjamin Kramer307c2c72010-01-10 09:51:00 +0000220 if (!Filename.equals_lower(getString(B.Key)))
Chris Lattner619e18c2007-12-17 21:38:04 +0000221 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000222
Chris Lattner619e18c2007-12-17 21:38:04 +0000223 // If so, we have a match in the hash table. Construct the destination
224 // path.
225 llvm::SmallString<1024> DestPath;
226 DestPath += getString(B.Prefix);
227 DestPath += getString(B.Suffix);
Chris Lattner8afa6de2010-11-21 09:55:08 +0000228 return FM.getFile(DestPath.str(), FileSystemOpts);
Chris Lattner619e18c2007-12-17 21:38:04 +0000229 }
Chris Lattner1587e6d2007-12-17 08:22:46 +0000230}