Skip to content

Commit 37ac088

Browse files
committed
EssentialType: Merge binary operation implementations
EssentialBinaryArithmeticOperation and EssentialBinaryBitwiseOperation only differ in their handling of + and - operations, so combine the two implementations to reduce duplication. In addition, change the characteristic predicate to an allow list. This ensures we only capture the intended binary operations, and exclude any others.
1 parent c3ff45e commit 37ac088

File tree

1 file changed

+21
-50
lines changed

1 file changed

+21
-50
lines changed

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

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,26 @@ class EssentialConditionalExpr extends EssentialExpr, ConditionalExpr {
286286
}
287287
}
288288

289-
class EssentialBinaryArithmeticExpr extends EssentialExpr, BinaryArithmeticOperation {
290-
EssentialBinaryArithmeticExpr() {
291-
// GNU C extension has min/max which we can ignore
292-
not this instanceof MinExpr and
293-
not this instanceof MaxExpr
289+
/**
290+
* A binary operation subject to usual conversions, with essential type behaviour as specified by D.7.9.
291+
*/
292+
class EssentialBinaryOperationSubjectToUsualConversions extends EssentialExpr, BinaryOperation {
293+
EssentialBinaryOperationSubjectToUsualConversions() {
294+
this instanceof MulExpr
295+
or
296+
this instanceof DivExpr
297+
or
298+
this instanceof RemExpr
299+
or
300+
this instanceof AddExpr
301+
or
302+
this instanceof SubExpr
303+
or
304+
this instanceof BitwiseAndExpr
305+
or
306+
this instanceof BitwiseOrExpr
307+
or
308+
this instanceof BitwiseXorExpr
294309
}
295310

296311
override Type getEssentialType() {
@@ -353,51 +368,7 @@ class EssentialBinaryArithmeticExpr extends EssentialExpr, BinaryArithmeticOpera
353368
}
354369
}
355370

356-
class EssentialBinaryBitwiseExpr extends EssentialExpr, BinaryBitwiseOperation {
357-
EssentialBinaryBitwiseExpr() {
358-
not this instanceof LShiftExpr and
359-
not this instanceof RShiftExpr
360-
}
361-
362-
override Type getEssentialType() {
363-
exists(
364-
Type leftEssentialType, Type rightEssentialType,
365-
EssentialTypeCategory leftEssentialTypeCategory,
366-
EssentialTypeCategory rightEssentialTypeCategory
367-
|
368-
leftEssentialType = getEssentialType(getLeftOperand()) and
369-
rightEssentialType = getEssentialType(getRightOperand()) and
370-
leftEssentialTypeCategory = getEssentialTypeCategory(leftEssentialType) and
371-
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType)
372-
|
373-
if
374-
leftEssentialTypeCategory = EssentiallySignedType() and
375-
rightEssentialTypeCategory = EssentiallySignedType()
376-
then
377-
if exists(getValue())
378-
then result = stlr(this)
379-
else (
380-
if leftEssentialType.getSize() > rightEssentialType.getSize()
381-
then result = leftEssentialType
382-
else result = rightEssentialType
383-
)
384-
else
385-
if
386-
leftEssentialTypeCategory = EssentiallyUnsignedType() and
387-
rightEssentialTypeCategory = EssentiallyUnsignedType()
388-
then
389-
if exists(getValue())
390-
then result = utlr(this)
391-
else (
392-
if leftEssentialType.getSize() > rightEssentialType.getSize()
393-
then result = leftEssentialType
394-
else result = rightEssentialType
395-
)
396-
else result = this.getStandardType()
397-
)
398-
}
399-
}
400-
371+
// }
401372
/**
402373
* A named Enum type, as per D.5.
403374
*/

0 commit comments

Comments
 (0)