Skip to content

Commit 70147d5

Browse files
committed
EssentialTypes: Implement Rule 10.6.
Adds a query which identifies implicit conversions of composite expressions that cause it to be casted to a wider essential type.
1 parent 87e7013 commit 70147d5

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @id c/misra/implicit-conversion-of-composite-expression
3+
* @name RULE-10-7: If a composite expression is used as one operand of an operator in which the usual arithmetic
4+
* @description If a composite expression is used as one operand of an operator in which the usual
5+
* arithmetic conversions are performed then the other operand shall not have wider
6+
* essential type
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity error
10+
* @tags external/misra/id/rule-10-7
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.c.misra.EssentialTypes
17+
import codingstandards.c.misra.MisraExpressions
18+
19+
from
20+
OperationWithUsualArithmeticConversions arith, CompositeExpression compositeOp, Expr otherOp,
21+
Type compositeEssentialType, Type otherOpEssentialType
22+
where
23+
not isExcluded(arith, EssentialTypesPackage::implicitConversionOfCompositeExpressionQuery()) and
24+
arith.getAnOperand() = compositeOp and
25+
arith.getAnOperand() = otherOp and
26+
not otherOp = compositeOp and
27+
compositeEssentialType = getEssentialType(compositeOp) and
28+
otherOpEssentialType = getEssentialType(otherOp) and
29+
compositeEssentialType.getSize() < otherOpEssentialType.getSize() and
30+
// Operands of a different type category in an operation with the usual arithmetic conversions is
31+
// prohibited by Rule 10.4, so we only report cases here where the essential type categories are
32+
// the same
33+
getEssentialTypeCategory(compositeEssentialType) = getEssentialTypeCategory(otherOpEssentialType)
34+
select arith,
35+
"Implicit conversion of $@ from " + compositeEssentialType + " to " + otherOpEssentialType,
36+
compositeOp, "composite op"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.c:5:3:5:16 | ... + ... | Implicit conversion of $@ from unsigned short to unsigned int | test.c:5:9:5:16 | ... * ... | composite op |
2+
| test.c:6:3:6:18 | ... * ... | Implicit conversion of $@ from unsigned short to unsigned int | test.c:6:9:6:17 | ... + ... | composite op |
3+
| test.c:9:3:9:20 | ... += ... | Implicit conversion of $@ from unsigned short to unsigned int | test.c:9:11:9:19 | ... + ... | composite op |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-10-7/ImplicitConversionOfCompositeExpression.ql

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
void testComposite() {
2+
unsigned int u32 = 100;
3+
unsigned short u16 = 100;
4+
u16 + u32 *u16; // COMPLIANT
5+
u32 + u16 *u16; // NON_COMPLIANT
6+
u32 *(u16 + u16); // NON_COMPLIANT
7+
u32 *(unsigned int)(u16 + u16); // COMPLIANT
8+
u32 + u16 + u16; // COMPLIANT
9+
u32 += (u16 + u16); // NON_COMPLIANT
10+
u32 += (u32 + u16); // COMPLIANT
11+
12+
signed int s32 = 100;
13+
s32 += (u16 + u16); // // ignored - prohibited by Rule 10.4
14+
}

0 commit comments

Comments
 (0)