[clang] [clang-format] Support multi-part extensions in git-clang-format (PR #248353)
Ankit Sahoo via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 2 23:06:46 PST 2026
https://github.com/ankit-sahoo-10 updated https://github.com/llvm/llvm-project/pull/248353
>From 88121647ca2534d02485557fc3490d92a7d266cf Mon Sep 17 00:00:00 2001
From: Ankit Sahoo <fkinggenie at gmail.com>
Date: Thu, 3 Dec 2026 11:54:39 +0530
Subject: [PATCH] [clang-format] Support multi-part extensions in
git-clang-format
filter_by_extension() only compared the text after the last period of
the path, so a multi-part extension could never match. As a result,
"pb.txt", which is already in the default extension list, never matched
any file, and users could not opt in to files such as "*.hpp.in" via
--extensions or clangFormat.extensions.
Match every dot-separated suffix of the file's base name instead, so
"foo.pb.txt" matches both "txt" and "pb.txt". Looking only at the base
name also stops periods in directory names from being taken as part of
the extension.
Fixes #100706.
---
clang/tools/clang-format/git-clang-format | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 50b35122bfb003..c8ccc5d61bfec4 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -495,13 +495,16 @@ def filter_by_extension(dictionary, allowed_extensions):
"""Delete every key in `dictionary` that doesn't have an allowed extension.
`allowed_extensions` must be a collection of lowercase file extensions,
- excluding the period."""
+ excluding the leading period. An extension may itself contain periods
+ (e.g. "pb.txt" or "hpp.in"), in which case it matches any file name
+ ending in that multi-part extension."""
allowed_extensions = frozenset(allowed_extensions)
for filename in list(dictionary.keys()):
- base_ext = filename.rsplit(".", 1)
- if len(base_ext) == 1 and "" in allowed_extensions:
- continue
- if len(base_ext) == 1 or base_ext[1].lower() not in allowed_extensions:
+ # Git paths always use '/', and only the base name has an extension.
+ parts = filename.rsplit("/", 1)[-1].lower().split(".")[1:]
+ # All dot-separated suffixes, e.g. "a.pb.txt" -> {"txt", "pb.txt"}.
+ extensions = {".".join(parts[i:]) for i in range(len(parts))} or {""}
+ if extensions.isdisjoint(allowed_extensions):
del dictionary[filename]
More information about the cfe-commits
mailing list