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

Tamir Duberstein via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 17 12:31:15 PST 2023


tamird created this revision.
tamird added a reviewer: hans.
Herald added a project: All.
tamird requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D144291

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


Index: clang/tools/clang-format/clang-format-diff.py
===================================================================
--- clang/tools/clang-format/clang-format-diff.py
+++ clang/tools/clang-format/clang-format-diff.py
@@ -84,12 +84,19 @@
       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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144291.498477.patch
Type: text/x-patch
Size: 971 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230217/31d72259/attachment.bin>


More information about the cfe-commits mailing list