Skip to content

Commit db6e09d

Browse files
committed
[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
1 parent 2e5a523 commit db6e09d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2633,12 +2633,20 @@ class AnnotatingParser {
26332633
PreviousNotConst->MatchingParen->Previous->isNot(tok::kw_template);
26342634
}
26352635

2636-
if ((PreviousNotConst->is(tok::r_paren) &&
2637-
PreviousNotConst->is(TT_TypeDeclarationParen)) ||
2638-
PreviousNotConst->is(TT_AttributeRParen)) {
2636+
if (PreviousNotConst->is(tok::r_paren) &&
2637+
PreviousNotConst->is(TT_TypeDeclarationParen)) {
26392638
return true;
26402639
}
26412640

2641+
auto InTypeDecl = [&]() {
2642+
for (auto Next = Tok.Next; Next; Next = Next->Next)
2643+
if (Next->isOneOf(TT_ClassLBrace, TT_StructLBrace))
2644+
return true;
2645+
return false;
2646+
};
2647+
if (PreviousNotConst->is(TT_AttributeRParen) && (!IsCpp || !InTypeDecl()))
2648+
return true;
2649+
26422650
// If is a preprocess keyword like #define.
26432651
if (IsPPKeyword)
26442652
return false;

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12415,6 +12415,18 @@ TEST_F(FormatTest, UnderstandsAttributes) {
1241512415
verifyFormat("SomeType s __unused{InitValue};", CustomAttrs);
1241612416
verifyFormat("SomeType *__capability s(InitValue);", CustomAttrs);
1241712417
verifyFormat("SomeType *__capability s{InitValue};", CustomAttrs);
12418+
12419+
FormatStyle Style = getLLVMStyle(FormatStyle::LK_Cpp);
12420+
verifyFormat(
12421+
"template <>\n"
12422+
"struct __declspec(uuid(\"3895C200-8F26-4F5A-B29D-2B5D72E68F99\"))\n"
12423+
"IAsyncOperation<IUnknown *> : IAsyncOperation_impl<IUnknown *> {};",
12424+
Style);
12425+
verifyFormat(
12426+
"template <>\n"
12427+
"class __declspec(uuid(\"3895C200-8F26-4F5A-B29D-2B5D72E68F99\"))\n"
12428+
"IAsyncOperation<IUnknown *> : IAsyncOperation_impl<IUnknown *> {};",
12429+
Style);
1241812430
}
1241912431

1242012432
TEST_F(FormatTest, UnderstandsPointerQualifiersInCast) {

0 commit comments

Comments
 (0)