[clang] 9e1eaff - [clang] Fix `gnu::init_priority` attribute handling for reserved values (#121577)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 4 09:07:44 PST 2025
Author: Iris
Date: 2025-03-04T12:07:40-05:00
New Revision: 9e1eaff95b3284ccec71fec70eb9e286c34974c4
URL: https://github.com/llvm/llvm-project/commit/9e1eaff95b3284ccec71fec70eb9e286c34974c4
DIFF: https://github.com/llvm/llvm-project/commit/9e1eaff95b3284ccec71fec70eb9e286c34974c4.diff
LOG: [clang] Fix `gnu::init_priority` attribute handling for reserved values (#121577)
- Added a new diagnostic group `InitPriorityReserved`
- Allow values within the range 0-100 of `init_priority` to be used
outside system library, but with a warning
- Updated relavant tests
Fixes #121108
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaDeclAttr.cpp
clang/test/SemaCXX/init-priority-attr.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 48d3eed04c823..37ea963bf337d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -147,6 +147,9 @@ related warnings within the method body.
``__attribute__((model("large")))`` on non-TLS globals in x86-64 compilations.
This forces the global to be considered small or large in regards to the
x86-64 code model, regardless of the code model specified for the compilation.
+- Clang now emits a warning ``-Wreserved-init-priority`` instead of a hard error
+ when ``__attribute__((init_priority(n)))`` is used with values of n in the
+ reserved range [0, 100]. The warning will be treated as an error by default.
- There is a new ``format_matches`` attribute to complement the existing
``format`` attribute. ``format_matches`` allows the compiler to verify that
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index d89648a8a2e83..0b121c04cd3c0 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -3343,6 +3343,9 @@ def err_attribute_argument_out_of_range : Error<
def err_init_priority_object_attr : Error<
"can only use 'init_priority' attribute on file-scope definitions "
"of objects of class type">;
+def warn_init_priority_reserved : Warning<
+ "requested 'init_priority' %0 is reserved for internal use">,
+ InGroup<DiagGroup<"init-priority-reserved">>, DefaultError;
def err_attribute_argument_out_of_bounds : Error<
"%0 attribute parameter %1 is out of bounds">;
def err_attribute_only_once_per_parameter : Error<
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 5785cf5eec3c5..1405ee5341dcf 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -3720,16 +3720,18 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
}
- // Only perform the priority check if the attribute is outside of a system
- // header. Values <= 100 are reserved for the implementation, and libc++
- // benefits from being able to specify values in that range.
- if ((prioritynum < 101 || prioritynum > 65535) &&
- !S.getSourceManager().isInSystemHeader(AL.getLoc())) {
+ if (prioritynum > 65535) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
- << E->getSourceRange() << AL << 101 << 65535;
+ << E->getSourceRange() << AL << 0 << 65535;
AL.setInvalid();
return;
}
+
+ // Values <= 100 are reserved for the implementation, and libc++
+ // benefits from being able to specify values in that range.
+ if (prioritynum < 101)
+ S.Diag(AL.getLoc(), diag::warn_init_priority_reserved)
+ << E->getSourceRange() << prioritynum;
D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
}
diff --git a/clang/test/SemaCXX/init-priority-attr.cpp b/clang/test/SemaCXX/init-priority-attr.cpp
index 8c0a17682bb02..8151bf7aecb95 100644
--- a/clang/test/SemaCXX/init-priority-attr.cpp
+++ b/clang/test/SemaCXX/init-priority-attr.cpp
@@ -1,5 +1,6 @@
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify %s
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -DSYSTEM -verify %s
+// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -DNOERROR -Wno-error=init-priority-reserved -verify %s
// RUN: %clang_cc1 -triple=s390x-none-zos -fsyntax-only -verify=unknown %s
// RUN: %clang_cc1 -triple=s390x-none-zos -fsyntax-only -DSYSTEM -verify=unknown-system %s
@@ -24,24 +25,32 @@ extern Two goo;
extern Two coo[];
extern Two koo[];
+// unknown-system-no-diagnostics
+
Two foo __attribute__((init_priority(101))) ( 5, 6 );
- // unknown-system-no-diagnostics
- // unknown-warning at -2 {{unknown attribute 'init_priority' ignored}}
+// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
+
+Two loo __attribute__((init_priority(65535))) ( 5, 6 );
+// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
Two goo __attribute__((init_priority(2,3))) ( 5, 6 ); // expected-error {{'init_priority' attribute takes one argument}}
// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
Two coo[2] __attribute__((init_priority(100)));
#if !defined(SYSTEM)
- // expected-error at -2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
- // unknown-warning at -3 {{unknown attribute 'init_priority' ignored}}
-#endif
+#if !defined(NOERROR)
+ // expected-error at -3 {{requested 'init_priority' 100 is reserved for internal use}}
+#else // defined(NOERROR)
+ // expected-warning at -5 {{requested 'init_priority' 100 is reserved for internal use}}
+#endif // !defined(NOERROR)
+ // unknown-warning at -7 {{unknown attribute 'init_priority' ignored}}
+#endif // !defined(SYSTEM)
-Two boo[2] __attribute__((init_priority(65536)));
-#if !defined(SYSTEM)
- // expected-error at -2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
- // unknown-warning at -3 {{unknown attribute 'init_priority' ignored}}
-#endif
+Two zoo[2] __attribute__((init_priority(-1))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
+// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
+
+Two boo[2] __attribute__((init_priority(65536))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
+// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
Two koo[4] __attribute__((init_priority(1.13))); // expected-error {{'init_priority' attribute requires an integer constant}}
// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
@@ -49,7 +58,6 @@ Two koo[4] __attribute__((init_priority(1.13))); // expected-error {{'init_prio
Two func() __attribute__((init_priority(1001))); // expected-error {{'init_priority' attribute only applies to variables}}
// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
-
int i __attribute__((init_priority(1001))); // expected-error {{can only use 'init_priority' attribute on file-scope definitions of objects of class type}}
// unknown-warning at -1 {{unknown attribute 'init_priority' ignored}}
More information about the cfe-commits
mailing list