Debian Codegraph @

clang/test/Sema/attr-sized-by-last-field.c

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
2
3 #define __sized_by(f) __attribute__((sized_by(f)))
4
5 // This has been adapted from clang/test/Sema/attr-counted-by-vla.c, but with VLAs replaced with pointers
6
7 struct bar;
8
9 struct not_found {
10 int size;
11 struct bar *ptr __sized_by(bork); // expected-error {{use of undeclared identifier 'bork'}}
12 };
13
14 struct no_found_size_not_in_substruct {
15 unsigned long flags;
16 unsigned char size; // expected-note {{'size' declared here}}
17 struct A {
18 int dummy;
19 int * ptr __sized_by(size); // expected-error {{'sized_by' field 'size' isn't within the same struct as the annotated pointer}}
20 } a;
21 };
22
23 struct not_found_size_not_in_unnamed_substruct {
24 unsigned char size; // expected-note {{'size' declared here}}
25 struct {
26 int dummy;
27 int * ptr __sized_by(size); // expected-error {{'sized_by' field 'size' isn't within the same struct as the annotated pointer}}
28 } a;
29 };
30
31 struct not_found_size_not_in_unnamed_substruct_2 {
32 struct {
33 unsigned char size; // expected-note {{'size' declared here}}
34 };
35 struct {
36 int dummy;
37 int * ptr __sized_by(size); // expected-error {{'sized_by' field 'size' isn't within the same struct as the annotated pointer}}
38 } a;
39 };
40
41 struct not_found_size_in_other_unnamed_substruct {
42 struct {
43 unsigned char size;
44 } a1;
45
46 struct {
47 int dummy;
48 int * ptr __sized_by(size); // expected-error {{use of undeclared identifier 'size'}}
49 };
50 };
51
52 struct not_found_size_in_other_substruct {
53 struct _a1 {
54 unsigned char size;
55 } a1;
56
57 struct {
58 int dummy;
59 int * ptr __sized_by(size); // expected-error {{use of undeclared identifier 'size'}}
60 };
61 };
62
63 struct not_found_size_in_other_substruct_2 {
64 struct _a2 {
65 unsigned char size;
66 } a2;
67
68 int * ptr __sized_by(size); // expected-error {{use of undeclared identifier 'size'}}
69 };
70
71 struct not_found_suggest {
72 int bork;
73 struct bar **ptr __sized_by(blork); // expected-error {{use of undeclared identifier 'blork'}}
74 };
75
76 int global; // expected-note {{'global' declared here}}
77
78 struct found_outside_of_struct {
79 int bork;
80 struct bar ** ptr __sized_by(global); // expected-error {{field 'global' in 'sized_by' not inside structure}}
81 };
82
83 struct self_referrential {
84 int bork;
85 struct bar *self[] __sized_by(self); // expected-error {{use of undeclared identifier 'self'}}
86 };
87
88 struct non_int_size {
89 double dbl_size;
90 struct bar ** ptr __sized_by(dbl_size); // expected-error {{'sized_by' requires a non-boolean integer type argument}}
91 };
92
93 struct array_of_ints_size {
94 int integers[2];
95 struct bar ** ptr __sized_by(integers); // expected-error {{'sized_by' requires a non-boolean integer type argument}}
96 };
97
98 struct not_a_c99_fam {
99 int size;
100 struct bar *non_c99_fam[0] __sized_by(size); // expected-error {{'sized_by' only applies to pointers; did you mean to use 'counted_by'?}}
101 };
102
103 struct annotated_with_anon_struct {
104 unsigned long flags;
105 struct {
106 unsigned char size;
107 int * ptr __sized_by(crount); // expected-error {{use of undeclared identifier 'crount'}}
108 };
109 };
110
111 //==============================================================================
112 // __sized_by on a struct ptr with element type that has unknown size
113 //==============================================================================
114
115 struct size_unknown;
116 struct on_member_ptr_incomplete_ty_ty_pos {
117 int size;
118 struct size_unknown * ptr __sized_by(size);
119 };
120
121 struct on_member_ptr_incomplete_const_ty_ty_pos {
122 int size;
123 const struct size_unknown * ptr __sized_by(size);
124 };
125
126 struct on_member_ptr_void_ty_ty_pos {
127 int size;
128 void * ptr __sized_by(size);
129 };
130
131 typedef void(fn_ty)(int);
132
133 struct on_member_ptr_fn_ptr_ty {
134 int size;
135 fn_ty* * ptr __sized_by(size);
136 };
137
138 struct on_member_ptr_fn_ty {
139 int size;
140 // expected-error@+1{{'sized_by' cannot be applied to a pointer with pointee of unknown size because 'fn_ty' (aka 'void (int)') is a function type}}
141 fn_ty * ptr __sized_by(size);
142 };