Skip to content

[clang-format] Support globstar in .clang-format-ignore #121404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 1, 2025
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 clang/docs/ClangFormat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ names. It has the following format:
* Patterns follow the rules specified in `POSIX 2.13.1, 2.13.2, and Rule 1 of
2.13.3 <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/
V3_chap02.html#tag_18_13>`_.
* Bash globstar (``**``) is supported.
* A pattern is negated if it starts with a bang (``!``).

To match all files in a directory, use e.g. ``foo/bar/*``. To match all files in
Expand Down
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ clang-format
- Adds ``RemoveEmptyLinesInUnwrappedLines`` option.
- Adds ``KeepFormFeed`` option and set it to ``true`` for ``GNU`` style.
- Adds ``AllowShortNamespacesOnASingleLine`` option.
- Adds support for bash globstar in ``.clang-format-ignore``.

libclang
--------
Expand Down
38 changes: 26 additions & 12 deletions clang/lib/Format/MatchFilePath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ bool matchFilePath(StringRef Pattern, StringRef FilePath) {
assert(!Pattern.empty());
assert(!FilePath.empty());

const auto FilePathBack = FilePath.back();

// No match if `Pattern` ends with a non-meta character not equal to the last
// character of `FilePath`.
if (const auto C = Pattern.back(); !strchr("?*]", C) && C != FilePath.back())
if (const auto C = Pattern.back(); !strchr("?*]", C) && C != FilePathBack)
return false;

constexpr auto Separator = '/';
Expand All @@ -49,25 +51,37 @@ bool matchFilePath(StringRef Pattern, StringRef FilePath) {
return false;
break;
case '*': {
while (++I < EOP && Pattern[I] == '*') { // Skip consecutive stars.
bool Globstar = I == 0 || Pattern[I - 1] == Separator;
int StarCount = 1;
for (; ++I < EOP && Pattern[I] == '*'; ++StarCount) {
// Skip consecutive stars.
}
if (StarCount != 2)
Globstar = false;
const auto K = FilePath.find(Separator, J); // Index of next `Separator`.
const bool NoMoreSeparatorsInFilePath = K == StringRef::npos;
if (I == EOP) // `Pattern` ends with a star.
return NoMoreSeparatorsInFilePath;
// `Pattern` ends with a lone backslash.
if (Pattern[I] == '\\' && ++I == EOP)
return false;
return Globstar || NoMoreSeparatorsInFilePath;
if (Pattern[I] != Separator) {
Globstar = false;
// `Pattern` ends with a lone backslash.
if (Pattern[I] == '\\' && ++I == EOP)
return false;
}
// The star is followed by a (possibly escaped) `Separator`.
if (Pattern[I] == Separator) {
if (NoMoreSeparatorsInFilePath)
return false;
J = K; // Skip to next `Separator` in `FilePath`.
break;
if (!Globstar) {
if (NoMoreSeparatorsInFilePath)
return false;
J = K; // Skip to next `Separator` in `FilePath`.
break;
}
if (++I == EOP)
return FilePathBack == Separator;
}
// Recurse.
for (auto Pat = Pattern.substr(I); J < End && FilePath[J] != Separator;
++J) {
for (auto Pat = Pattern.substr(I);
J < End && (Globstar || FilePath[J] != Separator); ++J) {
if (matchFilePath(Pat, FilePath.substr(J)))
return true;
}
Expand Down
35 changes: 35 additions & 0 deletions clang/unittests/Format/MatchFilePathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,41 @@ TEST_F(MatchFilePathTest, Path) {
EXPECT_FALSE(match("foo\\", R"(foo*\)"));
}

TEST_F(MatchFilePathTest, Globstar) {
EXPECT_TRUE(match("/", "**"));
EXPECT_TRUE(match("foo", "**"));
EXPECT_TRUE(match("/foo", "**"));
EXPECT_TRUE(match("foo/", "**"));
EXPECT_TRUE(match("foo/bar", "**"));

EXPECT_TRUE(match("/", "**/"));
EXPECT_TRUE(match("foo/", "**/"));
EXPECT_TRUE(match("/foo/", "**/"));
EXPECT_TRUE(match("foo/bar/", "**/"));

EXPECT_TRUE(match("/", "/**"));
EXPECT_TRUE(match("/foo", "/**"));
EXPECT_TRUE(match("/foo/", "/**"));
EXPECT_TRUE(match("/foo/bar", "/**"));

EXPECT_TRUE(match("foo", "**/foo"));
EXPECT_TRUE(match("/foo", "**/foo"));
EXPECT_TRUE(match("foo/bar", "**/bar"));
EXPECT_TRUE(match("/foo/bar", "**/foo/bar"));
EXPECT_TRUE(match("foo/bar/baz", "**/bar/baz"));

EXPECT_TRUE(match("abc/foo", "abc/**"));
EXPECT_TRUE(match("abc/foo/", "abc/**"));
EXPECT_TRUE(match("abc/foo/bar", "abc/**"));

EXPECT_TRUE(match("a/b", "a/**/b"));
EXPECT_TRUE(match("a/x/b", "a/**/b"));
EXPECT_TRUE(match("a/x/y/b", "a/**/b"));

EXPECT_FALSE(match("a/x/b", "a**/b"));
EXPECT_FALSE(match("a/x/b", "a/**b"));
}

} // namespace
} // namespace format
} // namespace clang
Loading