]> granicus.if.org Git - clang/commitdiff
Fixes error when splitting block comments.
authorManuel Klimek <klimek@google.com>
Wed, 29 May 2013 22:06:18 +0000 (22:06 +0000)
committerManuel Klimek <klimek@google.com>
Wed, 29 May 2013 22:06:18 +0000 (22:06 +0000)
When trying to fall back to search from the end onwards, we
would still find leading whitespace if the leading whitespace
went on after the end of the line.

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

lib/Format/BreakableToken.cpp
unittests/Format/FormatTest.cpp

index c102f8b1b2955fb5c4b3638e34a5f25b57ac487b..5c3ad9cee2bc3aed1e1389f07d02803cf34759c0 100644 (file)
@@ -87,8 +87,16 @@ BreakableToken::Split getCommentSplit(StringRef Text,
   StringRef::size_type SpaceOffset = Text.rfind(' ', MaxSplit);
   if (SpaceOffset == StringRef::npos ||
       // Don't break at leading whitespace.
-      Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos)
-    SpaceOffset = Text.find(' ', MaxSplit);
+      Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos) {
+    // Make sure that we don't break at leading whitespace that
+    // reaches past MaxSplit.
+    StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(" ");
+    if (FirstNonWhitespace == StringRef::npos)
+      // If the comment is only whitespace, we cannot split.
+      return BreakableToken::Split(StringRef::npos, 0);
+    SpaceOffset =
+        Text.find(' ', std::max<unsigned>(MaxSplit, FirstNonWhitespace));
+  }
   if (SpaceOffset != StringRef::npos && SpaceOffset != 0) {
     StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim();
     StringRef AfterCut = Text.substr(SpaceOffset).ltrim();
index 18a1a008582640a3fa2d3f1a589893b21680b607..1f4f4806c3b7f2c12b5c81aefc80499340cac68e 100644 (file)
@@ -3694,6 +3694,33 @@ TEST_F(FormatTest, BlockCommentsInMacros) {
                    getLLVMStyleWithColumns(20)));
 }
 
+TEST_F(FormatTest, BlockCommentsAtEndOfLine) {
+  EXPECT_EQ("a = {\n"
+            "  1111 /*    */\n"
+            "};",
+            format("a = {1111\n"
+                   "/*    */\n"
+                   "};",
+                   getLLVMStyleWithColumns(15)));
+  EXPECT_EQ("a = {\n"
+            "  1111 /*      */\n"
+            "};",
+            format("a = {1111\n"
+                   "/*      */\n"
+                   "};",
+                   getLLVMStyleWithColumns(15)));
+
+  // FIXME: The formatting is still wrong here.
+  EXPECT_EQ("a = {\n"
+            "  1111 /*      a\n"
+            "          */\n"
+            "};",
+            format("a = {1111\n"
+                   "/*      a */\n"
+                   "};",
+                   getLLVMStyleWithColumns(15)));
+}
+
 TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) {
   // FIXME: This is not what we want...
   verifyFormat("{\n"