Don't break single-line raw string literals.

Reviewers: djasper

Reviewed By: djasper

Subscribers: klimek, cfe-commits

Differential Revision: http://reviews.llvm.org/D6636

llvm-svn: 224223
This commit is contained in:
Alexander Kornienko 2014-12-14 20:47:11 +00:00
parent c4fbfee4ff
commit 732b6bd4d1
2 changed files with 9 additions and 41 deletions

View File

@ -902,24 +902,6 @@ unsigned ContinuationIndenter::addMultilineToken(const FormatToken &Current,
return 0;
}
static bool getRawStringLiteralPrefixPostfix(StringRef Text, StringRef &Prefix,
StringRef &Postfix) {
if (Text.startswith(Prefix = "R\"") || Text.startswith(Prefix = "uR\"") ||
Text.startswith(Prefix = "UR\"") || Text.startswith(Prefix = "u8R\"") ||
Text.startswith(Prefix = "LR\"")) {
size_t ParenPos = Text.find('(');
if (ParenPos != StringRef::npos) {
StringRef Delimiter =
Text.substr(Prefix.size(), ParenPos - Prefix.size());
Prefix = Text.substr(0, ParenPos + 1);
Postfix = Text.substr(Text.size() - 2 - Delimiter.size());
return Postfix.front() == ')' && Postfix.back() == '"' &&
Postfix.substr(1).startswith(Delimiter);
}
}
return false;
}
unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
LineState &State,
bool DryRun) {
@ -977,8 +959,7 @@ unsigned ContinuationIndenter::breakProtrudingToken(const FormatToken &Current,
Text.startswith(Prefix = "u\"") || Text.startswith(Prefix = "U\"") ||
Text.startswith(Prefix = "u8\"") ||
Text.startswith(Prefix = "L\""))) ||
(Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")")) ||
getRawStringLiteralPrefixPostfix(Text, Prefix, Postfix)) {
(Text.startswith(Prefix = "_T(\"") && Text.endswith(Postfix = "\")"))) {
Token.reset(new BreakableStringLiteral(
Current, State.Line->Level, StartColumn, Prefix, Postfix,
State.Line->InPPDirective, Encoding, Style));

View File

@ -7385,22 +7385,14 @@ TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {
format("@\"NSString literal\";", getGoogleStyleWithColumns(19)));
}
TEST_F(FormatTest, BreaksRawStringLiterals) {
EXPECT_EQ("R\"x(raw )x\"\n"
"R\"x(literal)x\";",
format("R\"x(raw literal)x\";", getGoogleStyleWithColumns(15)));
EXPECT_EQ("uR\"x(raw )x\"\n"
"uR\"x(literal)x\";",
format("uR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
EXPECT_EQ("u8R\"x(raw )x\"\n"
"u8R\"x(literal)x\";",
format("u8R\"x(raw literal)x\";", getGoogleStyleWithColumns(17)));
EXPECT_EQ("LR\"x(raw )x\"\n"
"LR\"x(literal)x\";",
format("LR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
EXPECT_EQ("UR\"x(raw )x\"\n"
"UR\"x(literal)x\";",
format("UR\"x(raw literal)x\";", getGoogleStyleWithColumns(16)));
TEST_F(FormatTest, DoesNotBreakRawStringLiterals) {
FormatStyle Style = getGoogleStyleWithColumns(15);
EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style));
EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style));
EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style));
EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style));
EXPECT_EQ("u8R\"x(raw literal)x\";",
format("u8R\"x(raw literal)x\";", Style));
}
TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
@ -7589,11 +7581,6 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
"\"00000000\"\n"
"\"1\"",
format("\"test\\000000000001\"", getLLVMStyleWithColumns(10)));
// FIXME: We probably don't need to care about escape sequences in raw
// literals.
EXPECT_EQ("R\"(\\x)\"\n"
"R\"(\\x00)\"\n",
format("R\"(\\x\\x00)\"\n", getGoogleStyleWithColumns(7)));
}
TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) {