1*7330f729Sjoerg //===--- HeaderMap.cpp - A file that acts like dir of symlinks ------------===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg //
9*7330f729Sjoerg // This file implements the HeaderMap interface.
10*7330f729Sjoerg //
11*7330f729Sjoerg //===----------------------------------------------------------------------===//
12*7330f729Sjoerg
13*7330f729Sjoerg #include "clang/Lex/HeaderMap.h"
14*7330f729Sjoerg #include "clang/Lex/HeaderMapTypes.h"
15*7330f729Sjoerg #include "clang/Basic/CharInfo.h"
16*7330f729Sjoerg #include "clang/Basic/FileManager.h"
17*7330f729Sjoerg #include "llvm/ADT/SmallString.h"
18*7330f729Sjoerg #include "llvm/Support/Compiler.h"
19*7330f729Sjoerg #include "llvm/Support/DataTypes.h"
20*7330f729Sjoerg #include "llvm/Support/MathExtras.h"
21*7330f729Sjoerg #include "llvm/Support/MemoryBuffer.h"
22*7330f729Sjoerg #include "llvm/Support/SwapByteOrder.h"
23*7330f729Sjoerg #include "llvm/Support/Debug.h"
24*7330f729Sjoerg #include <cstring>
25*7330f729Sjoerg #include <memory>
26*7330f729Sjoerg using namespace clang;
27*7330f729Sjoerg
28*7330f729Sjoerg /// HashHMapKey - This is the 'well known' hash function required by the file
29*7330f729Sjoerg /// format, used to look up keys in the hash table. The hash table uses simple
30*7330f729Sjoerg /// linear probing based on this function.
HashHMapKey(StringRef Str)31*7330f729Sjoerg static inline unsigned HashHMapKey(StringRef Str) {
32*7330f729Sjoerg unsigned Result = 0;
33*7330f729Sjoerg const char *S = Str.begin(), *End = Str.end();
34*7330f729Sjoerg
35*7330f729Sjoerg for (; S != End; S++)
36*7330f729Sjoerg Result += toLowercase(*S) * 13;
37*7330f729Sjoerg return Result;
38*7330f729Sjoerg }
39*7330f729Sjoerg
40*7330f729Sjoerg
41*7330f729Sjoerg
42*7330f729Sjoerg //===----------------------------------------------------------------------===//
43*7330f729Sjoerg // Verification and Construction
44*7330f729Sjoerg //===----------------------------------------------------------------------===//
45*7330f729Sjoerg
46*7330f729Sjoerg /// HeaderMap::Create - This attempts to load the specified file as a header
47*7330f729Sjoerg /// map. If it doesn't look like a HeaderMap, it gives up and returns null.
48*7330f729Sjoerg /// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
49*7330f729Sjoerg /// into the string error argument and returns null.
Create(const FileEntry * FE,FileManager & FM)50*7330f729Sjoerg std::unique_ptr<HeaderMap> HeaderMap::Create(const FileEntry *FE,
51*7330f729Sjoerg FileManager &FM) {
52*7330f729Sjoerg // If the file is too small to be a header map, ignore it.
53*7330f729Sjoerg unsigned FileSize = FE->getSize();
54*7330f729Sjoerg if (FileSize <= sizeof(HMapHeader)) return nullptr;
55*7330f729Sjoerg
56*7330f729Sjoerg auto FileBuffer = FM.getBufferForFile(FE);
57*7330f729Sjoerg if (!FileBuffer || !*FileBuffer)
58*7330f729Sjoerg return nullptr;
59*7330f729Sjoerg bool NeedsByteSwap;
60*7330f729Sjoerg if (!checkHeader(**FileBuffer, NeedsByteSwap))
61*7330f729Sjoerg return nullptr;
62*7330f729Sjoerg return std::unique_ptr<HeaderMap>(new HeaderMap(std::move(*FileBuffer), NeedsByteSwap));
63*7330f729Sjoerg }
64*7330f729Sjoerg
checkHeader(const llvm::MemoryBuffer & File,bool & NeedsByteSwap)65*7330f729Sjoerg bool HeaderMapImpl::checkHeader(const llvm::MemoryBuffer &File,
66*7330f729Sjoerg bool &NeedsByteSwap) {
67*7330f729Sjoerg if (File.getBufferSize() <= sizeof(HMapHeader))
68*7330f729Sjoerg return false;
69*7330f729Sjoerg const char *FileStart = File.getBufferStart();
70*7330f729Sjoerg
71*7330f729Sjoerg // We know the file is at least as big as the header, check it now.
72*7330f729Sjoerg const HMapHeader *Header = reinterpret_cast<const HMapHeader*>(FileStart);
73*7330f729Sjoerg
74*7330f729Sjoerg // Sniff it to see if it's a headermap by checking the magic number and
75*7330f729Sjoerg // version.
76*7330f729Sjoerg if (Header->Magic == HMAP_HeaderMagicNumber &&
77*7330f729Sjoerg Header->Version == HMAP_HeaderVersion)
78*7330f729Sjoerg NeedsByteSwap = false;
79*7330f729Sjoerg else if (Header->Magic == llvm::ByteSwap_32(HMAP_HeaderMagicNumber) &&
80*7330f729Sjoerg Header->Version == llvm::ByteSwap_16(HMAP_HeaderVersion))
81*7330f729Sjoerg NeedsByteSwap = true; // Mixed endianness headermap.
82*7330f729Sjoerg else
83*7330f729Sjoerg return false; // Not a header map.
84*7330f729Sjoerg
85*7330f729Sjoerg if (Header->Reserved != 0)
86*7330f729Sjoerg return false;
87*7330f729Sjoerg
88*7330f729Sjoerg // Check the number of buckets. It should be a power of two, and there
89*7330f729Sjoerg // should be enough space in the file for all of them.
90*7330f729Sjoerg uint32_t NumBuckets = NeedsByteSwap
91*7330f729Sjoerg ? llvm::sys::getSwappedBytes(Header->NumBuckets)
92*7330f729Sjoerg : Header->NumBuckets;
93*7330f729Sjoerg if (!llvm::isPowerOf2_32(NumBuckets))
94*7330f729Sjoerg return false;
95*7330f729Sjoerg if (File.getBufferSize() <
96*7330f729Sjoerg sizeof(HMapHeader) + sizeof(HMapBucket) * NumBuckets)
97*7330f729Sjoerg return false;
98*7330f729Sjoerg
99*7330f729Sjoerg // Okay, everything looks good.
100*7330f729Sjoerg return true;
101*7330f729Sjoerg }
102*7330f729Sjoerg
103*7330f729Sjoerg //===----------------------------------------------------------------------===//
104*7330f729Sjoerg // Utility Methods
105*7330f729Sjoerg //===----------------------------------------------------------------------===//
106*7330f729Sjoerg
107*7330f729Sjoerg
108*7330f729Sjoerg /// getFileName - Return the filename of the headermap.
getFileName() const109*7330f729Sjoerg StringRef HeaderMapImpl::getFileName() const {
110*7330f729Sjoerg return FileBuffer->getBufferIdentifier();
111*7330f729Sjoerg }
112*7330f729Sjoerg
getEndianAdjustedWord(unsigned X) const113*7330f729Sjoerg unsigned HeaderMapImpl::getEndianAdjustedWord(unsigned X) const {
114*7330f729Sjoerg if (!NeedsBSwap) return X;
115*7330f729Sjoerg return llvm::ByteSwap_32(X);
116*7330f729Sjoerg }
117*7330f729Sjoerg
118*7330f729Sjoerg /// getHeader - Return a reference to the file header, in unbyte-swapped form.
119*7330f729Sjoerg /// This method cannot fail.
getHeader() const120*7330f729Sjoerg const HMapHeader &HeaderMapImpl::getHeader() const {
121*7330f729Sjoerg // We know the file is at least as big as the header. Return it.
122*7330f729Sjoerg return *reinterpret_cast<const HMapHeader*>(FileBuffer->getBufferStart());
123*7330f729Sjoerg }
124*7330f729Sjoerg
125*7330f729Sjoerg /// getBucket - Return the specified hash table bucket from the header map,
126*7330f729Sjoerg /// bswap'ing its fields as appropriate. If the bucket number is not valid,
127*7330f729Sjoerg /// this return a bucket with an empty key (0).
getBucket(unsigned BucketNo) const128*7330f729Sjoerg HMapBucket HeaderMapImpl::getBucket(unsigned BucketNo) const {
129*7330f729Sjoerg assert(FileBuffer->getBufferSize() >=
130*7330f729Sjoerg sizeof(HMapHeader) + sizeof(HMapBucket) * BucketNo &&
131*7330f729Sjoerg "Expected bucket to be in range");
132*7330f729Sjoerg
133*7330f729Sjoerg HMapBucket Result;
134*7330f729Sjoerg Result.Key = HMAP_EmptyBucketKey;
135*7330f729Sjoerg
136*7330f729Sjoerg const HMapBucket *BucketArray =
137*7330f729Sjoerg reinterpret_cast<const HMapBucket*>(FileBuffer->getBufferStart() +
138*7330f729Sjoerg sizeof(HMapHeader));
139*7330f729Sjoerg const HMapBucket *BucketPtr = BucketArray+BucketNo;
140*7330f729Sjoerg
141*7330f729Sjoerg // Load the values, bswapping as needed.
142*7330f729Sjoerg Result.Key = getEndianAdjustedWord(BucketPtr->Key);
143*7330f729Sjoerg Result.Prefix = getEndianAdjustedWord(BucketPtr->Prefix);
144*7330f729Sjoerg Result.Suffix = getEndianAdjustedWord(BucketPtr->Suffix);
145*7330f729Sjoerg return Result;
146*7330f729Sjoerg }
147*7330f729Sjoerg
getString(unsigned StrTabIdx) const148*7330f729Sjoerg Optional<StringRef> HeaderMapImpl::getString(unsigned StrTabIdx) const {
149*7330f729Sjoerg // Add the start of the string table to the idx.
150*7330f729Sjoerg StrTabIdx += getEndianAdjustedWord(getHeader().StringsOffset);
151*7330f729Sjoerg
152*7330f729Sjoerg // Check for invalid index.
153*7330f729Sjoerg if (StrTabIdx >= FileBuffer->getBufferSize())
154*7330f729Sjoerg return None;
155*7330f729Sjoerg
156*7330f729Sjoerg const char *Data = FileBuffer->getBufferStart() + StrTabIdx;
157*7330f729Sjoerg unsigned MaxLen = FileBuffer->getBufferSize() - StrTabIdx;
158*7330f729Sjoerg unsigned Len = strnlen(Data, MaxLen);
159*7330f729Sjoerg
160*7330f729Sjoerg // Check whether the buffer is null-terminated.
161*7330f729Sjoerg if (Len == MaxLen && Data[Len - 1])
162*7330f729Sjoerg return None;
163*7330f729Sjoerg
164*7330f729Sjoerg return StringRef(Data, Len);
165*7330f729Sjoerg }
166*7330f729Sjoerg
167*7330f729Sjoerg //===----------------------------------------------------------------------===//
168*7330f729Sjoerg // The Main Drivers
169*7330f729Sjoerg //===----------------------------------------------------------------------===//
170*7330f729Sjoerg
171*7330f729Sjoerg /// dump - Print the contents of this headermap to stderr.
dump() const172*7330f729Sjoerg LLVM_DUMP_METHOD void HeaderMapImpl::dump() const {
173*7330f729Sjoerg const HMapHeader &Hdr = getHeader();
174*7330f729Sjoerg unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
175*7330f729Sjoerg
176*7330f729Sjoerg llvm::dbgs() << "Header Map " << getFileName() << ":\n " << NumBuckets
177*7330f729Sjoerg << ", " << getEndianAdjustedWord(Hdr.NumEntries) << "\n";
178*7330f729Sjoerg
179*7330f729Sjoerg auto getStringOrInvalid = [this](unsigned Id) -> StringRef {
180*7330f729Sjoerg if (Optional<StringRef> S = getString(Id))
181*7330f729Sjoerg return *S;
182*7330f729Sjoerg return "<invalid>";
183*7330f729Sjoerg };
184*7330f729Sjoerg
185*7330f729Sjoerg for (unsigned i = 0; i != NumBuckets; ++i) {
186*7330f729Sjoerg HMapBucket B = getBucket(i);
187*7330f729Sjoerg if (B.Key == HMAP_EmptyBucketKey) continue;
188*7330f729Sjoerg
189*7330f729Sjoerg StringRef Key = getStringOrInvalid(B.Key);
190*7330f729Sjoerg StringRef Prefix = getStringOrInvalid(B.Prefix);
191*7330f729Sjoerg StringRef Suffix = getStringOrInvalid(B.Suffix);
192*7330f729Sjoerg llvm::dbgs() << " " << i << ". " << Key << " -> '" << Prefix << "' '"
193*7330f729Sjoerg << Suffix << "'\n";
194*7330f729Sjoerg }
195*7330f729Sjoerg }
196*7330f729Sjoerg
197*7330f729Sjoerg /// LookupFile - Check to see if the specified relative filename is located in
198*7330f729Sjoerg /// this HeaderMap. If so, open it and return its FileEntry.
LookupFile(StringRef Filename,FileManager & FM) const199*7330f729Sjoerg Optional<FileEntryRef> HeaderMap::LookupFile(StringRef Filename,
200*7330f729Sjoerg FileManager &FM) const {
201*7330f729Sjoerg
202*7330f729Sjoerg SmallString<1024> Path;
203*7330f729Sjoerg StringRef Dest = HeaderMapImpl::lookupFilename(Filename, Path);
204*7330f729Sjoerg if (Dest.empty())
205*7330f729Sjoerg return None;
206*7330f729Sjoerg
207*7330f729Sjoerg return FM.getOptionalFileRef(Dest);
208*7330f729Sjoerg }
209*7330f729Sjoerg
lookupFilename(StringRef Filename,SmallVectorImpl<char> & DestPath) const210*7330f729Sjoerg StringRef HeaderMapImpl::lookupFilename(StringRef Filename,
211*7330f729Sjoerg SmallVectorImpl<char> &DestPath) const {
212*7330f729Sjoerg const HMapHeader &Hdr = getHeader();
213*7330f729Sjoerg unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
214*7330f729Sjoerg
215*7330f729Sjoerg // Don't probe infinitely. This should be checked before constructing.
216*7330f729Sjoerg assert(llvm::isPowerOf2_32(NumBuckets) && "Expected power of 2");
217*7330f729Sjoerg
218*7330f729Sjoerg // Linearly probe the hash table.
219*7330f729Sjoerg for (unsigned Bucket = HashHMapKey(Filename);; ++Bucket) {
220*7330f729Sjoerg HMapBucket B = getBucket(Bucket & (NumBuckets-1));
221*7330f729Sjoerg if (B.Key == HMAP_EmptyBucketKey) return StringRef(); // Hash miss.
222*7330f729Sjoerg
223*7330f729Sjoerg // See if the key matches. If not, probe on.
224*7330f729Sjoerg Optional<StringRef> Key = getString(B.Key);
225*7330f729Sjoerg if (LLVM_UNLIKELY(!Key))
226*7330f729Sjoerg continue;
227*7330f729Sjoerg if (!Filename.equals_lower(*Key))
228*7330f729Sjoerg continue;
229*7330f729Sjoerg
230*7330f729Sjoerg // If so, we have a match in the hash table. Construct the destination
231*7330f729Sjoerg // path.
232*7330f729Sjoerg Optional<StringRef> Prefix = getString(B.Prefix);
233*7330f729Sjoerg Optional<StringRef> Suffix = getString(B.Suffix);
234*7330f729Sjoerg
235*7330f729Sjoerg DestPath.clear();
236*7330f729Sjoerg if (LLVM_LIKELY(Prefix && Suffix)) {
237*7330f729Sjoerg DestPath.append(Prefix->begin(), Prefix->end());
238*7330f729Sjoerg DestPath.append(Suffix->begin(), Suffix->end());
239*7330f729Sjoerg }
240*7330f729Sjoerg return StringRef(DestPath.begin(), DestPath.size());
241*7330f729Sjoerg }
242*7330f729Sjoerg }
243