From abbd54ce009579da4aa532b83632cc584f0b7b19 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Fri, 16 Mar 2018 15:23:22 +0000 Subject: [PATCH] [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 llvm-svn: 327716 --- clang/lib/Format/TokenAnnotator.cpp | 3 +++ clang/unittests/Format/FormatTestTextProto.cpp | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 3de454c..edb5a6f 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2968,6 +2968,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, 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. diff --git a/clang/unittests/Format/FormatTestTextProto.cpp b/clang/unittests/Format/FormatTestTextProto.cpp index 1f2f2b6..f4e09cf 100644 --- a/clang/unittests/Format/FormatTestTextProto.cpp +++ b/clang/unittests/Format/FormatTestTextProto.cpp @@ -392,10 +392,12 @@ TEST_F(FormatTestTextProto, FormatsExtensions) { " .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, BreaksConsecutiveStringLiterals) { " \"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 -- 2.7.4