Skip to content

Commit

Permalink
[clang-format] Fix regression that breaks comments without a comment …
Browse files Browse the repository at this point in the history
…prefix

Summary:
Consider formatting the following code fragment with column limit 20:
```
{
  // line 1
  // line 2\
  // long long long line
}
```
Before this fix the output is:
```
{
  // line 1
  // line 2\
  // long long
  long line
}
```
This patch fixes a regression that breaks the last comment line without
adding the '//' prefix.

Reviewers: djasper

Reviewed By: djasper

Subscribers: cfe-commits, klimek

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

llvm-svn: 293548
  • Loading branch information
krasimirgg committed Jan 30, 2017
1 parent 98898f2 commit e518e0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion clang/lib/Format/BreakableToken.cpp
Expand Up @@ -642,7 +642,11 @@ BreakableLineCommentSection::BreakableLineCommentSection(
Prefix.resize(Lines.size());
OriginalPrefix.resize(Lines.size());
for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) {
StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i]);
// We need to trim the blanks in case this is not the first line in a
// multiline comment. Then the indent is included in Lines[i].
StringRef IndentPrefix =
getLineCommentIndentPrefix(Lines[i].ltrim(Blanks));
assert(IndentPrefix.startswith("//"));
OriginalPrefix[i] = Prefix[i] = IndentPrefix;
if (Lines[i].size() > Prefix[i].size() &&
isAlphanumeric(Lines[i][Prefix[i].size()])) {
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Expand Up @@ -1386,6 +1386,18 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
"#define XXX // q w e r\n"
" // t y u i",
format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
EXPECT_EQ("{\n"
" //\n"
" //\\\n"
" // long 1 2 3 4\n"
" // 5\n"
"}",
format("{\n"
" //\n"
" //\\\n"
" // long 1 2 3 4 5\n"
"}",
getLLVMStyleWithColumns(20)));
}

TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {
Expand Down

0 comments on commit e518e0b

Please sign in to comment.