1 /* warn_unused_result attribute tests.  */
2 /* { dg-do compile } */
3 /* { dg-options "-O -ftrack-macro-expansion=0" } */
4 
5 #define WUR __attribute__((warn_unused_result))
6 #define WURAI __attribute__((warn_unused_result, always_inline)) inline
7 typedef WUR int (*fnt) (void);
8 
9 typedef struct { long i; } A;
10 typedef struct { long i; long j; } B;
11 typedef struct { char big[1024]; fnt fn; } C;
12 
13 WUR int check1 (void);
14 WUR void check2 (void); /* { dg-warning "attribute ignored" } */
15 int foo WUR;	  /* { dg-message "only applies" } */
16 int bar (void);
check3(void)17 extern WURAI int check3 (void) { return bar (); }
18 WUR A check4 (void);
19 WUR B check5 (void);
20 WUR C check6 (void);
21 A bar7 (void);
22 B bar8 (void);
23 C bar9 (void);
check7(void)24 extern WURAI A check7 (void) { return bar7 (); }
check8(void)25 extern WURAI B check8 (void) { return bar8 (); }
check9(void)26 extern WURAI C check9 (void) { return bar9 (); }
27 /* This is useful for checking whether return value of statement
28    expressions (returning int in this case) is used.  */
check_int_result(int res)29 extern WURAI int check_int_result (int res) { return res; }
30 #define GU(v) ({ int e = 0; (v) = bar (); if ((v) < 23) e = 14; e; })
31 fnt fnptr;
32 WUR int check10 (void);
33 int baz (void);
check11(void)34 extern WURAI int check11 (void) { return baz (); }
35 int k;
36 
37 void
test(void)38 test (void)
39 {
40   int i = 0, j;
41   const fnt pcheck1 = check1;
42   const fnt pcheck3 = check3;
43   A a;
44   B b;
45   C c;
46   if (check1 ())
47     return;
48   i += check1 ();
49   i += ({ check1 (); });
50   check1 ();		/* { dg-warning "ignoring return value of" } */
51   (void) check1 ();	/* { dg-warning "ignoring return value of" } */
52   check1 (), bar ();	/* { dg-warning "ignoring return value of" } */
53   check2 ();
54   (void) check2 ();
55   check2 (), bar ();
56   if (check3 ())
57     return;
58   i += check3 ();
59   i += ({ check3 (); });
60   check3 ();		/* { dg-warning "ignoring return value of" } */
61   (void) check3 ();	/* { dg-warning "ignoring return value of" } */
62   check3 (), bar ();	/* { dg-warning "ignoring return value of" } */
63   a = check4 ();
64   if (a.i)
65     return;
66   if (check4 ().i)
67     return;
68   if (({ check4 (); }).i)
69     return;
70   check4 ();		/* { dg-warning "ignoring return value of" } */
71   (void) check4 ();	/* { dg-warning "ignoring return value of" } */
72   check4 (), bar ();	/* { dg-warning "ignoring return value of" } */
73   b = check5 ();
74   if (b.i + b.j)
75     return;
76   if (check5 ().j)
77     return;
78   if (({ check5 (); }).j)
79     return;
80   check5 ();		/* { dg-warning "ignoring return value of" } */
81   (void) check5 ();	/* { dg-warning "ignoring return value of" } */
82   check5 (), bar ();	/* { dg-warning "ignoring return value of" } */
83   c = check6 ();
84   if (c.big[12] + c.big[29])
85     return;
86   if (check6 ().big[27])
87     return;
88   if (({ check6 (); }).big[0])
89     return;
90   check6 ();		/* { dg-warning "ignoring return value of" } */
91   (void) check6 ();	/* { dg-warning "ignoring return value of" } */
92   check6 (), bar ();	/* { dg-warning "ignoring return value of" } */
93   a = check7 ();
94   if (a.i)
95     return;
96   if (check7 ().i)
97     return;
98   if (({ check7 (); }).i)
99     return;
100   check7 ();		/* { dg-warning "ignoring return value of" } */
101   (void) check7 ();	/* { dg-warning "ignoring return value of" } */
102   check7 (), bar ();	/* { dg-warning "ignoring return value of" } */
103   b = check8 ();
104   if (b.i + b.j)
105     return;
106   if (check8 ().j)
107     return;
108   if (({ check8 (); }).j)
109     return;
110   check8 ();		/* { dg-warning "ignoring return value of" } */
111   (void) check8 ();	/* { dg-warning "ignoring return value of" } */
112   check8 (), bar ();	/* { dg-warning "ignoring return value of" } */
113   c = check9 ();
114   if (c.big[12] + c.big[29])
115     return;
116   if (check9 ().big[27])
117     return;
118   if (({ check9 (); }).big[0])
119     return;
120   check9 ();		/* { dg-warning "ignoring return value of" } */
121   (void) check9 ();	/* { dg-warning "ignoring return value of" } */
122   check9 (), bar ();	/* { dg-warning "ignoring return value of" } */
123   if (check_int_result (GU (j)))
124     return;
125   i += check_int_result (GU (j));
126   i += ({ check_int_result (GU (j)); });
127   check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
128   (void) check_int_result (GU (j)); /* { dg-warning "ignoring return value of" } */
129   check_int_result (GU (j)), bar (); /* { dg-warning "ignoring return value of" } */
130   if (fnptr ())
131     return;
132   i += fnptr ();
133   i += ({ fnptr (); });
134   fnptr ();		/* { dg-warning "ignoring return value of" } */
135   (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
136   fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
137   fnptr = check1;
138   if (fnptr ())
139     return;
140   i += fnptr ();
141   i += ({ fnptr (); });
142   fnptr ();		/* { dg-warning "ignoring return value of" } */
143   (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
144   fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
145   fnptr = check3;
146   if (fnptr ())
147     return;
148   i += fnptr ();
149   i += ({ fnptr (); });
150   fnptr ();		/* { dg-warning "ignoring return value of" } */
151   (void) fnptr ();	/* { dg-warning "ignoring return value of" } */
152   fnptr (), bar ();	/* { dg-warning "ignoring return value of" } */
153   if (bar9 ().fn ())
154     return;
155   i += bar9 ().fn ();
156   i += ({ bar9 ().fn (); });
157   bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
158   (void) bar9 ().fn ();	/* { dg-warning "ignoring return value of" } */
159   bar9 ().fn (), bar (); /* { dg-warning "ignoring return value of" } */
160   if ((k ? check1 : check10) ())
161     return;
162   i += (k ? check1 : check10) ();
163   i += ({ (k ? check1 : check10) (); });
164   (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
165   (void) (k ? check1 : check10) (); /* { dg-warning "ignoring return value of" } */
166   (k ? check1 : check10) (), bar (); /* { dg-warning "ignoring return value of" } */
167   if ((k ? check3 : check11) ())
168     return;
169   i += (k ? check3 : check11) ();
170   i += ({ (k ? check3 : check11) (); });
171   (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
172   (void) (k ? check3 : check11) (); /* { dg-warning "ignoring return value of" } */
173   (k ? check3 : check11) (), bar (); /* { dg-warning "ignoring return value of" } */
174   if (pcheck1 ())
175     return;
176   i += pcheck1 ();
177   i += ({ pcheck1 (); });
178   pcheck1 ();		/* { dg-warning "ignoring return value of" } */
179   (void) pcheck1 ();	/* { dg-warning "ignoring return value of" } */
180   pcheck1 (), bar ();	/* { dg-warning "ignoring return value of" } */
181   if (pcheck3 ())
182     return;
183   i += pcheck3 ();
184   i += ({ pcheck3 (); });
185   pcheck3 ();		/* { dg-warning "ignoring return value of" } */
186   (void) pcheck3 ();	/* { dg-warning "ignoring return value of" } */
187   pcheck3 (), bar ();	/* { dg-warning "ignoring return value of" } */
188 }
189