r353033 - [clang-format] Fix breaking of qualified operator
Krasimir Georgiev via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 4 01:56:16 PST 2019
Author: krasimir
Date: Mon Feb 4 01:56:16 2019
New Revision: 353033
URL: http://llvm.org/viewvc/llvm-project?rev=353033&view=rev
Log:
[clang-format] Fix breaking of qualified operator
Summary:
>From https://bugs.llvm.org/show_bug.cgi?id=40516
```
$ cat a.cpp
const NamespaceName::VeryLongClassName &NamespaceName::VeryLongClassName::myFunction() {
// do stuff
}
const NamespaceName::VeryLongClassName &NamespaceName::VeryLongClassName::operator++() {
// do stuff
}
$ ~/ll/build/opt/bin/clang-format -style=LLVM a.cpp
const NamespaceName::VeryLongClassName &
NamespaceName::VeryLongClassName::myFunction() {
// do stuff
}
const NamespaceName::VeryLongClassName &NamespaceName::VeryLongClassName::
operator++() {
// do stuff
}
```
What was happening is that the split penalty before `operator` was being set to
a smaller value by a prior if block. Moved checks around to fix this and added a
regression test.
Reviewers: djasper
Reviewed By: djasper
Tags: #clang
Differential Revision: https://reviews.llvm.org/D57604
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=353033&r1=353032&r2=353033&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Mon Feb 4 01:56:16 2019
@@ -2249,6 +2249,9 @@ unsigned TokenAnnotator::splitPenalty(co
return 500;
}
+ if (Left.is(tok::coloncolon) ||
+ (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
+ return 500;
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
Right.is(tok::kw_operator)) {
if (Line.startsWith(tok::kw_for) && Right.PartOfMultiVariableDeclStmt)
@@ -2267,9 +2270,6 @@ unsigned TokenAnnotator::splitPenalty(co
return 160;
if (Left.is(TT_CastRParen))
return 100;
- if (Left.is(tok::coloncolon) ||
- (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto))
- return 500;
if (Left.isOneOf(tok::kw_class, tok::kw_struct))
return 5000;
if (Left.is(tok::comment))
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=353033&r1=353032&r2=353033&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Mon Feb 4 01:56:16 2019
@@ -4183,6 +4183,18 @@ TEST_F(FormatTest, BreaksFunctionDeclara
Style);
}
+TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
+ // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
+ // Prefer keeping `::` followed by `operator` together.
+ EXPECT_EQ("const aaaa::bbbbbbb &\n"
+ "ccccccccc::operator++() {\n"
+ " stuff();\n"
+ "}",
+ format("const aaaa::bbbbbbb\n"
+ "&ccccccccc::operator++() { stuff(); }",
+ getLLVMStyleWithColumns(40)));
+}
+
TEST_F(FormatTest, TrailingReturnType) {
verifyFormat("auto foo() -> int;\n");
verifyFormat("struct S {\n"
More information about the cfe-commits
mailing list