[clang] f931692 - [clang-format-diff] Fix missing formatting for zero length git diff lines

Zequan Wu via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 8 10:26:00 PDT 2021


Author: Zequan Wu
Date: 2021-10-08T10:25:54-07:00
New Revision: f93169226a298f8fb22d768671d5564030c0ffa9

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

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

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.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/clang-format-
diff .py b/clang/tools/clang-format/clang-format-
diff .py
index ecad0155d9bef..28ac0275615c0 100755
--- a/clang/tools/clang-format/clang-format-
diff .py
+++ b/clang/tools/clang-format/clang-format-
diff .py
@@ -90,9 +90,11 @@ def main():
       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)])
 

diff  --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 0233ceb3a868d..30184725e1223 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -321,8 +321,9 @@ def extract_lines(patch_file):
       line_count = 1
       if match.group(3):
         line_count = int(match.group(3))
-      if line_count > 0:
-        matches.setdefault(filename, []).append(Range(start_line, line_count))
+      if line_count == 0:
+        line_count = 1
+      matches.setdefault(filename, []).append(Range(start_line, line_count))
   return matches
 
 


        


More information about the cfe-commits mailing list