[PATCH] D79401: [clang-format] [PR45639] clang-format splits up the brackets of C++17 attribute [[ ]] when used with the first parameter

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 5 02:06:44 PDT 2020


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: krasimir, sammccall, mitchell-stellar.
MyDeveloperDay added projects: clang, clang-format.

https://bugs.llvm.org/show_bug.cgi?id=45639

clang-format incorrectly splits the `[[` in a long argument list

  void SomeLongClassName::ALongMethodNameInThatClass([[maybe_unused]] const shared_ptr<ALongTypeName>& argumentNameForThat
  LongType) {
  
  }

becomes

  void SomeLongClassName::ALongMethodNameInThatClass([
      [maybe_unused]] const shared_ptr<ALongTypeName> &argumentNameForThatLongType) {
  
  }

leaving one `[` on the previous line

For a function with just 1 very long argument, clang-format chooses to split between the `[[`,

This revision adjusts the penalty between `(` and `[` to ensure this is the more likely break point


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79401

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -7661,6 +7661,19 @@
                MultiLineFunctions);
 }
 
+TEST_F(FormatTest, AttributePenaltyBreaking) {
+  FormatStyle Style = getLLVMStyle();
+  verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n"
+               "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
+               Style);
+  verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n"
+               "    [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}",
+               Style);
+  verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const "
+               "shared_ptr<ALongTypeName> &C d) {\n}",
+               Style);
+}
+
 TEST_F(FormatTest, UnderstandsEllipsis) {
   verifyFormat("int printf(const char *fmt, ...);");
   verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2547,6 +2547,10 @@
                        TT_DesignatedInitializerLSquare, TT_AttributeSquare))
       return 500;
   }
+  // Favour breaking between ( and [ when attributes are used as the first
+  // parameter e.g. foo([[maybe_unused]] T &bar).
+  if (Left.is(tok::l_paren) && Right.is(TT_AttributeSquare))
+    return 1;
 
   if (Left.is(tok::coloncolon) ||
       (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
@@ -2635,7 +2639,6 @@
   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: D79401.262026.patch
Type: text/x-patch
Size: 1929 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200505/73bd1e3c/attachment-0001.bin>


More information about the cfe-commits mailing list