1 // RUN: %clang_analyze_cc1 -analyzer-checker=core \
2 // RUN:   -analyzer-output=text \
3 // RUN:   -verify %s
4 
5 namespace test_tracking_of_lhs_multiplier {
f(int x,int y)6   int f(int x, int y) {
7     bool p0 = x < 0;  // expected-note {{Assuming 'x' is >= 0}} \
8                       // expected-note {{'p0' initialized to 0}}
9     int div = p0 * y; // expected-note {{'div' initialized to 0}}
10     return 1 / div;   // expected-note {{Division by zero}} \
11                       // expected-warning {{Division by zero}}
12   }
13 } // namespace test_tracking_of_lhs_multiplier
14 
15 namespace test_tracking_of_rhs_multiplier {
f(int x,int y)16   int f(int x, int y) {
17     bool p0 = x < 0;  // expected-note {{Assuming 'x' is >= 0}} \
18                       // expected-note {{'p0' initialized to 0}}
19     int div = y * p0; // expected-note {{'div' initialized to 0}}
20     return 1 / div;   // expected-note {{Division by zero}} \
21                       // expected-warning {{Division by zero}}
22   }
23 } // namespace test_tracking_of_rhs_multiplier
24 
25 namespace test_tracking_of_nested_multiplier {
f(int x,int y,int z)26   int f(int x, int y, int z) {
27     bool p0 = x < 0;  // expected-note {{Assuming 'x' is >= 0}} \
28                       // expected-note {{'p0' initialized to 0}}
29     int div = y*z*p0; // expected-note {{'div' initialized to 0}}
30     return 1 / div;   // expected-note {{Division by zero}} \
31                       // expected-warning {{Division by zero}}
32   }
33 } // namespace test_tracking_of_nested_multiplier
34 
35 namespace test_tracking_through_multiple_stmts {
f(int x,int y)36   int f(int x, int y) {
37     bool p0 = x < 0;      // expected-note {{Assuming 'x' is >= 0}}
38     bool p1 = p0 ? 0 : 1; // expected-note {{'p0' is false}} \
39                           // expected-note {{'?' condition is false}}
40     bool p2 = 1 - p1;     // expected-note {{'p2' initialized to 0}}
41     int div = p2 * y;     // expected-note {{'div' initialized to 0}}
42     return 1 / div;       // expected-note {{Division by zero}} \
43                           // expected-warning {{Division by zero}}
44   }
45 } // namespace test_tracking_through_multiple_stmts
46 
47 namespace test_tracking_both_lhs_and_rhs {
f(int x,int y)48   int f(int x, int y) {
49     bool p0 = x < 0;   // expected-note {{Assuming 'x' is >= 0}} \
50                        // expected-note {{'p0' initialized to 0}}
51     bool p1 = y < 0;   // expected-note {{Assuming 'y' is >= 0}} \
52                        // expected-note {{'p1' initialized to 0}}
53     int div = p0 * p1; // expected-note {{'div' initialized to 0}}
54     return 1 / div;    // expected-note {{Division by zero}} \
55                        // expected-warning {{Division by zero}}
56   }
57 } // namespace test_tracking_both_lhs_and_rhs
58 
59 namespace test_tracking_of_multiplier_and_parens {
f(int x,int y,int z)60   int f(int x, int y, int z) {
61     bool p0 = x < 0;    // expected-note {{Assuming 'x' is >= 0}} \
62                         // expected-note {{'p0' initialized to 0}}
63     int div = y*(z*p0); // expected-note {{'div' initialized to 0}}
64     return 1 / div;     // expected-note {{Division by zero}} \
65                         // expected-warning {{Division by zero}}
66   }
67 } // namespace test_tracking_of_multiplier_and_parens
68 
69 namespace test_tracking_of_divisible {
f(int x,int y)70   int f(int x, int y) {
71     bool p0 = x < 0;    // expected-note {{Assuming 'x' is >= 0}} \
72                         // expected-note {{'p0' initialized to 0}}
73     int div = p0 / y;   // expected-note {{'div' initialized to 0}}
74     return 1 / div;     // expected-note {{Division by zero}} \
75                         // expected-warning {{Division by zero}}
76   }
77 } // namespace test_tracking_of_divisible
78 
79 namespace test_tracking_of_modulo {
f(int x,int y)80   int f(int x, int y) {
81     bool p0 = x < 0;    // expected-note {{Assuming 'x' is >= 0}} \
82                         // expected-note {{'p0' initialized to 0}}
83     int div = p0 % y;   // expected-note {{'div' initialized to 0}}
84     return 1 / div;     // expected-note {{Division by zero}} \
85                         // expected-warning {{Division by zero}}
86   }
87 } // namespace test_tracking_of_modulo
88 
89 namespace test_tracking_of_assignment {
f(int x)90   int f(int x) {
91     bool p0 = x < 0;    // expected-note {{Assuming 'x' is >= 0}} \
92                         // expected-note {{'p0' initialized to 0}}
93     int div = 1;
94     div *= p0;          // expected-note {{The value 0 is assigned to 'div'}}
95     return 1 / div;     // expected-note {{Division by zero}} \
96                         // expected-warning {{Division by zero}}
97   }
98 } // namespace test_tracking_of_assignment
99