Skip to content

Commit f9c2f82

Browse files
committed
[Clang] Fix unknown type attributes diagnosed twice with [[]] spelling
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 llvm#54817 Differential Revision: https://reviews.llvm.org/D123447
1 parent e810d55 commit f9c2f82

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ Bug Fixes
117117
`C++20 [dcl.fct.def.general]p2 <https://timsong-cpp.github.io/cppwp/n4868/dcl.fct.def#general-2.sentence-3>`_,
118118
Clang should not diagnose incomplete types in function definitions if the function body is "= delete;".
119119
This fixes Issue `Issue 52802 <https://github.com/llvm/llvm-project/issues/52802>`_.
120+
- Unknown type attributes with a ``[[]]`` spelling are no longer diagnosed twice.
121+
This fixes Issue `Issue 54817 <https://github.com/llvm/llvm-project/issues/54817>`_.
120122

121123
Improvements to Clang's diagnostics
122124
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/Parse/Parser.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2633,8 +2633,12 @@ class Parser : public CodeCompletionHandler {
26332633
// Forbid C++11 and C2x attributes that appear on certain syntactic locations
26342634
// which standard permits but we don't supported yet, for example, attributes
26352635
// appertain to decl specifiers.
2636+
// For the most cases we don't want to warn on unknown type attributes, but
2637+
// left them to later diagnoses. However, for a few cases like module
2638+
// declarations and module import declarations, we should do it.
26362639
void ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
2637-
bool DiagnoseEmptyAttrs = false);
2640+
bool DiagnoseEmptyAttrs = false,
2641+
bool WarnOnUnknownAttrs = false);
26382642

26392643
/// Skip C++11 and C2x attributes and return the end location of the
26402644
/// last one.

clang/lib/Parse/ParseDecl.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,8 @@ void Parser::DiagnoseProhibitedAttributes(
16581658
}
16591659

16601660
void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
1661-
bool DiagnoseEmptyAttrs) {
1661+
bool DiagnoseEmptyAttrs,
1662+
bool WarnOnUnknownAttrs) {
16621663

16631664
if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
16641665
// An attribute list has been parsed, but it was empty.
@@ -1685,10 +1686,11 @@ void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
16851686
for (const ParsedAttr &AL : Attrs) {
16861687
if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
16871688
continue;
1688-
if (AL.getKind() == ParsedAttr::UnknownAttribute)
1689-
Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
1690-
<< AL << AL.getRange();
1691-
else {
1689+
if (AL.getKind() == ParsedAttr::UnknownAttribute) {
1690+
if (WarnOnUnknownAttrs)
1691+
Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
1692+
<< AL << AL.getRange();
1693+
} else {
16921694
Diag(AL.getLoc(), DiagID) << AL;
16931695
AL.setInvalid();
16941696
}

clang/lib/Parse/Parser.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,7 +2386,9 @@ Parser::ParseModuleDecl(Sema::ModuleImportState &ImportState) {
23862386
// We don't support any module attributes yet; just parse them and diagnose.
23872387
ParsedAttributes Attrs(AttrFactory);
23882388
MaybeParseCXX11Attributes(Attrs);
2389-
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr);
2389+
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_module_attr,
2390+
/*DiagnoseEmptyAttrs=*/false,
2391+
/*WarnOnUnknownAttrs=*/true);
23902392

23912393
ExpectAndConsumeSemi(diag::err_module_expected_semi);
23922394

@@ -2453,7 +2455,9 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24532455
ParsedAttributes Attrs(AttrFactory);
24542456
MaybeParseCXX11Attributes(Attrs);
24552457
// We don't support any module import attributes yet.
2456-
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr);
2458+
ProhibitCXX11Attributes(Attrs, diag::err_attribute_not_import_attr,
2459+
/*DiagnoseEmptyAttrs=*/false,
2460+
/*WarnOnUnknownAttrs=*/true);
24572461

24582462
if (PP.hadModuleLoaderFatalFailure()) {
24592463
// With a fatal failure in the module loader, we abort parsing.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++11
2+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c2x -x c %s
3+
void foo() {
4+
int [[attr]] i; // expected-warning {{unknown attribute 'attr' ignored}}
5+
(void)sizeof(int [[attr]]); // expected-warning {{unknown attribute 'attr' ignored}}
6+
}
7+
8+
void bar() {
9+
[[attr]]; // expected-warning {{unknown attribute 'attr' ignored}}
10+
[[attr]] int i; // expected-warning {{unknown attribute 'attr' ignored}}
11+
}

0 commit comments

Comments
 (0)