Author: Luis Penagos Date: 2022-02-20T21:53:50+01:00 New Revision: d9567babef302cfd7e827df64138151ba2614b83
URL: https://github.com/llvm/llvm-project/commit/d9567babef302cfd7e827df64138151ba2614b83 DIFF: https://github.com/llvm/llvm-project/commit/d9567babef302cfd7e827df64138151ba2614b83.diff LOG: Fix extraneous whitespace addition in line comments on clang-format directives Fixes https://github.com/llvm/llvm-project/issues/53844. I believe this regression was caused by not accounting for clang-format directives in https://reviews.llvm.org/D92257. Reviewed By: HazardyKnusperkeks, curdeius Differential Revision: https://reviews.llvm.org/D120188 Added: Modified: clang/lib/Format/BreakableToken.cpp clang/unittests/Format/FormatTestComments.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp index 5138c7cd42cc6..967ddeb82383a 100644 --- a/clang/lib/Format/BreakableToken.cpp +++ b/clang/lib/Format/BreakableToken.cpp @@ -815,10 +815,13 @@ BreakableLineCommentSection::BreakableLineCommentSection( assert(Lines[i].size() > IndentPrefix.size()); const auto FirstNonSpace = Lines[i][IndentPrefix.size()]; - const auto AllowsSpaceChange = - SpacesInPrefix != 0 || - (!NoSpaceBeforeFirstCommentChar() || - (FirstNonSpace == '}' && FirstLineSpaceChange != 0)); + const bool IsFormatComment = LineTok && switchesFormatting(*LineTok); + const bool LineRequiresLeadingSpace = + !NoSpaceBeforeFirstCommentChar() || + (FirstNonSpace == '}' && FirstLineSpaceChange != 0); + const bool AllowsSpaceChange = + !IsFormatComment && + (SpacesInPrefix != 0 || LineRequiresLeadingSpace); if (PrefixSpaceChange[i] > 0 && AllowsSpaceChange) { Prefix[i] = IndentPrefix.str(); diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index c988a2869e568..f83ffb393ac2f 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -91,6 +91,11 @@ TEST_F(FormatTestComments, UnderstandsSingleLineComments) { "// line 2\n" "void f() {}\n"); + EXPECT_EQ("// comment\n" + "// clang-format on\n", + format("//comment\n" + "// clang-format on\n")); + verifyFormat("void f() {\n" " // Doesn't do anything\n" "}"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits