Skip to content

Commit e518e0b

Browse files
committedJan 30, 2017
[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
1 parent 98898f2 commit e518e0b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed
 

‎clang/lib/Format/BreakableToken.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,11 @@ BreakableLineCommentSection::BreakableLineCommentSection(
642642
Prefix.resize(Lines.size());
643643
OriginalPrefix.resize(Lines.size());
644644
for (size_t i = FirstLineIndex, e = Lines.size(); i < e; ++i) {
645-
StringRef IndentPrefix = getLineCommentIndentPrefix(Lines[i]);
645+
// We need to trim the blanks in case this is not the first line in a
646+
// multiline comment. Then the indent is included in Lines[i].
647+
StringRef IndentPrefix =
648+
getLineCommentIndentPrefix(Lines[i].ltrim(Blanks));
649+
assert(IndentPrefix.startswith("//"));
646650
OriginalPrefix[i] = Prefix[i] = IndentPrefix;
647651
if (Lines[i].size() > Prefix[i].size() &&
648652
isAlphanumeric(Lines[i][Prefix[i].size()])) {

‎clang/unittests/Format/FormatTest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,18 @@ TEST_F(FormatTest, SplitsLongCxxComments) {
13861386
"#define XXX // q w e r\n"
13871387
" // t y u i",
13881388
format("#define XXX //q w e r t y u i", getLLVMStyleWithColumns(22)));
1389+
EXPECT_EQ("{\n"
1390+
" //\n"
1391+
" //\\\n"
1392+
" // long 1 2 3 4\n"
1393+
" // 5\n"
1394+
"}",
1395+
format("{\n"
1396+
" //\n"
1397+
" //\\\n"
1398+
" // long 1 2 3 4 5\n"
1399+
"}",
1400+
getLLVMStyleWithColumns(20)));
13891401
}
13901402

13911403
TEST_F(FormatTest, PreservesHangingIndentInCxxComments) {

0 commit comments

Comments
 (0)