[PATCH] D120187: [clang-tidy] Allow newline characters as separators for checks in Clang-Tidy configurations

Danny Mösch via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 19 07:50:19 PST 2022


SimplyDanny created this revision.
SimplyDanny added a reviewer: alexfh.
Herald added subscribers: carlosgalvezp, xazax.hun.
SimplyDanny requested review of this revision.
Herald added subscribers: cfe-commits, aheejin.
Herald added a project: clang-tools-extra.

This is a fix for #53737. In addition to commas, newline characters are considered as separators of checks.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120187

Files:
  .clang-tidy
  clang-tools-extra/clang-tidy/GlobList.cpp
  clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
  clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp


Index: clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp
+++ clang-tools-extra/unittests/clang-tidy/GlobListTest.cpp
@@ -104,5 +104,18 @@
   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
Index: clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
===================================================================
--- clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
+++ clang-tools-extra/unittests/clang-tidy/ClangTidyOptionsTest.cpp
@@ -86,6 +86,20 @@
   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"(
Index: clang-tools-extra/clang-tidy/GlobList.cpp
===================================================================
--- clang-tools-extra/clang-tidy/GlobList.cpp
+++ clang-tools-extra/clang-tidy/GlobList.cpp
@@ -27,7 +27,8 @@
 // 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(','));
+  auto NextSplitIndex = std::min(GlobList.find(','), GlobList.find('\n'));
+  StringRef UntrimmedGlob = GlobList.substr(0, NextSplitIndex);
   StringRef Glob = UntrimmedGlob.trim();
   GlobList = GlobList.substr(UntrimmedGlob.size() + 1);
   SmallString<128> RegexText("^");
@@ -44,7 +45,7 @@
 }
 
 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);
Index: .clang-tidy
===================================================================
--- .clang-tidy
+++ .clang-tidy
@@ -1,4 +1,13 @@
-Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,-misc-no-recursion,readability-identifier-naming'
+Checks: |
+  -*
+  clang-diagnostic-*
+  llvm-*
+  misc-*
+  -misc-unused-parameters
+  -misc-non-private-member-variables-in-classes
+  -misc-no-recursion
+  readability-identifier-naming
+
 CheckOptions:
   - key:             readability-identifier-naming.ClassCase
     value:           CamelCase


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120187.410088.patch
Type: text/x-patch
Size: 3504 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220219/5880ffc2/attachment.bin>


More information about the cfe-commits mailing list