aboutsummaryrefslogtreecommitdiff
path: root/clang/test/Sema/constructor-attribute.cpp
blob: 81e9cb3141f1e459dfff52ee8fc5dac55365e0c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify -Wno-strict-prototypes %s
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wno-strict-prototypes %s

int x __attribute__((constructor)); // expected-warning {{'constructor' attribute only applies to functions}}
int f(void) __attribute__((constructor));
int f(void) __attribute__((constructor(1)));
int f(void) __attribute__((constructor(1,2))); // expected-error {{'constructor' attribute takes no more than 1 argument}}
int f(void) __attribute__((constructor(1.0))); // expected-error {{'constructor' attribute requires an integer constant}}
int f(void) __attribute__((constructor(0x100000000))); // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
void knr() __attribute__((constructor));

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

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}} 
template void f<1LL<<32>(); // expected-note {{in instantiation of function template specialization 'f<4294967296LL>' requested here}}
template void f<101>();

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}} 
template void f<long long>(); // expected-note {{in instantiation of function template specialization 'f<long long>' requested here}}
template void f<int>();

template <typename T>
[[gnu::constructor(static_cast<T>(101))]] void g() {}
template void g<int>();
template void g<long long>();

template <typename T>
[[gnu::constructor(static_cast<T>(T{101}))]] void h() {}
template void h<int>();
template void h<long long>();

template <typename T>
[[gnu::constructor(static_cast<T>(sizeof(T[101])))]] void a() {}
template void a<int>();
template void a<long long>();
#endif

int yd __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}}
int fd(void) __attribute__((destructor));
int fd(void) __attribute__((destructor(1)));
int fd(void) __attribute__((destructor(1,2))); // expected-error {{'destructor' attribute takes no more than 1 argument}}
int fd(void) __attribute__((destructor(1.0))); // expected-error {{'destructor' attribute requires an integer constant}}

#ifdef __cplusplus
template <float P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
template <double P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}
template <int *P> [[gnu::destructor(P)]] void fd(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}

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}} 
template void fd<1LL<<32>(); // expected-note {{in instantiation of function template specialization 'fd<4294967296LL>' requested here}}
template void fd<101>();

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}} 
template void fd<long long>(); // expected-note {{in instantiation of function template specialization 'fd<long long>' requested here}}
template void fd<int>();

template <typename T>
[[gnu::destructor(static_cast<T>(101))]] void gd() {}
template void gd<int>();
template void gd<long long>();

template <typename T>
[[gnu::destructor(static_cast<T>(T{101}))]] void hd() {}
template void hd<int>();
template void hd<long long>();

template <typename T>
[[gnu::destructor(static_cast<T>(sizeof(T[101])))]] void ad() {}
template void ad<int>();
template void ad<long long>();
#endif