Skip to content

Commit dd63211

Browse files
authored
Merge pull request #692 from github/lcartey/rule-10-1-improvements
`RULE-10-1`: Handle unary operators correctly
2 parents 7982e0b + 4c87873 commit dd63211

File tree

6 files changed

+255
-190
lines changed

6 files changed

+255
-190
lines changed

c/misra/src/codingstandards/c/misra/EssentialTypes.qll

+4
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ class EssentialBinaryLogicalOperationExpr extends EssentialExpr, BinaryLogicalOp
179179
override Type getEssentialType() { result instanceof BoolType }
180180
}
181181

182+
class EssentialUnaryLogicalOperationExpr extends EssentialExpr, UnaryLogicalOperation {
183+
override Type getEssentialType() { result instanceof BoolType }
184+
}
185+
182186
class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation {
183187
override Type getEssentialType() { result instanceof BoolType }
184188
}

c/misra/src/rules/RULE-10-1/OperandsOfAnInappropriateEssentialType.ql

+4-3
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ predicate isInappropriateEssentialType(
178178
child =
179179
[
180180
operator.(BinaryBitwiseOperation).getAnOperand(),
181-
operator.(Bitwise::AssignBitwiseOperation).getAnOperand()
181+
operator.(Bitwise::AssignBitwiseOperation).getAnOperand(),
182+
operator.(ComplementExpr).getAnOperand()
182183
] and
183184
not operator instanceof LShiftExpr and
184185
not operator instanceof RShiftExpr and
@@ -240,7 +241,7 @@ string getRationaleMessage(int rationaleId, EssentialTypeCategory etc) {
240241
result = "Bitwise operator applied to operand of " + etc + " and not essentially unsigned."
241242
or
242243
rationaleId = 7 and
243-
result = "Right hand operatand of shift operator is " + etc + " and not not essentially unsigned."
244+
result = "Right hand operand of shift operator is " + etc + " and not not essentially unsigned."
244245
or
245246
rationaleId = 8 and
246247
result =
@@ -251,4 +252,4 @@ from Expr operator, Expr child, int rationaleId, EssentialTypeCategory etc
251252
where
252253
not isExcluded(operator, EssentialTypesPackage::operandsOfAnInappropriateEssentialTypeQuery()) and
253254
isInappropriateEssentialType(operator, child, etc, rationaleId)
254-
select operator, getRationaleMessage(rationaleId, etc)
255+
select child, getRationaleMessage(rationaleId, etc)

c/misra/test/c/misra/EssentialTypes.expected

+32
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,35 @@
4141
| test.c:32:3:32:3 | 1 | signed char | signed char | essentially Signed type |
4242
| test.c:33:3:33:4 | 1 | unsigned char | unsigned char | essentially Unsigned type |
4343
| test.c:34:3:34:5 | 1 | unsigned long | unsigned long | essentially Unsigned type |
44+
| test.c:38:13:38:16 | 1 | bool | bool | essentially Boolean type |
45+
| test.c:38:13:38:16 | (bool)... | bool | bool | essentially Boolean type |
46+
| test.c:39:20:39:20 | 1 | signed char | signed char | essentially Signed type |
47+
| test.c:39:20:39:20 | (unsigned int)... | unsigned int | unsigned int | essentially Unsigned type |
48+
| test.c:40:23:40:23 | 1 | signed char | signed char | essentially Signed type |
49+
| test.c:40:23:40:23 | (unsigned short)... | unsigned short | unsigned short | essentially Unsigned type |
50+
| test.c:41:17:41:18 | 1 | signed char | signed char | essentially Signed type |
51+
| test.c:42:21:42:21 | 1 | signed char | signed char | essentially Signed type |
52+
| test.c:42:21:42:21 | (signed short)... | signed short | signed short | essentially Signed type |
53+
| test.c:44:3:44:4 | ! ... | bool | bool | essentially Boolean type |
54+
| test.c:44:4:44:4 | b | bool | bool | essentially Boolean type |
55+
| test.c:45:3:45:4 | ! ... | bool | bool | essentially Boolean type |
56+
| test.c:45:4:45:4 | u | unsigned int | unsigned int | essentially Unsigned type |
57+
| test.c:46:3:46:5 | ! ... | bool | bool | essentially Boolean type |
58+
| test.c:46:4:46:5 | us | unsigned short | unsigned short | essentially Unsigned type |
59+
| test.c:47:3:47:4 | ! ... | bool | bool | essentially Boolean type |
60+
| test.c:47:4:47:4 | s | signed int | signed int | essentially Signed type |
61+
| test.c:48:3:48:5 | ! ... | bool | bool | essentially Boolean type |
62+
| test.c:48:4:48:5 | ss | signed short | signed short | essentially Signed type |
63+
| test.c:50:3:50:4 | ~ ... | int | int | essentially Signed type |
64+
| test.c:50:4:50:4 | (int)... | int | int | essentially Signed type |
65+
| test.c:50:4:50:4 | b | bool | bool | essentially Boolean type |
66+
| test.c:51:3:51:4 | ~ ... | unsigned int | unsigned int | essentially Unsigned type |
67+
| test.c:51:4:51:4 | u | unsigned int | unsigned int | essentially Unsigned type |
68+
| test.c:52:3:52:5 | ~ ... | unsigned short | unsigned short | essentially Unsigned type |
69+
| test.c:52:4:52:5 | (int)... | int | int | essentially Signed type |
70+
| test.c:52:4:52:5 | us | unsigned short | unsigned short | essentially Unsigned type |
71+
| test.c:53:3:53:4 | ~ ... | signed int | signed int | essentially Signed type |
72+
| test.c:53:4:53:4 | s | signed int | signed int | essentially Signed type |
73+
| test.c:54:3:54:5 | ~ ... | int | int | essentially Signed type |
74+
| test.c:54:4:54:5 | (int)... | int | int | essentially Signed type |
75+
| test.c:54:4:54:5 | ss | signed short | signed short | essentially Signed type |

c/misra/test/c/misra/test.c

+20
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,24 @@ void testConstants() {
3232
1; // Essentially signed char
3333
1U; // Essentially unsigned char
3434
1UL; // Essentially unsigned long
35+
}
36+
37+
void testUnary() {
38+
_Bool b = true;
39+
unsigned int u = 1;
40+
unsigned short us = 1;
41+
signed int s = 1;
42+
signed short ss = 1;
43+
44+
!b; // Should be boolean
45+
!u; // Should be boolean
46+
!us; // Should be boolean
47+
!s; // Should be boolean
48+
!ss; // Should be boolean
49+
50+
~b; // Should be essentially signed
51+
~u; // Should be essentially unsigned
52+
~us; // Should be essentially unsigned
53+
~s; // Should be essentially signed
54+
~ss; // Should be essentially signed
3555
}

0 commit comments

Comments
 (0)