[clang-format] `SeparateDefinitionBlocks: Always` separates trailing block comments (relevant for `// NOLINTEND`)
Formatting with SeparateDefinitionBlocks: Always turns this example code
// NOLINTBEGIN
int x = 1;
int y = 2;
// NOLINTEND
void some_function() {}
into
// NOLINTBEGIN
int x = 1;
int y = 2;
// NOLINTEND
void some_function() {}
which is of course not what anyone would want.
@llvm/issue-subscribers-clang-format
Author: Damian Höster (damster101)
// NOLINTBEGIN
int x = 1;
int y = 2;
// NOLINTEND
void some_function() {}
into
// NOLINTBEGIN
int x = 1;
int y = 2;
// NOLINTEND
void some_function() {}
which is of course not what anyone would want.
I’m working on a fix for this issue and plan to open a PR soon.
Not a bug IMO. We have OneLineFormatOffRegex for this.
Not a bug IMO. We have OneLineFormatOffRegex for this.
That indeed provides a fix for that specific case, but
- I don't think users should be expected to research and implement this fix just because they want to use some standard clang-format option.
- I actually think trailing block comments should probably be generally allowed. Sure, when definition blocks are not already separated, there is no way to tell apart trailing from leading block comments, but in case they are already separated by an empty newline, trailing block comments are clearly identifiable.
- I actually think trailing block comments should probably be generally allowed. Sure, when definition blocks are not already separated, there is no way to tell apart trailing from leading block comments, but in case they are already separated by an empty newline, trailing block comments are clearly identifiable.
I agree. To clarify, SeparateDefinitionBlocks should not reformat the following:
// any single/multi-line comment
int x = 1;
int y = 2;
// any single/multi-line comment
void some_function() {}
Looking at https://github.com/llvm/llvm-project/blob/d46af3a78dea1dfb0a6f319e7abbcd6b6e2df64e/clang/lib/Format/DefinitionBlockSeparator.cpp#L149-L152
this seems to be on purpose, and you yourself added the exception for // clang-format on.
So we either add the exception for NOLINTEND as it falls in the same category. Or need to tweak the check a bit more. Maybe looking at Next and NewLinesBefore?
Looking at
llvm-project/clang/lib/Format/DefinitionBlockSeparator.cpp
Lines 149 to 152 in d46af3a
if (const auto *Tok = OperateLine->First; Tok->is(tok::comment) && !isClangFormatOn(Tok->TokenText)) { return true; }
this seems to be on purpose, and you yourself added the exception for
// clang-format on. So we either add the exception forNOLINTENDas it falls in the same category. Or need to tweak the check a bit more. Maybe looking atNextandNewLinesBefore?
clang-format "owns" // clang-format on but not // NOLINTEND, so I would check NewlinesBefore the comment and NextLine like you said. See https://github.com/llvm/llvm-project/pull/190741/changes#r3055674622.