junaire created this revision. junaire added reviewers: aaron.ballman, rsmith, erichkeane. Herald added a project: All. junaire requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
Don't warn on unknown type attributes in Parser::ProhibitCXX11Attributes for most cases, but left the diagnostic to the later checks. module declaration and module import declaration are special cases. Fixes https://github.com/llvm/llvm-project/issues/54817 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D123447 Files: clang/include/clang/Parse/Parser.h clang/lib/Parse/ParseDecl.cpp clang/lib/Parse/Parser.cpp Index: clang/lib/Parse/Parser.cpp =================================================================== --- clang/lib/Parse/Parser.cpp +++ clang/lib/Parse/Parser.cpp @@ -2377,7 +2377,9 @@ // We don't support any module attributes yet; just parse them and diagnose. ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); - ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr); + ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr, + /*DiagnoseEmptyAttrs=*/false, + /*WarnOnUnknownAttr=*/true); ExpectAndConsumeSemi(diag::err_module_expected_semi); @@ -2444,7 +2446,9 @@ ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); // We don't support any module import attributes yet. - ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr); + ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr, + /*DiagnoseEmptyAttrs=*/false, + /*WarnOnUnknownAttr=*/true); if (PP.hadModuleLoaderFatalFailure()) { // With a fatal failure in the module loader, we abort parsing. Index: clang/lib/Parse/ParseDecl.cpp =================================================================== --- clang/lib/Parse/ParseDecl.cpp +++ clang/lib/Parse/ParseDecl.cpp @@ -1658,7 +1658,8 @@ } void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID, - bool DiagnoseEmptyAttrs) { + bool DiagnoseEmptyAttrs, + bool WarnOnUnknownAttr) { if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) { // An attribute list has been parsed, but it was empty. @@ -1685,10 +1686,11 @@ for (const ParsedAttr &AL : Attrs) { if (!AL.isCXX11Attribute() && !AL.isC2xAttribute()) continue; - if (AL.getKind() == ParsedAttr::UnknownAttribute) - Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) - << AL << AL.getRange(); - else { + if (AL.getKind() == ParsedAttr::UnknownAttribute) { + if (WarnOnUnknownAttr) + Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) + << AL << AL.getRange(); + } else { Diag(AL.getLoc(), DiagID) << AL; AL.setInvalid(); } Index: clang/include/clang/Parse/Parser.h =================================================================== --- clang/include/clang/Parse/Parser.h +++ clang/include/clang/Parse/Parser.h @@ -2633,8 +2633,12 @@ // Forbid C++11 and C2x attributes that appear on certain syntactic locations // which standard permits but we don't supported yet, for example, attributes // appertain to decl specifiers. + // For the most cases we don't want to warn on unknown attributes, but left + // them to later diagnoses. However, for a few cases like module declarations + // and module import declarations, we should do it. void ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID, - bool DiagnoseEmptyAttrs = false); + bool DiagnoseEmptyAttrs = false, + bool WarnOnUnknownAttr = false); /// Skip C++11 and C2x attributes and return the end location of the /// last one.
Index: clang/lib/Parse/Parser.cpp =================================================================== --- clang/lib/Parse/Parser.cpp +++ clang/lib/Parse/Parser.cpp @@ -2377,7 +2377,9 @@ // We don't support any module attributes yet; just parse them and diagnose. ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); - ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr); + ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr, + /*DiagnoseEmptyAttrs=*/false, + /*WarnOnUnknownAttr=*/true); ExpectAndConsumeSemi(diag::err_module_expected_semi); @@ -2444,7 +2446,9 @@ ParsedAttributes Attrs(AttrFactory); MaybeParseCXX11Attributes(Attrs); // We don't support any module import attributes yet. - ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr); + ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr, + /*DiagnoseEmptyAttrs=*/false, + /*WarnOnUnknownAttr=*/true); if (PP.hadModuleLoaderFatalFailure()) { // With a fatal failure in the module loader, we abort parsing. Index: clang/lib/Parse/ParseDecl.cpp =================================================================== --- clang/lib/Parse/ParseDecl.cpp +++ clang/lib/Parse/ParseDecl.cpp @@ -1658,7 +1658,8 @@ } void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID, - bool DiagnoseEmptyAttrs) { + bool DiagnoseEmptyAttrs, + bool WarnOnUnknownAttr) { if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) { // An attribute list has been parsed, but it was empty. @@ -1685,10 +1686,11 @@ for (const ParsedAttr &AL : Attrs) { if (!AL.isCXX11Attribute() && !AL.isC2xAttribute()) continue; - if (AL.getKind() == ParsedAttr::UnknownAttribute) - Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) - << AL << AL.getRange(); - else { + if (AL.getKind() == ParsedAttr::UnknownAttribute) { + if (WarnOnUnknownAttr) + Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) + << AL << AL.getRange(); + } else { Diag(AL.getLoc(), DiagID) << AL; AL.setInvalid(); } Index: clang/include/clang/Parse/Parser.h =================================================================== --- clang/include/clang/Parse/Parser.h +++ clang/include/clang/Parse/Parser.h @@ -2633,8 +2633,12 @@ // Forbid C++11 and C2x attributes that appear on certain syntactic locations // which standard permits but we don't supported yet, for example, attributes // appertain to decl specifiers. + // For the most cases we don't want to warn on unknown attributes, but left + // them to later diagnoses. However, for a few cases like module declarations + // and module import declarations, we should do it. void ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID, - bool DiagnoseEmptyAttrs = false); + bool DiagnoseEmptyAttrs = false, + bool WarnOnUnknownAttr = false); /// Skip C++11 and C2x attributes and return the end location of the /// last one.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits