]> granicus.if.org Git - clang/commitdiff
Fix bug where we wouldn't break columns over the limit.
authorManuel Klimek <klimek@google.com>
Mon, 4 Dec 2017 08:53:16 +0000 (08:53 +0000)
committerManuel Klimek <klimek@google.com>
Mon, 4 Dec 2017 08:53:16 +0000 (08:53 +0000)
Before, we would not break:
  int a = foo(/* trailing */);
when the end of /* trailing */ was exactly the column limit; the reason
is that block comments can have an unbreakable tail length - in this case
2, for the trailing ");"; we would unconditionally account that when
calculating the column state at the end of the token, but not correctly
add it into the remaining column length before, as we do for string
literals.
The fix is to correctly account the trailing unbreakable sequence length
into our formatting decisions for block comments. Line comments cannot
have a trailing unbreakable sequence, so no change is needed for them.

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

lib/Format/BreakableToken.cpp
lib/Format/BreakableToken.h
unittests/Format/FormatTestComments.cpp

index a5fabfe2379ecf9b9e7beebec13ec7c3435f1cb8..4735ab3564f0ec91c3c50475eec1fe40023796c7 100644 (file)
@@ -316,7 +316,8 @@ BreakableBlockComment::BreakableBlockComment(
     unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
     encoding::Encoding Encoding, const FormatStyle &Style)
     : BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style),
-      DelimitersOnNewline(false) {
+      DelimitersOnNewline(false),
+      UnbreakableTailLength(Token.UnbreakableTailLength) {
   assert(Tok.is(TT_BlockComment) &&
          "block comment section must start with a block comment");
 
@@ -497,7 +498,8 @@ unsigned BreakableBlockComment::getRangeLength(unsigned LineIndex,
 unsigned BreakableBlockComment::getRemainingLength(unsigned LineIndex,
                                                    unsigned Offset,
                                                    unsigned StartColumn) const {
-  return getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn);
+  return UnbreakableTailLength +
+         getRangeLength(LineIndex, Offset, StringRef::npos, StartColumn);
 }
 
 unsigned BreakableBlockComment::getContentStartColumn(unsigned LineIndex,
index 8d9b2b295c9977fb9c838b60afe3d6bbf8c2151e..8ef26ef464da9ea8b085ec26994edac178ea8fcd 100644 (file)
@@ -405,6 +405,10 @@ private:
   // If true, make sure that the opening '/**' and the closing '*/' ends on a
   // line of itself. Styles like jsdoc require this for multiline comments.
   bool DelimitersOnNewline;
+
+  // Length of the sequence of tokens after this string literal that cannot
+  // contain line breaks.
+  unsigned UnbreakableTailLength;
 };
 
 class BreakableLineCommentSection : public BreakableComment {
index 220ce08ff53fb1e41d4e123fbaafc43c0cdcdc3a..ed11fbdb1fc0da92c686f7f2bbab7de0be78be48 100644 (file)
@@ -3080,6 +3080,15 @@ TEST_F(FormatTestComments, PythonStyleComments) {
                    getTextProtoStyleWithColumns(20)));
 }
 
+TEST_F(FormatTestComments, BreaksBeforeTrailingUnbreakableSequence) {
+  // The end of /* trail */ is exactly at 80 columns, but the unbreakable
+  // trailing sequence ); after it exceeds the column limit. Make sure we
+  // correctly break the line in that case.
+  verifyFormat("int a =\n"
+               "    foo(/* trail */);",
+               getLLVMStyleWithColumns(23));
+}
+
 } // end namespace
 } // end namespace format
 } // end namespace clang