Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3324,6 +3324,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">,
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<
Expand Down
15 changes: 10 additions & 5 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3591,15 +3591,20 @@ static void handleInitPriorityAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
return;
}

if (prioritynum < 0 || prioritynum > 65535) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
<< E->getSourceRange() << AL << 0 << 65535;
AL.setInvalid();
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) &&
if (prioritynum < 101 &&
!S.getSourceManager().isInSystemHeader(AL.getLoc())) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_range)
<< E->getSourceRange() << AL << 101 << 65535;
AL.setInvalid();
return;
S.Diag(AL.getLoc(), diag::warn_init_priority_reserved)
<< E->getSourceRange() << prioritynum;
}
D->addAttr(::new (S.Context) InitPriorityAttr(S.Context, AL, prioritynum));
}
Expand Down
13 changes: 6 additions & 7 deletions clang/test/SemaCXX/init-priority-attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ Two goo __attribute__((init_priority(2,3))) ( 5, 6 ); // expected-error {{'init_

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

Two boo[2] __attribute__((init_priority(65536)));
#if !defined(SYSTEM)
// expected-error@-2 {{'init_priority' attribute requires integer constant between 101 and 65535 inclusive}}
// unknown-warning@-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@-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@-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@-1 {{unknown attribute 'init_priority' ignored}}

Two func() __attribute__((init_priority(1001))); // expected-error {{'init_priority' attribute only applies to variables}}
// unknown-warning@-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@-1 {{unknown attribute 'init_priority' ignored}}

Expand Down
Loading