xref: /aosp_15_r20/external/clang/test/Sema/attr-deprecated.c (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 %s -verify -fsyntax-only
2*67e74705SXin Li 
3*67e74705SXin Li int f() __attribute__((deprecated)); // expected-note 2 {{'f' has been explicitly marked deprecated here}}
4*67e74705SXin Li void g() __attribute__((deprecated));
5*67e74705SXin Li void g(); // expected-note {{'g' has been explicitly marked deprecated here}}
6*67e74705SXin Li 
7*67e74705SXin Li extern int var __attribute__((deprecated)); // expected-note {{'var' has been explicitly marked deprecated here}}
8*67e74705SXin Li 
a()9*67e74705SXin Li int a() {
10*67e74705SXin Li   int (*ptr)() = f; // expected-warning {{'f' is deprecated}}
11*67e74705SXin Li   f(); // expected-warning {{'f' is deprecated}}
12*67e74705SXin Li 
13*67e74705SXin Li   // test if attributes propagate to functions
14*67e74705SXin Li   g(); // expected-warning {{'g' is deprecated}}
15*67e74705SXin Li 
16*67e74705SXin Li   return var; // expected-warning {{'var' is deprecated}}
17*67e74705SXin Li }
18*67e74705SXin Li 
19*67e74705SXin Li // test if attributes propagate to variables
20*67e74705SXin Li extern int var; // expected-note {{'var' has been explicitly marked deprecated here}}
w()21*67e74705SXin Li int w() {
22*67e74705SXin Li   return var; // expected-warning {{'var' is deprecated}}
23*67e74705SXin Li }
24*67e74705SXin Li 
25*67e74705SXin Li int old_fn() __attribute__ ((deprecated));
26*67e74705SXin Li int old_fn(); // expected-note {{'old_fn' has been explicitly marked deprecated here}}
27*67e74705SXin Li int (*fn_ptr)() = old_fn; // expected-warning {{'old_fn' is deprecated}}
28*67e74705SXin Li 
old_fn()29*67e74705SXin Li int old_fn() {
30*67e74705SXin Li   return old_fn()+1;  // no warning, deprecated functions can use deprecated symbols.
31*67e74705SXin Li }
32*67e74705SXin Li 
33*67e74705SXin Li 
34*67e74705SXin Li struct foo {
35*67e74705SXin Li   int x __attribute__((deprecated)); // expected-note 3 {{'x' has been explicitly marked deprecated here}}
36*67e74705SXin Li };
37*67e74705SXin Li 
test1(struct foo * F)38*67e74705SXin Li void test1(struct foo *F) {
39*67e74705SXin Li   ++F->x;  // expected-warning {{'x' is deprecated}}
40*67e74705SXin Li   struct foo f1 = { .x = 17 }; // expected-warning {{'x' is deprecated}}
41*67e74705SXin Li   struct foo f2 = { 17 }; // expected-warning {{'x' is deprecated}}
42*67e74705SXin Li }
43*67e74705SXin Li 
44*67e74705SXin Li typedef struct foo foo_dep __attribute__((deprecated)); // expected-note 12 {{'foo_dep' has been explicitly marked deprecated here}}
45*67e74705SXin Li foo_dep *test2;    // expected-warning {{'foo_dep' is deprecated}}
46*67e74705SXin Li 
47*67e74705SXin Li struct __attribute__((deprecated,
48*67e74705SXin Li                       invalid_attribute)) bar_dep ;  // expected-warning {{unknown attribute 'invalid_attribute' ignored}} expected-note 2 {{'bar_dep' has been explicitly marked deprecated here}}
49*67e74705SXin Li 
50*67e74705SXin Li struct bar_dep *test3;   // expected-warning {{'bar_dep' is deprecated}}
51*67e74705SXin Li 
52*67e74705SXin Li 
53*67e74705SXin Li // These should not warn because the actually declaration itself is deprecated.
54*67e74705SXin Li // rdar://6756623
55*67e74705SXin Li foo_dep *test4 __attribute__((deprecated));
56*67e74705SXin Li struct bar_dep *test5 __attribute__((deprecated));
57*67e74705SXin Li 
58*67e74705SXin Li typedef foo_dep test6(struct bar_dep*); // expected-warning {{'foo_dep' is deprecated}} \
59*67e74705SXin Li                                         // expected-warning {{'bar_dep' is deprecated}}
60*67e74705SXin Li typedef foo_dep test7(struct bar_dep*) __attribute__((deprecated));
61*67e74705SXin Li 
test8(char * p)62*67e74705SXin Li int test8(char *p) {
63*67e74705SXin Li   p += sizeof(foo_dep); // expected-warning {{'foo_dep' is deprecated}}
64*67e74705SXin Li 
65*67e74705SXin Li   foo_dep *ptr;         // expected-warning {{'foo_dep' is deprecated}}
66*67e74705SXin Li   ptr = (foo_dep*) p;   // expected-warning {{'foo_dep' is deprecated}}
67*67e74705SXin Li 
68*67e74705SXin Li   int func(foo_dep *foo); // expected-warning {{'foo_dep' is deprecated}}
69*67e74705SXin Li   return func(ptr);
70*67e74705SXin Li }
71*67e74705SXin Li 
72*67e74705SXin Li foo_dep *test9(void) __attribute__((deprecated));
test9(void)73*67e74705SXin Li foo_dep *test9(void) {
74*67e74705SXin Li   void* myalloc(unsigned long);
75*67e74705SXin Li 
76*67e74705SXin Li   foo_dep *ptr
77*67e74705SXin Li     = (foo_dep*)
78*67e74705SXin Li         myalloc(sizeof(foo_dep));
79*67e74705SXin Li   return ptr;
80*67e74705SXin Li }
81*67e74705SXin Li 
82*67e74705SXin Li void test10(void) __attribute__((deprecated));
test10(void)83*67e74705SXin Li void test10(void) {
84*67e74705SXin Li   if (sizeof(foo_dep) == sizeof(void*)) {
85*67e74705SXin Li   }
86*67e74705SXin Li   foo_dep *localfunc(void);
87*67e74705SXin Li   foo_dep localvar;
88*67e74705SXin Li }
89*67e74705SXin Li 
90*67e74705SXin Li char test11[sizeof(foo_dep)] __attribute__((deprecated));
91*67e74705SXin Li char test12[sizeof(foo_dep)]; // expected-warning {{'foo_dep' is deprecated}}
92*67e74705SXin Li 
93*67e74705SXin Li int test13(foo_dep *foo) __attribute__((deprecated));
94*67e74705SXin Li int test14(foo_dep *foo); // expected-warning {{'foo_dep' is deprecated}}
95*67e74705SXin Li 
96*67e74705SXin Li unsigned long test15 = sizeof(foo_dep); // expected-warning {{'foo_dep' is deprecated}}
97*67e74705SXin Li unsigned long test16 __attribute__((deprecated))
98*67e74705SXin Li   = sizeof(foo_dep);
99*67e74705SXin Li 
100*67e74705SXin Li foo_dep test17, // expected-warning {{'foo_dep' is deprecated}}
101*67e74705SXin Li         test18 __attribute__((deprecated)),
102*67e74705SXin Li         test19;
103*67e74705SXin Li 
104*67e74705SXin Li // rdar://problem/8518751
105*67e74705SXin Li enum __attribute__((deprecated)) Test20 { // expected-note {{'Test20' has been explicitly marked deprecated here}}
106*67e74705SXin Li   test20_a __attribute__((deprecated)), // expected-note {{'test20_a' has been explicitly marked deprecated here}}
107*67e74705SXin Li   test20_b // expected-note {{'test20_b' has been explicitly marked deprecated here}}
108*67e74705SXin Li };
test20()109*67e74705SXin Li void test20() {
110*67e74705SXin Li   enum Test20 f; // expected-warning {{'Test20' is deprecated}}
111*67e74705SXin Li   f = test20_a; // expected-warning {{'test20_a' is deprecated}}
112*67e74705SXin Li   f = test20_b; // expected-warning {{'test20_b' is deprecated}}
113*67e74705SXin Li }
114*67e74705SXin Li 
115*67e74705SXin Li char test21[__has_feature(attribute_deprecated_with_message) ? 1 : -1];
116*67e74705SXin Li 
117*67e74705SXin Li struct test22 {
118*67e74705SXin Li   foo_dep a __attribute((deprecated));
119*67e74705SXin Li   foo_dep b; // expected-warning {{'foo_dep' is deprecated}}
120*67e74705SXin Li   foo_dep c, d __attribute((deprecated)); // expected-warning {{'foo_dep' is deprecated}}
121*67e74705SXin Li   __attribute((deprecated)) foo_dep e, f;
122*67e74705SXin Li };
123*67e74705SXin Li 
124*67e74705SXin Li typedef int test23_ty __attribute((deprecated));
125*67e74705SXin Li // Redefining a typedef is a C11 feature.
126*67e74705SXin Li #if __STDC_VERSION__ <= 199901L
127*67e74705SXin Li // expected-note@-3 {{'test23_ty' has been explicitly marked deprecated here}}
128*67e74705SXin Li #else
129*67e74705SXin Li typedef int test23_ty; // expected-note {{'test23_ty' has been explicitly marked deprecated here}}
130*67e74705SXin Li #endif
131*67e74705SXin Li test23_ty test23_v; // expected-warning {{'test23_ty' is deprecated}}
132