[clang] [clang-format] Correctly annotate l_brace after TypenameMacro (PR #96026)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 18 22:34:37 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/96026
Closes #95418.
>From cc4cd4d9e7eea3ba670a29a053d18739b40058ab Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 18 Jun 2024 22:29:53 -0700
Subject: [PATCH] [clang-format] Correctly annotate l_brace after TypenameMacro
Closes #95418.
---
clang/lib/Format/UnwrappedLineParser.cpp | 12 +++++++++---
clang/unittests/Format/FormatTest.cpp | 3 +++
clang/unittests/Format/TokenAnnotatorTest.cpp | 12 ++++++++++++
3 files changed, 24 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index ce877ac2bb9ef..d406a531a5c0c 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -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();
@@ -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);
@@ -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;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fc63afce70042..db1decb20d626 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -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) {
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 3735316dc3478..0dc506458f7c3 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -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
More information about the cfe-commits
mailing list