blob: 060703e102fc8e6fdc13df66a01428af77305116 [file] [log] [blame]
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00001//===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth9a67b072017-06-06 11:06:56 +000010#include "llvm/Support/SpecialCaseList.h"
Alexey Samsonovb9b80272015-02-04 17:39:48 +000011#include "llvm/Support/FileSystem.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000012#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000013#include "gtest/gtest.h"
14
15using namespace llvm;
16
17namespace {
18
19class SpecialCaseListTest : public ::testing::Test {
20protected:
David Blaikie15913f42014-09-02 18:13:54 +000021 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
22 std::string &Error) {
David Blaikiedfbe3d62014-08-27 20:14:18 +000023 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000024 return SpecialCaseList::create(MB.get(), Error);
25 }
26
David Blaikie15913f42014-09-02 18:13:54 +000027 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000028 std::string Error;
David Blaikie15913f42014-09-02 18:13:54 +000029 auto SCL = makeSpecialCaseList(List, Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000030 assert(SCL);
31 assert(Error == "");
32 return SCL;
33 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +000034
35 std::string makeSpecialCaseListFile(StringRef Contents) {
36 int FD;
37 SmallString<64> Path;
38 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
39 raw_fd_ostream OF(FD, true, true);
40 OF << Contents;
41 OF.close();
42 return Path.str();
43 }
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000044};
45
46TEST_F(SpecialCaseListTest, Basic) {
David Blaikie15913f42014-09-02 18:13:54 +000047 std::unique_ptr<SpecialCaseList> SCL =
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000048 makeSpecialCaseList("# This is a comment.\n"
49 "\n"
50 "src:hello\n"
51 "src:bye\n"
52 "src:hi=category\n"
David Blaikie15913f42014-09-02 18:13:54 +000053 "src:z*=category\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000054 EXPECT_TRUE(SCL->inSection("", "src", "hello"));
55 EXPECT_TRUE(SCL->inSection("", "src", "bye"));
56 EXPECT_TRUE(SCL->inSection("", "src", "hi", "category"));
57 EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category"));
58 EXPECT_FALSE(SCL->inSection("", "src", "hi"));
59 EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
60 EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));
Mitch Phillips40d66632017-11-07 21:16:46 +000061
62 EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello"));
63 EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye"));
64 EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category"));
65 EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category"));
66 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi"));
67 EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello"));
68 EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category"));
69}
70
71TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) {
72 std::string Error;
73 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n"
74 "\n"
75 "[not valid\n",
76 Error));
77 EXPECT_TRUE(
78 ((StringRef)Error).startswith("malformed section header on line 3:"));
79
80 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n"
81 "[not valid\n",
82 Error));
83 EXPECT_TRUE(
84 ((StringRef)Error).startswith("malformed section header on line 4:"));
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000085}
86
87TEST_F(SpecialCaseListTest, SectionRegexErrorHandling) {
88 std::string Error;
89 EXPECT_EQ(makeSpecialCaseList("[address", Error), nullptr);
90 EXPECT_TRUE(((StringRef)Error).startswith("malformed section header "));
91
92 EXPECT_EQ(makeSpecialCaseList("[[]", Error), nullptr);
93 EXPECT_TRUE(((StringRef)Error).startswith("malformed regex for section [: "));
Mitch Phillipsfa2eda82017-10-24 23:56:12 +000094
95 EXPECT_EQ(makeSpecialCaseList("src:=", Error), nullptr);
96 EXPECT_TRUE(((StringRef)Error).endswith("Supplied regexp was blank"));
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +000097}
98
99TEST_F(SpecialCaseListTest, Section) {
100 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:global\n"
101 "[sect1|sect2]\n"
102 "src:test1\n"
103 "[sect3*]\n"
104 "src:test2\n");
105 EXPECT_TRUE(SCL->inSection("arbitrary", "src", "global"));
106 EXPECT_TRUE(SCL->inSection("", "src", "global"));
107 EXPECT_TRUE(SCL->inSection("sect1", "src", "test1"));
108 EXPECT_FALSE(SCL->inSection("sect1-arbitrary", "src", "test1"));
109 EXPECT_FALSE(SCL->inSection("sect", "src", "test1"));
110 EXPECT_FALSE(SCL->inSection("sect1", "src", "test2"));
111 EXPECT_TRUE(SCL->inSection("sect2", "src", "test1"));
112 EXPECT_TRUE(SCL->inSection("sect3", "src", "test2"));
113 EXPECT_TRUE(SCL->inSection("sect3-arbitrary", "src", "test2"));
114 EXPECT_FALSE(SCL->inSection("", "src", "test1"));
115 EXPECT_FALSE(SCL->inSection("", "src", "test2"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000116}
117
Alexey Samsonovcfb97aa2014-11-20 01:27:19 +0000118TEST_F(SpecialCaseListTest, GlobalInit) {
David Blaikie15913f42014-09-02 18:13:54 +0000119 std::unique_ptr<SpecialCaseList> SCL =
120 makeSpecialCaseList("global:foo=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000121 EXPECT_FALSE(SCL->inSection("", "global", "foo"));
122 EXPECT_FALSE(SCL->inSection("", "global", "bar"));
123 EXPECT_TRUE(SCL->inSection("", "global", "foo", "init"));
124 EXPECT_FALSE(SCL->inSection("", "global", "bar", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000125
David Blaikie15913f42014-09-02 18:13:54 +0000126 SCL = makeSpecialCaseList("type:t2=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000127 EXPECT_FALSE(SCL->inSection("", "type", "t1"));
128 EXPECT_FALSE(SCL->inSection("", "type", "t2"));
129 EXPECT_FALSE(SCL->inSection("", "type", "t1", "init"));
130 EXPECT_TRUE(SCL->inSection("", "type", "t2", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000131
David Blaikie15913f42014-09-02 18:13:54 +0000132 SCL = makeSpecialCaseList("src:hello=init\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000133 EXPECT_FALSE(SCL->inSection("", "src", "hello"));
134 EXPECT_FALSE(SCL->inSection("", "src", "bye"));
135 EXPECT_TRUE(SCL->inSection("", "src", "hello", "init"));
136 EXPECT_FALSE(SCL->inSection("", "src", "bye", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000137}
138
139TEST_F(SpecialCaseListTest, Substring) {
David Blaikie15913f42014-09-02 18:13:54 +0000140 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
141 "fun:foo\n"
142 "global:bar\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000143 EXPECT_FALSE(SCL->inSection("", "src", "othello"));
144 EXPECT_FALSE(SCL->inSection("", "fun", "tomfoolery"));
145 EXPECT_FALSE(SCL->inSection("", "global", "bartender"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000146
David Blaikie15913f42014-09-02 18:13:54 +0000147 SCL = makeSpecialCaseList("fun:*foo*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000148 EXPECT_TRUE(SCL->inSection("", "fun", "tomfoolery"));
149 EXPECT_TRUE(SCL->inSection("", "fun", "foobar"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000150}
151
152TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
153 std::string Error;
154 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000155 EXPECT_EQ("malformed line 1: 'badline'", Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000156 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000157 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000158 Error);
159 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
160 "fun:fun(a\n",
161 Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000162 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000163 Error);
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000164 std::vector<std::string> Files(1, "unexisting");
165 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
166 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000167}
168
169TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
David Blaikie15913f42014-09-02 18:13:54 +0000170 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000171 EXPECT_FALSE(SCL->inSection("", "foo", "bar"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000172}
173
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000174TEST_F(SpecialCaseListTest, MultipleBlacklists) {
175 std::vector<std::string> Files;
176 Files.push_back(makeSpecialCaseListFile("src:bar\n"
177 "src:*foo*\n"
178 "src:ban=init\n"));
179 Files.push_back(makeSpecialCaseListFile("src:baz\n"
180 "src:*fog*\n"));
181 auto SCL = SpecialCaseList::createOrDie(Files);
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000182 EXPECT_TRUE(SCL->inSection("", "src", "bar"));
183 EXPECT_TRUE(SCL->inSection("", "src", "baz"));
184 EXPECT_FALSE(SCL->inSection("", "src", "ban"));
185 EXPECT_TRUE(SCL->inSection("", "src", "ban", "init"));
186 EXPECT_TRUE(SCL->inSection("", "src", "tomfoolery"));
187 EXPECT_TRUE(SCL->inSection("", "src", "tomfoglery"));
Reid Kleckner75e557f2016-09-02 00:51:34 +0000188 for (auto &Path : Files)
189 sys::fs::remove(Path);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000190}
191
Ivan Krasin3dade412016-12-01 02:54:54 +0000192TEST_F(SpecialCaseListTest, NoTrigramsInRules) {
193 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:b.r\n"
194 "fun:za*az\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000195 EXPECT_TRUE(SCL->inSection("", "fun", "bar"));
196 EXPECT_FALSE(SCL->inSection("", "fun", "baz"));
197 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
198 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000199}
200
201TEST_F(SpecialCaseListTest, NoTrigramsInARule) {
202 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*\n"
203 "fun:za*az\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000204 EXPECT_TRUE(SCL->inSection("", "fun", "abara"));
205 EXPECT_FALSE(SCL->inSection("", "fun", "bor"));
206 EXPECT_TRUE(SCL->inSection("", "fun", "zakaz"));
207 EXPECT_FALSE(SCL->inSection("", "fun", "zaraza"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000208}
209
210TEST_F(SpecialCaseListTest, RepetitiveRule) {
211 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
212 "fun:bar*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000213 EXPECT_TRUE(SCL->inSection("", "fun", "bara"));
214 EXPECT_FALSE(SCL->inSection("", "fun", "abara"));
215 EXPECT_TRUE(SCL->inSection("", "fun", "barbarbarbar"));
216 EXPECT_TRUE(SCL->inSection("", "fun", "abarbarbarbar"));
217 EXPECT_FALSE(SCL->inSection("", "fun", "abarbarbar"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000218}
219
220TEST_F(SpecialCaseListTest, SpecialSymbolRule) {
221 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000222 EXPECT_TRUE(SCL->inSection("", "src", "c++abi"));
223 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000224}
225
226TEST_F(SpecialCaseListTest, PopularTrigram) {
227 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:*aaaaaa*\n"
228 "fun:*aaaaa*\n"
229 "fun:*aaaa*\n"
230 "fun:*aaa*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000231 EXPECT_TRUE(SCL->inSection("", "fun", "aaa"));
232 EXPECT_TRUE(SCL->inSection("", "fun", "aaaa"));
233 EXPECT_TRUE(SCL->inSection("", "fun", "aaaabbbaaa"));
Ivan Krasin3dade412016-12-01 02:54:54 +0000234}
235
Ivan Krasin75453b02016-12-02 23:30:16 +0000236TEST_F(SpecialCaseListTest, EscapedSymbols) {
237 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:*c\\+\\+abi*\n"
238 "src:*hello\\\\world*\n");
Vlad Tsyrklevich998b2202017-09-25 22:11:11 +0000239 EXPECT_TRUE(SCL->inSection("", "src", "dir/c++abi"));
240 EXPECT_FALSE(SCL->inSection("", "src", "dir/c\\+\\+abi"));
241 EXPECT_FALSE(SCL->inSection("", "src", "c\\+\\+abi"));
242 EXPECT_TRUE(SCL->inSection("", "src", "C:\\hello\\world"));
243 EXPECT_TRUE(SCL->inSection("", "src", "hello\\world"));
244 EXPECT_FALSE(SCL->inSection("", "src", "hello\\\\world"));
Ivan Krasin75453b02016-12-02 23:30:16 +0000245}
246
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000247}