[clang] [clang-format] Fix a bug in annotating struct braces (PR #90555)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 29 21:15:20 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/90555
Fixes #60040.
>From 77d807d47d47ca9916edc03182e1952c27300a8a Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 29 Apr 2024 21:11:28 -0700
Subject: [PATCH] [clang-format] Fix a bug in annotating struct braces
Fixes #60040.
---
clang/lib/Format/UnwrappedLineParser.cpp | 5 ++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 13 +++++++++++++
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 3a263955a6a8fe..854428389740d8 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3944,8 +3944,11 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr) {
switch (FormatTok->Tok.getKind()) {
case tok::l_paren:
// We can have macros in between 'class' and the class name.
- if (!IsNonMacroIdentifier(Previous))
+ if (!IsNonMacroIdentifier(Previous) ||
+ // e.g. `struct macro(a) S { int i; };`
+ Previous->Previous == &InitialToken) {
parseParens();
+ }
break;
case tok::coloncolon:
break;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index dff5251d2e9406..01daf8dee505bc 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -438,6 +438,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_StructLBrace);
EXPECT_TOKEN(Tokens[3], tok::r_brace, TT_StructRBrace);
+ Tokens = annotate("struct macro(a) S {};");
+ ASSERT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[7], tok::r_brace, TT_StructRBrace);
+
Tokens = annotate("struct EXPORT_MACRO [[nodiscard]] C { int i; };");
ASSERT_EQ(Tokens.size(), 15u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_StructLBrace);
@@ -448,6 +453,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsStructs) {
EXPECT_TOKEN(Tokens[12], tok::l_brace, TT_StructLBrace);
EXPECT_TOKEN(Tokens[16], tok::r_brace, TT_StructRBrace);
+ Tokens = annotate("struct macro(a) S {\n"
+ " void f(T &t);\n"
+ "};");
+ ASSERT_EQ(Tokens.size(), 18u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_StructLBrace);
+ EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[15], tok::r_brace, TT_StructRBrace);
+
Tokens = annotate("template <typename T> struct S<const T[N]> {};");
ASSERT_EQ(Tokens.size(), 18u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::less, TT_TemplateOpener);
More information about the cfe-commits
mailing list