Debian Codegraph @

llvm/unittests/Support/SpecialCaseListTest.cpp

raw
1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
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 #include "llvm/Support/SpecialCaseList.h"
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "llvm/Support/VirtualFileSystem.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15
16 using testing::HasSubstr;
17 using testing::Pair;
18 using testing::StartsWith;
19 using namespace llvm;
20
21 namespace {
22
23 class SpecialCaseListTest : public ::testing::Test {
24 protected:
25 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
26 std::string &Error,
27 bool UseGlobs = true) {
28 auto S = List.str();
29 if (!UseGlobs)
30 S = (Twine("#!special-case-list-v1\n") + S).str();
31 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(S);
32 return SpecialCaseList::create(MB.get(), Error);
33 }
34
35 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
36 bool UseGlobs = true) {
37 std::string Error;
38 auto SCL = makeSpecialCaseList(List, Error, UseGlobs);
39 assert(SCL);
40 assert(Error == "");
41 return SCL;
42 }
43
44 std::string makeSpecialCaseListFile(StringRef Contents,
45 bool UseGlobs = true) {
46 int FD;
47 SmallString<64> Path;
48 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
49 raw_fd_ostream OF(FD, true, true);
50 if (!UseGlobs)
51 OF << "#!special-case-list-v1\n";
52 OF << Contents;
53 OF.close();
54 return std::string(Path.str());
55 }
56 };
57
58 TEST_F(SpecialCaseListTest, Basic) {
59 std::unique_ptr<SpecialCaseList> SCL =
60 makeSpecialCaseList("# This is a comment.\n"
61 "\n"
62 "src:hello\n"
63 "src:bye\n"
64 "src:hi=category\n"
65 "src:z*=category\n");
66 EXPECT_TRUE(SCL->inSection("", "src", "hello"));
67 EXPECT_TRUE(SCL->inSection("", "src", "bye"));
68 EXPECT_TRUE(SCL->inSection("", "src", "hi", "category"));
69 EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category"));
70 EXPECT_FALSE(SCL->inSection("", "src", "hi"));
71 EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
72 EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));
73
74 EXPECT_THAT(SCL->inSectionBlame("", "src", "hello"), Pair(0u, 3u));
75 EXPECT_THAT(SCL->inSectionBlame("", "src", "bye"), Pair(0u, 4u));
76 EXPECT_THAT(SCL->inSectionBlame("", "src", "hi", "category"), Pair(0u, 5u));
77 EXPECT_THAT(SCL->inSectionBlame("", "src", "zzzz", "category"), Pair(0u, 6u));
78 EXPECT_THAT(SCL->inSectionBlame("", "src", "hi"), Pair(0u, 0u));
79 EXPECT_THAT(SCL->inSectionBlame("", "fun", "hello"), Pair(0u, 0u));
80 EXPECT_THAT(SCL->inSectionBlame("", "src", "hello", "category"),
81 Pair(0u, 0u));
82 }
83
84 TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) {
85 std::string Error;
86 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n"
87 "\n"
88 "[not valid\n",
89 Error));
90 EXPECT_THAT(Error, StartsWith("malformed section header on line 3:"));
91
92 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n"
93 "[not valid\n",
94 Error));
95 EXPECT_THAT(Error, StartsWith("malformed section header on line 4:"));
96 }
97
98 TEST_F(SpecialCaseListTest, SectionGlobErrorHandling) {
99 std::string Error;
100 EXPECT_EQ(makeSpecialCaseList("[address", Error), nullptr);
101 EXPECT_THAT(Error, StartsWith("malformed section header "));
102
103 EXPECT_EQ(makeSpecialCaseList("[[]", Error), nullptr);
104 EXPECT_EQ(
105 Error,
106 "malformed section at line 1: '[': invalid glob pattern, unmatched '['");
107
108 EXPECT_EQ(makeSpecialCaseList("src:=", Error), nullptr);
109 EXPECT_THAT(Error, HasSubstr("Supplied glob was blank"));
110 }
111
112 TEST_F(SpecialCaseListTest, Section) {
113 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n"
114 "[{sect1,sect2}]\n"
115 "src:test1\n"
116 "[sect3*]\n"
117 "src:test2\n");
118 EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global"));
119 EXPECT_TRUE(SCL->inSection("", "src", "global"));
120 EXPECT_TRUE(SCL->inSection("sect1", "src", "test1"));
121 EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1"));
122 EXPECT_FALSE(SCL->inSection("sect", "src", "test1"));
123 EXPECT_FALSE(SCL->inSection("sect1", "src", "test2"));
124 EXPECT_TRUE(SCL->inSection("sect2", "src", "test1"));
125 EXPECT_TRUE(SCL->inSection("sect3", "src", "test2"));
126 EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2"));
127 EXPECT_FALSE(SCL->inSection("", "src", "test1"));
128 EXPECT_FALSE(SCL->inSection("", "src", "test2"));
129 }
130
131 TEST_F(SpecialCaseListTest, GlobalInit) {
132 std::unique_ptr<SpecialCaseList> SCL =
133 makeSpecialCaseList("global:foo=init\n");
134 EXPECT_FALSE(SCL->inSection("", "global", "foo"));
135 EXPECT_FALSE(SCL->inSection("", "global", "bar"));
136 EXPECT_TRUE(SCL->inSection("", "global", "foo", "init"));
137 EXPECT_FALSE(SCL->inSection("", "global", "bar", "init"));
138
139 SCL = makeSpecialCaseList("type:t2=init\n");
140 EXPECT_FALSE(SCL->inSection("", "type", "t1"));
141 EXPECT_FALSE(SCL->inSection("", "type", "t2"));
142 EXPECT_FALSE(SCL->inSection("", "type", "t1", "init"));
143 EXPECT_TRUE(SCL->inSection("", "type", "t2", "init"));
144
145 SCL = makeSpecialCaseList("src:hello=init\n");
146 EXPECT_FALSE(SCL->inSection("", "src", "hello"));
147 EXPECT_FALSE(SCL->inSection("", "src", "bye"));
148 EXPECT_TRUE(SCL->inSection("", "src", "hello", "init"));
149 EXPECT_FALSE(SCL->inSection("", "src", "bye", "init"));
150 }
151
152 TEST_F(SpecialCaseListTest, Substring) {
153 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
154 "fun:foo\n"
155 "global:bar\n");
156 EXPECT_FALSE(SCL->inSection("", "src", "othello"));
157 EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery"));
158 EXPECT_FALSE(SCL->inSection("", "global", "bartender"));
159
160 SCL = makeSpecialCaseList("fun:*foo*\n");
161 EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery"));
162 EXPECT_TRUE(SCL->inSection("", "fun", "foobar"));
163 }
164
165 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
166 std::string Error;
167 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
168 EXPECT_EQ("malformed line 1: 'badline'", Error);
169 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
170 EXPECT_EQ(
171 "malformed glob in line 1: 'bad[a-': invalid glob pattern, unmatched '['",
172 Error);
173 std::vector<std::string> Files(1, "unexisting");
174 EXPECT_EQ(nullptr,
175 SpecialCaseList::create(Files, *vfs::getRealFileSystem(), Error));
176 EXPECT_THAT(Error, StartsWith("can't open file 'unexisting':"));
177 }
178
179 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
180 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
181 EXPECT_FALSE(SCL->inSection("", "foo", "bar"));
182 }
183
184 TEST_F(SpecialCaseListTest, MultipleExclusions) {
185 std::vector<std::string> Files;
186 Files.push_back(makeSpecialCaseListFile("src:bar\n"
187 "src:*foo*\n"
188 "src:ban=init\n"));
189 Files.push_back(makeSpecialCaseListFile("src:baz\n"
190 "src:*fog*\n"));
191 auto SCL = SpecialCaseList::createOrDie(Files, *vfs::getRealFileSystem());
192 EXPECT_TRUE(SCL->inSection("", "src", "bar"));
193 EXPECT_TRUE(SCL->inSection("", "src", "baz"));
194 EXPECT_FALSE(SCL->inSection("", "src", "ban"));
195 EXPECT_TRUE(SCL->inSection("", "src", "ban", "init"));
196 EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery"));
197 EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery"));
198 for (auto &Path : Files)
199 sys::fs::remove(Path);
200 }
201
202 TEST_F(SpecialCaseListTest, NoTrigramsInRules) {
203 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b?r\n"
204 "fun:za*az\n");
205 EXPECT_TRUE(SCL->inSection("", "fun", "bar"));
206 EXPECT_FALSE(SCL->inSection("", "fun", "baz"));
207 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
208 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
209 }
210
211 TEST_F(SpecialCaseListTest, NoTrigramsInARule) {
212 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n"
213 "fun:za*az\n");
214 EXPECT_TRUE(SCL->inSection("", "fun", "abara"));
215 EXPECT_FALSE(SCL->inSection("", "fun", "bor"));
216 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
217 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
218 }
219
220 TEST_F(SpecialCaseListTest, RepetitiveRule) {
221 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
222 "fun:bar*\n");
223 EXPECT_TRUE(SCL->inSection("", "fun", "bara"));
224 EXPECT_FALSE(SCL->inSection("", "fun", "abara"));
225 EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar"));
226 EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar"));
227 EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar"));
228 }
229
230 TEST_F(SpecialCaseListTest, SpecialSymbolRule) {
231 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n");
232 EXPECT_TRUE(SCL->inSection("", "src", "c++abi"));
233 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
234 }
235
236 TEST_F(SpecialCaseListTest, PopularTrigram) {
237 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n"
238 "fun:*aaaaa*\n"
239 "fun:*aaaa*\n"
240 "fun:*aaa*\n");
241 EXPECT_TRUE(SCL->inSection("", "fun", "aaa"));
242 EXPECT_TRUE(SCL->inSection("", "fun", "aaaa"));
243 EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa"));
244 }
245
246 TEST_F(SpecialCaseListTest, EscapedSymbols) {
247 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"
248 "src:*hello\\\\world*\n");
249 EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi"));
250 EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi"));
251 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
252 EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world"));
253 EXPECT_TRUE(SCL->inSection("", "src", "hello\\world"));
254 EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world"));
255 }
256
257 TEST_F(SpecialCaseListTest, Version1) {
258 std::unique_ptr<SpecialCaseList> SCL =
259 makeSpecialCaseList("[sect1|sect2]\n"
260 // Does not match foo!
261 "fun:foo.*\n"
262 "fun:abc|def\n"
263 "fun:b.r\n",
264 /*UseGlobs=*/false);
265
266 EXPECT_TRUE(SCL->inSection("sect1", "fun", "fooz"));
267 EXPECT_TRUE(SCL->inSection("sect2", "fun", "fooz"));
268 EXPECT_FALSE(SCL->inSection("sect3", "fun", "fooz"));
269
270 // `foo.*` does not match `foo` because the pattern is translated to `foo..*`
271 EXPECT_FALSE(SCL->inSection("sect1", "fun", "foo"));
272
273 EXPECT_TRUE(SCL->inSection("sect1", "fun", "abc"));
274 EXPECT_TRUE(SCL->inSection("sect2", "fun", "abc"));
275 EXPECT_FALSE(SCL->inSection("sect3", "fun", "abc"));
276
277 EXPECT_TRUE(SCL->inSection("sect1", "fun", "def"));
278 EXPECT_TRUE(SCL->inSection("sect2", "fun", "def"));
279 EXPECT_FALSE(SCL->inSection("sect3", "fun", "def"));
280
281 EXPECT_TRUE(SCL->inSection("sect1", "fun", "bar"));
282 EXPECT_TRUE(SCL->inSection("sect2", "fun", "bar"));
283 EXPECT_FALSE(SCL->inSection("sect3", "fun", "bar"));
284 }
285
286 TEST_F(SpecialCaseListTest, Version2) {
287 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("[{sect1,sect2}]\n"
288 "fun:foo*\n"
289 "fun:{abc,def}\n"
290 "fun:b?r\n");
291 EXPECT_TRUE(SCL->inSection("sect1", "fun", "fooz"));
292 EXPECT_TRUE(SCL->inSection("sect2", "fun", "fooz"));
293 EXPECT_FALSE(SCL->inSection("sect3", "fun", "fooz"));
294
295 EXPECT_TRUE(SCL->inSection("sect1", "fun", "foo"));
296 EXPECT_TRUE(SCL->inSection("sect2", "fun", "foo"));
297 EXPECT_FALSE(SCL->inSection("sect3", "fun", "foo"));
298
299 EXPECT_TRUE(SCL->inSection("sect1", "fun", "abc"));
300 EXPECT_TRUE(SCL->inSection("sect2", "fun", "abc"));
301 EXPECT_FALSE(SCL->inSection("sect3", "fun", "abc"));
302
303 EXPECT_TRUE(SCL->inSection("sect1", "fun", "def"));
304 EXPECT_TRUE(SCL->inSection("sect2", "fun", "def"));
305 EXPECT_FALSE(SCL->inSection("sect3", "fun", "def"));
306
307 EXPECT_TRUE(SCL->inSection("sect1", "fun", "bar"));
308 EXPECT_TRUE(SCL->inSection("sect2", "fun", "bar"));
309 EXPECT_FALSE(SCL->inSection("sect3", "fun", "bar"));
310 }
311
312 TEST_F(SpecialCaseListTest, LinesInSection) {
313 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:foo\n"
314 "fun:bar\n"
315 "fun:foo\n");
316 EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "foo"), Pair(0u, 3u));
317 EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "bar"), Pair(0u, 2u));
318 }
319
320 TEST_F(SpecialCaseListTest, LinesCrossSection) {
321 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:foo\n"
322 "fun:bar\n"
323 "fun:foo\n"
324 "[sect1]\n"
325 "fun:bar\n");
326 EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "foo"), Pair(0u, 3u));
327 EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "bar"), Pair(0u, 5u));
328 }
329
330 TEST_F(SpecialCaseListTest, Blame) {
331 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("[sect1]\n"
332 "src:foo*\n"
333 "[sect1]\n"
334 "src:bar*\n"
335 "src:def\n"
336 "[sect2]\n"
337 "src:def\n"
338 "src:de*\n");
339 EXPECT_TRUE(SCL->inSection("sect1", "src", "fooz"));
340 EXPECT_TRUE(SCL->inSection("sect1", "src", "barz"));
341 EXPECT_FALSE(SCL->inSection("sect2", "src", "fooz"));
342
343 EXPECT_TRUE(SCL->inSection("sect2", "src", "def"));
344 EXPECT_TRUE(SCL->inSection("sect1", "src", "def"));
345
346 EXPECT_THAT(SCL->inSectionBlame("sect1", "src", "fooz"), Pair(0u, 2u));
347 EXPECT_THAT(SCL->inSectionBlame("sect1", "src", "barz"), Pair(0u, 4u));
348 EXPECT_THAT(SCL->inSectionBlame("sect1", "src", "def"), Pair(0u, 5u));
349 EXPECT_THAT(SCL->inSectionBlame("sect2", "src", "def"), Pair(0u, 8u));
350 EXPECT_THAT(SCL->inSectionBlame("sect2", "src", "dez"), Pair(0u, 8u));
351 }
352
353 TEST_F(SpecialCaseListTest, FileIdx) {
354 std::vector<std::string> Files;
355 Files.push_back(makeSpecialCaseListFile("src:bar\n"
356 "src:*foo*\n"
357 "src:ban=init\n"
358 "src:baz\n"
359 "src:*def\n"));
360 Files.push_back(makeSpecialCaseListFile("src:baz\n"
361 "src:car\n"
362 "src:def*"));
363 auto SCL = SpecialCaseList::createOrDie(Files, *vfs::getRealFileSystem());
364 EXPECT_THAT(SCL->inSectionBlame("", "src", "bar"), Pair(0u, 1u));
365 EXPECT_THAT(SCL->inSectionBlame("", "src", "fooaaaaaa"), Pair(0u, 2u));
366 EXPECT_THAT(SCL->inSectionBlame("", "src", "ban", "init"), Pair(0u, 3u));
367 EXPECT_THAT(SCL->inSectionBlame("", "src", "baz"), Pair(1u, 1u));
368 EXPECT_THAT(SCL->inSectionBlame("", "src", "car"), Pair(1u, 2u));
369 EXPECT_THAT(SCL->inSectionBlame("", "src", "aaaadef"), Pair(0u, 5u));
370 EXPECT_THAT(SCL->inSectionBlame("", "src", "defaaaaa"), Pair(1u, 3u));
371 for (auto &Path : Files)
372 sys::fs::remove(Path);
373 }
374
375 } // namespace