annotate clang/test/Frontend/noderef.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fblocks -verify %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 /**
anatofuz
parents:
diff changeset
4 * Test 'noderef' attribute with c++ constructs.
anatofuz
parents:
diff changeset
5 */
anatofuz
parents:
diff changeset
6
anatofuz
parents:
diff changeset
7 #define NODEREF __attribute__((noderef))
anatofuz
parents:
diff changeset
8
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
9 // Stub out types for 'typeid' to work.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
10 namespace std {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
11 class type_info {};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
12 } // namespace std
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
13
150
anatofuz
parents:
diff changeset
14 void Normal() {
anatofuz
parents:
diff changeset
15 int NODEREF i; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
16 int NODEREF *i_ptr; // expected-note 2 {{i_ptr declared here}}
anatofuz
parents:
diff changeset
17 int NODEREF **i_ptr2; // ok
anatofuz
parents:
diff changeset
18 int *NODEREF i_ptr3; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
19 int *NODEREF *i_ptr4; // ok
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 auto NODEREF *auto_i_ptr = i_ptr;
anatofuz
parents:
diff changeset
22 auto NODEREF auto_i = i; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
23
anatofuz
parents:
diff changeset
24 struct {
anatofuz
parents:
diff changeset
25 int x;
anatofuz
parents:
diff changeset
26 int y;
anatofuz
parents:
diff changeset
27 } NODEREF *s;
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 int __attribute__((noderef(10))) * no_args; // expected-error{{'noderef' attribute takes no arguments}}
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 int i2 = *i_ptr; // expected-warning{{dereferencing i_ptr; was declared with a 'noderef' type}}
anatofuz
parents:
diff changeset
32 int &i3 = *i_ptr; // expected-warning{{dereferencing i_ptr; was declared with a 'noderef' type}}
anatofuz
parents:
diff changeset
33 int *i_ptr5 = i_ptr; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
anatofuz
parents:
diff changeset
34 int *i_ptr6(i_ptr); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
anatofuz
parents:
diff changeset
35 }
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 const int NODEREF *const_i_ptr;
anatofuz
parents:
diff changeset
38 static int NODEREF *static_i_ptr;
anatofuz
parents:
diff changeset
39
anatofuz
parents:
diff changeset
40 void ParenTypes() {
anatofuz
parents:
diff changeset
41 int NODEREF(*i_ptr); // ok (same as `int NODEREF *`)
anatofuz
parents:
diff changeset
42 int NODEREF *(*i_ptr2); // ok (same as `int NODEREF **`)
anatofuz
parents:
diff changeset
43 }
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 // Function declarations
anatofuz
parents:
diff changeset
46 int NODEREF func(); // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
47 int NODEREF *func2(); // ok (returning pointer)
anatofuz
parents:
diff changeset
48
anatofuz
parents:
diff changeset
49 typedef int NODEREF (*func3)(int); // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
50 typedef int NODEREF *(*func4)(int);
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 void Arrays() {
anatofuz
parents:
diff changeset
53 int NODEREF i_arr[10]; // ok
anatofuz
parents:
diff changeset
54 int NODEREF i_arr2[10][10]; // ok
anatofuz
parents:
diff changeset
55 int NODEREF *i_arr3[10]; // ok
anatofuz
parents:
diff changeset
56 int NODEREF i_arr4[] = {1, 2};
anatofuz
parents:
diff changeset
57 }
anatofuz
parents:
diff changeset
58
anatofuz
parents:
diff changeset
59 void ParenArrays() {
anatofuz
parents:
diff changeset
60 int NODEREF(i_ptr[10]);
anatofuz
parents:
diff changeset
61 int NODEREF(i_ptr2[10])[10];
anatofuz
parents:
diff changeset
62 }
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 typedef int NODEREF *(*func5[10])(int);
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 // Arguments
anatofuz
parents:
diff changeset
67 void func6(int NODEREF x); // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
68 void func7(int NODEREF *x);
anatofuz
parents:
diff changeset
69 void func8() NODEREF;
anatofuz
parents:
diff changeset
70
anatofuz
parents:
diff changeset
71 void References() {
anatofuz
parents:
diff changeset
72 int x = 2;
anatofuz
parents:
diff changeset
73 int NODEREF &y = x; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
74 int *xp = &x;
anatofuz
parents:
diff changeset
75 int NODEREF *&a = xp; // ok (reference to a NODEREF *)
anatofuz
parents:
diff changeset
76 int *NODEREF &b = xp; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
77 }
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 void BlockPointers() {
anatofuz
parents:
diff changeset
80 typedef int NODEREF (^IntBlock)(); // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
81 }
anatofuz
parents:
diff changeset
82
anatofuz
parents:
diff changeset
83 class A {
anatofuz
parents:
diff changeset
84 public:
anatofuz
parents:
diff changeset
85 int member;
anatofuz
parents:
diff changeset
86 int NODEREF *member2;
anatofuz
parents:
diff changeset
87 int NODEREF member3; // expected-warning{{'noderef' can only be used on an array or pointer type}}
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
88 int *member4;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
89
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
90 int func() { return member; }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
91 virtual int func_virt() { return member; }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
92
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
93 A(NODEREF int *x) : member4(x) {} // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
150
anatofuz
parents:
diff changeset
94 };
anatofuz
parents:
diff changeset
95
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
96 class Child : public A {};
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
97
150
anatofuz
parents:
diff changeset
98 void MemberPointer() {
anatofuz
parents:
diff changeset
99 int NODEREF A::*var = &A::member; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
100 }
anatofuz
parents:
diff changeset
101
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
102 int MethodCall(NODEREF A *a) { // expected-note{{a declared here}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
103 return a->func(); // expected-warning{{dereferencing a; was declared with a 'noderef' type}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
104 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
105
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106 int ChildCall(NODEREF Child *child) { // expected-note{{child declared here}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 return child->func(); // expected-warning{{dereferencing child; was declared with a 'noderef' type}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
109
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
110 std::type_info TypeIdPolymorphic(NODEREF A *a) { // expected-note{{a declared here}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
111 return typeid(*a); // expected-warning{{dereferencing a; was declared with a 'noderef' type}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
112 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
113
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
114 class SimpleClass {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115 int a;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
116 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
117
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
118 std::type_info TypeIdNonPolymorphic(NODEREF SimpleClass *simple) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
119 return typeid(*simple);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
121
150
anatofuz
parents:
diff changeset
122 template <class Ty>
anatofuz
parents:
diff changeset
123 class B {
anatofuz
parents:
diff changeset
124 Ty NODEREF *member;
anatofuz
parents:
diff changeset
125 Ty NODEREF member2; // expected-warning{{'noderef' can only be used on an array or pointer type}}
anatofuz
parents:
diff changeset
126 };
anatofuz
parents:
diff changeset
127
anatofuz
parents:
diff changeset
128 void test_lambdas() {
anatofuz
parents:
diff changeset
129 auto l = [](int NODEREF *x){ // expected-note{{x declared here}}
anatofuz
parents:
diff changeset
130 return *x; // expected-warning{{dereferencing x; was declared with a 'noderef' type}}
anatofuz
parents:
diff changeset
131 };
anatofuz
parents:
diff changeset
132 }
anatofuz
parents:
diff changeset
133
anatofuz
parents:
diff changeset
134 int NODEREF *glob_ptr; // expected-note{{glob_ptr declared here}}
anatofuz
parents:
diff changeset
135 int glob_int = *glob_ptr; // expected-warning{{dereferencing glob_ptr; was declared with a 'noderef' type}}
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
136
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
137 void cast_from_void_ptr(NODEREF void *x) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
138 int *a = static_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
139
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
140 // Allow regular C-style casts and C-style through reinterpret_casts to be holes
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
141 int *b = reinterpret_cast<int *>(x);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
142 int *c = (int *)x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
143 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
144
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
145 void conversion_sequences() {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
146 NODEREF int *x;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
147 int *x2 = x; // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
148 int *x3 = static_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
149 int *x4 = reinterpret_cast<int *>(x);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
150
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
151 // Functional cast - This is exactly equivalent to a C-style cast.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
152 typedef int *INT_PTR;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
153 int *x5 = INT_PTR(x);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
154
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
155 NODEREF Child *child;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
156 Child *child2 = dynamic_cast<Child *>(child); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
157 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
158
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
159 int *static_cast_from_same_ptr_type(NODEREF int *x) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
160 return static_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
161 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
162
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
163 A *dynamic_cast_up(NODEREF Child *child) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
164 return dynamic_cast<A *>(child); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
165 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
166
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167 Child *dynamic_cast_down(NODEREF A *a) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
168 return dynamic_cast<Child *>(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
169 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
170
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
171 A *dynamic_cast_side(NODEREF A *a) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
172 return dynamic_cast<A *>(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
173 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
174
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
175 void *dynamic_cast_to_void_ptr(NODEREF A *a) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
176 return dynamic_cast<void *>(a); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
177 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
178
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
179 int *const_cast_check(NODEREF const int *x) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
180 return const_cast<int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
181 }
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
182
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
183 const int *const_cast_check(NODEREF int *x) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
184 return const_cast<const int *>(x); // expected-warning{{casting to dereferenceable pointer removes 'noderef' attribute}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
185 }