summaryrefslogtreecommitdiff
path: root/clang/test/Analysis/constant-folding.c
blob: e7a5705fa59aefe11a4b40b54d7028f5d0c58d7f (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
// RUN: %clang_cc1 -analyze -analyzer-checker=core,experimental.deadcode.UnreachableCode -Wno-null-dereference -verify %s

// Trigger a warning if the analyzer reaches this point in the control flow.
#define WARN ((void)*(char*)0)

// There should be no warnings unless otherwise indicated.

void testComparisons (int a) {
  // Sema can already catch the simple comparison a==a,
  // since that's usually a logic error (and not path-dependent).
  int b = a;
  if (!(b==a)) WARN; // expected-warning{{never executed}}
  if (!(b>=a)) WARN; // expected-warning{{never executed}}
  if (!(b<=a)) WARN; // expected-warning{{never executed}}
  if (b!=a) WARN;    // expected-warning{{never executed}}
  if (b>a) WARN;     // expected-warning{{never executed}}
  if (b<a) WARN;     // expected-warning{{never executed}}
}

void testSelfOperations (int a) {
  if ((a|a) != a) WARN; // expected-warning{{never executed}}
  if ((a&a) != a) WARN; // expected-warning{{never executed}}
  if ((a^a) != 0) WARN; // expected-warning{{never executed}}
  if ((a-a) != 0) WARN; // expected-warning{{never executed}}
}

void testIdempotent (int a) {
  if ((a*1) != a) WARN;    // expected-warning{{never executed}}
  if ((a/1) != a) WARN;    // expected-warning{{never executed}}
  if ((a+0) != a) WARN;    // expected-warning{{never executed}}
  if ((a-0) != a) WARN;    // expected-warning{{never executed}}
  if ((a<<0) != a) WARN;   // expected-warning{{never executed}}
  if ((a>>0) != a) WARN;   // expected-warning{{never executed}}
  if ((a^0) != a) WARN;    // expected-warning{{never executed}}
  if ((a&(~0)) != a) WARN; // expected-warning{{never executed}}
  if ((a|0) != a) WARN;    // expected-warning{{never executed}}
}

void testReductionToConstant (int a) {
  if ((a*0) != 0) WARN; // expected-warning{{never executed}}
  if ((a&0) != 0) WARN; // expected-warning{{never executed}}
  if ((a|(~0)) != (~0)) WARN; // expected-warning{{never executed}}
}

void testSymmetricIntSymOperations (int a) {
  if ((2+a) != (a+2)) WARN; // expected-warning{{never executed}}
  if ((2*a) != (a*2)) WARN; // expected-warning{{never executed}}
  if ((2&a) != (a&2)) WARN; // expected-warning{{never executed}}
  if ((2^a) != (a^2)) WARN; // expected-warning{{never executed}}
  if ((2|a) != (a|2)) WARN; // expected-warning{{never executed}}
}

void testAsymmetricIntSymOperations (int a) {
  if (((~0) >> a) != (~0)) WARN; // expected-warning{{never executed}}
  if ((0 >> a) != 0) WARN; // expected-warning{{never executed}}
  if ((0 << a) != 0) WARN; // expected-warning{{never executed}}

  // Unsigned right shift shifts in zeroes.
  if ((((unsigned)(~0)) >> ((unsigned) a)) != ((unsigned)(~0)))
    WARN; // expected-warning{{}}
}

void testLocations (char *a) {
  char *b = a;
  if (!(b==a)) WARN; // expected-warning{{never executed}}
  if (!(b>=a)) WARN; // expected-warning{{never executed}}
  if (!(b<=a)) WARN; // expected-warning{{never executed}}
  if (b!=a) WARN; // expected-warning{{never executed}}
  if (b>a) WARN; // expected-warning{{never executed}}
  if (b<a) WARN; // expected-warning{{never executed}}
  if (b-a) WARN; // expected-warning{{never executed}}
}