Skip to content

Commit

Permalink
[clang-format] Fix bug in reflow of block comments containing CR/LF
Browse files Browse the repository at this point in the history
Fix PR36119

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

llvm-svn: 359029
  • Loading branch information
owenca committed Apr 23, 2019
1 parent acbf005 commit 108cbbc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 5 deletions.
7 changes: 4 additions & 3 deletions clang/lib/Format/BreakableToken.cpp
Expand Up @@ -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) {
Expand All @@ -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());
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/BreakableToken.h
Expand Up @@ -359,7 +359,7 @@ class BreakableBlockComment : public BreakableComment {
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,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Expand Up @@ -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))) {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Format/WhitespaceManager.h
Expand Up @@ -40,6 +40,8 @@ class WhitespaceManager {
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.
///
Expand Down
6 changes: 6 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -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) {
Expand Down

0 comments on commit 108cbbc

Please sign in to comment.