Skip to content

Commit 5551c17

Browse files
committed
fix gnu::init_priority behavior
1 parent 56e944b commit 5551c17

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ def PrivateModule : DiagGroup<"private-module">;
510510
def CXX11InlineNamespace : DiagGroup<"c++11-inline-namespace">;
511511
def InlineNamespaceReopenedNoninline
512512
: DiagGroup<"inline-namespace-reopened-noninline">;
513+
def InitPriorityReserved : DiagGroup<"init-priority-reserved">;
513514
def InvalidNoreturn : DiagGroup<"invalid-noreturn">;
514515
def InvalidSourceEncoding : DiagGroup<"invalid-source-encoding">;
515516
def KNRPromotedParameter : DiagGroup<"knr-promoted-parameter">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3324,6 +3324,9 @@ def err_attribute_argument_out_of_range : Error<
33243324
def err_init_priority_object_attr : Error<
33253325
"can only use 'init_priority' attribute on file-scope definitions "
33263326
"of objects of class type">;
3327+
def warn_init_priority_reserved : Warning<
3328+
"requested 'init_priority' %0 is reserved for internal use">,
3329+
InGroup<InitPriorityReserved>;
33273330
def err_attribute_argument_out_of_bounds : Error<
33283331
"%0 attribute parameter %1 is out of bounds">;
33293332
def err_attribute_only_once_per_parameter : Error<

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,15 +3591,20 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
35913591
return;
35923592
}
35933593

3594+
if (prioritynum < 0 || prioritynum > 65535) {
3595+
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
3596+
<< E->getSourceRange() << AL << 0 << 65535;
3597+
AL.setInvalid();
3598+
return;
3599+
}
3600+
35943601
// Only perform the priority check if the attribute is outside of a system
35953602
// header. Values <= 100 are reserved for the implementation, and libc++
35963603
// benefits from being able to specify values in that range.
3597-
if ((prioritynum < 101 || prioritynum > 65535) &&
3604+
if (prioritynum < 101 &&
35983605
!S.getSourceManager().isInSystemHeader(AL.getLoc())) {
3599-
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
3600-
<< E->getSourceRange() << AL << 101 << 65535;
3601-
AL.setInvalid();
3602-
return;
3606+
S.Diag(AL.getLoc(), diag::warn_init_priority_reserved)
3607+
<< E->getSourceRange() << prioritynum;
36033608
}
36043609
D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
36053610
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@ Two goo __attribute__((init_priority(2,3))) ( 5, 6 ); // expected-error {{'init_
3333

3434
Two coo[2] __attribute__((init_priority(100)));
3535
#if !defined(SYSTEM)
36-
// expected-error@-2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
36+
// expected-warning@-2 {{requested 'init_priority' 100 is reserved for internal use}}
3737
// unknown-warning@-3 {{unknown attribute 'init_priority' ignored}}
3838
#endif
3939

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
40+
Two zoo[2] __attribute__((init_priority(-1))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
41+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
42+
43+
Two boo[2] __attribute__((init_priority(65536))); // expected-error {{'init_priority' attribute requires integer constant between 0 and 65535 inclusive}}
44+
// unknown-warning@-1 {{unknown attribute 'init_priority' ignored}}
4545

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

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

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

0 commit comments

Comments
 (0)