Become a little smarter with formatting long chains of pipes.
Assign a high penalty to breaking before "<<" if the previous token is a
string literal ending in ":" or "=".
Before:
llvm::outs()
<< "aaaaaaaaaaaaaaaaa = " << aaaaaaaaaaaaaaaaa << "bbbbbbbbbbbbbbbbb = "
<< bbbbbbbbbbbbbbbbb << "ccccccccccccccccc = " << ccccccccccccccccc
<< "ddddddddddddddddd = " << ddddddddddddddddd << "eeeeeeeeeeeeeeeee = "
<< eeeeeeeeeeeeeeeee;
After:
llvm::outs() << "aaaaaaaaaaaaaaaaa = " << aaaaaaaaaaaaaaaaa
<< "bbbbbbbbbbbbbbbbb = " << bbbbbbbbbbbbbbbbb
<< "ccccccccccccccccc = " << ccccccccccccccccc
<< "ddddddddddddddddd = " << ddddddddddddddddd
<< "eeeeeeeeeeeeeeeee = " << eeeeeeeeeeeeeeeee;
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174545 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp
index 9c7834f..ad3baae 100644
--- a/lib/Format/TokenAnnotator.cpp
+++ b/lib/Format/TokenAnnotator.cpp
@@ -784,8 +784,16 @@
Left.Type == TT_TemplateOpener)
return 20;
- if (Right.is(tok::lessless))
+ if (Right.is(tok::lessless)) {
+ if (Left.is(tok::string_literal)) {
+ char LastChar =
+ StringRef(Left.FormatTok.Tok.getLiteralData(),
+ Left.FormatTok.TokenLength).drop_back(1).rtrim().back();
+ if (LastChar == ':' || LastChar == '=')
+ return 100;
+ }
return prec::Shift;
+ }
if (Left.Type == TT_ConditionalExpr)
return prec::Assignment;
prec::Level Level = getPrecedence(Left);
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index 6909607..160126b 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -1318,6 +1318,13 @@
" << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
" << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
" << \"}\";");
+
+ verifyFormat(
+ "llvm::outs() << \"aaaaaaaaaaaaaaaaa = \" << aaaaaaaaaaaaaaaaa\n"
+ " << \"bbbbbbbbbbbbbbbbb = \" << bbbbbbbbbbbbbbbbb\n"
+ " << \"ccccccccccccccccc = \" << ccccccccccccccccc\n"
+ " << \"ddddddddddddddddd = \" << ddddddddddddddddd\n"
+ " << \"eeeeeeeeeeeeeeeee = \" << eeeeeeeeeeeeeeeee;");
}
TEST_F(FormatTest, UnderstandsEquals) {