summaryrefslogtreecommitdiff
path: root/clang/test/Sema/unused-expr.c
blob: 97611168f1a2ebdcf2e2d73cd7ac41f172b22f87 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// RUN: %clang_cc1 -fsyntax-only -verify %s -Wno-unreachable-code

int foo(int X, int Y);

double sqrt(double X);  // implicitly const because of no -fmath-errno!

void bar(volatile int *VP, int *P, int A,
         _Complex double C, volatile _Complex double VC) {
  
  VP < P;              // expected-warning {{expression result unused}}
  (void)A;
  (void)foo(1,2);      // no warning.
  
  A < foo(1, 2);       // expected-warning {{expression result unused}}

  foo(1,2)+foo(4,3);   // expected-warning {{expression result unused}}


  *P;                  // expected-warning {{expression result unused}}
  *VP;                 // no warning.
  P[4];                // expected-warning {{expression result unused}}
  VP[4];               // no warning.

  __real__ C;          // expected-warning {{expression result unused}}
  __real__ VC;
  
  // We know this can't change errno because of no -fmath-errno.
  sqrt(A);  // expected-warning {{ignoring return value of function declared with const attribute}}
}

extern void t1();
extern void t2();
void t3(int c) {
  c ? t1() : t2();
}

// This shouldn't warn: the expr at the end of the stmtexpr really is used.
int stmt_expr(int x, int y) {
  return ({int _a = x, _b = y; _a > _b ? _a : _b; });
}

void nowarn(unsigned char* a, unsigned char* b)
{
  unsigned char c = 1;
  *a |= c, *b += c;


  // PR4633
  int y, x;
  ((void)0), y = x;
}

void t4(int a) {
  int b = 0;

  if (a)
    b < 1; // expected-warning{{expression result unused}}
  else
    b < 2; // expected-warning{{expression result unused}}
    
  while (1)
    b < 3; // expected-warning{{expression result unused}}

  do
    b < 4; // expected-warning{{expression result unused}}
  while (1);
  
  for (;;)
    b < 5; // expected-warning{{expression result unused}}
    
  for (b < 1;;) {} // expected-warning{{expression result unused}}
  for (;b < 1;) {}
  for (;;b < 1) {} // expected-warning{{expression result unused}}
}

// rdar://7186119
int t5f(void) __attribute__((warn_unused_result));
void t5() {
  t5f();   // expected-warning {{ignoring return value of function declared with warn_unused_result}}
}


int fn1() __attribute__ ((warn_unused_result));
int fn2() __attribute__ ((pure));
int fn3() __attribute__ ((const));
// rdar://6587766
int t6() {
  if (fn1() < 0 || fn2(2,1) < 0 || fn3(2) < 0)  // no warnings
    return -1;

  fn1();  // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
  fn2(92, 21);  // expected-warning {{ignoring return value of function declared with pure attribute}}
  fn3(42);  // expected-warning {{ignoring return value of function declared with const attribute}}
  __builtin_fabsf(0); // expected-warning {{ignoring return value of function declared with const attribute}}
  return 0;
}

int t7 __attribute__ ((warn_unused_result)); // expected-warning {{'warn_unused_result' attribute only applies to functions}}

// PR4010
int (*fn4)(void) __attribute__ ((warn_unused_result));
void t8() {
  fn4(); // expected-warning {{ignoring return value of function declared with warn_unused_result attribute}}
}

void t9() __attribute__((warn_unused_result)); // expected-warning {{attribute 'warn_unused_result' cannot be applied to functions without return value}}

// rdar://7410924
void *some_function(void);
void t10() {
  (void*) some_function(); //expected-warning {{expression result unused; should this cast be to 'void'?}}
}

void f(int i, ...) {
    __builtin_va_list ap;
    
    __builtin_va_start(ap, i);
    __builtin_va_arg(ap, int);
    __builtin_va_end(ap);
}

// PR8371
int fn5() __attribute__ ((__const));