Skip to content
5 changes: 4 additions & 1 deletion clang/lib/Basic/Diagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,10 @@ void WarningsSpecialCaseList::processSections(DiagnosticsEngine &Diags) {
// Each section has a matcher with that section's name, attached to that
// line.
const auto &DiagSectionMatcher = Entry.SectionMatcher;
unsigned DiagLine = DiagSectionMatcher->Globs.at(DiagName).second;
unsigned DiagLine = 0;
for (const auto &[Pattern, Pair] : DiagSectionMatcher->Globs)
if (Pattern == DiagName)
DiagLine = Pair.second;
LineAndSectionEntry.emplace_back(DiagLine, &Entry);
}
llvm::sort(LineAndSectionEntry);
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Support/SpecialCaseList.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class SpecialCaseList {
// Returns zero if no match is found.
LLVM_ABI unsigned match(StringRef Query) const;

StringMap<std::pair<GlobPattern, unsigned>> Globs;
std::vector<std::pair<std::string, std::pair<GlobPattern, unsigned>>> Globs;
std::vector<std::pair<std::unique_ptr<Regex>, unsigned>> RegExes;
};

Expand Down
12 changes: 11 additions & 1 deletion llvm/lib/Support/GlobPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ GlobPattern::create(StringRef S, std::optional<size_t> MaxSubPatterns) {
// Store the prefix that does not contain any metacharacter.
size_t PrefixSize = S.find_first_of("?*[{\\");
Pat.Prefix = S.substr(0, PrefixSize);
llvm::errs() << "GlobPattern::create: Prefix: " << Pat.Prefix << "\n";
if (PrefixSize == std::string::npos)
return Pat;
S = S.substr(PrefixSize);
Expand Down Expand Up @@ -191,8 +192,17 @@ GlobPattern::SubGlobPattern::create(StringRef S) {
}

bool GlobPattern::match(StringRef S) const {
if (!S.consume_front(Prefix))
int debug = 0;
if (S == "hello") {
llvm::errs() << "Prefix: " << Prefix << "\n";
debug = 1;
}
if (!S.consume_front(Prefix)) {
if (debug == 1) {
llvm::errs() << "consume_front: " << Prefix << "\n";
}
return false;
}
if (SubGlobs.empty() && S.empty())
return true;
for (auto &Glob : SubGlobs)
Expand Down
29 changes: 17 additions & 12 deletions llvm/lib/Support/SpecialCaseList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,27 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
return Error::success();
}

auto [It, DidEmplace] = Globs.try_emplace(Pattern);
if (DidEmplace) {
// We must be sure to use the string in the map rather than the provided
// reference which could be destroyed before match() is called
Pattern = It->getKey();
auto &Pair = It->getValue();
if (auto Err = GlobPattern::create(Pattern, /*MaxSubPatterns=*/1024)
.moveInto(Pair.first))
return Err;
Pair.second = LineNumber;
}
Globs.emplace_back();
auto &Glob = Globs.back();
Glob.first = Pattern.str();
auto &Pair = Glob.second;
// We must be sure to use the string in the map rather than the provided
// reference which could be destroyed before match() is called
llvm::errs() << __func__ << " GlobPattern::create: " << Glob.first << "\n";
if (auto Err = GlobPattern::create(Glob.first, /*MaxSubPatterns=*/1024)
.moveInto(Pair.first))
return Err;
Pair.second = LineNumber;
return Error::success();
}

unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
for (const auto &[Pattern, Pair] : Globs)
for (const auto &[Pattern, Pair] : Globs) {
llvm::outs() << "Inside match: " << Pattern
<< " Line number: " << Pair.second << "\n";
if (Pair.first.match(Query))
return Pair.second;
}
for (const auto &[Regex, LineNumber] : RegExes)
if (Regex->match(Query))
return LineNumber;
Expand Down Expand Up @@ -228,6 +231,8 @@ unsigned SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
StringRef Prefix, StringRef Query,
StringRef Category) const {
llvm::outs() << "Input Arguments. Prefix: " << Prefix << " Query: " << Query
<< " Category: " << Category << " \n";
SectionEntries::const_iterator I = Entries.find(Prefix);
if (I == Entries.end())
return 0;
Expand Down
28 changes: 14 additions & 14 deletions llvm/unittests/Support/SpecialCaseListTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ TEST_F(SpecialCaseListTest, Basic) {
"src:hi=category\n"
"src:z*=category\n");
EXPECT_TRUE(SCL->inSection("", "src", "hello"));
EXPECT_TRUE(SCL->inSection("", "src", "bye"));
EXPECT_TRUE(SCL->inSection("", "src", "hi", "category"));
EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category"));
EXPECT_FALSE(SCL->inSection("", "src", "hi"));
EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));

EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello"));
EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye"));
EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category"));
EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category"));
EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi"));
EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello"));
EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category"));
// EXPECT_TRUE(SCL->inSection("", "src", "bye"));
// EXPECT_TRUE(SCL->inSection("", "src", "hi", "category"));
// EXPECT_TRUE(SCL->inSection("", "src", "zzzz", "category"));
// EXPECT_FALSE(SCL->inSection("", "src", "hi"));
// EXPECT_FALSE(SCL->inSection("", "fun", "hello"));
// EXPECT_FALSE(SCL->inSection("", "src", "hello", "category"));

// EXPECT_EQ(3u, SCL->inSectionBlame("", "src", "hello"));
// EXPECT_EQ(4u, SCL->inSectionBlame("", "src", "bye"));
// EXPECT_EQ(5u, SCL->inSectionBlame("", "src", "hi", "category"));
// EXPECT_EQ(6u, SCL->inSectionBlame("", "src", "zzzz", "category"));
// EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hi"));
// EXPECT_EQ(0u, SCL->inSectionBlame("", "fun", "hello"));
// EXPECT_EQ(0u, SCL->inSectionBlame("", "src", "hello", "category"));
}

TEST_F(SpecialCaseListTest, CorrectErrorLineNumberWithBlankLine) {
Expand Down
Loading