[clang] 179d24d - Parse: handle another case of invalid handling for attributes

Saleem Abdulrasool via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 5 12:54:10 PST 2023


Author: Saleem Abdulrasool
Date: 2023-01-05T20:53:52Z
New Revision: 179d24d764ac68220047a8771e1bb06a7cb2a0da

URL: https://github.com/llvm/llvm-project/commit/179d24d764ac68220047a8771e1bb06a7cb2a0da
DIFF: https://github.com/llvm/llvm-project/commit/179d24d764ac68220047a8771e1bb06a7cb2a0da.diff

LOG: 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

Added: 
    

Modified: 
    clang/lib/Parse/ParseDeclCXX.cpp
    clang/test/Parser/cxx-attributes.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 53afdd7e7291d..b3ab5bcb238ab 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -359,8 +359,11 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
                 Tok.is(tok::l_brace) ? Tok.getLocation() : SourceLocation());
 
   ParsedAttributes DeclAttrs(AttrFactory);
-  MaybeParseCXX11Attributes(DeclAttrs);
-  ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
+  ParsedAttributes DeclSpecAttrs(AttrFactory);
+
+  while (MaybeParseCXX11Attributes(DeclAttrs) ||
+         MaybeParseGNUAttributes(DeclSpecAttrs))
+    ;
 
   if (Tok.isNot(tok::l_brace)) {
     // Reset the source range in DS, as the leading "extern"
@@ -369,7 +372,7 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
     DS.SetRangeEnd(SourceLocation());
     // ... but anyway remember that such an "extern" was seen.
     DS.setExternInLinkageSpec(true);
-    ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs, &DS);
+    ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs, &DS);
     return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
                              getCurScope(), LinkageSpec, SourceLocation())
                        : nullptr;
@@ -411,7 +414,7 @@ Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
     default:
       ParsedAttributes DeclAttrs(AttrFactory);
       MaybeParseCXX11Attributes(DeclAttrs);
-      ParseExternalDeclaration(DeclAttrs, EmptyDeclSpecAttrs);
+      ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
       continue;
     }
 

diff  --git a/clang/test/Parser/cxx-attributes.cpp b/clang/test/Parser/cxx-attributes.cpp
index 27cb8547c8b71..b46326c4d474f 100644
--- a/clang/test/Parser/cxx-attributes.cpp
+++ b/clang/test/Parser/cxx-attributes.cpp
@@ -3,6 +3,8 @@
 // GH#58229 - rejects-valid
 __attribute__((__visibility__("default"))) [[nodiscard]] int f();
 [[nodiscard]] __attribute__((__visibility__("default"))) int f();
+extern "C" __attribute__((__visibility__("default"))) [[nodiscard]]
+int g() { return 32; }
 
 class c {
   virtual void f1(const char* a, ...)


        


More information about the cfe-commits mailing list