xref: /llvm-project/clang/test/Analysis/constraint-assignor.c (revision 0dd49a5628bbe01cecf6516017da59ae44863ab3)
1 // RUN: %clang_analyze_cc1 %s \
2 // RUN:   -analyzer-checker=core \
3 // RUN:   -analyzer-checker=debug.ExprInspection \
4 // RUN:   -verify
5 
6 void clang_analyzer_warnIfReached(void);
7 void clang_analyzer_eval(int);
8 
rem_constant_rhs_ne_zero(int x,int y)9 void rem_constant_rhs_ne_zero(int x, int y) {
10   if (x % 3 == 0) // x % 3 != 0 -> x != 0
11     return;
12   if (x * y != 0) // x * y == 0
13     return;
14   if (y != 1)     // y == 1     -> x == 0
15     return;
16   clang_analyzer_warnIfReached(); // no-warning
17   (void)x; // keep the constraints alive.
18 }
19 
rem_symbolic_rhs_ne_zero(int x,int y,int z)20 void rem_symbolic_rhs_ne_zero(int x, int y, int z) {
21   if (x % z == 0) // x % z != 0 -> x != 0
22     return;
23   if (x * y != 0) // x * y == 0
24     return;
25   if (y != 1)     // y == 1     -> x == 0
26     return;
27   clang_analyzer_warnIfReached(); // no-warning
28   (void)x; // keep the constraints alive.
29 }
30 
rem_symbolic_rhs_ne_zero_nested(int w,int x,int y,int z)31 void rem_symbolic_rhs_ne_zero_nested(int w, int x, int y, int z) {
32   if (w % x % z == 0) // w % x % z != 0 -> w % x != 0
33     return;
34   if (w % x * y != 0) // w % x * y == 0
35     return;
36   if (y != 1)         // y == 1         -> w % x == 0
37     return;
38   clang_analyzer_warnIfReached(); // no-warning
39   (void)(w * x); // keep the constraints alive.
40 }
41 
rem_constant_rhs_ne_zero_early_contradiction(int x,int y)42 void rem_constant_rhs_ne_zero_early_contradiction(int x, int y) {
43   if ((x + y) != 0)     // (x + y) == 0
44     return;
45   if ((x + y) % 3 == 0) // (x + y) % 3 != 0 -> (x + y) != 0 -> contradiction
46     return;
47   clang_analyzer_warnIfReached(); // no-warning
48   (void)x; // keep the constraints alive.
49 }
50 
rem_symbolic_rhs_ne_zero_early_contradiction(int x,int y,int z)51 void rem_symbolic_rhs_ne_zero_early_contradiction(int x, int y, int z) {
52   if ((x + y) != 0)     // (x + y) == 0
53     return;
54   if ((x + y) % z == 0) // (x + y) % z != 0 -> (x + y) != 0 -> contradiction
55     return;
56   clang_analyzer_warnIfReached(); // no-warning
57   (void)x; // keep the constraints alive.
58 }
59 
internal_unsigned_signed_mismatch(unsigned a)60 void internal_unsigned_signed_mismatch(unsigned a) {
61   int d = a;
62   // Implicit casts are not handled, thus the analyzer models `d % 2` as
63   // `(reg_$0<unsigned int a>) % 2`
64   // However, this should not result in internal signedness mismatch error when
65   // we assign new constraints below.
66   if (d % 2 != 0)
67     return;
68 }
69 
remainder_with_adjustment(int x)70 void remainder_with_adjustment(int x) {
71   if ((x + 1) % 3 == 0) // (x + 1) % 3 != 0 -> x + 1 != 0 -> x != -1
72     return;
73   clang_analyzer_eval(x + 1 != 0); // expected-warning{{TRUE}}
74   clang_analyzer_eval(x != -1);    // expected-warning{{TRUE}}
75   (void)x; // keep the constraints alive.
76 }
77 
remainder_with_adjustment_of_composit_lhs(int x,int y)78 void remainder_with_adjustment_of_composit_lhs(int x, int y) {
79   if ((x + y + 1) % 3 == 0) // (x + 1) % 3 != 0 -> x + 1 != 0 -> x != -1
80     return;
81   clang_analyzer_eval(x + y + 1 != 0); // expected-warning{{TRUE}}
82   clang_analyzer_eval(x + y != -1);    // expected-warning{{TRUE}}
83   (void)(x * y); // keep the constraints alive.
84 }
85