Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions clang/lib/Basic/SanitizerSpecialCaseList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void SanitizerSpecialCaseList::createSanitizerSections() {
SanitizerMask Mask;

#define SANITIZER(NAME, ID) \
if (S.SectionMatcher.match(NAME)) \
if (S.SectionMatcher.matchAny(NAME)) \
Mask |= SanitizerKind::ID;
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)

Expand All @@ -66,8 +66,7 @@ SanitizerSpecialCaseList::inSectionBlame(SanitizerMask Mask, StringRef Prefix,
StringRef Category) const {
for (const auto &S : llvm::reverse(SanitizerSections)) {
if (S.Mask & Mask) {
unsigned LineNum =
SpecialCaseList::inSectionBlame(S.S.Entries, Prefix, Query, Category);
unsigned LineNum = S.S.getLastMatch(Prefix, Query, Category);
if (LineNum > 0)
return {S.S.FileIdx, LineNum};
}
Expand Down
32 changes: 23 additions & 9 deletions llvm/include/llvm/Support/SpecialCaseList.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,15 @@ class SpecialCaseList {
public:
LLVM_ABI Error insert(StringRef Pattern, unsigned LineNumber,
bool UseRegex);
// Returns the line number in the source file that this query matches to.
// Returns zero if no match is found.
LLVM_ABI unsigned match(StringRef Query) const;
LLVM_ABI void
match(StringRef Query,
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const;

LLVM_ABI bool matchAny(StringRef Query) const {
bool R = false;
match(Query, [&](StringRef, unsigned) { R = true; });
return R;
}

struct Glob {
std::string Name;
Expand Down Expand Up @@ -153,6 +159,20 @@ class SpecialCaseList {
SectionEntries Entries;
std::string SectionStr;
unsigned FileIdx;

// Helper method to search by Prefix, Query, and Category. Returns
// 1-based line number on which rule is defined, or 0 if there is no match.
LLVM_ABI unsigned getLastMatch(StringRef Prefix, StringRef Query,
StringRef Category) const;

// Helper method to search by Prefix, Query, and Category. Returns
// matching rule, or empty string if there is no match.
LLVM_ABI StringRef getLongestMatch(StringRef Prefix, StringRef Query,
StringRef Category) const;

private:
LLVM_ABI const SpecialCaseList::Matcher *
findMatcher(StringRef Prefix, StringRef Category) const;
};

std::vector<Section> Sections;
Expand All @@ -164,12 +184,6 @@ class SpecialCaseList {
/// Parses just-constructed SpecialCaseList entries from a memory buffer.
LLVM_ABI bool parse(unsigned FileIdx, const MemoryBuffer *MB,
std::string &Error);

// Helper method for derived classes to search by Prefix, Query, and Category
// once they have already resolved a section entry.
LLVM_ABI unsigned inSectionBlame(const SectionEntries &Entries,
StringRef Prefix, StringRef Query,
StringRef Category) const;
};

} // namespace llvm
Expand Down
38 changes: 26 additions & 12 deletions llvm/lib/Support/SpecialCaseList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <algorithm>
#include <limits>
#include <stdio.h>
#include <string>
Expand Down Expand Up @@ -69,14 +70,15 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
return Error::success();
}

unsigned SpecialCaseList::Matcher::match(StringRef Query) const {
void SpecialCaseList::Matcher::match(
StringRef Query,
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
for (const auto &Glob : reverse(Globs))
if (Glob->Pattern.match(Query))
return Glob->LineNo;
Cb(Glob->Name, Glob->LineNo);
for (const auto &[Regex, LineNumber] : reverse(RegExes))
if (Regex->match(Query))
return LineNumber;
return 0;
Cb(/*FIXME: there is no users of this param yet */ "", LineNumber);
}

// TODO: Refactor this to return Expected<...>
Expand Down Expand Up @@ -227,26 +229,38 @@ std::pair<unsigned, unsigned>
SpecialCaseList::inSectionBlame(StringRef Section, StringRef Prefix,
StringRef Query, StringRef Category) const {
for (const auto &S : reverse(Sections)) {
if (S.SectionMatcher.match(Section)) {
unsigned Blame = inSectionBlame(S.Entries, Prefix, Query, Category);
if (S.SectionMatcher.matchAny(Section)) {
unsigned Blame = S.getLastMatch(Prefix, Query, Category);
if (Blame)
return {S.FileIdx, Blame};
}
}
return NotFound;
}

unsigned SpecialCaseList::inSectionBlame(const SectionEntries &Entries,
StringRef Prefix, StringRef Query,
StringRef Category) const {
const SpecialCaseList::Matcher *
SpecialCaseList::Section::findMatcher(StringRef Prefix,
StringRef Category) const {
SectionEntries::const_iterator I = Entries.find(Prefix);
if (I == Entries.end())
return 0;
return nullptr;
StringMap<Matcher>::const_iterator II = I->second.find(Category);
if (II == I->second.end())
return 0;
return nullptr;

return II->getValue().match(Query);
return &II->second;
}

unsigned SpecialCaseList::Section::getLastMatch(StringRef Prefix,
StringRef Query,
StringRef Category) const {
unsigned LastLine = 0;
if (const Matcher *M = findMatcher(Prefix, Category)) {
M->match(Query, [&](StringRef, unsigned LineNo) {
LastLine = std::max(LastLine, LineNo);
});
}
return LastLine;
}

} // namespace llvm