Author: krasimir
Date: Fri Mar 16 07:01:25 2018
New Revision: 327708

URL: http://llvm.org/viewvc/llvm-project?rev=327708&view=rev
Log:
[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

Modified:
    cfe/trunk/lib/Format/ContinuationIndenter.cpp
    cfe/trunk/unittests/Format/FormatTestRawStrings.cpp

Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=327708&r1=327707&r2=327708&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Fri Mar 16 07:01:25 2018
@@ -1454,7 +1454,13 @@ unsigned ContinuationIndenter::reformatR
   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,

Modified: cfe/trunk/unittests/Format/FormatTestRawStrings.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestRawStrings.cpp?rev=327708&r1=327707&r2=327708&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestRawStrings.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestRawStrings.cpp Fri Mar 16 07:01:25 2018
@@ -794,6 +794,34 @@ TEST_F(FormatTestRawStrings, UpdatesToCa
             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


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to