|
20 | 20 | #include "llvm/Support/LineIterator.h" |
21 | 21 | #include "llvm/Support/MemoryBuffer.h" |
22 | 22 | #include "llvm/Support/VirtualFileSystem.h" |
23 | | -#include <limits> |
| 23 | +#include <algorithm> |
24 | 24 | #include <stdio.h> |
25 | 25 | #include <string> |
26 | 26 | #include <system_error> |
@@ -69,14 +69,15 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber, |
69 | 69 | return Error::success(); |
70 | 70 | } |
71 | 71 |
|
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 { |
73 | 75 | for (const auto &Glob : reverse(Globs)) |
74 | 76 | if (Glob->Pattern.match(Query)) |
75 | | - return Glob->LineNo; |
| 77 | + Cb(Glob->Name, Glob->LineNo); |
76 | 78 | for (const auto &[Regex, LineNumber] : reverse(RegExes)) |
77 | 79 | if (Regex->match(Query)) |
78 | | - return LineNumber; |
79 | | - return 0; |
| 80 | + Cb(/*FIXME: there is no users of this param yet */ "", LineNumber); |
80 | 81 | } |
81 | 82 |
|
82 | 83 | // TODO: Refactor this to return Expected<...> |
@@ -169,7 +170,9 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB, |
169 | 170 | return false; |
170 | 171 | } |
171 | 172 |
|
172 | | - for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#'); |
| 173 | + for (line_iterator LineIt(*MB, /*SkipBlanks=*/ |
| 174 | + true, |
| 175 | + /*CommentMarker=*/'#'); |
173 | 176 | !LineIt.is_at_eof(); LineIt++) { |
174 | 177 | unsigned LineNo = LineIt.line_number(); |
175 | 178 | StringRef Line = LineIt->trim(); |
@@ -227,26 +230,38 @@ std::pair<unsigned, unsigned> |
227 | 230 | SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix, |
228 | 231 | StringRef Query, StringRef Category) const { |
229 | 232 | 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); |
232 | 235 | if (Blame) |
233 | 236 | return {S.FileIdx, Blame}; |
234 | 237 | } |
235 | 238 | } |
236 | 239 | return NotFound; |
237 | 240 | } |
238 | 241 |
|
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 { |
242 | 245 | SectionEntries::const_iterator I = Entries.find(Prefix); |
243 | 246 | if (I == Entries.end()) |
244 | | - return 0; |
| 247 | + return nullptr; |
245 | 248 | StringMap<Matcher>::const_iterator II = I->second.find(Category); |
246 | 249 | if (II == I->second.end()) |
247 | | - return 0; |
| 250 | + return nullptr; |
248 | 251 |
|
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; |
250 | 265 | } |
251 | 266 |
|
252 | 267 | } // namespace llvm |
0 commit comments