Skip to content

Commit 1062773

Browse files
committed
C#: Introduce Ssa::Definition::getElement() and AssignableDefinition::getElement()
1 parent 91e4f7a commit 1062773

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,12 @@ class AssignableDefinition extends TAssignableDefinition {
484484
*/
485485
Expr getExpr() { none() }
486486

487+
/**
488+
* Gets the underlying element associated with this definition. This is either
489+
* an expression or a parameter.
490+
*/
491+
Element getElement() { result = this.getExpr() }
492+
487493
/** DEPRECATED: Use `getAControlFlowNode()` instead. */
488494
deprecated
489495
ControlFlow::Node getControlFlowNode() { result = this.getAControlFlowNode() }
@@ -800,6 +806,8 @@ module AssignableDefinitions {
800806
result = p.getCallable().getEntryPoint()
801807
}
802808

809+
override Parameter getElement() { result = p }
810+
803811
override Callable getEnclosingCallable() {
804812
result = p.getCallable()
805813
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ private predicate defMaybeNull(Ssa::Definition def, string msg, Element reason)
163163
de = def.getARead() |
164164
reason = de.getANullCheck(_, true) and
165165
msg = "as suggested by $@ null check" and
166-
not def instanceof Ssa::PseudoDefinition and
167-
strictcount(Location l |
168-
l = any(Ssa::Definition def0 | de = def0.getARead()).getLocation()
166+
not de = any(Ssa::PseudoDefinition pdef).getARead() and
167+
strictcount(Element e |
168+
e = any(Ssa::Definition def0 | de = def0.getARead()).getElement()
169169
) = 1 and
170170
not nonNullDef(def) and
171171
// Don't use a check as reason if there is a `null` assignment

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,6 +2252,18 @@ module Ssa {
22522252
/** Gets the basic block to which this SSA definition belongs. */
22532253
BasicBlock getBasicBlock() { this.definesAt(result, _) }
22542254

2255+
/**
2256+
* Gets the syntax element associated with this SSA definition, if any.
2257+
* This is either an expression, for example `x = 0`, a parameter, or a
2258+
* callable. Pseudo nodes have no associated syntax element.
2259+
*/
2260+
Element getElement() {
2261+
exists(BasicBlock bb, int i |
2262+
this.definesAt(bb, i) |
2263+
result = bb.getNode(i).getElement()
2264+
)
2265+
}
2266+
22552267
/**
22562268
* Holds if this SSA definition assigns to `out`/`ref` parameter `p`, and the
22572269
* parameter may remain unchanged throughout the rest of the enclosing callable.
@@ -2341,6 +2353,8 @@ module Ssa {
23412353
isCapturedVariableDefinitionFlowOut(this, cdef)
23422354
}
23432355

2356+
override Element getElement() { result = ad.getElement() }
2357+
23442358
override string toString() {
23452359
if this.getADefinition() instanceof AssignableDefinitions::ImplicitParameterDefinition then
23462360
result = getToStringPrefix(this) + "SSA param(" + this.getSourceVariable() + ")"
@@ -2384,6 +2398,8 @@ module Ssa {
23842398
)
23852399
}
23862400

2401+
override Callable getElement() { result = this.getCallable() }
2402+
23872403
override string toString() {
23882404
if this.getSourceVariable().getAssignable() instanceof LocalScopeVariable then
23892405
result = getToStringPrefix(this) + "SSA capture def(" + this.getSourceVariable() + ")"

0 commit comments

Comments
 (0)