1f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code
2f4a2713aSLionel Sambuc
3f4a2713aSLionel Sambuc int foo(int X, int Y);
4f4a2713aSLionel Sambuc
5f4a2713aSLionel Sambuc double sqrt(double X); // implicitly const because of no -fmath-errno!
6f4a2713aSLionel Sambuc
bar(volatile int * VP,int * P,int A,_Complex double C,volatile _Complex double VC)7f4a2713aSLionel Sambuc void bar(volatile int *VP, int *P, int A,
8f4a2713aSLionel Sambuc _Complex double C, volatile _Complex double VC) {
9f4a2713aSLionel Sambuc
10*0a6a1f1dSLionel Sambuc VP < P; // expected-warning {{relational comparison result unused}}
11f4a2713aSLionel Sambuc (void)A;
12f4a2713aSLionel Sambuc (void)foo(1,2); // no warning.
13f4a2713aSLionel Sambuc
14*0a6a1f1dSLionel Sambuc A < foo(1, 2); // expected-warning {{relational comparison result unused}}
15f4a2713aSLionel Sambuc
16f4a2713aSLionel Sambuc foo(1,2)+foo(4,3); // expected-warning {{expression result unused}}
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc
19f4a2713aSLionel Sambuc *P; // expected-warning {{expression result unused}}
20f4a2713aSLionel Sambuc *VP; // no warning.
21f4a2713aSLionel Sambuc P[4]; // expected-warning {{expression result unused}}
22f4a2713aSLionel Sambuc VP[4]; // no warning.
23f4a2713aSLionel Sambuc
24f4a2713aSLionel Sambuc __real__ C; // expected-warning {{expression result unused}}
25f4a2713aSLionel Sambuc __real__ VC;
26f4a2713aSLionel Sambuc
27f4a2713aSLionel Sambuc // We know this can't change errno because of no -fmath-errno.
28f4a2713aSLionel Sambuc sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}}
29f4a2713aSLionel Sambuc }
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc extern void t1();
32f4a2713aSLionel Sambuc extern void t2();
t3(int c)33f4a2713aSLionel Sambuc void t3(int c) {
34f4a2713aSLionel Sambuc c ? t1() : t2();
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc // This shouldn't warn: the expr at the end of the stmtexpr really is used.
stmt_expr(int x,int y)38f4a2713aSLionel Sambuc int stmt_expr(int x, int y) {
39f4a2713aSLionel Sambuc return ({int _a = x, _b = y; _a > _b ? _a : _b; });
40f4a2713aSLionel Sambuc }
41f4a2713aSLionel Sambuc
nowarn(unsigned char * a,unsigned char * b)42f4a2713aSLionel Sambuc void nowarn(unsigned char* a, unsigned char* b)
43f4a2713aSLionel Sambuc {
44f4a2713aSLionel Sambuc unsigned char c = 1;
45f4a2713aSLionel Sambuc *a |= c, *b += c;
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc
48f4a2713aSLionel Sambuc // PR4633
49f4a2713aSLionel Sambuc int y, x;
50f4a2713aSLionel Sambuc ((void)0), y = x;
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc
t4(int a)53f4a2713aSLionel Sambuc void t4(int a) {
54f4a2713aSLionel Sambuc int b = 0;
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc if (a)
57*0a6a1f1dSLionel Sambuc b < 1; // expected-warning{{relational comparison result unused}}
58f4a2713aSLionel Sambuc else
59*0a6a1f1dSLionel Sambuc b < 2; // expected-warning{{relational comparison result unused}}
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc while (1)
62*0a6a1f1dSLionel Sambuc b < 3; // expected-warning{{relational comparison result unused}}
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc do
65*0a6a1f1dSLionel Sambuc b < 4; // expected-warning{{relational comparison result unused}}
66f4a2713aSLionel Sambuc while (1);
67f4a2713aSLionel Sambuc
68f4a2713aSLionel Sambuc for (;;)
69*0a6a1f1dSLionel Sambuc b < 5; // expected-warning{{relational comparison result unused}}
70f4a2713aSLionel Sambuc
71*0a6a1f1dSLionel Sambuc for (b < 1;;) {} // expected-warning{{relational comparison result unused}}
72f4a2713aSLionel Sambuc for (;b < 1;) {}
73*0a6a1f1dSLionel Sambuc for (;;b < 1) {} // expected-warning{{relational comparison result unused}}
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc // rdar://7186119
77f4a2713aSLionel Sambuc int t5f(void) __attribute__((warn_unused_result));
t5()78f4a2713aSLionel Sambuc void t5() {
79f4a2713aSLionel Sambuc t5f(); // expected-warning {{ignoring return value of function declared with warn_unused_result}}
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc
82f4a2713aSLionel Sambuc
83f4a2713aSLionel Sambuc int fn1() __attribute__ ((warn_unused_result));
84f4a2713aSLionel Sambuc int fn2() __attribute__ ((pure));
85f4a2713aSLionel Sambuc int fn3() __attribute__ ((__const));
86f4a2713aSLionel Sambuc // rdar://6587766
t6()87f4a2713aSLionel Sambuc int t6() {
88f4a2713aSLionel Sambuc if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings
89f4a2713aSLionel Sambuc return -1;
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
92f4a2713aSLionel Sambuc fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}}
93f4a2713aSLionel Sambuc fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}}
94*0a6a1f1dSLionel Sambuc __builtin_abs(0); // expected-warning {{ignoring return value of function declared with const attribute}}
95f4a2713aSLionel Sambuc (void)0, fn1(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
96f4a2713aSLionel Sambuc return 0;
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc
99f4a2713aSLionel Sambuc int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}}
100f4a2713aSLionel Sambuc
101f4a2713aSLionel Sambuc // PR4010
102f4a2713aSLionel Sambuc int (*fn4)(void) __attribute__ ((warn_unused_result));
t8()103f4a2713aSLionel Sambuc void t8() {
104f4a2713aSLionel Sambuc fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc
107f4a2713aSLionel Sambuc void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}
108f4a2713aSLionel Sambuc
109f4a2713aSLionel Sambuc // rdar://7410924
110f4a2713aSLionel Sambuc void *some_function(void);
t10()111f4a2713aSLionel Sambuc void t10() {
112f4a2713aSLionel Sambuc (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
113f4a2713aSLionel Sambuc }
114f4a2713aSLionel Sambuc
f(int i,...)115f4a2713aSLionel Sambuc void f(int i, ...) {
116f4a2713aSLionel Sambuc __builtin_va_list ap;
117f4a2713aSLionel Sambuc
118f4a2713aSLionel Sambuc __builtin_va_start(ap, i);
119f4a2713aSLionel Sambuc __builtin_va_arg(ap, int);
120f4a2713aSLionel Sambuc __builtin_va_end(ap);
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
123f4a2713aSLionel Sambuc // PR8371
124f4a2713aSLionel Sambuc int fn5() __attribute__ ((__const));
125f4a2713aSLionel Sambuc
126f4a2713aSLionel Sambuc // Don't warn for unused expressions in macro bodies; however, do warn for
127f4a2713aSLionel Sambuc // unused expressions in macro arguments. Macros below are reduced from code
128f4a2713aSLionel Sambuc // found in the wild.
129f4a2713aSLionel Sambuc #define NOP(a) (a)
130f4a2713aSLionel Sambuc #define M1(a, b) (long)foo((a), (b))
131f4a2713aSLionel Sambuc #define M2 (long)0;
132f4a2713aSLionel Sambuc #define M3(a) (t3(a), fn2())
133f4a2713aSLionel Sambuc #define M4(a, b) (foo((a), (b)) ? 0 : t3(a), 1)
134f4a2713aSLionel Sambuc #define M5(a, b) (foo((a), (b)), 1)
135f4a2713aSLionel Sambuc #define M6() fn1()
136f4a2713aSLionel Sambuc #define M7() fn2()
t11(int i,int j)137f4a2713aSLionel Sambuc void t11(int i, int j) {
138f4a2713aSLionel Sambuc M1(i, j); // no warning
139f4a2713aSLionel Sambuc NOP((long)foo(i, j)); // expected-warning {{expression result unused}}
140f4a2713aSLionel Sambuc M2; // no warning
141f4a2713aSLionel Sambuc NOP((long)0); // expected-warning {{expression result unused}}
142f4a2713aSLionel Sambuc M3(i); // no warning
143f4a2713aSLionel Sambuc NOP((t3(i), fn2())); // expected-warning {{ignoring return value}}
144f4a2713aSLionel Sambuc M4(i, j); // no warning
145f4a2713aSLionel Sambuc NOP((foo(i, j) ? 0 : t3(i), 1)); // expected-warning {{expression result unused}}
146f4a2713aSLionel Sambuc M5(i, j); // no warning
147f4a2713aSLionel Sambuc NOP((foo(i, j), 1)); // expected-warning {{expression result unused}}
148f4a2713aSLionel Sambuc M6(); // expected-warning {{ignoring return value}}
149f4a2713aSLionel Sambuc M7(); // no warning
150f4a2713aSLionel Sambuc }
151f4a2713aSLionel Sambuc #undef NOP
152f4a2713aSLionel Sambuc #undef M1
153f4a2713aSLionel Sambuc #undef M2
154f4a2713aSLionel Sambuc #undef M3
155f4a2713aSLionel Sambuc #undef M4
156f4a2713aSLionel Sambuc #undef M5
157f4a2713aSLionel Sambuc #undef M6
158f4a2713aSLionel Sambuc #undef M7
159