@@ -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) {
@@ -2061,6 +2070,11 @@ void UnwrappedLineParser::parseStructuralElement(
20612070 case tok::kw_new:
20622071 parseNew ();
20632072 break ;
2073+ case tok::kw_switch:
2074+ if (Style.Language == FormatStyle::LK_Java)
2075+ parseSwitch (/* IsExpr=*/ true );
2076+ nextToken ();
2077+ break ;
20642078 case tok::kw_case:
20652079 // Proto: there are no switch/case statements.
20662080 if (Style.Language == FormatStyle::LK_Proto) {
@@ -2583,6 +2597,9 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25832597 else
25842598 nextToken ();
25852599 break ;
2600+ case tok::kw_switch:
2601+ parseSwitch (/* IsExpr=*/ true );
2602+ break ;
25862603 case tok::kw_requires: {
25872604 auto RequiresToken = FormatTok;
25882605 nextToken ();
@@ -3240,6 +3257,7 @@ void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) {
32403257
32413258void UnwrappedLineParser::parseCaseLabel () {
32423259 assert (FormatTok->is (tok::kw_case) && " 'case' expected" );
3260+ auto *Case = FormatTok;
32433261
32443262 // FIXME: fix handling of complex expressions here.
32453263 do {
@@ -3248,11 +3266,16 @@ void UnwrappedLineParser::parseCaseLabel() {
32483266 FormatTok->setFinalizedType (TT_CaseLabelColon);
32493267 break ;
32503268 }
3269+ if (Style.Language == FormatStyle::LK_Java && FormatTok->is (tok::arrow)) {
3270+ FormatTok->setFinalizedType (TT_CaseLabelArrow);
3271+ Case->setFinalizedType (TT_SwitchExpressionLabel);
3272+ break ;
3273+ }
32513274 } while (!eof ());
32523275 parseLabel ();
32533276}
32543277
3255- void UnwrappedLineParser::parseSwitch () {
3278+ void UnwrappedLineParser::parseSwitch (bool IsExpr ) {
32563279 assert (FormatTok->is (tok::kw_switch) && " 'switch' expected" );
32573280 nextToken ();
32583281 if (FormatTok->is (tok::l_paren))
@@ -3262,10 +3285,15 @@ void UnwrappedLineParser::parseSwitch() {
32623285
32633286 if (FormatTok->is (tok::l_brace)) {
32643287 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 ();
32673294 setPreviousRBraceType (TT_ControlStatementRBrace);
3268- addUnwrappedLine ();
3295+ if (!IsExpr)
3296+ addUnwrappedLine ();
32693297 } else {
32703298 addUnwrappedLine ();
32713299 ++Line->Level ;
0 commit comments