@@ -430,9 +430,9 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
430430 unsigned StoredPosition = Tokens->getPosition ();
431431 auto *Next = Tokens->getNextNonComment ();
432432 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.
436436 parseStructuralElement ();
437437 break ;
438438 }
@@ -451,6 +451,7 @@ bool UnwrappedLineParser::parseLevel(const FormatToken *OpeningBrace,
451451 }
452452 if (!SwitchLabelEncountered &&
453453 (Style.IndentCaseLabels ||
454+ (OpeningBrace && OpeningBrace->is (TT_SwitchExpressionLBrace)) ||
454455 (Line->InPPDirective && Line->Level == 1 ))) {
455456 ++Line->Level ;
456457 }
@@ -1519,24 +1520,32 @@ void UnwrappedLineParser::parseStructuralElement(
15191520 // 'switch: string' field declaration.
15201521 break ;
15211522 }
1522- parseSwitch ();
1523+ parseSwitch (/* IsExpr= */ false );
15231524 return ;
1524- case tok::kw_default:
1525+ case tok::kw_default: {
15251526 // In Verilog default along with other labels are handled in the next loop.
15261527 if (Style.isVerilog ())
15271528 break ;
15281529 if (Style.isJavaScript () && Line->MustBeDeclaration ) {
15291530 // 'default: string' field declaration.
15301531 break ;
15311532 }
1533+ auto *Default = FormatTok;
15321534 nextToken ();
15331535 if (FormatTok->is (tok::colon)) {
15341536 FormatTok->setFinalizedType (TT_CaseLabelColon);
15351537 parseLabel ();
15361538 return ;
15371539 }
1540+ if (FormatTok->is (tok::arrow)) {
1541+ FormatTok->setFinalizedType (TT_CaseLabelArrow);
1542+ Default->setFinalizedType (TT_SwitchExpressionLabel);
1543+ parseLabel ();
1544+ return ;
1545+ }
15381546 // e.g. "default void f() {}" in a Java interface.
15391547 break ;
1548+ }
15401549 case tok::kw_case:
15411550 // Proto: there are no switch/case statements.
15421551 if (Style.Language == FormatStyle::LK_Proto) {
@@ -2062,6 +2071,11 @@ void UnwrappedLineParser::parseStructuralElement(
20622071 case tok::kw_new:
20632072 parseNew ();
20642073 break ;
2074+ case tok::kw_switch:
2075+ if (Style.Language == FormatStyle::LK_Java)
2076+ parseSwitch (/* IsExpr=*/ true );
2077+ nextToken ();
2078+ break ;
20652079 case tok::kw_case:
20662080 // Proto: there are no switch/case statements.
20672081 if (Style.Language == FormatStyle::LK_Proto) {
@@ -2589,6 +2603,9 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25892603 else
25902604 nextToken ();
25912605 break ;
2606+ case tok::kw_switch:
2607+ parseSwitch (/* IsExpr=*/ true );
2608+ break ;
25922609 case tok::kw_requires: {
25932610 auto RequiresToken = FormatTok;
25942611 nextToken ();
@@ -3246,6 +3263,7 @@ void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
32463263
32473264void UnwrappedLineParser::parseCaseLabel () {
32483265 assert (FormatTok->is (tok::kw_case) && " 'case' expected" );
3266+ auto *Case = FormatTok;
32493267
32503268 // FIXME: fix handling of complex expressions here.
32513269 do {
@@ -3254,11 +3272,16 @@ void UnwrappedLineParser::parseCaseLabel() {
32543272 FormatTok->setFinalizedType (TT_CaseLabelColon);
32553273 break ;
32563274 }
3275+ if (Style.Language == FormatStyle::LK_Java && FormatTok->is (tok::arrow)) {
3276+ FormatTok->setFinalizedType (TT_CaseLabelArrow);
3277+ Case->setFinalizedType (TT_SwitchExpressionLabel);
3278+ break ;
3279+ }
32573280 } while (!eof ());
32583281 parseLabel ();
32593282}
32603283
3261- void UnwrappedLineParser::parseSwitch () {
3284+ void UnwrappedLineParser::parseSwitch (bool IsExpr ) {
32623285 assert (FormatTok->is (tok::kw_switch) && " 'switch' expected" );
32633286 nextToken ();
32643287 if (FormatTok->is (tok::l_paren))
@@ -3268,10 +3291,15 @@ void UnwrappedLineParser::parseSwitch() {
32683291
32693292 if (FormatTok->is (tok::l_brace)) {
32703293 CompoundStatementIndenter Indenter (this , Style, Line->Level );
3271- FormatTok->setFinalizedType (TT_ControlStatementLBrace);
3272- parseBlock ();
3294+ FormatTok->setFinalizedType (IsExpr ? TT_SwitchExpressionLBrace
3295+ : TT_ControlStatementLBrace);
3296+ if (IsExpr)
3297+ parseChildBlock ();
3298+ else
3299+ parseBlock ();
32733300 setPreviousRBraceType (TT_ControlStatementRBrace);
3274- addUnwrappedLine ();
3301+ if (!IsExpr)
3302+ addUnwrappedLine ();
32753303 } else {
32763304 addUnwrappedLine ();
32773305 ++Line->Level ;
0 commit comments