@@ -430,9 +430,9 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
430
430
unsigned StoredPosition = Tokens->getPosition();
431
431
auto *Next = Tokens->getNextNonComment();
432
432
FormatTok = Tokens->setPosition(StoredPosition);
433
- if (Next->isNot (tok::colon)) {
434
- // default not followed by ':' is not a case label; treat it like
435
- // an identifier.
433
+ if (! Next->isOneOf (tok::colon, tok::arrow )) {
434
+ // default not followed by `:` or `->` is not a case label; treat it
435
+ // like an identifier.
436
436
parseStructuralElement();
437
437
break;
438
438
}
@@ -451,6 +451,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
451
451
}
452
452
if (!SwitchLabelEncountered &&
453
453
(Style.IndentCaseLabels ||
454
+ (OpeningBrace && OpeningBrace->is(TT_SwitchExpressionLBrace)) ||
454
455
(Line->InPPDirective && Line->Level == 1))) {
455
456
++Line->Level;
456
457
}
@@ -1519,24 +1520,32 @@ void UnwrappedLineParser::parseStructuralElement(
1519
1520
// 'switch: string' field declaration.
1520
1521
break;
1521
1522
}
1522
- parseSwitch();
1523
+ parseSwitch(/*IsExpr=*/false );
1523
1524
return;
1524
- case tok::kw_default:
1525
+ case tok::kw_default: {
1525
1526
// In Verilog default along with other labels are handled in the next loop.
1526
1527
if (Style.isVerilog())
1527
1528
break;
1528
1529
if (Style.isJavaScript() && Line->MustBeDeclaration) {
1529
1530
// 'default: string' field declaration.
1530
1531
break;
1531
1532
}
1533
+ auto *Default = FormatTok;
1532
1534
nextToken();
1533
1535
if (FormatTok->is(tok::colon)) {
1534
1536
FormatTok->setFinalizedType(TT_CaseLabelColon);
1535
1537
parseLabel();
1536
1538
return;
1537
1539
}
1540
+ if (FormatTok->is(tok::arrow)) {
1541
+ FormatTok->setFinalizedType(TT_CaseLabelArrow);
1542
+ Default->setFinalizedType(TT_SwitchExpressionLabel);
1543
+ parseLabel();
1544
+ return;
1545
+ }
1538
1546
// e.g. "default void f() {}" in a Java interface.
1539
1547
break;
1548
+ }
1540
1549
case tok::kw_case:
1541
1550
// Proto: there are no switch/case statements.
1542
1551
if (Style.Language == FormatStyle::LK_Proto) {
@@ -2061,6 +2070,11 @@ void UnwrappedLineParser::parseStructuralElement(
2061
2070
case tok::kw_new:
2062
2071
parseNew();
2063
2072
break;
2073
+ case tok::kw_switch:
2074
+ if (Style.Language == FormatStyle::LK_Java)
2075
+ parseSwitch(/*IsExpr=*/true);
2076
+ nextToken();
2077
+ break;
2064
2078
case tok::kw_case:
2065
2079
// Proto: there are no switch/case statements.
2066
2080
if (Style.Language == FormatStyle::LK_Proto) {
@@ -2583,6 +2597,9 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
2583
2597
else
2584
2598
nextToken();
2585
2599
break;
2600
+ case tok::kw_switch:
2601
+ parseSwitch(/*IsExpr=*/true);
2602
+ break;
2586
2603
case tok::kw_requires: {
2587
2604
auto RequiresToken = FormatTok;
2588
2605
nextToken();
@@ -3240,6 +3257,7 @@ void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
3240
3257
3241
3258
void UnwrappedLineParser::parseCaseLabel() {
3242
3259
assert(FormatTok->is(tok::kw_case) && "'case' expected");
3260
+ auto *Case = FormatTok;
3243
3261
3244
3262
// FIXME: fix handling of complex expressions here.
3245
3263
do {
@@ -3248,11 +3266,16 @@ void UnwrappedLineParser::parseCaseLabel() {
3248
3266
FormatTok->setFinalizedType(TT_CaseLabelColon);
3249
3267
break;
3250
3268
}
3269
+ if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::arrow)) {
3270
+ FormatTok->setFinalizedType(TT_CaseLabelArrow);
3271
+ Case->setFinalizedType(TT_SwitchExpressionLabel);
3272
+ break;
3273
+ }
3251
3274
} while (!eof());
3252
3275
parseLabel();
3253
3276
}
3254
3277
3255
- void UnwrappedLineParser::parseSwitch() {
3278
+ void UnwrappedLineParser::parseSwitch(bool IsExpr ) {
3256
3279
assert(FormatTok->is(tok::kw_switch) && "'switch' expected");
3257
3280
nextToken();
3258
3281
if (FormatTok->is(tok::l_paren))
@@ -3262,10 +3285,15 @@ void UnwrappedLineParser::parseSwitch() {
3262
3285
3263
3286
if (FormatTok->is(tok::l_brace)) {
3264
3287
CompoundStatementIndenter Indenter(this, Style, Line->Level);
3265
- FormatTok->setFinalizedType(TT_ControlStatementLBrace);
3266
- parseBlock();
3288
+ FormatTok->setFinalizedType(IsExpr ? TT_SwitchExpressionLBrace
3289
+ : TT_ControlStatementLBrace);
3290
+ if (IsExpr)
3291
+ parseChildBlock();
3292
+ else
3293
+ parseBlock();
3267
3294
setPreviousRBraceType(TT_ControlStatementRBrace);
3268
- addUnwrappedLine();
3295
+ if (!IsExpr)
3296
+ addUnwrappedLine();
3269
3297
} else {
3270
3298
addUnwrappedLine();
3271
3299
++Line->Level;
0 commit comments