Skip to content

Commit c60f8be

Browse files
mbaludaCopilot
andcommitted
Refine MISRA C:2025 DIR-4-6 to check integer types
Add the integer-only query while preserving legacy numerical-type results, with shared detection, metadata, regression tests, and a change note. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 26a444d commit c60f8be

11 files changed

Lines changed: 282 additions & 100 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/** Provides the numeric-type detector shared by legacy and current directive wrappers. */
2+
3+
import cpp
4+
import codingstandards.cpp.BuiltInNumericTypes
5+
6+
/** A numeric type covered by the legacy directive. */
7+
private class NumericType extends BuiltInType {
8+
NumericType() {
9+
this.(CharType).isExplicitlySigned()
10+
or
11+
this.(CharType).isExplicitlyUnsigned()
12+
or
13+
this instanceof BuiltInIntegerType
14+
or
15+
this instanceof FloatType
16+
or
17+
this instanceof DoubleType
18+
or
19+
this instanceof LongDoubleType
20+
or
21+
this instanceof ComplexNumberType
22+
}
23+
}
24+
25+
/** A typedef name that encodes numeric width and signedness. */
26+
private class SizedTypeName extends string {
27+
string pattern;
28+
int width;
29+
30+
bindingset[this]
31+
pragma[inline]
32+
SizedTypeName() {
33+
pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and
34+
this.regexpMatch(pattern) and
35+
width = this.regexpCapture(pattern, 2).toInt()
36+
}
37+
38+
/** Gets the width encoded by this name. */
39+
bindingset[this]
40+
pragma[inline]
41+
int getWidth() { result = width }
42+
43+
/** Holds if this name denotes a complex type. */
44+
bindingset[this]
45+
pragma[inline]
46+
predicate isComplex() { this.charAt(0) = "c" }
47+
}
48+
49+
/** Holds if `type` has an invalid complex name, with `message` explaining why. */
50+
private predicate forbiddenComplexType(CTypedefType type, string message) {
51+
type.getName().(SizedTypeName).isComplex() and
52+
(
53+
if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
54+
then
55+
type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() * 2 and
56+
message = "The typedef type " + type.getName() + " does not have its indicated real size."
57+
else message = "The typedef type " + type.getName() + " is not a complex type."
58+
)
59+
}
60+
61+
/** Holds if `type` has an invalid real name, with `message` explaining why. */
62+
private predicate forbiddenRealType(CTypedefType type, string message) {
63+
not type.getName().(SizedTypeName).isComplex() and
64+
(
65+
if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
66+
then message = "The typedef name " + type.getName() + " does not indicate a complex type."
67+
else (
68+
type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() and
69+
message = "The typedef type " + type.getName() + " does not have its indicated size."
70+
)
71+
)
72+
}
73+
74+
/** Holds if `type` is a forbidden alias, with `message` explaining why. */
75+
private predicate forbiddenAlias(CTypedefType type, string message) {
76+
if type.getName() instanceof SizedTypeName
77+
then forbiddenRealType(type, message) or forbiddenComplexType(type, message)
78+
else (
79+
(
80+
type.getBaseType() instanceof NumericType
81+
or
82+
forbiddenAlias(type.getBaseType(), _)
83+
) and
84+
message = "The type " + type.getName() + " is not an alias to a fixed-width numeric type."
85+
)
86+
}
87+
88+
/** Holds if `element` violates the legacy directive, with `message` explaining why. */
89+
predicate problems(Element element, string message) {
90+
exists(Variable variable | variable = element |
91+
variable.getType() instanceof NumericType and
92+
not variable instanceof ExcludedVariable and
93+
message = "The type " + variable.getType() + " is not a fixed-width numeric type."
94+
)
95+
or
96+
forbiddenAlias(element, message)
97+
}
98+
99+
/** Holds if `element` declares or aliases a real or complex floating-point type. */
100+
predicate hasFloatingType(Element element) {
101+
exists(Type type |
102+
type = element.(Variable).getType() or type = element.(CTypedefType).getBaseType()
103+
|
104+
type.stripType() instanceof FloatingPointType
105+
or
106+
type.stripType() instanceof ComplexNumberType
107+
)
108+
}
109+
110+
/** Holds if `element` violates the integer-only directive, with `message` explaining why. */
111+
predicate integerProblems(Element element, string message) {
112+
not hasFloatingType(element) and
113+
(
114+
exists(Variable variable | variable = element |
115+
variable.getType() instanceof NumericType and
116+
not variable instanceof ExcludedVariable and
117+
message = "The type '" + variable.getType() + "' is not a fixed-width integer type."
118+
)
119+
or
120+
exists(CTypedefType type | type = element and forbiddenAlias(type, _) |
121+
if type.getName() instanceof SizedTypeName
122+
then
123+
if type.getName().(SizedTypeName).isComplex()
124+
then message = "The typedef type '" + type.getName() + "' is not a complex type."
125+
else message = "The typedef type '" + type.getName() + "' does not have its indicated size."
126+
else
127+
message = "The type '" + type.getName() + "' is not an alias to a fixed-width integer type."
128+
)
129+
)
130+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @id c/misra/plain-integer-type-used-over-explicit-typedef
3+
* @name DIR-4-6: Plain integer types should use explicit typedefs
4+
* @description Using integer typedefs makes their size and signedness explicit.
5+
* @kind problem
6+
* @precision high
7+
* @problem.severity recommendation
8+
* @tags external/misra/id/dir-4-6
9+
* correctness
10+
* maintainability
11+
* external/misra/c/2025
12+
* external/misra/obligation/advisory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.c.misra.PlainNumericalTypes
18+
19+
from Element element, string message
20+
where
21+
not isExcluded(element, Types1Package::plainIntegerTypeUsedOverExplicitTypedefQuery()) and
22+
integerProblems(element, message)
23+
select element, message

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

Lines changed: 3 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -8,110 +8,16 @@
88
* @problem.severity error
99
* @tags external/misra/id/dir-4-6
1010
* external/misra/c/2012/third-edition-first-revision
11+
* external/misra/legacy
1112
* external/misra/obligation/advisory
1213
*/
1314

1415
import cpp
1516
import codingstandards.c.misra
16-
import codingstandards.cpp.BuiltInNumericTypes
17-
18-
class BuiltInNumericType extends BuiltInType {
19-
BuiltInNumericType() {
20-
/* Exclude the plain char because it does not count as a numeric type */
21-
this.(CharType).isExplicitlySigned()
22-
or
23-
this.(CharType).isExplicitlyUnsigned()
24-
or
25-
this instanceof BuiltInIntegerType
26-
or
27-
this instanceof FloatType
28-
or
29-
this instanceof DoubleType
30-
or
31-
this instanceof LongDoubleType
32-
or
33-
this instanceof ComplexNumberType
34-
}
35-
}
36-
37-
predicate forbiddenBuiltinNumericUsedInDecl(Variable var, string message) {
38-
var.getType() instanceof BuiltInNumericType and
39-
not var instanceof ExcludedVariable and
40-
message = "The type " + var.getType() + " is not a fixed-width numeric type."
41-
}
42-
43-
class SizedTypeString extends string {
44-
string pattern;
45-
int size;
46-
47-
bindingset[this]
48-
pragma[inline]
49-
SizedTypeString() {
50-
pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and
51-
this.regexpMatch(pattern) and
52-
size = this.regexpCapture(pattern, 2).toInt()
53-
}
54-
55-
bindingset[this]
56-
pragma[inline]
57-
int getSize() { result = size }
58-
59-
bindingset[this]
60-
pragma[inline]
61-
predicate isComplex() { this.charAt(0) = "c" }
62-
}
63-
64-
predicate forbiddenComplexType(CTypedefType typedef, string message) {
65-
typedef.getName().(SizedTypeString).isComplex() and
66-
(
67-
if typedef.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
68-
then
69-
typedef.getSize() * 8 != typedef.getName().(SizedTypeString).getSize() * 2 and
70-
message = "The typedef type " + typedef.getName() + " does not have its indicated real size."
71-
else message = "The typedef type " + typedef.getName() + " is not a complex type."
72-
)
73-
}
74-
75-
predicate forbiddenRealType(CTypedefType typedef, string message) {
76-
not typedef.getName().(SizedTypeString).isComplex() and
77-
(
78-
if typedef.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType
79-
then message = "The typedef name " + typedef.getName() + " does not indicate a complex type."
80-
else (
81-
typedef.getSize() * 8 != typedef.getName().(SizedTypeString).getSize() and
82-
message = "The typedef type " + typedef.getName() + " does not have its indicated size."
83-
)
84-
)
85-
}
86-
87-
predicate forbiddenTypedef(CTypedefType typedef, string message) {
88-
/* If the typedef's name contains an explicit size */
89-
(
90-
if typedef.getName() instanceof SizedTypeString
91-
then (
92-
forbiddenRealType(typedef, message)
93-
or
94-
forbiddenComplexType(typedef, message)
95-
) else (
96-
(
97-
// type def is to a built in numeric type
98-
typedef.getBaseType() instanceof BuiltInNumericType and
99-
// but does not include the size in the name
100-
not typedef.getName() instanceof SizedTypeString
101-
or
102-
// this is a typedef to a forbidden type def
103-
forbiddenTypedef(typedef.getBaseType(), _)
104-
) and
105-
message = "The type " + typedef.getName() + " is not an alias to a fixed-width numeric type."
106-
)
107-
)
108-
}
17+
import codingstandards.c.misra.PlainNumericalTypes
10918

11019
from Element elem, string message
11120
where
11221
not isExcluded(elem, Types1Package::plainNumericalTypeUsedOverExplicitTypedefQuery()) and
113-
(
114-
forbiddenBuiltinNumericUsedInDecl(elem, message) or
115-
forbiddenTypedef(elem, message)
116-
)
22+
problems(elem, message)
11723
select elem, message
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
| test.c:9:26:9:31 | int4_t | The typedef type 'int4_t' does not have its indicated size. |
2+
| test.c:10:28:10:34 | uint4_t | The typedef type 'uint4_t' does not have its indicated size. |
3+
| test.c:13:13:13:27 | plain_int_alias | The type 'plain_int_alias' is not an alias to a fixed-width integer type. |
4+
| test.c:23:15:23:16 | c2 | The type 'signed char' is not a fixed-width integer type. |
5+
| test.c:24:17:24:18 | c3 | The type 'unsigned char' is not a fixed-width integer type. |
6+
| test.c:27:9:27:10 | s1 | The type 'short' is not a fixed-width integer type. |
7+
| test.c:28:16:28:17 | s2 | The type 'signed short' is not a fixed-width integer type. |
8+
| test.c:29:18:29:19 | s3 | The type 'unsigned short' is not a fixed-width integer type. |
9+
| test.c:32:7:32:8 | i1 | The type 'int' is not a fixed-width integer type. |
10+
| test.c:33:14:33:15 | i2 | The type 'signed int' is not a fixed-width integer type. |
11+
| test.c:34:16:34:17 | i3 | The type 'unsigned int' is not a fixed-width integer type. |
12+
| test.c:37:8:37:9 | l1 | The type 'long' is not a fixed-width integer type. |
13+
| test.c:38:15:38:16 | l2 | The type 'signed long' is not a fixed-width integer type. |
14+
| test.c:39:17:39:18 | l3 | The type 'unsigned long' is not a fixed-width integer type. |
15+
| test.c:42:13:42:15 | ll1 | The type 'long long' is not a fixed-width integer type. |
16+
| test.c:43:20:43:22 | ll2 | The type 'signed long long' is not a fixed-width integer type. |
17+
| test.c:44:22:44:24 | ll3 | The type 'unsigned long long' is not a fixed-width integer type. |
18+
| test.c:54:15:54:24 | cfloat32_t | The typedef type 'cfloat32_t' is not a complex type. |
19+
| test.c:55:15:55:24 | cfloat16_t | The typedef type 'cfloat16_t' is not a complex type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
typedef signed char int8_t; // COMPLIANT
2+
typedef unsigned char uint8_t; // COMPLIANT
3+
typedef signed short int16_t; // COMPLIANT
4+
typedef unsigned short uint16_t; // COMPLIANT
5+
typedef signed int int32_t; // COMPLIANT
6+
typedef unsigned int uint32_t; // COMPLIANT
7+
typedef signed long int64_t; // COMPLIANT
8+
typedef unsigned long uint64_t; // COMPLIANT
9+
typedef signed long long int4_t; // NON_COMPLIANT - wrong indicated size
10+
typedef unsigned long long uint4_t; // NON_COMPLIANT - wrong indicated size
11+
12+
typedef int8_t count_t; // COMPLIANT
13+
typedef int plain_int_alias; // NON_COMPLIANT - aliases a plain integer type
14+
15+
typedef float real_type; // COMPLIANT - floating-point excluded
16+
typedef real_type real_alias; // COMPLIANT - floating-point excluded
17+
typedef double _Complex float64_t; // COMPLIANT - floating-point excluded
18+
typedef float cfloat32_t; // COMPLIANT - floating-point excluded
19+
typedef double _Complex cfloat128_t; // COMPLIANT - floating-point excluded
20+
21+
int main(int argc, char *argv[]) { // COMPLIANT - main signature exception
22+
char c1 = 1; // COMPLIANT - plain char excluded
23+
signed char c2 = 1; // NON_COMPLIANT
24+
unsigned char c3 = 1; // NON_COMPLIANT
25+
int8_t c4 = 1; // COMPLIANT
26+
27+
short s1 = 1; // NON_COMPLIANT
28+
signed short s2 = 1; // NON_COMPLIANT
29+
unsigned short s3 = 1; // NON_COMPLIANT
30+
int16_t s4 = 1; // COMPLIANT
31+
32+
int i1 = 1; // NON_COMPLIANT
33+
signed int i2 = 1; // NON_COMPLIANT
34+
unsigned int i3 = 1; // NON_COMPLIANT
35+
int32_t i4 = 1; // COMPLIANT
36+
37+
long l1 = 1; // NON_COMPLIANT
38+
signed long l2 = 1; // NON_COMPLIANT
39+
unsigned long l3 = 1; // NON_COMPLIANT
40+
int64_t l4 = 1; // COMPLIANT
41+
42+
long long ll1 = 1; // NON_COMPLIANT
43+
signed long long ll2 = 1; // NON_COMPLIANT
44+
unsigned long long ll3 = 1; // NON_COMPLIANT
45+
46+
float f1 = 1.0f; // COMPLIANT - floating-point excluded
47+
double d1 = 1.0; // COMPLIANT - floating-point excluded
48+
long double ld1 = 1.0L; // COMPLIANT - floating-point excluded
49+
float _Complex cf1 = 1.0f; // COMPLIANT - floating-point excluded
50+
return 0;
51+
}
52+
53+
void test_complex_names(void) {
54+
typedef int cfloat32_t; // NON_COMPLIANT - matching width, but not complex
55+
typedef int cfloat16_t; // NON_COMPLIANT - mismatched width, but not complex
56+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- `DIR-4-6` - `PlainIntegerTypeUsedOverExplicitTypedef.ql`:
2+
- The MISRA C:2025 query checks integer types and excludes floating-point declarations and
3+
aliases. Earlier editions retain the numerical-type query.
4+
- Integer typedefs with complex-type names report that the type is not complex, rather than an
5+
incorrect size.

‎cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ newtype Types1Query =
88
TConvertingAPointerToIntegerOrIntegerToPointerQuery() or
99
TPlainNumericalTypeUsedOverExplicitTypedefQuery() or
1010
TSizeofOperatorUsedOnArrayTypeParamQuery() or
11-
TStringLiteralAssignedToNonConstCharQuery()
11+
TStringLiteralAssignedToNonConstCharQuery() or
12+
TPlainIntegerTypeUsedOverExplicitTypedefQuery()
1213

1314
predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, string category) {
1415
query =
@@ -55,6 +56,15 @@ predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, stri
5556
"c/misra/string-literal-assigned-to-non-const-char" and
5657
ruleId = "RULE-7-4" and
5758
category = "required"
59+
or
60+
query =
61+
// `Query` instance for the `plainIntegerTypeUsedOverExplicitTypedef` query
62+
Types1Package::plainIntegerTypeUsedOverExplicitTypedefQuery() and
63+
queryId =
64+
// `@id` for the `plainIntegerTypeUsedOverExplicitTypedef` query
65+
"c/misra/plain-integer-type-used-over-explicit-typedef" and
66+
ruleId = "DIR-4-6" and
67+
category = "advisory"
5868
}
5969

6070
module Types1Package {
@@ -92,4 +102,11 @@ module Types1Package {
92102
// `Query` type for `stringLiteralAssignedToNonConstChar` query
93103
TQueryC(TTypes1PackageQuery(TStringLiteralAssignedToNonConstCharQuery()))
94104
}
105+
106+
Query plainIntegerTypeUsedOverExplicitTypedefQuery() {
107+
//autogenerate `Query` type
108+
result =
109+
// `Query` type for `plainIntegerTypeUsedOverExplicitTypedef` query
110+
TQueryC(TTypes1PackageQuery(TPlainIntegerTypeUsedOverExplicitTypedefQuery()))
111+
}
95112
}

0 commit comments

Comments
 (0)