Skip to content

Commit

Permalink
[clang-format] Fix raw string prefix penalty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
krasimirgg committed Mar 16, 2018
1 parent 23578e7 commit ce00978
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
8 changes: 7 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions clang/unittests/Format/FormatTestRawStrings.cpp
Expand Up @@ -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

0 comments on commit ce00978

Please sign in to comment.