From dfbe035574ed9d48f0163ff0173f53a8e018f0f2 Mon Sep 17 00:00:00 2001 From: Krasimir Georgiev Date: Fri, 16 Mar 2018 14:01:25 +0000 Subject: [PATCH] [clang-format] Fix raw string prefix penalty Summary: We weren't penalizing cases where the raw string prefix goes over the column limit. Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D44563 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@327708 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 8 ++++++- unittests/Format/FormatTestRawStrings.cpp | 28 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 6027f5fd61..9d19262397 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -1454,7 +1454,13 @@ unsigned ContinuationIndenter::reformatRawStringLiteral( unsigned RawLastLineEndColumn = getLastLineEndColumn( *NewCode, FirstStartColumn, Style.TabWidth, Encoding); State.Column = RawLastLineEndColumn + NewSuffixSize; - return Fixes.second; + // Since we're updating the column to after the raw string literal here, we + // have to manually add the penalty for the prefix R"delim( over the column + // limit. + unsigned PrefixExcessCharacters = + StartColumn + NewPrefixSize > Style.ColumnLimit ? + StartColumn + NewPrefixSize - Style.ColumnLimit : 0; + return Fixes.second + PrefixExcessCharacters * Style.PenaltyExcessCharacter; } unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current, diff --git a/unittests/Format/FormatTestRawStrings.cpp b/unittests/Format/FormatTestRawStrings.cpp index 85f0aab871..f5a0c1e181 100644 --- a/unittests/Format/FormatTestRawStrings.cpp +++ b/unittests/Format/FormatTestRawStrings.cpp @@ -794,6 +794,34 @@ TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) { format(R"test(a = R"pb(key:")proto")pb";)test", Style)); } +TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) { + FormatStyle Style = getRawStringPbStyleWithColumns(60); + + // The '(' in R"pb is at column 60, no break. + expect_eq(R"test( +xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( + Category: aaaaaaaaaaaaaaaaaaaaaaaaaa +)pb")); +)test", + format(R"test( +xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb( + Category: aaaaaaaaaaaaaaaaaaaaaaaaaa +)pb")); +)test", Style)); + // The '(' in R"pb is at column 61, break. + expect_eq(R"test( +xxxxxxxaaaaax wwwwwww = + _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( + Category: aaaaaaaaaaaaaaaaaaaaaaaaaa + )pb")); +)test", + format(R"test( +xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb( + Category: aaaaaaaaaaaaaaaaaaaaaaaaaa +)pb")); +)test", Style)); +} + } // end namespace } // end namespace format } // end namespace clang -- 2.50.1