]> granicus.if.org Git - clang/commitdiff
Improve formatting of stream operators.
authorDaniel Jasper <djasper@google.com>
Mon, 4 Feb 2013 07:34:48 +0000 (07:34 +0000)
committerDaniel Jasper <djasper@google.com>
Mon, 4 Feb 2013 07:34:48 +0000 (07:34 +0000)
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"
     << "}";

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174310 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index 0cfd9903cb886b1ab165fbd5570a60aa16b77b83..34bccb978e7873374397c024ba8ca9aef4826d32 100644 (file)
@@ -669,6 +669,10 @@ void TokenAnnotator::calculateFormattingInformation(AnnotatedToken &Current) {
              (Current.is(tok::string_literal) &&
               Current.Parent->is(tok::string_literal))) {
     Current.MustBreakBefore = true;
+  } else if (Current.is(tok::lessless) && !Current.Children.empty() &&
+             Current.Parent->is(tok::string_literal) &&
+             Current.Children[0].is(tok::string_literal)) {
+    Current.MustBreakBefore = true;
   } else {
     Current.MustBreakBefore = false;
   }
index 17a8dc48abb9013de18cd1a718a32b09bc6025d9..3b5256990d1156748f4ab9ef4cdc97b6d5a0db31 100644 (file)
@@ -1289,6 +1289,13 @@ TEST_F(FormatTest, AlignsPipes) {
       "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
       "                                 << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
       "         << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
+
+  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"
+               "           << \"}\";");
 }
 
 TEST_F(FormatTest, UnderstandsEquals) {