[clang] 5056394 - [clang-format-diff] Correctly parse start-of-file diffs

Leonard Chan via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 27 12:03:47 PST 2023


Author: Tamir Duberstein
Date: 2023-02-27T20:02:51Z
New Revision: 50563944ab962b58a1e00763ce16d8c712965c6d

URL: https://github.com/llvm/llvm-project/commit/50563944ab962b58a1e00763ce16d8c712965c6d
DIFF: https://github.com/llvm/llvm-project/commit/50563944ab962b58a1e00763ce16d8c712965c6d.diff

LOG: [clang-format-diff] Correctly parse start-of-file diffs

Handle the case where the diff is a pure removal of lines. Before this
change start_line would end up as 0 which is rejected by clang-format.

Submitting on behalf of @tamird.

Differential Revision: https://reviews.llvm.org/D144291

Added: 
    

Modified: 
    clang/tools/clang-format/clang-format-diff.py

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/clang-format-
diff .py b/clang/tools/clang-format/clang-format-
diff .py
index 1f6ff0fe295f..1dcc8689d5fe 100755
--- a/clang/tools/clang-format/clang-format-
diff .py
+++ b/clang/tools/clang-format/clang-format-
diff .py
@@ -84,12 +84,19 @@ def main():
       if not re.match('^%s$' % args.iregex, filename, re.IGNORECASE):
         continue
 
-    match = re.search(r'^@@.*\+(\d+)(,(\d+))?', line)
+    match = re.search(r'^@@.*\+(\d+)(?:,(\d+))?', line)
     if match:
       start_line = int(match.group(1))
       line_count = 1
-      if match.group(3):
-        line_count = int(match.group(3))
+      if match.group(2):
+        line_count = int(match.group(2))
+        # The input is something like
+        #
+        # @@ -1, +0,0 @@
+        #
+        # which means no lines were added.
+        if line_count == 0:
+          continue
       # Also format lines range if line_count is 0 in case of deleting
       # surrounding statements.
       end_line = start_line


        


More information about the cfe-commits mailing list