Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions llvm/include/llvm/Support/SpecialCaseList.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class SpecialCaseList {

std::vector<std::unique_ptr<Matcher::Glob>> Globs;
std::vector<std::unique_ptr<Reg>> RegExes;
bool RemoveDotSlash = false;
};

using SectionEntries = StringMap<StringMap<Matcher>>;
Expand Down
11 changes: 11 additions & 0 deletions llvm/lib/Support/SpecialCaseList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "llvm/Support/VirtualFileSystem.h"
#include <algorithm>
#include <limits>
#include <memory>
#include <stdio.h>
#include <string>
#include <system_error>
Expand Down Expand Up @@ -72,6 +73,8 @@ Error SpecialCaseList::Matcher::insert(StringRef Pattern, unsigned LineNumber,
void SpecialCaseList::Matcher::match(
StringRef Query,
llvm::function_ref<void(StringRef Rule, unsigned LineNo)> Cb) const {
if (RemoveDotSlash)
Query = llvm::sys::path::remove_leading_dotslash(Query);
for (const auto &Glob : reverse(Globs))
if (Glob->Pattern.match(Query))
Cb(Glob->Name, Glob->LineNo);
Expand Down Expand Up @@ -164,12 +167,18 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,
// https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666
bool UseGlobs = Version > 1;

bool RemoveDotSlash = Version > 2;

Section *CurrentSection;
if (auto Err = addSection("*", FileIdx, 1).moveInto(CurrentSection)) {
Error = toString(std::move(Err));
return false;
}

// This is the current list of prefixes for all existing users matching file
// path. We may need parametrization in constructor in future.
constexpr StringRef PathPrefixes[] = {"src", "!src", "mainfile", "source"};

for (line_iterator LineIt(*MB, /*SkipBlanks=*/true, /*CommentMarker=*/'#');
!LineIt.is_at_eof(); LineIt++) {
unsigned LineNo = LineIt.line_number();
Expand Down Expand Up @@ -205,6 +214,8 @@ bool SpecialCaseList::parse(unsigned FileIdx, const MemoryBuffer *MB,

auto [Pattern, Category] = Postfix.split("=");
auto &Entry = CurrentSection->Entries[Prefix][Category];
Entry.RemoveDotSlash =
RemoveDotSlash && llvm::is_contained(PathPrefixes, Prefix);
if (auto Err = Entry.insert(Pattern, LineNo, UseGlobs)) {
Error =
(Twine("malformed ") + (UseGlobs ? "glob" : "regex") + " in line " +
Expand Down
62 changes: 50 additions & 12 deletions llvm/unittests/Support/SpecialCaseListTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,31 @@ namespace {

class SpecialCaseListTest : public ::testing::Test {
protected:
std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
std::string &Error,
bool UseGlobs = true) {
std::unique_ptr<SpecialCaseList>
makeSpecialCaseList(StringRef List, std::string &Error, int Version = 0) {
auto S = List.str();
if (!UseGlobs)
S = (Twine("#!special-case-list-v1\n") + S).str();
if (Version)
S = (Twine("#!special-case-list-v") + Twine(Version) + "\n" + S).str();
std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(S);
return SpecialCaseList::create(MB.get(), Error);
}

std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
bool UseGlobs = true) {
int Version = 0) {
std::string Error;
auto SCL = makeSpecialCaseList(List, Error, UseGlobs);
auto SCL = makeSpecialCaseList(List, Error, Version);
assert(SCL);
assert(Error == "");
return SCL;
}

std::string makeSpecialCaseListFile(StringRef Contents,
bool UseGlobs = true) {
std::string makeSpecialCaseListFile(StringRef Contents, int Version = 0) {
int FD;
SmallString<64> Path;
sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
raw_fd_ostream OF(FD, true, true);
if (!UseGlobs)
OF << "#!special-case-list-v1\n";
if (Version)
OF << "#!special-case-list-v" << Version << "\n";
OF << Contents;
OF.close();
return std::string(Path.str());
Expand Down Expand Up @@ -261,7 +259,7 @@ TEST_F(SpecialCaseListTest, Version1) {
"fun:foo.*\n"
"fun:abc|def\n"
"fun:b.r\n",
/*UseGlobs=*/false);
/*Version=*/1);

EXPECT_TRUE(SCL->inSection("sect1", "fun", "fooz"));
EXPECT_TRUE(SCL->inSection("sect2", "fun", "fooz"));
Expand Down Expand Up @@ -309,6 +307,46 @@ TEST_F(SpecialCaseListTest, Version2) {
EXPECT_FALSE(SCL->inSection("sect3", "fun", "bar"));
}

TEST_F(SpecialCaseListTest, DotSlash) {
std::unique_ptr<SpecialCaseList> SCL2 = makeSpecialCaseList("[dot]\n"
"fun:./foo\n"
"src:./bar\n"
"[not]\n"
"fun:foo\n"
"src:bar\n");
std::unique_ptr<SpecialCaseList> SCL3 = makeSpecialCaseList("[dot]\n"
"fun:./foo\n"
"src:./bar\n"
"[not]\n"
"fun:foo\n"
"src:bar\n",
/*Version=*/3);

EXPECT_TRUE(SCL2->inSection("dot", "fun", "./foo"));
EXPECT_TRUE(SCL3->inSection("dot", "fun", "./foo"));

EXPECT_FALSE(SCL2->inSection("dot", "fun", "foo"));
EXPECT_FALSE(SCL3->inSection("dot", "fun", "foo"));

EXPECT_TRUE(SCL2->inSection("dot", "src", "./bar"));
EXPECT_FALSE(SCL3->inSection("dot", "src", "./bar"));

EXPECT_FALSE(SCL2->inSection("dot", "src", "bar"));
EXPECT_FALSE(SCL3->inSection("dot", "src", "bar"));

EXPECT_FALSE(SCL2->inSection("not", "fun", "./foo"));
EXPECT_FALSE(SCL3->inSection("not", "fun", "./foo"));

EXPECT_TRUE(SCL2->inSection("not", "fun", "foo"));
EXPECT_TRUE(SCL3->inSection("not", "fun", "foo"));

EXPECT_FALSE(SCL2->inSection("not", "src", "./bar"));
EXPECT_TRUE(SCL3->inSection("not", "src", "./bar"));

EXPECT_TRUE(SCL2->inSection("not", "src", "bar"));
EXPECT_TRUE(SCL3->inSection("not", "src", "bar"));
}

TEST_F(SpecialCaseListTest, LinesInSection) {
std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("fun:foo\n"
"fun:bar\n"
Expand Down