diff options
Diffstat (limited to 'clang')
| -rw-r--r-- | clang/lib/Format/BreakableToken.cpp | 12 | ||||
| -rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 27 |
2 files changed, 37 insertions, 2 deletions
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index c102f8b1b29..5c3ad9cee2b 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -87,8 +87,16 @@ BreakableToken::Split getCommentSplit(StringRef Text, StringRef::size_type SpaceOffset = Text.rfind(' ', MaxSplit); if (SpaceOffset == StringRef::npos || // Don't break at leading whitespace. - Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos) - SpaceOffset = Text.find(' ', MaxSplit); + Text.find_last_not_of(' ', SpaceOffset) == StringRef::npos) { + // Make sure that we don't break at leading whitespace that + // reaches past MaxSplit. + StringRef::size_type FirstNonWhitespace = Text.find_first_not_of(" "); + if (FirstNonWhitespace == StringRef::npos) + // If the comment is only whitespace, we cannot split. + return BreakableToken::Split(StringRef::npos, 0); + SpaceOffset = + Text.find(' ', std::max<unsigned>(MaxSplit, FirstNonWhitespace)); + } if (SpaceOffset != StringRef::npos && SpaceOffset != 0) { StringRef BeforeCut = Text.substr(0, SpaceOffset).rtrim(); StringRef AfterCut = Text.substr(SpaceOffset).ltrim(); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 18a1a008582..1f4f4806c3b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -3694,6 +3694,33 @@ TEST_F(FormatTest, BlockCommentsInMacros) { getLLVMStyleWithColumns(20))); } +TEST_F(FormatTest, BlockCommentsAtEndOfLine) { + EXPECT_EQ("a = {\n" + " 1111 /* */\n" + "};", + format("a = {1111\n" + "/* */\n" + "};", + getLLVMStyleWithColumns(15))); + EXPECT_EQ("a = {\n" + " 1111 /* */\n" + "};", + format("a = {1111\n" + "/* */\n" + "};", + getLLVMStyleWithColumns(15))); + + // FIXME: The formatting is still wrong here. + EXPECT_EQ("a = {\n" + " 1111 /* a\n" + " */\n" + "};", + format("a = {1111\n" + "/* a */\n" + "};", + getLLVMStyleWithColumns(15))); +} + TEST_F(FormatTest, IndentLineCommentsInStartOfBlockAtEndOfFile) { // FIXME: This is not what we want... verifyFormat("{\n" |

