blob: 40b26b5c91ae9563a351d08c1df078fff413ea02 [file] [log] [blame]
Artem Dergachev61199442018-03-01 18:53:13 +00001// RUN: %clang_analyze_cc1 -analyze -analyzer-checker core -analyzer-config cfg-temporary-dtors=true,c++-temp-dtor-inlining=true -analyzer-output=text -verify %s
Artem Dergachev60f5aab2018-02-15 19:28:21 +00002
3namespace test_simple_temporary {
4class C {
5 int x;
6
7public:
8 C(int x): x(x) {} // expected-note{{The value 0 is assigned to field 'x'}}
9 ~C() { x = 1 / x; } // expected-warning{{Division by zero}}
10 // expected-note@-1{{Division by zero}}
11};
12
13void test() {
14 C(0); // expected-note {{Passing the value 0 via 1st parameter 'x'}}
15 // expected-note@-1{{Calling constructor for 'C'}}
16 // expected-note@-2{{Returning from constructor for 'C'}}
17 // expected-note@-3{{Calling '~C'}}
18}
19} // end namespace test_simple_temporary
20
21namespace test_lifetime_extended_temporary {
22class C {
23 int x;
24
25public:
26 C(int x): x(x) {} // expected-note{{The value 0 is assigned to field 'x'}}
27 void nop() const {}
28 ~C() { x = 1 / x; } // expected-warning{{Division by zero}}
29 // expected-note@-1{{Division by zero}}
30};
31
32void test(int coin) {
Artem Dergachev9a209ad2018-06-28 00:30:18 +000033 // We'd divide by zero in the automatic destructor for variable 'c'.
Artem Dergachev60f5aab2018-02-15 19:28:21 +000034 const C &c = coin ? C(1) : C(0); // expected-note {{Assuming 'coin' is 0}}
35 // expected-note@-1{{'?' condition is false}}
36 // expected-note@-2{{Passing the value 0 via 1st parameter 'x'}}
37 // expected-note@-3{{Calling constructor for 'C'}}
38 // expected-note@-4{{Returning from constructor for 'C'}}
Artem Dergachev60f5aab2018-02-15 19:28:21 +000039 c.nop();
Artem Dergachev9a209ad2018-06-28 00:30:18 +000040} // expected-note{{Calling '~C'}}
Artem Dergachev60f5aab2018-02-15 19:28:21 +000041} // end namespace test_lifetime_extended_temporary
42
43namespace test_bug_after_dtor {
44int glob;
45
46class C {
47public:
48 C() { glob += 1; }
49 ~C() { glob -= 2; } // expected-note{{The value 0 is assigned to 'glob'}}
50};
51
52void test() {
53 glob = 1;
54 C(); // expected-note {{Calling '~C'}}
55 // expected-note@-1{{Returning from '~C'}}
56 glob = 1 / glob; // expected-warning{{Division by zero}}
57 // expected-note@-1{{Division by zero}}
58}
59} // end namespace test_bug_after_dtor