[PATCH] D111273: [clang-format-diff] Fix missing formatting for zero length git diff lines

Zequan Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 6 14:29:32 PDT 2021


zequanwu created this revision.
zequanwu added reviewers: rnk, phosek.
zequanwu requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

If we only delete lines that are outer block statements (if, while, etc),
clang-format-diff.py can't format the statements inside the block statements.

An example to repro:

1. Delete the if statment at line 118 in llvm/lib/CodeGen/Analysis.cpp.
2. Run `git diff -U0 --no-color HEAD^ | clang/tools/clang-format/clang-format-diff.py -i -p1`

It fails to format the statement after if.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D111273

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
@@ -90,9 +90,11 @@
       line_count = 1
       if match.group(3):
         line_count = int(match.group(3))
-      if line_count == 0:
-        continue
-      end_line = start_line + line_count - 1
+      # Also format lines range if line_count is 0 in case of deleting
+      # surrounding statements.
+      end_line = start_line
+      if line_count != 0:
+        end_line += line_count - 1
       lines_by_file.setdefault(filename, []).extend(
           ['-lines', str(start_line) + ':' + str(end_line)])
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111273.377686.patch
Type: text/x-patch
Size: 736 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211006/1a94ac51/attachment.bin>


More information about the cfe-commits mailing list