blob: 97611168f1a2ebdcf2e2d73cd7ac41f172b22f87 [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
Chandler Carruthec8058f2011-08-17 09:34:37 +000010 VP < P; // expected-warning {{expression 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
Chandler Carruthec8058f2011-08-17 09:34:37 +000014 A < foo(1, 2); // expected-warning {{expression 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)
Chandler Carruthec8058f2011-08-17 09:34:37 +000057 b < 1; // expected-warning{{expression result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000058 else
Chandler Carruthec8058f2011-08-17 09:34:37 +000059 b < 2; // expected-warning{{expression result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000060
61 while (1)
Chandler Carruthec8058f2011-08-17 09:34:37 +000062 b < 3; // expected-warning{{expression result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000063
64 do
Chandler Carruthec8058f2011-08-17 09:34:37 +000065 b < 4; // expected-warning{{expression result unused}}
Anders Carlsson75443112009-07-30 22:39:03 +000066 while (1);
67
68 for (;;)
Chandler Carruthec8058f2011-08-17 09:34:37 +000069 b < 5; // expected-warning{{expression result unused}}
Anders Carlsson3af708f2009-08-01 01:39:59 +000070
Chandler Carruthec8058f2011-08-17 09:34:37 +000071 for (b < 1;;) {} // expected-warning{{expression result unused}}
72 for (;b < 1;) {}
73 for (;;b < 1) {} // expected-warning{{expression 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));
85int 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}}
Chris Lattner634785c2009-12-30 22:10:22 +000094 __builtin_fabsf(0); // expected-warning {{ignoring return value of function declared with const attribute}}
Chris Lattner9079cd32009-10-13 04:56:49 +000095 return 0;
96}
97
John McCalld6b8de02011-01-25 03:51:08 +000098int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}}
Chris Lattner9079cd32009-10-13 04:56:49 +000099
Nuno Lopesd20254f2009-12-20 23:11:08 +0000100// PR4010
101int (*fn4)(void) __attribute__ ((warn_unused_result));
102void t8() {
103 fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
104}
Nuno Lopesf8577982009-12-22 23:59:52 +0000105
106void 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 +0000107
108// rdar://7410924
109void *some_function(void);
110void t10() {
111 (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
112}
Douglas Gregorc6dfe192010-05-08 22:41:50 +0000113
114void f(int i, ...) {
115 __builtin_va_list ap;
116
117 __builtin_va_start(ap, i);
118 __builtin_va_arg(ap, int);
119 __builtin_va_end(ap);
120}
Gabor Greif1f4295b2010-10-15 08:44:44 +0000121
122// PR8371
123int fn5() __attribute__ ((__const));