-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathIRUtilities.qll
More file actions
60 lines (56 loc) · 2.19 KB
/
IRUtilities.qll
File metadata and controls
60 lines (56 loc) · 2.19 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
private import cpp
/**
* Given a type, get the type that would result by applying "pointer decay".
* A function type becomes a pointer to that function type, and an array type
* becomes a pointer to the element type of the array. If the specified type
* is not subject to pointer decay, this predicate does not hold.
*/
private Type getDecayedType(Type type) {
result.(FunctionPointerType).getBaseType() = type.(RoutineType) or
result.(PointerType).getBaseType() = type.(ArrayType).getBaseType()
}
/**
* Holds if the specified variable is a structured binding with a non-reference
* type.
*/
predicate isNonReferenceStructuredBinding(Variable v) {
v.isStructuredBinding() and
not v.getUnspecifiedType() instanceof ReferenceType
}
/**
* Get the actual type of the specified variable, as opposed to the declared type.
* This returns the type of the variable after any pointer decay is applied, and
* after any unsized array type has its size inferred from the initializer.
*/
Type getVariableType(Variable v) {
exists(Type declaredType |
declaredType = v.getUnspecifiedType() and
if v instanceof Parameter
then
result = getDecayedType(declaredType)
or
not exists(getDecayedType(declaredType)) and result = v.getType()
else
if declaredType instanceof ArrayType and not declaredType.(ArrayType).hasArraySize()
then
result = v.getInitializer().getExpr().getType()
or
not exists(v.getInitializer()) and result = v.getType()
else
if isNonReferenceStructuredBinding(v)
then
// The extractor ensures `r` exists when `isNonReferenceStructuredBinding(v)` holds.
exists(LValueReferenceType r | r.getBaseType() = v.getUnspecifiedType() | result = r)
else result = v.getType()
)
}
/**
* Holds if the database contains a `switchCase` label with the specified minimum `minValue`
* and maximum `maxValue` value.
*/
predicate hasCaseEdge(SwitchCase switchCase, string minValue, string maxValue) {
minValue = switchCase.getExpr().getFullyConverted().getValue() and
if exists(switchCase.getEndExpr())
then maxValue = switchCase.getEndExpr().getFullyConverted().getValue()
else maxValue = minValue
}