blob: 220e70d8d589e95d571c3ee986fa5e604b8c1a82 [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"
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000015#include "clang/Lex/HeaderMapTypes.h"
Jordan Rose4938f272013-02-09 10:09:43 +000016#include "clang/Basic/CharInfo.h"
Chris Lattner4ffe46c2007-12-17 18:34:53 +000017#include "clang/Basic/FileManager.h"
Chris Lattner619e18c2007-12-17 21:38:04 +000018#include "llvm/ADT/SmallString.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000019#include "llvm/Support/DataTypes.h"
Chris Lattner4ffe46c2007-12-17 18:34:53 +000020#include "llvm/Support/MathExtras.h"
21#include "llvm/Support/MemoryBuffer.h"
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000022#include "llvm/Support/SwapByteOrder.h"
Chris Lattnerc25d8a72009-03-02 22:20:04 +000023#include <cstdio>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000024#include <memory>
Chris Lattner1587e6d2007-12-17 08:22:46 +000025using namespace clang;
26
Chris Lattner619e18c2007-12-17 21:38:04 +000027/// HashHMapKey - This is the 'well known' hash function required by the file
28/// format, used to look up keys in the hash table. The hash table uses simple
29/// linear probing based on this function.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000030static inline unsigned HashHMapKey(StringRef Str) {
Chris Lattner619e18c2007-12-17 21:38:04 +000031 unsigned Result = 0;
Chris Lattnerd081f8c2010-01-10 01:35:12 +000032 const char *S = Str.begin(), *End = Str.end();
Mike Stump11289f42009-09-09 15:08:12 +000033
Chris Lattner619e18c2007-12-17 21:38:04 +000034 for (; S != End; S++)
Jordan Rose4938f272013-02-09 10:09:43 +000035 Result += toLowercase(*S) * 13;
Chris Lattner619e18c2007-12-17 21:38:04 +000036 return Result;
37}
38
39
40
Chris Lattner9f9a6192007-12-17 21:06:11 +000041//===----------------------------------------------------------------------===//
42// Verification and Construction
43//===----------------------------------------------------------------------===//
Chris Lattner4ffe46c2007-12-17 18:34:53 +000044
45/// HeaderMap::Create - This attempts to load the specified file as a header
46/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
47/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
48/// into the string error argument and returns null.
Chris Lattner5159f612010-11-23 08:35:12 +000049const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
Chris Lattner4ffe46c2007-12-17 18:34:53 +000050 // If the file is too small to be a header map, ignore it.
51 unsigned FileSize = FE->getSize();
Craig Topperd2d442c2014-05-17 23:10:59 +000052 if (FileSize <= sizeof(HMapHeader)) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000053
Benjamin Kramera8857962014-10-26 22:44:13 +000054 auto FileBuffer = FM.getBufferForFile(FE);
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000055 if (!FileBuffer || !*FileBuffer)
56 return nullptr;
57 bool NeedsByteSwap;
58 if (!checkHeader(**FileBuffer, NeedsByteSwap))
59 return nullptr;
60 return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap);
61}
62
63bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
64 bool &NeedsByteSwap) {
65 if (File.getBufferSize() <= sizeof(HMapHeader))
66 return false;
67 const char *FileStart = File.getBufferStart();
Chris Lattner4ffe46c2007-12-17 18:34:53 +000068
69 // We know the file is at least as big as the header, check it now.
70 const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerd39b8c02007-12-17 18:59:44 +000072 // Sniff it to see if it's a headermap by checking the magic number and
73 // version.
Mike Stump11289f42009-09-09 15:08:12 +000074 if (Header->Magic == HMAP_HeaderMagicNumber &&
Chris Lattner9f9a6192007-12-17 21:06:11 +000075 Header->Version == HMAP_HeaderVersion)
Chris Lattner4ffe46c2007-12-17 18:34:53 +000076 NeedsByteSwap = false;
Chris Lattner9f9a6192007-12-17 21:06:11 +000077 else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
78 Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
Chris Lattner4ffe46c2007-12-17 18:34:53 +000079 NeedsByteSwap = true; // Mixed endianness headermap.
Mike Stump11289f42009-09-09 15:08:12 +000080 else
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000081 return false; // Not a header map.
Mike Stump11289f42009-09-09 15:08:12 +000082
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000083 if (Header->Reserved != 0)
84 return false;
Chris Lattner79764a62007-12-17 18:44:09 +000085
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +000086 // Check the number of buckets. It should be a power of two, and there
87 // should be enough space in the file for all of them.
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000088 auto NumBuckets = NeedsByteSwap
89 ? llvm::sys::getSwappedBytes(Header->NumBuckets)
90 : Header->NumBuckets;
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000091 if (NumBuckets & (NumBuckets - 1))
92 return false;
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +000093 if (File.getBufferSize() <
94 sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
95 return false;
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000096
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000097 // Okay, everything looks good.
98 return true;
Chris Lattner79764a62007-12-17 18:44:09 +000099}
100
Chris Lattner9f9a6192007-12-17 21:06:11 +0000101//===----------------------------------------------------------------------===//
102// Utility Methods
103//===----------------------------------------------------------------------===//
104
Chris Lattner79764a62007-12-17 18:44:09 +0000105
106/// getFileName - Return the filename of the headermap.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000107const char *HeaderMapImpl::getFileName() const {
Chris Lattner79764a62007-12-17 18:44:09 +0000108 return FileBuffer->getBufferIdentifier();
Chris Lattner1587e6d2007-12-17 08:22:46 +0000109}
110
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000111unsigned HeaderMapImpl::getEndianAdjustedWord(unsigned X) const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000112 if (!NeedsBSwap) return X;
113 return llvm::ByteSwap_32(X);
114}
115
116/// getHeader - Return a reference to the file header, in unbyte-swapped form.
117/// This method cannot fail.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000118const HMapHeader &HeaderMapImpl::getHeader() const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000119 // We know the file is at least as big as the header. Return it.
120 return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
121}
122
123/// getBucket - Return the specified hash table bucket from the header map,
124/// bswap'ing its fields as appropriate. If the bucket number is not valid,
125/// this return a bucket with an empty key (0).
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000126HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +0000127 assert(FileBuffer->getBufferSize() >=
128 sizeof(HMapHeader) + sizeof(HMapBucket) * BucketNo &&
129 "Expected bucket to be in range");
130
Chris Lattner9f9a6192007-12-17 21:06:11 +0000131 HMapBucket Result;
132 Result.Key = HMAP_EmptyBucketKey;
Mike Stump11289f42009-09-09 15:08:12 +0000133
134 const HMapBucket *BucketArray =
Chris Lattner9f9a6192007-12-17 21:06:11 +0000135 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
136 sizeof(HMapHeader));
Chris Lattner9f9a6192007-12-17 21:06:11 +0000137 const HMapBucket *BucketPtr = BucketArray+BucketNo;
Chris Lattner9f9a6192007-12-17 21:06:11 +0000138
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +0000139 // Load the values, bswapping as needed.
Chris Lattner9f9a6192007-12-17 21:06:11 +0000140 Result.Key = getEndianAdjustedWord(BucketPtr->Key);
141 Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
142 Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
143 return Result;
144}
145
146/// getString - Look up the specified string in the string table. If the string
147/// index is not valid, it returns an empty string.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000148const char *HeaderMapImpl::getString(unsigned StrTabIdx) const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000149 // Add the start of the string table to the idx.
150 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000151
Chris Lattner9f9a6192007-12-17 21:06:11 +0000152 // Check for invalid index.
153 if (StrTabIdx >= FileBuffer->getBufferSize())
Craig Topperd2d442c2014-05-17 23:10:59 +0000154 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +0000155
Chris Lattner9f9a6192007-12-17 21:06:11 +0000156 // Otherwise, we have a valid pointer into the file. Just return it. We know
157 // that the "string" can not overrun the end of the file, because the buffer
158 // is nul terminated by virtue of being a MemoryBuffer.
159 return FileBuffer->getBufferStart()+StrTabIdx;
160}
161
162//===----------------------------------------------------------------------===//
163// The Main Drivers
164//===----------------------------------------------------------------------===//
165
166/// dump - Print the contents of this headermap to stderr.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000167LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000168 const HMapHeader &Hdr = getHeader();
169 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
Mike Stump11289f42009-09-09 15:08:12 +0000170
171 fprintf(stderr, "Header Map %s:\n %d buckets, %d entries\n",
Chris Lattner9f9a6192007-12-17 21:06:11 +0000172 getFileName(), NumBuckets,
173 getEndianAdjustedWord(Hdr.NumEntries));
Mike Stump11289f42009-09-09 15:08:12 +0000174
Chris Lattner9f9a6192007-12-17 21:06:11 +0000175 for (unsigned i = 0; i != NumBuckets; ++i) {
176 HMapBucket B = getBucket(i);
177 if (B.Key == HMAP_EmptyBucketKey) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner9f9a6192007-12-17 21:06:11 +0000179 const char *Key = getString(B.Key);
180 const char *Prefix = getString(B.Prefix);
181 const char *Suffix = getString(B.Suffix);
182 fprintf(stderr, " %d. %s -> '%s' '%s'\n", i, Key, Prefix, Suffix);
183 }
184}
185
Chris Lattner1587e6d2007-12-17 08:22:46 +0000186/// LookupFile - Check to see if the specified relative filename is located in
187/// this HeaderMap. If so, open it and return its FileEntry.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000188const FileEntry *HeaderMap::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000189 StringRef Filename, FileManager &FM) const {
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000190
191 SmallString<1024> Path;
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000192 StringRef Dest = HeaderMapImpl::lookupFilename(Filename, Path);
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000193 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000194 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000195
196 return FM.getFile(Dest);
197}
198
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000199StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
200 SmallVectorImpl<char> &DestPath) const {
Chris Lattner619e18c2007-12-17 21:38:04 +0000201 const HMapHeader &Hdr = getHeader();
202 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
203
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +0000204 // Don't probe infinitely. This should be checked before constructing.
205 assert(!(NumBuckets & (NumBuckets - 1)) && "Expected power of 2");
Mike Stump11289f42009-09-09 15:08:12 +0000206
Chris Lattner619e18c2007-12-17 21:38:04 +0000207 // Linearly probe the hash table.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000208 for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
Chris Lattner619e18c2007-12-17 21:38:04 +0000209 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000210 if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
Mike Stump11289f42009-09-09 15:08:12 +0000211
Chris Lattner619e18c2007-12-17 21:38:04 +0000212 // See if the key matches. If not, probe on.
Benjamin Kramer307c2c72010-01-10 09:51:00 +0000213 if (!Filename.equals_lower(getString(B.Key)))
Chris Lattner619e18c2007-12-17 21:38:04 +0000214 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000215
Chris Lattner619e18c2007-12-17 21:38:04 +0000216 // If so, we have a match in the hash table. Construct the destination
217 // path.
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000218 StringRef Prefix = getString(B.Prefix);
219 StringRef Suffix = getString(B.Suffix);
220 DestPath.clear();
221 DestPath.append(Prefix.begin(), Prefix.end());
222 DestPath.append(Suffix.begin(), Suffix.end());
223 return StringRef(DestPath.begin(), DestPath.size());
Chris Lattner619e18c2007-12-17 21:38:04 +0000224 }
Chris Lattner1587e6d2007-12-17 08:22:46 +0000225}