Skip to content

Commit 2603ee0

Browse files
committed
Improve formatting of stream operators.
If there are string literals on either side of a '<<', chances are high that they represent logically separate concepts. Otherwise, the author could just have just a single literal (possible split over multiple lines). So, we can now nicely format things like: cout << "somepacket = {\n" << " val a = " << ValueA << "\n" << " val b = " << ValueB << "\n" << "}"; llvm-svn: 174310
1 parent 1f14098 commit 2603ee0

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedToken &Current) {
669669
(Current.is(tok::string_literal) &&
670670
Current.Parent->is(tok::string_literal))) {
671671
Current.MustBreakBefore = true;
672+
} else if (Current.is(tok::lessless) && !Current.Children.empty() &&
673+
Current.Parent->is(tok::string_literal) &&
674+
Current.Children[0].is(tok::string_literal)) {
675+
Current.MustBreakBefore = true;
672676
} else {
673677
Current.MustBreakBefore = false;
674678
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,13 @@ TEST_F(FormatTest, AlignsPipes) {
12891289
"aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
12901290
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
12911291
" << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
1292+
1293+
verifyFormat("return out << \"somepacket = {\\n\"\n"
1294+
" << \" aaaaaa = \" << pkt.aaaaaa << \"\\n\"\n"
1295+
" << \" bbbb = \" << pkt.bbbb << \"\\n\"\n"
1296+
" << \" cccccc = \" << pkt.cccccc << \"\\n\"\n"
1297+
" << \" ddd = [\" << pkt.ddd << \"]\\n\"\n"
1298+
" << \"}\";");
12921299
}
12931300

12941301
TEST_F(FormatTest, UnderstandsEquals) {

0 commit comments

Comments
 (0)