[PATCH] D12501: [clang-format] Obj-C dictionary literals: Fixed typecast getting put on a separate line from the key

Kent Sutherland via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 31 13:16:41 PDT 2015


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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12501.33618.patch
Type: text/x-patch
Size: 1454 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150831/e660897b/attachment-0001.bin>


More information about the cfe-commits mailing list