1 //===- unittests/Lex/HeaderMapTest.cpp - HeaderMap tests ----------===//
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
7 //===--------------------------------------------------------------===//
9 #include "HeaderMapTestUtils.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "gtest/gtest.h"
12 #include <type_traits>
14 using namespace clang
;
16 using namespace clang::test
;
20 TEST(HeaderMapTest
, checkHeaderEmpty
) {
22 ASSERT_FALSE(HeaderMapImpl::checkHeader(
23 *MemoryBuffer::getMemBufferCopy("", "empty"), NeedsSwap
));
24 ASSERT_FALSE(HeaderMapImpl::checkHeader(
25 *MemoryBuffer::getMemBufferCopy("", "empty"), NeedsSwap
));
28 TEST(HeaderMapTest
, checkHeaderMagic
) {
29 HMapFileMock
<1, 1> File
;
31 File
.Header
.Magic
= 0;
33 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
36 TEST(HeaderMapTest
, checkHeaderReserved
) {
37 HMapFileMock
<1, 1> File
;
39 File
.Header
.Reserved
= 1;
41 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
44 TEST(HeaderMapTest
, checkHeaderVersion
) {
45 HMapFileMock
<1, 1> File
;
47 ++File
.Header
.Version
;
49 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
52 TEST(HeaderMapTest
, checkHeaderValidButEmpty
) {
53 HMapFileMock
<1, 1> File
;
56 ASSERT_TRUE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
57 ASSERT_FALSE(NeedsSwap
);
60 ASSERT_TRUE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
61 ASSERT_TRUE(NeedsSwap
);
64 TEST(HeaderMapTest
, checkHeader3Buckets
) {
65 HMapFileMock
<3, 1> File
;
66 ASSERT_EQ(3 * sizeof(HMapBucket
), sizeof(File
.Buckets
));
70 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
73 TEST(HeaderMapTest
, checkHeader0Buckets
) {
74 // Create with 1 bucket to avoid 0-sized arrays.
75 HMapFileMock
<1, 1> File
;
77 File
.Header
.NumBuckets
= 0;
79 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
82 TEST(HeaderMapTest
, checkHeaderNotEnoughBuckets
) {
83 HMapFileMock
<1, 1> File
;
85 File
.Header
.NumBuckets
= 8;
87 ASSERT_FALSE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
90 TEST(HeaderMapTest
, lookupFilename
) {
91 typedef HMapFileMock
<2, 7> FileTy
;
95 HMapFileMockMaker
<FileTy
> Maker(File
);
96 auto a
= Maker
.addString("a");
97 auto b
= Maker
.addString("b");
98 auto c
= Maker
.addString("c");
99 Maker
.addBucket("a", a
, b
, c
);
102 ASSERT_TRUE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
103 ASSERT_FALSE(NeedsSwap
);
104 HeaderMapImpl
Map(File
.getBuffer(), NeedsSwap
);
106 SmallString
<8> DestPath
;
107 ASSERT_EQ("bc", Map
.lookupFilename("a", DestPath
));
110 template <class FileTy
, class PaddingTy
> struct PaddedFile
{
115 TEST(HeaderMapTest
, lookupFilenameTruncatedSuffix
) {
116 typedef HMapFileMock
<2, 64 - sizeof(HMapHeader
) - 2 * sizeof(HMapBucket
)>
118 static_assert(std::is_standard_layout_v
<FileTy
>, "Expected standard layout");
119 static_assert(sizeof(FileTy
) == 64, "check the math");
120 PaddedFile
<FileTy
, uint64_t> P
;
122 auto &Padding
= P
.Padding
;
125 HMapFileMockMaker
<FileTy
> Maker(File
);
126 auto a
= Maker
.addString("a");
127 auto b
= Maker
.addString("b");
128 auto c
= Maker
.addString("c");
129 Maker
.addBucket("a", a
, b
, c
);
131 // Add 'x' characters to cause an overflow into Padding.
132 ASSERT_EQ('c', File
.Bytes
[5]);
133 for (unsigned I
= 6; I
< sizeof(File
.Bytes
); ++I
) {
134 ASSERT_EQ(0, File
.Bytes
[I
]);
137 Padding
= 0xffffffff; // Padding won't stop it either.
140 ASSERT_TRUE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
141 ASSERT_FALSE(NeedsSwap
);
142 HeaderMapImpl
Map(File
.getBuffer(), NeedsSwap
);
144 // The string for "c" runs to the end of File. Check that the suffix
145 // ("cxxxx...") is detected as truncated, and an empty string is returned.
146 SmallString
<24> DestPath
;
147 ASSERT_EQ("", Map
.lookupFilename("a", DestPath
));
150 TEST(HeaderMapTest
, lookupFilenameTruncatedPrefix
) {
151 typedef HMapFileMock
<2, 64 - sizeof(HMapHeader
) - 2 * sizeof(HMapBucket
)>
153 static_assert(std::is_standard_layout_v
<FileTy
>, "Expected standard layout");
154 static_assert(sizeof(FileTy
) == 64, "check the math");
155 PaddedFile
<FileTy
, uint64_t> P
;
157 auto &Padding
= P
.Padding
;
160 HMapFileMockMaker
<FileTy
> Maker(File
);
161 auto a
= Maker
.addString("a");
162 auto c
= Maker
.addString("c");
163 auto b
= Maker
.addString("b"); // Store the prefix last.
164 Maker
.addBucket("a", a
, b
, c
);
166 // Add 'x' characters to cause an overflow into Padding.
167 ASSERT_EQ('b', File
.Bytes
[5]);
168 for (unsigned I
= 6; I
< sizeof(File
.Bytes
); ++I
) {
169 ASSERT_EQ(0, File
.Bytes
[I
]);
172 Padding
= 0xffffffff; // Padding won't stop it either.
175 ASSERT_TRUE(HeaderMapImpl::checkHeader(*File
.getBuffer(), NeedsSwap
));
176 ASSERT_FALSE(NeedsSwap
);
177 HeaderMapImpl
Map(File
.getBuffer(), NeedsSwap
);
179 // The string for "b" runs to the end of File. Check that the prefix
180 // ("bxxxx...") is detected as truncated, and an empty string is returned.
181 SmallString
<24> DestPath
;
182 ASSERT_EQ("", Map
.lookupFilename("a", DestPath
));