Skip to content

Commit 179d24d

Browse files
committed
Parse: handle another case of invalid handling for attributes
clang would improperly disallow GNU attributes before C++ standard attributes when a declaration had a linkage specifier. Handle this similarly to the previous case of invalid parsing. We now better match the parsing rules from GCC. Differential Revision: https://reviews.llvm.org/D140507 Reviewed By: aaron.ballman
1 parent 6daa983 commit 179d24d

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

clang/lib/Parse/ParseDeclCXX.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,11 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
359359
Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
360360

361361
ParsedAttributes DeclAttrs(AttrFactory);
362-
MaybeParseCXX11Attributes(DeclAttrs);
363-
ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
362+
ParsedAttributes DeclSpecAttrs(AttrFactory);
363+
364+
while (MaybeParseCXX11Attributes(DeclAttrs) ||
365+
MaybeParseGNUAttributes(DeclSpecAttrs))
366+
;
364367

365368
if (Tok.isNot(tok::l_brace)) {
366369
// Reset the source range in DS, as the leading "extern"
@@ -369,7 +372,7 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
369372
DS.SetRangeEnd(SourceLocation());
370373
// ... but anyway remember that such an "extern" was seen.
371374
DS.setExternInLinkageSpec(true);
372-
ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs, &DS);
375+
ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs, &DS);
373376
return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
374377
getCurScope(), LinkageSpec, SourceLocation())
375378
: nullptr;
@@ -411,7 +414,7 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
411414
default:
412415
ParsedAttributes DeclAttrs(AttrFactory);
413416
MaybeParseCXX11Attributes(DeclAttrs);
414-
ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs);
417+
ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
415418
continue;
416419
}
417420

clang/test/Parser/cxx-attributes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// GH#58229 - rejects-valid
44
__attribute__((__visibility__("default"))) [[nodiscard]] int f();
55
[[nodiscard]] __attribute__((__visibility__("default"))) int f();
6+
extern "C" __attribute__((__visibility__("default"))) [[nodiscard]]
7+
int g() { return 32; }
68

79
class c {
810
virtual void f1(const char* a, ...)

0 commit comments

Comments
 (0)