Allow newline characters as separators for checks in Clang-Tidy configurations

This is a fix for #53737. In addition to commas, newline characters are
considered as separators of checks.
This commit is contained in:
Danny Mösch 2022-03-15 14:29:33 -04:00 committed by Aaron Ballman
parent 1da2c5ba09
commit 2b21fc5520
3 changed files with 29 additions and 2 deletions

View File

@ -27,7 +27,7 @@ static bool consumeNegativeIndicator(StringRef &GlobList) {
// Converts first glob from the comma-separated list of globs to Regex and
// removes it and the trailing comma from the GlobList.
static llvm::Regex consumeGlob(StringRef &GlobList) {
StringRef UntrimmedGlob = GlobList.substr(0, GlobList.find(','));
StringRef UntrimmedGlob = GlobList.substr(0, GlobList.find_first_of(",\n"));
StringRef Glob = UntrimmedGlob.trim();
GlobList = GlobList.substr(UntrimmedGlob.size() + 1);
SmallString<128> RegexText("^");
@ -44,7 +44,7 @@ static llvm::Regex consumeGlob(StringRef &GlobList) {
}
GlobList::GlobList(StringRef Globs, bool KeepNegativeGlobs /* =true */) {
Items.reserve(Globs.count(',') + 1);
Items.reserve(Globs.count(',') + Globs.count('\n') + 1);
do {
GlobListItem Item;
Item.IsPositive = !consumeNegativeIndicator(Globs);

View File

@ -86,6 +86,20 @@ TEST(ParseConfiguration, ValidConfiguration) {
EXPECT_EQ("some.user", *Options->User);
}
TEST(ParseConfiguration, ChecksSeparatedByNewlines) {
auto MemoryBuffer = llvm::MemoryBufferRef("Checks: |\n"
" -*,misc-*\n"
" llvm-*\n"
" -clang-*,\n"
" google-*",
"Options");
auto Options = parseConfiguration(MemoryBuffer);
EXPECT_TRUE(!!Options);
EXPECT_EQ("-*,misc-*\nllvm-*\n-clang-*,\ngoogle-*\n", *Options->Checks);
}
TEST(ParseConfiguration, MergeConfigurations) {
llvm::ErrorOr<ClangTidyOptions> Options1 =
parseConfiguration(llvm::MemoryBufferRef(R"(

View File

@ -104,5 +104,18 @@ TYPED_TEST(GlobListTest, Complex) {
EXPECT_TRUE(Filter.contains("asdfqwEasdf"));
}
TYPED_TEST(GlobListTest, NewlineCharactersAsSeparators) {
TypeParam Filter("a* \n b,\n-c*,dd");
EXPECT_FALSE(Filter.contains(""));
EXPECT_TRUE(Filter.contains("aaa"));
EXPECT_TRUE(Filter.contains("b"));
EXPECT_FALSE(Filter.contains("c"));
EXPECT_FALSE(Filter.contains("ccc"));
EXPECT_FALSE(Filter.contains("d"));
EXPECT_TRUE(Filter.contains("dd"));
EXPECT_FALSE(Filter.contains("ddd"));
}
} // namespace tidy
} // namespace clang