llvm / llvm/llvm-project

[clang-formatter] Some code fragments does not preserve formatting

Open
#211,557 1 comment 0 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following code examples are expected not to change formatting with `OverEmptyLines > 2`:
```
// Scope-based alignment with trailing comments on }; lines and gaps.
struct A {
int a; /* a */
int b;
int c; /* c */
}; /* end
A */
int i; /* comment */
int j;
int k; /* k */
struct M {
int d ; /* d */
int abcd; /* abcd */
int e ;
}; /* end
M */

// OverEmptyLines should not align trailing comments across ); boundaries.
x = (5, // comm 5
10, // comm 4
15 // comm 3
);
y = (longvalue, // comm a
othervalue, // comm b
third // comm c
);

// A trailing comment on a ); line aligns with its block, then breaks.
auto a = Foo(
x, // param 1
y, // param 2
z // param 3
); // end Foo
auto b = Bar(
longname, // param 1
othername, // param 2
third // param 3
);

// Within a single expression, ), on its own line also breaks alignment.
result = (Foo(
x, // param 1
y, // param 2
z // param 3
),
Bar(
longname, // param 1
othername, // param 2
third // param 3
));

// Trailing comments on ), and )); lines align with their respective blocks.
result = (Foo(
x, // param 1
y, // param 2
z // param 3
), // end Foo
Bar(
longname, // param 1
othername, // param 2
third // param 3
)); // end Bar
```
I think it's more feature request rather than a bug but i'll leave to maintainers to decide the desired formatting in those cases as now these situation are not properly covered by the test suite.

.clang-format settings:
```
BasedOnStyle: LLVM
ColumnLimit: 0
AlignTrailingComments:
OverEmptyLines: 3
```

Discovered while working on https://github.com/llvm/llvm-project/pull/208324.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the examples with clang-format using the shown LLVM-based settings and AlignTrailingComments: OverEmptyLines: 3. Trace the formatting behavior for trailing comments across ); and )); boundaries, then add regression coverage so the examples preserve their expected alignment and gaps.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.