2016-12-10 09:16:09 +08:00
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=optin.cplusplus.VirtualCall -analyzer-store region -verify -std=c++11 %s
|
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=optin.cplusplus.VirtualCall -analyzer-store region -analyzer-config optin.cplusplus.VirtualCall:Interprocedural=true -DINTERPROCEDURAL=1 -verify -std=c++11 %s
|
|
|
|
// RUN: %clang_cc1 -analyze -analyzer-checker=optin.cplusplus.VirtualCall -analyzer-store region -analyzer-config optin.cplusplus.VirtualCall:PureOnly=true -DPUREONLY=1 -verify -std=c++11 %s
|
|
|
|
|
|
|
|
/* When INTERPROCEDURAL is set, we expect diagnostics in all functions reachable
|
|
|
|
from a constructor or destructor. If it is not set, we expect diagnostics
|
|
|
|
only in the constructor or destructor.
|
|
|
|
|
|
|
|
When PUREONLY is set, we expect diagnostics only for calls to pure virtual
|
|
|
|
functions not to non-pure virtual functions.
|
|
|
|
*/
|
2012-01-04 07:18:57 +08:00
|
|
|
|
|
|
|
class A {
|
|
|
|
public:
|
|
|
|
A();
|
2016-12-10 09:16:09 +08:00
|
|
|
A(int i);
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
~A() {};
|
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
virtual int foo() = 0; // from Sema: expected-note {{'foo' declared here}}
|
2012-01-04 07:18:57 +08:00
|
|
|
virtual void bar() = 0;
|
|
|
|
void f() {
|
2016-12-10 09:16:09 +08:00
|
|
|
foo();
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-2 {{{{^}}Call Path : foo <-- fCall to pure virtual function during construction has undefined behavior}}
|
|
|
|
#endif
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class B : public A {
|
|
|
|
public:
|
|
|
|
B() {
|
2016-12-10 09:16:09 +08:00
|
|
|
foo();
|
|
|
|
#if !PUREONLY
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-3 {{{{^}}Call Path : fooCall to virtual function during construction will not dispatch to derived class}}
|
|
|
|
#else
|
|
|
|
// expected-warning-re@-5 {{{{^}}Call to virtual function during construction will not dispatch to derived class}}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
~B();
|
|
|
|
|
|
|
|
virtual int foo();
|
2016-12-10 09:16:09 +08:00
|
|
|
virtual void bar() { foo(); }
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-2 {{{{^}}Call Path : foo <-- barCall to virtual function during destruction will not dispatch to derived class}}
|
|
|
|
#endif
|
2012-01-04 07:18:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
A::A() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2016-12-10 09:16:09 +08:00
|
|
|
A::A(int i) {
|
|
|
|
foo(); // From Sema: expected-warning {{call to pure virtual member function 'foo' has undefined behavior}}
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-2 {{{{^}}Call Path : fooCall to pure virtual function during construction has undefined behavior}}
|
|
|
|
#else
|
|
|
|
// expected-warning-re@-4 {{{{^}}Call to pure virtual function during construction has undefined behavior}}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
B::~B() {
|
|
|
|
this->B::foo(); // no-warning
|
|
|
|
this->B::bar();
|
2016-12-10 09:16:09 +08:00
|
|
|
this->foo();
|
|
|
|
#if !PUREONLY
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-3 {{{{^}}Call Path : fooCall to virtual function during destruction will not dispatch to derived class}}
|
|
|
|
#else
|
|
|
|
// expected-warning-re@-5 {{{{^}}Call to virtual function during destruction will not dispatch to derived class}}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class C : public B {
|
|
|
|
public:
|
|
|
|
C();
|
|
|
|
~C();
|
|
|
|
|
|
|
|
virtual int foo();
|
|
|
|
void f(int i);
|
|
|
|
};
|
|
|
|
|
|
|
|
C::C() {
|
2016-12-10 09:16:09 +08:00
|
|
|
f(foo());
|
|
|
|
#if !PUREONLY
|
|
|
|
#if INTERPROCEDURAL
|
|
|
|
// expected-warning-re@-3 {{{{^}}Call Path : fooCall to virtual function during construction will not dispatch to derived class}}
|
|
|
|
#else
|
|
|
|
// expected-warning-re@-5 {{{{^}}Call to virtual function during construction will not dispatch to derived class}}
|
|
|
|
#endif
|
|
|
|
#endif
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
|
|
|
|
2014-08-21 18:25:03 +08:00
|
|
|
class D : public B {
|
|
|
|
public:
|
|
|
|
D() {
|
|
|
|
foo(); // no-warning
|
|
|
|
}
|
|
|
|
~D() { bar(); }
|
|
|
|
int foo() final;
|
|
|
|
void bar() final { foo(); } // no-warning
|
|
|
|
};
|
|
|
|
|
|
|
|
class E final : public B {
|
|
|
|
public:
|
|
|
|
E() {
|
|
|
|
foo(); // no-warning
|
|
|
|
}
|
|
|
|
~E() { bar(); }
|
|
|
|
int foo() override;
|
|
|
|
};
|
|
|
|
|
2012-01-04 07:18:57 +08:00
|
|
|
int main() {
|
|
|
|
A *a;
|
|
|
|
B *b;
|
|
|
|
C *c;
|
2014-08-21 18:25:03 +08:00
|
|
|
D *d;
|
|
|
|
E *e;
|
2012-01-04 07:18:57 +08:00
|
|
|
}
|
2012-10-11 01:55:40 +08:00
|
|
|
|
|
|
|
#include "virtualcall.h"
|
|
|
|
|
|
|
|
#define AS_SYSTEM
|
|
|
|
#include "virtualcall.h"
|
|
|
|
#undef AS_SYSTEM
|