Skip to content

Commit 326534f

Browse files
committed
add test.c and sketch query
1 parent 50049bf commit 326534f

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

c/misra/src/rules/RULE-6-2/SingleBitNamedBitFieldsOfASignedType.ql

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

16-
from
16+
predicate isSigned(Type type) {
17+
/* Check if it's a fixed number type, because declaring fixed number types like int8_t as 1 bit is obviously absurd */
18+
type instanceof FixedWidthIntegralType or
19+
/* Check if it's EXPLICITLY signed, because according to Rule 6.1, 'int' may be either signed or unsigned depending on the implementation. In the latter case, the query would lead to false positives. */
20+
type instanceof IntegralType and
21+
type.(IntegralType).isExplicitlySigned()
22+
}
23+
24+
/* Check if the DECLARED bit-fields is a single bit, because Rule 6.2 also intends to catch confusion on the programmers' part. Consider:
25+
26+
struct S {
27+
int32_t x: 1;
28+
}
29+
30+
In this case, field x is essentially of 32 bits, but is declared as 1 bit and its type int32_t is signed. Therefore, it indicates confusion by the programmer, which is exactly what this rule intends to find. */
31+
predicate isSingleBit(BitField bitField) {
32+
bitField.getDeclaredNumBits() = 1
33+
}
34+
35+
// predicate isNamedBitField(BitField bitField) {
36+
// bitField.getName().length() != 0
37+
// bitField.hasName(_)
38+
// bitField.hasDefinition()
39+
// wat
40+
// }
41+
42+
from BitField bitField
1743
where
18-
not isExcluded(x, TypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and
19-
select
44+
not isExcluded(bitField, TypesPackage::singleBitNamedBitFieldsOfASignedTypeQuery()) and
45+
isSingleBit(bitField) and // Single-bit,
46+
// isNamedBitField(bitField) and // named,
47+
isSigned(bitField.getType()) // but its type is signed.
48+
select bitField, "Single-bit bit-field named " + bitField.toString() + " has a signed type " + bitField.getType() + "."

c/misra/test/rules/RULE-6-2/test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <stdint.h>
2+
3+
struct SampleStruct {
4+
int x1 : 1; // compilant: single-bit named field without signed declaration
5+
signed int x2 : 1; // non_compilant: single-bit named field with a signed type
6+
signed char
7+
x3 : 1; // non_compilant: single-bit named field with a signed type
8+
signed short
9+
x4 : 1; // non_compilant: single-bit named field with a signed type
10+
unsigned int
11+
x5 : 1; // compilant: single-bit named field but with an unsigned type
12+
signed int x6 : 2; // compilant: named field with a signed type but declared
13+
// to carry more than 1 bit
14+
int32_t x7 : 1; // non_compilant: single-bit named field that has single-bit
15+
// bit-field, even though technically it has 32 bits
16+
signed char : 1; // compilant: single-bit bit-field but unnamed
17+
} sample_struct;

0 commit comments

Comments
 (0)