Skip to content

Commit f376279

Browse files
committed
Address PR comments.
1 parent 184f51a commit f376279

File tree

3 files changed

+54
-41
lines changed

3 files changed

+54
-41
lines changed

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,24 +2156,24 @@ static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
21562156
D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
21572157
}
21582158

2159-
static std::optional<Expr *>
2160-
sharedGetConstructorDestructorAttrExpr(Sema &S, const ParsedAttr &AL) {
2161-
// If no Expr node exists on the attribute, return a nullptr (default priority
2162-
// to be used). If Expr node exists but is not valid, return a nullopt.
2163-
// Otherwise, return the Expr.
2159+
static ExprResult sharedGetConstructorDestructorAttrExpr(Sema &S,
2160+
const ParsedAttr &AL) {
2161+
// If no Expr node exists on the attribute, return a nullptr result (default
2162+
// priority to be used). If Expr node exists but is not valid, return an
2163+
// invalid result. Otherwise, return the Expr.
21642164
Expr *E = nullptr;
21652165
if (AL.getNumArgs() == 1) {
21662166
E = AL.getArgAsExpr(0);
21672167
if (E->isValueDependent()) {
21682168
if (!E->isTypeDependent() && !E->getType()->isIntegerType()) {
21692169
S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
21702170
<< AL << AANT_ArgumentIntegerConstant << E->getSourceRange();
2171-
return std::nullopt;
2171+
return ExprResult(/*Invalid=*/true);
21722172
}
21732173
} else {
21742174
uint32_t priority;
21752175
if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority)) {
2176-
return std::nullopt;
2176+
return ExprResult(/*Invalid=*/true);
21772177
}
21782178
return ConstantExpr::Create(S.Context, E,
21792179
APValue(llvm::APSInt::getUnsigned(priority)));
@@ -2187,20 +2187,20 @@ static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
21872187
S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
21882188
return;
21892189
}
2190-
auto E = sharedGetConstructorDestructorAttrExpr(S, AL);
2191-
if (!E.has_value())
2190+
ExprResult E = sharedGetConstructorDestructorAttrExpr(S, AL);
2191+
if (!E.isUsable())
21922192
return;
21932193
S.Diag(D->getLocation(), diag::warn_global_constructor)
21942194
<< D->getSourceRange();
2195-
D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, E.value()));
2195+
D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, E.get()));
21962196
}
21972197

21982198
static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
2199-
auto E = sharedGetConstructorDestructorAttrExpr(S, AL);
2200-
if (!E.has_value())
2199+
ExprResult E = sharedGetConstructorDestructorAttrExpr(S, AL);
2200+
if (!E.isUsable())
22012201
return;
22022202
S.Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange();
2203-
D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, E.value()));
2203+
D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, E.get()));
22042204
}
22052205

