|
| 1 | +/** |
| 2 | + * @id c/misra/operands-with-mismatched-essential-type-category |
| 3 | + * @name RULE-10-4: Both operands of an operator in which the usual arithmetic conversions are performed shall have the |
| 4 | + * @description Both operands of an operator in which the usual arithmetic conversions are performed |
| 5 | + * shall have the same essential type category |
| 6 | + * @kind problem |
| 7 | + * @precision very-high |
| 8 | + * @problem.severity error |
| 9 | + * @tags external/misra/id/rule-10-4 |
| 10 | + * external/misra/obligation/required |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | +import codingstandards.c.misra |
| 15 | +import codingstandards.c.misra.EssentialTypes |
| 16 | +import codingstandards.c.misra.MisraExpressions |
| 17 | + |
| 18 | +from |
| 19 | + OperationWithUsualArithmeticConversions op, Type leftOpEssentialType, Type rightOpEssentialType, |
| 20 | + EssentialTypeCategory leftOpTypeCategory, EssentialTypeCategory rightOpTypeCategory, |
| 21 | + string message |
| 22 | +where |
| 23 | + not isExcluded(op, EssentialTypesPackage::operandsWithMismatchedEssentialTypeCategoryQuery()) and |
| 24 | + leftOpEssentialType = getEssentialType(op.getLeftOperand()) and |
| 25 | + rightOpEssentialType = getEssentialType(op.getRightOperand()) and |
| 26 | + leftOpTypeCategory = getEssentialTypeCategory(leftOpEssentialType) and |
| 27 | + rightOpTypeCategory = getEssentialTypeCategory(rightOpEssentialType) and |
| 28 | + ( |
| 29 | + not leftOpTypeCategory = rightOpTypeCategory and |
| 30 | + message = |
| 31 | + "The operands of this operator with usual arithmetic conversions have mismatched essential types (left operand: " |
| 32 | + + leftOpTypeCategory + ", right operand: " + rightOpTypeCategory + ")." |
| 33 | + or |
| 34 | + // This is not technically covered by the rule, but the examples make it clear that this should |
| 35 | + // be reported as non-compliant. |
| 36 | + leftOpTypeCategory = EssentiallyEnumType() and |
| 37 | + rightOpTypeCategory = EssentiallyEnumType() and |
| 38 | + not leftOpEssentialType = rightOpEssentialType and |
| 39 | + message = |
| 40 | + "The operands of this operator with usual arithmetic conversions have mismatched essentially Enum types (left operand: " |
| 41 | + + leftOpEssentialType + ", right operand: " + rightOpEssentialType + ")." |
| 42 | + ) and |
| 43 | + not ( |
| 44 | + // Mismatch is permitted if using "+" or "+=" with one character operand and one integer operand |
| 45 | + op.getOperator() = ["+", "+="] and |
| 46 | + [leftOpTypeCategory, rightOpTypeCategory] = EssentiallyCharacterType() and |
| 47 | + [leftOpTypeCategory, rightOpTypeCategory] = |
| 48 | + [EssentiallyUnsignedType().(TEssentialTypeCategory), EssentiallySignedType()] |
| 49 | + ) and |
| 50 | + not ( |
| 51 | + // Mismatch is permitted if using "+" or "+=" with one pointer operand and one integer operand |
| 52 | + op.getOperator() = ["-", "-="] and |
| 53 | + leftOpTypeCategory = EssentiallyCharacterType() and |
| 54 | + rightOpTypeCategory = |
| 55 | + [EssentiallyUnsignedType().(TEssentialTypeCategory), EssentiallySignedType()] |
| 56 | + ) |
| 57 | +select op, message |
0 commit comments