blob: 7dc0491392ccb76721d26d1506f7085ce00858f5 [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//
Chris Lattner0bc735f2007-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 Lattner0f441ab2007-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 Lattner1bfd4a62007-12-17 18:34:53 +000015#include "clang/Basic/FileManager.h"
Ted Kremenekee533642007-12-20 19:47:16 +000016#include "llvm/ADT/OwningPtr.h"
Chris Lattner5fb125c2007-12-17 21:38:04 +000017#include "llvm/ADT/SmallString.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000018#include "llvm/Support/DataTypes.h"
Chris Lattner1bfd4a62007-12-17 18:34:53 +000019#include "llvm/Support/MathExtras.h"
20#include "llvm/Support/MemoryBuffer.h"
Nick Lewycky403ba352010-12-19 20:49:25 +000021#include <cctype>
Chris Lattner3daed522009-03-02 22:20:04 +000022#include <cstdio>
Chris Lattner0f441ab2007-12-17 08:22:46 +000023using namespace clang;
24
Chris Lattner1adbf632007-12-17 21:06:11 +000025//===----------------------------------------------------------------------===//
26// Data Structures and Manifest Constants
27//===----------------------------------------------------------------------===//
28
Chris Lattner1bfd4a62007-12-17 18:34:53 +000029enum {
Chris Lattner1adbf632007-12-17 21:06:11 +000030 HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
31 HMAP_HeaderVersion = 1,
Mike Stump1eb44332009-09-09 15:08:12 +000032
33 HMAP_EmptyBucketKey = 0
Chris Lattner1adbf632007-12-17 21:06:11 +000034};
35
36namespace clang {
37struct HMapBucket {
38 uint32_t Key; // Offset (into strings) of key.
39
40 uint32_t Prefix; // Offset (into strings) of value prefix.
41 uint32_t Suffix; // Offset (into strings) of value suffix.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000042};
43
44struct HMapHeader {
45 uint32_t Magic; // Magic word, also indicates byte order.
46 uint16_t Version; // Version number -- currently 1.
47 uint16_t Reserved; // Reserved for future use - zero for now.
48 uint32_t StringsOffset; // Offset to start of string pool.
Chris Lattner1adbf632007-12-17 21:06:11 +000049 uint32_t NumEntries; // Number of entries in the string table.
50 uint32_t NumBuckets; // Number of buckets (always a power of 2).
Chris Lattner1bfd4a62007-12-17 18:34:53 +000051 uint32_t MaxValueLength; // Length of longest result path (excluding nul).
Chris Lattner1adbf632007-12-17 21:06:11 +000052 // An array of 'NumBuckets' HMapBucket objects follows this header.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000053 // Strings follow the buckets, at StringsOffset.
54};
Chris Lattner1adbf632007-12-17 21:06:11 +000055} // end namespace clang.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000056
Chris Lattner5fb125c2007-12-17 21:38:04 +000057/// HashHMapKey - This is the 'well known' hash function required by the file
58/// format, used to look up keys in the hash table. The hash table uses simple
59/// linear probing based on this function.
Chris Lattner5f9e2722011-07-23 10:55:15 +000060static inline unsigned HashHMapKey(StringRef Str) {
Chris Lattner5fb125c2007-12-17 21:38:04 +000061 unsigned Result = 0;
Chris Lattnera1394812010-01-10 01:35:12 +000062 const char *S = Str.begin(), *End = Str.end();
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattner5fb125c2007-12-17 21:38:04 +000064 for (; S != End; S++)
65 Result += tolower(*S) * 13;
66 return Result;
67}
68
69
70
Chris Lattner1adbf632007-12-17 21:06:11 +000071//===----------------------------------------------------------------------===//
72// Verification and Construction
73//===----------------------------------------------------------------------===//
Chris Lattner1bfd4a62007-12-17 18:34:53 +000074
75/// HeaderMap::Create - This attempts to load the specified file as a header
76/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
77/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
78/// into the string error argument and returns null.
Chris Lattner39b49bc2010-11-23 08:35:12 +000079const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
Chris Lattner1bfd4a62007-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 Stump1eb44332009-09-09 15:08:12 +000083
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000084 OwningPtr<const llvm::MemoryBuffer> FileBuffer(FM.getBufferForFile(FE));
Chris Lattner98751312007-12-17 18:44:09 +000085 if (FileBuffer == 0) return 0; // Unreadable file?
86 const char *FileStart = FileBuffer->getBufferStart();
Chris Lattner1bfd4a62007-12-17 18:34:53 +000087
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 Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattnerba832652007-12-17 18:59:44 +000091 // Sniff it to see if it's a headermap by checking the magic number and
92 // version.
Chris Lattner1bfd4a62007-12-17 18:34:53 +000093 bool NeedsByteSwap;
Mike Stump1eb44332009-09-09 15:08:12 +000094 if (Header->Magic == HMAP_HeaderMagicNumber &&
Chris Lattner1adbf632007-12-17 21:06:11 +000095 Header->Version == HMAP_HeaderVersion)
Chris Lattner1bfd4a62007-12-17 18:34:53 +000096 NeedsByteSwap = false;
Chris Lattner1adbf632007-12-17 21:06:11 +000097 else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
98 Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
Chris Lattner1bfd4a62007-12-17 18:34:53 +000099 NeedsByteSwap = true; // Mixed endianness headermap.
Mike Stump1eb44332009-09-09 15:08:12 +0000100 else
Chris Lattner1bfd4a62007-12-17 18:34:53 +0000101 return 0; // Not a header map.
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattnerba832652007-12-17 18:59:44 +0000103 if (Header->Reserved != 0) return 0;
Chris Lattner98751312007-12-17 18:44:09 +0000104
105 // Okay, everything looks good, create the header map.
Chris Lattnerba832652007-12-17 18:59:44 +0000106 return new HeaderMap(FileBuffer.take(), NeedsByteSwap);
Chris Lattner98751312007-12-17 18:44:09 +0000107}
108
109HeaderMap::~HeaderMap() {
110 delete FileBuffer;
111}
112
Chris Lattner1adbf632007-12-17 21:06:11 +0000113//===----------------------------------------------------------------------===//
114// Utility Methods
115//===----------------------------------------------------------------------===//
116
Chris Lattner98751312007-12-17 18:44:09 +0000117
118/// getFileName - Return the filename of the headermap.
119const char *HeaderMap::getFileName() const {
120 return FileBuffer->getBufferIdentifier();
Chris Lattner0f441ab2007-12-17 08:22:46 +0000121}
122
Chris Lattner1adbf632007-12-17 21:06:11 +0000123unsigned 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.
130const 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).
138HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
139 HMapBucket Result;
140 Result.Key = HMAP_EmptyBucketKey;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
142 const HMapBucket *BucketArray =
Chris Lattner1adbf632007-12-17 21:06:11 +0000143 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
144 sizeof(HMapHeader));
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Chris Lattner1adbf632007-12-17 21:06:11 +0000146 const HMapBucket *BucketPtr = BucketArray+BucketNo;
Roman Divacky31ba6132012-09-06 15:59:27 +0000147 if ((const char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
Ted Kremenek8173dba2008-10-28 00:18:42 +0000148 Result.Prefix = 0;
149 Result.Suffix = 0;
Chris Lattner1adbf632007-12-17 21:06:11 +0000150 return Result; // Invalid buffer, corrupt hmap.
Ted Kremenek8173dba2008-10-28 00:18:42 +0000151 }
Chris Lattner1adbf632007-12-17 21:06:11 +0000152
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.
162const char *HeaderMap::getString(unsigned StrTabIdx) const {
163 // Add the start of the string table to the idx.
164 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Chris Lattner1adbf632007-12-17 21:06:11 +0000166 // Check for invalid index.
167 if (StrTabIdx >= FileBuffer->getBufferSize())
168 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Chris Lattner1adbf632007-12-17 21:06:11 +0000170 // 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.
181void HeaderMap::dump() const {
182 const HMapHeader &Hdr = getHeader();
183 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
Mike Stump1eb44332009-09-09 15:08:12 +0000184
185 fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
Chris Lattner1adbf632007-12-17 21:06:11 +0000186 getFileName(), NumBuckets,
187 getEndianAdjustedWord(Hdr.NumEntries));
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattner1adbf632007-12-17 21:06:11 +0000189 for (unsigned i = 0; i != NumBuckets; ++i) {
190 HMapBucket B = getBucket(i);
191 if (B.Key == HMAP_EmptyBucketKey) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Chris Lattner1adbf632007-12-17 21:06:11 +0000193 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 Lattner0f441ab2007-12-17 08:22:46 +0000200/// LookupFile - Check to see if the specified relative filename is located in
201/// this HeaderMap. If so, open it and return its FileEntry.
Chandler Carruthb5142bb2011-03-16 18:34:36 +0000202const FileEntry *HeaderMap::LookupFile(
Chris Lattner5f9e2722011-07-23 10:55:15 +0000203 StringRef Filename, FileManager &FM) const {
Chris Lattner5fb125c2007-12-17 21:38:04 +0000204 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 Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattner5fb125c2007-12-17 21:38:04 +0000212 // Linearly probe the hash table.
Chris Lattnera1394812010-01-10 01:35:12 +0000213 for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
Chris Lattner5fb125c2007-12-17 21:38:04 +0000214 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
215 if (B.Key == HMAP_EmptyBucketKey) return 0; // Hash miss.
Mike Stump1eb44332009-09-09 15:08:12 +0000216
Chris Lattner5fb125c2007-12-17 21:38:04 +0000217 // See if the key matches. If not, probe on.
Benjamin Kramerdbdaf832010-01-10 09:51:00 +0000218 if (!Filename.equals_lower(getString(B.Key)))
Chris Lattner5fb125c2007-12-17 21:38:04 +0000219 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Chris Lattner5fb125c2007-12-17 21:38:04 +0000221 // If so, we have a match in the hash table. Construct the destination
222 // path.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000223 SmallString<1024> DestPath;
Chris Lattner5fb125c2007-12-17 21:38:04 +0000224 DestPath += getString(B.Prefix);
225 DestPath += getString(B.Suffix);
Chris Lattner39b49bc2010-11-23 08:35:12 +0000226 return FM.getFile(DestPath.str());
Chris Lattner5fb125c2007-12-17 21:38:04 +0000227 }
Chris Lattner0f441ab2007-12-17 08:22:46 +0000228}