Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions c/misra/src/codingstandards/c/misra/PlainNumericalTypes.qll
Original file line number Diff line number Diff line change
@@ -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."
)
)
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c/misra/test/rules/DIR-4-6/integer-only/PlainIntegerTypeUsedOverExplicitTypedef.qlref
Original file line number Diff line number Diff line change
@@ -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. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/DIR-4-6/PlainIntegerTypeUsedOverExplicitTypedef.ql
56 changes: 56 additions & 0 deletions c/misra/test/rules/DIR-4-6/integer-only/test.c
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions change_notes/2026-11-19-misra-c-2025-dir-4-6.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 18 additions & 1 deletion cpp/common/src/codingstandards/cpp/exclusions/c/Types1.qll
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()))
}
}
Loading
Loading