Skip to content

Commit adbb3a4

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.6
2 parents 768f438 + e39a053 commit adbb3a4

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() {
4242
SanitizerMask Mask;
4343

4444
#define SANITIZER(NAME, ID) \
45-
if (S.SectionMatcher.match(NAME)) \
45+
if (S.SectionMatcher.matchAny(NAME)) \
4646
Mask |= SanitizerKind::ID;
4747
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)
4848

@@ -66,8 +66,7 @@ SanitizerSpecialCaseList::inSectionBlame(SanitizerMask Mask, StringRef Prefix,
6666
StringRef Category) const {
6767
for (const auto &S : llvm::reverse(SanitizerSections)) {
6868
if (S.Mask & Mask) {
69-
unsigned LineNum =
70-
SpecialCaseList::inSectionBlame(S.S.Entries, Prefix, Query, Category);
69+
unsigned LineNum = S.S.getLastMatch(Prefix, Query, Category);
7170
if (LineNum > 0)
7271
return {S.S.FileIdx, LineNum};
7372
}

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,15 @@ class SpecialCaseList {
123123
public:
124124
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
125125
bool UseRegex);
126-
// Returns the line number in the source file that this query matches to.
127-
// Returns zero if no match is found.
128-
LLVM_ABI unsigned match(StringRef Query) const;
126+
LLVM_ABI void
127+
match(StringRef Query,
128+
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const;
129+
130+
LLVM_ABI bool matchAny(StringRef Query) const {
131+
bool R = false;
132+
match(Query, [&](StringRef, unsigned) { R = true; });
133+
return R;
134+
}
129135

130136
struct Glob {
131137
std::string Name;
@@ -153,6 +159,20 @@ class SpecialCaseList {
153159
SectionEntries Entries;
154160
std::string SectionStr;
155161
unsigned FileIdx;
162+
163+
// Helper method to search by Prefix, Query, and Category. Returns
164+
// 1-based line number on which rule is defined, or 0 if there is no match.
165+
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query,
166+
StringRef Category) const;
167+
168+
// Helper method to search by Prefix, Query, and Category. Returns
169+
// matching rule, or empty string if there is no match.
170+
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query,
171+
StringRef Category) const;
172+
173+
private:
174+
LLVM_ABI const SpecialCaseList::Matcher *
175+
findMatcher(StringRef Prefix, StringRef Category) const;
156176
};
157177

158178
std::vector<Section> Sections;
@@ -164,12 +184,6 @@ class SpecialCaseList {
164184
/// Parses just-constructed SpecialCaseList entries from a memory buffer.
165185
LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB,
166186
std::string &Error);
167-
168-
// Helper method for derived classes to search by Prefix, Query, and Category
169-
// once they have already resolved a section entry.
170-
LLVM_ABI unsigned inSectionBlame(const SectionEntries &Entries,
171-
StringRef Prefix, StringRef Query,
172-
StringRef Category) const;
173187
};
174188

175189
} // namespace llvm

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "llvm/Support/LineIterator.h"
2121
#include "llvm/Support/MemoryBuffer.h"
2222
#include "llvm/Support/VirtualFileSystem.h"
23-
#include <limits>
23+
#include <algorithm>
2424
#include <stdio.h>
2525
#include <string>
2626
#include <system_error>
@@ -69,14 +69,15 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
6969
return Error::success();
7070
}
7171

72-
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
72+
void SpecialCaseList::Matcher::match(
73+
StringRef Query,
74+
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
7375
for (const auto &Glob : reverse(Globs))
7476
if (Glob->Pattern.match(Query))
75-
return Glob->LineNo;
77+
Cb(Glob->Name, Glob->LineNo);
7678
for (const auto &[Regex, LineNumber] : reverse(RegExes))
7779
if (Regex->match(Query))
78-
return LineNumber;
79-
return 0;
80+
Cb(/*FIXME: there is no users of this param yet */ "", LineNumber);
8081
}
8182

8283
// TODO: Refactor this to return Expected<...>
@@ -169,7 +170,9 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
169170
return false;
170171
}
171172

172-
for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#');
173+
for (line_iterator LineIt(*MB, /*SkipBlanks=*/
174+
true,
175+
/*CommentMarker=*/'#');
173176
!LineIt.is_at_eof(); LineIt++) {
174177
unsigned LineNo = LineIt.line_number();
175178
StringRef Line = LineIt->trim();
@@ -227,26 +230,38 @@ std::pair<unsigned, unsigned>
227230
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
228231
StringRef Query, StringRef Category) const {
229232
for (const auto &S : reverse(Sections)) {
230-
if (S.SectionMatcher.match(Section)) {
231-
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
233+
if (S.SectionMatcher.matchAny(Section)) {
234+
unsigned Blame = S.getLastMatch(Prefix, Query, Category);
232235
if (Blame)
233236
return {S.FileIdx, Blame};
234237
}
235238
}
236239
return NotFound;
237240
}
238241

239-
unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
240-
StringRef Prefix, StringRef Query,
241-
StringRef Category) const {
242+
const SpecialCaseList::Matcher *
243+
SpecialCaseList::Section::findMatcher(StringRef Prefix,
244+
StringRef Category) const {
242245
SectionEntries::const_iterator I = Entries.find(Prefix);
243246
if (I == Entries.end())
244-
return 0;
247+
return nullptr;
245248
StringMap<Matcher>::const_iterator II = I->second.find(Category);
246249
if (II == I->second.end())
247-
return 0;
250+
return nullptr;
248251

249-
return II->getValue().match(Query);
252+
return &II->second;
253+
}
254+
255+
unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
256+
StringRef Query,
257+
StringRef Category) const {
258+
unsigned LastLine = 0;
259+
if (const Matcher *M = findMatcher(Prefix, Category)) {
260+
M->match(Query, [&](StringRef, unsigned LineNo) {
261+
LastLine = std::max(LastLine, LineNo);
262+
});
263+
}
264+
return LastLine;
250265
}
251266

252267
} // namespace llvm

0 commit comments

Comments
 (0)