[llvm] [CI][format] Explicitly pass extensions to git-clang-format (take 2) (PR #98227)

Louis Dionne via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 10 08:24:24 PDT 2024


https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/98227

>From 0cf09f60e233ecdd158632f67c0c1f186d68a333 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 9 Jul 2024 17:28:55 -0400
Subject: [PATCH 1/5] [CI][format] Explicitly pass extensions to
 git-clang-format (take 2)

This is a second attempt to land 7620fe0, which was reverted in 9572388
because it caused formatting not to be enforced for several patches.
---
 llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp |  2 ++
 llvm/utils/git/code-format-helper.py        | 11 +++++++++++
 2 files changed, 13 insertions(+)

diff --git a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
index 7f90457d720b4..536984e6c9a5a 100644
--- a/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
+++ b/llvm/lib/CodeGen/MLRegAllocEvictAdvisor.cpp
@@ -10,6 +10,8 @@
 //
 //===----------------------------------------------------------------------===//
 
+// This line should never get through the formatting job clang-format please format this into something that is actually reasonable.
+
 #include "AllocationOrder.h"
 #include "RegAllocEvictionAdvisor.h"
 #include "RegAllocGreedy.h"
diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index f1207026704e8..d60d4131bc94b 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -216,6 +216,17 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
             cf_cmd.append(args.start_rev)
             cf_cmd.append(args.end_rev)
 
+        # Gather the extension of all modified files and pass them explicitly to git-clang-format.
+        # This prevents git-clang-format from applying its own filtering rules on top of ours.
+        extensions = set()
+        for file in cpp_files:
+            _, ext = os.path.splitext(file)
+            extensions.add(
+                ext.strip(".")
+            )  # Exclude periods since git-clang-format takes extensions without them
+        cf_cmd.append("--extensions")
+        cf_cmd.append("'{}'".format(",".join(extensions)))
+
         cf_cmd.append("--")
         cf_cmd += cpp_files
 

>From d4dad1ea9258a81f1cf5839c6b5cb037533f1c3b Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 10 Jul 2024 10:02:19 -0400
Subject: [PATCH 2/5] Try to use the formatting job from the PR (for testing)

---
 .github/workflows/pr-code-format.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/.github/workflows/pr-code-format.yml b/.github/workflows/pr-code-format.yml
index 22357e5d99e4c..663779909416d 100644
--- a/.github/workflows/pr-code-format.yml
+++ b/.github/workflows/pr-code-format.yml
@@ -80,7 +80,7 @@ jobs:
         # Create an empty comments file so the pr-write job doesn't fail.
         run: |
           echo "[]" > comments &&
-          python ./code-format-tools/llvm/utils/git/code-format-helper.py \
+          python ./llvm/utils/git/code-format-helper.py \
             --write-comment-to-file \
             --token ${{ secrets.GITHUB_TOKEN }} \
             --issue-number $GITHUB_PR_NUMBER \

>From f5999399e2561c1d3885627d387f212f8e4ff305 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 10 Jul 2024 10:15:01 -0400
Subject: [PATCH 3/5] Add additional debugging

---
 llvm/utils/git/code-format-helper.py | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index d60d4131bc94b..eea21189e1d8c 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -236,6 +236,9 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
         proc = subprocess.run(cf_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
         sys.stdout.write(proc.stderr.decode("utf-8"))
 
+        print("LDIONNE: return code of cmd was {}".format(proc.returncode))
+        print("LDIONNE: output of cmd was {}".format(proc.stdout.decode("utf-8")))
+
         if proc.returncode != 0:
             # formatting needed, or the command otherwise failed
             if args.verbose:

>From 30c958fa916713b02a999451e55a74c4350f1148 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 10 Jul 2024 11:04:49 -0400
Subject: [PATCH 4/5] Run git-clang-format with --verbose

---
 llvm/utils/git/code-format-helper.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index eea21189e1d8c..a38d11795cc52 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -210,7 +210,7 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
         if not cpp_files:
             return None
 
-        cf_cmd = [self.clang_fmt_path, "--diff"]
+        cf_cmd = [self.clang_fmt_path, "--verbose", "--diff"]
 
         if args.start_rev and args.end_rev:
             cf_cmd.append(args.start_rev)
@@ -237,7 +237,7 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
         sys.stdout.write(proc.stderr.decode("utf-8"))
 
         print("LDIONNE: return code of cmd was {}".format(proc.returncode))
-        print("LDIONNE: output of cmd was {}".format(proc.stdout.decode("utf-8")))
+        print("LDIONNE: output of cmd was:\n{}".format(proc.stdout.decode("utf-8")))
 
         if proc.returncode != 0:
             # formatting needed, or the command otherwise failed

>From a955fb945d815f39f43a8d304418c7abd17bbd70 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 10 Jul 2024 11:24:11 -0400
Subject: [PATCH 5/5] Get rid of spurious quoting

---
 llvm/utils/git/code-format-helper.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/utils/git/code-format-helper.py b/llvm/utils/git/code-format-helper.py
index a38d11795cc52..72d414ddb0716 100755
--- a/llvm/utils/git/code-format-helper.py
+++ b/llvm/utils/git/code-format-helper.py
@@ -225,7 +225,7 @@ def format_run(self, changed_files: List[str], args: FormatArgs) -> Optional[str
                 ext.strip(".")
             )  # Exclude periods since git-clang-format takes extensions without them
         cf_cmd.append("--extensions")
-        cf_cmd.append("'{}'".format(",".join(extensions)))
+        cf_cmd.append("{}".format(",".join(extensions)))
 
         cf_cmd.append("--")
         cf_cmd += cpp_files



More information about the llvm-commits mailing list