clang-format: Keep string-literal-label + value pairs on a line.

We have previously done that for <<-operators. This patch also adds
this logic for "," and "+".

Before:
  string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " +
             aaaaaaaaaaaaaaaa + "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
  string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ",
                    aaaaaaaaaaaaaaaa, "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);

After:
  string v = "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
	     "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa +
	     "aaaaaaaaaaaaaaaa: " + aaaaaaaaaaaaaaaa;
  string v = StrCat("aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
		    "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa,
		    "aaaaaaaaaaaaaaaa: ", aaaaaaaaaaaaaaaa);

llvm-svn: 289531
This commit is contained in:
Daniel Jasper 2016-12-13 11:16:42 +00:00
parent e8af792439
commit 7209bb9d4e
3 changed files with 77 additions and 56 deletions

View File

@ -396,6 +396,21 @@ struct FormatToken {
} }
} }
/// \brief Returns \c true if this is a string literal that's like a label,
/// e.g. ends with "=" or ":".
bool isLabelString() const {
if (!is(tok::string_literal))
return false;
StringRef Content = TokenText;
if (Content.startswith("\"") || Content.startswith("'"))
Content = Content.drop_front(1);
if (Content.endswith("\"") || Content.endswith("'"))
Content = Content.drop_back(1);
Content = Content.trim();
return Content.size() > 1 &&
(Content.back() == ':' || Content.back() == '=');
}
/// \brief Returns actual token start location without leading escaped /// \brief Returns actual token start location without leading escaped
/// newlines and whitespace. /// newlines and whitespace.
/// ///

View File

@ -1882,8 +1882,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
return 100; return 100;
} }
if (Left.is(tok::comma) || (Right.is(tok::identifier) && Right.Next && if (Right.is(tok::identifier) && Right.Next && Right.Next->is(TT_DictLiteral))
Right.Next->is(TT_DictLiteral)))
return 1; return 1;
if (Right.is(tok::l_square)) { if (Right.is(tok::l_square)) {
if (Style.Language == FormatStyle::LK_Proto) if (Style.Language == FormatStyle::LK_Proto)
@ -1999,21 +1998,17 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line,
if (Left.is(TT_JavaAnnotation)) if (Left.is(TT_JavaAnnotation))
return 50; return 50;
if (Right.is(tok::lessless)) { if (Left.isOneOf(tok::plus, tok::comma) && Left.Previous &&
if (Left.is(tok::string_literal) && Left.Previous->isLabelString() &&
(Right.NextOperator || Right.OperatorIndex != 1)) { (Left.NextOperator || Left.OperatorIndex != 1))
StringRef Content = Left.TokenText; return 100;
if (Content.startswith("\"")) if (Left.is(tok::comma))
Content = Content.drop_front(1); return 1;
if (Content.endswith("\"")) if (Right.isOneOf(tok::lessless, tok::plus) && Left.isLabelString() &&
Content = Content.drop_back(1); (Right.NextOperator || Right.OperatorIndex != 1))
Content = Content.trim(); return 25;
if (Content.size() > 1 && if (Right.is(tok::lessless))
(Content.back() == ':' || Content.back() == '='))
return 25;
}
return 1; // Breaking at a << is really cheap. return 1; // Breaking at a << is really cheap.
}
if (Left.is(TT_ConditionalExpr)) if (Left.is(TT_ConditionalExpr))
return prec::Conditional; return prec::Conditional;
prec::Level Level = Left.getPrecedence(); prec::Level Level = Left.getPrecedence();
@ -2631,7 +2626,8 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) {
<< " FakeLParens="; << " FakeLParens=";
for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i) for (unsigned i = 0, e = Tok->FakeLParens.size(); i != e; ++i)
llvm::errs() << Tok->FakeLParens[i] << "/"; llvm::errs() << Tok->FakeLParens[i] << "/";
llvm::errs() << " FakeRParens=" << Tok->FakeRParens << "\n"; llvm::errs() << " FakeRParens=" << Tok->FakeRParens;
llvm::errs() << " Text='" << Tok->TokenText << "'\n";
if (!Tok->Next) if (!Tok->Next)
assert(Tok == Line.Last); assert(Tok == Line.Last);
Tok = Tok->Next; Tok = Tok->Next;

View File

@ -5109,29 +5109,6 @@ TEST_F(FormatTest, AlignsPipes) {
"llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" "llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); " aaaaaaaaaaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaaaa);");
verifyFormat("return out << \"somepacket = {\\n\"\n"
" << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
" << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
" << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
" << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
" << \"}\";");
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
verifyFormat(
"llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
" << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
" << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
" << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
" << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
" << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
verifyFormat(
"void f() {\n"
" llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
"}");
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n" verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();"); " << aaaaaaaa.aaaaaaaaaaaa(aaa)->aaaaaaaaaaaaaa();");
verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" verifyFormat("llvm::errs() << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
@ -5142,22 +5119,6 @@ TEST_F(FormatTest, AlignsPipes) {
" bbb)\n" " bbb)\n"
" << a << b;"); " << a << b;");
// Breaking before the first "<<" is generally not desirable.
verifyFormat(
"llvm::errs()\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
// But sometimes, breaking before the first "<<" is desirable. // But sometimes, breaking before the first "<<" is desirable.
verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n" verifyFormat("Diag(aaaaaaaaaaaaaaaaaaaa, aaaaaaaa)\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);"); " << aaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaa);");
@ -5203,6 +5164,55 @@ TEST_F(FormatTest, AlignsPipes) {
verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";"); verifyFormat("llvm::errs() << \"\\n\" << bbbbbbbbbbbbbbbbbbbbbb << \"\\n\";");
} }
TEST_F(FormatTest, KeepStringLabelValuePairsOnALine) {
verifyFormat("return out << \"somepacket = {\\n\"\n"
" << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
" << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
" << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
" << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
" << \"}\";");
verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaa;");
verifyFormat(
"llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
" << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
" << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
" << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
" << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
verifyFormat("llvm::outs() << aaaaaaaaaaaaaaaaaaaaaaaa << \"=\"\n"
" << bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;");
verifyFormat(
"void f() {\n"
" llvm::outs() << \"aaaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaa);\n"
"}");
// Breaking before the first "<<" is generally not desirable.
verifyFormat(
"llvm::errs()\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
verifyFormat("llvm::errs() << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
" << \"aaaaaaaaaaaaaaaaaaa: \"\n"
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;",
getLLVMStyleWithColumns(70));
verifyFormat("string v = \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
" \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa +\n"
" \"aaaaaaaaaaaaaaaa: \" + aaaaaaaaaaaaaaaa;");
verifyFormat("string v = StrCat(\"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
" \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa,\n"
" \"aaaaaaaaaaaaaaaa: \", aaaaaaaaaaaaaaaa);");
}
TEST_F(FormatTest, UnderstandsEquals) { TEST_F(FormatTest, UnderstandsEquals) {
verifyFormat( verifyFormat(
"aaaaaaaaaaaaaaaaa =\n" "aaaaaaaaaaaaaaaaa =\n"