Skip to content

Commit 33775f0

Browse files
mbaludaCopilot
andcommitted
Defer the MISRA C:2025 DIR-4-6 refinement
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 8e5afab commit 33775f0

13 files changed

Lines changed: 108 additions & 211 deletions

‎c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll‎

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

Lines changed: 0 additions & 24 deletions
This file was deleted.

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

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,110 @@
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
1211
* external/misra/obligation/advisory
1312
*/
1413

1514
import cpp
1615
import codingstandards.c.misra
17-
import codingstandards.c.misra.PlainNumericalTypes
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+
}
18109

19110
from Element elem, string message
20111
where
21112
not isExcluded(elem, Types1Package::plainNumericalTypeUsedOverExplicitTypedefQuery()) and
22-
problems(elem, message)
113+
(
114+
forbiddenBuiltinNumericUsedInDecl(elem, message) or
115+
forbiddenTypedef(elem, message)
116+
)
23117
select elem, message

‎c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.expected‎

Lines changed: 0 additions & 17 deletions
This file was deleted.

‎c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.qlref‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎c/misra/test/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.expected‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@
2222
| test.c:65:10:65:11 | d1 | The type double is not a fixed-width numeric type. |
2323
| test.c:68:15:68:17 | ld1 | The type long double is not a fixed-width numeric type. |
2424
| test.c:71:18:71:20 | cf1 | The type _Complex float is not a fixed-width numeric type. |
25-
| test_MISRA_C_2025.c:1:15:1:23 | real_type | The type real_type is not an alias to a fixed-width numeric type. |
26-
| test_MISRA_C_2025.c:2:19:2:28 | real_alias | The type real_alias is not an alias to a fixed-width numeric type. |

‎c/misra/test/rules/DIR-4-6/test.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ typedef unsigned long long
1616
uint4_t; // NON_COMPLIANT: typedef does not have its indicated size
1717

1818
typedef float float32_t; // COMPLIANT: exception, typedefs are permitted
19-
typedef double _Complex float64_t; // NON_COMPLIANT (MISRA-C-2012 only)
19+
typedef double _Complex float64_t; // NON-COMPLIANT: not complex floating type
2020
typedef long double float128_t; // COMPLIANT: exception, typedefs are permitted
2121

22-
typedef float cfloat32_t; // NON_COMPLIANT (MISRA-C-2012 only)
22+
typedef float cfloat32_t; // NON-COMPLIANT: not a complex floating type
2323
typedef double _Complex cfloat64_t; // COMPLIANT: correct complex floating type
24-
typedef double _Complex cfloat128_t; // NON_COMPLIANT (MISRA-C-2012 only)
24+
typedef double _Complex cfloat128_t; // NON-COMPLIANT: incorrect complex size
2525

2626
typedef int8_t
2727
astronomical_number_t; // COMPLIANT: aliasing a fixed-width numeric typedef
@@ -59,15 +59,15 @@ main(int argc, // COMPLIANT: exception, argc's type can be plain int
5959
unsigned long long ll3 = 1; // NON_COMPLIANT: use typedef uint64_t
6060
int64_t ll4 = 1; // COMPLIANT: typedef used instead
6161

62-
float f1 = 1; // NON_COMPLIANT (MISRA-C-2012 only)
62+
float f1 = 1; // NON_COMPLIANT: float is a basic numeric type, use a typedef
6363
float32_t f2 = 1; // COMPLIANT: typedef used instead
6464

65-
double d1 = 1; // NON_COMPLIANT (MISRA-C-2012 only)
65+
double d1 = 1; // NON_COMPLIANT: int is a basic numeric type
6666
float64_t d2 = 1; // COMPLIANT: typedef used instead
6767

68-
long double ld1 = 1; // NON_COMPLIANT (MISRA-C-2012 only)
68+
long double ld1 = 1; // NON_COMPLIANT: int is a basic numeric type
6969
float128_t ld2 = 1; // COMPLIANT: typedef used instead
7070

71-
float _Complex cf1 = 1; // NON_COMPLIANT (MISRA-C-2012 only)
71+
float _Complex cf1 = 1; // NON_COMPLIANT: complex basic numeric type
7272
cfloat64_t cf2 = 1; // COMPLIANT: typedef used instead
7373
}

‎c/misra/test/rules/DIR-4-6/test_MISRA_C_2025.c‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎change_notes/2026-11-03-misra-c-2025-query-updates.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Explicit 2023 and 2012 suites retain their guideline sets and query IDs.
33
- The supported-rules report lists unchanged applicable guidelines under MISRA C:2025
44
while retaining their older-edition entries.
5-
- `DIR-4-6` - `PlainIntegerTypeUsedOverExplicitTypedef.ql`:
6-
- The 2025 query excludes floating-point declarations and aliases.
75
- `RULE-15-5` - `SinglePointOfExit.ql`:
86
- Disapplied by default; recategorization can reactivate the query.
97
- `RULE-17-6` - `UseOfArrayStatic.ql`:

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

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

1413
predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, string category) {
1514
query =
@@ -56,15 +55,6 @@ predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, stri
5655
"c/misra/string-literal-assigned-to-non-const-char" and
5756
ruleId = "RULE-7-4" and
5857
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"
6858
}
6959

7060
module Types1Package {
@@ -102,11 +92,4 @@ module Types1Package {
10292
// `Query` type for `stringLiteralAssignedToNonConstChar` query
10393
TQueryC(TTypes1PackageQuery(TStringLiteralAssignedToNonConstCharQuery()))
10494
}
105-
106-
Query plainIntegerTypeUsedOverExplicitTypedefQuery() {
107-
//autogenerate `Query` type
108-
result =
109-
// `Query` type for `plainIntegerTypeUsedOverExplicitTypedef` query
110-
TQueryC(TTypes1PackageQuery(TPlainIntegerTypeUsedOverExplicitTypedefQuery()))
111-
}
11295
}

0 commit comments

Comments
 (0)