Skip to content

Commit

Permalink
[Clang] Fix unknown type attributes diagnosed twice with [[]] spelling
Browse files Browse the repository at this point in the history
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 #54817

Differential Revision: https://reviews.llvm.org/D123447
  • Loading branch information
junaire committed Apr 12, 2022
1 parent e810d55 commit f9c2f82
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -117,6 +117,8 @@ Bug Fixes
`C++20 [dcl.fct.def.general]p2 <https://timsong-cpp.github.io/cppwp/n4868/dcl.fct.def#general-2.sentence-3>`_,
Clang should not diagnose incomplete types in function definitions if the function body is "= delete;".
This fixes Issue `Issue 52802 <https://github.com/llvm/llvm-project/issues/52802>`_.
- Unknown type attributes with a ``[[]]`` spelling are no longer diagnosed twice.
This fixes Issue `Issue 54817 <https://github.com/llvm/llvm-project/issues/54817>`_.

Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion clang/include/clang/Parse/Parser.h
Expand Up @@ -2633,8 +2633,12 @@ class Parser : public CodeCompletionHandler {
// 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 type 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 WarnOnUnknownAttrs = false);

/// Skip C++11 and C2x attributes and return the end location of the
/// last one.
Expand Down
12 changes: 7 additions & 5 deletions clang/lib/Parse/ParseDecl.cpp
Expand Up @@ -1658,7 +1658,8 @@ void Parser::DiagnoseProhibitedAttributes(
}

void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
bool DiagnoseEmptyAttrs) {
bool DiagnoseEmptyAttrs,
bool WarnOnUnknownAttrs) {

if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
// An attribute list has been parsed, but it was empty.
Expand All @@ -1685,10 +1686,11 @@ void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
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 (WarnOnUnknownAttrs)
Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
<< AL << AL.getRange();
} else {
Diag(AL.getLoc(), DiagID) << AL;
AL.setInvalid();
}
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/Parse/Parser.cpp
Expand Up @@ -2386,7 +2386,9 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
// 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,
/*WarnOnUnknownAttrs=*/true);

ExpectAndConsumeSemi(diag::err_module_expected_semi);

Expand Down Expand Up @@ -2453,7 +2455,9 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
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,
/*WarnOnUnknownAttrs=*/true);

if (PP.hadModuleLoaderFatalFailure()) {
// With a fatal failure in the module loader, we abort parsing.
Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/warn-once-on-unknown-attr.cpp
@@ -0,0 +1,11 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
// RUN: %clang_cc1 -fsyntax-only -verify -std=c2x -x c %s
void foo() {
int [[attr]] i; // expected-warning {{unknown attribute 'attr' ignored}}
(void)sizeof(int [[attr]]); // expected-warning {{unknown attribute 'attr' ignored}}
}

void bar() {
[[attr]]; // expected-warning {{unknown attribute 'attr' ignored}}
[[attr]] int i; // expected-warning {{unknown attribute 'attr' ignored}}
}

0 comments on commit f9c2f82

Please sign in to comment.