This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rC327708: [clang-format] Fix raw string prefix penalty 
(authored by krasimir, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D44563?vs=138696&id=138697#toc

Repository:
  rC Clang

https://reviews.llvm.org/D44563

Files:
  lib/Format/ContinuationIndenter.cpp
  unittests/Format/FormatTestRawStrings.cpp


Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -1454,7 +1454,13 @@
   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,
Index: unittests/Format/FormatTestRawStrings.cpp
===================================================================
--- unittests/Format/FormatTestRawStrings.cpp
+++ unittests/Format/FormatTestRawStrings.cpp
@@ -794,6 +794,34 @@
             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


Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -1454,7 +1454,13 @@
   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,
Index: unittests/Format/FormatTestRawStrings.cpp
===================================================================
--- unittests/Format/FormatTestRawStrings.cpp
+++ unittests/Format/FormatTestRawStrings.cpp
@@ -794,6 +794,34 @@
             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
  • [PATCH] D44563: [clang-f... Krasimir Georgiev via Phabricator via cfe-commits
    • [PATCH] D44563: [cl... Krasimir Georgiev via Phabricator via cfe-commits

Reply via email to