22062206
template <typename AttrTy>

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,13 @@ static void sharedInstantiateConstructorDestructorAttr(
245245
if (Result.isInvalid())
246246
return;
247247
tempInstPriority = Result.get();
248-
if (auto CE = tempInstPriority->getIntegerConstantExpr(C)) {
248+
if (std::optional<llvm::APSInt> CE =
249+
tempInstPriority->getIntegerConstantExpr(C)) {
249250
// Consistent with non-templated priority arguments, which must fit in a
250251
// 32-bit unsigned integer.
251252
if (!CE->isIntN(32)) {
252253
S.Diag(tempInstPriority->getExprLoc(), diag::err_ice_too_large)
253-
<< toString(*CE, 10, false) << 32 << 1;
254+
<< toString(*CE, 10, false) << /*Size=*/32 << /*Unsigned=*/1;
254255
return;
255256
}
256257
}
Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -Wno-strict-prototypes %s
1+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify -Wno-strict-prototypes %s
22
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wno-strict-prototypes %s
33

44
int x __attribute__((constructor)); // expected-warning {{'constructor' attribute only applies to functions}}
@@ -10,53 +10,65 @@ int f(void) __attribute__((constructor(0x100000000))); // expected-error {{integ
1010
void knr() __attribute__((constructor));
1111

1212
#ifdef __cplusplus
13-
template <int *P>
14-
[[gnu::constructor(P)]] void f(); // expected-error {{'gnu::constructor' attribute requires an integer constant}}
13+
template <float P> [[gnu::constructor(P)]] void f(); // expected-error {{'gnu::constructor' attribute requires an integer constant}}
14+
template <double P> [[gnu::constructor(P)]] void f(); // expected-error {{'gnu::constructor' attribute requires an integer constant}}
15+
template <int *P> [[gnu::constructor(P)]] void f(); // expected-error {{'gnu::constructor' attribute requires an integer constant}}
1516

16-
template <long long P>
17-
[[gnu::constructor(P)]] void f() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
17+
template <long long P> [[gnu::constructor(P)]] void f() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
1818
template void f<1LL<<32>(); // expected-note {{in instantiation of function template specialization 'f<4294967296LL>' requested here}}
1919
template void f<101>();
2020

21-
template <typename T>
22-
[[gnu::constructor(static_cast<T>(101))]] void f() {}
21+
template <typename T> [[gnu::constructor(static_cast<T>(1LL<<32))]] void f() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
22+
template void f<long long>(); // expected-note {{in instantiation of function template specialization 'f<long long>' requested here}}
2323
template void f<int>();
24-
template void f<long long>();
2524

2625
template <typename T>
27-
[[gnu::constructor(static_cast<T>(1LL<<32))]] void g() {}
26+
[[gnu::constructor(static_cast<T>(101))]] void g() {}
2827
template void g<int>();
28+
template void g<long long>();
29+
30+
template <typename T>
31+
[[gnu::constructor(static_cast<T>(T{101}))]] void h() {}
32+
template void h<int>();
33+
template void h<long long>();
2934

3035
template <typename T>
31-
[[gnu::constructor(static_cast<T>(1LL<<32))]] void h() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
32-
template void h<long long>(); // expected-note {{in instantiation of function template specialization 'h<long long>' requested here}}
36+
[[gnu::constructor(static_cast<T>(sizeof(T[101])))]] void a() {}
37+
template void a<int>();
38+
template void a<long long>();
3339
#endif
3440

35-
int y __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}}
36-
int f(void) __attribute__((destructor));
37-
int f(void) __attribute__((destructor(1)));
38-
int f(void) __attribute__((destructor(1,2))); // expected-error {{'destructor' attribute takes no more than 1 argument}}
39-
int f(void) __attribute__((destructor(1.0))); // expected-error {{'destructor' attribute requires an integer constant}}
41+
int yd __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}}
42+
int fd(void) __attribute__((destructor));
43+
int fd(void) __attribute__((destructor(1)));
44+
int fd(void) __attribute__((destructor(1,2))); // expected-error {{'destructor' attribute takes no more than 1 argument}}
45+
int fd(void) __attribute__((destructor(1.0))); // expected-error {{'destructor' attribute requires an integer constant}}
4046

4147
#ifdef __cplusplus
42-
template <int *I>
43-
[[gnu::destructor(I)]] void f(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
48+
template <float P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
49+
template <double P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
50+
template <int *P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
4451

45-
template <long long P>
46-
[[gnu::destructor(P)]] void fd() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
52+
template <long long P> [[gnu::destructor(P)]] void fd() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
4753
template void fd<1LL<<32>(); // expected-note {{in instantiation of function template specialization 'fd<4294967296LL>' requested here}}
4854
template void fd<101>();
4955

50-
template <typename T>
51-
[[gnu::destructor(static_cast<T>(101))]] void fd() {}
56+
template <typename T> [[gnu::destructor(static_cast<T>(1LL<<32))]] void fd() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
57+
template void fd<long long>(); // expected-note {{in instantiation of function template specialization 'fd<long long>' requested here}}
5258
template void fd<int>();
53-
template void fd<long long>();
5459

5560
template <typename T>
56-
[[gnu::destructor(static_cast<T>(1LL<<32))]] void gd() {}
61+
[[gnu::destructor(static_cast<T>(101))]] void gd() {}
5762
template void gd<int>();
63+
template void gd<long long>();
64+
65+
template <typename T>
66+
[[gnu::destructor(static_cast<T>(T{101}))]] void hd() {}
67+
template void hd<int>();
68+
template void hd<long long>();
5869

5970
template <typename T>
60-
[[gnu::destructor(static_cast<T>(1LL<<32))]] void hd() {} // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
61-
template void hd<long long>(); // expected-note {{in instantiation of function template specialization 'hd<long long>' requested here}}
71+
[[gnu::destructor(static_cast<T>(sizeof(T[101])))]] void ad() {}
72+
template void ad<int>();
73+
template void ad<long long>();
6274
#endif

0 commit comments

Comments
 (0)