Debian Codegraph @

clang/test/Frontend/cfi-unchecked-callee-attribute.c

raw
1 // RUN: %clang_cc1 -Wall -Wno-unused -Wno-uninitialized -verify %s
2
3 #define CFI_UNCHECKED_CALLEE __attribute__((cfi_unchecked_callee))
4
5 void unchecked(void) CFI_UNCHECKED_CALLEE {}
6 void checked(void) {}
7
8 void (*checked_ptr)(void) = unchecked; // expected-warning{{implicit conversion from 'void (void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
9 void (CFI_UNCHECKED_CALLEE *unchecked_ptr)(void) = unchecked;
10 void (CFI_UNCHECKED_CALLEE *from_normal)(void) = checked;
11 void (CFI_UNCHECKED_CALLEE *c_no_function_decay)(void) = &unchecked;
12
13 typedef void (CFI_UNCHECKED_CALLEE unchecked_func_t)(void);
14 typedef void (checked_func_t)(void);
15 typedef void (CFI_UNCHECKED_CALLEE *cfi_unchecked_func_ptr_t)(void);
16 typedef void (*checked_func_ptr_t)(void);
17 checked_func_t *cfi_func = unchecked; // expected-warning{{implicit conversion from 'void (void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
18 unchecked_func_t *unchecked_func = unchecked;
19
20 void CFI_UNCHECKED_CALLEE after_ret_type(void);
21 CFI_UNCHECKED_CALLEE void before_ret_type(void);
22 void (* CFI_UNCHECKED_CALLEE after_name)(void);
23
24 void UsageOnImproperTypes() {
25 int CFI_UNCHECKED_CALLEE i; // expected-warning{{'cfi_unchecked_callee' only applies to function types; type here is 'int'}}
26
27 /// The attribute must be used only on functions with prototypes. The omission of `void` means it is not prototyped.
28 void (CFI_UNCHECKED_CALLEE *noproto)(void); // expecteed-warning{{'cfi_unchecked_callee' attribute only applies to non-K&R-style functions}}
29 }
30
31 /// Explicit casts suppress the warning.
32 void CheckCasts() {
33 void (*should_warn)(void) = unchecked; // expected-warning{{implicit conversion from 'void (void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
34
35 void (*no_warn_c_style_cast)(void) = (void (*)(void))unchecked;
36
37 struct B {} CFI_UNCHECKED_CALLEE b; // expected-warning{{'cfi_unchecked_callee' attribute only applies to functions and methods}}
38 struct CFI_UNCHECKED_CALLEE C {} c; // expected-warning{{'cfi_unchecked_callee' attribute only applies to functions and methods}}
39 CFI_UNCHECKED_CALLEE struct D {} d; // expected-warning{{'cfi_unchecked_callee' only applies to function types; type here is 'struct D'}}
40
41 void *ptr2 = (void *)unchecked;
42 }
43
44 int checked_arg_func(checked_func_t *checked_func);
45
46 void CheckDifferentConstructions() {
47 void (CFI_UNCHECKED_CALLEE *arr[10])(void);
48 void (*cfi_elem)(void) = arr[1]; // expected-warning{{implicit conversion from 'void (*)(void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
49 void (CFI_UNCHECKED_CALLEE *cfi_unchecked_elem)(void) = arr[1];
50
51 int invoke = checked_arg_func(unchecked); // expected-warning{{implicit conversion from 'void (void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
52 }
53
54 checked_func_t *returning_checked_func() {
55 return unchecked; // expected-warning{{implicit conversion from 'void (void) __attribute__((cfi_unchecked_callee))' to 'void (*)(void)' discards 'cfi_unchecked_callee' attribute}}
56 }
57
58 void no_args(void) __attribute__((cfi_unchecked_callee(10))); // expected-error{{'cfi_unchecked_callee' attribute takes no arguments}}
59
60 void Comparisons() {
61 /// Let's be able to compare checked and unchecked pointers without warnings.
62 unchecked == checked_ptr;
63 checked_ptr == unchecked;
64 unchecked == unchecked_ptr;
65 unchecked != checked_ptr;
66 checked_ptr != unchecked;
67 unchecked != unchecked_ptr;
68
69 (void (*)(void))unchecked == checked_ptr;
70 checked_ptr == (void (*)(void))unchecked;
71 }