Home | History | Annotate | Download | only in Analysis
      1 // RUN: %clang_cc1 -analyze -analyzer-checker=alpha.cplusplus.VirtualCall -analyzer-store region -verify -std=c++11 %s
      2 
      3 class A {
      4 public:
      5   A();
      6   ~A() {};
      7 
      8   virtual int foo() = 0;
      9   virtual void bar() = 0;
     10   void f() {
     11     foo(); // expected-warning{{Call pure virtual functions during construction or destruction may leads undefined behaviour}}
     12   }
     13 };
     14 
     15 class B : public A {
     16 public:
     17   B() {
     18     foo(); // expected-warning{{Call virtual functions during construction or destruction will never go to a more derived class}}
     19   }
     20   ~B();
     21 
     22   virtual int foo();
     23   virtual void bar() { foo(); }  // expected-warning{{Call virtual functions during construction or destruction will never go to a more derived class}}
     24 };
     25 
     26 A::A() {
     27   f();
     28 }
     29 
     30 B::~B() {
     31   this->B::foo(); // no-warning
     32   this->B::bar();
     33   this->foo(); // expected-warning{{Call virtual functions during construction or destruction will never go to a more derived class}}
     34 }
     35 
     36 class C : public B {
     37 public:
     38   C();
     39   ~C();
     40 
     41   virtual int foo();
     42   void f(int i);
     43 };
     44 
     45 C::C() {
     46   f(foo()); // expected-warning{{Call virtual functions during construction or destruction will never go to a more derived class}}
     47 }
     48 
     49 class D : public B {
     50 public:
     51   D() {
     52     foo(); // no-warning
     53   }
     54   ~D() { bar(); }
     55   int foo() final;
     56   void bar() final { foo(); } // no-warning
     57 };
     58 
     59 class E final : public B {
     60 public:
     61   E() {
     62     foo(); // no-warning
     63   }
     64   ~E() { bar(); }
     65   int foo() override;
     66 };
     67 
     68 int main() {
     69   A *a;
     70   B *b;
     71   C *c;
     72   D *d;
     73   E *e;
     74 }
     75 
     76 #include "virtualcall.h"
     77 
     78 #define AS_SYSTEM
     79 #include "virtualcall.h"
     80 #undef AS_SYSTEM
     81