1 // RUN: %clang_cc1 -fblocks -verify %s
4 * Test 'noderef' attribute with c++ constructs.
7 #define NODEREF __attribute__((noderef))
9 // Stub out types for 'typeid' to work.
15 int NODEREF i
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
16 int NODEREF
*i_ptr
; // expected-note 2 {{i_ptr declared here}}
17 int NODEREF
**i_ptr2
; // ok
18 int *NODEREF i_ptr3
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
19 int *NODEREF
*i_ptr4
; // ok
21 auto NODEREF
*auto_i_ptr
= i_ptr
;
22 auto NODEREF auto_i
= i
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
29 int __attribute__((noderef(10))) * no_args
; // expected-error{{'noderef' attribute takes no arguments}}
31 int i2
= *i_ptr
; // expected-warning{{dereferencing i_ptr; was declared with a 'noderef' type}}
32 int &i3
= *i_ptr
; // expected-warning{{dereferencing i_ptr; was declared with a 'noderef' type}}
33 int *i_ptr5
= i_ptr
; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
34 int *i_ptr6(i_ptr
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
37 const int NODEREF
*const_i_ptr
;
38 static int NODEREF
*static_i_ptr
;
41 int NODEREF(*i_ptr
); // ok (same as `int NODEREF *`)
42 int NODEREF
*(*i_ptr2
); // ok (same as `int NODEREF **`)
45 // Function declarations
46 int NODEREF
func(); // expected-warning{{'noderef' can only be used on an array or pointer type}}
47 int NODEREF
*func2(); // ok (returning pointer)
49 typedef int NODEREF (*func3
)(int); // expected-warning{{'noderef' can only be used on an array or pointer type}}
50 typedef int NODEREF
*(*func4
)(int);
53 int NODEREF i_arr
[10]; // ok
54 int NODEREF i_arr2
[10][10]; // ok
55 int NODEREF
*i_arr3
[10]; // ok
56 int NODEREF i_arr4
[] = {1, 2};
60 int NODEREF(i_ptr
[10]);
61 int NODEREF(i_ptr2
[10])[10];
64 typedef int NODEREF
*(*func5
[10])(int);
67 void func6(int NODEREF x
); // expected-warning{{'noderef' can only be used on an array or pointer type}}
68 void func7(int NODEREF
*x
);
73 int NODEREF
&y
= x
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
75 int NODEREF
*&a
= xp
; // ok (reference to a NODEREF *)
76 int *NODEREF
&b
= xp
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
79 void BlockPointers() {
80 typedef int NODEREF (^IntBlock
)(); // expected-warning{{'noderef' can only be used on an array or pointer type}}
87 int NODEREF member3
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
90 int func() { return member
; }
91 virtual int func_virt() { return member
; }
93 A(NODEREF
int *x
) : member4(x
) {} // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
96 class Child
: public A
{};
98 void MemberPointer() {
99 int NODEREF
A::*var
= &A::member
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
102 int MethodCall(NODEREF A
*a
) { // expected-note{{a declared here}}
103 return a
->func(); // expected-warning{{dereferencing a; was declared with a 'noderef' type}}
106 int ChildCall(NODEREF Child
*child
) { // expected-note{{child declared here}}
107 return child
->func(); // expected-warning{{dereferencing child; was declared with a 'noderef' type}}
110 std::type_info
TypeIdPolymorphic(NODEREF A
*a
) { // expected-note{{a declared here}}
111 return typeid(*a
); // expected-warning{{dereferencing a; was declared with a 'noderef' type}}
118 std::type_info
TypeIdNonPolymorphic(NODEREF SimpleClass
*simple
) {
119 return typeid(*simple
);
125 Ty NODEREF member2
; // expected-warning{{'noderef' can only be used on an array or pointer type}}
128 void test_lambdas() {
129 auto l
= [](int NODEREF
*x
){ // expected-note{{x declared here}}
130 return *x
; // expected-warning{{dereferencing x; was declared with a 'noderef' type}}
134 int NODEREF
*glob_ptr
; // expected-note{{glob_ptr declared here}}
135 int glob_int
= *glob_ptr
; // expected-warning{{dereferencing glob_ptr; was declared with a 'noderef' type}}
137 void cast_from_void_ptr(NODEREF
void *x
) {
138 int *a
= static_cast<int *>(x
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
140 // Allow regular C-style casts and C-style through reinterpret_casts to be holes
141 int *b
= reinterpret_cast<int *>(x
);
145 void conversion_sequences() {
147 int *x2
= x
; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
148 int *x3
= static_cast<int *>(x
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
149 int *x4
= reinterpret_cast<int *>(x
);
151 // Functional cast - This is exactly equivalent to a C-style cast.
152 typedef int *INT_PTR
;
153 int *x5
= INT_PTR(x
);
155 NODEREF Child
*child
;
156 Child
*child2
= dynamic_cast<Child
*>(child
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
159 int *static_cast_from_same_ptr_type(NODEREF
int *x
) {
160 return static_cast<int *>(x
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
163 A
*dynamic_cast_up(NODEREF Child
*child
) {
164 return dynamic_cast<A
*>(child
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
167 Child
*dynamic_cast_down(NODEREF A
*a
) {
168 return dynamic_cast<Child
*>(a
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
171 A
*dynamic_cast_side(NODEREF A
*a
) {
172 return dynamic_cast<A
*>(a
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
175 void *dynamic_cast_to_void_ptr(NODEREF A
*a
) {
176 return dynamic_cast<void *>(a
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
179 int *const_cast_check(NODEREF
const int *x
) {
180 return const_cast<int *>(x
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
183 const int *const_cast_check(NODEREF
int *x
) {
184 return const_cast<const int *>(x
); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
188 // This declaration would previously cause a failed assertion.
189 int *_Atomic a
__attribute__((noderef
));