Skip to content

Commit 87a0a3d

Browse files
committed
Draft of 4-6
1 parent 7e837dc commit 87a0a3d

File tree

2 files changed

+73
-45
lines changed

2 files changed

+73
-45
lines changed

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,40 @@
1313
import cpp
1414
import codingstandards.c.misra
1515

16-
from
17-
where
18-
not isExcluded(x, TypesPackage::plainNumericalTypeUsedOverExplicitTypedefQuery()) and
19-
select
16+
abstract class ForbiddenType extends Type { }
17+
18+
class BuiltinNumericType extends ForbiddenType {
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 ShortType
26+
or
27+
this instanceof IntType
28+
or
29+
this instanceof LongType
30+
or
31+
this instanceof LongLongType
32+
or
33+
this instanceof FloatType
34+
or
35+
this instanceof DoubleType
36+
or
37+
this instanceof LongDoubleType
38+
}
39+
}
40+
41+
class ForbiddenTypedefType extends ForbiddenType, TypedefType {
42+
ForbiddenTypedefType() {
43+
this.(TypedefType).getBaseType() instanceof BuiltinNumericType and
44+
not this.getName().regexpMatch("u?(int|float)(4|8|16|32|64|128)_t")
45+
}
46+
}
47+
48+
/* TODO: BuiltinNumericType not being flagged */
49+
from ForbiddenType forbiddenType
50+
where not isExcluded(forbiddenType, TypesPackage::plainNumericalTypeUsedOverExplicitTypedefQuery())
51+
select forbiddenType,
52+
"The type " + forbiddenType + " is not a fixed-width numeric type nor an alias to one."

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

Lines changed: 36 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
1-
#include <stdint.h>
1+
typedef signed char int8_t; // COMPLIANT: exception, typedefs are permitted
2+
typedef unsigned char uint8_t; // COMPLIANT: exception, typedefs are permitted
23

3-
typedef signed char INT8; // COMPLIANT: exception, typedefs are permitted
4-
typedef unsigned char UINT8; // COMPLIANT: exception, typedefs are permitted
4+
typedef signed short int16_t; // COMPLIANT: exception, typedefs are permitted
5+
typedef unsigned short uint16_t; // COMPLIANT: exception, typedefs are permitted
56

6-
typedef short _INT16; // COMPLIANT: exception, typedefs are permitted
7-
typedef signed short INT16; // COMPLIANT: exception, typedefs are permitted
8-
typedef unsigned short UINT16; // COMPLIANT: exception, typedefs are permitted
7+
typedef signed int int32_t; // COMPLIANT: exception, typedefs are permitted
8+
typedef unsigned int uint32_t; // COMPLIANT: exception, typedefs are permitted
99

10-
typedef int _INT32; // COMPLIANT: exception, typedefs are permitted
11-
typedef signed int INT32; // COMPLIANT: exception, typedefs are permitted
12-
typedef unsigned int UINT32; // COMPLIANT: exception, typedefs are permitted
10+
typedef signed long int64_t; // COMPLIANT: exception, typedefs are permitted
11+
typedef unsigned long uint64_t; // COMPLIANT: exception, typedefs are permitted
1312

14-
typedef long _INT64; // COMPLIANT: exception, typedefs are permitted
15-
typedef signed long INT64; // COMPLIANT: exception, typedefs are permitted
16-
typedef unsigned long UINT64; // COMPLIANT: exception, typedefs are permitted
17-
18-
typedef long long _INT128; // COMPLIANT: exception, typedefs are permitted
19-
typedef signed long long INT128; // COMPLIANT: exception, typedefs are permitted
13+
typedef signed long long
14+
int128_t; // COMPLIANT: exception, typedefs are permitted
2015
typedef unsigned long long
21-
UINT128; // COMPLIANT: exception, typedefs are permitted
16+
uint128_t; // COMPLIANT: exception, typedefs are permitted
2217

23-
typedef float FLOAT32; // COMPLIANT: exception, typedefs are permitted
24-
typedef double FLOAT64; // COMPLIANT: exception, typedefs are permitted
25-
typedef long double FLOAT128; // COMPLIANT: exception, typedefs are permitted
18+
typedef float float32_t; // COMPLIANT: exception, typedefs are permitted
19+
typedef double float64_t; // COMPLIANT: exception, typedefs are permitted
20+
typedef long double float128_t; // COMPLIANT: exception, typedefs are permitted
2621

2722
typedef int8_t
2823
astronomical_number_t; // COMPLIANT: aliasing a fixed-width numeric typedef
2924
typedef uint8_t u_astronomical_number_t; // COMPLIANT: aliasing a fixed-width
3025
// numeric typedef
3126
typedef int
32-
astronomical_number_t; // NON_COMPLIANT: aliasing a basic numeric type
27+
_astronomical_number_t; // NON_COMPLIANT: aliasing a basic numeric type
3328

