Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EssentialType: Implement correct essential types for bitwise binary operators &, ^ and |. #787

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions c/misra/src/codingstandards/c/misra/EssentialTypes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation {
override Type getEssentialType() { result instanceof BoolType }
}

class EssentialBinaryBitwiseOperationExpr extends EssentialExpr, BinaryBitwiseOperation {
EssentialBinaryBitwiseOperationExpr() {
class EssentialShiftOperationExpr extends EssentialExpr, BinaryBitwiseOperation {
EssentialShiftOperationExpr() {
this instanceof LShiftExpr or
this instanceof RShiftExpr
}
Expand Down Expand Up @@ -353,6 +353,51 @@ class EssentialBinaryArithmeticExpr extends EssentialExpr, BinaryArithmeticOpera
}
}

class EssentialBinaryBitwiseExpr extends EssentialExpr, BinaryBitwiseOperation {
EssentialBinaryBitwiseExpr() {
not this instanceof LShiftExpr and
not this instanceof RShiftExpr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any concern here about future updates to the characteristic predicate of EssentialShiftOperationExpr that aren't matched here (and vice versa)? In theory we could not find certain expressions to analyze, or worse, return two results from the overriden member predicates.

It may be better to entangle them here, eg, not this instanceof EssentialBinaryBitwiseOperationExpr

}

override Type getEssentialType() {
exists(
Type leftEssentialType, Type rightEssentialType,
EssentialTypeCategory leftEssentialTypeCategory,
EssentialTypeCategory rightEssentialTypeCategory
|
leftEssentialType = getEssentialType(getLeftOperand()) and
rightEssentialType = getEssentialType(getRightOperand()) and
leftEssentialTypeCategory = getEssentialTypeCategory(leftEssentialType) and
rightEssentialTypeCategory = getEssentialTypeCategory(rightEssentialType)
|
if
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is now the third occurrence of this pattern, may be worth creating some kind of predicate to deduplicate this/reduce the bloat. Perhaps something like coalesce<stlr>(this, leftEssentialType, rightEssentialType, EssentiallySignedType()), even just bothSigned(leftEssentialType, rightEssentialType) could reduce the boilerplate. And it might be worthy of a bigger hammer such as Type standardCombine(Expr e, Expr operandA, Expr operandB)

leftEssentialTypeCategory = EssentiallySignedType() and
rightEssentialTypeCategory = EssentiallySignedType()
then
if exists(getValue())
then result = stlr(this)
else (
if leftEssentialType.getSize() > rightEssentialType.getSize()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we should have a predicate Type maxRank(Type a, Type b) to simplify this repetition

then result = leftEssentialType
else result = rightEssentialType
)
else
if
leftEssentialTypeCategory = EssentiallyUnsignedType() and
rightEssentialTypeCategory = EssentiallyUnsignedType()
then
if exists(getValue())
then result = utlr(this)
else (
if leftEssentialType.getSize() > rightEssentialType.getSize()
then result = leftEssentialType
else result = rightEssentialType
)
else result = this.getStandardType()
)
}
}

/**
* A named Enum type, as per D.5.
*/
Expand Down
Loading
Loading