Skip to content

Commit 1c3cff9

Browse files
qinkunbaovitalybuka
authored andcommitted
[SpecialCaseList] Iterate sections and matchers in reverse order
Issue #139128 needs to find the last one in the file. Pull Request: #141697
1 parent 6d6feaf commit 1c3cff9

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

llvm/lib/Support/SpecialCaseList.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//===----------------------------------------------------------------------===//
1515

1616
#include "llvm/Support/SpecialCaseList.h"
17+
#include "llvm/ADT/STLExtras.h"
1718
#include "llvm/Support/LineIterator.h"
1819
#include "llvm/Support/MemoryBuffer.h"
1920
#include "llvm/Support/VirtualFileSystem.h"
@@ -66,10 +67,10 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
6667
}
6768

6869
unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
69-
for (const auto &Glob : Globs)
70+
for (const auto &Glob : reverse(Globs))
7071
if (Glob->Pattern.match(Query))
7172
return Glob->LineNo;
72-
for (const auto &[Regex, LineNumber] : RegExes)
73+
for (const auto &[Regex, LineNumber] : reverse(RegExes))
7374
if (Regex->match(Query))
7475
return LineNumber;
7576
return 0;
@@ -213,7 +214,7 @@ bool SpecialCaseList::inSection(StringRef Section, StringRef Prefix,
213214
unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
214215
StringRef Query,
215216
StringRef Category) const {
216-
for (const auto &S : Sections) {
217+
for (const auto &S : reverse(Sections)) {
217218
if (S.SectionMatcher->match(Section)) {
218219
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
219220
if (Blame)

llvm/unittests/Support/SpecialCaseListTest.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ TEST_F(SpecialCaseListTest, LinesInSection) {
311311
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:foo\n"
312312
"fun:bar\n"
313313
"fun:foo\n");
314-
// FIXME: Get the last one for #139128.
315-
EXPECT_EQ(1u, SCL->inSectionBlame("sect1", "fun", "foo"));
314+
EXPECT_EQ(3u, SCL->inSectionBlame("sect1", "fun", "foo"));
316315
EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "fun", "bar"));
317316
}
318317

@@ -322,8 +321,30 @@ TEST_F(SpecialCaseListTest, LinesCrossSection) {
322321
"fun:foo\n"
323322
"[sect1]\n"
324323
"fun:bar\n");
325-
// FIXME: Get the last one for #139128.
326-
EXPECT_EQ(1u, SCL->inSectionBlame("sect1", "fun", "foo"));
327-
EXPECT_EQ(2u, SCL->inSectionBlame("sect1", "fun", "bar"));
324+
EXPECT_EQ(3u, SCL->inSectionBlame("sect1", "fun", "foo"));
325+
EXPECT_EQ(5u, SCL->inSectionBlame("sect1", "fun", "bar"));
326+
}
327+
328+
TEST_F(SpecialCaseListTest, Blame) {
329+
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("[sect1]\n"
330+
"src:foo*\n"
331+
"[sect1]\n"
332+
"src:bar*\n"
333+
"src:def\n"
334+
"[sect2]\n"
335+
"src:def\n"
336+
"src:de*\n");
337+
EXPECT_TRUE(SCL->inSection("sect1", "src", "fooz"));
338+
EXPECT_TRUE(SCL->inSection("sect1", "src", "barz"));
339+
EXPECT_FALSE(SCL->inSection("sect2", "src", "fooz"));
340+
341+
EXPECT_TRUE(SCL->inSection("sect2", "src", "def"));
342+
EXPECT_TRUE(SCL->inSection("sect1", "src", "def"));
343+
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"));
328349
}
329350
}

0 commit comments

Comments
 (0)