Skip to content

Commit aed53d1

Browse files
authored
[NFC][SpecialCaseList] Extract findMatcher and ::match with callback (#162397)
There are some users who rely on more than line number. It would be easy to move some logic from users side here with extracted methods.
1 parent bdef80f commit aed53d1

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 1 addition & 1 deletion
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

llvm/include/llvm/Support/SpecialCaseList.h

Lines changed: 18 additions & 3 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;
@@ -158,6 +164,15 @@ class SpecialCaseList {
158164
// 1-based line number on which rule is defined, or 0 if there is no match.
159165
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query,
160166
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;
161176
};
162177

163178
std::vector<Section> Sections;

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Support/LineIterator.h"
2121
#include "llvm/Support/MemoryBuffer.h"
2222
#include "llvm/Support/VirtualFileSystem.h"
23+
#include <algorithm>
2324
#include <limits>
2425
#include <stdio.h>
2526
#include <string>
@@ -69,14 +70,15 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
6970
return Error::success();
7071
}
7172

72-
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
73+
void SpecialCaseList::Matcher::match(
74+
StringRef Query,
75+
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
7376
for (const auto &Glob : reverse(Globs))
7477
if (Glob->Pattern.match(Query))
75-
return Glob->LineNo;
78+
Cb(Glob->Name, Glob->LineNo);
7679
for (const auto &[Regex, LineNumber] : reverse(RegExes))
7780
if (Regex->match(Query))
78-
return LineNumber;
79-
return 0;
81+
Cb(/*FIXME: there is no users of this param yet */ "", LineNumber);
8082
}
8183

8284
// TODO: Refactor this to return Expected<...>
@@ -227,7 +229,7 @@ std::pair<unsigned, unsigned>
227229
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
228230
StringRef Query, StringRef Category) const {
229231
for (const auto &S : reverse(Sections)) {
230-
if (S.SectionMatcher.match(Section)) {
232+
if (S.SectionMatcher.matchAny(Section)) {
231233
unsigned Blame = S.getLastMatch(Prefix, Query, Category);
232234
if (Blame)
233235
return {S.FileIdx, Blame};
@@ -236,17 +238,29 @@ SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
236238
return NotFound;
237239
}
238240

239-
unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
240-
StringRef Query,
241-
StringRef Category) const {
241+
const SpecialCaseList::Matcher *
242+
SpecialCaseList::Section::findMatcher(StringRef Prefix,
243+
StringRef Category) const {
242244
SectionEntries::const_iterator I = Entries.find(Prefix);
243245
if (I == Entries.end())
244-
return 0;
246+
return nullptr;
245247
StringMap<Matcher>::const_iterator II = I->second.find(Category);
246248
if (II == I->second.end())
247-
return 0;
249+
return nullptr;
250+
251+
return &II->second;
252+
}
248253

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

252266
} // namespace llvm

0 commit comments

Comments
 (0)