Argyrios Kyrtzidis | 1b2ad2f | 2010-09-19 23:03:35 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2 | |
| 3 | int foo(int X, int Y); |
| 4 | |
Dan Gohman | c31176d | 2010-01-08 02:20:44 +0000 | [diff] [blame] | 5 | double sqrt(double X); // implicitly const because of no -fmath-errno! |
Chris Lattner | 5bef8dd | 2009-02-17 00:35:09 +0000 | [diff] [blame] | 6 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | void bar(volatile int *VP, int *P, int A, |
| 8 | _Complex double C, volatile _Complex double VC) { |
| 9 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 10 | VP < P; // expected-warning {{relational comparison result unused}} |
Chris Lattner | 6e844ad | 2007-08-26 17:32:59 +0000 | [diff] [blame] | 11 | (void)A; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 12 | (void)foo(1,2); // no warning. |
| 13 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 14 | A < foo(1, 2); // expected-warning {{relational comparison result unused}} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | |
| 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 Lattner | 7d84c76 | 2009-02-17 00:32:04 +0000 | [diff] [blame] | 24 | __real__ C; // expected-warning {{expression result unused}} |
| 25 | __real__ VC; |
Chris Lattner | 5bef8dd | 2009-02-17 00:35:09 +0000 | [diff] [blame] | 26 | |
Dan Gohman | c31176d | 2010-01-08 02:20:44 +0000 | [diff] [blame] | 27 | // We know this can't change errno because of no -fmath-errno. |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 28 | sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Chris Lattner | 6e844ad | 2007-08-26 17:32:59 +0000 | [diff] [blame] | 31 | extern void t1(); |
| 32 | extern void t2(); |
| 33 | void t3(int c) { |
| 34 | c ? t1() : t2(); |
| 35 | } |
| 36 | |
Chris Lattner | 98414c1 | 2007-08-31 21:49:55 +0000 | [diff] [blame] | 37 | // This shouldn't warn: the expr at the end of the stmtexpr really is used. |
| 38 | int stmt_expr(int x, int y) { |
| 39 | return ({int _a = x, _b = y; _a > _b ? _a : _b; }); |
| 40 | } |
| 41 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 42 | void nowarn(unsigned char* a, unsigned char* b) |
| 43 | { |
| 44 | unsigned char c = 1; |
| 45 | *a |= c, *b += c; |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | // PR4633 |
| 49 | int y, x; |
| 50 | ((void)0), y = x; |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 52 | |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 53 | void t4(int a) { |
| 54 | int b = 0; |
| 55 | |
| 56 | if (a) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 57 | b < 1; // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 58 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 59 | b < 2; // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 60 | |
| 61 | while (1) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 62 | b < 3; // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 63 | |
| 64 | do |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 65 | b < 4; // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 66 | while (1); |
| 67 | |
| 68 | for (;;) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 69 | b < 5; // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 3af708f | 2009-08-01 01:39:59 +0000 | [diff] [blame] | 70 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 71 | for (b < 1;;) {} // expected-warning{{relational comparison result unused}} |
Chandler Carruth | ec8058f | 2011-08-17 09:34:37 +0000 | [diff] [blame] | 72 | for (;b < 1;) {} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 73 | for (;;b < 1) {} // expected-warning{{relational comparison result unused}} |
Anders Carlsson | 7544311 | 2009-07-30 22:39:03 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 76 | // rdar://7186119 |
| 77 | int t5f(void) __attribute__((warn_unused_result)); |
| 78 | void t5() { |
| 79 | t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}} |
| 80 | } |
Chris Lattner | 9079cd3 | 2009-10-13 04:56:49 +0000 | [diff] [blame] | 81 | |
| 82 | |
| 83 | int fn1() __attribute__ ((warn_unused_result)); |
| 84 | int fn2() __attribute__ ((pure)); |
Fariborz Jahanian | a784685 | 2012-08-13 18:04:58 +0000 | [diff] [blame] | 85 | int fn3() __attribute__ ((__const)); |
Chris Lattner | 7909fee | 2009-10-13 04:57:27 +0000 | [diff] [blame] | 86 | // rdar://6587766 |
Chris Lattner | 9079cd3 | 2009-10-13 04:56:49 +0000 | [diff] [blame] | 87 | int t6() { |
| 88 | if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings |
| 89 | return -1; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 9079cd3 | 2009-10-13 04:56:49 +0000 | [diff] [blame] | 91 | 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 Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 94 | __builtin_abs(0); // expected-warning {{ignoring return value of function declared with const attribute}} |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 95 | (void)0, fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}} |
Chris Lattner | 9079cd3 | 2009-10-13 04:56:49 +0000 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
John McCall | d6b8de0 | 2011-01-25 03:51:08 +0000 | [diff] [blame] | 99 | int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}} |
Chris Lattner | 9079cd3 | 2009-10-13 04:56:49 +0000 | [diff] [blame] | 100 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 101 | // PR4010 |
| 102 | int (*fn4)(void) __attribute__ ((warn_unused_result)); |
| 103 | void t8() { |
| 104 | fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}} |
| 105 | } |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 106 | |
| 107 | void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}} |
John McCall | 209acbd | 2010-04-06 22:24:14 +0000 | [diff] [blame] | 108 | |
| 109 | // rdar://7410924 |
| 110 | void *some_function(void); |
| 111 | void t10() { |
| 112 | (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}} |
| 113 | } |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 114 | |
| 115 | void 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 Greif | 1f4295b | 2010-10-15 08:44:44 +0000 | [diff] [blame] | 122 | |
| 123 | // PR8371 |
| 124 | int fn5() __attribute__ ((__const)); |
Matt Beaumont-Gay | c7db84d | 2012-10-23 23:19:32 +0000 | [diff] [blame] | 125 | |
Matt Beaumont-Gay | 87b73ba | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 126 | // 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-Gay | 6d919fb | 2012-10-24 01:14:28 +0000 | [diff] [blame] | 130 | #define M1(a, b) (long)foo((a), (b)) |
Matt Beaumont-Gay | 6d919fb | 2012-10-24 01:14:28 +0000 | [diff] [blame] | 131 | #define M2 (long)0; |
Matt Beaumont-Gay | 87b73ba | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 132 | #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-Gay | 9016bb7 | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 135 | #define M6() fn1() |
| 136 | #define M7() fn2() |
Matt Beaumont-Gay | c7db84d | 2012-10-23 23:19:32 +0000 | [diff] [blame] | 137 | void t11(int i, int j) { |
Matt Beaumont-Gay | 6d919fb | 2012-10-24 01:14:28 +0000 | [diff] [blame] | 138 | M1(i, j); // no warning |
Matt Beaumont-Gay | 87b73ba | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 139 | 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-Gay | 9016bb7 | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 148 | M6(); // expected-warning {{ignoring return value}} |
| 149 | M7(); // no warning |
Matt Beaumont-Gay | c7db84d | 2012-10-23 23:19:32 +0000 | [diff] [blame] | 150 | } |
Matt Beaumont-Gay | 87b73ba | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 151 | #undef NOP |
Matt Beaumont-Gay | 6d919fb | 2012-10-24 01:14:28 +0000 | [diff] [blame] | 152 | #undef M1 |
| 153 | #undef M2 |
Matt Beaumont-Gay | 87b73ba | 2013-01-17 02:06:08 +0000 | [diff] [blame] | 154 | #undef M3 |
| 155 | #undef M4 |
| 156 | #undef M5 |
Matt Beaumont-Gay | 9016bb7 | 2013-02-26 19:34:08 +0000 | [diff] [blame] | 157 | #undef M6 |
| 158 | #undef M7 |