From c60f8beb11aa7fd20c2ff2cf016ea7b7196f9f26 Mon Sep 17 00:00:00 2001 From: Mauro Baluda Date: Thu, 19 Nov 2026 11:50:09 +0100 Subject: [PATCH] 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> --- .../c/misra/PlainNumericalTypes.qll | 130 ++++++++++++++++++ ...PlainIntegerTypeUsedOverExplicitTypedef.ql | 23 ++++ ...ainNumericalTypeUsedOverExplicitTypedef.ql | 100 +------------- ...IntegerTypeUsedOverExplicitTypedef.testref | 1 + ...ntegerTypeUsedOverExplicitTypedef.expected | 19 +++ ...inIntegerTypeUsedOverExplicitTypedef.qlref | 1 + .../test/rules/DIR-4-6/integer-only/test.c | 56 ++++++++ .../2026-11-19-misra-c-2025-dir-4-6.md | 5 + .../cpp/exclusions/c/Types1.qll | 19 ++- rule_packages/c/Types1.json | 26 +++- rules-misra-c-2025.csv | 2 +- 11 files changed, 282 insertions(+), 100 deletions(-) create mode 100644 c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll create mode 100644 c/misra/src/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql create mode 100644 c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.testref create mode 100644 c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.expected create mode 100644 c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref create mode 100644 c/misra/test/rules/DIR-4-6/integer-only/test.c create mode 100644 change_notes/2026-11-19-misra-c-2025-dir-4-6.md diff --git a/c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll b/c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll new file mode 100644 index 0000000000..5041de1427 --- /dev/null +++ b/c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll @@ -0,0 +1,130 @@ +/** Provides the numeric-type detector shared by legacy and current directive wrappers. */ + +import cpp +import codingstandards.cpp.BuiltInNumericTypes + +/** A numeric type covered by the legacy directive. */ +private class NumericType extends BuiltInType { + NumericType() { + this.(CharType).isExplicitlySigned() + or + this.(CharType).isExplicitlyUnsigned() + or + this instanceof BuiltInIntegerType + or + this instanceof FloatType + or + this instanceof DoubleType + or + this instanceof LongDoubleType + or + this instanceof ComplexNumberType + } +} + +/** A typedef name that encodes numeric width and signedness. */ +private class SizedTypeName extends string { + string pattern; + int width; + + bindingset[this] + pragma[inline] + SizedTypeName() { + pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and + this.regexpMatch(pattern) and + width = this.regexpCapture(pattern, 2).toInt() + } + + /** Gets the width encoded by this name. */ + bindingset[this] + pragma[inline] + int getWidth() { result = width } + + /** Holds if this name denotes a complex type. */ + bindingset[this] + pragma[inline] + predicate isComplex() { this.charAt(0) = "c" } +} + +/** Holds if `type` has an invalid complex name, with `message` explaining why. */ +private predicate forbiddenComplexType(CTypedefType type, string message) { + type.getName().(SizedTypeName).isComplex() and + ( + if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType + then + type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() * 2 and + message = "The typedef type " + type.getName() + " does not have its indicated real size." + else message = "The typedef type " + type.getName() + " is not a complex type." + ) +} + +/** Holds if `type` has an invalid real name, with `message` explaining why. */ +private predicate forbiddenRealType(CTypedefType type, string message) { + not type.getName().(SizedTypeName).isComplex() and + ( + if type.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType + then message = "The typedef name " + type.getName() + " does not indicate a complex type." + else ( + type.getSize() * 8 != type.getName().(SizedTypeName).getWidth() and + message = "The typedef type " + type.getName() + " does not have its indicated size." + ) + ) +} + +/** Holds if `type` is a forbidden alias, with `message` explaining why. */ +private predicate forbiddenAlias(CTypedefType type, string message) { + if type.getName() instanceof SizedTypeName + then forbiddenRealType(type, message) or forbiddenComplexType(type, message) + else ( + ( + type.getBaseType() instanceof NumericType + or + forbiddenAlias(type.getBaseType(), _) + ) and + message = "The type " + type.getName() + " is not an alias to a fixed-width numeric type." + ) +} + +/** Holds if `element` violates the legacy directive, with `message` explaining why. */ +predicate problems(Element element, string message) { + exists(Variable variable | variable = element | + variable.getType() instanceof NumericType and + not variable instanceof ExcludedVariable and + message = "The type " + variable.getType() + " is not a fixed-width numeric type." + ) + or + forbiddenAlias(element, message) +} + +/** Holds if `element` declares or aliases a real or complex floating-point type. */ +predicate hasFloatingType(Element element) { + exists(Type type | + type = element.(Variable).getType() or type = element.(CTypedefType).getBaseType() + | + type.stripType() instanceof FloatingPointType + or + type.stripType() instanceof ComplexNumberType + ) +} + +/** Holds if `element` violates the integer-only directive, with `message` explaining why. */ +predicate integerProblems(Element element, string message) { + not hasFloatingType(element) and + ( + exists(Variable variable | variable = element | + variable.getType() instanceof NumericType and + not variable instanceof ExcludedVariable and + message = "The type '" + variable.getType() + "' is not a fixed-width integer type." + ) + or + exists(CTypedefType type | type = element and forbiddenAlias(type, _) | + if type.getName() instanceof SizedTypeName + then + if type.getName().(SizedTypeName).isComplex() + then message = "The typedef type '" + type.getName() + "' is not a complex type." + else message = "The typedef type '" + type.getName() + "' does not have its indicated size." + else + message = "The type '" + type.getName() + "' is not an alias to a fixed-width integer type." + ) + ) +} diff --git a/c/misra/src/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql b/c/misra/src/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql new file mode 100644 index 0000000000..fdcfced4d1 --- /dev/null +++ b/c/misra/src/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql @@ -0,0 +1,23 @@ +/** + * @id c/misra/plain-integer-type-used-over-explicit-typedef + * @name DIR-4-6: Plain integer types should use explicit typedefs + * @description Using integer typedefs makes their size and signedness explicit. + * @kind problem + * @precision high + * @problem.severity recommendation + * @tags external/misra/id/dir-4-6 + * correctness + * maintainability + * external/misra/c/2025 + * external/misra/obligation/advisory + */ + +import cpp +import codingstandards.c.misra +import codingstandards.c.misra.PlainNumericalTypes + +from Element element, string message +where + not isExcluded(element, Types1Package::plainIntegerTypeUsedOverExplicitTypedefQuery()) and + integerProblems(element, message) +select element, message diff --git a/c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql b/c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql index 0e6c902441..9d16a2127d 100644 --- a/c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql +++ b/c/misra/src/rules/DIR-4-6/PlainNumericalTypeUsedOverExplicitTypedef.ql @@ -8,110 +8,16 @@ * @problem.severity error * @tags external/misra/id/dir-4-6 * external/misra/c/2012/third-edition-first-revision + * external/misra/legacy * external/misra/obligation/advisory */ import cpp import codingstandards.c.misra -import codingstandards.cpp.BuiltInNumericTypes - -class BuiltInNumericType extends BuiltInType { - BuiltInNumericType() { - /* Exclude the plain char because it does not count as a numeric type */ - this.(CharType).isExplicitlySigned() - or - this.(CharType).isExplicitlyUnsigned() - or - this instanceof BuiltInIntegerType - or - this instanceof FloatType - or - this instanceof DoubleType - or - this instanceof LongDoubleType - or - this instanceof ComplexNumberType - } -} - -predicate forbiddenBuiltinNumericUsedInDecl(Variable var, string message) { - var.getType() instanceof BuiltInNumericType and - not var instanceof ExcludedVariable and - message = "The type " + var.getType() + " is not a fixed-width numeric type." -} - -class SizedTypeString extends string { - string pattern; - int size; - - bindingset[this] - pragma[inline] - SizedTypeString() { - pattern = "(u?int|c?float)(4|8|16|32|64|128)_t" and - this.regexpMatch(pattern) and - size = this.regexpCapture(pattern, 2).toInt() - } - - bindingset[this] - pragma[inline] - int getSize() { result = size } - - bindingset[this] - pragma[inline] - predicate isComplex() { this.charAt(0) = "c" } -} - -predicate forbiddenComplexType(CTypedefType typedef, string message) { - typedef.getName().(SizedTypeString).isComplex() and - ( - if typedef.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType - then - typedef.getSize() * 8 != typedef.getName().(SizedTypeString).getSize() * 2 and - message = "The typedef type " + typedef.getName() + " does not have its indicated real size." - else message = "The typedef type " + typedef.getName() + " is not a complex type." - ) -} - -predicate forbiddenRealType(CTypedefType typedef, string message) { - not typedef.getName().(SizedTypeString).isComplex() and - ( - if typedef.getBaseType().stripTopLevelSpecifiers() instanceof ComplexNumberType - then message = "The typedef name " + typedef.getName() + " does not indicate a complex type." - else ( - typedef.getSize() * 8 != typedef.getName().(SizedTypeString).getSize() and - message = "The typedef type " + typedef.getName() + " does not have its indicated size." - ) - ) -} - -predicate forbiddenTypedef(CTypedefType typedef, string message) { - /* If the typedef's name contains an explicit size */ - ( - if typedef.getName() instanceof SizedTypeString - then ( - forbiddenRealType(typedef, message) - or - forbiddenComplexType(typedef, message) - ) else ( - ( - // type def is to a built in numeric type - typedef.getBaseType() instanceof BuiltInNumericType and - // but does not include the size in the name - not typedef.getName() instanceof SizedTypeString - or - // this is a typedef to a forbidden type def - forbiddenTypedef(typedef.getBaseType(), _) - ) and - message = "The type " + typedef.getName() + " is not an alias to a fixed-width numeric type." - ) - ) -} +import codingstandards.c.misra.PlainNumericalTypes from Element elem, string message where not isExcluded(elem, Types1Package::plainNumericalTypeUsedOverExplicitTypedefQuery()) and - ( - forbiddenBuiltinNumericUsedInDecl(elem, message) or - forbiddenTypedef(elem, message) - ) + problems(elem, message) select elem, message diff --git a/c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.testref b/c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.testref new file mode 100644 index 0000000000..e27f524dd8 --- /dev/null +++ b/c/misra/test/rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.testref @@ -0,0 +1 @@ +c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref diff --git a/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.expected b/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.expected new file mode 100644 index 0000000000..ef38009367 --- /dev/null +++ b/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.expected @@ -0,0 +1,19 @@ +| test.c:9:26:9:31 | int4_t | The typedef type 'int4_t' does not have its indicated size. | +| test.c:10:28:10:34 | uint4_t | The typedef type 'uint4_t' does not have its indicated size. | +| test.c:13:13:13:27 | plain_int_alias | The type 'plain_int_alias' is not an alias to a fixed-width integer type. | +| test.c:23:15:23:16 | c2 | The type 'signed char' is not a fixed-width integer type. | +| test.c:24:17:24:18 | c3 | The type 'unsigned char' is not a fixed-width integer type. | +| test.c:27:9:27:10 | s1 | The type 'short' is not a fixed-width integer type. | +| test.c:28:16:28:17 | s2 | The type 'signed short' is not a fixed-width integer type. | +| test.c:29:18:29:19 | s3 | The type 'unsigned short' is not a fixed-width integer type. | +| test.c:32:7:32:8 | i1 | The type 'int' is not a fixed-width integer type. | +| test.c:33:14:33:15 | i2 | The type 'signed int' is not a fixed-width integer type. | +| test.c:34:16:34:17 | i3 | The type 'unsigned int' is not a fixed-width integer type. | +| test.c:37:8:37:9 | l1 | The type 'long' is not a fixed-width integer type. | +| test.c:38:15:38:16 | l2 | The type 'signed long' is not a fixed-width integer type. | +| test.c:39:17:39:18 | l3 | The type 'unsigned long' is not a fixed-width integer type. | +| test.c:42:13:42:15 | ll1 | The type 'long long' is not a fixed-width integer type. | +| test.c:43:20:43:22 | ll2 | The type 'signed long long' is not a fixed-width integer type. | +| test.c:44:22:44:24 | ll3 | The type 'unsigned long long' is not a fixed-width integer type. | +| test.c:54:15:54:24 | cfloat32_t | The typedef type 'cfloat32_t' is not a complex type. | +| test.c:55:15:55:24 | cfloat16_t | The typedef type 'cfloat16_t' is not a complex type. | diff --git a/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref b/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref new file mode 100644 index 0000000000..5cd8e7b310 --- /dev/null +++ b/c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref @@ -0,0 +1 @@ +rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql diff --git a/c/misra/test/rules/DIR-4-6/integer-only/test.c b/c/misra/test/rules/DIR-4-6/integer-only/test.c new file mode 100644 index 0000000000..3e7e23fa79 --- /dev/null +++ b/c/misra/test/rules/DIR-4-6/integer-only/test.c @@ -0,0 +1,56 @@ +typedef signed char int8_t; // COMPLIANT +typedef unsigned char uint8_t; // COMPLIANT +typedef signed short int16_t; // COMPLIANT +typedef unsigned short uint16_t; // COMPLIANT +typedef signed int int32_t; // COMPLIANT +typedef unsigned int uint32_t; // COMPLIANT +typedef signed long int64_t; // COMPLIANT +typedef unsigned long uint64_t; // COMPLIANT +typedef signed long long int4_t; // NON_COMPLIANT - wrong indicated size +typedef unsigned long long uint4_t; // NON_COMPLIANT - wrong indicated size + +typedef int8_t count_t; // COMPLIANT +typedef int plain_int_alias; // NON_COMPLIANT - aliases a plain integer type + +typedef float real_type; // COMPLIANT - floating-point excluded +typedef real_type real_alias; // COMPLIANT - floating-point excluded +typedef double _Complex float64_t; // COMPLIANT - floating-point excluded +typedef float cfloat32_t; // COMPLIANT - floating-point excluded +typedef double _Complex cfloat128_t; // COMPLIANT - floating-point excluded + +int main(int argc, char *argv[]) { // COMPLIANT - main signature exception + char c1 = 1; // COMPLIANT - plain char excluded + signed char c2 = 1; // NON_COMPLIANT + unsigned char c3 = 1; // NON_COMPLIANT + int8_t c4 = 1; // COMPLIANT + + short s1 = 1; // NON_COMPLIANT + signed short s2 = 1; // NON_COMPLIANT + unsigned short s3 = 1; // NON_COMPLIANT + int16_t s4 = 1; // COMPLIANT + + int i1 = 1; // NON_COMPLIANT + signed int i2 = 1; // NON_COMPLIANT + unsigned int i3 = 1; // NON_COMPLIANT + int32_t i4 = 1; // COMPLIANT + + long l1 = 1; // NON_COMPLIANT + signed long l2 = 1; // NON_COMPLIANT + unsigned long l3 = 1; // NON_COMPLIANT + int64_t l4 = 1; // COMPLIANT + + long long ll1 = 1; // NON_COMPLIANT + signed long long ll2 = 1; // NON_COMPLIANT + unsigned long long ll3 = 1; // NON_COMPLIANT + + float f1 = 1.0f; // COMPLIANT - floating-point excluded + double d1 = 1.0; // COMPLIANT - floating-point excluded + long double ld1 = 1.0L; // COMPLIANT - floating-point excluded + float _Complex cf1 = 1.0f; // COMPLIANT - floating-point excluded + return 0; +} + +void test_complex_names(void) { + typedef int cfloat32_t; // NON_COMPLIANT - matching width, but not complex + typedef int cfloat16_t; // NON_COMPLIANT - mismatched width, but not complex +} diff --git a/change_notes/2026-11-19-misra-c-2025-dir-4-6.md b/change_notes/2026-11-19-misra-c-2025-dir-4-6.md new file mode 100644 index 0000000000..8bc24e8ca0 --- /dev/null +++ b/change_notes/2026-11-19-misra-c-2025-dir-4-6.md @@ -0,0 +1,5 @@ +- `DIR-4-6` - `PlainIntegerTypeUsedOverExplicitTypedef.ql`: + - The MISRA C:2025 query checks integer types and excludes floating-point declarations and + aliases. Earlier editions retain the numerical-type query. + - Integer typedefs with complex-type names report that the type is not complex, rather than an + incorrect size. diff --git a/cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll b/cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll index ab7333b4c0..d4ef1a2252 100644 --- a/cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll +++ b/cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll @@ -8,7 +8,8 @@ newtype Types1Query = TConvertingAPointerToIntegerOrIntegerToPointerQuery() or TPlainNumericalTypeUsedOverExplicitTypedefQuery() or TSizeofOperatorUsedOnArrayTypeParamQuery() or - TStringLiteralAssignedToNonConstCharQuery() + TStringLiteralAssignedToNonConstCharQuery() or + TPlainIntegerTypeUsedOverExplicitTypedefQuery() predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, string category) { query = @@ -55,6 +56,15 @@ predicate isTypes1QueryMetadata(Query query, string queryId, string ruleId, stri "c/misra/string-literal-assigned-to-non-const-char" and ruleId = "RULE-7-4" and category = "required" + or + query = + // `Query` instance for the `plainIntegerTypeUsedOverExplicitTypedef` query + Types1Package::plainIntegerTypeUsedOverExplicitTypedefQuery() and + queryId = + // `@id` for the `plainIntegerTypeUsedOverExplicitTypedef` query + "c/misra/plain-integer-type-used-over-explicit-typedef" and + ruleId = "DIR-4-6" and + category = "advisory" } module Types1Package { @@ -92,4 +102,11 @@ module Types1Package { // `Query` type for `stringLiteralAssignedToNonConstChar` query TQueryC(TTypes1PackageQuery(TStringLiteralAssignedToNonConstCharQuery())) } + + Query plainIntegerTypeUsedOverExplicitTypedefQuery() { + //autogenerate `Query` type + result = + // `Query` type for `plainIntegerTypeUsedOverExplicitTypedef` query + TQueryC(TTypes1PackageQuery(TPlainIntegerTypeUsedOverExplicitTypedefQuery())) + } } diff --git a/rule_packages/c/Types1.json b/rule_packages/c/Types1.json index bb451eba70..18dfd0748d 100644 --- a/rule_packages/c/Types1.json +++ b/rule_packages/c/Types1.json @@ -61,7 +61,8 @@ "severity": "error", "short_name": "PlainNumericalTypeUsedOverExplicitTypedef", "tags": [ - "external/misra/c/2012/third-edition-first-revision" + "external/misra/c/2012/third-edition-first-revision", + "external/misra/legacy" ] } ], @@ -105,5 +106,28 @@ ], "title": "A string literal shall not be assigned to an object unless the object's type is 'pointer to const-qualified char'" } + }, + "MISRA-C-2025": { + "DIR-4-6": { + "properties": { + "obligation": "advisory" + }, + "queries": [ + { + "description": "Using integer typedefs makes their size and signedness explicit.", + "kind": "problem", + "name": "Plain integer types should use explicit typedefs", + "precision": "high", + "severity": "recommendation", + "short_name": "PlainIntegerTypeUsedOverExplicitTypedef", + "tags": [ + "correctness", + "maintainability", + "external/misra/c/2025" + ] + } + ], + "title": "typedefs that indicate size and signedness should be used in place of the basic integer types" + } } } \ No newline at end of file diff --git a/rules-misra-c-2025.csv b/rules-misra-c-2025.csv index 31aa6113d4..93b4c2b2d5 100644 --- a/rules-misra-c-2025.csv +++ b/rules-misra-c-2025.csv @@ -1,6 +1,6 @@ language,standard,amendment,rule_id,supportable,implementation_category,implemented,difficulty c,MISRA-C-2025,Edition2025,DIR-1-2,Yes,Renumber,Yes,Easy -c,MISRA-C-2025,Edition2025,DIR-4-6,Yes,Refine,No,Medium +c,MISRA-C-2025,Edition2025,DIR-4-6,Yes,Refine,Yes,Medium c,MISRA-C-2025,Edition2025,DIR-4-10,Yes,Expand,No,Medium c,MISRA-C-2025,Edition2025,RULE-1-2,Yes,Renumber,Yes,Easy c,MISRA-C-2025,Edition2025,RULE-5-10,Yes,Renumber,Yes,Easy