bugprone-parent-virtual-call.cpp
6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// RUN: %check_clang_tidy %s bugprone-parent-virtual-call %t
extern int foo();
class A {
public:
A() = default;
virtual ~A() = default;
virtual int virt_1() { return foo() + 1; }
virtual int virt_2() { return foo() + 2; }
int non_virt() { return foo() + 3; }
static int stat() { return foo() + 4; }
};
class B : public A {
public:
B() = default;
// Nothing to fix: calls to direct parent.
int virt_1() override { return A::virt_1() + 3; }
int virt_2() override { return A::virt_2() + 4; }
};
class C : public B {
public:
int virt_1() override { return A::virt_1() + B::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' refers to a member overridden in subclass; did you mean 'B'? [bugprone-parent-virtual-call]
// CHECK-FIXES: int virt_1() override { return B::virt_1() + B::virt_1(); }
int virt_2() override { return A::virt_1() + B::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'B'? {{.*}}
// CHECK-FIXES: int virt_2() override { return B::virt_1() + B::virt_1(); }
// Test that non-virtual and static methods are not affected by this cherker.
int method_c() { return A::stat() + A::non_virt(); }
};
// Check aliased type names
using A1 = A;
typedef A A2;
#define A3 A
class C2 : public B {
public:
int virt_1() override { return A1::virt_1() + A2::virt_1() + A3::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A1::virt_1' {{.*}}; did you mean 'B'? {{.*}}
// CHECK-MESSAGES: :[[@LINE-2]]:49: warning: qualified name 'A2::virt_1' {{.*}}; did you mean 'B'? {{.*}}
// CHECK-MESSAGES: :[[@LINE-3]]:64: warning: qualified name 'A3::virt_1' {{.*}}; did you mean 'B'? {{.*}}
// CHECK-FIXES: int virt_1() override { return B::virt_1() + B::virt_1() + B::virt_1(); }
};
// Test that the check affects grand-grand..-parent calls too.
class D : public C {
public:
int virt_1() override { return A::virt_1() + B::virt_1() + D::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'C'? {{.*}}
// CHECK-MESSAGES: :[[@LINE-2]]:48: warning: qualified name 'B::virt_1' {{.*}}; did you mean 'C'? {{.*}}
// CHECK-FIXES: int virt_1() override { return C::virt_1() + C::virt_1() + D::virt_1(); }
int virt_2() override { return A::virt_1() + B::virt_1() + D::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'C'? {{.*}}
// CHECK-MESSAGES: :[[@LINE-2]]:48: warning: qualified name 'B::virt_1' {{.*}}; did you mean 'C'? {{.*}}
// CHECK-FIXES: int virt_2() override { return C::virt_1() + C::virt_1() + D::virt_1(); }
};
// Test classes in namespaces.
namespace {
class BN : public A {
public:
int virt_1() override { return A::virt_1() + 3; }
int virt_2() override { return A::virt_2() + 4; }
};
} // namespace
namespace N1 {
class A {
public:
A() = default;
virtual int virt_1() { return foo() + 1; }
virtual int virt_2() { return foo() + 2; }
};
} // namespace N1
namespace N2 {
class BN : public N1::A {
public:
int virt_1() override { return A::virt_1() + 3; }
int virt_2() override { return A::virt_2() + 4; }
};
} // namespace N2
class CN : public BN {
public:
int virt_1() override { return A::virt_1() + BN::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'BN'? {{.*}}
// CHECK-FIXES: int virt_1() override { return BN::virt_1() + BN::virt_1(); }
int virt_2() override { return A::virt_1() + BN::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'BN'? {{.*}}
// CHECK-FIXES: int virt_2() override { return BN::virt_1() + BN::virt_1(); }
};
class CNN : public N2::BN {
public:
int virt_1() override { return N1::A::virt_1() + N2::BN::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'N1::A::virt_1' {{.*}}; did you mean 'N2::BN'? {{.*}}
// CHECK-FIXES: int virt_1() override { return N2::BN::virt_1() + N2::BN::virt_1(); }
int virt_2() override { return N1::A::virt_1() + N2::BN::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'N1::A::virt_1' {{.*}}; did you mean 'N2::BN'? {{.*}}
// CHECK-FIXES: int virt_2() override { return N2::BN::virt_1() + N2::BN::virt_1(); }
};
// Test multiple inheritance fixes
class AA {
public:
AA() = default;
virtual ~AA() = default;
virtual int virt_1() { return foo() + 1; }
virtual int virt_2() { return foo() + 2; }
int non_virt() { return foo() + 3; }
static int stat() { return foo() + 4; }
};
class BB_1 : virtual public AA {
public:
BB_1() = default;
// Nothing to fix: calls to parent.
int virt_1() override { return AA::virt_1() + 3; }
int virt_2() override { return AA::virt_2() + 4; }
};
class BB_2 : virtual public AA {
public:
BB_2() = default;
int virt_1() override { return AA::virt_1() + 3; }
};
class CC : public BB_1, public BB_2 {
public:
int virt_1() override { return AA::virt_1() + 3; }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'AA::virt_1' refers to a member overridden in subclasses; did you mean 'BB_1' or 'BB_2'? {{.*}}
// No fix available due to multiple choice of parent class.
};
// Test that virtual method is not diagnosed as not overridden in parent.
class BI : public A {
public:
BI() = default;
};
class CI : BI {
int virt_1() override { return A::virt_1(); }
};
// Test templated classes.
template <class F> class BF : public A {
public:
int virt_1() override { return A::virt_1() + 3; }
};
// Test templated parent class.
class CF : public BF<int> {
public:
int virt_1() override { return A::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'BF'? {{.*}}
};
// Test both templated class and its parent class.
template <class F> class DF : public BF<F> {
public:
DF() = default;
int virt_1() override { return A::virt_1(); }
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: qualified name 'A::virt_1' {{.*}}; did you mean 'BF'? {{.*}}
};
// Just to instantiate DF<F>.
int bar() { return (new DF<int>())->virt_1(); }