-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[Clang] raise extension warning for unknown namespaced attributes #120925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-modules Author: Oleksandr T. (a-tarasyuk) ChangesFixes #120875 Full diff: https://github.com/llvm/llvm-project/pull/120925.diff 12 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6b9e1109f3906e..e2cf90aecf3666 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -552,6 +552,8 @@ Attribute Changes in Clang
- Clang now permits the usage of the placement new operator in ``[[msvc::constexpr]]``
context outside of the std namespace. (#GH74924)
+- Clang now raises warnings for unknown namespaced attributes only in pedantic mode (#GH120875).
+
Improvements to Clang's diagnostics
-----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index f4a155bb00bb37..85653429aa5332 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -179,6 +179,8 @@ def err_opencl_unknown_type_specifier : Error<
def warn_unknown_attribute_ignored : Warning<
"unknown attribute %0 ignored">, InGroup<UnknownAttributes>;
+def ext_unknown_attribute_ignored : Extension<
+ "unknown attribute %0 ignored">, InGroup<UnknownAttributes>;
def warn_attribute_ignored : Warning<"%0 attribute ignored">,
InGroup<IgnoredAttributes>;
def err_keyword_not_supported_on_target : Error<
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index bb4d33560b93b8..9b1ffebe0804a5 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -6548,6 +6548,8 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
? (unsigned)diag::err_keyword_not_supported_on_target
: AL.isDeclspecAttribute()
? (unsigned)diag::warn_unhandled_ms_attribute_ignored
+ : AL.getScopeName()
+ ? (unsigned)diag::ext_unknown_attribute_ignored
: (unsigned)diag::warn_unknown_attribute_ignored)
<< AL << AL.getRange();
return;
diff --git a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.grammar/p2-1z.cpp b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.grammar/p2-1z.cpp
index 192fa126109873..c7e66649fb7b22 100644
--- a/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.grammar/p2-1z.cpp
+++ b/clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.grammar/p2-1z.cpp
@@ -12,5 +12,5 @@
[[using clang:]] extern int n; // ok
[[using blah: clang::optnone]] extern int n; // expected-error {{attribute with scope specifier cannot follow}} expected-warning {{only applies to functions}}
-[[using clang: unknown_attr]] extern int n; // expected-warning {{unknown attribute}}
-[[using unknown_ns: something]] extern int n; // expected-warning {{unknown attribute}}
+[[using clang: unknown_attr]] extern int n;
+[[using unknown_ns: something]] extern int n;
diff --git a/clang/test/CXX/module/module.interface/p3.cpp b/clang/test/CXX/module/module.interface/p3.cpp
index 32819b2dccb11d..3cde92c1a2cf34 100644
--- a/clang/test/CXX/module/module.interface/p3.cpp
+++ b/clang/test/CXX/module/module.interface/p3.cpp
@@ -40,7 +40,7 @@ export { // No diagnostic after P2615R1 DR
extern "C++" {} // No diagnostic after P2615R1 DR
}
export [[]]; // No diagnostic after P2615R1 DR
-export [[example::attr]]; // expected-warning {{unknown attribute 'attr'}}
+export [[example::attr]]; // expected-error {{unknown attribute 'attr'}}
// [...] shall not declare a name with internal linkage
export static int a; // expected-error {{declaration of 'a' with internal linkage cannot be exported}}
diff --git a/clang/test/Lexer/cxx2a-spaceship.cpp b/clang/test/Lexer/cxx2a-spaceship.cpp
index 2163a0bf190f90..505f2f47c8ffb8 100644
--- a/clang/test/Lexer/cxx2a-spaceship.cpp
+++ b/clang/test/Lexer/cxx2a-spaceship.cpp
@@ -61,7 +61,7 @@ static_assert(__builtin_strcmp(b, "<=>") == 0);
// CXX20: preprocess8: <=>=
#define ID(x) x
-[[some_vendor::some_attribute( // expected-warning {{unknown attribute}}
+[[some_vendor::some_attribute(
preprocess1: ID(<)ID(=>),
preprocess2: ID(<=)ID(>),
preprocess3: ID(<)ID(=)ID(>),
diff --git a/clang/test/OpenMP/openmp_attribute_parsing.cpp b/clang/test/OpenMP/openmp_attribute_parsing.cpp
index e273702dfadcb3..4a3a885e407a75 100644
--- a/clang/test/OpenMP/openmp_attribute_parsing.cpp
+++ b/clang/test/OpenMP/openmp_attribute_parsing.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++17 -fopenmp -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++17 -Wunknown-attributes -fopenmp -fsyntax-only -verify %s
// This file tests the custom parsing logic for the OpenMP 5.1 attribute
// syntax. It does not test actual OpenMP directive syntax, just the attribute
diff --git a/clang/test/Parser/c2x-attributes.c b/clang/test/Parser/c2x-attributes.c
index be039e40f98ef1..30653e2e6f67a6 100644
--- a/clang/test/Parser/c2x-attributes.c
+++ b/clang/test/Parser/c2x-attributes.c
@@ -133,7 +133,7 @@ void f11(void) {
}
[[attr]] void f12(void); // expected-warning {{unknown attribute 'attr' ignored}}
-[[vendor::attr]] void f13(void); // expected-warning {{unknown attribute 'attr' ignored}}
+[[vendor::attr]] void f13(void);
// Ensure that asm statements properly handle double colons.
void test_asm(void) {
diff --git a/clang/test/Parser/cxx0x-attributes.cpp b/clang/test/Parser/cxx0x-attributes.cpp
index fad3010c98b9c2..41e4ac4907ef54 100644
--- a/clang/test/Parser/cxx0x-attributes.cpp
+++ b/clang/test/Parser/cxx0x-attributes.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fcxx-exceptions -fdeclspec -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++17-extensions %s
+// RUN: %clang_cc1 -fcxx-exceptions -fdeclspec -fexceptions -fsyntax-only -verify -std=c++11 -Wc++14-compat -Wc++14-extensions -Wc++17-extensions -Wunknown-attributes %s
// Need std::initializer_list
namespace std {
diff --git a/clang/test/Sema/patchable-function-entry-attr.cpp b/clang/test/Sema/patchable-function-entry-attr.cpp
index bd4d57a7e30936..8c988b453749e9 100644
--- a/clang/test/Sema/patchable-function-entry-attr.cpp
+++ b/clang/test/Sema/patchable-function-entry-attr.cpp
@@ -8,7 +8,7 @@
// RUN: %clang_cc1 -triple riscv64 -fsyntax-only -verify=silence %s
// RUN: %clang_cc1 -triple powerpc-unknown-linux-gnu -fsyntax-only -verify=silence %s
// RUN: %clang_cc1 -triple powerpc64-unknown-linux-gnu -fsyntax-only -verify=silence %s
-// RUN: %clang_cc1 -triple ppc64le -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple ppc64le -fsyntax-only -verify=silence %s
// RUN: %clang_cc1 -triple powerpc64-ibm-aix-xcoff -fsyntax-only -verify=AIX %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix-xcoff -fsyntax-only -verify=AIX %s
diff --git a/clang/test/Sema/unknown-attributes.c b/clang/test/Sema/unknown-attributes.c
new file mode 100644
index 00000000000000..26fa0258302a38
--- /dev/null
+++ b/clang/test/Sema/unknown-attributes.c
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c23 -verify=pedantic -pedantic %s
+// RUN: %clang_cc1 -fsyntax-only -std=c23 -verify %s
+
+// expected-no-diagnostics
+
+[[unknown::a(b((c)) d(e((f)))), unknown::g(h k)]] // pedantic-warning {{unknown attribute 'a' ignored}} \
+ // pedantic-warning {{unknown attribute 'g' ignored}}
+int main(void) {
+ return 0;
+}
diff --git a/clang/test/SemaCXX/cxx2a-ms-no-unique-address.cpp b/clang/test/SemaCXX/cxx2a-ms-no-unique-address.cpp
index 822ed752fa9c75..90c814a6a7774a 100644
--- a/clang/test/SemaCXX/cxx2a-ms-no-unique-address.cpp
+++ b/clang/test/SemaCXX/cxx2a-ms-no-unique-address.cpp
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -std=c++2a %s -verify=unsupported -triple x86_64-linux-gnu
-// RUN: %clang_cc1 -std=c++2a %s -verify -triple x86_64-windows -fms-compatibility
+// RUN: %clang_cc1 -std=c++2a %s -verify=unsupported -triple x86_64-linux-gnu -Wunknown-attributes
+// RUN: %clang_cc1 -std=c++2a %s -verify -triple x86_64-windows -fms-compatibility -Wunknown-attributes
[[msvc::no_unique_address]] int a; // expected-error {{only applies to non-bit-field non-static data members}} unsupported-warning {{unknown}}
[[msvc::no_unique_address]] void f(); // expected-error {{only applies to non-bit-field non-static data members}} unsupported-warning {{unknown}}
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
Hi, will it be possible to add a separate warning |
@namandixit it is possible. I'm not confident about the new option., @AaronBallman @erichkeane - should unknown namespaced attributes be separated by the new option? |
Thanks for this patch. Sorry I did not see this sooner. I am not convinced this the best way to resolve #120875 - and I suspect in practice it would be a bit difficult to use/be under used. I think we should definitely display the namespace name in the current diagnostic, but for the opt-out mechanism, i think we should have a better sense of a solution that covers all the use cases and then implement that. Note that I haven't chatted to @AaronBallman and @erichkeane yet, but I think that's worth discussing before proceeding. P.S: Please offer a description in your non-trivial changes, explaining your design choices and so forth. thanks a lot |
I'm not convinced here? I think a flag that says "warn me about unknown namespaces" is particularly useful, but the effect of "don't warn me about unknown namespaces, but warn me about attributes in known namespaces" IS also particularly useful. (that is, if you don't know the namespace, shush, else tell me about it).
If anything, a list on a command line as an extension to this (that is, don't warn me about THIS LIST of unknown namespaces) would be incredibly useful, something like:
THAT is the downside unfortunately to our diagnostics system in general, and I'm particularly upset in this case that we don't have a way of differentiating between 'typo' and 'intentionally ignored. Which is why the flag you propose I believe is a REALLY good idea, but I'm hesitent to make it required in this patch.
|
+1
That's how this already works in this patch:
I think what Corentin is asking for is a similar (follow-up?) feature that allows you to say "warn me about unknown attributes, but not of unknown attributes." that can take attributes with or without a namespace. Btw, I just realized we only have tests for
I'm in total support of that, though not as part of this PR. |
I'm not hearing a difference? |
Wow. My fingers are obviously not listening to my brain very well. :-D I mean "warn me about unknown attribute, but not these particular unknown attributes." e.g., (fake warning group name)
|
Ah, hrm. I see much less value in that. How likely is it that you have an attribute in a 'known' namespace that is 'unknown' but valid? I thought the whole point of namespaces was that this is NOT particularly useful? |
For example, we know about the |
Sure, but we've never needed/wanted that before for general attributes, right? I am seeing THAT as particularly novel for this patch. I would be open to reviewing that, but don't see it as necessary for this patch. |
Ultimately we want
I sort of question the last bullet point (the original issue), as it's a bit of a nuclear option. But maybe that's fine. I will also point out that And again, I apologize I did not see this patch sooner @a-tarasyuk. |
Yeah, I was thinking about the converse:
I think it makes sense to not land this for Clang 20 so we can get the interface right the first time. Let's see if we have some agreement on where we're at.
Does that seem like an accurate summary? |
Hi, original issue submitter here. Personally, just being able to silence warnings about specific unknown prefixes is fine, the "nuclear" option is not necessary. The reason behind the issue was that I am annotating type definitions, etc. with custom attributes so that I can generate meta-programming information using my own tools, and I want Clang to not warn about those attributes. Being able to prevent warnings about a few specific prefixes will suffice, if the so called "nuclear" option is undesirable. |
@erichkeane @AaronBallman @cor3ntin, just checking if there are any updates on this, as the |
Erich and I discussed this more offline and our thinking is:
|
Fixes #120875
This patch improves Clang's diagnostics for attributes by: