HazardyKnusperkeks created this revision. HazardyKnusperkeks added reviewers: MyDeveloperDay, klimek, djasper. HazardyKnusperkeks added a project: clang-format. HazardyKnusperkeks requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
If ``false``, spaces will be removed before case colon. true: false switch (x) { vs. switch (x) { case 1 : break; case 1: break; } } Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D93240 Files: clang/docs/ClangFormatStyleOptions.rst clang/docs/ReleaseNotes.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -12043,7 +12043,11 @@ "}"); verifyFormat("switch (x) {\n" "case 1:\n" + "case 2: {\n" + " break;\n" + "}\n" "default:\n" + " break;\n" "}"); FormatStyle CtorInitializerStyle = getLLVMStyleWithColumns(30); @@ -12059,7 +12063,11 @@ CtorInitializerStyle); verifyFormat("switch (x) {\n" "case 1:\n" + "case 2: {\n" + " break;\n" + "}\n" "default:\n" + " break;\n" "}", CtorInitializerStyle); CtorInitializerStyle.BreakConstructorInitializers = @@ -12099,7 +12107,11 @@ InheritanceStyle); verifyFormat("switch (x) {\n" "case 1:\n" + "case 2: {\n" + " break;\n" + "}\n" "default:\n" + " break;\n" "}", InheritanceStyle); InheritanceStyle.BreakInheritanceList = FormatStyle::BILS_AfterColon; @@ -12139,11 +12151,32 @@ ForLoopStyle); verifyFormat("switch (x) {\n" "case 1:\n" + "case 2: {\n" + " break;\n" + "}\n" "default:\n" + " break;\n" "}", ForLoopStyle); + FormatStyle CaseStyle = getLLVMStyle(); + CaseStyle.SpaceBeforeCaseColon = true; + verifyFormat("class Foo : public Bar {};", CaseStyle); + verifyFormat("Foo::Foo() : foo(1) {}", CaseStyle); + verifyFormat("for (auto a : b) {\n}", CaseStyle); + verifyFormat("int x = a ? b : c;", CaseStyle); + verifyFormat("switch (x) {\n" + "case 1 :\n" + "case 2 : {\n" + " break;\n" + "}\n" + "default :\n" + " break;\n" + "}", + CaseStyle); + FormatStyle NoSpaceStyle = getLLVMStyle(); + NoSpaceStyle.SpaceBeforeCaseColon = false; NoSpaceStyle.SpaceBeforeCtorInitializerColon = false; NoSpaceStyle.SpaceBeforeInheritanceColon = false; NoSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; @@ -12158,9 +12191,37 @@ NoSpaceStyle); verifyFormat("switch (x) {\n" "case 1:\n" + "case 2: {\n" + " break;\n" + "}\n" "default:\n" + " break;\n" "}", NoSpaceStyle); + + FormatStyle InvertedSpaceStyle = getLLVMStyle(); + InvertedSpaceStyle.SpaceBeforeCaseColon = true; + InvertedSpaceStyle.SpaceBeforeCtorInitializerColon = false; + InvertedSpaceStyle.SpaceBeforeInheritanceColon = false; + InvertedSpaceStyle.SpaceBeforeRangeBasedForLoopColon = false; + verifyFormat("class Foo: public Bar {};", InvertedSpaceStyle); + verifyFormat("Foo::Foo(): foo(1) {}", InvertedSpaceStyle); + verifyFormat("for (auto a: b) {\n}", InvertedSpaceStyle); + verifyFormat("int x = a ? b : c;", InvertedSpaceStyle); + verifyFormat("{\n" + "label3:\n" + " int x = 0;\n" + "}", + InvertedSpaceStyle); + verifyFormat("switch (x) {\n" + "case 1 :\n" + "case 2 : {\n" + " break;\n" + "}\n" + "default :\n" + " break;\n" + "}", + InvertedSpaceStyle); } TEST_F(FormatTest, ConfigurableSpaceAroundPointerQualifiers) { @@ -14138,6 +14199,7 @@ CHECK_PARSE_BOOL(SpaceAfterTemplateKeyword); CHECK_PARSE_BOOL(SpaceAfterLogicalNot); CHECK_PARSE_BOOL(SpaceBeforeAssignmentOperators); + CHECK_PARSE_BOOL(SpaceBeforeCaseColon); CHECK_PARSE_BOOL(SpaceBeforeCpp11BracedList); CHECK_PARSE_BOOL(SpaceBeforeCtorInitializerColon); CHECK_PARSE_BOOL(SpaceBeforeInheritanceColon); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -896,7 +896,8 @@ } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { Tok->setType(TT_BitFieldColon); } else if (Contexts.size() == 1 && - !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) { + !Line.First->isOneOf(tok::kw_enum, tok::kw_case, + tok::kw_default)) { FormatToken *Prev = Tok->getPreviousNonComment(); if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept)) Tok->setType(TT_CtorInitializerColon); @@ -3339,8 +3340,9 @@ return Style.BitFieldColonSpacing == FormatStyle::BFCS_Both || Style.BitFieldColonSpacing == FormatStyle::BFCS_After; if (Right.is(tok::colon)) { - if (Line.First->isOneOf(tok::kw_case, tok::kw_default) || - !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) + if (Line.First->isOneOf(tok::kw_default, tok::kw_case)) + return Style.SpaceBeforeCaseColon; + if (!Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi)) return false; if (Right.is(TT_ObjCMethodExpr)) return false; Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -608,6 +608,7 @@ Style.SpaceAfterTemplateKeyword); IO.mapOptional("SpaceBeforeAssignmentOperators", Style.SpaceBeforeAssignmentOperators); + IO.mapOptional("SpaceBeforeCaseColon", Style.SpaceBeforeCaseColon); IO.mapOptional("SpaceBeforeCpp11BracedList", Style.SpaceBeforeCpp11BracedList); IO.mapOptional("SpaceBeforeCtorInitializerColon", @@ -974,6 +975,7 @@ LLVMStyle.SpaceAfterLogicalNot = false; LLVMStyle.SpaceAfterTemplateKeyword = true; LLVMStyle.SpaceAroundPointerQualifiers = FormatStyle::SAPQ_Default; + LLVMStyle.SpaceBeforeCaseColon = false; LLVMStyle.SpaceBeforeCtorInitializerColon = true; LLVMStyle.SpaceBeforeInheritanceColon = true; LLVMStyle.SpaceBeforeParens = FormatStyle::SBPO_ControlStatements; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -2438,6 +2438,15 @@ /// \endcode bool SpaceBeforeAssignmentOperators; + /// If ``false``, spaces will be removed before case colon. + /// \code + /// true: false + /// switch (x) { vs. switch (x) { + /// case 1 : break; case 1: break; + /// } } + /// \endcode + bool SpaceBeforeCaseColon; + /// If ``true``, a space will be inserted before a C++11 braced list /// used to initialize an object (after the preceding identifier or type). /// \code @@ -2841,6 +2850,7 @@ SpaceAfterLogicalNot == R.SpaceAfterLogicalNot && SpaceAfterTemplateKeyword == R.SpaceAfterTemplateKeyword && SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators && + SpaceBeforeCaseColon == R.SpaceBeforeCaseColon && SpaceBeforeCpp11BracedList == R.SpaceBeforeCpp11BracedList && SpaceBeforeCtorInitializerColon == R.SpaceBeforeCtorInitializerColon && Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -281,6 +281,9 @@ - Option ``IndentPragmas`` has been added to allow #pragma to indented with the current scope level. This is especially useful when using #pragma to mark OpenMP sections of code. +- Option ``SpaceBeforeCaseColon`` has been added to add a space before the + colon in a case or default statement. + libclang -------- Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -2839,6 +2839,16 @@ int a = 5; vs. int a= 5; a += 42; a+= 42; +**SpaceBeforeCaseColon** (``bool``) + If ``false``, spaces will be removed before case colon. + + .. code-block:: c++ + + true: false + switch (x) { vs. switch (x) { + case 1 : break; case 1: break; + } } + **SpaceBeforeCpp11BracedList** (``bool``) If ``true``, a space will be inserted before a C++11 braced list used to initialize an object (after the preceding identifier or type).
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits