ksuther created this revision. ksuther added a reviewer: djasper. ksuther added a subscriber: cfe-commits. Herald added a subscriber: klimek.
Fixes this bug: https://llvm.org/bugs/show_bug.cgi?id=22647 The following dictionary was getting formatted oddly: NSDictionary *query = @{ (__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword, (__bridge id)kSecReturnData: (__bridge id)kCFBooleanTrue, }; It was turning into this: NSDictionary *passwordQuery = @{ (__bridge id) kSecClass : (__bridge id)kSecClassGenericPassword, (__bridge id) kSecReturnData : (__bridge id)kCFBooleanTrue, (__bridge id) kSecReturnAttributes : (__bridge id)kCFBooleanTrue, }; As far as I can tell, changes to format Proto lines correctly was turning the key (e.g. kSecClass) into a TT_SelectorName, which was then force the cast to get separated from the key. I added an extra check to see if the current context is in a dictionary literal, and if so kept the type as TT_Unknown. http://reviews.llvm.org/D12501 Files: lib/Format/TokenAnnotator.cpp unittests/Format/FormatTest.cpp Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -7502,6 +7502,12 @@ " bbbbbbbbbbbbbbbbbb : bbbbb,\n" " cccccccccccccccc : ccccccccccccccc\n" " }];"); + + // Ensure that casts before the key are kept on the same line as the key + verifyFormat("NSDictionary *query = @{\n" + " (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,\n" + " (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,\n" + "};"); } TEST_F(FormatTest, ObjCArrayLiterals) { Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -372,7 +372,9 @@ updateParameterCount(Left, CurrentToken); if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) { FormatToken *Previous = CurrentToken->getPreviousNonComment(); - if ((CurrentToken->is(tok::colon) || + if (((CurrentToken->is(tok::colon) && + (!Contexts.back().ColonIsDictLiteral || + Style.Language != FormatStyle::LK_Cpp)) || Style.Language == FormatStyle::LK_Proto) && Previous->is(tok::identifier)) Previous->Type = TT_SelectorName;
Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -7502,6 +7502,12 @@ " bbbbbbbbbbbbbbbbbb : bbbbb,\n" " cccccccccccccccc : ccccccccccccccc\n" " }];"); + + // Ensure that casts before the key are kept on the same line as the key + verifyFormat("NSDictionary *query = @{\n" + " (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,\n" + " (__bridge id)kSecReturnData : (__bridge id)kCFBooleanTrue,\n" + "};"); } TEST_F(FormatTest, ObjCArrayLiterals) { Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -372,7 +372,9 @@ updateParameterCount(Left, CurrentToken); if (CurrentToken->isOneOf(tok::colon, tok::l_brace)) { FormatToken *Previous = CurrentToken->getPreviousNonComment(); - if ((CurrentToken->is(tok::colon) || + if (((CurrentToken->is(tok::colon) && + (!Contexts.back().ColonIsDictLiteral || + Style.Language != FormatStyle::LK_Cpp)) || Style.Language == FormatStyle::LK_Proto) && Previous->is(tok::identifier)) Previous->Type = TT_SelectorName;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits