[clang] Add ability for clang-format-diff to exit with non-0 status (PR #70883)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 8 13:06:39 PST 2023


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/70883

>From 646cabcf167facb63c78277b50ea62afbc61b3c0 Mon Sep 17 00:00:00 2001
From: Conrad Donahue <conradjdonahue at gmail.com>
Date: Tue, 31 Oct 2023 21:55:27 -0400
Subject: [PATCH 1/4] Add ability for clang-format-diff to exit with non-0
 status

---
 clang/docs/ClangFormat.rst                    | 1 +
 clang/tools/clang-format/clang-format-diff.py | 8 ++++++++
 2 files changed, 9 insertions(+)

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index f52f35550d03eb6..42864b90e408037 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -325,6 +325,7 @@ output of a unified diff and reformats all contained lines with
                           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-formatfile to use.
     -binary BINARY        location of binary to use for clang-format
+    -non-zero-exit-code   exit with a non-zero status if formatting changes are necessary
 
 To reformat all the lines in the latest Mercurial/:program:`hg` commit, do:
 
diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 324ef5b7f6b35f6..755ef23649a2469 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -95,6 +95,12 @@ def main():
         default="clang-format",
         help="location of binary to use for clang-format",
     )
+    parser.add_argument(
+        "-non-zero-exit-code",
+        action="store_true",
+        default=False,
+        help="exit with a non-zero status if formatting changes are necessary"
+    )
     args = parser.parse_args()
 
     # Extract changed lines for each file.
@@ -185,6 +191,8 @@ def main():
             diff_string = "".join(diff)
             if len(diff_string) > 0:
                 sys.stdout.write(diff_string)
+                if args.non_zero_exit_code:
+                    sys.exit(1)
 
 
 if __name__ == "__main__":

>From 929ccfdae7bf8d377e20f69200826cb67fcca76a Mon Sep 17 00:00:00 2001
From: Conrad Donahue <conradjdonahue at gmail.com>
Date: Tue, 31 Oct 2023 22:14:50 -0400
Subject: [PATCH 2/4] Fix formatting in clang-format-diff.py

---
 clang/tools/clang-format/clang-format-diff.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 755ef23649a2469..3d5542b65529df2 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -99,7 +99,7 @@ def main():
         "-non-zero-exit-code",
         action="store_true",
         default=False,
-        help="exit with a non-zero status if formatting changes are necessary"
+        help="exit with a non-zero status if formatting changes are necessary",
     )
     args = parser.parse_args()
 

>From 4a06c6596c6f721340b731b1119146a80b472d5b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Wed, 8 Nov 2023 13:06:21 -0800
Subject: [PATCH 3/4] Update clang/tools/clang-format/clang-format-diff.py

---
 clang/tools/clang-format/clang-format-diff.py | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 3d5542b65529df2..dbb18d39d460ffe 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -191,8 +191,7 @@ def main():
             diff_string = "".join(diff)
             if len(diff_string) > 0:
                 sys.stdout.write(diff_string)
-                if args.non_zero_exit_code:
-                    sys.exit(1)
+                sys.exit(1)
 
 
 if __name__ == "__main__":

>From 657cdc255913ef694a7691e2347a331047d8fd3f Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Wed, 8 Nov 2023 13:06:30 -0800
Subject: [PATCH 4/4] Update clang/docs/ClangFormat.rst

---
 clang/docs/ClangFormat.rst | 1 -
 1 file changed, 1 deletion(-)

diff --git a/clang/docs/ClangFormat.rst b/clang/docs/ClangFormat.rst
index 42864b90e408037..f52f35550d03eb6 100644
--- a/clang/docs/ClangFormat.rst
+++ b/clang/docs/ClangFormat.rst
@@ -325,7 +325,6 @@ output of a unified diff and reformats all contained lines with
                           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-formatfile to use.
     -binary BINARY        location of binary to use for clang-format
-    -non-zero-exit-code   exit with a non-zero status if formatting changes are necessary
 
 To reformat all the lines in the latest Mercurial/:program:`hg` commit, do:
 



More information about the cfe-commits mailing list