Author: mydeveloperday
Date: 2019-12-06T17:26:02Z
New Revision: b8e03ff50389d83dabf800203f689e227697c996

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

LOG: [clang-format] update trailing newline treatment in clang-format.py

Summary:
The current clang-format.py does not handle trailing newlines at the end of a 
file correctly.
Trailing empty lines get removed except one.
As far as I understand this is because clang-format gets fed from stdin and 
writes to stdout when called from clang-format.py.
In a "normal" file (with no trailing empty lines) the string that gets passed 
to clang-format does not contain a trailing '\n' after the '\n'.join from 
python.
The clang-format binary does not add a trailing newline to input from stdin, 
but (if there are multiple trailing '\n', all except one get removed).

When reading back this means that we see in python from a "normal" file a 
string with no trailing '\n'. From a file with (potentially multiple) empty 
line(s) at the end, we get a string with one trailing '\n' back in python. In 
the former case all is fine, in the latter case split('\n') makes one empty 
line at the end of the file out of the clang-format output. Desired would be 
instead that the **file** ends with a newline, but not with an empty line.

For the case that a user specifies a range to format (and wants to keep 
trailing empty lines) I did **not** try to fix this by simply removing all 
trailing newlines from the clang-format output. Instead, I add a '\n' to the 
unformatted file content (i.e. newline-terminate what is passed to 
clang-format) and then strip off the last newline from the output (which itself 
is now for sure the newline termination of the clang-format output).

(Should this get approved, I'll need someone to help me land this.)

Reviewers: klimek, MyDeveloperDay

Reviewed By: MyDeveloperDay

Patch By: pseyfert

Subscribers: cfe-commits, llvm-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D70864
update trailing newline treatment in clang-format.py

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-format/clang-format.py 
b/clang/tools/clang-format/clang-format.py
index 976c222df055..596270cb55d3 100644
--- a/clang/tools/clang-format/clang-format.py
+++ b/clang/tools/clang-format/clang-format.py
@@ -70,7 +70,8 @@ def main():
   # Get the current text.
   encoding = vim.eval("&encoding")
   buf = get_buffer(encoding)
-  text = '\n'.join(buf)
+  # Join the buffer into a single string with a terminating newline
+  text = '\n'.join(buf) + '\n'
 
   # Determine range to format.
   if vim.eval('exists("l:lines")') == '1':
@@ -129,7 +130,10 @@ def main():
   else:
     lines = stdout.decode(encoding).split('\n')
     output = json.loads(lines[0])
-    lines = lines[1:]
+    # Strip off the trailing newline (added above).
+    # This maintains trailing empty lines present in the buffer if
+    # the -lines specification requests them to remain unchanged.
+    lines = lines[1:-1]
     sequence = 
diff lib.SequenceMatcher(None, buf, lines)
     for op in reversed(sequence.get_opcodes()):
       if op[0] is not 'equal':


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to