Skip to content

[clang-format] Correctly annotate l_brace after TypenameMacro #96026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,8 @@ void UnwrappedLineParser::parseStructuralElement(
} else if (Style.BraceWrapping.AfterFunction) {
addUnwrappedLine();
}
FormatTok->setFinalizedType(TT_FunctionLBrace);
if (!Previous || Previous->isNot(TT_TypeDeclarationParen))
FormatTok->setFinalizedType(TT_FunctionLBrace);
parseBlock();
IsDecltypeAutoFunction = false;
addUnwrappedLine();
Expand Down Expand Up @@ -2545,10 +2546,10 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
if (Style.Language == FormatStyle::LK_Java && FormatTok->is(tok::l_brace))
parseChildBlock();
break;
case tok::r_paren:
case tok::r_paren: {
const auto *Prev = LeftParen->Previous;
if (!MightBeStmtExpr && !MightBeFoldExpr && !Line->InMacroBody &&
Style.RemoveParentheses > FormatStyle::RPS_Leave) {
const auto *Prev = LeftParen->Previous;
const auto *Next = Tokens->peekNextToken();
const bool DoubleParens =
Prev && Prev->is(tok::l_paren) && Next && Next->is(tok::r_paren);
Expand All @@ -2570,8 +2571,13 @@ bool UnwrappedLineParser::parseParens(TokenType AmpAmpTokenType) {
FormatTok->Optional = true;
}
}
if (Prev && Prev->is(TT_TypenameMacro)) {
LeftParen->setFinalizedType(TT_TypeDeclarationParen);
FormatTok->setFinalizedType(TT_TypeDeclarationParen);
}
nextToken();
return SeenEqual;
}
case tok::r_brace:
// A "}" inside parenthesis is an error if there wasn't a matching "{".
return SeenEqual;
Expand Down
3 changes: 3 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27007,6 +27007,9 @@ TEST_F(FormatTest, RemoveSemicolon) {
"; int bar;",
Style);
#endif

Style.TypenameMacros.push_back("STRUCT");
verifyFormat("STRUCT(T, B) { int i; };", Style);
}

TEST_F(FormatTest, BreakAfterAttributes) {
Expand Down
12 changes: 12 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3180,6 +3180,18 @@ TEST_F(TokenAnnotatorTest, FunctionTryBlock) {
EXPECT_TOKEN(Tokens[36], tok::l_brace, TT_FunctionLBrace);
}

TEST_F(TokenAnnotatorTest, TypenameMacro) {
auto Style = getLLVMStyle();
Style.TypenameMacros.push_back("STRUCT");

auto Tokens = annotate("STRUCT(T, B) { int i; };", Style);
ASSERT_EQ(Tokens.size(), 13u);
EXPECT_TOKEN(Tokens[0], tok::identifier, TT_TypenameMacro);
EXPECT_TOKEN(Tokens[1], tok::l_paren, TT_TypeDeclarationParen);
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_TypeDeclarationParen);
EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_Unknown);
}

} // namespace
} // namespace format
} // namespace clang
Loading