Skip to content

Commit 81bde10

Browse files
authored
[NFCI] Change SpecialCaseList::inSectionBlame to return pair<uint, uint> (FileIdx, LineNo). (#141540)
Accoring to the discussion in #140529, we need to SSCL can be created from multiple ignore list files, so we can repeat-fsanitize-ignorelist=. The change is necessary to achieve the feature described in #139128.
1 parent 2340a4e commit 81bde10

File tree

4 files changed

+85
-49
lines changed

4 files changed

+85
-49
lines changed

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Regex.h"
1919
#include <memory>
2020
#include <string>
21+
#include <utility>
2122
#include <vector>
2223

2324
namespace llvm {
@@ -69,6 +70,7 @@ class FileSystem;
6970
/// ---
7071
class SpecialCaseList {
7172
public:
73+
static constexpr std::pair<unsigned, unsigned> NotFound = {0, 0};
7274
/// Parses the special case list entries from files. On failure, returns
7375
/// 0 and writes an error message to string.
7476
LLVM_ABI static std::unique_ptr<SpecialCaseList>
@@ -93,17 +95,17 @@ class SpecialCaseList {
9395
LLVM_ABI bool inSection(StringRef Section, StringRef Prefix, StringRef Query,
9496
StringRef Category = StringRef()) const;
9597

96-
/// Returns the line number corresponding to the special case list entry if
97-
/// the special case list contains a line
98+
/// Returns the file index and the line number <FileIdx, LineNo> corresponding
99+
/// to the special case list entry if the special case list contains a line
98100
/// \code
99101
/// @Prefix:<E>=@Category
100102
/// \endcode
101103
/// where @Query satisfies the glob <E> in a given @Section.
102-
/// Returns zero if there is no exclusion entry corresponding to this
104+
/// Returns (zero, zero) if there is no exclusion entry corresponding to this
103105
/// expression.
104-
LLVM_ABI unsigned inSectionBlame(StringRef Section, StringRef Prefix,
105-
StringRef Query,
106-
StringRef Category = StringRef()) const;
106+
LLVM_ABI std::pair<unsigned, unsigned>
107+
inSectionBlame(StringRef Section, StringRef Prefix, StringRef Query,
108+
StringRef Category = StringRef()) const;
107109

108110
protected:
109111
// Implementations of the create*() functions that can also be used by derived
@@ -142,21 +144,24 @@ class SpecialCaseList {
142144
using SectionEntries = StringMap<StringMap<Matcher>>;
143145

144146
struct Section {
145-
Section(std::unique_ptr<Matcher> M) : SectionMatcher(std::move(M)) {};
146-
Section() : Section(std::make_unique<Matcher>()) {};
147+
Section(StringRef Str, unsigned FileIdx)
148+
: SectionStr(Str), FileIdx(FileIdx) {};
147149

148-
std::unique_ptr<Matcher> SectionMatcher;
150+
std::unique_ptr<Matcher> SectionMatcher = std::make_unique<Matcher>();
149151
SectionEntries Entries;
150152
std::string SectionStr;
153+
unsigned FileIdx;
151154
};
152155

153156
std::vector<Section> Sections;
154157

155-
LLVM_ABI Expected<Section *> addSection(StringRef SectionStr, unsigned LineNo,
158+
LLVM_ABI Expected<Section *> addSection(StringRef SectionStr,
159+
unsigned FileIdx, unsigned LineNo,
156160
bool UseGlobs = true);
157161

158162
/// Parses just-constructed SpecialCaseList entries from a memory buffer.
159-
LLVM_ABI bool parse(const MemoryBuffer *MB, std::string &Error);
163+
LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB,
164+
std::string &Error);
160165

161166
// Helper method for derived classes to search by Prefix, Query, and Category
162167
// once they have already resolved a section entry.

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,16 @@ SpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
105105

106106
bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
107107
vfs::FileSystem &VFS, std::string &Error) {
108-
for (const auto &Path : Paths) {
108+
for (size_t i = 0; i < Paths.size(); ++i) {
109+
const auto &Path = Paths[i];
109110
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
110111
VFS.getBufferForFile(Path);
111112
if (std::error_code EC = FileOrErr.getError()) {
112113
Error = (Twine("can't open file '") + Path + "': " + EC.message()).str();
113114
return false;
114115
}
115116
std::string ParseError;
116-
if (!parse(FileOrErr.get().get(), ParseError)) {
117+
if (!parse(i, FileOrErr.get().get(), ParseError)) {
117118
Error = (Twine("error parsing file '") + Path + "': " + ParseError).str();
118119
return false;
119120
}
@@ -123,17 +124,16 @@ bool SpecialCaseList::createInternal(const std::vector<std::string> &Paths,
123124

124125
bool SpecialCaseList::createInternal(const MemoryBuffer *MB,
125126
std::string &Error) {
126-
if (!parse(MB, Error))
127+
if (!parse(0, MB, Error))
127128
return false;
128129
return true;
129130
}
130131

131132
Expected<SpecialCaseList::Section *>
132-
SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
133-
bool UseGlobs) {
134-
Sections.emplace_back();
133+
SpecialCaseList::addSection(StringRef SectionStr, unsigned FileNo,
134+
unsigned LineNo, bool UseGlobs) {
135+
Sections.emplace_back(SectionStr, FileNo);
135136
auto &Section = Sections.back();
136-
Section.SectionStr = SectionStr;
137137

138138
if (auto Err = Section.SectionMatcher->insert(SectionStr, LineNo, UseGlobs)) {
139139
return createStringError(errc::invalid_argument,
@@ -145,9 +145,10 @@ SpecialCaseList::addSection(StringRef SectionStr, unsigned LineNo,
145145
return &Section;
146146
}
147147

148-
bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
148+
bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
149+
std::string &Error) {
149150
Section *CurrentSection;
150-
if (auto Err = addSection("*", 1).moveInto(CurrentSection)) {
151+
if (auto Err = addSection("*", FileIdx, 1).moveInto(CurrentSection)) {
151152
Error = toString(std::move(Err));
152153
return false;
153154
}
@@ -175,7 +176,8 @@ bool SpecialCaseList::parse(const MemoryBuffer *MB, std::string &Error) {
175176
return false;
176177
}
177178

178-
if (auto Err = addSection(Line.drop_front().drop_back(), LineNo, UseGlobs)
179+
if (auto Err = addSection(Line.drop_front().drop_back(), FileIdx, LineNo,
180+
UseGlobs)
179181
.moveInto(CurrentSection)) {
180182
Error = toString(std::move(Err));
181183
return false;
@@ -208,20 +210,21 @@ SpecialCaseList::~SpecialCaseList() = default;
208210

209211
bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
210212
StringRef Query, StringRef Category) const {
211-
return inSectionBlame(Section, Prefix, Query, Category);
213+
auto [FileIdx, LineNo] = inSectionBlame(Section, Prefix, Query, Category);
214+
return LineNo;
212215
}
213216

214-
unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
215-
StringRef Query,
216-
StringRef Category) const {
217+
std::pair<unsigned, unsigned>
218+
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
219+
StringRef Query, StringRef Category) const {
217220
for (const auto &S : reverse(Sections)) {
218221
if (S.SectionMatcher->match(Section)) {
219222
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
220223
if (Blame)
221-
return Blame;
224+
return {S.FileIdx, Blame};
222225
}
223226
}
224-
return 0;
227+
return NotFound;
225228
}
226229

227230
unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,

llvm/tools/llvm-cfi-verify/llvm-cfi-verify.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ static void printBlameContext(const DILineInfo &LineInfo, unsigned Context) {
7878
File->getBuffer().split(Lines, '\n');
7979

8080
for (unsigned i = std::max<size_t>(1, LineInfo.Line - Context);
81-
i <
82-
std::min<size_t>(Lines.size() + 1, LineInfo.Line + Context + 1);
81+
i < std::min<size_t>(Lines.size() + 1, LineInfo.Line + Context + 1);
8382
++i) {
8483
if (i == LineInfo.Line)
8584
outs() << ">";
@@ -193,12 +192,16 @@ printIndirectCFInstructions(FileAnalysis &Analysis,
193192

194193
unsigned BlameLine = 0;
195194
for (auto &K : {"cfi-icall", "cfi-vcall"}) {
196-
if (!BlameLine)
197-
BlameLine =
195+
if (!BlameLine) {
196+
auto [FileIdx, Line] =
198197
SpecialCaseList->inSectionBlame(K, "src", LineInfo.FileName);
199-
if (!BlameLine)
200-
BlameLine =
198+
BlameLine = Line;
199+
}
200+
if (!BlameLine) {
201+
auto [FileIdx, Line] =
201202
SpecialCaseList->inSectionBlame(K, "fun", LineInfo.FunctionName);
203+
BlameLine = Line;
204+
}
202205
}
203206

204207
if (BlameLine) {

llvm/unittests/Support/SpecialCaseListTest.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "gtest/gtest.h"
1515

1616
using testing::HasSubstr;
17+
using testing::Pair;
1718
using testing::StartsWith;
1819
using namespace llvm;
1920

@@ -70,13 +71,14 @@ TEST_F(SpecialCaseListTest, Basic) {
7071
EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
7172
EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));
7273

73-
EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello"));
74-
EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye"));
75-
EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category"));
76-
EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category"));
77-
EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi"));
78-
EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello"));
79-
EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category"));
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));
8082
}
8183

8284
TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) {
@@ -311,8 +313,8 @@ TEST_F(SpecialCaseListTest, LinesInSection) {
311313
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:foo\n"
312314
"fun:bar\n"
313315
"fun:foo\n");
314-
EXPECT_EQ(3u, SCL->inSectionBlame("sect1", "fun", "foo"));
315-
EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "fun", "bar"));
316+
EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "foo"), Pair(0u, 3u));
317+
EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "bar"), Pair(0u, 2u));
316318
}
317319

318320
TEST_F(SpecialCaseListTest, LinesCrossSection) {
@@ -321,8 +323,8 @@ TEST_F(SpecialCaseListTest, LinesCrossSection) {
321323
"fun:foo\n"
322324
"[sect1]\n"
323325
"fun:bar\n");
324-
EXPECT_EQ(3u, SCL->inSectionBlame("sect1", "fun", "foo"));
325-
EXPECT_EQ(5u, SCL->inSectionBlame("sect1", "fun", "bar"));
326+
EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "foo"), Pair(0u, 3u));
327+
EXPECT_THAT(SCL->inSectionBlame("sect1", "fun", "bar"), Pair(0u, 5u));
326328
}
327329

328330
TEST_F(SpecialCaseListTest, Blame) {
@@ -341,10 +343,33 @@ TEST_F(SpecialCaseListTest, Blame) {
341343
EXPECT_TRUE(SCL->inSection("sect2", "src", "def"));
342344
EXPECT_TRUE(SCL->inSection("sect1", "src", "def"));
343345

344-
EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "src", "fooz"));
345-
EXPECT_EQ(4u, SCL->inSectionBlame("sect1", "src", "barz"));
346-
EXPECT_EQ(5u, SCL->inSectionBlame("sect1", "src", "def"));
347-
EXPECT_EQ(8u, SCL->inSectionBlame("sect2", "src", "def"));
348-
EXPECT_EQ(8u, SCL->inSectionBlame("sect2", "src", "dez"));
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));
349351
}
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);
350373
}
374+
375+
} // namespace

0 commit comments

Comments
 (0)