]> granicus.if.org Git - clang/commitdiff
[clang-format] Fix bug in reflow of block comments containing CR/LF
authorOwen Pan <owenpiano@gmail.com>
Tue, 23 Apr 2019 20:29:46 +0000 (20:29 +0000)
committerOwen Pan <owenpiano@gmail.com>
Tue, 23 Apr 2019 20:29:46 +0000 (20:29 +0000)
Fix PR36119

Differential Revision: https://reviews.llvm.org/D60996

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

lib/Format/BreakableToken.cpp
lib/Format/BreakableToken.h
lib/Format/ContinuationIndenter.cpp
lib/Format/WhitespaceManager.h
unittests/Format/FormatTest.cpp

index ec197aa541a0fc359f4562352f45c35023e931d3..5172c97f8afa045adfab20f174d31e6d4808b0ce 100644 (file)
@@ -329,7 +329,7 @@ static bool mayReflowContent(StringRef Content) {
 BreakableBlockComment::BreakableBlockComment(
     const FormatToken &Token, unsigned StartColumn,
     unsigned OriginalStartColumn, bool FirstInLine, bool InPPDirective,
-    encoding::Encoding Encoding, const FormatStyle &Style)
+    encoding::Encoding Encoding, const FormatStyle &Style, bool UseCRLF)
     : BreakableComment(Token, StartColumn, InPPDirective, Encoding, Style),
       DelimitersOnNewline(false),
       UnbreakableTailLength(Token.UnbreakableTailLength) {
@@ -338,7 +338,8 @@ BreakableBlockComment::BreakableBlockComment(
 
   StringRef TokenText(Tok.TokenText);
   assert(TokenText.startswith("/*") && TokenText.endswith("*/"));
-  TokenText.substr(2, TokenText.size() - 4).split(Lines, "\n");
+  TokenText.substr(2, TokenText.size() - 4).split(Lines,
+                                                  UseCRLF ? "\r\n" : "\n");
 
   int IndentDelta = StartColumn - OriginalStartColumn;
   Content.resize(Lines.size());
@@ -472,7 +473,7 @@ void BreakableBlockComment::adjustWhitespace(unsigned LineIndex,
   // Calculate the start of the non-whitespace text in the current line.
   size_t StartOfLine = Lines[LineIndex].find_first_not_of(Blanks);
   if (StartOfLine == StringRef::npos)
-    StartOfLine = Lines[LineIndex].rtrim("\r\n").size();
+    StartOfLine = Lines[LineIndex].size();
 
   StringRef Whitespace = Lines[LineIndex].substr(0, StartOfLine);
   // Adjust Lines to only contain relevant text.
index 5fab3f2f1145aa3ebb6d837f3f1bb58bc40d0cc5..dba763c6ad13560e74f61ae2335667fcb1abf747 100644 (file)
@@ -359,7 +359,7 @@ public:
   BreakableBlockComment(const FormatToken &Token, unsigned StartColumn,
                         unsigned OriginalStartColumn, bool FirstInLine,
                         bool InPPDirective, encoding::Encoding Encoding,
-                        const FormatStyle &Style);
+                        const FormatStyle &Style, bool UseCRLF);
 
   unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
                           StringRef::size_type Length,
index 01665cd5de3dc36d6dc910d63588530fa655874e..b04ede6fa939c4b1e939cf250a5051e47485ac05 100644 (file)
@@ -1810,7 +1810,7 @@ ContinuationIndenter::createBreakableToken(const FormatToken &Current,
     }
     return llvm::make_unique<BreakableBlockComment>(
         Current, StartColumn, Current.OriginalColumn, !Current.Previous,
-        State.Line->InPPDirective, Encoding, Style);
+        State.Line->InPPDirective, Encoding, Style, Whitespaces.useCRLF());
   } else if (Current.is(TT_LineComment) &&
              (Current.Previous == nullptr ||
               Current.Previous->isNot(TT_ImplicitStringLiteral))) {
index f08e71495cc2846d9837baa4ed0e8e0bf88bb11d..e19b2a5ab9feb9a70c20d5e62829fc2537f5be31 100644 (file)
@@ -40,6 +40,8 @@ public:
                     bool UseCRLF)
       : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
 
+  bool useCRLF() const { return UseCRLF; }
+
   /// Replaces the whitespace in front of \p Tok. Only call once for
   /// each \c AnnotatedToken.
   ///
index be8566c754503ed4677bb08ec973754d67d43703..d269aab02ed4c542dbcfdf6d5332b6f1b2cd8597 100644 (file)
@@ -12857,6 +12857,12 @@ TEST_F(FormatTest, SupportsCRLF) {
                    "should not introduce\r\n"
                    "an extra carriage return\r\n"
                    "*/\r\n"));
+  EXPECT_EQ("/*\r\n"
+            "\r\n"
+            "*/",
+            format("/*\r\n"
+                   "    \r\r\r\n"
+                   "*/"));
 }
 
 TEST_F(FormatTest, MunchSemicolonAfterBlocks) {