Skip to content

Commit 9e1eaff

Browse files
authored
[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
1 parent cd3acd1 commit 9e1eaff

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ related warnings within the method body.
147147
``__attribute__((model("large")))`` on non-TLS globals in x86-64 compilations.
148148
This forces the global to be considered small or large in regards to the
149149
x86-64 code model, regardless of the code model specified for the compilation.
150+
- Clang now emits a warning ``-Wreserved-init-priority`` instead of a hard error
151+
when ``__attribute__((init_priority(n)))`` is used with values of n in the
152+
reserved range [0, 100]. The warning will be treated as an error by default.
150153

151154
- There is a new ``format_matches`` attribute to complement the existing
152155
``format`` attribute. ``format_matches`` allows the compiler to verify that

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3343,6 +3343,9 @@ def err_attribute_argument_out_of_range : Error<
33433343
def err_init_priority_object_attr : Error<
33443344
"can only use 'init_priority' attribute on file-scope definitions "
33453345
"of objects of class type">;
3346+
def warn_init_priority_reserved : Warning<
3347+
"requested 'init_priority' %0 is reserved for internal use">,
3348+
InGroup<DiagGroup<"init-priority-reserved">>, DefaultError;
33463349
def err_attribute_argument_out_of_bounds : Error<
33473350
"%0 attribute parameter %1 is out of bounds">;
33483351
def err_attribute_only_once_per_parameter : Error<

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3720,16 +3720,18 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
37203720
return;
37213721
}
37223722

3723-
// Only perform the priority check if the attribute is outside of a system
3724-
// header. Values <= 100 are reserved for the implementation, and libc++
3725-
// benefits from being able to specify values in that range.
3726-
if ((prioritynum < 101 || prioritynum > 65535) &&
3727-
!S.getSourceManager().isInSystemHeader(AL.getLoc())) {
3723+
if (prioritynum > 65535) {
37283724
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
3729-
<< E->getSourceRange() << AL << 101 << 65535;
3725+
<< E->getSourceRange() << AL << 0 << 65535;
37303726
AL.setInvalid();
37313727
return;
37323728
}
3729+
3730+
// Values <= 100 are reserved for the implementation, and libc++
3731+
// benefits from being able to specify values in that range.
3732+
if (prioritynum < 101)
3733+
S.Diag(AL.getLoc(), diag::warn_init_priority_reserved)
3734+
<< E->getSourceRange() << prioritynum;
37333735
D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
37343736
}
37353737

clang/test/SemaCXX/init-priority-attr.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -verify %s
22
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -DSYSTEM -verify %s
3+
// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -fsyntax-only -DNOERROR -Wno-error=init-priority-reserved -verify %s
34
// RUN: %clang_cc1 -triple=s390x-none-zos -fsyntax-only -verify=unknown %s
45
// RUN: %clang_cc1 -triple=s390x-none-zos -fsyntax-only -DSYSTEM -verify=unknown-system %s
56

@@ -24,32 +25,39 @@ extern Two goo;
2425
extern Two coo[];
2526
extern Two koo[];
2627

28+
// unknown-system-no-diagnostics
29+
2730
Two foo __attribute__((init_priority(101))) ( 5, 6 );
28-
// unknown-system-no-diagnostics
29-
// unknown-warning@-2 {{unknown attribute 'init_priority' ignored}}
31+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
32+
33+
Two loo __attribute__((init_priority(65535))) ( 5, 6 );
34+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
3035

3136
Two goo __attribute__((init_priority(2,3))) ( 5, 6 ); // expected-error {{'init_priority' attribute takes one argument}}
3237
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
3338

3439
Two coo[2] __attribute__((init_priority(100)));
3540
#if !defined(SYSTEM)
36-
// expected-error@-2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
37-
// unknown-warning@-3 {{unknown attribute 'init_priority' ignored}}
38-
#endif
41+
#if !defined(NOERROR)
42+
// expected-error@-3 {{requested 'init_priority' 100 is reserved for internal use}}
43+
#else // defined(NOERROR)
44+
// expected-warning@-5 {{requested 'init_priority' 100 is reserved for internal use}}
45+
#endif // !defined(NOERROR)
46+
// unknown-warning@-7 {{unknown attribute 'init_priority' ignored}}
47+
#endif // !defined(SYSTEM)
3948

40-
Two boo[2] __attribute__((init_priority(65536)));
41-
#if !defined(SYSTEM)
42-
// expected-error@-2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
43-
// unknown-warning@-3 {{unknown attribute 'init_priority' ignored}}
44-
#endif
49+
Two zoo[2] __attribute__((init_priority(-1))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
50+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
51+
52+
Two boo[2] __attribute__((init_priority(65536))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
53+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
4554

4655
Two koo[4] __attribute__((init_priority(1.13))); // expected-error {{'init_priority' attribute requires an integer constant}}
4756
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
4857

4958
Two func() __attribute__((init_priority(1001))); // expected-error {{'init_priority' attribute only applies to variables}}
5059
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
5160

52-
5361
int i __attribute__((init_priority(1001))); // expected-error {{can only use 'init_priority' attribute on file-scope definitions of objects of class type}}
5462
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
5563

0 commit comments

Comments
 (0)