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 generates extra trailing spaces in the last line with InsertNewlineAtEOF enabled #108333

Closed
kevinptt0323 opened this issue Sep 12, 2024 · 5 comments · Fixed by #108513
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior clang-format release:backport

Comments

@kevinptt0323
Copy link

kevinptt0323 commented Sep 12, 2024

clang-format generates extra trailing spaces in the last line when the following three statements are true:

  1. InsertNewlineAtEOF is true.
  2. The file does not end with EOL (0x0a)
  3. The argument -lines is specified

Minimal reproducible example:

$ clang-format --version
Ubuntu clang-format version 18.1.3 (1ubuntu1)
$ cat test.cpp
namespace foo {
int bar;
} // namespace foo$ xxd test.cpp
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f              mespace foo
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a 2020 2020  mespace foo.    
00000030: 2020 2020 2020 2020 2020 2020 2020                     
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a            mespace foo.
$ clang-format -style '{InsertNewlineAtEOF: false}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f              mespace foo
@owenca
Copy link
Contributor

owenca commented Sep 12, 2024

How did you run clang-format?

@owenca owenca added the incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) label Sep 12, 2024
@kevinptt0323
Copy link
Author

How did you run clang-format?

Like google/vim-codefmt#217 mentioned, I use https://github.com/google/vim-codefmt with Vim to format the code. The plugin passes the buffer content to clang-format and replace the whole buffer.

I've narrowed down the issue to the minimal reproducible example in #108333 (comment). The key point is that the last line does not contain EOL (0x0a), which causes the bug.

Another one-line example:

$ echo -n $'namespace {\nint a;\n} // namespace' | clang-format -style '{InsertNewlineAtEOF: true}' -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 7b0a 696e 7420  namespace {.int
00000010: 613b 0a7d 202f 2f20 6e61 6d65 7370 6163  a;.} // namespac
00000020: 650a 2020 2020 2020 2020 2020 2020 2020  e.

@owenca owenca added bug Indicates an unexpected problem or unintended behavior and removed incomplete Issue not complete (e.g. missing a reproducer, build arguments, etc.) labels Sep 13, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 13, 2024

@llvm/issue-subscribers-bug

Author: Kevin Lin (kevinptt0323)

clang-format generates extra trailing spaces in the last line when the following three statements are true:
  1. InsertNewlineAtEOF is true.
  2. The file does not end with EOL (0x0a)
  3. The argument -lines is specified

Minimal reproducible example:

$ clang-format --version
Ubuntu clang-format version 18.1.3 (1ubuntu1)
$ cat test.cpp
namespace foo {
int bar;
} // namespace foo$ xxd test.cpp
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f              mespace foo
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a 2020 2020  mespace foo.    
00000030: 2020 2020 2020 2020 2020 2020 2020                     
$ clang-format -style '{InsertNewlineAtEOF: true}' test.cpp | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f0a            mespace foo.
$ clang-format -style '{InsertNewlineAtEOF: false}' test.cpp -lines 1:3 | xxd
00000000: 6e61 6d65 7370 6163 6520 666f 6f20 7b0a  namespace foo {.
00000010: 696e 7420 6261 723b 0a7d 202f 2f20 6e61  int bar;.} // na
00000020: 6d65 7370 6163 6520 666f 6f              mespace foo

@owenca owenca self-assigned this Sep 13, 2024
owenca added a commit to owenca/llvm-project that referenced this issue Sep 13, 2024
@owenca owenca added this to the LLVM 19.X Release milestone Sep 18, 2024
@owenca
Copy link
Contributor

owenca commented Sep 18, 2024

/cherry-pick 7153a4b

llvmbot pushed a commit to llvmbot/llvm-project that referenced this issue Sep 18, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 18, 2024

/pull-request #109170

tmsri pushed a commit to tmsri/llvm-project that referenced this issue Sep 19, 2024
tru pushed a commit to llvmbot/llvm-project that referenced this issue Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior clang-format release:backport
Projects
Development

Successfully merging a pull request may close this issue.

4 participants