From d39312ec8420cc9b862416f4f620710afb86f693 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 28 May 2014 10:09:11 +0000 Subject: [PATCH] clang-format: Don't break before a case's colon. Before (with just the right line length: switch (a) { case some_namespace::some_constant : return; } After: switch (a) { case some_namespace:: some_constant: return; } llvm-svn: 209725 --- clang/lib/Format/TokenAnnotator.cpp | 9 +++++---- clang/unittests/Format/FormatTest.cpp | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 3fea52b..902cbb8 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -392,7 +392,8 @@ private: Tok->Type = TT_RangeBasedForLoopColon; } else if (CurrentToken && CurrentToken->is(tok::numeric_constant)) { Tok->Type = TT_BitFieldColon; - } else if (Contexts.size() == 1 && Line.First->isNot(tok::kw_enum)) { + } else if (Contexts.size() == 1 && + !Line.First->isOneOf(tok::kw_enum, tok::kw_case)) { Tok->Type = TT_InheritanceColon; } else if (Contexts.back().ContextKind == tok::l_paren) { Tok->Type = TT_InlineASMColon; @@ -1655,11 +1656,11 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return Style.BreakBeforeTernaryOperators; if (Left.Type == TT_ConditionalExpr || Left.is(tok::question)) return !Style.BreakBeforeTernaryOperators; - if (Right.is(tok::colon) && - (Right.Type == TT_DictLiteral || Right.Type == TT_ObjCMethodExpr)) - return false; if (Right.Type == TT_InheritanceColon) return true; + if (Right.is(tok::colon) && (Right.Type != TT_CtorInitializerColon && + Right.Type != TT_InlineASMColon)) + return false; if (Left.is(tok::colon) && (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr)) return true; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8ad59cd..712af88 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -683,6 +683,13 @@ TEST_F(FormatTest, FormatsSwitchStatement) { "case (b):\n" " return;\n" "}"); + + verifyFormat("switch (a) {\n" + "case some_namespace::\n" + " some_constant:\n" + " return;\n" + "}", + getLLVMStyleWithColumns(34)); } TEST_F(FormatTest, CaseRanges) { -- 2.7.4