[PATCH] D36614: [clang-format] Refine trailing comment detection
Krasimir Georgiev via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 11 07:09:54 PDT 2017
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36614.110720.patch
Type: text/x-patch
Size: 1502 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170811/72709871/attachment.bin>
More information about the cfe-commits
mailing list