[PATCH] D45526: [clang-format] Prefer breaking after ObjC category close paren

Ben Hamilton via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 11 10:05:37 PDT 2018


benhamilton created this revision.
benhamilton added reviewers: djasper, jolesiak.
Herald added subscribers: cfe-commits, klimek.

Previously, `clang-format` would break Objective-C
category extensions after the opening parenthesis to avoid
breaking the protocol list:

  % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \
    clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \
    ColumnLimit: 40}"
  @interface ccccccccccccc (
      ccccccccccc) <ccccccccccccc> {
  }

This looks fairly odd, as we could have kept the category extension
on the previous line.

Category extensions are a single item, so they are generally very
short compared to protocol lists. We should prefer breaking after the
opening `<` of the protocol list over breaking after the opening `(`
of the category extension.

With this diff, we now prefer breaking after the category extension's
closing paren:

  % echo "@interface ccccccccccccc (ccccccccccc) <ccccccccccccc> { }" | \
    ./bin/clang-format -assume-filename=foo.h -style="{BasedOnStyle: llvm, \
    ColumnLimit: 40}"
  @interface ccccccccccccc (ccccccccccc) <
      ccccccccccccc> {
  }

Test Plan: New test added. Confirmed test failed before diff and

  passed after diff by running:
  % make -j16 FormatTests && ./tools/clang/unittests/Format/FormatTests


Repository:
  rC Clang

https://reviews.llvm.org/D45526

Files:
  lib/Format/TokenAnnotator.cpp
  unittests/Format/FormatTestObjC.cpp


Index: unittests/Format/FormatTestObjC.cpp
===================================================================
--- unittests/Format/FormatTestObjC.cpp
+++ unittests/Format/FormatTestObjC.cpp
@@ -334,6 +334,9 @@
                "    ccccccccccccc, ccccccccccccc,\n"
                "    ccccccccccccc, ccccccccccccc> {\n"
                "}");
+  verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n"
+               "    ccccccccccccc> {\n"
+               "}");
   Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
   verifyFormat("@interface ddddddddddddd () <\n"
                "    ddddddddddddd,\n"
Index: lib/Format/TokenAnnotator.cpp
===================================================================
--- lib/Format/TokenAnnotator.cpp
+++ lib/Format/TokenAnnotator.cpp
@@ -2272,6 +2272,12 @@
   if (Left.is(tok::colon) && Left.is(TT_ObjCMethodExpr))
     return Line.MightBeFunctionDecl ? 50 : 500;
 
+  // In Objective-C type declarations, prefer breaking after the category's
+  // close paren instead of after the open paren.
+  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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45526.142033.patch
Type: text/x-patch
Size: 1339 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180411/e9184a99/attachment.bin>


More information about the cfe-commits mailing list