diff options
| author | Krasimir Georgiev <krasimir@google.com> | 2017-01-30 21:00:01 +0000 |
|---|---|---|
| committer | Krasimir Georgiev <krasimir@google.com> | 2017-01-30 21:00:01 +0000 |
| commit | e518e0bfe9326e32eca17bf532e4b58a07e4bc3a (patch) | |
| tree | 2dd583e0d12f4b95a0c42bf38c6a1ac5d6c24d64 | |
| parent | 98898f2b0283629a03e52950da0007bb82b01ef7 (diff) | |
| download | bcm5719-llvm-e518e0bfe9326e32eca17bf532e4b58a07e4bc3a.tar.gz bcm5719-llvm-e518e0bfe9326e32eca17bf532e4b58a07e4bc3a.zip | |
[clang-format] Fix regression that breaks comments without a comment 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
| -rw-r--r-- | clang/lib/Format/BreakableToken.cpp | 6 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 12 |
2 files changed, 17 insertions, 1 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 569ff1c6483..4a29faded14 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -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()])) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index d2820010405..4d4b64b20b3 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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) { |

