-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathAV Rule 76.ql
More file actions
47 lines (42 loc) · 1.59 KB
/
AV Rule 76.ql
File metadata and controls
47 lines (42 loc) · 1.59 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
/**
* @name AV Rule 76
* @description A copy constructor and an assignment operator shall be declared for classes that contain pointers to data items or nontrivial destructors. If the copy constructor and assignment operators are not required, they should be explicitly disallowed.
* @kind problem
* @id cpp/jsf/av-rule-76
* @problem.severity warning
* @tags maintainability
* external/jsf
*/
import cpp
predicate hasPointerMember(Class c) {
// Note: any function pointers are fine
exists(MemberVariable v, Type t |
v.getDeclaringType() = c and
t = v.getType().getUnderlyingType() and
(t instanceof PointerType or t instanceof PointerToMemberType) and
not t instanceof FunctionPointerType and
not t.(PointerToMemberType).getBaseType() instanceof RoutineType
)
}
class TrivialStmt extends Stmt {
TrivialStmt() {
this instanceof EmptyStmt
or
this instanceof ReturnStmt and not this.(ReturnStmt).hasExpr()
}
}
// What is a nontrivial destructor? JSF is unclear about that. We'll just
// take any nonempty destructor as nontrivial. Exclude the generated 'return' stmt
predicate hasNontrivialDestructor(Class c) {
exists(Stmt s | s = c.getDestructor().getBlock().getAStmt() | not s instanceof TrivialStmt)
}
from Class c
where
(hasPointerMember(c) or hasNontrivialDestructor(c)) and
(
c.hasImplicitCopyAssignmentOperator() or
c.hasImplicitCopyConstructor()
) and
not c instanceof Struct
select c,
"AV Rule 76: A copy constructor and an assignment operator shall be declared for classes that contain pointers to data items or nontrivial destructors."