llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] `SeparateDefinitionBlocks: Always` separates trailing block comments (relevant for `// NOLINTEND`)

Open damster101 opened this issue 8 months ago • 7 comments

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.

damster101 avatar Mar 30 '26 13:03 damster101

@llvm/issue-subscribers-clang-format

Author: Damian Höster (damster101)

`SeparateDefinitionBlocks: Always`, formatting 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.

llvmbot avatar Mar 30 '26 13:03 llvmbot

I’m working on a fix for this issue and plan to open a PR soon.

heturing avatar Apr 07 '26 06:04 heturing

Not a bug IMO. We have OneLineFormatOffRegex for this.

owenca avatar Apr 08 '26 05:04 owenca

Not a bug IMO. We have OneLineFormatOffRegex for this.

That indeed provides a fix for that specific case, but

  1. 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.
  2. 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.

damster101 avatar Apr 08 '26 05:04 damster101

  1. 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() {}

owenca avatar Apr 08 '26 06:04 owenca

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?

HazardyKnusperkeks avatar Apr 08 '26 09:04 HazardyKnusperkeks

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 for NOLINTEND as it falls in the same category. Or need to tweak the check a bit more. Maybe looking at Next and NewLinesBefore?

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.

owenca avatar Apr 09 '26 05:04 owenca