r327716 - [clang-format] Disallow breaks before ']' in text proto extensions

Krasimir Georgiev via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 16 08:23:23 PDT 2018


Author: krasimir
Date: Fri Mar 16 08:23:22 2018
New Revision: 327716

URL: http://llvm.org/viewvc/llvm-project?rev=327716&view=rev
Log:
[clang-format] Disallow breaks before ']' in text proto extensions

Summary: This disallows patterns like `[ext.name\n]` in text protos.

Subscribers: klimek, cfe-commits

Differential Revision: https://reviews.llvm.org/D44569

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTestTextProto.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=327716&r1=327715&r2=327716&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Mar 16 08:23:22 2018
@@ -2968,6 +2968,9 @@ bool TokenAnnotator::canBreakBefore(cons
       return false;
     return true;
   }
+  if (Right.is(tok::r_square) && Right.MatchingParen &&
+      Right.MatchingParen->is(TT_ProtoExtensionLSquare))
+    return false;
   if (Right.is(TT_SelectorName) || (Right.is(tok::identifier) && Right.Next &&
                                     Right.Next->is(TT_ObjCMethodExpr)))
     return Left.isNot(tok::period); // FIXME: Properly parse ObjC calls.

Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=327716&r1=327715&r2=327716&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Fri Mar 16 08:23:22 2018
@@ -392,10 +392,12 @@ TEST_F(FormatTestTextProto, FormatsExten
                "     .long/longg.longlong] { key: value }");
   verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/\n"
                " bbbbbbbbbbbbbb] { key: value }");
-  verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
-               "] { key: value }");
-  verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
-               "] {\n"
+  // These go over the column limit intentionally, since the alternative
+  // [aa..a\n] is worse.
+  verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
+               "  key: value\n"
+               "}");
+  verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa] {\n"
                "  [type.type] {\n"
                "    keyyyyyyyyyyyyyy: valuuuuuuuuuuuuuuuuuuuuuuuuue\n"
                "  }\n"
@@ -475,5 +477,20 @@ TEST_F(FormatTestTextProto, BreaksConsec
                "     \"str2\"\n");
 }
 
+TEST_F(FormatTestTextProto, PutsMultipleEntriesInExtensionsOnNewlines) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_TextProto);
+  verifyFormat("pppppppppp: {\n"
+               "  ssssss: \"http://example.com/blahblahblah\"\n"
+               "  ppppppp: \"sssss/MMMMMMMMMMMM\"\n"
+               "  [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] { begin: 24 end: 252 }\n"
+               "  [ns.sssss.eeeeeeeee.eeeeeeeeeeeeeee] {\n"
+               "    begin: 24\n"
+               "    end: 252\n"
+               "    key: value\n"
+               "    key: value\n"
+               "  }\n"
+               "}", Style);
+}
+
 } // end namespace tooling
 } // end namespace clang




More information about the cfe-commits mailing list