[clang-format] Handle generic selections inside parentheses (#79785)

new

```C
while (_Generic(x, //
           long: x)(x) > x) {
}
while (_Generic(x, //
           long: x)(x)) {
}
```

old

```C
while (_Generic(x, //
       long: x)(x) > x) {
}
while (_Generic(x, //
    long: x)(x)) {
}
```

In the first case above, the second line previously aligned to the open
parenthesis.  The 4 spaces did not get added by the fallback line near
the end of getNewLineColumn because there was already some indentaton.
Now the spaces get added explicitly.

In the second case above, without the fake parentheses, the second line
did not respect the outer parentheses, because the LastSpace field did
not get set without the fake parentheses.  Now the indentation of the
outer level is used instead.
This commit is contained in:
sstwcw
2024-01-29 05:24:51 +00:00
parent 942cb2427a
commit 617602d4f2
2 changed files with 14 additions and 2 deletions

View File

@@ -1702,8 +1702,11 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
// Special case for generic selection expressions, its comma-separated
// expressions are not aligned to the opening paren like regular calls, but
// rather continuation-indented relative to the _Generic keyword.
if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic))
NewParenState.Indent = CurrentState.LastSpace;
if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic) &&
State.Stack.size() > 1) {
NewParenState.Indent = State.Stack[State.Stack.size() - 2].Indent +
Style.ContinuationIndentWidth;
}
if ((shouldUnindentNextOperator(Current) ||
(Previous &&

View File

@@ -24147,6 +24147,15 @@ TEST_F(FormatTest, C11Generic) {
" double _Complex: dc,\n"
" long double _Complex: ldc)");
verifyFormat("while (_Generic(x, //\n"
" long: x)(x) > x) {\n"
"}");
verifyFormat("while (_Generic(x, //\n"
" long: x)(x)) {\n"
"}");
verifyFormat("x(_Generic(x, //\n"
" long: x)(x));");
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = 40;
verifyFormat("#define LIMIT_MAX(T) \\\n"