annotate clang/test/Sema/unused-expr.c @ 222:81f6424ef0e3 llvm-original

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