-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathBasicIntTypes.ql
More file actions
59 lines (53 loc) · 1.57 KB
/
BasicIntTypes.ql
File metadata and controls
59 lines (53 loc) · 1.57 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
/**
* @name Use of basic integral type
* @description Typedefs that indicate size and signedness should be used in place of the basic types.
* @kind problem
* @id cpp/jpl-c/basic-int-types
* @problem.severity recommendation
* @tags maintainability
* readability
* external/jpl
*/
import cpp
predicate allowedTypedefs(TypedefType t) {
t.getName() =
[
"I64", "U64", "I32", "U32", "I16", "U16", "I8", "U8", "F64", "F32", "int64_t", "uint64_t",
"int32_t", "uint32_t", "int16_t", "uint16_t", "int8_t", "uint8_t"
]
}
/**
* Gets a type which appears literally in the declaration of `d`.
*/
Type getAnImmediateUsedType(Declaration d) {
d.hasDefinition() and
(
result = d.(Function).getType() or
result = d.(Variable).getType()
)
}
/**
* Gets a type which appears indirectly in `t`, stopping at allowed typedefs.
*/
Type getAUsedType(Type t) {
not allowedTypedefs(t) and
(
result = t.(TypedefType).getBaseType() or
result = t.(DerivedType).getBaseType()
)
}
predicate problematic(IntegralType t) {
// List any exceptions that should be allowed.
any()
}
from Declaration d, Type usedType
where
usedType = getAUsedType*(getAnImmediateUsedType(d)) and
problematic(usedType) and
// Allow uses of boolean types where defined by the language.
not usedType instanceof BoolType and
// Ignore violations for which we do not have a valid location.
not d.getLocation() instanceof UnknownLocation
select d,
d.getName() + " uses the basic integral type " + usedType.getName() +
" rather than a typedef with size and signedness."