-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStructLikeClass.qll
More file actions
69 lines (64 loc) · 2.41 KB
/
StructLikeClass.qll
File metadata and controls
69 lines (64 loc) · 2.41 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
import semmle.code.cpp.Class
/**
* A class that is either a `struct` or just has getters and setters
* for its members. In either case it just stores data and has no
* real encapsulation.
*/
class StructLikeClass extends Class {
StructLikeClass() {
this instanceof Struct
or
forall(MemberFunction f | f = this.getAMemberFunction() |
exists(MemberVariable v | setter(v, f, this) or getter(v, f, this))
or
f instanceof Constructor
or
f instanceof Destructor
or
// Allow the copy and move assignment ops - still struct-like
f instanceof CopyAssignmentOperator
or
f instanceof MoveAssignmentOperator
)
}
/**
* Gets a setter function in this class, setting the given variable.
* This is a function whose name begins "set"... that assigns to this variable and no other
* member variable of the class. In addition, it takes a single parameter of
* type the type of the corresponding member variable.
*/
MemberFunction getASetter(MemberVariable v) { setter(v, result, this) }
/**
* Gets a getter function in this class, getting the given variable.
* This is a function whose name begins "get"... that reads this variable and no other
* member variable of the class. In addition, its return type is the type
* of the corresponding member variable.
*/
MemberFunction getAGetter(MemberVariable v) { getter(v, result, this) }
}
/**
* Holds if `f` is a setter member function for `v`, in class `c`.
* See `StructLikeClass.getASetter`.
*/
predicate setter(MemberVariable v, MemberFunction f, Class c) {
f.getDeclaringType() = c and
v.getDeclaringType() = c and
f.getName().matches("set%") and
v.getAnAssignedValue().getEnclosingFunction() = f and
forall(MemberVariable v2 | v2.getAnAssignedValue().getEnclosingFunction() = f | v2 = v) and
f.getNumberOfParameters() = 1 and
f.getParameter(0).getType().stripType() = v.getType().stripType()
}
/**
* Holds if `f` is a getter member function for `v`, in class `c`.
* See `StructLikeClass.getAGetter`.
*/
predicate getter(MemberVariable v, MemberFunction f, Class c) {
f.getDeclaringType() = c and
v.getDeclaringType() = c and
f.getName().matches("get%") and
v.getAnAccess().getEnclosingFunction() = f and
forall(MemberVariable v2 | v2.getAnAccess().getEnclosingFunction() = f | v2 = v) and
f.getNumberOfParameters() = 0 and
f.getType().stripType() = v.getType().stripType()
}