https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/119044
>From c5bf1fc70df08ef94cc32a47d1bdce69c92c2abf Mon Sep 17 00:00:00 2001 From: Gedare Bloom <ged...@rtems.org> Date: Fri, 6 Dec 2024 16:52:35 -0700 Subject: [PATCH 1/2] [clang-format] Reorder TokenAnnotator::canBreakBefore Move the checks related to breaking before right braces and right parens earlier to avoid conflicting checks that prevent breaking based on the left-hand token. This allows properly formatting declarations with pointers and references. --- clang/lib/Format/TokenAnnotator.cpp | 57 ++++++++++++++------------- clang/unittests/Format/FormatTest.cpp | 9 +++++ 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index bc5239209f3aab..8d5ec0d0bb71ed 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -6105,6 +6105,35 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return false; } + // We can break before an r_brace if there was a corresponding break after + // the l_brace, which is tracked by BreakBeforeClosingBrace, or if we are + // in a block indented initialization list. + if (Right.is(tok::r_brace)) { + return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) || + (Right.isBlockIndentedInitRBrace(Style))); + } + + // We only break before r_paren if we're in a block indented context. + if (Right.is(tok::r_paren)) { + if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent || + !Right.MatchingParen) { + return false; + } + auto Next = Right.Next; + if (Next && Next->is(tok::r_paren)) + Next = Next->Next; + if (Next && Next->is(tok::l_paren)) + return false; + const FormatToken *Previous = Right.MatchingParen->Previous; + return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf())); + } + + if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) && + Right.is(TT_TrailingAnnotation) && + Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { + return false; + } + if (Left.is(tok::at)) return false; if (Left.Tok.getObjCKeywordID() == tok::objc_interface) @@ -6260,34 +6289,6 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return false; } - // We only break before r_brace if there was a corresponding break before - // the l_brace, which is tracked by BreakBeforeClosingBrace. - if (Right.is(tok::r_brace)) { - return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) || - (Right.isBlockIndentedInitRBrace(Style))); - } - - // We only break before r_paren if we're in a block indented context. - if (Right.is(tok::r_paren)) { - if (Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent || - !Right.MatchingParen) { - return false; - } - auto Next = Right.Next; - if (Next && Next->is(tok::r_paren)) - Next = Next->Next; - if (Next && Next->is(tok::l_paren)) - return false; - const FormatToken *Previous = Right.MatchingParen->Previous; - return !(Previous && (Previous->is(tok::kw_for) || Previous->isIf())); - } - - if (Left.isOneOf(tok::r_paren, TT_TrailingAnnotation) && - Right.is(TT_TrailingAnnotation) && - Style.AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) { - return false; - } - // Allow breaking after a trailing annotation, e.g. after a method // declaration. if (Left.is(TT_TrailingAnnotation)) { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 250e51b5421664..8e9f2695f5f8a8 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -9441,6 +9441,15 @@ TEST_F(FormatTest, AlignsAfterOpenBracket) { " aaaaaaaaaaaaaaaa\n" ");", Style); + verifyFormat("void foo(\n" + " void (*foobarpntr)(\n" + " aaaaaaaaaaaaaaaaaa *,\n" + " bbbbbbbbbbbbbb *,\n" + " cccccccccccccccccccc *,\n" + " dddddddddddddddddd *\n" + " )\n" + ");", + Style); verifyFormat("aaaaaaa<bbbbbbbb> const aaaaaaaaaa{\n" " aaaaaaaaaaaaa(aaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" "};", >From 19ab2bf53b516e5553d8a631c1972160dc024977 Mon Sep 17 00:00:00 2001 From: Owen Pan <owenpi...@gmail.com> Date: Mon, 9 Dec 2024 21:04:33 -0800 Subject: [PATCH 2/2] Update clang/lib/Format/TokenAnnotator.cpp comments --- clang/lib/Format/TokenAnnotator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 8d5ec0d0bb71ed..aaac2ab10a1720 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -6105,9 +6105,9 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, return false; } - // We can break before an r_brace if there was a corresponding break after - // the l_brace, which is tracked by BreakBeforeClosingBrace, or if we are - // in a block indented initialization list. + // We can break before an r_brace if there was a break after the matching + // l_brace, which is tracked by BreakBeforeClosingBrace, or if we are in a + // block-indented initialization list. if (Right.is(tok::r_brace)) { return Right.MatchingParen && (Right.MatchingParen->is(BK_Block) || (Right.isBlockIndentedInitRBrace(Style))); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits