krasimir created this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits. krasimir requested review of this revision.
In C++ with -Werror=comment, multiline comments are not allowed. clang-format could accidentally introduce multiline comments when reflowing. This adapts clang-format to not introduce multiline comments by not allowing a break after `\`. Note that this does not apply to comment lines that already are multiline comments, such as comments in macros. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D90949 Files: clang/lib/Format/BreakableToken.cpp clang/unittests/Format/FormatTestComments.cpp Index: clang/unittests/Format/FormatTestComments.cpp =================================================================== --- clang/unittests/Format/FormatTestComments.cpp +++ clang/unittests/Format/FormatTestComments.cpp @@ -745,6 +745,20 @@ getLLVMStyleWithColumns(49))); } +TEST_F(FormatTestComments, DontIntroduceMultilineComments) { + // Avoid introducing a multiline comment by breaking after `\`. + for (int ColumnLimit = 15; ColumnLimit <= 17; ++ColumnLimit) { + EXPECT_EQ( + "// aaaaaaaaaa\n" + "// \\ bb", + format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); + EXPECT_EQ( + "// aaaaaaaaa\n" + "// \\ bb", + format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); + } +} + TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { FormatStyle Pragmas = getLLVMStyleWithColumns(30); Pragmas.CommentPragmas = "^ IWYU pragma:"; Index: clang/lib/Format/BreakableToken.cpp =================================================================== --- clang/lib/Format/BreakableToken.cpp +++ clang/lib/Format/BreakableToken.cpp @@ -102,6 +102,17 @@ static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\."); while (SpaceOffset != StringRef::npos) { + // In C++ with the -Werror=comment option, having multiline comments ( + // two consecutive comment lines, the first ending in `\`) is an + // error. Avoid introducing multiline comments by not allowing a break right + // after '\'. + if (Style.isCpp()) { + StringRef::size_type LastNonBlank = + Text.find_last_not_of(Blanks, SpaceOffset); + if (LastNonBlank != StringRef::npos && Text[LastNonBlank] == '\\') + SpaceOffset = Text.find_last_of(Blanks, LastNonBlank); + } + // Do not split before a number followed by a dot: this would be interpreted // as a numbered list, which would prevent re-flowing in subsequent passes. if (kNumberedListRegexp.match(Text.substr(SpaceOffset).ltrim(Blanks)))
Index: clang/unittests/Format/FormatTestComments.cpp =================================================================== --- clang/unittests/Format/FormatTestComments.cpp +++ clang/unittests/Format/FormatTestComments.cpp @@ -745,6 +745,20 @@ getLLVMStyleWithColumns(49))); } +TEST_F(FormatTestComments, DontIntroduceMultilineComments) { + // Avoid introducing a multiline comment by breaking after `\`. + for (int ColumnLimit = 15; ColumnLimit <= 17; ++ColumnLimit) { + EXPECT_EQ( + "// aaaaaaaaaa\n" + "// \\ bb", + format("// aaaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); + EXPECT_EQ( + "// aaaaaaaaa\n" + "// \\ bb", + format("// aaaaaaaaa \\ bb", getLLVMStyleWithColumns(ColumnLimit))); + } +} + TEST_F(FormatTestComments, DontSplitLineCommentsWithPragmas) { FormatStyle Pragmas = getLLVMStyleWithColumns(30); Pragmas.CommentPragmas = "^ IWYU pragma:"; Index: clang/lib/Format/BreakableToken.cpp =================================================================== --- clang/lib/Format/BreakableToken.cpp +++ clang/lib/Format/BreakableToken.cpp @@ -102,6 +102,17 @@ static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\."); while (SpaceOffset != StringRef::npos) { + // In C++ with the -Werror=comment option, having multiline comments ( + // two consecutive comment lines, the first ending in `\`) is an + // error. Avoid introducing multiline comments by not allowing a break right + // after '\'. + if (Style.isCpp()) { + StringRef::size_type LastNonBlank = + Text.find_last_not_of(Blanks, SpaceOffset); + if (LastNonBlank != StringRef::npos && Text[LastNonBlank] == '\\') + SpaceOffset = Text.find_last_of(Blanks, LastNonBlank); + } + // Do not split before a number followed by a dot: this would be interpreted // as a numbered list, which would prevent re-flowing in subsequent passes. if (kNumberedListRegexp.match(Text.substr(SpaceOffset).ltrim(Blanks)))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits