r342116 - [clang-format] Wrapped block after case label should not be merged into one line
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 13 00:27:16 PDT 2018
Author: owenpan
Date: Thu Sep 13 00:27:15 2018
New Revision: 342116
URL: http://llvm.org/viewvc/llvm-project?rev=342116&view=rev
Log:
[clang-format] Wrapped block after case label should not be merged into one line
PR38854
Differential Revision: http://reviews.llvm.org/D51719
Modified:
cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp?rev=342116&r1=342115&r2=342116&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineFormatter.cpp Thu Sep 13 00:27:15 2018
@@ -323,6 +323,10 @@ private:
kwId == clang::tok::objc_synchronized)
return 0;
}
+ // Don't merge block with left brace wrapped after case labels
+ if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
+ I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
+ return 0;
// Try to merge a block with left brace wrapped that wasn't yet covered
if (TheLine->Last->is(tok::l_brace)) {
return !Style.BraceWrapping.AfterFunction ||
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=342116&r1=342115&r2=342116&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Thu Sep 13 00:27:15 2018
@@ -1064,6 +1064,32 @@ TEST_F(FormatTest, FormatsSwitchStatemen
" return;\n"
"}",
getLLVMStyleWithColumns(34));
+
+ FormatStyle Style = getLLVMStyle();
+ Style.IndentCaseLabels = true;
+ Style.AllowShortBlocksOnASingleLine = false;
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.AfterControlStatement = true;
+ EXPECT_EQ("switch (n)\n"
+ "{\n"
+ " case 0:\n"
+ " {\n"
+ " return false;\n"
+ " }\n"
+ " default:\n"
+ " {\n"
+ " return true;\n"
+ " }\n"
+ "}",
+ format("switch (n) {\n"
+ " case 0: {\n"
+ " return false;\n"
+ " }\n"
+ " default: {\n"
+ " return true;\n"
+ " }\n"
+ "}",
+ Style));
}
TEST_F(FormatTest, CaseRanges) {
More information about the cfe-commits
mailing list