Skip to content

Commit dcda9b6

Browse files
committed
implement MISRA RULE-6-1
1 parent 82dadd9 commit dcda9b6

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

c/misra/src/rules/RULE-6-1/BitFieldsShallOnlyBeDeclaredWithAnAppropriateType.ql

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

16-
from
16+
predicate isSignedOrUnsignedInt(Type type) {
17+
type instanceof IntType and
18+
(type.(IntegralType).isExplicitlySigned() or
19+
type.(IntegralType).isExplicitlyUnsigned())
20+
}
21+
22+
predicate isAppropriatePrimitive(Type type) {
23+
isSignedOrUnsignedInt(type) or type instanceof BoolType
24+
}
25+
26+
predicate isAppropriateTypedef(Type type) {
27+
type instanceof TypedefType and
28+
isAppropriatePrimitive(type.(TypedefType).resolveTypedefs())
29+
}
30+
31+
predicate isInappropriateType(Type type) {
32+
not (isAppropriatePrimitive(type) or isAppropriateTypedef(type))
33+
}
34+
35+
from BitField bitField
1736
where
18-
not isExcluded(x, TypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and
19-
select
37+
not isExcluded(bitField, TypesPackage::bitFieldsShallOnlyBeDeclaredWithAnAppropriateTypeQuery()) and
38+
isInappropriateType(bitField.getType())
39+
select bitField, "Type " + bitField.getType() + " should not have a bit-field declaration at " + bitField + "."
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
No expected results have yet been specified
1+
| test.c:8:7:8:8 | b3 | Type int should not have a bit-field declaration at b3. |
2+
| test.c:11:15:11:16 | b5 | Type signed long should not have a bit-field declaration at b5. |
3+
| test.c:13:15:13:16 | b6 | Type signed char should not have a bit-field declaration at b6. |
4+
| test.c:14:14:14:15 | b7 | Type Color should not have a bit-field declaration at b7. |

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
typedef unsigned int UINT_16;
2+
3+
enum Color { R, G, B };
4+
5+
struct SampleStruct {
6+
unsigned int b1 : 2; // COMPILANT - explicitly unsigned (example in the doc)
7+
signed int b2 : 2; // COMPILANT - explicitly signed
8+
int b3 : 2; // NON_COMPLIANT - plain int not permitted (example in the doc)
9+
UINT_16 b4 : 2; // COMPLIANT - typedef designating unsigned int (example in
10+
// the doc)
11+
signed long b5 : 2; // NON_COMPLIANT - even if long and int are the same size
12+
// (example in the doc)
13+
signed char b6 : 2; // NON_COMPILANT - cannot declare bit field for char
14+
enum Color b7 : 3; // NON_COMPILANT - cannot declare bit field for enum
15+
} sample_struct;

0 commit comments

Comments
 (0)