[PATCH] Fix assertion in BreakableBlockComment (http://llvm.org/PR21916).

Alexander Kornienko alexfh at google.com
Fri Jan 9 08:19:13 PST 2015


Hi djasper,

http://reviews.llvm.org/D6894

Files:
  lib/Format/BreakableToken.cpp
  lib/Format/BreakableToken.h
  unittests/Format/FormatTest.cpp

Index: lib/Format/BreakableToken.cpp
===================================================================
--- lib/Format/BreakableToken.cpp
+++ lib/Format/BreakableToken.cpp
@@ -303,7 +303,8 @@
     StartOfLineColumn[i] += Decoration.size();
     Lines[i] = Lines[i].substr(Decoration.size());
     LeadingWhitespace[i] += Decoration.size();
-    IndentAtLineBreak = std::min<int>(IndentAtLineBreak, StartOfLineColumn[i]);
+    IndentAtLineBreak =
+        std::min<int>(IndentAtLineBreak, std::max(0, StartOfLineColumn[i]));
   }
   IndentAtLineBreak = std::max<unsigned>(IndentAtLineBreak, Decoration.size());
   DEBUG({
Index: lib/Format/BreakableToken.h
===================================================================
--- lib/Format/BreakableToken.h
+++ lib/Format/BreakableToken.h
@@ -212,6 +212,11 @@
   // StartOfLineColumn[i] is the target column at which Line[i] should be.
   // Note that this excludes a leading "* " or "*" in case all lines have
   // a "*" prefix.
+  // The first line's target column is always positive. The rest lines' target
+  // columns are relative to the first line to allow correct indentation of
+  // comments in \c WhitespaceManager, so they can be negative as well (in case
+  // the first line needs to be unindented more than there's actual whitespace
+  // in another line).
   SmallVector<int, 16> StartOfLineColumn;
 
   // The column at which the text of a broken line should start.
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -2812,6 +2812,17 @@
   EXPECT_EQ("/* \\  \\  \\\n*/", format("\\\n/* \\  \\  \\\n*/"));
 }
 
+TEST_F(FormatTest, DontCrashOnBlockComments) {
+  EXPECT_EQ(
+      "int xxxxxxxxx; /* "
+      "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\n"
+      "zzzzzz\n"
+      "0*/",
+      format("int xxxxxxxxx;                          /* "
+             "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy zzzzzz\n"
+             "0*/"));
+}
+
 TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) {
   verifyFormat("#define A \\\n"
                "  int v(  \\\n"

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6894.17927.patch
Type: text/x-patch
Size: 2198 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150109/39c19e2a/attachment.bin>


More information about the cfe-commits mailing list