Skip to content

Commit 8f8f48f

Browse files
authored
Merge pull request #390 from github/lcartey/m5-0-20-pointers
Exclude pointer assign from bitwise assign
2 parents b72ba4f + f782538 commit 8f8f48f

11 files changed

+77
-29
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import cpp
1515
import codingstandards.c.misra
1616
import codingstandards.c.misra.EssentialTypes
17+
import codingstandards.cpp.Bitwise
1718

1819
/**
1920
* Holds if the operator `operator` has an operand `child` that is of an inappropriate essential type
@@ -177,7 +178,7 @@ predicate isInappropriateEssentialType(
177178
child =
178179
[
179180
operator.(BinaryBitwiseOperation).getAnOperand(),
180-
operator.(AssignBitwiseOperation).getAnOperand()
181+
operator.(Bitwise::AssignBitwiseOperation).getAnOperand()
181182
] and
182183
not operator instanceof LShiftExpr and
183184
not operator instanceof RShiftExpr and

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

+2
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,6 @@ void pointerType() {
492492
b || b; // COMPLIANT
493493
p || b; // NON_COMPLIANT
494494
b || p; // NON_COMPLIANT
495+
p += 1; // COMPLIANT
496+
p -= 1; // COMPLIANT
495497
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `M5-0-20`, `M5-0-21`, `RULE-10-1` - exclude pointer assignment operators as bitwise operators.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M5-0-20` - `BitwiseOperatorOperandsHaveDifferentUnderlyingType.ql`:
2+
- Use the Misra definition of underlying type.

cpp/autosar/src/rules/M5-0-20/BitwiseOperatorOperandsHaveDifferentUnderlyingType.ql

+16-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,30 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19+
import codingstandards.cpp.Bitwise
20+
import codingstandards.cpp.Conversion
1921

2022
predicate isBinaryBitwiseOperation(Operation o, VariableAccess l, VariableAccess r) {
2123
exists(BinaryBitwiseOperation bbo | bbo = o |
2224
l = bbo.getLeftOperand() and r = bbo.getRightOperand()
2325
)
2426
or
25-
exists(AssignBitwiseOperation abo | abo = o | l = abo.getLValue() and r = abo.getRValue())
27+
exists(Bitwise::AssignBitwiseOperation abo | abo = o |
28+
l = abo.getLValue() and
29+
r = abo.getRValue()
30+
)
2631
}
2732

28-
from Operation o, Variable left, Variable right
33+
from
34+
Operation o, VariableAccess left, VariableAccess right, Type leftUnderlyingType,
35+
Type rightUnderlyingType
2936
where
3037
not isExcluded(o, ExpressionsPackage::bitwiseOperatorOperandsHaveDifferentUnderlyingTypeQuery()) and
3138
not o.isFromUninstantiatedTemplate(_) and
32-
isBinaryBitwiseOperation(o, left.getAnAccess(), right.getAnAccess()) and
33-
left.getUnderlyingType() != right.getUnderlyingType()
34-
select o, "Operands of the '" + o.getOperator() + "' operation have different underlying types."
39+
isBinaryBitwiseOperation(o, left, right) and
40+
leftUnderlyingType = MisraConversion::getUnderlyingType(left) and
41+
rightUnderlyingType = MisraConversion::getUnderlyingType(right) and
42+
leftUnderlyingType != rightUnderlyingType
43+
select o,
44+
"Operands of the '" + o.getOperator() + "' operation have different underlying types '" +
45+
leftUnderlyingType.getName() + "' and '" + rightUnderlyingType.getName() + "'."

cpp/autosar/src/rules/M5-0-21/BitwiseOperatorAppliedToSignedTypes.ql

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Bitwise
2021

2122
from Operation o, VariableAccess va
2223
where
2324
not isExcluded(o, ExpressionsPackage::bitwiseOperatorAppliedToSignedTypesQuery()) and
2425
(
2526
o instanceof UnaryBitwiseOperation or
2627
o instanceof BinaryBitwiseOperation or
27-
o instanceof AssignBitwiseOperation
28+
o instanceof Bitwise::AssignBitwiseOperation
2829
) and
2930
o.getAnOperand() = va and
3031
va.getTarget().getUnderlyingType().(IntegralType).isSigned()

cpp/autosar/src/rules/M5-8-1/RightBitShiftOperandIsNegativeOrTooWide.ql

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Bitwise
2021

2122
class ShiftOperation extends Operation {
2223
Expr leftOperand;
@@ -33,7 +34,7 @@ class ShiftOperation extends Operation {
3334
rightOperand = o.getRightOperand()
3435
)
3536
or
36-
exists(AssignBitwiseOperation o | this = o |
37+
exists(Bitwise::AssignBitwiseOperation o | this = o |
3738
(
3839
o instanceof AssignLShiftExpr
3940
or
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
| test.cpp:18:3:18:6 | ... & ... | Operands of the '&' operation have different underlying types. |
2-
| test.cpp:19:3:19:7 | ... \| ... | Operands of the '\|' operation have different underlying types. |
3-
| test.cpp:20:3:20:7 | ... ^ ... | Operands of the '^' operation have different underlying types. |
4-
| test.cpp:21:3:21:8 | ... << ... | Operands of the '<<' operation have different underlying types. |
5-
| test.cpp:22:3:22:8 | ... >> ... | Operands of the '>>' operation have different underlying types. |
6-
| test.cpp:23:3:23:8 | ... &= ... | Operands of the '&=' operation have different underlying types. |
7-
| test.cpp:24:3:24:8 | ... \|= ... | Operands of the '\|=' operation have different underlying types. |
8-
| test.cpp:25:3:25:8 | ... ^= ... | Operands of the '^=' operation have different underlying types. |
9-
| test.cpp:26:3:26:9 | ... <<= ... | Operands of the '<<=' operation have different underlying types. |
10-
| test.cpp:27:3:27:9 | ... >>= ... | Operands of the '>>=' operation have different underlying types. |
11-
| test.cpp:45:3:45:6 | ... & ... | Operands of the '&' operation have different underlying types. |
12-
| test.cpp:46:3:46:7 | ... \| ... | Operands of the '\|' operation have different underlying types. |
13-
| test.cpp:47:3:47:7 | ... ^ ... | Operands of the '^' operation have different underlying types. |
14-
| test.cpp:48:3:48:8 | ... << ... | Operands of the '<<' operation have different underlying types. |
15-
| test.cpp:49:3:49:8 | ... >> ... | Operands of the '>>' operation have different underlying types. |
16-
| test.cpp:50:3:50:8 | ... &= ... | Operands of the '&=' operation have different underlying types. |
17-
| test.cpp:51:3:51:8 | ... \|= ... | Operands of the '\|=' operation have different underlying types. |
18-
| test.cpp:52:3:52:8 | ... ^= ... | Operands of the '^=' operation have different underlying types. |
19-
| test.cpp:53:3:53:9 | ... <<= ... | Operands of the '<<=' operation have different underlying types. |
20-
| test.cpp:54:3:54:9 | ... >>= ... | Operands of the '>>=' operation have different underlying types. |
21-
| test.cpp:67:3:67:14 | ... << ... | Operands of the '<<' operation have different underlying types. |
1+
| test.cpp:18:3:18:6 | ... & ... | Operands of the '&' operation have different underlying types 'unsigned int' and 'unsigned short'. |
2+
| test.cpp:19:3:19:7 | ... \| ... | Operands of the '\|' operation have different underlying types 'unsigned int' and 'unsigned short'. |
3+
| test.cpp:20:3:20:7 | ... ^ ... | Operands of the '^' operation have different underlying types 'unsigned int' and 'unsigned short'. |
4+
| test.cpp:21:3:21:8 | ... << ... | Operands of the '<<' operation have different underlying types 'unsigned int' and 'unsigned short'. |
5+
| test.cpp:22:3:22:8 | ... >> ... | Operands of the '>>' operation have different underlying types 'unsigned int' and 'unsigned short'. |
6+
| test.cpp:23:3:23:8 | ... &= ... | Operands of the '&=' operation have different underlying types 'unsigned int' and 'unsigned short'. |
7+
| test.cpp:24:3:24:8 | ... \|= ... | Operands of the '\|=' operation have different underlying types 'unsigned int' and 'unsigned short'. |
8+
| test.cpp:25:3:25:8 | ... ^= ... | Operands of the '^=' operation have different underlying types 'unsigned int' and 'unsigned short'. |
9+
| test.cpp:26:3:26:9 | ... <<= ... | Operands of the '<<=' operation have different underlying types 'unsigned int' and 'unsigned short'. |
10+
| test.cpp:27:3:27:9 | ... >>= ... | Operands of the '>>=' operation have different underlying types 'unsigned int' and 'unsigned short'. |
11+
| test.cpp:45:3:45:6 | ... & ... | Operands of the '&' operation have different underlying types 'unsigned char' and 'unsigned short'. |
12+
| test.cpp:46:3:46:7 | ... \| ... | Operands of the '\|' operation have different underlying types 'unsigned char' and 'unsigned short'. |
13+
| test.cpp:47:3:47:7 | ... ^ ... | Operands of the '^' operation have different underlying types 'unsigned char' and 'unsigned short'. |
14+
| test.cpp:48:3:48:8 | ... << ... | Operands of the '<<' operation have different underlying types 'unsigned char' and 'unsigned short'. |
15+
| test.cpp:49:3:49:8 | ... >> ... | Operands of the '>>' operation have different underlying types 'unsigned char' and 'unsigned short'. |
16+
| test.cpp:50:3:50:8 | ... &= ... | Operands of the '&=' operation have different underlying types 'unsigned char' and 'unsigned short'. |
17+
| test.cpp:51:3:51:8 | ... \|= ... | Operands of the '\|=' operation have different underlying types 'unsigned char' and 'unsigned short'. |
18+
| test.cpp:52:3:52:8 | ... ^= ... | Operands of the '^=' operation have different underlying types 'unsigned char' and 'unsigned short'. |
19+
| test.cpp:53:3:53:9 | ... <<= ... | Operands of the '<<=' operation have different underlying types 'unsigned char' and 'unsigned short'. |
20+
| test.cpp:54:3:54:9 | ... >>= ... | Operands of the '>>=' operation have different underlying types 'unsigned char' and 'unsigned short'. |
21+
| test.cpp:67:3:67:14 | ... << ... | Operands of the '<<' operation have different underlying types 'int &' and 'char &'. |

cpp/autosar/test/rules/M5-0-20/test.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,8 @@ void test463_2_instantiations() {
7171
char shift2 = 2;
7272
test463_2(val, shift2);
7373
}
74+
75+
void test_add(char *val) {
76+
int add = 2;
77+
val += add; // COMPLIANT
78+
}

cpp/autosar/test/rules/M5-0-21/test.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ void test() {
4545
u ^= u; // COMPLIANT
4646
u | 0; // COMPLIANT
4747
u |= 0; // COMPLIANT
48+
49+
int *p = 0;
50+
p += 1; // COMPLIANT
51+
p -= 1; // COMPLIANT
4852
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* A library for addressing issues in bitwise operator modelling in our database schema.
3+
*/
4+
5+
private import cpp as cpp
6+
7+
module Bitwise {
8+
/**
9+
* A binary bitwise assign operation, excluding += and -= on pointers, which seem to be erroneously
10+
* included.
11+
*/
12+
class AssignBitwiseOperation extends cpp::AssignBitwiseOperation {
13+
AssignBitwiseOperation() {
14+
// exclude += and -= on pointers, which seem to be erroneously included
15+
// in the database schema
16+
not this instanceof cpp::AssignPointerAddExpr and
17+
not this instanceof cpp::AssignPointerSubExpr
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)