Skip to content

[clang-format] Formatting requires two passes to stabilize due to column miscalculation #140535

Open
@rocfu

Description

@rocfu

Hi,

I've discovered that the following C++ code requires two formatting passes to stabilize:

Source code: column 120

int aaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbb; //testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte

After the first pass, a space is added after the //, making it column 121

int aaaaaaaaaaaaaaa = bbbbbbbbbbbbbbbbbbbbbbbbbbbb; // testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte

After the second pass, the line breaks

int aaaaaaaaaaaaaaa =
    bbbbbbbbbbbbbbbbbbbbbbbbbbbb; // testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttestte

I suspect that the first formatting pass doesn't account for the added space after the // when calculating the column, which causes the second pass to detect that column=121 > ColumnLimit, resulting in the line break.

My environment:

  • clang-format version 17.0.6 and version 18.1.4

.clang-format configuration:

Language:     Cpp 
ColumnLimit:  120    
ReflowComments: true
SpacesInLineCommentPrefix:
  Minimum : 1
  Maximum : 1

Thank you for looking into this issue!

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions