| 1 |
//===- unittests/Lex/HeaderMapTestUtils.h - HeaderMap utils -------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===--------------------------------------------------------------===// |
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_UNITTESTS_LEX_HEADERMAPTESTUTILS_H |
| 10 |
#define LLVM_CLANG_UNITTESTS_LEX_HEADERMAPTESTUTILS_H |
| 11 |
|
| 12 |
#include "clang/Basic/CharInfo.h" |
| 13 |
#include "clang/Lex/HeaderMap.h" |
| 14 |
#include "clang/Lex/HeaderMapTypes.h" |
| 15 |
#include "llvm/Support/SwapByteOrder.h" |
| 16 |
#include <cassert> |
| 17 |
|
| 18 |
namespace clang { |
| 19 |
namespace test { |
| 20 |
|
| 21 |
// Lay out a header file for testing. |
| 22 |
template <unsigned NumBuckets, unsigned NumBytes> struct HMapFileMock { |
| 23 |
HMapHeader Header; |
| 24 |
HMapBucket Buckets[NumBuckets]; |
| 25 |
unsigned char Bytes[NumBytes]; |
| 26 |
|
| 27 |
void init() { |
| 28 |
memset(this, 0, sizeof(HMapFileMock)); |
| 29 |
Header.Magic = HMAP_HeaderMagicNumber; |
| 30 |
Header.Version = HMAP_HeaderVersion; |
| 31 |
Header.NumBuckets = NumBuckets; |
| 32 |
Header.StringsOffset = sizeof(Header) + sizeof(Buckets); |
| 33 |
} |
| 34 |
|
| 35 |
void swapBytes() { |
| 36 |
using llvm::sys::getSwappedBytes; |
| 37 |
Header.Magic = getSwappedBytes(Header.Magic); |
| 38 |
Header.Version = getSwappedBytes(Header.Version); |
| 39 |
Header.NumBuckets = getSwappedBytes(Header.NumBuckets); |
| 40 |
Header.StringsOffset = getSwappedBytes(Header.StringsOffset); |
| 41 |
} |
| 42 |
|
| 43 |
std::unique_ptr<llvm::MemoryBuffer> getBuffer() { |
| 44 |
return llvm::MemoryBuffer::getMemBuffer( |
| 45 |
StringRef(reinterpret_cast<char *>(this), sizeof(HMapFileMock)), |
| 46 |
"header", |
| 47 |
/* RequresNullTerminator */ false); |
| 48 |
} |
| 49 |
}; |
| 50 |
|
| 51 |
template <class FileTy> struct HMapFileMockMaker { |
| 52 |
FileTy &File; |
| 53 |
unsigned SI = 1; |
| 54 |
unsigned BI = 0; |
| 55 |
HMapFileMockMaker(FileTy &File) : File(File) {} |
| 56 |
|
| 57 |
unsigned addString(StringRef S) { |
| 58 |
assert(SI + S.size() + 1 <= sizeof(File.Bytes)); |
| 59 |
std::copy(S.begin(), S.end(), File.Bytes + SI); |
| 60 |
auto OldSI = SI; |
| 61 |
SI += S.size() + 1; |
| 62 |
return OldSI; |
| 63 |
} |
| 64 |
|
| 65 |
void addBucket(StringRef Str, unsigned Key, unsigned Prefix, |
| 66 |
unsigned Suffix) { |
| 67 |
addBucket(getHash(Str), Key, Prefix, Suffix); |
| 68 |
} |
| 69 |
|
| 70 |
void addBucket(unsigned Hash, unsigned Key, unsigned Prefix, |
| 71 |
unsigned Suffix) { |
| 72 |
assert(!(File.Header.NumBuckets & (File.Header.NumBuckets - 1))); |
| 73 |
unsigned I = Hash & (File.Header.NumBuckets - 1); |
| 74 |
do { |
| 75 |
if (!File.Buckets[I].Key) { |
| 76 |
File.Buckets[I].Key = Key; |
| 77 |
File.Buckets[I].Prefix = Prefix; |
| 78 |
File.Buckets[I].Suffix = Suffix; |
| 79 |
++File.Header.NumEntries; |
| 80 |
return; |
| 81 |
} |
| 82 |
++I; |
| 83 |
I &= File.Header.NumBuckets - 1; |
| 84 |
} while (I != (Hash & (File.Header.NumBuckets - 1))); |
| 85 |
llvm_unreachable("no empty buckets"); |
| 86 |
} |
| 87 |
|
| 88 |
// The header map hash function. |
| 89 |
static unsigned getHash(StringRef Str) { |
| 90 |
unsigned Result = 0; |
| 91 |
for (char C : Str) |
| 92 |
Result += toLowercase(C) * 13; |
| 93 |
return Result; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} // namespace test |
| 98 |
} // namespace clang |
| 99 |
|
| 100 |
#endif |