r174310 - Improve formatting of stream operators.

Daniel Jasper djasper at google.com
Sun Feb 3 23:34:49 PST 2013


Author: djasper
Date: Mon Feb  4 01:34:48 2013
New Revision: 174310

URL: http://llvm.org/viewvc/llvm-project?rev=174310&view=rev
Log:
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"
     << "}";

Modified:
    cfe/trunk/lib/Format/TokenAnnotator.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=174310&r1=174309&r2=174310&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Feb  4 01:34:48 2013
@@ -669,6 +669,10 @@ void TokenAnnotator::calculateFormatting
              (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;
   }

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=174310&r1=174309&r2=174310&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb  4 01:34:48 2013
@@ -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) {





More information about the cfe-commits mailing list