blob: c1ed5f84cf9359d4e072c0dd52f10be006ac8dc6 (
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
// RUN: %clang_cc1 -fsyntax-only -verify=expected-nowarn %s
// RUN: %clang_cc1 -Wpointer-arith -fsyntax-only -verify=expected-warn %s
// RUN: %clang_cc1 -fexperimental-bounds-safety -fsyntax-only -verify=expected-bounds %s
// expected-nowarn-no-diagnostics
// expected-bounds-no-diagnostics
#define NULL (void*)0
#define __counted_by(f) __attribute__((counted_by(f)))
#define __counted_by_or_null(f) __attribute__((counted_by_or_null(f)))
#define __sized_by(f) __attribute__((sized_by(f)))
//==============================================================================
// Test: counted_by on void* is allowed (warns with -Wpointer-arith)
//==============================================================================
struct test_void_ptr_gnu {
int count;
// expected-warn-warning@+2{{'counted_by' on a pointer to void is a GNU extension, treated as 'sized_by'}}
// expected-warn-note@+1{{use '__sized_by' to suppress this warning}}
void* buf __counted_by(count);
};
struct test_const_void_ptr_gnu {
int count;
// expected-warn-warning@+2{{'counted_by' on a pointer to void is a GNU extension, treated as 'sized_by'}}
// expected-warn-note@+1{{use '__sized_by' to suppress this warning}}
const void* buf __counted_by(count);
};
struct test_volatile_void_ptr_gnu {
int count;
// expected-warn-warning@+2{{'counted_by' on a pointer to void is a GNU extension, treated as 'sized_by'}}
// expected-warn-note@+1{{use '__sized_by' to suppress this warning}}
volatile void* buf __counted_by(count);
};
struct test_const_volatile_void_ptr_gnu {
int count;
// expected-warn-warning@+2{{'counted_by' on a pointer to void is a GNU extension, treated as 'sized_by'}}
// expected-warn-note@+1{{use '__sized_by' to suppress this warning}}
const volatile void* buf __counted_by(count);
};
// Verify sized_by still works the same way (always allowed, no warning)
struct test_sized_by_void_ptr {
int size;
void* buf __sized_by(size); // OK in both modes, no warning
};
//==============================================================================
// Test: counted_by_or_null on void* behaves the same
//==============================================================================
struct test_void_ptr_or_null_gnu {
int count;
// expected-warn-warning@+2{{'counted_by_or_null' on a pointer to void is a GNU extension, treated as 'sized_by_or_null'}}
// expected-warn-note@+1{{use '__sized_by_or_null' to suppress this warning}}
void* buf __counted_by_or_null(count);
};
struct test_const_void_ptr_or_null_gnu {
int count;
// expected-warn-warning@+2{{'counted_by_or_null' on a pointer to void is a GNU extension, treated as 'sized_by_or_null'}}
// expected-warn-note@+1{{use '__sized_by_or_null' to suppress this warning}}
const void* buf __counted_by_or_null(count);
};
//==============================================================================
// Test: Using void* __counted_by(...) pointers (not just declaring them)
//==============================================================================
// Verify that void* __counted_by pointers can be used as rvalues, assigned to,
// passed to functions, etc.
void* use_as_rvalue(struct test_void_ptr_gnu* t) {
return t->buf;
}
void assign_to_pointer(struct test_void_ptr_gnu* t) {
t->buf = NULL;
t->count = 0;
}
extern void* my_allocator(unsigned long);
void assign_from_allocator(struct test_void_ptr_gnu* t) {
t->buf = my_allocator(100);
t->count = 100;
}
void takes_void_ptr(void* p);
void pass_to_function(struct test_void_ptr_gnu* t) {
takes_void_ptr(t->buf);
}
void* pointer_arithmetic(struct test_void_ptr_gnu* t) {
// expected-warn-warning@+1{{arithmetic on a pointer to void is a GNU extension}}
return t->buf + 10;
}
|