Skip to content

Commit ce02e4a

Browse files
geoffw0Copilot
andcommitted
C++: Test virtual calls from constructors and destructors
Cover direct and indirect dispatch on this, plus qualified and non-overridden calls for cpp/virtual-call-in-constructor. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 43c6106 commit ce02e4a

3 files changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
| test.cpp:4:5:4:8 | call to init | Call to virtual function $@ which is overridden in $@. If you intend to statically call this virtual function, it should be qualified with Base::. | test.cpp:18:16:18:19 | init | init | test.cpp:38:8:38:11 | init | Derived |
2+
| test.cpp:5:11:5:14 | call to init | Call to virtual function $@ which is overridden in $@. If you intend to statically call this virtual function, it should be qualified with Base::. | test.cpp:18:16:18:19 | init | init | test.cpp:38:8:38:11 | init | Derived |
3+
| test.cpp:6:13:6:16 | call to init | Call to virtual function $@ which is overridden in $@. If you intend to statically call this virtual function, it should be qualified with Base::. | test.cpp:18:16:18:19 | init | init | test.cpp:38:8:38:11 | init | Derived |
4+
| test.cpp:7:5:7:10 | call to helper | Call to function helper that calls virtual function $@ (overridden in $@). | test.cpp:18:16:18:19 | init | init | test.cpp:38:8:38:11 | init | Derived |
5+
| test.cpp:14:5:14:11 | call to cleanup | Call to virtual function $@ which is overridden in $@. If you intend to statically call this virtual function, it should be qualified with Base::. | test.cpp:19:16:19:22 | cleanup | cleanup | test.cpp:39:8:39:14 | cleanup | Derived |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
query: jsf/4.10 Classes/AV Rule 71.1.ql
2+
postprocess: utils/test/InlineExpectationsTestQuery.ql
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Base {
2+
public:
3+
Base() {
4+
init(); // $ Alert // BAD: virtual call, overridden in Derived
5+
this->init(); // $ Alert // BAD: virtual call, overridden in Derived
6+
(*this).init(); // $ Alert // BAD: virtual call, overridden in Derived
7+
helper(); // $ Alert // BAD: indirectly calls a virtual function
8+
Base::init(); // GOOD: explicitly qualified, so statically bound
9+
notOverridden(); // GOOD: not overridden in any derived class
10+
nonVirtual(); // GOOD: not virtual
11+
}
12+
13+
~Base() {
14+
cleanup(); // $ Alert // BAD: virtual call, overridden in Derived
15+
Base::cleanup(); // GOOD: explicitly qualified
16+
}
17+
18+
virtual void init() {}
19+
virtual void cleanup() {}
20+
virtual void notOverridden() {}
21+
void nonVirtual() {}
22+
23+
void helper() {
24+
init();
25+
}
26+
27+
void other() {
28+
init(); // GOOD: not in a constructor or destructor
29+
}
30+
};
31+
32+
class Derived : public Base {
33+
public:
34+
Derived() {
35+
init(); // GOOD: not overridden in a class derived from Derived
36+
}
37+
38+
void init() override {}
39+
void cleanup() override {}
40+
};
41+
42+
class Unrelated {
43+
public:
44+
Unrelated(Base &b) {
45+
b.init(); // GOOD: not a call on `this`
46+
}
47+
};

0 commit comments

Comments
 (0)