This revision was automatically updated to reflect the committed changes. Closed by commit rGc7dcc4c72588: [clang-format] PR48569 clang-format fails to align case label with `switch`… (authored by MyDeveloperDay).
Changed prior to commit: https://reviews.llvm.org/D93806?vs=313699&id=313746#toc Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D93806/new/ https://reviews.llvm.org/D93806 Files: clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -13658,7 +13658,7 @@ " {\n" " switch (a)\n" " {\n" - " case 2:\n" + " case 2:\n" " {\n" " }\n" " break;\n" @@ -13670,18 +13670,18 @@ " {\n" " switch (a)\n" " {\n" - " case 0:\n" + " case 0:\n" " break;\n" - " case 1:\n" + " case 1:\n" " {\n" " foo();\n" " break;\n" " }\n" - " case 2:\n" + " case 2:\n" " {\n" " }\n" " break;\n" - " default:\n" + " default:\n" " break;\n" " }\n" " }\n", @@ -13691,12 +13691,12 @@ " {\n" " switch (a)\n" " {\n" - " case 0:\n" + " case 0:\n" " {\n" " foo(x);\n" " }\n" " break;\n" - " default:\n" + " default:\n" " {\n" " foo(1);\n" " }\n" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2244,18 +2244,26 @@ --Line->Level; if (LeftAlignLabel) Line->Level = 0; + + bool RemoveWhitesmithsCaseIndent = + (!Style.IndentCaseBlocks && + Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths); + + if (RemoveWhitesmithsCaseIndent) + --Line->Level; + if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Line->Level, - Style.BraceWrapping.AfterCaseLabel, - Style.BraceWrapping.IndentBraces); + + CompoundStatementIndenter Indenter( + this, Line->Level, Style.BraceWrapping.AfterCaseLabel, + Style.BraceWrapping.IndentBraces || RemoveWhitesmithsCaseIndent); parseBlock(/*MustBeDeclaration=*/false); if (FormatTok->Tok.is(tok::kw_break)) { if (Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Always) { addUnwrappedLine(); - if (!Style.IndentCaseBlocks && - Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { + if (RemoveWhitesmithsCaseIndent) { Line->Level++; } } @@ -2276,6 +2284,7 @@ void UnwrappedLineParser::parseCaseLabel() { assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); + // FIXME: fix handling of complex expressions here. do { nextToken();
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -13658,7 +13658,7 @@ " {\n" " switch (a)\n" " {\n" - " case 2:\n" + " case 2:\n" " {\n" " }\n" " break;\n" @@ -13670,18 +13670,18 @@ " {\n" " switch (a)\n" " {\n" - " case 0:\n" + " case 0:\n" " break;\n" - " case 1:\n" + " case 1:\n" " {\n" " foo();\n" " break;\n" " }\n" - " case 2:\n" + " case 2:\n" " {\n" " }\n" " break;\n" - " default:\n" + " default:\n" " break;\n" " }\n" " }\n", @@ -13691,12 +13691,12 @@ " {\n" " switch (a)\n" " {\n" - " case 0:\n" + " case 0:\n" " {\n" " foo(x);\n" " }\n" " break;\n" - " default:\n" + " default:\n" " {\n" " foo(1);\n" " }\n" Index: clang/lib/Format/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -2244,18 +2244,26 @@ --Line->Level; if (LeftAlignLabel) Line->Level = 0; + + bool RemoveWhitesmithsCaseIndent = + (!Style.IndentCaseBlocks && + Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths); + + if (RemoveWhitesmithsCaseIndent) + --Line->Level; + if (!Style.IndentCaseBlocks && CommentsBeforeNextToken.empty() && FormatTok->Tok.is(tok::l_brace)) { - CompoundStatementIndenter Indenter(this, Line->Level, - Style.BraceWrapping.AfterCaseLabel, - Style.BraceWrapping.IndentBraces); + + CompoundStatementIndenter Indenter( + this, Line->Level, Style.BraceWrapping.AfterCaseLabel, + Style.BraceWrapping.IndentBraces || RemoveWhitesmithsCaseIndent); parseBlock(/*MustBeDeclaration=*/false); if (FormatTok->Tok.is(tok::kw_break)) { if (Style.BraceWrapping.AfterControlStatement == FormatStyle::BWACS_Always) { addUnwrappedLine(); - if (!Style.IndentCaseBlocks && - Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { + if (RemoveWhitesmithsCaseIndent) { Line->Level++; } } @@ -2276,6 +2284,7 @@ void UnwrappedLineParser::parseCaseLabel() { assert(FormatTok->Tok.is(tok::kw_case) && "'case' expected"); + // FIXME: fix handling of complex expressions here. do { nextToken();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits