Skip to content

RULE-10-1: Handle unary operators correctly #692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 17, 2024
Merged
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
4 changes: 4 additions & 0 deletions c/misra/src/codingstandards/c/misra/EssentialTypes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class EssentialBinaryLogicalOperationExpr extends EssentialExpr, BinaryLogicalOp
override Type getEssentialType() { result instanceof BoolType }
}

class EssentialUnaryLogicalOperationExpr extends EssentialExpr, UnaryLogicalOperation {
override Type getEssentialType() { result instanceof BoolType }
}

class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation {
override Type getEssentialType() { result instanceof BoolType }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ predicate isInappropriateEssentialType(
child =
[
operator.(BinaryBitwiseOperation).getAnOperand(),
operator.(Bitwise::AssignBitwiseOperation).getAnOperand()
operator.(Bitwise::AssignBitwiseOperation).getAnOperand(),
operator.(ComplementExpr).getAnOperand()
] and
not operator instanceof LShiftExpr and
not operator instanceof RShiftExpr and
Expand Down Expand Up @@ -240,7 +241,7 @@ string getRationaleMessage(int rationaleId, EssentialTypeCategory etc) {
result = "Bitwise operator applied to operand of " + etc + " and not essentially unsigned."
or
rationaleId = 7 and
result = "Right hand operatand of shift operator is " + etc + " and not not essentially unsigned."
result = "Right hand operand of shift operator is " + etc + " and not not essentially unsigned."
or
rationaleId = 8 and
result =
Expand All @@ -251,4 +252,4 @@ from Expr operator, Expr child, int rationaleId, EssentialTypeCategory etc
where
not isExcluded(operator, EssentialTypesPackage::operandsOfAnInappropriateEssentialTypeQuery()) and
isInappropriateEssentialType(operator, child, etc, rationaleId)
select operator, getRationaleMessage(rationaleId, etc)
select child, getRationaleMessage(rationaleId, etc)
32 changes: 32 additions & 0 deletions c/misra/test/c/misra/EssentialTypes.expected
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,35 @@
| test.c:32:3:32:3 | 1 | signed char | signed char | essentially Signed type |
| test.c:33:3:33:4 | 1 | unsigned char | unsigned char | essentially Unsigned type |
| test.c:34:3:34:5 | 1 | unsigned long | unsigned long | essentially Unsigned type |
| test.c:38:13:38:16 | 1 | bool | bool | essentially Boolean type |
| test.c:38:13:38:16 | (bool)... | bool | bool | essentially Boolean type |
| test.c:39:20:39:20 | 1 | signed char | signed char | essentially Signed type |
| test.c:39:20:39:20 | (unsigned int)... | unsigned int | unsigned int | essentially Unsigned type |
| test.c:40:23:40:23 | 1 | signed char | signed char | essentially Signed type |
| test.c:40:23:40:23 | (unsigned short)... | unsigned short | unsigned short | essentially Unsigned type |
| test.c:41:17:41:18 | 1 | signed char | signed char | essentially Signed type |
| test.c:42:21:42:21 | 1 | signed char | signed char | essentially Signed type |
| test.c:42:21:42:21 | (signed short)... | signed short | signed short | essentially Signed type |
| test.c:44:3:44:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:44:4:44:4 | b | bool | bool | essentially Boolean type |
| test.c:45:3:45:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:45:4:45:4 | u | unsigned int | unsigned int | essentially Unsigned type |
| test.c:46:3:46:5 | ! ... | bool | bool | essentially Boolean type |
| test.c:46:4:46:5 | us | unsigned short | unsigned short | essentially Unsigned type |
| test.c:47:3:47:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:47:4:47:4 | s | signed int | signed int | essentially Signed type |
| test.c:48:3:48:5 | ! ... | bool | bool | essentially Boolean type |
| test.c:48:4:48:5 | ss | signed short | signed short | essentially Signed type |
| test.c:50:3:50:4 | ~ ... | int | int | essentially Signed type |
| test.c:50:4:50:4 | (int)... | int | int | essentially Signed type |
| test.c:50:4:50:4 | b | bool | bool | essentially Boolean type |
| test.c:51:3:51:4 | ~ ... | unsigned int | unsigned int | essentially Unsigned type |
| test.c:51:4:51:4 | u | unsigned int | unsigned int | essentially Unsigned type |
| test.c:52:3:52:5 | ~ ... | unsigned short | unsigned short | essentially Unsigned type |
| test.c:52:4:52:5 | (int)... | int | int | essentially Signed type |
| test.c:52:4:52:5 | us | unsigned short | unsigned short | essentially Unsigned type |
| test.c:53:3:53:4 | ~ ... | signed int | signed int | essentially Signed type |
| test.c:53:4:53:4 | s | signed int | signed int | essentially Signed type |
| test.c:54:3:54:5 | ~ ... | int | int | essentially Signed type |
| test.c:54:4:54:5 | (int)... | int | int | essentially Signed type |
| test.c:54:4:54:5 | ss | signed short | signed short | essentially Signed type |
20 changes: 20 additions & 0 deletions c/misra/test/c/misra/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,24 @@ void testConstants() {
1; // Essentially signed char
1U; // Essentially unsigned char
1UL; // Essentially unsigned long
}

void testUnary() {
_Bool b = true;
unsigned int u = 1;
unsigned short us = 1;
signed int s = 1;
signed short ss = 1;

!b; // Should be boolean
!u; // Should be boolean
!us; // Should be boolean
!s; // Should be boolean
!ss; // Should be boolean

~b; // Should be essentially signed
~u; // Should be essentially unsigned
~us; // Should be essentially unsigned
~s; // Should be essentially signed
~ss; // Should be essentially signed
}
Loading
Loading