r176350 - Implements breaking string literals at slashes.

Manuel Klimek klimek at google.com
Fri Mar 1 05:29:19 PST 2013


Author: klimek
Date: Fri Mar  1 07:29:19 2013
New Revision: 176350

URL: http://llvm.org/viewvc/llvm-project?rev=176350&view=rev
Log:
Implements breaking string literals at slashes.

We now break at a slash if we do not find a space to break on.

Also fixes a bug where we would go over the limit when breaking the
second line.

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

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=176350&r1=176349&r2=176350&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar  1 07:29:19 2013
@@ -720,10 +720,10 @@ private:
     while (StartColumn + TailLength > getColumnLimit()) {
       StringRef Text = StringRef(Current.FormatTok.Tok.getLiteralData() +
                                  TailOffset, TailLength);
-      if (StartColumn + 1 > getColumnLimit())
+      if (StartColumn + OffsetFromStart + 1 > getColumnLimit())
         break;
-      StringRef::size_type SplitPoint =
-          getSplitPoint(Text, getColumnLimit() - StartColumn - 1);
+      StringRef::size_type SplitPoint = getSplitPoint(
+          Text, getColumnLimit() - StartColumn - OffsetFromStart - 1);
       if (SplitPoint == StringRef::npos)
         break;
       assert(SplitPoint != 0);
@@ -751,10 +751,15 @@ private:
   StringRef::size_type
   getSplitPoint(StringRef Text, StringRef::size_type Offset) {
     StringRef::size_type SpaceOffset = Text.rfind(' ', Offset);
-    if (SpaceOffset == StringRef::npos && Offset > 0) {
+    if (SpaceOffset != StringRef::npos)
+      return SpaceOffset;
+    StringRef::size_type SlashOffset = Text.rfind('/', Offset);
+    if (SlashOffset != StringRef::npos)
+      return SlashOffset;
+    if (Offset > 1)
+      // Do not split at 0.
       return Offset - 1;
-    }
-    return SpaceOffset;
+    return StringRef::npos;
   }
 
   unsigned getColumnLimit() {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176350&r1=176349&r2=176350&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar  1 07:29:19 2013
@@ -3045,9 +3045,15 @@ TEST_F(FormatTest, BreakStringLiterals)
 
   EXPECT_EQ(
       "\"splitmea\"\n"
-      "\"trandompo\"\n"
-      "\"int\"",
+      "\"trandomp\"\n"
+      "\"oint\"",
       format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)));
+
+  EXPECT_EQ(
+      "\"split/\"\n"
+      "\"pathat/\"\n"
+      "\"slashes\"",
+      format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)));
 }
 
 } // end namespace tooling





More information about the cfe-commits mailing list