MyDeveloperDay created this revision. MyDeveloperDay added reviewers: krasimir, sammccall, mitchell-stellar. MyDeveloperDay added projects: clang, clang-format.
https://bugs.llvm.org/show_bug.cgi?id=45639 clang-format incorrectly splits the `[[` in a long argument list void SomeLongClassName::ALongMethodNameInThatClass([[maybe_unused]] const shared_ptr<ALongTypeName>& argumentNameForThat LongType) { } becomes void SomeLongClassName::ALongMethodNameInThatClass([ [maybe_unused]] const shared_ptr<ALongTypeName> &argumentNameForThatLongType) { } leaving one `[` on the previous line For a function with just 1 very long argument, clang-format chooses to split between the `[[`, This revision adjusts the penalty between `(` and `[` to ensure this is the more likely break point Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D79401 Files: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7661,6 +7661,19 @@ MultiLineFunctions); } +TEST_F(FormatTest, AttributePenaltyBreaking) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" + " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" + " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " + "shared_ptr<ALongTypeName> &C d) {\n}", + Style); +} + TEST_F(FormatTest, UnderstandsEllipsis) { verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2547,6 +2547,10 @@ TT_DesignatedInitializerLSquare, TT_AttributeSquare)) return 500; } + // Favour breaking between ( and [ when attributes are used as the first + // parameter e.g. foo([[maybe_unused]] T &bar). + if (Left.is(tok::l_paren) && Right.is(TT_AttributeSquare)) + return 1; if (Left.is(tok::coloncolon) || (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) @@ -2635,7 +2639,6 @@ if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && Left.Previous->isOneOf(tok::identifier, tok::greater)) return 500; - if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) return 100;
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -7661,6 +7661,19 @@ MultiLineFunctions); } +TEST_F(FormatTest, AttributePenaltyBreaking) { + FormatStyle Style = getLLVMStyle(); + verifyFormat("void ABCDEFGH::ABCDEFGHIJKLMN(\n" + " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGHIJK(\n" + " [[maybe_unused]] const shared_ptr<ALongTypeName> &C d) {}", + Style); + verifyFormat("void ABCDEFGH::ABCDEFGH([[maybe_unused]] const " + "shared_ptr<ALongTypeName> &C d) {\n}", + Style); +} + TEST_F(FormatTest, UnderstandsEllipsis) { verifyFormat("int printf(const char *fmt, ...);"); verifyFormat("template <class... Ts> void Foo(Ts... ts) { Foo(ts...); }"); Index: clang/lib/Format/TokenAnnotator.cpp =================================================================== --- clang/lib/Format/TokenAnnotator.cpp +++ clang/lib/Format/TokenAnnotator.cpp @@ -2547,6 +2547,10 @@ TT_DesignatedInitializerLSquare, TT_AttributeSquare)) return 500; } + // Favour breaking between ( and [ when attributes are used as the first + // parameter e.g. foo([[maybe_unused]] T &bar). + if (Left.is(tok::l_paren) && Right.is(TT_AttributeSquare)) + return 1; if (Left.is(tok::coloncolon) || (Right.is(tok::period) && Style.Language == FormatStyle::LK_Proto)) @@ -2635,7 +2639,6 @@ if (Line.Type == LT_ObjCDecl && Left.is(tok::l_paren) && Left.Previous && Left.Previous->isOneOf(tok::identifier, tok::greater)) return 500; - if (Left.is(tok::l_paren) && InFunctionDecl && Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign) return 100;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits