[clang] [clang-format] clang-format-ignore: Add support for double asterisk patterns (PR #110560)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 24 23:36:39 PDT 2024


================
@@ -164,6 +164,40 @@ TEST_F(MatchFilePathTest, Path) {
   EXPECT_FALSE(match("foo\\", R"(foo*\)"));
 }
 
+TEST_F(MatchFilePathTest, DoubleAsterisk) {
+  EXPECT_TRUE(match("a/b/c/d.cpp", "**b**"));
+  EXPECT_TRUE(match("a/b/c/d.cpp", "**/b/**"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**d_*"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**/d_*"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**d_**"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**/d_**"));
+
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**/b/c/**"));
+
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "a/**/b/c/d_e.cpp"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "a/**/c/d_e.cpp"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "a/**/b/**/d_e.cpp"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "**/b/**/d_e.cpp"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "a/**/**/b/**"));
+
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "**/d"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "**/b/d"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "**/b/d/**"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "**/b/c/"));
+
+  // Multiple consecutive asterisks are treated as **
+  EXPECT_TRUE(match("a/b/c/d.cpp", "***b****"));
+  EXPECT_TRUE(match("a/b/c/d.cpp", "****/b/***"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "***d_**"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "****/d_*"));
+  EXPECT_TRUE(match("a/b/c/d_e.cpp", "***/b/c/*****"));
+
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "*****/d"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "***/b/d"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "*****/b/d/***"));
+  EXPECT_FALSE(match("a/b/c/d_e.cpp", "***/b/c"));
----------------
owenca wrote:

> > if we are doing this an saying "Its like .gitignore" should we support
> 
> I am only indicating that the `**` glob pattern is implemented in .gitignore, and we follow the same rules they do for this specific pattern.

When I added .clang-format-ignore, I chose a POSIX glob-like model and intentionally stayed clear of the .gitignore model, which is, IMO, confusing and overcomplicated. I prefer to not add `**` now to partially match .gitignore unless there's a good reason and strong demand for it. There's also a possibility of regression as `**` is currently matched the same way as a single `*`.

> I think the current implementation only considers one pattern in the file at a time, in isolation of all other lines in the .clang-format-ignore file.

Yes, the current behavior is to check the patterns in the .clang-format-ignore file one pattern at a time until a match is found.

https://github.com/llvm/llvm-project/pull/110560


More information about the cfe-commits mailing list