Author: krasimir Date: Tue Jan 23 03:26:19 2018 New Revision: 323188 URL: http://llvm.org/viewvc/llvm-project?rev=323188&view=rev Log: [clang-format] Ignore UnbreakableTailLength sometimes during breaking
Summary: This patch fixes an issue where the UnbreakableTailLength would be counted towards the length of a token during breaking, even though we can break after the token. For example, this proto text with column limit 20 ``` # ColumnLimit: 20 V foo: { bar: { bazoo: "aaaaaaa" } } ``` was broken: ``` # ColumnLimit: 20 V foo: { bar: { bazoo: "aaaaaaa" } } ``` because the 2 closing `}` were counted towards the string literal's `UnbreakableTailLength`. Reviewers: djasper Reviewed By: djasper Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D42376 Modified: cfe/trunk/lib/Format/BreakableToken.cpp cfe/trunk/lib/Format/BreakableToken.h cfe/trunk/lib/Format/ContinuationIndenter.cpp cfe/trunk/unittests/Format/FormatTestTextProto.cpp Modified: cfe/trunk/lib/Format/BreakableToken.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/BreakableToken.cpp?rev=323188&r1=323187&r2=323188&view=diff ============================================================================== --- cfe/trunk/lib/Format/BreakableToken.cpp (original) +++ cfe/trunk/lib/Format/BreakableToken.cpp Tue Jan 23 03:26:19 2018 @@ -214,11 +214,11 @@ unsigned BreakableStringLiteral::getCont BreakableStringLiteral::BreakableStringLiteral( const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, - StringRef Postfix, bool InPPDirective, encoding::Encoding Encoding, - const FormatStyle &Style) + StringRef Postfix, unsigned UnbreakableTailLength, bool InPPDirective, + encoding::Encoding Encoding, const FormatStyle &Style) : BreakableToken(Tok, InPPDirective, Encoding, Style), StartColumn(StartColumn), Prefix(Prefix), Postfix(Postfix), - UnbreakableTailLength(Tok.UnbreakableTailLength) { + UnbreakableTailLength(UnbreakableTailLength) { assert(Tok.TokenText.startswith(Prefix) && Tok.TokenText.endswith(Postfix)); Line = Tok.TokenText.substr( Prefix.size(), Tok.TokenText.size() - Prefix.size() - Postfix.size()); Modified: cfe/trunk/lib/Format/BreakableToken.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/BreakableToken.h?rev=323188&r1=323187&r2=323188&view=diff ============================================================================== --- cfe/trunk/lib/Format/BreakableToken.h (original) +++ cfe/trunk/lib/Format/BreakableToken.h Tue Jan 23 03:26:19 2018 @@ -238,8 +238,8 @@ public: /// after formatting. BreakableStringLiteral(const FormatToken &Tok, unsigned StartColumn, StringRef Prefix, StringRef Postfix, - bool InPPDirective, encoding::Encoding Encoding, - const FormatStyle &Style); + unsigned UnbreakableTailLength, bool InPPDirective, + encoding::Encoding Encoding, const FormatStyle &Style); Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit, unsigned ReflowColumn, Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=323188&r1=323187&r2=323188&view=diff ============================================================================== --- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original) +++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Tue Jan 23 03:26:19 2018 @@ -1576,9 +1576,16 @@ std::unique_ptr<BreakableToken> Continua Text.startswith(Prefix = "u8\"") || Text.startswith(Prefix = "L\""))) || (Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) { + // We need this to address the case where there is an unbreakable tail + // only if certain other formatting decisions have been taken. The + // UnbreakableTailLength of Current is an overapproximation is that case + // and we need to be correct here. + unsigned UnbreakableTailLength = (State.NextToken && canBreak(State)) + ? 0 + : Current.UnbreakableTailLength; return llvm::make_unique<BreakableStringLiteral>( - Current, StartColumn, Prefix, Postfix, State.Line->InPPDirective, - Encoding, Style); + Current, StartColumn, Prefix, Postfix, UnbreakableTailLength, + State.Line->InPPDirective, Encoding, Style); } } else if (Current.is(TT_BlockComment)) { if (!Style.ReflowComments || Modified: cfe/trunk/unittests/Format/FormatTestTextProto.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestTextProto.cpp?rev=323188&r1=323187&r2=323188&view=diff ============================================================================== --- cfe/trunk/unittests/Format/FormatTestTextProto.cpp (original) +++ cfe/trunk/unittests/Format/FormatTestTextProto.cpp Tue Jan 23 03:26:19 2018 @@ -290,5 +290,17 @@ TEST_F(FormatTestTextProto, SupportsAngl " product_data <product {1}>\n" ">"); } + +TEST_F(FormatTestTextProto, DiscardsUnbreakableTailIfCanBreakAfter) { + // The two closing braces count towards the string UnbreakableTailLength, but + // since we have broken after the corresponding opening braces, we don't + // consider that length for string breaking. + verifyFormat( + "foo: {\n" + " bar: {\n" + " text: \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" + " }\n" + "}"); +} } // end namespace tooling } // end namespace clang _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits