From: Daniel Jasper Date: Thu, 1 Aug 2013 23:13:03 +0000 (+0000) Subject: clang-format: Operator precendence in ObjC method exprs. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1eaa99779a254411600d77a715c761d9d84ec8bd;p=clang clang-format: Operator precendence in ObjC method exprs. Patch (mostly) by Adam Strzelecki. Thanks! Before: [self aaaaaa:bbbbbbbbbbbbb aaaaaaaaaa:bbbbbbbbbbbbbbbbb aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb aaaa:bbb]; After: [self aaaaaa:bbbbbbbbbbbbb aaaaaaaaaa:bbbbbbbbbbbbbbbbb aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb aaaa:bbb]; This fixes llvm.org/PR16150. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187631 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index af80650271..e46a700a1a 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -863,6 +863,11 @@ public: CurrentPrecedence = 1; else if (Current->Type == TT_BinaryOperator || Current->is(tok::comma)) CurrentPrecedence = 1 + (int)Current->getPrecedence(); + else if (Current->Type == TT_ObjCSelectorName) { + CurrentPrecedence = 1 + (int)prec::Assignment; + if (Precedence == CurrentPrecedence) + Start = Current; + } } // At the end of the line or when an operator with higher precedence is diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e383fc2d62..3a3164c0f4 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4787,17 +4787,17 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { verifyFormat( "void f() {\n" " if ((self = [super initWithContentRect:contentRect\n" - " styleMask:styleMask\n" + " styleMask:styleMask ?: otherMask\n" " backing:NSBackingStoreBuffered\n" " defer:YES]))"); verifyFormat( "[foo checkThatBreakingAfterColonWorksOk:\n" - " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); + " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); verifyFormat("[myObj short:arg1 // Force line break\n" - " longKeyword:arg2\n" - " evenLongerKeyword:arg3\n" + " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" + " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" " error:arg4];"); verifyFormat( "void f() {\n" @@ -4834,6 +4834,10 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { "scoped_nsobject message(\n" " // The frame will be fixed up when |-setMessageText:| is called.\n" " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); + verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" + " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" + " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" + " aaaa:bbb];"); } TEST_F(FormatTest, ObjCAt) {