Author: mydeveloperday Date: 2020-06-27T11:35:22+01:00 New Revision: eb50838ba08d4149182828b96956a57ec6f5f658
URL: https://github.com/llvm/llvm-project/commit/eb50838ba08d4149182828b96956a57ec6f5f658 DIFF: https://github.com/llvm/llvm-project/commit/eb50838ba08d4149182828b96956a57ec6f5f658.diff LOG: [clang-format] [PR462254] fix indentation of default and break correctly in whitesmiths style Summary: https://bugs.llvm.org/show_bug.cgi?id=46254 Reviewed By: curdeius, jbcoe Differential Revision: https://reviews.llvm.org/D8201 Added: Modified: clang/lib/Format/UnwrappedLineFormatter.cpp clang/lib/Format/UnwrappedLineParser.cpp clang/unittests/Format/FormatTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 3392a055c0c6..22f27a668dcc 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -1230,7 +1230,8 @@ void UnwrappedLineFormatter::formatFirstToken( // If in Whitemsmiths mode, indent start and end of blocks if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { - if (RootToken.isOneOf(tok::l_brace, tok::r_brace, tok::kw_case)) + if (RootToken.isOneOf(tok::l_brace, tok::r_brace, tok::kw_case, + tok::kw_default)) Indent += Style.IndentWidth; } diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 577b60bc51e2..a37386425aae 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2220,8 +2220,13 @@ void UnwrappedLineParser::parseLabel(bool LeftAlignLabel) { parseBlock(/*MustBeDeclaration=*/false); if (FormatTok->Tok.is(tok::kw_break)) { if (Style.BraceWrapping.AfterControlStatement == - FormatStyle::BWACS_Always) + FormatStyle::BWACS_Always) { addUnwrappedLine(); + if (!Style.IndentCaseBlocks && + Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) { + Line->Level++; + } + } parseStructuralElement(); } addUnwrappedLine(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index beaa68a24617..a47c66cbf92a 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -12979,9 +12979,7 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) { " }\n", WhitesmithsBraceStyle); - // FIXME: the block and the break under case 2 in this test don't get indented - // correctly - /* + WhitesmithsBraceStyle.IndentCaseBlocks = true; verifyFormat("void switchTest1(int a)\n" " {\n" " switch (a)\n" @@ -12989,35 +12987,101 @@ TEST_F(FormatTest, WhitesmithsBraceBreaking) { " case 2:\n" " {\n" " }\n" - " break;\n" + " break;\n" " }\n" " }\n", WhitesmithsBraceStyle); - */ - // FIXME: the block and the break under case 2 in this test don't get indented - // correctly - /* verifyFormat("void switchTest2(int a)\n" " {\n" " switch (a)\n" " {\n" - " case 0:\n" + " case 0:\n" " break;\n" - " case 1:\n" + " case 1:\n" + " {\n" + " break;\n" + " }\n" + " case 2:\n" + " {\n" + " }\n" + " break;\n" + " default:\n" + " break;\n" + " }\n" + " }\n", + WhitesmithsBraceStyle); + + verifyFormat("void switchTest3(int a)\n" + " {\n" + " switch (a)\n" " {\n" + " case 0:\n" + " {\n" + " foo(x);\n" + " }\n" + " break;\n" + " default:\n" + " {\n" + " foo(1);\n" + " }\n" " break;\n" " }\n" - " case 2:\n" + " }\n", + WhitesmithsBraceStyle); + + WhitesmithsBraceStyle.IndentCaseBlocks = false; + + verifyFormat("void switchTest4(int a)\n" + " {\n" + " switch (a)\n" + " {\n" + " case 2:\n" " {\n" " }\n" " break;\n" - " default:\n" + " }\n" + " }\n", + WhitesmithsBraceStyle); + + verifyFormat("void switchTest5(int a)\n" + " {\n" + " switch (a)\n" + " {\n" + " case 0:\n" + " break;\n" + " case 1:\n" + " {\n" + " foo();\n" + " break;\n" + " }\n" + " case 2:\n" + " {\n" + " }\n" + " break;\n" + " default:\n" + " break;\n" + " }\n" + " }\n", + WhitesmithsBraceStyle); + + verifyFormat("void switchTest6(int a)\n" + " {\n" + " switch (a)\n" + " {\n" + " case 0:\n" + " {\n" + " foo(x);\n" + " }\n" + " break;\n" + " default:\n" + " {\n" + " foo(1);\n" + " }\n" " break;\n" " }\n" " }\n", WhitesmithsBraceStyle); - */ verifyFormat("enum X\n" " {\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits