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