summaryrefslogtreecommitdiffstats
path: root/test/Sema/parentheses.c
blob: fa3c3c26b04218eb40892fe7bd65d0192525d181 (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
// RUN: %clang_cc1 -Wparentheses -fsyntax-only -verify %s
// RUN: %clang_cc1 -Wparentheses -fixit %s -o - | %clang_cc1 -Wparentheses -Werror -

// Test the various warnings under -Wparentheses
void if_assign(void) {
  int i;
  if (i = 4) {} // expected-warning {{assignment as a condition}} \
                // expected-note{{use '==' to turn this assignment into an equality comparison}} \
  // expected-note{{place parentheses around the assignment to silence this warning}}
  if ((i = 4)) {}
}

void bitwise_rel(unsigned i) {
  (void)(i & 0x2 == 0); // expected-warning {{& has lower precedence than ==}} \
                        // expected-note{{place parentheses around the & expression to evaluate it first}} \
  // expected-note{{place parentheses around the == expression to silence this warning}}
  (void)(0 == i & 0x2); // expected-warning {{& has lower precedence than ==}} \
                        // expected-note{{place parentheses around the & expression to evaluate it first}} \
  // expected-note{{place parentheses around the == expression to silence this warning}}
  (void)(i & 0xff < 30); // expected-warning {{& has lower precedence than <}} \
                        // expected-note{{place parentheses around the & expression to evaluate it first}} \
  // expected-note{{place parentheses around the < expression to silence this warning}}
  (void)((i & 0x2) == 0);
  (void)(i & (0x2 == 0));
  // Eager logical op
  (void)(i == 1 | i == 2 | i == 3);
  (void)(i != 1 & i != 2 & i != 3);

  (void)(i ||
             i && i); // expected-warning {{'&&' within '||'}} \
                       // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  (void)(i || i && "w00t"); // no warning.
  (void)("w00t" && i || i); // no warning.
  (void)(i || i && "w00t" || i); // expected-warning {{'&&' within '||'}} \
                                 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  (void)(i || "w00t" && i || i); // expected-warning {{'&&' within '||'}} \
                                 // expected-note {{place parentheses around the '&&' expression to silence this warning}}
  (void)(i && i || 0); // no warning.
  (void)(0 || i && i); // no warning.
}

_Bool someConditionFunc();

void conditional_op(int x, int y, _Bool b) {
  (void)(x + someConditionFunc() ? 1 : 2); // expected-warning {{?: has lower precedence than +}} \
                                           // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
                                           // expected-note {{place parentheses around the + expression to silence this warning}}

  (void)(x - b ? 1 : 2); // expected-warning {{?: has lower precedence than -}} \
                         // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
                         // expected-note {{place parentheses around the - expression to silence this warning}}

  (void)(x * (x == y) ? 1 : 2); // expected-warning {{?: has lower precedence than *}} \
                                // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
                                // expected-note {{place parentheses around the * expression to silence this warning}}

  (void)(x / !x ? 1 : 2); // expected-warning {{?: has lower precedence than /}} \
                          // expected-note {{place parentheses around the ?: expression to evaluate it first}} \
                          // expected-note {{place parentheses around the / expression to silence this warning}}


  (void)(x % 2 ? 1 : 2); // no warning
}

// RUN: %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s
// CHECK: error: using the result of an assignment as a condition without parentheses [-Werror,-Wparentheses]
OpenPOWER on IntegriCloud