Python points-to: Add float objects for better backwards compatibility.

This commit is contained in:
Mark Shannon
2019-03-27 11:59:29 +00:00
parent e9f58ba3a7
commit 931100c772
4 changed files with 55 additions and 8 deletions

View File

@@ -169,7 +169,7 @@ class IntObjectInternal extends ConstantObjectInternal, TInt {
}
override Builtin getBuiltin() {
result.(Builtin).intValue() = this.intValue()
result.intValue() = this.intValue()
}
override int intValue() {
@@ -188,6 +188,45 @@ class IntObjectInternal extends ConstantObjectInternal, TInt {
}
class FloatObjectInternal extends ConstantObjectInternal, TFloat {
override string toString() {
result = "float " + this.floatValue().toString()
}
override predicate introduced(ControlFlowNode node, PointsToContext context) {
context.appliesTo(node) and
node.getNode().(FloatLiteral).getValue() = this.floatValue()
}
override ObjectInternal getClass() {
result = TBuiltinClassObject(Builtin::special("float"))
}
override Builtin getBuiltin() {
result.getName().toFloat() = this.floatValue()
}
private float floatValue() {
this = TFloat(result)
}
override int intValue() {
this = TFloat(result)
}
override string strValue() {
none()
}
override boolean booleanValue() {
this.floatValue() = 0.0 and result = false
or
this.floatValue() != 0.0 and result = true
}
}
class StringObjectInternal extends ConstantObjectInternal, TString {

View File

@@ -221,9 +221,7 @@ class UnknownInstanceInternal extends TUnknownInstance, ObjectInternal {
}
override predicate introduced(ControlFlowNode node, PointsToContext context) {
context.appliesTo(node) and
this.getClass() = ObjectInternal::builtin("float") and
node.getNode() instanceof FloatLiteral
none()
}
/** Gets the class declaration for this object, if it is a declared class. */

View File

@@ -22,6 +22,7 @@ newtype TObject =
not bltn.isMethod() and not bltn.isModule() and
not bltn.getClass() = Builtin::special("tuple") and
not exists(bltn.intValue()) and
not exists(bltn.floatValue()) and
not exists(bltn.strValue()) and
not py_special_objects(bltn, _)
}
@@ -58,8 +59,8 @@ newtype TObject =
// And all combinations of flags up to 2^8
n in [0..511] or
// Any number explicitly mentioned in the source code.
exists(Num num |
n = num.getN().toInt() or
exists(IntegerLiteral num |
n = num.getValue() or
exists(UnaryExpr neg | neg.getOp() instanceof USub and neg.getOperand() = num)
and n = -num.getN().toInt()
)
@@ -67,6 +68,10 @@ newtype TObject =
n = any(Builtin b).intValue()
}
or
TFloat(float f) {
f = any(FloatLiteral num).getValue()
}
or
TString(string s) {
// Any string explicitly mentioned in the source code.
s = any(StrConst str).getText()
@@ -139,8 +144,6 @@ predicate literal_instantiation(ControlFlowNode n, ClassObjectInternal cls, Poin
or
n instanceof DictNode and cls = ObjectInternal::builtin("dict")
or
n.getNode() instanceof FloatLiteral and cls = ObjectInternal::builtin("float")
or
n.getNode() instanceof ImaginaryLiteral and cls = ObjectInternal::builtin("complex")
)
}
@@ -303,6 +306,8 @@ library class ClassDecl extends @py_object {
name = "MethodType" or
name = "ModuleType"
)
or
this = Builtin::builtin("float")
}
}

View File

@@ -87,6 +87,11 @@ class Builtin extends @py_cobject {
result = this.getName().toInt()
}
float floatValue() {
(this.getClass() = Builtin::special("float")) and
result = this.getName().toFloat()
}
string strValue() {
(this.getClass() = Builtin::special("unicode") or
this.getClass() = Builtin::special("bytes")) and