Merge pull request #12635 from MathiasVP/bool-to-int-is-safe

C++: `bool` -> `int` are safe conversions
This commit is contained in:
Mathias Vorreiter Pedersen
2023-03-22 16:29:29 +00:00
committed by GitHub

View File

@@ -250,16 +250,26 @@ SemType getSemanticType(Specific::Type type) {
Specific::unknownType(type) and result = TSemUnknownType()
}
private class SemNumericOrBooleanType extends SemSizedType {
SemNumericOrBooleanType() {
this instanceof SemNumericType
or
this instanceof SemBooleanType
}
}
/**
* Holds if the conversion from `fromType` to `toType` can never overflow or underflow.
*/
predicate conversionCannotOverflow(SemNumericType fromType, SemNumericType toType) {
predicate conversionCannotOverflow(SemNumericOrBooleanType fromType, SemNumericOrBooleanType toType) {
// Identity cast
fromType = toType
or
// Treat any cast to an FP type as safe. It can lose precision, but not overflow.
toType instanceof SemFloatingPointType and fromType = any(SemNumericType n)
or
fromType instanceof SemBooleanType and toType instanceof SemIntegerType
or
exists(SemIntegerType fromInteger, SemIntegerType toInteger, int fromSize, int toSize |
fromInteger = fromType and
toInteger = toType and