[clang] 8e9b1e9 - [clang-format] Fix a bug in annotating struct braces (#90555)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Apr 30 20:01:37 PDT 2024
Author: Owen Pan
Date: 2024-04-30T20:01:34-07:00
New Revision: 8e9b1e9aa83b44fad44954bbd01c824def2b190f
URL: https://github.com/llvm/llvm-project/commit/8e9b1e9aa83b44fad44954bbd01c824def2b190f
DIFF: https://github.com/llvm/llvm-project/commit/8e9b1e9aa83b44fad44954bbd01c824def2b190f.diff
LOG: [clang-format] Fix a bug in annotating struct braces (#90555)
Fixes #60040.
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
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