3429
int // COMPLIANT: exception, main's return type can be plain int
3530
main(int argc, // COMPLIANT: exception, argc's type can be plain int
3631
char *argv[]) { // COMPLIANT: char is not a basic numeric type
3732

3833
char c1 = 1; // COMPLIANT: char is not a basic numeric type
39-
signed char c2 = 1; // NON_COMPLIANT: use typedef int8_t in stdint
40-
unsigned char c3 = 1; // NON_COMPLIANT: use typedef uint8_t in stdint
41-
INT8 c4 = 1; // COMPLIANT: typedef used instead
34+
signed char c2 = 1; // NON_COMPLIANT: use typedef int8_t
35+
unsigned char c3 = 1; // NON_COMPLIANT: use typedef uint8_t
36+
int8_t c4 = 1; // COMPLIANT: typedef used instead
4237

4338
short s1 = 1; // NON_COMPLIANT: short is a basic numeric type
44-
signed short s2 = 1; // NON_COMPLIANT: use typedef int16_t in stdint
45-
unsigned short s3 = 1; // NON_COMPLIANT: use typedef uint16_t in stdint
46-
INT16 s4 = 1; // COMPLIANT: typedef used instead
39+
signed short s2 = 1; // NON_COMPLIANT: use typedef int16_t
40+
unsigned short s3 = 1; // NON_COMPLIANT: use typedef uint16_t
41+
int16_t s4 = 1; // COMPLIANT: typedef used instead
4742

4843
int i1 = 1; // NON_COMPLIANT: int is a basic numeric type
49-
signed int i2 = 1; // NON_COMPLIANT: use typedef int32_t in stdint
50-
unsigned int i3 = 1; // NON_COMPLIANT: use typedef uint32_t in stdint
51-
INT32 s4 = 1; // COMPLIANT: typedef used instead
44+
signed int i2 = 1; // NON_COMPLIANT: use typedef int32_t
45+
unsigned int i3 = 1; // NON_COMPLIANT: use typedef uint32_t
46+
int32_t i4 = 1; // COMPLIANT: typedef used instead
5247

5348
long l1 = 1; // NON_COMPLIANT: int is a basic numeric type
54-
signed long l2 = 1; // NON_COMPLIANT: use typedef int64_t in stdint
55-
unsigned long l3 = 1; // NON_COMPLIANT: use typedef uint64_t in stdint
56-
INT64 s4 = 1; // COMPLIANT: typedef used instead
49+
signed long l2 = 1; // NON_COMPLIANT: use typedef int64_t
50+
unsigned long l3 = 1; // NON_COMPLIANT: use typedef uint64_t
51+
int64_t l4 = 1; // COMPLIANT: typedef used instead
5752

58-
long long l1 = 1; // NON_COMPLIANT: int is a basic numeric type
59-
signed long long l2 = 1; // NON_COMPLIANT: use typedef int128_t in stdint
60-
unsigned long long l3 = 1; // NON_COMPLIANT: use typedef uint128_t in stdint
61-
INT128 s4 = 1; // COMPLIANT: typedef used instead
53+
long long ll1 = 1; // NON_COMPLIANT: int is a basic numeric type
54+
signed long long ll2 = 1; // NON_COMPLIANT: use typedef int128_t
55+
unsigned long long ll3 = 1; // NON_COMPLIANT: use typedef uint128_t
56+
int128_t ll4 = 1; // COMPLIANT: typedef used instead
6257

63-
float f1 = 1; // NON_COMPLIANT: float is a basic numeric type, use a typedef
64-
FLOAT32 f2 = 1; // COMPLIANT: typedef used instead
58+
float f1 = 1; // NON_COMPLIANT: float is a basic numeric type, use a typedef
59+
float32_t f2 = 1; // COMPLIANT: typedef used instead
6560

66-
double d1 = 1; // NON_COMPLIANT: int is a basic numeric type
67-
FLOAT64 d2 = 1; // COMPLIANT: typedef used instead
61+
double d1 = 1; // NON_COMPLIANT: int is a basic numeric type
62+
float64_t d2 = 1; // COMPLIANT: typedef used instead
6863

6964
long double ld1 = 1; // NON_COMPLIANT: int is a basic numeric type
70-
FLOAT128 ld2 = 1; // COMPLIANT: typedef used instead
65+
float128_t ld2 = 1; // COMPLIANT: typedef used instead
7166
}

0 commit comments

Comments
 (0)