blob: 43d8150b02cbf28a2c2d595b5fd65fe94716dc42 [file] [log] [blame]
Argyrios Kyrtzidis1b2ad2f2010-09-19 23:03:35 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code
Reid Spencer5f016e22007-07-11 17:01:13 +00002
3int foo(int X, int Y);
4
Dan Gohmanc31176d2010-01-08 02:20:44 +00005double sqrt(double X); // implicitly const because of no -fmath-errno!
Chris Lattner5bef8dd2009-02-17 00:35:09 +00006
Reid Spencer5f016e22007-07-11 17:01:13 +00007void bar(volatile int *VP, int *P, int A,
8 _Complex double C, volatile _Complex double VC) {
9
Stephen Hines651f13c2014-04-23 16:59:28 -070010 VP < P; // expected-warning {{relational comparison result unused}}
Chris Lattner6e844ad2007-08-26 17:32:59 +000011 (void)A;
Reid Spencer5f016e22007-07-11 17:01:13 +000012 (void)foo(1,2); // no warning.
13
Stephen Hines651f13c2014-04-23 16:59:28 -070014 A < foo(1, 2); // expected-warning {{relational comparison result unused}}
Reid Spencer5f016e22007-07-11 17:01:13 +000015
16 foo(1,2)+foo(4,3); // expected-warning {{expression result unused}}
17
18
19 *P; // expected-warning {{expression result unused}}
20 *VP; // no warning.
21 P[4]; // expected-warning {{expression result unused}}
22 VP[4]; // no warning.
23
Chris Lattner7d84c762009-02-17 00:32:04 +000024 __real__ C; // expected-warning {{expression result unused}}
25 __real__ VC;
Chris Lattner5bef8dd2009-02-17 00:35:09 +000026
Dan Gohmanc31176d2010-01-08 02:20:44 +000027 // We know this can't change errno because of no -fmath-errno.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +000028 sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}}
Reid Spencer5f016e22007-07-11 17:01:13 +000029}
30
Chris Lattner6e844ad2007-08-26 17:32:59 +000031extern void t1();
32extern void t2();
33void t3(int c) {
34 c ? t1() : t2();
35}
36
Chris Lattner98414c12007-08-31 21:49:55 +000037// This shouldn't warn: the expr at the end of the stmtexpr really is used.
38int stmt_expr(int x, int y) {
39 return ({int _a = x, _b = y; _a > _b ? _a : _b; });
40}
41
Eli Friedman4be1f472008-05-19 21:24:43 +000042void nowarn(unsigned char* a, unsigned char* b)
43{
44 unsigned char c = 1;
45 *a |= c, *b += c;
Chris Lattnerfb846642009-07-28 18:25:28 +000046
47
48 // PR4633
49 int y, x;
50 ((void)0), y = x;
Eli Friedman4be1f472008-05-19 21:24:43 +000051}
Chris Lattnerfb846642009-07-28 18:25:28 +000052
Anders Carlsson75443112009-07-30 22:39:03 +000053void t4(int a) {
54 int b = 0;
55
56 if (a)
Stephen Hines651f13c2014-04-23 16:59:28 -070057 b < 1; // expected-warning{{relational comparison result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000058 else
Stephen Hines651f13c2014-04-23 16:59:28 -070059 b < 2; // expected-warning{{relational comparison result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000060
61 while (1)
Stephen Hines651f13c2014-04-23 16:59:28 -070062 b < 3; // expected-warning{{relational comparison result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000063
64 do
Stephen Hines651f13c2014-04-23 16:59:28 -070065 b < 4; // expected-warning{{relational comparison result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000066 while (1);
67
68 for (;;)
Stephen Hines651f13c2014-04-23 16:59:28 -070069 b < 5; // expected-warning{{relational comparison result unused}}
Anders Carlsson3af708f2009-08-01 01:39:59 +000070
Stephen Hines651f13c2014-04-23 16:59:28 -070071 for (b < 1;;) {} // expected-warning{{relational comparison result unused}}
Chandler Carruthec8058f2011-08-17 09:34:37 +000072 for (;b < 1;) {}
Stephen Hines651f13c2014-04-23 16:59:28 -070073 for (;;b < 1) {} // expected-warning{{relational comparison result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000074}
75
Chris Lattnerbc8d42c2009-10-13 04:53:48 +000076// rdar://7186119
77int t5f(void) __attribute__((warn_unused_result));
78void t5() {
79 t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}}
80}
Chris Lattner9079cd32009-10-13 04:56:49 +000081
82
83int fn1() __attribute__ ((warn_unused_result));
84int fn2() __attribute__ ((pure));
Fariborz Jahaniana7846852012-08-13 18:04:58 +000085int fn3() __attribute__ ((__const));
Chris Lattner7909fee2009-10-13 04:57:27 +000086// rdar://6587766
Chris Lattner9079cd32009-10-13 04:56:49 +000087int t6() {
88 if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings
89 return -1;
Nuno Lopesf8577982009-12-22 23:59:52 +000090
Chris Lattner9079cd32009-10-13 04:56:49 +000091 fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
92 fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}}
93 fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}}
Stephen Hines651f13c2014-04-23 16:59:28 -070094 __builtin_abs(0); // expected-warning {{ignoring return value of function declared with const attribute}}
Eli Friedmana6115062012-05-24 00:47:05 +000095 (void)0, fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
Chris Lattner9079cd32009-10-13 04:56:49 +000096 return 0;
97}
98
John McCalld6b8de02011-01-25 03:51:08 +000099int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}}
Chris Lattner9079cd32009-10-13 04:56:49 +0000100
Nuno Lopesd20254f2009-12-20 23:11:08 +0000101// PR4010
102int (*fn4)(void) __attribute__ ((warn_unused_result));
103void t8() {
104 fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
105}
Nuno Lopesf8577982009-12-22 23:59:52 +0000106
107void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}
John McCall209acbd2010-04-06 22:24:14 +0000108
109// rdar://7410924
110void *some_function(void);
111void t10() {
112 (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
113}
Douglas Gregorc6dfe192010-05-08 22:41:50 +0000114
115void f(int i, ...) {
116 __builtin_va_list ap;
117
118 __builtin_va_start(ap, i);
119 __builtin_va_arg(ap, int);
120 __builtin_va_end(ap);
121}
Gabor Greif1f4295b2010-10-15 08:44:44 +0000122
123// PR8371
124int fn5() __attribute__ ((__const));
Matt Beaumont-Gayc7db84d2012-10-23 23:19:32 +0000125
Matt Beaumont-Gay87b73ba2013-01-17 02:06:08 +0000126// Don't warn for unused expressions in macro bodies; however, do warn for
127// unused expressions in macro arguments. Macros below are reduced from code
128// found in the wild.
129#define NOP(a) (a)
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +0000130#define M1(a, b) (long)foo((a), (b))
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +0000131#define M2 (long)0;
Matt Beaumont-Gay87b73ba2013-01-17 02:06:08 +0000132#define M3(a) (t3(a), fn2())
133#define M4(a, b) (foo((a), (b)) ? 0 : t3(a), 1)
134#define M5(a, b) (foo((a), (b)), 1)
Matt Beaumont-Gay9016bb72013-02-26 19:34:08 +0000135#define M6() fn1()
136#define M7() fn2()
Matt Beaumont-Gayc7db84d2012-10-23 23:19:32 +0000137void t11(int i, int j) {
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +0000138 M1(i, j); // no warning
Matt Beaumont-Gay87b73ba2013-01-17 02:06:08 +0000139 NOP((long)foo(i, j)); // expected-warning {{expression result unused}}
140 M2; // no warning
141 NOP((long)0); // expected-warning {{expression result unused}}
142 M3(i); // no warning
143 NOP((t3(i), fn2())); // expected-warning {{ignoring return value}}
144 M4(i, j); // no warning
145 NOP((foo(i, j) ? 0 : t3(i), 1)); // expected-warning {{expression result unused}}
146 M5(i, j); // no warning
147 NOP((foo(i, j), 1)); // expected-warning {{expression result unused}}
Matt Beaumont-Gay9016bb72013-02-26 19:34:08 +0000148 M6(); // expected-warning {{ignoring return value}}
149 M7(); // no warning
Matt Beaumont-Gayc7db84d2012-10-23 23:19:32 +0000150}
Matt Beaumont-Gay87b73ba2013-01-17 02:06:08 +0000151#undef NOP
Matt Beaumont-Gay6d919fb2012-10-24 01:14:28 +0000152#undef M1
153#undef M2
Matt Beaumont-Gay87b73ba2013-01-17 02:06:08 +0000154#undef M3
155#undef M4
156#undef M5
Matt Beaumont-Gay9016bb72013-02-26 19:34:08 +0000157#undef M6
158#undef M7