[clang] [git-clang-format] add fallback style argument option (PR #137609)
Andrej Pištek via cfe-commits
cfe-commits at lists.llvm.org
Thu May 8 08:26:43 PDT 2025
https://github.com/apdofficial updated https://github.com/llvm/llvm-project/pull/137609
>From 2e82063e9e2c159a7a713ecedbcc0bfbdbf66143 Mon Sep 17 00:00:00 2001
From: Andrej Pistek <a.pistek at cosine.nl>
Date: Mon, 7 Apr 2025 13:37:47 +0200
Subject: [PATCH] [git-clang-format] add fallback style arg
---
clang/tools/clang-format/git-clang-format | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index e709803d9a3f1..8a92853dc96c4 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -189,6 +189,13 @@ def main():
default=config.get("clangformat.style", None),
help="passed to clang-format",
),
+ p.add_argument(
+ "--fallback-style",
+ help="The name of the predefined style used as a "
+ "fallback in case clang-format is invoked with "
+ "--style=file, but can not find the .clang-format "
+ "file to use.",
+ ),
p.add_argument(
"-v",
"--verbose",
@@ -279,7 +286,11 @@ def main():
old_tree = create_tree_from_workdir(changed_lines)
revision = None
new_tree = run_clang_format_and_save_to_tree(
- changed_lines, revision, binary=opts.binary, style=opts.style
+ changed_lines,
+ revision,
+ binary=opts.binary,
+ style=opts.style,
+ fallback_style=opts.fallback_style,
)
if opts.verbose >= 1:
print("old tree: %s" % old_tree)
@@ -531,7 +542,11 @@ def create_tree_from_index(filenames):
def run_clang_format_and_save_to_tree(
- changed_lines, revision=None, binary="clang-format", style=None
+ changed_lines,
+ revision=None,
+ binary="clang-format",
+ style=None,
+ fallback_style=None,
):
"""Run clang-format on each file and save the result to a git tree.
@@ -583,6 +598,7 @@ def run_clang_format_and_save_to_tree(
revision=revision,
binary=binary,
style=style,
+ fallback_style=fallback_style,
env=env,
)
yield "%s %s\t%s" % (mode, blob_id, filename)
@@ -616,6 +632,7 @@ def clang_format_to_blob(
revision=None,
binary="clang-format",
style=None,
+ fallback_style=None,
env=None,
):
"""Run clang-format on the given file and save the result to a git blob.
@@ -628,6 +645,8 @@ def clang_format_to_blob(
clang_format_cmd = [binary]
if style:
clang_format_cmd.extend(["--style=" + style])
+ if fallback_style:
+ clang_format_cmd.extend(["--fallback-style=" + fallback_style])
clang_format_cmd.extend(
[
"--lines=%s:%s" % (start_line, start_line + line_count - 1)
More information about the cfe-commits
mailing list