Skip to content

[clang-format] Add --fallback-style to git-clang-format #137609

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

apdofficial
Copy link

@apdofficial apdofficial commented Apr 28, 2025

This MR implements a fallback style option for the cases when no .clang-format is available.

I need a pipeline workflow where some repositories don't have a .clang-format file. I want to allow specifying a fallback style so that formatting can still run consistently in those cases.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Apr 28, 2025

@llvm/pr-subscribers-clang-format

Author: Andrej Pištek (apdofficial)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/137609.diff

1 Files Affected:

  • (modified) clang/tools/clang-format/git-clang-format (+22-2)
diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index e709803d9a3f1..99373630d59f5 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -189,6 +189,14 @@ def main():
         default=config.get("clangformat.style", None),
         help="passed to clang-format",
     ),
+    p.add_argument(
+        "--fallback-style",
+        default=None,
+        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 +287,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 +543,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 +599,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 +633,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 +646,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)

@owenca
Copy link
Contributor

owenca commented May 1, 2025

Please add a description so that we'll understand why you need this new option.

@apdofficial
Copy link
Author

apdofficial commented May 8, 2025

@llvm/pr-subscribers-clang-format

Author: Andrej Pištek (apdofficial)
Changes

Please let me know whether the updated description is sufficient. If not, I can update it. Thank you.

@apdofficial apdofficial force-pushed the feature/git-clang-format-fallback-style branch 2 times, most recently from 49c012f to 2e82063 Compare May 8, 2025 15:26
@owenca owenca changed the title [git-clang-format] add fallback style argument option [clang-format] Add --fallback-style to git-clang-format May 15, 2025
@owenca owenca changed the title [clang-format] Add --fallback-style to git-clang-format [clang-format] Add --fallback-style to git-clang-format May 15, 2025
@mydeveloperday
Copy link
Contributor

For those repos that don't have a .clang-format why not just add one with whatever your fallback style is?

BasedOnStyle: <whatever_you_need>

I know this seems like a reasonable feature but it just causes another set of functionality we have to think about, I don't see the values to people who actually use clang-format? isn't the purpose here to encourage those who don't use clang-format to use it...

@apdofficial apdofficial force-pushed the feature/git-clang-format-fallback-style branch from 534efbf to 6e23e19 Compare May 20, 2025 07:37
@apdofficial apdofficial force-pushed the feature/git-clang-format-fallback-style branch from 6e23e19 to 502d5be Compare May 20, 2025 08:16
@apdofficial
Copy link
Author

@mydeveloperday, that is a valid question.

The reason is that we have many legacy repositories that do not have a .clang-format file. The legacy repositories are in a maintenance phase, and most importantly, they follow a custom code style. We want to implement a standard code style without refactoring the legacy code. This means adding a .clang-format file would not truthfully reflect the actual code style in the legacy repositories. This MR enables checking only the newly added code according to the new code style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants