Dataflow: Replace ppReprType with DataFlowType.toString.

This commit is contained in:
Anders Schack-Mulligen
2024-07-25 11:34:16 +02:00
parent 4cbc3349f6
commit 7a48fe1102
11 changed files with 36 additions and 46 deletions

View File

@@ -215,19 +215,16 @@ predicate typeStrongerThan(DataFlowType t1, DataFlowType t2) { none() }
predicate localMustFlowStep(Node node1, Node node2) { none() } predicate localMustFlowStep(Node node1, Node node2) { none() }
/** Gets the type of `n` used for type pruning. */ /** Gets the type of `n` used for type pruning. */
Type getNodeType(Node n) { DataFlowType getNodeType(Node n) {
exists(n) and exists(n) and
result instanceof VoidType // stub implementation result instanceof VoidType // stub implementation
} }
/** Gets a string representation of a type returned by `getNodeType`. */
string ppReprType(Type t) { none() } // stub implementation
/** /**
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from * Holds if `t1` and `t2` are compatible, that is, whether data can flow from
* a node of type `t1` to a node of type `t2`. * a node of type `t1` to a node of type `t2`.
*/ */
predicate compatibleTypes(Type t1, Type t2) { predicate compatibleTypes(DataFlowType t1, DataFlowType t2) {
t1 instanceof VoidType and t2 instanceof VoidType // stub implementation t1 instanceof VoidType and t2 instanceof VoidType // stub implementation
} }
@@ -243,7 +240,11 @@ class DataFlowCallable extends Function { }
class DataFlowExpr = Expr; class DataFlowExpr = Expr;
class DataFlowType = Type; final private class TypeFinal = Type;
class DataFlowType extends TypeFinal {
string toString() { result = "" }
}
/** A function call relevant for data flow. */ /** A function call relevant for data flow. */
class DataFlowCall extends Expr instanceof Call { class DataFlowCall extends Expr instanceof Call {

View File

@@ -994,9 +994,6 @@ DataFlowType getNodeType(Node n) {
result instanceof VoidType // stub implementation result instanceof VoidType // stub implementation
} }
/** Gets a string representation of a type returned by `getNodeType`. */
string ppReprType(DataFlowType t) { none() } // stub implementation
/** /**
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from * Holds if `t1` and `t2` are compatible, that is, whether data can flow from
* a node of type `t1` to a node of type `t2`. * a node of type `t1` to a node of type `t2`.
@@ -1097,7 +1094,11 @@ class SummarizedCallable extends DataFlowCallable, TSummarizedCallable {
class DataFlowExpr = Expr; class DataFlowExpr = Expr;
class DataFlowType = Type; final private class TypeFinal = Type;
class DataFlowType extends TypeFinal {
string toString() { result = "" }
}
cached cached
private newtype TDataFlowCall = private newtype TDataFlowCall =

View File

@@ -2444,9 +2444,6 @@ DataFlowType getNodeType(Node n) {
] = result.getADelegateCreation() ] = result.getADelegateCreation()
} }
/** Gets a string representation of a `DataFlowType`. */
string ppReprType(DataFlowType t) { result = t.toString() }
private class DataFlowNullType extends Gvn::GvnType { private class DataFlowNullType extends Gvn::GvnType {
DataFlowNullType() { this = Gvn::getGlobalValueNumber(any(NullType nt)) } DataFlowNullType() { this = Gvn::getGlobalValueNumber(any(NullType nt)) }

View File

@@ -548,12 +548,9 @@ DataFlowType getNodeType(Node n)
``` ```
and every `Node` should have a type. and every `Node` should have a type.
One also needs to define the string representation of a `DataFlowType`: One also needs to define the string representation of a `DataFlowType`.
```ql The `DataFlowType.toString` predicate is used for printing a type in the labels of
string ppReprType(DataFlowType t) `PathNode`s, this should be defined as `result = ""` if type pruning is not used.
```
The `ppReprType` predicate is used for printing a type in the labels of
`PathNode`s, this can be defined as `none()` if type pruning is not used.
Finally, one must define `CastNode` as a subclass of `Node` as those nodes Finally, one must define `CastNode` as a subclass of `Node` as those nodes
where types should be checked. Usually this will be things like explicit casts. where types should be checked. Usually this will be things like explicit casts.

View File

@@ -215,9 +215,6 @@ predicate localMustFlowStep(Node node1, Node node2) { none() }
/** Gets the type of `n` used for type pruning. */ /** Gets the type of `n` used for type pruning. */
DataFlowType getNodeType(Node n) { result = TTodoDataFlowType() and exists(n) } DataFlowType getNodeType(Node n) { result = TTodoDataFlowType() and exists(n) }
/** Gets a string representation of a type returned by `getNodeType()`. */
string ppReprType(DataFlowType t) { none() }
/** /**
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from * Holds if `t1` and `t2` are compatible, that is, whether data can flow from
* a node of type `t1` to a node of type `t2`. * a node of type `t1` to a node of type `t2`.

View File

@@ -356,8 +356,12 @@ RefType getErasedRepr(Type t) {
t instanceof NullType and result instanceof TypeObject t instanceof NullType and result instanceof TypeObject
} }
class DataFlowType extends SrcRefType { final private class SrcRefTypeFinal = SrcRefType;
class DataFlowType extends SrcRefTypeFinal {
DataFlowType() { this = getErasedRepr(_) } DataFlowType() { this = getErasedRepr(_) }
string toString() { result = ppReprType(this) }
} }
pragma[nomagic] pragma[nomagic]
@@ -371,7 +375,7 @@ DataFlowType getNodeType(Node n) {
} }
/** Gets a string representation of a type returned by `getErasedRepr`. */ /** Gets a string representation of a type returned by `getErasedRepr`. */
string ppReprType(DataFlowType t) { private string ppReprType(SrcRefType t) {
if t.(BoxedType).getPrimitiveType().getName() = "double" if t.(BoxedType).getPrimitiveType().getName() = "double"
then result = "Number" then result = "Number"
else result = t.toString() else result = t.toString()

View File

@@ -534,7 +534,7 @@ newtype TDataFlowType = TAnyFlow()
class DataFlowType extends TDataFlowType { class DataFlowType extends TDataFlowType {
/** Gets a textual representation of this element. */ /** Gets a textual representation of this element. */
string toString() { result = "DataFlowType" } string toString() { result = "" }
} }
/** A node that performs a type cast. */ /** A node that performs a type cast. */
@@ -578,9 +578,6 @@ DataFlowType getNodeType(Node node) {
exists(node) exists(node)
} }
/** Gets a string representation of a type returned by `getErasedRepr`. */
string ppReprType(DataFlowType t) { none() }
//-------- //--------
// Extra flow // Extra flow
//-------- //--------

View File

@@ -2077,9 +2077,6 @@ DataFlowType getNodeType(Node n) {
result = TUnknownDataFlowType() result = TUnknownDataFlowType()
} }
/** Gets a string representation of a `DataFlowType`. */
string ppReprType(DataFlowType t) { none() }
pragma[inline] pragma[inline]
private predicate compatibleTypesNonSymRefl(DataFlowType t1, DataFlowType t2) { private predicate compatibleTypesNonSymRefl(DataFlowType t1, DataFlowType t2) {
t1 != TUnknownDataFlowType() and t1 != TUnknownDataFlowType() and

View File

@@ -124,8 +124,6 @@ signature module InputSig<LocationSig Location> {
string toString(); string toString();
} }
string ppReprType(DataFlowType t);
/** /**
* Holds if `t1` and `t2` are compatible types. * Holds if `t1` and `t2` are compatible types.
* *

View File

@@ -3445,9 +3445,11 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
AccessPathApproxConsNil() { this = TConsNil(c, t) } AccessPathApproxConsNil() { this = TConsNil(c, t) }
private string ppTyp() { result = t.toString() and result != "" }
override string toString() { override string toString() {
// The `concat` becomes "" if `ppReprType` has no result. // The `concat` becomes "" if `ppTyp` has no result.
result = "[" + c.toString() + "]" + concat(" : " + ppReprType(t)) result = "[" + c.toString() + "]" + concat(" : " + this.ppTyp())
} }
override Content getHead() { result = c } override Content getHead() { result = c }
@@ -3668,7 +3670,9 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
ParamNodeEx getParamNode() { result = p } ParamNodeEx getParamNode() { result = p }
override string toString() { result = p + concat(" : " + ppReprType(t)) + " " + ap } private string ppTyp() { result = t.toString() and result != "" }
override string toString() { result = p + concat(" : " + this.ppTyp()) + " " + ap }
Location getLocation() { result = p.getLocation() } Location getLocation() { result = p.getLocation() }
} }
@@ -3935,10 +3939,12 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
override int length() { result = 1 + tail_.length() } override int length() { result = 1 + tail_.length() }
private string ppTyp() { result = t.toString() and result != "" }
private string toStringImpl(boolean needsSuffix) { private string toStringImpl(boolean needsSuffix) {
tail_ = TAccessPathNil() and tail_ = TAccessPathNil() and
needsSuffix = false and needsSuffix = false and
result = head_.toString() + "]" + concat(" : " + ppReprType(t)) result = head_.toString() + "]" + concat(" : " + this.ppTyp())
or or
result = head_ + ", " + tail_.(AccessPathCons).toStringImpl(needsSuffix) result = head_ + ", " + tail_.(AccessPathCons).toStringImpl(needsSuffix)
or or
@@ -4087,9 +4093,8 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
private string ppType() { private string ppType() {
this instanceof PathNodeSink and result = "" this instanceof PathNodeSink and result = ""
or or
exists(DataFlowType t | t = this.(PathNodeMid).getType() | exists(string t | t = this.(PathNodeMid).getType().toString() |
// The `concat` becomes "" if `ppReprType` has no result. if t = "" then result = "" else result = " : " + t
result = concat(" : " + ppReprType(t))
) )
} }
@@ -5402,9 +5407,8 @@ module MakeImpl<LocationSig Location, InputSig<Location> Lang> {
private string ppType() { private string ppType() {
this instanceof PartialPathNodeRev and result = "" this instanceof PartialPathNodeRev and result = ""
or or
exists(DataFlowType t | t = this.(PartialPathNodeFwd).getType() | exists(string t | t = this.(PartialPathNodeFwd).getType().toString() |
// The `concat` becomes "" if `ppReprType` has no result. if t = "" then result = "" else result = " : " + t
result = concat(" : " + ppReprType(t))
) )
} }

View File

@@ -1310,9 +1310,6 @@ DataFlowType getNodeType(Node n) {
any() // return the singleton DataFlowType until we support type pruning for Swift any() // return the singleton DataFlowType until we support type pruning for Swift
} }
/** Gets a string representation of a `DataFlowType`. */
string ppReprType(DataFlowType t) { none() }
/** /**
* Holds if `t1` and `t2` are compatible, that is, whether data can flow from * Holds if `t1` and `t2` are compatible, that is, whether data can flow from
* a node of type `t1` to a node of type `t2`. * a node of type `t1` to a node of type `t2`.