Skip to content

Commit dc34b10

Browse files
committed
C#: Replace Ssa::ExplicitDefinition with SsaExplicitWrite.
1 parent a6c7f27 commit dc34b10

18 files changed

Lines changed: 67 additions & 86 deletions

File tree

csharp/ql/consistency-queries/SsaConsistency.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ query predicate localDeclWithSsaDef(LocalVariableDeclExpr d) {
77
// Local variables in C# must be initialized before every use, so uninitialized
88
// local variables should not have an SSA definition, as that would imply that
99
// the declaration is live (can reach a use without passing through a definition)
10-
exists(ExplicitDefinition def |
11-
d = def.getADefinition().(AssignableDefinitions::LocalVariableDefinition).getDeclaration()
10+
exists(SsaExplicitWrite def |
11+
d = def.getDefinition().(AssignableDefinitions::LocalVariableDefinition).getDeclaration()
1212
|
1313
not d = any(ForeachStmt fs).getVariableDeclExpr() and
1414
not d = any(SpecificCatchClause scc).getVariableDeclExpr() and

csharp/ql/lib/semmle/code/csharp/Assignable.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,7 @@ class AssignableDefinition extends TAssignableDefinition {
500500
*/
501501
pragma[nomagic]
502502
AssignableRead getAFirstRead() {
503-
exists(Ssa::ExplicitDefinition def | result = Ssa::ssaGetAFirstUse(def) |
504-
this = def.getADefinition()
505-
)
503+
exists(SsaExplicitWrite def | result = Ssa::ssaGetAFirstUse(def) | this = def.getDefinition())
506504
}
507505

508506
/** Gets a textual representation of this assignable definition. */

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ private SsaDefinition getAnSsaQualifier(Expr e, ControlFlowNode cfn) {
588588
private AssignableAccess getATrackedAccess(SsaDefinition def, ControlFlowNode cfn) {
589589
result = def.getARead() and cfn = result.getControlFlowNode()
590590
or
591-
result = def.(Ssa::ExplicitDefinition).getADefinition().getTargetAccess() and
591+
result = def.(SsaExplicitWrite).getDefinition().getTargetAccess() and
592592
cfn = def.getControlFlowNode()
593593
}
594594

@@ -830,9 +830,7 @@ module Internal {
830830
).getARead()
831831
}
832832

833-
private predicate nullDef(Ssa::ExplicitDefinition def) {
834-
nullValueImplied(def.getADefinition().getSource())
835-
}
833+
private predicate nullDef(SsaExplicitWrite def) { nullValueImplied(def.getValue()) }
836834

837835
predicate nonNullValueImplied(Expr e) {
838836
nonNullValue(e)
@@ -845,9 +843,7 @@ module Internal {
845843
).getARead()
846844
}
847845

848-
private predicate nonNullDef(Ssa::ExplicitDefinition def) {
849-
nonNullValueImplied(def.getADefinition().getSource())
850-
}
846+
private predicate nonNullDef(SsaExplicitWrite def) { nonNullValueImplied(def.getValue()) }
851847

852848
/** A callable that always returns a non-`null` value. */
853849
private class NonNullCallable extends Callable {

csharp/ql/lib/semmle/code/csharp/dataflow/Nullness.qll

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ class AlwaysNullExpr extends Expr {
8080
}
8181

8282
/** Holds if SSA definition `def` is always `null`. */
83-
private predicate nullDef(Ssa::ExplicitDefinition def) {
84-
def.getADefinition().getSource() instanceof AlwaysNullExpr
85-
}
83+
private predicate nullDef(SsaExplicitWrite def) { def.getValue() instanceof AlwaysNullExpr }
8684

8785
/** An expression that is never `null`. */
8886
class NonNullExpr extends Expr {
@@ -108,10 +106,10 @@ class NonNullExpr extends Expr {
108106
}
109107

110108
/** Holds if SSA definition `def` is never `null`. */
111-
private predicate nonNullDef(Ssa::ExplicitDefinition def) {
112-
def.getADefinition().getSource() instanceof NonNullExpr
109+
private predicate nonNullDef(SsaExplicitWrite def) {
110+
def.getValue() instanceof NonNullExpr
113111
or
114-
exists(AssignableDefinition ad | ad = def.getADefinition() |
112+
exists(AssignableDefinition ad | ad = def.getDefinition() |
115113
ad instanceof AssignableDefinitions::PatternDefinition
116114
or
117115
ad =
@@ -191,7 +189,7 @@ private predicate defMaybeNull(SsaDefinition def, ControlFlowNode node, string m
191189
not de = any(Ssa::PhiNode phi).getARead() and
192190
// Don't use a check as reason if there is a `null` assignment
193191
// or argument
194-
not def.(Ssa::ExplicitDefinition).getADefinition().getSource() instanceof MaybeNullExpr and
192+
not def.(SsaExplicitWrite).getValue() instanceof MaybeNullExpr and
195193
not isMaybeNullArgument(def, _)
196194
)
197195
or
@@ -205,7 +203,7 @@ private predicate defMaybeNull(SsaDefinition def, ControlFlowNode node, string m
205203
)
206204
or
207205
// If the source of a variable is `null` then the variable may be `null`
208-
exists(AssignableDefinition adef | adef = def.(Ssa::ExplicitDefinition).getADefinition() |
206+
exists(AssignableDefinition adef | adef = def.(SsaExplicitWrite).getDefinition() |
209207
adef.getSource() = maybeNullExpr(node.asExpr()) and
210208
reason = adef.getExpr() and
211209
msg = "because of $@ assignment"
@@ -336,7 +334,7 @@ class Dereference extends G::DereferenceableExpr {
336334

337335
private predicate isAlwaysNull0(SsaDefinition def) {
338336
forall(SsaDefinition input | input = getAnUltimateDefinition(def) |
339-
input.(Ssa::ExplicitDefinition).getADefinition().getSource() instanceof AlwaysNullExpr
337+
input.(SsaExplicitWrite).getValue() instanceof AlwaysNullExpr
340338
) and
341339
not nonNullDef(def) and
342340
this = def.getARead() and

csharp/ql/lib/semmle/code/csharp/dataflow/SSA.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,19 +428,25 @@ module Ssa {
428428
}
429429

430430
/**
431+
* DEPRECATED: Use `SsaExplicitWrite` instead.
432+
*
431433
* An SSA definition that corresponds to an explicit assignable definition.
432434
*/
433-
class ExplicitDefinition extends Definition, SsaImpl::WriteDefinition {
435+
deprecated class ExplicitDefinition extends Definition, SsaImpl::WriteDefinition {
434436
AssignableDefinition ad;
435437

436438
ExplicitDefinition() { SsaImpl::explicitDefinition(this, _, ad) }
437439

438440
/**
441+
* DEPRECATED: Use `SsaExplicitWrite.getDefinition()` instead.
442+
*
439443
* Gets an underlying assignable definition. The result is always unique,
440444
* except for pathological `out`/`ref` assignments like `M(out x, out x)`,
441445
* where there may be more than one underlying definition.
442446
*/
443-
final AssignableDefinition getADefinition() { result = SsaImpl::getADefinition(this) }
447+
deprecated final AssignableDefinition getADefinition() {
448+
result = SsaImpl::getADefinition(this)
449+
}
444450

445451
/**
446452
* DEPRECATED.

csharp/ql/lib/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ module VariableCapture {
272272
or
273273
exists(SsaDefinition def, AssignableDefinition adef |
274274
LocalFlow::defAssigns(adef, _, _, e1) and
275-
def.getAnUltimateDefinition().(Ssa::ExplicitDefinition).getADefinition() = adef and
275+
def.getAnUltimateDefinition().(SsaExplicitWrite).getDefinition() = adef and
276276
def.getARead().getControlFlowNode() = e2
277277
)
278278
}
@@ -600,8 +600,8 @@ module LocalFlow {
600600
or
601601
ThisFlow::adjacentThisRefs(nodeFrom.(PostUpdateNode).getPreUpdateNode(), nodeTo)
602602
or
603-
exists(AssignableDefinition def, ControlFlowNode cfn, Ssa::ExplicitDefinition ssaDef |
604-
ssaDef.getADefinition() = def and
603+
exists(AssignableDefinition def, ControlFlowNode cfn, SsaExplicitWrite ssaDef |
604+
ssaDef.getDefinition() = def and
605605
ssaDef.getControlFlowNode() = cfn and
606606
nodeFrom = TAssignableDefinitionNode(def, cfn) and
607607
nodeTo.(SsaDefinitionNode).getDefinition() = ssaDef
@@ -2220,12 +2220,11 @@ private predicate readContentStep(Node node1, Content c, Node node2) {
22202220
c instanceof ElementContent
22212221
or
22222222
exists(
2223-
ForeachStmt fs, Ssa::ExplicitDefinition def,
2224-
AssignableDefinitions::LocalVariableDefinition defTo
2223+
ForeachStmt fs, SsaExplicitWrite def, AssignableDefinitions::LocalVariableDefinition defTo
22252224
|
22262225
node1.asExpr() = fs.getIterableExpr() and
22272226
defTo.getDeclaration() = fs.getVariableDeclExpr() and
2228-
def.getADefinition() = defTo and
2227+
def.getDefinition() = defTo and
22292228
node2.(SsaDefinitionNode).getDefinition() = def and
22302229
c instanceof ElementContent
22312230
)

csharp/ql/lib/semmle/code/csharp/dataflow/internal/SsaImpl.qll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ private module Cached {
825825
}
826826

827827
cached
828-
AssignableDefinition getADefinition(Ssa::ExplicitDefinition def) {
828+
deprecated AssignableDefinition getADefinition(Ssa::ExplicitDefinition def) {
829829
exists(Ssa::SourceVariable v, AssignableDefinition ad | explicitDefinition(def, v, ad) |
830830
result = ad or
831831
result = getASameOutRefDefAfter(v, ad)
@@ -858,7 +858,9 @@ private module Cached {
858858
}
859859

860860
cached
861-
predicate explicitDefinition(WriteDefinition def, Ssa::SourceVariable v, AssignableDefinition ad) {
861+
deprecated predicate explicitDefinition(
862+
WriteDefinition def, Ssa::SourceVariable v, AssignableDefinition ad
863+
) {
862864
exists(BasicBlock bb, int i |
863865
def.definesAt(v, bb, i) and
864866
variableDefinition(bb, i, v, ad)
@@ -1023,7 +1025,7 @@ private module DataFlowIntegrationInput implements Impl::DataFlowIntegrationInpu
10231025
* as we, conservatively, consider such definitions to be certain.
10241026
*/
10251027
predicate allowFlowIntoUncertainDef(UncertainWriteDefinition def) {
1026-
def instanceof Ssa::ExplicitDefinition
1028+
def instanceof SsaExplicitWrite
10271029
or
10281030
def =
10291031
any(Ssa::ImplicitQualifierDefinition qdef |

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/RangeUtils.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private module Impl {
1919
}
2020

2121
/** Holds if SSA definition `def` equals `e + delta`. */
22-
predicate ssaUpdateStep(ExplicitDefinition def, ExprNode e, int delta) {
22+
predicate ssaUpdateStep(SsaExplicitWrite def, ExprNode e, int delta) {
2323
exists(ControlFlowNode cfn | cfn = def.getControlFlowNode() |
2424
e = cfn.(ExprNode::Assignment).getRightOperand() and
2525
delta = 0 and

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SignAnalysisSpecific.qll

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module Private {
3535

3636
class Expr = CS::ControlFlowNodes::ExprNode;
3737

38-
class VariableUpdate = CS::Ssa::ExplicitDefinition;
38+
class VariableUpdate = CS::SsaExplicitWrite;
3939

4040
class Field = CS::Field;
4141

@@ -122,37 +122,25 @@ private module Impl {
122122
}
123123

124124
/** Returns the underlying variable update of the explicit SSA variable `v`. */
125-
Ssa::ExplicitDefinition getExplicitSsaAssignment(Ssa::ExplicitDefinition v) { result = v }
125+
SsaExplicitWrite getExplicitSsaAssignment(SsaExplicitWrite v) { result = v }
126126

127127
/** Returns the assignment of the variable update `def`. */
128-
ExprNode getExprFromSsaAssignment(Ssa::ExplicitDefinition def) {
129-
exists(AssignableDefinition adef |
130-
adef = def.getADefinition() and
131-
hasChild(adef.getExpr(), adef.getSource(), def.getControlFlowNode(), result)
132-
)
133-
or
134-
exists(AssignableDefinitions::AssignOperationDefinition adef |
135-
adef = def.getADefinition() and
136-
result.getExpr() = adef.getSource()
137-
)
138-
}
128+
ExprNode getExprFromSsaAssignment(SsaExplicitWrite def) { result.getExpr() = def.getValue() }
139129

140130
/** Holds if `def` can have any sign. */
141-
predicate explicitSsaDefWithAnySign(Ssa::ExplicitDefinition def) {
142-
not exists(def.getADefinition().getSource()) and
143-
not def.getElement() instanceof MutatorOperation
131+
predicate explicitSsaDefWithAnySign(SsaExplicitWrite def) {
132+
not exists(def.getValue()) and
133+
not def.getDefiningExpr() instanceof MutatorOperation
144134
}
145135

146136
/** Returns the operand of the operation if `def` is a decrement. */
147-
ExprNode getDecrementOperand(Ssa::ExplicitDefinition def) {
148-
hasChild(def.getElement(), def.getElement().(DecrementOperation).getOperand(),
149-
def.getControlFlowNode(), result)
137+
ExprNode getDecrementOperand(SsaExplicitWrite def) {
138+
result.getExpr() = def.getDefiningExpr().(DecrementOperation).getOperand()
150139
}
151140

152141
/** Returns the operand of the operation if `def` is an increment. */
153-
ExprNode getIncrementOperand(Ssa::ExplicitDefinition def) {
154-
hasChild(def.getElement(), def.getElement().(IncrementOperation).getOperand(),
155-
def.getControlFlowNode(), result)
142+
ExprNode getIncrementOperand(SsaExplicitWrite def) {
143+
result.getExpr() = def.getDefiningExpr().(IncrementOperation).getOperand()
156144
}
157145

158146
/** Gets the variable underlying the implicit SSA variable `def`. */

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/SsaUtils.qll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class SsaVariable extends SsaDefinition {
1717

1818
/** Gets a node that reads `src` via an SSA explicit definition. */
1919
ExprNode getAnExplicitDefinitionRead(ExprNode src) {
20-
exists(ExplicitDefinition def |
20+
exists(SsaExplicitWrite def |
2121
def.getARead().getControlFlowNode() = result and
22-
hasChild(def.getElement(), def.getADefinition().getSource(), def.getControlFlowNode(), src)
22+
hasChild(def.getDefiningExpr(), def.getValue(), def.getControlFlowNode(), src)
2323
)
2424
}
2525

@@ -45,15 +45,15 @@ ExprNode ssaRead(SsaDefinition v, int delta) {
4545
delta = d1 + c.getIntValue()
4646
)
4747
or
48-
v.(ExplicitDefinition).getControlFlowNode().(ExprNode::PreIncrExpr) = result and delta = 0
48+
v.(SsaExplicitWrite).getControlFlowNode().(ExprNode::PreIncrExpr) = result and delta = 0
4949
or
50-
v.(ExplicitDefinition).getControlFlowNode().(ExprNode::PreDecrExpr) = result and delta = 0
50+
v.(SsaExplicitWrite).getControlFlowNode().(ExprNode::PreDecrExpr) = result and delta = 0
5151
or
52-
v.(ExplicitDefinition).getControlFlowNode().(ExprNode::PostIncrExpr) = result and delta = 1 // x++ === ++x - 1
52+
v.(SsaExplicitWrite).getControlFlowNode().(ExprNode::PostIncrExpr) = result and delta = 1 // x++ === ++x - 1
5353
or
54-
v.(ExplicitDefinition).getControlFlowNode().(ExprNode::PostDecrExpr) = result and delta = -1 // x-- === --x + 1
54+
v.(SsaExplicitWrite).getControlFlowNode().(ExprNode::PostDecrExpr) = result and delta = -1 // x-- === --x + 1
5555
or
56-
v.(ExplicitDefinition).getControlFlowNode().(ExprNode::Assignment) = result and delta = 0
56+
v.(SsaExplicitWrite).getControlFlowNode().(ExprNode::Assignment) = result and delta = 0
5757
or
5858
result.(ExprNode::AssignExpr).getRightOperand() = ssaRead(v, delta)
5959
}

0 commit comments

Comments
 (0)