[AMDGPU][AsmParser][NFC] Get rid of custom default operand handlers.
[llvm-project.git] / clang / test / Analysis / virtualcall.cpp
blob80f89d14ea97c2cedbe4a8fdcd7f36cb81239370
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.cplusplus.VirtualCall \
2 // RUN: -analyzer-checker=debug.ExprInspection \
3 // RUN: -std=c++11 -verify=impure %s
5 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.PureVirtualCall \
6 // RUN: -analyzer-checker=debug.ExprInspection \
7 // RUN: -std=c++11 -verify=pure -std=c++11 %s
9 // RUN: %clang_analyze_cc1 -analyzer-checker=core,optin.cplusplus.VirtualCall \
10 // RUN: -analyzer-config \
11 // RUN: optin.cplusplus.VirtualCall:PureOnly=true \
12 // RUN: -analyzer-checker=debug.ExprInspection \
13 // RUN: -std=c++11 -verify=none %s
15 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.PureVirtualCall \
16 // RUN: -analyzer-checker=optin.cplusplus.VirtualCall \
17 // RUN: -analyzer-checker=debug.ExprInspection \
18 // RUN: -std=c++11 -verify=pure,impure -std=c++11 %s
20 // RUN: %clang_analyze_cc1 -analyzer-checker=core,cplusplus.PureVirtualCall \
21 // RUN: -analyzer-checker=optin.cplusplus.VirtualCall \
22 // RUN: -analyzer-config \
23 // RUN: optin.cplusplus.VirtualCall:PureOnly=true \
24 // RUN: -analyzer-checker=debug.ExprInspection \
25 // RUN: -std=c++11 -verify=pure %s
28 // We expect no diagnostics when all checks are disabled.
29 // none-no-diagnostics
32 #include "virtualcall.h"
34 void clang_analyzer_warnIfReached();
36 class A {
37 public:
38 A();
40 ~A(){};
42 virtual int foo() = 0;
43 virtual void bar() = 0;
44 void f() {
45 foo(); // pure-warning{{Call to pure virtual method 'A::foo' during construction has undefined behavior}}
46 clang_analyzer_warnIfReached(); // no-warning
50 A::A() {
51 f();
54 class B {
55 public:
56 B() {
57 foo(); // impure-warning {{Call to virtual method 'B::foo' during construction bypasses virtual dispatch}}
59 ~B();
61 virtual int foo();
62 virtual void bar() {
63 foo(); // impure-warning {{Call to virtual method 'B::foo' during destruction bypasses virtual dispatch}}
67 B::~B() {
68 this->B::foo(); // no-warning
69 this->B::bar();
70 this->foo(); // impure-warning {{Call to virtual method 'B::foo' during destruction bypasses virtual dispatch}}
73 class C : public B {
74 public:
75 C();
76 ~C();
78 virtual int foo();
79 void f(int i);
82 C::C() {
83 f(foo()); // impure-warning {{Call to virtual method 'C::foo' during construction bypasses virtual dispatch}}
86 class D : public B {
87 public:
88 D() {
89 foo(); // no-warning
91 ~D() { bar(); }
92 int foo() final;
93 void bar() final { foo(); } // no-warning
96 class E final : public B {
97 public:
98 E() {
99 foo(); // no-warning
101 ~E() { bar(); }
102 int foo() override;
105 class F {
106 public:
107 F() {
108 void (F::*ptr)() = &F::foo;
109 (this->*ptr)();
111 void foo();
114 class G {
115 public:
116 G() {}
117 virtual void bar();
118 void foo() {
119 bar(); // no warning
123 class H {
124 public:
125 H() : initState(0) { init(); }
126 int initState;
127 virtual void f() const;
128 void init() {
129 if (initState)
130 f(); // no warning
133 H(int i) {
134 G g;
135 g.foo();
136 g.bar(); // no warning
137 f(); // impure-warning {{Call to virtual method 'H::f' during construction bypasses virtual dispatch}}
138 H &h = *this;
139 h.f(); // impure-warning {{Call to virtual method 'H::f' during construction bypasses virtual dispatch}}
143 class X {
144 public:
145 X() {
146 g(); // impure-warning {{Call to virtual method 'X::g' during construction bypasses virtual dispatch}}
148 X(int i) {
149 if (i > 0) {
150 X x(i - 1);
151 x.g(); // no warning
153 g(); // impure-warning {{Call to virtual method 'X::g' during construction bypasses virtual dispatch}}
155 virtual void g();
158 class M;
159 class N {
160 public:
161 virtual void virtualMethod();
162 void callFooOfM(M *);
164 class M {
165 public:
166 M() {
167 N n;
168 n.virtualMethod(); // no warning
169 n.callFooOfM(this);
171 virtual void foo();
173 void N::callFooOfM(M *m) {
174 m->foo(); // impure-warning {{Call to virtual method 'M::foo' during construction bypasses virtual dispatch}}
177 class Y {
178 public:
179 virtual void foobar();
180 void fooY() {
181 F f1;
182 foobar(); // impure-warning {{Call to virtual method 'Y::foobar' during construction bypasses virtual dispatch}}
184 Y() { fooY(); }
187 int main() {
188 B b;
189 C c;
190 D d;
191 E e;
192 F f;
193 G g;
194 H h;
195 H h1(1);
196 X x;
197 X x1(1);
198 M m;
199 Y *y = new Y;
200 delete y;
201 header::Z z;
204 namespace PR34451 {
205 struct a {
206 void b() {
207 a c[1];
208 c->b();
212 class e {
213 public:
214 void b() const;
217 class c {
218 void m_fn2() const;
219 e d[];
222 void c::m_fn2() const { d->b(); }