summaryrefslogtreecommitdiff
path: root/test/Sema/warn-integer-constants-in-ternary.c
blob: 95c78d272794ef3f171d92bb558789c1abb36bdf (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
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wtautological-constant-compare %s
// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wtautological-constant-compare %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s

#define ONE 1
#define TWO 2

#define TERN(c, l, r) c ? l : r

#ifdef __cplusplus
typedef bool boolean;
#else
typedef _Bool boolean;
#endif

void test(boolean a) {
  boolean r;
  r = a ? (1) : TWO;
  r = a ? 3 : TWO; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
  r = a ? -2 : 0;
  r = a ? 3 : -2;  // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
  r = a ? 0 : TWO;
  r = a ? 3 : ONE; // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
  r = a ? ONE : 0;
  r = a ? 0 : -0;
  r = a ? 1 : 0;
  r = a ? ONE : 0;
  r = a ? ONE : ONE;
  r = TERN(a, 4, 8);   // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
  r = TERN(a, -1, -8); // expected-warning {{converting the result of '?:' with integer constants to a boolean always evaluates to 'true'}}
}