[clang] [clang-format] Fix parsing attrs in class/struct (PR #124634)
Gedare Bloom via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 27 13:55:07 PST 2025
https://github.com/gedare created https://github.com/llvm/llvm-project/pull/124634
An attribute in a class/struct declaration confuses the parser to treat the identifier following the attribute as a function or variable name.
Fixes #124574
>From db6e09dca9d39b1c21543a069e7a1d1bf3997c0c Mon Sep 17 00:00:00 2001
From: Gedare Bloom <gedare at rtems.org>
Date: Mon, 27 Jan 2025 14:49:54 -0700
Subject: [PATCH] [clang-format] Fix parsing attrs in class/struct
An attribute in a class/struct declaration confuses the parser to treat
the identifier following the attribute as a function or variable name.
Fixes #124574
---
clang/lib/Format/TokenAnnotator.cpp | 14 +++++++++++---
clang/unittests/Format/FormatTest.cpp | 12 ++++++++++++
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 08fda7a2ffa51a..3b4b0b78e8bf16 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2633,12 +2633,20 @@ class AnnotatingParser {
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
}
- if ((PreviousNotConst->is(tok::r_paren) &&
- PreviousNotConst->is(TT_TypeDeclarationParen)) ||
- PreviousNotConst->is(TT_AttributeRParen)) {
+ if (PreviousNotConst->is(tok::r_paren) &&
+ PreviousNotConst->is(TT_TypeDeclarationParen)) {
return true;
}
+ auto InTypeDecl = [&]() {
+ for (auto Next = Tok.Next; Next; Next = Next->Next)
+ if (Next->isOneOf(TT_ClassLBrace, TT_StructLBrace))
+ return true;
+ return false;
+ };
+ if (PreviousNotConst->is(TT_AttributeRParen) && (!IsCpp || !InTypeDecl()))
+ return true;
+
// If is a preprocess keyword like #define.
if (IsPPKeyword)
return false;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 265461561d2012..6145500a56582a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -12415,6 +12415,18 @@ TEST_F(FormatTest, UnderstandsAttributes) {
verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
+
+ FormatStyle Style = getLLVMStyle(FormatStyle::LK_Cpp);
+ verifyFormat(
+ "template <>\n"
+ "struct __declspec(uuid(\"3895C200-8F26-4F5A-B29D-2B5D72E68F99\"))\n"
+ "IAsyncOperation<IUnknown *> : IAsyncOperation_impl<IUnknown *> {};",
+ Style);
+ verifyFormat(
+ "template <>\n"
+ "class __declspec(uuid(\"3895C200-8F26-4F5A-B29D-2B5D72E68F99\"))\n"
+ "IAsyncOperation<IUnknown *> : IAsyncOperation_impl<IUnknown *> {};",
+ Style);
}
TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {
More information about the cfe-commits
mailing list