diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3888,7 +3888,7 @@ if (Right.is(tok::kw___attribute) || (Right.is(tok::l_square) && Right.is(TT_AttributeSquare))) - return true; + return !Left.is(TT_AttributeSquare); if (Left.is(tok::identifier) && Right.is(tok::string_literal)) return true; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -7672,6 +7672,19 @@ MultiLineFunctions); } +TEST_F(FormatTest, AttributePenaltyBreaking) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" + " [[maybe_unused]] const shared_ptr &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" + " [[maybe_unused]] const shared_ptr &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " + "shared_ptr &C d) {\n}", + Style); +} + TEST_F(FormatTest, UnderstandsEllipsis) { verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template void Foo(Ts... ts) { Foo(ts...); }");