1 // RUN: %clang_analyze_cc1 -analyzer-checker=debug.ExprInspection,core.builtin -analyzer-config aggressive-binary-operation-simplification=true -verify %s
3 void clang_analyzer_eval(int);
8 #define UINT_MAX (~UINT_MIN)
9 #define UINT_MID (UINT_MAX / 2 + 1)
10 #define INT_MAX (UINT_MAX & (UINT_MAX >> 1))
11 #define INT_MIN (UINT_MAX & ~(UINT_MAX >> 1))
13 extern void __assert_fail (__const
char *__assertion
, __const
char *__file
,
14 unsigned int __line
, __const
char *__function
)
15 __attribute__ ((__noreturn__
));
16 #define assert(expr) \
17 ((expr) ? (void)(0) : __assert_fail (#expr, __FILE__, __LINE__, __func__))
19 void assert_in_range(int x
) {
20 assert(x
<= ((int)INT_MAX
/ 4));
21 assert(x
>= -(((int)INT_MAX
) / 4));
24 void assert_in_wide_range(int x
) {
25 assert(x
<= ((int)INT_MAX
/ 2));
26 assert(x
>= -(((int)INT_MAX
) / 2));
29 void assert_in_range_2(int m
, int n
) {
34 void equal(int m
, int n
) {
35 assert_in_range_2(m
, n
);
38 assert_in_wide_range(m
- n
);
39 clang_analyzer_eval(n
== m
); // expected-warning{{TRUE}}
42 void non_equal(int m
, int n
) {
43 assert_in_range_2(m
, n
);
46 assert_in_wide_range(m
- n
);
47 clang_analyzer_eval(n
!= m
); // expected-warning{{TRUE}}
50 void less_or_equal(int m
, int n
) {
51 assert_in_range_2(m
, n
);
54 assert_in_wide_range(m
- n
);
55 clang_analyzer_eval(n
<= m
); // expected-warning{{TRUE}}
58 void less(int m
, int n
) {
59 assert_in_range_2(m
, n
);
62 assert_in_wide_range(m
- n
);
63 clang_analyzer_eval(n
< m
); // expected-warning{{TRUE}}
66 void greater_or_equal(int m
, int n
) {
67 assert_in_range_2(m
, n
);
70 assert_in_wide_range(m
- n
);
71 clang_analyzer_eval(n
>= m
); // expected-warning{{TRUE}}
74 void greater(int m
, int n
) {
75 assert_in_range_2(m
, n
);
78 assert_in_wide_range(m
- n
);
79 clang_analyzer_eval(n
> m
); // expected-warning{{TRUE}}
82 void negate_positive_range(int m
, int n
) {
85 clang_analyzer_eval(n
- m
< 0); // expected-warning{{TRUE}}
86 clang_analyzer_eval(n
- m
> INT_MIN
); // expected-warning{{TRUE}}
87 clang_analyzer_eval(n
- m
== INT_MIN
); // expected-warning{{FALSE}}
90 void negate_int_min(int m
, int n
) {
93 clang_analyzer_eval(n
- m
== INT_MIN
); // expected-warning{{TRUE}}
96 void negate_mixed(int m
, int n
) {
97 if (m
-n
> INT_MIN
&& m
- n
<= 0)
99 clang_analyzer_eval(n
- m
<= 0); // expected-warning{{TRUE}}
102 void effective_range(int m
, int n
) {
105 clang_analyzer_eval(m
- n
== 0); // expected-warning{{TRUE}}
106 clang_analyzer_eval(n
- m
== 0); // expected-warning{{TRUE}}
109 void effective_range_2(int m
, int n
) {
112 clang_analyzer_eval(m
- n
== 0); // expected-warning{{TRUE}} expected-warning{{FALSE}}
113 clang_analyzer_eval(n
- m
== 0); // expected-warning{{TRUE}} expected-warning{{FALSE}}
116 void negate_unsigned_min(unsigned m
, unsigned n
) {
117 if (m
- n
== UINT_MIN
) {
118 clang_analyzer_eval(n
- m
== UINT_MIN
); // expected-warning{{TRUE}}
119 clang_analyzer_eval(n
- m
!= UINT_MIN
); // expected-warning{{FALSE}}
120 clang_analyzer_eval(n
- m
> UINT_MIN
); // expected-warning{{FALSE}}
121 clang_analyzer_eval(n
- m
< UINT_MIN
); // expected-warning{{FALSE}}
125 void negate_unsigned_mid(unsigned m
, unsigned n
) {
126 if (m
- n
== UINT_MID
) {
127 clang_analyzer_eval(n
- m
== UINT_MID
); // expected-warning{{TRUE}}
128 clang_analyzer_eval(n
- m
!= UINT_MID
); // expected-warning{{FALSE}}
132 void negate_unsigned_mid2(unsigned m
, unsigned n
) {
133 if (m
- n
< UINT_MID
&& m
- n
> UINT_MIN
) {
134 clang_analyzer_eval(n
- m
> UINT_MID
); // expected-warning{{TRUE}}
135 clang_analyzer_eval(n
- m
< UINT_MID
); // expected-warning{{FALSE}}
139 void negate_unsigned_max(unsigned m
, unsigned n
) {
140 if (m
- n
== UINT_MAX
) {
141 clang_analyzer_eval(n
- m
== 1); // expected-warning{{TRUE}}
142 clang_analyzer_eval(n
- m
!= 1); // expected-warning{{FALSE}}
146 void negate_unsigned_one(unsigned m
, unsigned n
) {
148 clang_analyzer_eval(n
- m
== UINT_MAX
); // expected-warning{{TRUE}}
149 clang_analyzer_eval(n
- m
< UINT_MAX
); // expected-warning{{FALSE}}
153 // The next code is a repro for the bug PR41588
154 void negated_unsigned_range(unsigned x
, unsigned y
) {
155 clang_analyzer_eval(x
- y
!= 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}
156 clang_analyzer_eval(y
- x
!= 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}
157 // expected no assertion on the next line
158 clang_analyzer_eval(x
- y
!= 0); // expected-warning{{FALSE}} expected-warning{{TRUE}}