Debian Codegraph @

clang/test/AST/attr-counted-by-struct-ptrs.c

raw
1 // RUN: %clang_cc1 %s -ast-dump | FileCheck %s
2
3 #define __counted_by(f) __attribute__((counted_by(f)))
4
5 struct size_unknown;
6 struct size_known {
7 int field;
8 };
9
10 //==============================================================================
11 // __counted_by on struct member pointer in decl attribute position
12 //==============================================================================
13
14 // CHECK-LABEL: RecordDecl {{.+}} struct on_member_pointer_complete_ty definition
15 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
16 // CHECK-NEXT: `-FieldDecl {{.+}} buf 'struct size_known * __counted_by(count)':'struct size_known *'
17 struct on_member_pointer_complete_ty {
18 int count;
19 struct size_known * buf __counted_by(count);
20 };
21
22 // CHECK-LABEL: RecordDecl {{.+}} struct on_pointer_anon_buf definition
23 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
24 // CHECK-NEXT: |-RecordDecl {{.+}} struct definition
25 // CHECK-NEXT: | `-FieldDecl {{.+}} buf 'struct size_known * __counted_by(count)':'struct size_known *'
26 // CHECK-NEXT: |-FieldDecl {{.+}} implicit 'struct on_pointer_anon_buf::(anonymous at [[ANON_STRUCT_PATH:.+]])'
27 // CHECK-NEXT: `-IndirectFieldDecl {{.+}} implicit buf 'struct size_known * __counted_by(count)':'struct size_known *'
28 // CHECK-NEXT: |-Field {{.+}} '' 'struct on_pointer_anon_buf::(anonymous at [[ANON_STRUCT_PATH]])'
29 // CHECK-NEXT: `-Field {{.+}} 'buf' 'struct size_known * __counted_by(count)':'struct size_known *'
30 struct on_pointer_anon_buf {
31 int count;
32 struct {
33 struct size_known *buf __counted_by(count);
34 };
35 };
36
37 struct on_pointer_anon_count {
38 struct {
39 int count;
40 };
41 struct size_known *buf __counted_by(count);
42 };
43
44 //==============================================================================
45 // __counted_by on struct member pointer in type attribute position
46 //==============================================================================
47 // TODO: Correctly parse counted_by as a type attribute. Currently it is parsed
48 // as a declaration attribute
49
50 // CHECK-LABEL: RecordDecl {{.+}} struct on_member_pointer_complete_ty_ty_pos definition
51 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
52 // CHECK-NEXT: `-FieldDecl {{.+}} buf 'struct size_known * __counted_by(count)':'struct size_known *'
53 struct on_member_pointer_complete_ty_ty_pos {
54 int count;
55 struct size_known *__counted_by(count) buf;
56 };
57
58 // TODO: This should be forbidden but isn't due to counted_by being treated as a
59 // declaration attribute. The attribute ends up on the outer most pointer
60 // (allowed by sema) even though syntactically its supposed to be on the inner
61 // pointer (would not allowed by sema due to pointee being a function type).
62 // CHECK-LABEL: RecordDecl {{.+}} struct on_member_pointer_fn_ptr_ty_ty_pos_inner definition
63 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
64 // CHECK-NEXT: `-FieldDecl {{.+}} fn_ptr 'void (** __counted_by(count))(void)':'void (**)(void)'
65 struct on_member_pointer_fn_ptr_ty_ty_pos_inner {
66 int count;
67 void (* __counted_by(count) * fn_ptr)(void);
68 };
69
70 // FIXME: The generated AST here is wrong. The attribute should be on the inner
71 // pointer.
72 // CHECK-LABEL: RecordDecl {{.+}} struct on_nested_pointer_inner definition
73 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
74 // CHECK-NEXT: `-FieldDecl {{.+}} buf 'struct size_known ** __counted_by(count)':'struct size_known **'
75 struct on_nested_pointer_inner {
76 int count;
77 // TODO: This should be disallowed because in the `-fbounds-safety` model
78 // `__counted_by` can only be nested when used in function parameters.
79 struct size_known *__counted_by(count) *buf;
80 };
81
82 // CHECK-LABEL: RecordDecl {{.+}} struct on_nested_pointer_outer definition
83 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
84 // CHECK-NEXT: `-FieldDecl {{.+}} buf 'struct size_known ** __counted_by(count)':'struct size_known **'
85 struct on_nested_pointer_outer {
86 int count;
87 struct size_known **__counted_by(count) buf;
88 };
89
90 // CHECK-LABEL: RecordDecl {{.+}} struct on_pointer_anon_buf_ty_pos definition
91 // CHECK-NEXT: |-FieldDecl {{.+}} referenced count 'int'
92 // CHECK-NEXT: |-RecordDecl {{.+}} struct definition
93 // CHECK-NEXT: | `-FieldDecl {{.+}} buf 'struct size_known * __counted_by(count)':'struct size_known *'
94 // CHECK-NEXT: |-FieldDecl {{.+}} implicit 'struct on_pointer_anon_buf_ty_pos::(anonymous at [[ANON_STRUCT_PATH2:.+]])'
95 // CHECK-NEXT: `-IndirectFieldDecl {{.+}} implicit buf 'struct size_known * __counted_by(count)':'struct size_known *'
96 // CHECK-NEXT: |-Field {{.+}} '' 'struct on_pointer_anon_buf_ty_pos::(anonymous at [[ANON_STRUCT_PATH2]])'
97 // CHECK-NEXT: `-Field {{.+}} 'buf' 'struct size_known * __counted_by(count)':'struct size_known *'
98 struct on_pointer_anon_buf_ty_pos {
99 int count;
100 struct {
101 struct size_known * __counted_by(count) buf;
102 };
103 };
104
105 // CHECK-LABEL: RecordDecl {{.+}} struct on_pointer_anon_count_ty_pos definition
106 // CHECK-NEXT: |-RecordDecl {{.+}} struct definition
107 // CHECK-NEXT: | `-FieldDecl {{.+}} count 'int'
108 // CHECK-NEXT: |-FieldDecl {{.+}} implicit 'struct on_pointer_anon_count_ty_pos::(anonymous at [[ANON_STRUCT_PATH3:.+]])'
109 // CHECK-NEXT: |-IndirectFieldDecl {{.+}} implicit referenced count 'int'
110 // CHECK-NEXT: | |-Field {{.+}} '' 'struct on_pointer_anon_count_ty_pos::(anonymous at [[ANON_STRUCT_PATH3]])'
111 // CHECK-NEXT: | `-Field {{.+}} 'count' 'int'
112 struct on_pointer_anon_count_ty_pos {
113 struct {
114 int count;
115 };
116 struct size_known *__counted_by(count) buf;
117 };