krasimir created this revision. Herald added a subscriber: klimek. This patch fixes an non-idempotency issue connected with detection of trailing comments. Consider formatting the following code with column limit at `V`:
V const /* comment comment */ A = B; The comment is not a trailing comment, breaking before it doesn't bring it under the column limit. The formatter breaks after it, resulting in: V const /* comment comment */ A = B; For a next reformat, the formatter considers the comment as a trailing comment, so it is free to break it further, resulting in: V const /* comment comment */ A = B; This patch improves this by refining the trailing comment detection, so that the comment in the second version is not considered trailing anymore. https://reviews.llvm.org/D36614 Files: lib/Format/FormatToken.h unittests/Format/FormatTestComments.cpp Index: unittests/Format/FormatTestComments.cpp =================================================================== --- unittests/Format/FormatTestComments.cpp +++ unittests/Format/FormatTestComments.cpp @@ -451,6 +451,17 @@ verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("const /** comment comment */ A = B;", + getLLVMStyleWithColumns(40)); + + verifyFormat("const /** comment comment comment */ A =\n" + " B;", + getLLVMStyleWithColumns(40)); + + verifyFormat("const /** comment comment comment comment */\n" + " A = B;", + getLLVMStyleWithColumns(40)); + FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" Index: lib/Format/FormatToken.h =================================================================== --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -391,7 +391,11 @@ bool isTrailingComment() const { return is(tok::comment) && - (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0); + (is(TT_LineComment) || !Next || + (Next->NewlinesBefore > 0 && + (!Previous || + Previous->isOneOf(tok::comma, tok::equal, tok::l_brace) || + Next->is(tok::r_brace)))); } /// \brief Returns \c true if this is a keyword that can be used
Index: unittests/Format/FormatTestComments.cpp =================================================================== --- unittests/Format/FormatTestComments.cpp +++ unittests/Format/FormatTestComments.cpp @@ -451,6 +451,17 @@ verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); + verifyFormat("const /** comment comment */ A = B;", + getLLVMStyleWithColumns(40)); + + verifyFormat("const /** comment comment comment */ A =\n" + " B;", + getLLVMStyleWithColumns(40)); + + verifyFormat("const /** comment comment comment comment */\n" + " A = B;", + getLLVMStyleWithColumns(40)); + FormatStyle NoBinPacking = getLLVMStyle(); NoBinPacking.BinPackParameters = false; verifyFormat("aaaaaaaa(/* parameter 1 */ aaaaaa,\n" Index: lib/Format/FormatToken.h =================================================================== --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -391,7 +391,11 @@ bool isTrailingComment() const { return is(tok::comment) && - (is(TT_LineComment) || !Next || Next->NewlinesBefore > 0); + (is(TT_LineComment) || !Next || + (Next->NewlinesBefore > 0 && + (!Previous || + Previous->isOneOf(tok::comma, tok::equal, tok::l_brace) || + Next->is(tok::r_brace)))); } /// \brief Returns \c true if this is a keyword that can be used
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits