blob: 67b663158f0b78af1cbaec68200f332387f4fae9 [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"
Duncan P. N. Exon Smithbef47f02016-02-20 23:09:14 +000023#include "llvm/Support/Debug.h"
Duncan P. N. Exon Smith96a01fa2016-02-21 00:14:36 +000024#include <cstring>
Ahmed Charlesdfca6f92014-03-09 11:36:40 +000025#include <memory>
Chris Lattner1587e6d2007-12-17 08:22:46 +000026using namespace clang;
27
Chris Lattner619e18c2007-12-17 21:38:04 +000028/// HashHMapKey - This is the 'well known' hash function required by the file
29/// format, used to look up keys in the hash table. The hash table uses simple
30/// linear probing based on this function.
Chris Lattner0e62c1c2011-07-23 10:55:15 +000031static inline unsigned HashHMapKey(StringRef Str) {
Chris Lattner619e18c2007-12-17 21:38:04 +000032 unsigned Result = 0;
Chris Lattnerd081f8c2010-01-10 01:35:12 +000033 const char *S = Str.begin(), *End = Str.end();
Mike Stump11289f42009-09-09 15:08:12 +000034
Chris Lattner619e18c2007-12-17 21:38:04 +000035 for (; S != End; S++)
Jordan Rose4938f272013-02-09 10:09:43 +000036 Result += toLowercase(*S) * 13;
Chris Lattner619e18c2007-12-17 21:38:04 +000037 return Result;
38}
39
40
41
Chris Lattner9f9a6192007-12-17 21:06:11 +000042//===----------------------------------------------------------------------===//
43// Verification and Construction
44//===----------------------------------------------------------------------===//
Chris Lattner4ffe46c2007-12-17 18:34:53 +000045
46/// HeaderMap::Create - This attempts to load the specified file as a header
47/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
48/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
49/// into the string error argument and returns null.
Chris Lattner5159f612010-11-23 08:35:12 +000050const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM) {
Chris Lattner4ffe46c2007-12-17 18:34:53 +000051 // If the file is too small to be a header map, ignore it.
52 unsigned FileSize = FE->getSize();
Craig Topperd2d442c2014-05-17 23:10:59 +000053 if (FileSize <= sizeof(HMapHeader)) return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +000054
Benjamin Kramera8857962014-10-26 22:44:13 +000055 auto FileBuffer = FM.getBufferForFile(FE);
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000056 if (!FileBuffer || !*FileBuffer)
57 return nullptr;
58 bool NeedsByteSwap;
59 if (!checkHeader(**FileBuffer, NeedsByteSwap))
60 return nullptr;
61 return new HeaderMap(std::move(*FileBuffer), NeedsByteSwap);
62}
63
64bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
65 bool &NeedsByteSwap) {
66 if (File.getBufferSize() <= sizeof(HMapHeader))
67 return false;
68 const char *FileStart = File.getBufferStart();
Chris Lattner4ffe46c2007-12-17 18:34:53 +000069
70 // We know the file is at least as big as the header, check it now.
71 const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
Mike Stump11289f42009-09-09 15:08:12 +000072
Chris Lattnerd39b8c02007-12-17 18:59:44 +000073 // Sniff it to see if it's a headermap by checking the magic number and
74 // version.
Mike Stump11289f42009-09-09 15:08:12 +000075 if (Header->Magic == HMAP_HeaderMagicNumber &&
Chris Lattner9f9a6192007-12-17 21:06:11 +000076 Header->Version == HMAP_HeaderVersion)
Chris Lattner4ffe46c2007-12-17 18:34:53 +000077 NeedsByteSwap = false;
Chris Lattner9f9a6192007-12-17 21:06:11 +000078 else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
79 Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
Chris Lattner4ffe46c2007-12-17 18:34:53 +000080 NeedsByteSwap = true; // Mixed endianness headermap.
Mike Stump11289f42009-09-09 15:08:12 +000081 else
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000082 return false; // Not a header map.
Mike Stump11289f42009-09-09 15:08:12 +000083
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000084 if (Header->Reserved != 0)
85 return false;
Chris Lattner79764a62007-12-17 18:44:09 +000086
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +000087 // Check the number of buckets. It should be a power of two, and there
88 // should be enough space in the file for all of them.
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000089 auto NumBuckets = NeedsByteSwap
90 ? llvm::sys::getSwappedBytes(Header->NumBuckets)
91 : Header->NumBuckets;
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000092 if (NumBuckets & (NumBuckets - 1))
93 return false;
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +000094 if (File.getBufferSize() <
95 sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
96 return false;
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +000097
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +000098 // Okay, everything looks good.
99 return true;
Chris Lattner79764a62007-12-17 18:44:09 +0000100}
101
Chris Lattner9f9a6192007-12-17 21:06:11 +0000102//===----------------------------------------------------------------------===//
103// Utility Methods
104//===----------------------------------------------------------------------===//
105
Chris Lattner79764a62007-12-17 18:44:09 +0000106
107/// getFileName - Return the filename of the headermap.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000108const char *HeaderMapImpl::getFileName() const {
Chris Lattner79764a62007-12-17 18:44:09 +0000109 return FileBuffer->getBufferIdentifier();
Chris Lattner1587e6d2007-12-17 08:22:46 +0000110}
111
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000112unsigned HeaderMapImpl::getEndianAdjustedWord(unsigned X) const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000113 if (!NeedsBSwap) return X;
114 return llvm::ByteSwap_32(X);
115}
116
117/// getHeader - Return a reference to the file header, in unbyte-swapped form.
118/// This method cannot fail.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000119const HMapHeader &HeaderMapImpl::getHeader() const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000120 // We know the file is at least as big as the header. Return it.
121 return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
122}
123
124/// getBucket - Return the specified hash table bucket from the header map,
125/// bswap'ing its fields as appropriate. If the bucket number is not valid,
126/// this return a bucket with an empty key (0).
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000127HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +0000128 assert(FileBuffer->getBufferSize() >=
129 sizeof(HMapHeader) + sizeof(HMapBucket) * BucketNo &&
130 "Expected bucket to be in range");
131
Chris Lattner9f9a6192007-12-17 21:06:11 +0000132 HMapBucket Result;
133 Result.Key = HMAP_EmptyBucketKey;
Mike Stump11289f42009-09-09 15:08:12 +0000134
135 const HMapBucket *BucketArray =
Chris Lattner9f9a6192007-12-17 21:06:11 +0000136 reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
137 sizeof(HMapHeader));
Chris Lattner9f9a6192007-12-17 21:06:11 +0000138 const HMapBucket *BucketPtr = BucketArray+BucketNo;
Chris Lattner9f9a6192007-12-17 21:06:11 +0000139
Duncan P. N. Exon Smith8d6a31c022016-02-20 21:24:31 +0000140 // Load the values, bswapping as needed.
Chris Lattner9f9a6192007-12-17 21:06:11 +0000141 Result.Key = getEndianAdjustedWord(BucketPtr->Key);
142 Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
143 Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
144 return Result;
145}
146
147/// getString - Look up the specified string in the string table. If the string
148/// index is not valid, it returns an empty string.
Duncan P. N. Exon Smith0bbfa432016-02-20 23:12:51 +0000149StringRef HeaderMapImpl::getString(unsigned StrTabIdx) const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000150 // Add the start of the string table to the idx.
151 StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Chris Lattner9f9a6192007-12-17 21:06:11 +0000153 // Check for invalid index.
154 if (StrTabIdx >= FileBuffer->getBufferSize())
Duncan P. N. Exon Smith96a01fa2016-02-21 00:14:36 +0000155 return "";
Mike Stump11289f42009-09-09 15:08:12 +0000156
Duncan P. N. Exon Smith96a01fa2016-02-21 00:14:36 +0000157 const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
158 unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
159 unsigned Len = strnlen(Data, MaxLen);
160
161 // Check whether the buffer is null-terminated.
162 if (Len == MaxLen && Data[Len - 1])
163 return "";
164
165 return StringRef(Data, Len);
Chris Lattner9f9a6192007-12-17 21:06:11 +0000166}
167
168//===----------------------------------------------------------------------===//
169// The Main Drivers
170//===----------------------------------------------------------------------===//
171
172/// dump - Print the contents of this headermap to stderr.
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000173LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
Chris Lattner9f9a6192007-12-17 21:06:11 +0000174 const HMapHeader &Hdr = getHeader();
175 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
Mike Stump11289f42009-09-09 15:08:12 +0000176
Duncan P. N. Exon Smithbef47f02016-02-20 23:09:14 +0000177 llvm::dbgs() << "Header Map " << getFileName() << ":\n " << NumBuckets
178 << ", " << getEndianAdjustedWord(Hdr.NumEntries) << "\n";
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner9f9a6192007-12-17 21:06:11 +0000180 for (unsigned i = 0; i != NumBuckets; ++i) {
181 HMapBucket B = getBucket(i);
182 if (B.Key == HMAP_EmptyBucketKey) continue;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Duncan P. N. Exon Smith0bbfa432016-02-20 23:12:51 +0000184 StringRef Key = getString(B.Key);
185 StringRef Prefix = getString(B.Prefix);
186 StringRef Suffix = getString(B.Suffix);
Duncan P. N. Exon Smithbef47f02016-02-20 23:09:14 +0000187 llvm::dbgs() << " " << i << ". " << Key << " -> '" << Prefix << "' '"
188 << Suffix << "'\n";
Chris Lattner9f9a6192007-12-17 21:06:11 +0000189 }
190}
191
Chris Lattner1587e6d2007-12-17 08:22:46 +0000192/// LookupFile - Check to see if the specified relative filename is located in
193/// this HeaderMap. If so, open it and return its FileEntry.
Chandler Carruth3cc331a2011-03-16 18:34:36 +0000194const FileEntry *HeaderMap::LookupFile(
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000195 StringRef Filename, FileManager &FM) const {
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000196
197 SmallString<1024> Path;
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000198 StringRef Dest = HeaderMapImpl::lookupFilename(Filename, Path);
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000199 if (Dest.empty())
Craig Topperd2d442c2014-05-17 23:10:59 +0000200 return nullptr;
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000201
202 return FM.getFile(Dest);
203}
204
Duncan P. N. Exon Smith9ab99ee2016-02-20 20:39:51 +0000205StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
206 SmallVectorImpl<char> &DestPath) const {
Chris Lattner619e18c2007-12-17 21:38:04 +0000207 const HMapHeader &Hdr = getHeader();
208 unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
209
Duncan P. N. Exon Smithdfe85302016-02-20 21:00:58 +0000210 // Don't probe infinitely. This should be checked before constructing.
211 assert(!(NumBuckets & (NumBuckets - 1)) && "Expected power of 2");
Mike Stump11289f42009-09-09 15:08:12 +0000212
Chris Lattner619e18c2007-12-17 21:38:04 +0000213 // Linearly probe the hash table.
Chris Lattnerd081f8c2010-01-10 01:35:12 +0000214 for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
Chris Lattner619e18c2007-12-17 21:38:04 +0000215 HMapBucket B = getBucket(Bucket & (NumBuckets-1));
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000216 if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
Mike Stump11289f42009-09-09 15:08:12 +0000217
Chris Lattner619e18c2007-12-17 21:38:04 +0000218 // See if the key matches. If not, probe on.
Benjamin Kramer307c2c72010-01-10 09:51:00 +0000219 if (!Filename.equals_lower(getString(B.Key)))
Chris Lattner619e18c2007-12-17 21:38:04 +0000220 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000221
Chris Lattner619e18c2007-12-17 21:38:04 +0000222 // If so, we have a match in the hash table. Construct the destination
223 // path.
Argyrios Kyrtzidis75fa9ed2014-02-14 14:58:28 +0000224 StringRef Prefix = getString(B.Prefix);
225 StringRef Suffix = getString(B.Suffix);
226 DestPath.clear();
227 DestPath.append(Prefix.begin(), Prefix.end());
228 DestPath.append(Suffix.begin(), Suffix.end());
229 return StringRef(DestPath.begin(), DestPath.size());
Chris Lattner619e18c2007-12-17 21:38:04 +0000230 }
Chris Lattner1587e6d2007-12-17 08:22:46 +0000231}