Skip to content

Commit 8f81b4e

Browse files
committed
EssentialTypes: Correct handling of bitwise binary expressions
They shouldn't have the standard type if either both the operands are signed or they are both unsigned.
1 parent 041150a commit 8f81b4e

File tree

3 files changed

+402
-2
lines changed

3 files changed

+402
-2
lines changed

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

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation {
192192
override Type getEssentialType() { result instanceof BoolType }
193193
}
194194

195-
class EssentialBinaryBitwiseOperationExpr extends EssentialExpr, BinaryBitwiseOperation {
196-
EssentialBinaryBitwiseOperationExpr() {
195+
class EssentialShiftOperationExpr extends EssentialExpr, BinaryBitwiseOperation {
196+
EssentialShiftOperationExpr() {
197197
this instanceof LShiftExpr or
198198
this instanceof RShiftExpr
199199
}
@@ -353,6 +353,51 @@ class EssentialBinaryArithmeticExpr extends EssentialExpr, BinaryArithmeticOpera
353353
}
354354
}
355355

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+
356401
/**
357402
* A named Enum type, as per D.5.
358403
*/

0 commit comments

Comments
 (0)