diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 60244924079e..49e21520b702 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2272,6 +2272,13 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr)) return Line.MightBeFunctionDecl ? 50 : 500; + // In Objective-C type declarations, avoid breaking after the category's + // open paren (we'll prefer breaking after the protocol list's opening + // angle bracket, if present). + if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && + Left.Previous->isOneOf(tok::identifier, tok::greater)) + return 500; + if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) return 100; diff --git a/clang/unittests/Format/FormatTestObjC.cpp b/clang/unittests/Format/FormatTestObjC.cpp index 9c8f252143f0..b878d7886e10 100644 --- a/clang/unittests/Format/FormatTestObjC.cpp +++ b/clang/unittests/Format/FormatTestObjC.cpp @@ -334,6 +334,9 @@ TEST_F(FormatTestObjC, FormatObjCInterface) { " ccccccccccccc, ccccccccccccc,\n" " ccccccccccccc, ccccccccccccc> {\n" "}"); + verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n" + " ccccccccccccc> {\n" + "}"); Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never; verifyFormat("@interface ddddddddddddd () <\n" " ddddddddddddd,\n"