Skip to content

Commit 617602d

Browse files
committed
[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.
1 parent 942cb24 commit 617602d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,8 +1702,11 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
17021702
// Special case for generic selection expressions, its comma-separated
17031703
// expressions are not aligned to the opening paren like regular calls, but
17041704
// rather continuation-indented relative to the _Generic keyword.
1705-
if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic))
1706-
NewParenState.Indent = CurrentState.LastSpace;
1705+
if (Previous && Previous->endsSequence(tok::l_paren, tok::kw__Generic) &&
1706+
State.Stack.size() > 1) {
1707+
NewParenState.Indent = State.Stack[State.Stack.size() - 2].Indent +
1708+
Style.ContinuationIndentWidth;
1709+
}
17071710

17081711
if ((shouldUnindentNextOperator(Current) ||
17091712
(Previous &&

clang/unittests/Format/FormatTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24147,6 +24147,15 @@ TEST_F(FormatTest, C11Generic) {
2414724147
" double _Complex: dc,\n"
2414824148
" long double _Complex: ldc)");
2414924149

24150+
verifyFormat("while (_Generic(x, //\n"
24151+
" long: x)(x) > x) {\n"
24152+
"}");
24153+
verifyFormat("while (_Generic(x, //\n"
24154+
" long: x)(x)) {\n"
24155+
"}");
24156+
verifyFormat("x(_Generic(x, //\n"
24157+
" long: x)(x));");
24158+
2415024159
FormatStyle Style = getLLVMStyle();
2415124160
Style.ColumnLimit = 40;
2415224161
verifyFormat("#define LIMIT_MAX(T) \\\n"

0 commit comments

Comments
 (0)