Skip to content

Commit 93b9391

Browse files
committed
DIR-4-6: typedefs should be to numeric types
Recursively identify typedefs that are to the built in numeric types, but do not use the size alias.
1 parent 9fc57bd commit 93b9391

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ predicate forbiddenBuiltinNumericUsedInDecl(Variable var, string message) {
3737
message = "The type " + var.getType() + " is not a fixed-width numeric type."
3838
}
3939

40-
// TODO: add some inline comments
4140
predicate forbiddenTypedef(CTypedefType typedef, string message) {
4241
/* If the typedef's name contains an explicit size */
4342
(
@@ -49,9 +48,15 @@ predicate forbiddenTypedef(CTypedefType typedef, string message) {
4948
typedef.getName().regexpCapture("u?(int|float)(4|8|16|32|64|128)_t", 2).toInt() and
5049
message = "The typedef type " + typedef.getName() + " does not have its indicated size."
5150
) else (
52-
/* Otherwise, if the type is an alias of a built in numeric type it should have an explicit size in its name. */
53-
typedef.getUnspecifiedType() instanceof BuiltInNumericType and
54-
not typedef.getBaseType+().getName().regexpMatch("u?(int|float)(4|8|16|32|64|128)_t") and
51+
(
52+
// type def is to a built in numeric type
53+
typedef.getBaseType() instanceof BuiltInNumericType and
54+
// but does not include the size in the name
55+
not typedef.getName().regexpMatch("u?(int|float)(4|8|16|32|64|128)_t")
56+
or
57+
// this is a typedef to a forbidden type def
58+
forbiddenTypedef(typedef.getBaseType(), _)
59+
) and
5560
message = "The type " + typedef.getName() + " is not an alias to a fixed-width numeric type."
5661
)
5762
)

0 commit comments

Comments
 (0)