brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.0 KiB · 3580f11 Raw
60 lines · cpp
1// RUN: %clang_analyze_cc1 -analyzer-checker core -analyzer-config cfg-temporary-dtors=true,c++-temp-dtor-inlining=true -analyzer-output=text -verify %s2 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_temporary20 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) {33  // We'd divide by zero in the automatic destructor for variable 'c'.34  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'}}39  c.nop();40} // expected-note{{Calling '~C'}}41} // end namespace test_lifetime_extended_temporary42 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_dtor60