Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-format option InsertNewlineAtEOF does not add newline #63150

Closed
jasonfsmitty opened this issue Jun 6, 2023 · 4 comments
Closed

clang-format option InsertNewlineAtEOF does not add newline #63150

jasonfsmitty opened this issue Jun 6, 2023 · 4 comments

Comments

@jasonfsmitty
Copy link

On Fedora 36, running a local build from source of LLVM 16.0.4

The clang format option InsertNewlineAtEOF : true is not being applied, instead the newline is being removed:

[smithj1]$ /opt/llvm-16.0.4/bin/clang-format --version
clang-format version 16.0.4
[smithj1]$ printf "namespace aaa {}\n\n"
namespace aaa {}

[smithj1]$ printf "namespace aaa {}\n\n" | /opt/llvm-16.0.4/bin/clang-format --style="{ InsertNewlineAtEOF : true }"
namespace aaa {}
[smithj1]$ printf "namespace aaa {}\n\n" | /opt/llvm-16.0.4/bin/clang-format --style="{ InsertNewlineAtEOF : false }"
namespace aaa {}
[smithj1]$

This also happens in our codebase, where we leverage .clang-format files to auto-format sections of our repo:

[smithj1]$ grep NewlineAtEOF .clang-format 
InsertNewlineAtEOF: true
[smithj1]$ cat test.cpp 
#include <iostream>

int main(int argc, char* argv[])
{
	std::cout << "Hello World" << std::endl;
	return 0;
}

[smithj1]$ /opt/llvm-16.0.4/bin/clang-format --style=file -Werror test.cpp 
#include <iostream>

int main(int argc, char* argv[])
{
	std::cout << "Hello World" << std::endl;
	return 0;
}
[smithj1]$

One counter-point that I can't explain, is that clang-format does format properly when using plugins in VS Code and vim.

For vim, I'm using the vim-clang-format plugin, with the following in .vimrc:

let g:clang_format#command = "/opt/llvm-16.0.4/bin/clang-format"
let g:clang_format#auto_format = 1
let g:clang_format#detect_style_file = 1
let g:clang_format#auto_filetypes = [ "c", "cpp" ]
let g:clang_format#enable_fallback_style = 0

If I open a file and save it with vim, it formats the file correctly including adding the newline at EOF.

@jasonfsmitty
Copy link
Author

jasonfsmitty commented Jun 6, 2023

#56054 is related (it's where I got the 'namespace aaa {}' example), but I think that request pre-dates the 16.0.0 release, which added the InsertNewlineAtEOF option.

#61551 is also asking for a newline at EOF, but the response refers the submitter to InsertNewlineAtEOF and was closed.

@llvmbot
Copy link
Collaborator

llvmbot commented Jun 6, 2023

@llvm/issue-subscribers-clang-format

@owenca
Copy link
Contributor

owenca commented Jun 6, 2023

On Fedora 36, running a local build from source of LLVM 16.0.4

The clang format option InsertNewlineAtEOF : true is not being applied, instead the newline is being removed:

[smithj1]$ /opt/llvm-16.0.4/bin/clang-format --version
clang-format version 16.0.4
[smithj1]$ printf "namespace aaa {}\n\n"
namespace aaa {}

[smithj1]$ printf "namespace aaa {}\n\n" | /opt/llvm-16.0.4/bin/clang-format --style="{ InsertNewlineAtEOF : true }"
namespace aaa {}
[smithj1]$ printf "namespace aaa {}\n\n" | /opt/llvm-16.0.4/bin/clang-format --style="{ InsertNewlineAtEOF : false }"
namespace aaa {}
[smithj1]$

InsertNewlineAtEOF inserts a newline only if the input doesn't end with one:

$ printf "namespace aaa {}"
namespace aaa {}$ printf "namespace aaa {}" | clang-format -style='{InsertNewlineAtEOF: true}'
namespace aaa {}
$ printf "namespace aaa {}\n"
namespace aaa {}
$ printf "namespace aaa {}\n" | clang-format -style='{InsertNewlineAtEOF: true}'
namespace aaa {}
$ 

It seems that clang-format always removes empty lines at the end of the input. Please file another issue if you believe it shouldn't.

@owenca owenca closed this as completed Jun 6, 2023
@owenca owenca added the awaiting-review Has pending Phabricator review label Jun 6, 2023
@owenca
Copy link
Contributor

owenca commented Jun 6, 2023

https://reviews.llvm.org/D152305

owenca added a commit that referenced this issue Jun 7, 2023
Adds an option KeepEmptyLinesAtEOF to keep empty lines (up to
MaxEmptyLinesToKeep) before EOF. This remedies the probably unintentional
change in behavior introduced in 3d3ea84, which started to always
remove empty lines before EOF.

Fixes #56054.
Fixes #63150.

Differential Revision: https://reviews.llvm.org/D152305
@owenca owenca removed the awaiting-review Has pending Phabricator review label Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants