Skip to content

Commit fa00e8b

Browse files
authored
[clang-format] Correctly annotate l_brace after TypenameMacro (#96026)
Closes #95418.
1 parent 34313eb commit fa00e8b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,7 +1910,8 @@ void UnwrappedLineParser::parseStructuralElement(
19101910
} else if (Style.BraceWrapping.AfterFunction) {
19111911
addUnwrappedLine();
19121912
}
1913-
FormatTok->setFinalizedType(TT_FunctionLBrace);
1913+
if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
1914+
FormatTok->setFinalizedType(TT_FunctionLBrace);
19141915
parseBlock();
19151916
IsDecltypeAutoFunction = false;
19161917
addUnwrappedLine();
@@ -2545,10 +2546,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25452546
if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
25462547
parseChildBlock();
25472548
break;
2548-
case tok::r_paren:
2549+
case tok::r_paren: {
2550+
const auto *Prev = LeftParen->Previous;
25492551
if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
25502552
Style.RemoveParentheses > FormatStyle::RPS_Leave) {
2551-
const auto *Prev = LeftParen->Previous;
25522553
const auto *Next = Tokens->peekNextToken();
25532554
const bool DoubleParens =
25542555
Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
@@ -2570,8 +2571,13 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
25702571
FormatTok->Optional = true;
25712572
}
25722573
}
2574+
if (Prev && Prev->is(TT_TypenameMacro)) {
2575+
LeftParen->setFinalizedType(TT_TypeDeclarationParen);
2576+
FormatTok->setFinalizedType(TT_TypeDeclarationParen);
2577+
}
25732578
nextToken();
25742579
return SeenEqual;
2580+
}
25752581
case tok::r_brace:
25762582
// A "}" inside parenthesis is an error if there wasn't a matching "{".
25772583
return SeenEqual;

clang/unittests/Format/FormatTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27007,6 +27007,9 @@ TEST_F(FormatTest, RemoveSemicolon) {
2700727007
"; int bar;",
2700827008
Style);
2700927009
#endif
27010+
27011+
Style.TypenameMacros.push_back("STRUCT");
27012+
verifyFormat("STRUCT(T, B) { int i; };", Style);
2701027013
}
2701127014

2701227015
TEST_F(FormatTest, BreakAfterAttributes) {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,6 +3180,18 @@ TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
31803180
EXPECT_TOKEN(Tokens[36], tok::l_brace, TT_FunctionLBrace);
31813181
}
31823182

3183+
TEST_F(TokenAnnotatorTest, TypenameMacro) {
3184+
auto Style = getLLVMStyle();
3185+
Style.TypenameMacros.push_back("STRUCT");
3186+
3187+
auto Tokens = annotate("STRUCT(T, B) { int i; };", Style);
3188+
ASSERT_EQ(Tokens.size(), 13u);
3189+
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TypenameMacro);
3190+
EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen);
3191+
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_TypeDeclarationParen);
3192+
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_Unknown);
3193+
}
3194+
31833195
} // namespace
31843196
} // namespace format
31853197
} // namespace clang

0 commit comments

Comments
 (0)