[clang] [clang-format] Exit clang-format-diff only after all diffs are printed (PR #86776)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 26 23:31:55 PDT 2024


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/86776

None

>From 216681ceb6346b56e8013935b2d3938bde5039ea Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 26 Mar 2024 23:27:51 -0700
Subject: [PATCH] [clang-format] Exit clang-format-diff only after all diffs
 are printed

---
 clang/tools/clang-format/clang-format-diff.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 0a2c24743678d0..3a74b90e731578 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -138,6 +138,7 @@ def main():
             )
 
     # Reformat files containing changes in place.
+    has_diff = False
     for filename, lines in lines_by_file.items():
         if args.i and args.verbose:
             print("Formatting {}".format(filename))
@@ -169,7 +170,7 @@ def main():
 
         stdout, stderr = p.communicate()
         if p.returncode != 0:
-            sys.exit(p.returncode)
+            return p.returncode
 
         if not args.i:
             with open(filename) as f:
@@ -185,9 +186,12 @@ def main():
             )
             diff_string = "".join(diff)
             if len(diff_string) > 0:
+                has_diff = True
                 sys.stdout.write(diff_string)
-                sys.exit(1)
+
+    if has_diff:
+        return 1
 
 
 if __name__ == "__main__":
-    main()
+    sys.exit(main())



More information about the cfe-commits mailing list