summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKrasimir Georgiev <krasimir@google.com>2018-03-16 14:01:25 +0000
committerKrasimir Georgiev <krasimir@google.com>2018-03-16 14:01:25 +0000
commitce00978b101db664451f127fbd219f2c777f99db (patch)
treeb7e8d3eb7f97dce2b9ba2544a27c19f8f29b5cbe
parent23578e7d3cb0bff9d4b29d3bab1c75a03b101cfd (diff)
downloadbcm5719-llvm-ce00978b101db664451f127fbd219f2c777f99db.tar.gz
bcm5719-llvm-ce00978b101db664451f127fbd219f2c777f99db.zip
[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 llvm-svn: 327708
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp8
-rw-r--r--clang/unittests/Format/FormatTestRawStrings.cpp28
2 files changed, 35 insertions, 1 deletions
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 6027f5fd612..9d19262397a 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/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/clang/unittests/Format/FormatTestRawStrings.cpp b/clang/unittests/Format/FormatTestRawStrings.cpp
index 85f0aab8710..f5a0c1e1816 100644
--- a/clang/unittests/Format/FormatTestRawStrings.cpp
+++ b/clang/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
OpenPOWER on IntegriCloud