r204456 - clang-format: Preserve meaning of trailing comments on parameters.
Daniel Jasper
djasper at google.com
Fri Mar 21 04:58:46 PDT 2014
Author: djasper
Date: Fri Mar 21 06:58:45 2014
New Revision: 204456
URL: http://llvm.org/viewvc/llvm-project?rev=204456&view=rev
Log:
clang-format: Preserve meaning of trailing comments on parameters.
Formatting:
SomeFunction(a,
b, // comment
c);
Before:
SomeFunction(a, b, // comment
c);
After:
SomeFunction(a,
b, // comment
c);
Modified:
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=204456&r1=204455&r2=204456&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Mar 21 06:58:45 2014
@@ -1114,6 +1114,27 @@ void TokenAnnotator::calculateFormatting
Current->SpacesRequiredBefore = Style.Cpp11BracedListStyle ? 0 : 1;
else
Current->SpacesRequiredBefore = Style.SpacesBeforeTrailingComments;
+
+ // If we find a trailing comment, iterate backwards to determine whether
+ // it seems to relate to a specific parameter. If so, break before that
+ // parameter to avoid changing the comment's meaning. E.g. don't move 'b'
+ // to the previous line in:
+ // SomeFunction(a,
+ // b, // comment
+ // c);
+ if (Current->isTrailingComment()) {
+ for (FormatToken *Parameter = Current->Previous; Parameter;
+ Parameter = Parameter->Previous) {
+ if (Parameter->isOneOf(tok::comment, tok::r_brace))
+ break;
+ if (Parameter->Previous && Parameter->Previous->is(tok::comma)) {
+ if (Parameter->Previous->Type != TT_CtorInitializerComma &&
+ Parameter->HasUnescapedNewline)
+ Parameter->MustBreakBefore = true;
+ break;
+ }
+ }
+ }
} else if (Current->SpacesRequiredBefore == 0 &&
spaceRequiredBefore(Line, *Current)) {
Current->SpacesRequiredBefore = 1;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=204456&r1=204455&r2=204456&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 21 06:58:45 2014
@@ -777,6 +777,25 @@ TEST_F(FormatTest, UnderstandsSingleLine
"otherLine();"));
}
+TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
+ EXPECT_EQ("SomeFunction(a,\n"
+ " b, // comment\n"
+ " c);",
+ format("SomeFunction(a,\n"
+ " b, // comment\n"
+ " c);"));
+ EXPECT_EQ("SomeFunction(a, b, // comment (unclear relation)\n"
+ " c);",
+ format("SomeFunction(a, b, // comment (unclear relation)\n"
+ " c);"));
+ EXPECT_EQ("SomeFunction(a, // comment\n"
+ " b,\n"
+ " c); // comment",
+ format("SomeFunction(a, // comment\n"
+ " b,\n"
+ " c); // comment"));
+}
+
TEST_F(FormatTest, CanFormatCommentsLocally) {
EXPECT_EQ("int a; // comment\n"
"int b; // comment",
More information about the cfe-commits
mailing list