[clang] [clang-format] Correctly annotate l_brace after TypenameMacro (PR #96026)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 18 22:35:05 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Closes #<!-- -->95418.
---
Full diff: https://github.com/llvm/llvm-project/pull/96026.diff
3 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+9-3)
- (modified) clang/unittests/Format/FormatTest.cpp (+3)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+12)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/96026
More information about the cfe-commits
mailing list