blob: c86569f74a67502f4d9acab43053a15a97876b91 [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"
Jordan Rose4938f272013-02-09 10:09:43 +000015#include "clang/Basic/CharInfo.h"
Chris Lattner4ffe46c2007-12-17 18:34:53 +000016#include "clang/Basic/FileManager.h"
Chris Lattner619e18c2007-12-17 21:38:04 +000017#include "llvm/ADT/SmallString.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000018#include "llvm/Support/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>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000022#include <memory>
Chris Lattner1587e6d2007-12-17 08:22:46 +000023using namespace clang;
24
Chris Lattner9f9a6192007-12-17 21:06:11 +000025//===----------------------------------------------------------------------===//
26// Data Structures and Manifest Constants
27//===----------------------------------------------------------------------===//
28
Chris Lattner4ffe46c2007-12-17 18:34:53 +000029enum {
Chris Lattner9f9a6192007-12-17 21:06:11 +000030 HMAP_HeaderMagicNumber = ('h' << 24) | ('m' << 16) | ('a' << 8) | 'p',
31 HMAP_HeaderVersion = 1,
Mike Stump11289f42009-09-09 15:08:12 +000032
33 HMAP_EmptyBucketKey = 0
Chris Lattner9f9a6192007-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 Lattner4ffe46c2007-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 Lattner9f9a6192007-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 Lattner4ffe46c2007-12-17 18:34:53 +000051 uint32_t MaxValueLength; // Length of longest result path (excluding nul).
Chris Lattner9f9a6192007-12-17 21:06:11 +000052 // An array of 'NumBuckets' HMapBucket objects follows this header.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000053 // Strings follow the buckets, at StringsOffset.
54};
Chris Lattner9f9a6192007-12-17 21:06:11 +000055} // end namespace clang.
Chris Lattner4ffe46c2007-12-17 18:34:53 +000056
Chris Lattner619e18c2007-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 Lattner0e62c1c2011-07-23 10:55:15 +000060static inline unsigned HashHMapKey(StringRef Str) {
Chris Lattner619e18c2007-12-17 21:38:04 +000061 unsigned Result = 0;
Chris Lattnerd081f8c2010-01-10 01:35:12 +000062 const char *S = Str.begin(), *End = Str.end();
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattner619e18c2007-12-17 21:38:04 +000064 for (; S != End; S++)
Jordan Rose4938f272013-02-09 10:09:43 +000065 Result += toLowercase(*S) * 13;
Chris Lattner619e18c2007-12-17 21:38:04 +000066 return Result;
67}
68
69
70
Chris Lattner9f9a6192007-12-17 21:06:11 +000071//===----------------------------------------------------------------------===//
72// Verification and Construction
73//===----------------------------------------------------------------------===//
Chris Lattner4ffe46c2007-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 Lattner5159f612010-11-23 08:35:12 +000079const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
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();
Craig Topperd2d442c2014-05-17 23:10:59 +000082 if (FileSize <= sizeof(HMapHeader)) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000083
David Blaikie09844ad2014-08-29 19:51:32 +000084 std::unique_ptr<const llvm::MemoryBuffer> FileBuffer =
85 FM.getBufferForFile(FE);
Craig Topperd2d442c2014-05-17 23:10:59 +000086 if (!FileBuffer) return nullptr; // Unreadable file?
Chris Lattner79764a62007-12-17 18:44:09 +000087 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
Craig Topperd2d442c2014-05-17 23:10:59 +0000102 return nullptr; // Not a header map.
Mike Stump11289f42009-09-09 15:08:12 +0000103
Craig Topperd2d442c2014-05-17 23:10:59 +0000104 if (Header->Reserved != 0) return nullptr;
Chris Lattner79764a62007-12-17 18:44:09 +0000105
106 // Okay, everything looks good, create the header map.
David Blaikie09844ad2014-08-29 19:51:32 +0000107 return new HeaderMap(std::move(FileBuffer), NeedsByteSwap);
Chris Lattner79764a62007-12-17 18:44:09 +0000108}
109
Chris Lattner9f9a6192007-12-17 21:06:11 +0000110//===----------------------------------------------------------------------===//
111// Utility Methods
112//===----------------------------------------------------------------------===//
113
Chris Lattner79764a62007-12-17 18:44:09 +0000114
115/// getFileName - Return the filename of the headermap.
116const char *HeaderMap::getFileName() const {
117 return FileBuffer->getBufferIdentifier();
Chris Lattner1587e6d2007-12-17 08:22:46 +0000118}
119
Chris Lattner9f9a6192007-12-17 21:06:11 +0000120unsigned HeaderMap::getEndianAdjustedWord(unsigned X) const {
121 if (!NeedsBSwap) return X;
122 return llvm::ByteSwap_32(X);
123}
124
125/// getHeader - Return a reference to the file header, in unbyte-swapped form.
126/// This method cannot fail.
127const HMapHeader &HeaderMap::getHeader() const {
128 // We know the file is at least as big as the header. Return it.
129 return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
130}
131
132/// getBucket - Return the specified hash table bucket from the header map,
133/// bswap'ing its fields as appropriate. If the bucket number is not valid,
134/// this return a bucket with an empty key (0).
135HMapBucket HeaderMap::getBucket(unsigned BucketNo) const {
136 HMapBucket Result;
137 Result.Key = HMAP_EmptyBucketKey;
Mike Stump11289f42009-09-09 15:08:12 +0000138
139 const HMapBucket *BucketArray =
Chris Lattner9f9a6192007-12-17 21:06:11 +0000140 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
141 sizeof(HMapHeader));
Mike Stump11289f42009-09-09 15:08:12 +0000142
Chris Lattner9f9a6192007-12-17 21:06:11 +0000143 const HMapBucket *BucketPtr = BucketArray+BucketNo;
Roman Divackye6377112012-09-06 15:59:27 +0000144 if ((const char*)(BucketPtr+1) > FileBuffer->getBufferEnd()) {
Ted Kremenek100f87d2008-10-28 00:18:42 +0000145 Result.Prefix = 0;
146 Result.Suffix = 0;
Chris Lattner9f9a6192007-12-17 21:06:11 +0000147 return Result; // Invalid buffer, corrupt hmap.
Ted Kremenek100f87d2008-10-28 00:18:42 +0000148 }
Chris Lattner9f9a6192007-12-17 21:06:11 +0000149
150 // Otherwise, the bucket is valid. Load the values, bswapping as needed.
151 Result.Key = getEndianAdjustedWord(BucketPtr->Key);
152 Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
153 Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
154 return Result;
155}
156
157/// getString - Look up the specified string in the string table. If the string
158/// index is not valid, it returns an empty string.
159const char *HeaderMap::getString(unsigned StrTabIdx) const {
160 // Add the start of the string table to the idx.
161 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner9f9a6192007-12-17 21:06:11 +0000163 // Check for invalid index.
164 if (StrTabIdx >= FileBuffer->getBufferSize())
Craig Topperd2d442c2014-05-17 23:10:59 +0000165 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000166
Chris Lattner9f9a6192007-12-17 21:06:11 +0000167 // Otherwise, we have a valid pointer into the file. Just return it. We know
168 // that the "string" can not overrun the end of the file, because the buffer
169 // is nul terminated by virtue of being a MemoryBuffer.
170 return FileBuffer->getBufferStart()+StrTabIdx;
171}
172
173//===----------------------------------------------------------------------===//
174// The Main Drivers
175//===----------------------------------------------------------------------===//
176
177/// dump - Print the contents of this headermap to stderr.
178void HeaderMap::dump() const {
179 const HMapHeader &Hdr = getHeader();
180 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
Mike Stump11289f42009-09-09 15:08:12 +0000181
182 fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
Chris Lattner9f9a6192007-12-17 21:06:11 +0000183 getFileName(), NumBuckets,
184 getEndianAdjustedWord(Hdr.NumEntries));
Mike Stump11289f42009-09-09 15:08:12 +0000185
Chris Lattner9f9a6192007-12-17 21:06:11 +0000186 for (unsigned i = 0; i != NumBuckets; ++i) {
187 HMapBucket B = getBucket(i);
188 if (B.Key == HMAP_EmptyBucketKey) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000189
Chris Lattner9f9a6192007-12-17 21:06:11 +0000190 const char *Key = getString(B.Key);
191 const char *Prefix = getString(B.Prefix);
192 const char *Suffix = getString(B.Suffix);
193 fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
194 }
195}
196
Chris Lattner1587e6d2007-12-17 08:22:46 +0000197/// LookupFile - Check to see if the specified relative filename is located in
198/// this HeaderMap. If so, open it and return its FileEntry.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000199const FileEntry *HeaderMap::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000200 StringRef Filename, FileManager &FM) const {
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000201
202 SmallString<1024> Path;
203 StringRef Dest = lookupFilename(Filename, Path);
204 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000205 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000206
207 return FM.getFile(Dest);
208}
209
210StringRef HeaderMap::lookupFilename(StringRef Filename,
211 SmallVectorImpl<char> &DestPath) const {
Chris Lattner619e18c2007-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))
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000218 return StringRef();
Mike Stump11289f42009-09-09 15:08:12 +0000219
Chris Lattner619e18c2007-12-17 21:38:04 +0000220 // Linearly probe the hash table.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000221 for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
Chris Lattner619e18c2007-12-17 21:38:04 +0000222 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000223 if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
Mike Stump11289f42009-09-09 15:08:12 +0000224
Chris Lattner619e18c2007-12-17 21:38:04 +0000225 // See if the key matches. If not, probe on.
Benjamin Kramer307c2c72010-01-10 09:51:00 +0000226 if (!Filename.equals_lower(getString(B.Key)))
Chris Lattner619e18c2007-12-17 21:38:04 +0000227 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000228
Chris Lattner619e18c2007-12-17 21:38:04 +0000229 // If so, we have a match in the hash table. Construct the destination
230 // path.
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000231 StringRef Prefix = getString(B.Prefix);
232 StringRef Suffix = getString(B.Suffix);
233 DestPath.clear();
234 DestPath.append(Prefix.begin(), Prefix.end());
235 DestPath.append(Suffix.begin(), Suffix.end());
236 return StringRef(DestPath.begin(), DestPath.size());
Chris Lattner619e18c2007-12-17 21:38:04 +0000237 }
Chris Lattner1587e6d2007-12-17 08:22:46 +0000238}