164 lines · c
1// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code -Wno-strict-prototypes2 3int foo(int X, int Y);4 5double sqrt(double X); // implicitly const because of no -fmath-errno!6 7void bar(volatile int *VP, int *P, int A,8 _Complex double C, volatile _Complex double VC) {9 10 VP < P; // expected-warning {{relational comparison result unused}}11 (void)A;12 (void)foo(1,2); // no warning.13 14 A < foo(1, 2); // expected-warning {{relational comparison result unused}}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 24 __real__ C; // expected-warning {{expression result unused}}25 __real__ VC;26 27 // We know this can't change errno because of no -fmath-errno.28 sqrt(A); // expected-warning {{ignoring return value of function declared with const attribute}}29}30 31extern void t1(void);32extern void t2(void);33void t3(int c) {34 c ? t1() : t2();35}36 37// 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 42void nowarn(unsigned char* a, unsigned char* b)43{44 unsigned char c = 1;45 *a |= c, *b += c;46 47 48 // PR463349 int y, x;50 ((void)0), y = x;51}52 53void t4(int a) {54 int b = 0;55 56 if (a)57 b < 1; // expected-warning{{relational comparison result unused}}58 else59 b < 2; // expected-warning{{relational comparison result unused}}60 61 while (1)62 b < 3; // expected-warning{{relational comparison result unused}}63 64 do65 b < 4; // expected-warning{{relational comparison result unused}}66 while (1);67 68 for (;;)69 b < 5; // expected-warning{{relational comparison result unused}}70 71 for (b < 1;;) {} // expected-warning{{relational comparison result unused}}72 for (;b < 1;) {}73 for (;;b < 1) {} // expected-warning{{relational comparison result unused}}74}75 76int t5f(void) __attribute__((warn_unused_result));77void t5(void) {78 t5f(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}79}80 81 82int fn1(void) __attribute__ ((warn_unused_result));83int fn2() __attribute__ ((pure));84int fn3() __attribute__ ((__const));85int t6(void) {86 if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0) // no warnings87 return -1;88 89 fn1(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}90 fn2(92, 21); // expected-warning {{ignoring return value of function declared with pure attribute}}91 fn3(42); // expected-warning {{ignoring return value of function declared with const attribute}}92 __builtin_abs(0); // expected-warning {{ignoring return value of function declared with const attribute}}93 (void)0, fn1(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}94 return 0;95}96 97int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to Objective-C methods, enums, structs, unions, classes, functions, function pointers, and typedefs}}98 99// PR4010100int (*fn4)(void) __attribute__ ((warn_unused_result));101void t8(void) {102 fn4(); // expected-warning {{ignoring return value of function declared with 'warn_unused_result' attribute}}103}104 105void t9(void) __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}106 107void *some_function(void);108void t10(void) {109 (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}110}111 112void f(int i, ...) {113 __builtin_va_list ap;114 115 __builtin_va_start(ap, i);116 __builtin_va_arg(ap, int);117 __builtin_va_end(ap);118}119 120// PR8371121int fn5(void) __attribute__ ((__const));122 123// Don't warn for unused expressions in macro bodies; however, do warn for124// unused expressions in macro arguments. Macros below are reduced from code125// found in the wild.126#define NOP(a) (a)127#define M1(a, b) (long)foo((a), (b))128#define M2 (long)0;129#define M3(a) (t3(a), fn2())130#define M4(a, b) (foo((a), (b)) ? 0 : t3(a), 1)131#define M5(a, b) (foo((a), (b)), 1)132#define M6() fn1()133#define M7() fn2()134void t11(int i, int j) {135 M1(i, j); // no warning136 NOP((long)foo(i, j)); // expected-warning {{expression result unused}}137 M2; // no warning138 NOP((long)0); // expected-warning {{expression result unused}}139 M3(i); // no warning140 NOP((t3(i), fn2())); // expected-warning {{ignoring return value}}141 M4(i, j); // no warning142 NOP((foo(i, j) ? 0 : t3(i), 1)); // expected-warning {{expression result unused}}143 M5(i, j); // no warning144 NOP((foo(i, j), 1)); // expected-warning {{expression result unused}}145 M6(); // expected-warning {{ignoring return value}}146 M7(); // no warning147}148#undef NOP149#undef M1150#undef M2151#undef M3152#undef M4153#undef M5154#undef M6155#undef M7156 157#define UNREFERENCED_PARAMETER(x) (x)158 159void unused_parm(int a) {160 // Don't warn if the warning is introduced by a macro that's spelled161 // UNREFERENCED_PARAMETER, as that's a commonly used macro in Windows headers.162 UNREFERENCED_PARAMETER(a);163}164