From 04b95ad719b5443ee000ff598b10eded14103c29 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Fri, 17 Oct 2025 20:07:23 -0700 Subject: [PATCH] [clang-format] Fix a crash on BAS_BlockIndent The crash was introduced in commit 413a7cb6e0db96b29bda0dab8541e225193ad139. Fixes #163355 --- clang/lib/Format/ContinuationIndenter.cpp | 2 +- clang/lib/Format/FormatToken.cpp | 3 ++- clang/unittests/Format/AlignBracketsTest.cpp | 13 +++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp index cd4c1aabac971..b7d85693b2082 100644 --- a/clang/lib/Format/ContinuationIndenter.cpp +++ b/clang/lib/Format/ContinuationIndenter.cpp @@ -411,7 +411,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { } if (CurrentState.BreakBeforeClosingBrace && (Current.closesBlockOrBlockTypeList(Style) || - (Current.is(tok::r_brace) && + (Current.is(tok::r_brace) && Current.MatchingParen && Current.isBlockIndentedInitRBrace(Style)))) { return true; } diff --git a/clang/lib/Format/FormatToken.cpp b/clang/lib/Format/FormatToken.cpp index c2956a179b8ed..222d521d52df9 100644 --- a/clang/lib/Format/FormatToken.cpp +++ b/clang/lib/Format/FormatToken.cpp @@ -66,12 +66,13 @@ bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const { bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const { assert(is(tok::r_brace)); + assert(MatchingParen); + assert(MatchingParen->is(tok::l_brace)); if (!Style.Cpp11BracedListStyle || Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) { return false; } const auto *LBrace = MatchingParen; - assert(LBrace && LBrace->is(tok::l_brace)); if (LBrace->is(BK_BracedInit)) return true; if (LBrace->Previous && LBrace->Previous->is(tok::equal)) diff --git a/clang/unittests/Format/AlignBracketsTest.cpp b/clang/unittests/Format/AlignBracketsTest.cpp index c4380ae415751..ea8db51a4d18e 100644 --- a/clang/unittests/Format/AlignBracketsTest.cpp +++ b/clang/unittests/Format/AlignBracketsTest.cpp @@ -778,6 +778,19 @@ TEST_F(AlignBracketsTest, ParenthesesAndOperandAlignment) { Style); } +TEST_F(AlignBracketsTest, BlockIndentAndNamespace) { + auto Style = getLLVMStyleWithColumns(120); + Style.AllowShortNamespacesOnASingleLine = true; + Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; + + verifyNoCrash( + "namespace {\n" + "void xxxxxxxxxxxxxxxxxxxxx(nnnnn::TTTTTTTTTTTTT const *mmmm,\n" + " YYYYYYYYYYYYYYYYY &yyyyyyyyyyyyyy);\n" + "} //", + Style); +} + } // namespace } // namespace test } // namespace format