Skip to content

Commit 480a904

Browse files
owencac-rhodes
authored andcommitted
release/21.x: [clang-format] Fix a crash on BAS_BlockIndent (#164047)
Backport 44a77f2
1 parent 3333dd8 commit 480a904

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

clang/lib/Format/ContinuationIndenter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) {
411411
}
412412
if (CurrentState.BreakBeforeClosingBrace &&
413413
(Current.closesBlockOrBlockTypeList(Style) ||
414-
(Current.is(tok::r_brace) &&
414+
(Current.is(tok::r_brace) && Current.MatchingParen &&
415415
Current.isBlockIndentedInitRBrace(Style)))) {
416416
return true;
417417
}

clang/lib/Format/FormatToken.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ bool FormatToken::isTypeOrIdentifier(const LangOptions &LangOpts) const {
5353

5454
bool FormatToken::isBlockIndentedInitRBrace(const FormatStyle &Style) const {
5555
assert(is(tok::r_brace));
56+
assert(MatchingParen);
57+
assert(MatchingParen->is(tok::l_brace));
5658
if (!Style.Cpp11BracedListStyle ||
5759
Style.AlignAfterOpenBracket != FormatStyle::BAS_BlockIndent) {
5860
return false;
5961
}
6062
const auto *LBrace = MatchingParen;
61-
assert(LBrace && LBrace->is(tok::l_brace));
6263
if (LBrace->is(BK_BracedInit))
6364
return true;
6465
if (LBrace->Previous && LBrace->Previous->is(tok::equal))

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9727,6 +9727,19 @@ TEST_F(FormatTest, ParenthesesAndOperandAlignment) {
97279727
Style);
97289728
}
97299729

9730+
TEST_F(FormatTest, BlockIndentAndNamespace) {
9731+
auto Style = getLLVMStyleWithColumns(120);
9732+
Style.AllowShortNamespacesOnASingleLine = true;
9733+
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent;
9734+
9735+
verifyNoCrash(
9736+
"namespace {\n"
9737+
"void xxxxxxxxxxxxxxxxxxxxx(nnnnn::TTTTTTTTTTTTT const *mmmm,\n"
9738+
" YYYYYYYYYYYYYYYYY &yyyyyyyyyyyyyy);\n"
9739+
"} //",
9740+
Style);
9741+
}
9742+
97309743
TEST_F(FormatTest, BreaksConditionalExpressions) {
97319744
verifyFormat(
97329745
"aaaa(aaaaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaaaaaaaaaaa\n"

0 commit comments

Comments
 (0)