Skip to content

Commit c8c811c

Browse files
committed
C#: Update some references in preparation for CFG swap.
1 parent 7ad4a14 commit c8c811c

File tree

27 files changed

+80
-78
lines changed

27 files changed

+80
-78
lines changed

csharp/ql/lib/semmle/code/csharp/commons/Constants.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ private import semmle.code.csharp.commons.StructuralComparison as StructuralComp
66

77
pragma[noinline]
88
private predicate isConstantCondition0(ControlFlowNode cfn, boolean b) {
9-
exists(cfn.getASuccessorByType(any(ControlFlow::BooleanSuccessor t | t.getValue() = b))) and
10-
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessorByType(t))) = 1
9+
exists(cfn.getASuccessor(any(ControlFlow::BooleanSuccessor t | t.getValue() = b))) and
10+
strictcount(ControlFlow::SuccessorType t | exists(cfn.getASuccessor(t))) = 1
1111
}
1212

1313
/**

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module ControlFlow {
3030
/** Gets the control flow element that this node corresponds to, if any. */
3131
final ControlFlowElement getAstNode() { result = super.getAstNode() }
3232

33+
Expr asExpr() { result = this.getAstNode() }
34+
35+
Stmt asStmt() { result = this.getAstNode() }
36+
3337
/** Gets the basic block that this control flow node belongs to. */
3438
BasicBlock getBasicBlock() { result.getANode() = this }
3539

@@ -175,13 +179,13 @@ module ControlFlow {
175179
}
176180

177181
/** Gets a successor node of a given type, if any. */
178-
Node getASuccessorByType(SuccessorType t) { result = this.getASuccessor(t) }
182+
Node getASuccessor(SuccessorType t) { result = super.getASuccessor(t) }
179183

180184
/** Gets an immediate successor, if any. */
181-
Node getASuccessor() { result = this.getASuccessorByType(_) }
185+
Node getASuccessor() { result = this.getASuccessor(_) }
182186

183187
/** Gets an immediate predecessor node of a given flow type, if any. */
184-
Node getAPredecessorByType(SuccessorType t) { result.getASuccessorByType(t) = this }
188+
Node getAPredecessorByType(SuccessorType t) { result.getASuccessor(t) = this }
185189

186190
/** Gets an immediate predecessor, if any. */
187191
Node getAPredecessor() { result = this.getAPredecessorByType(_) }
@@ -203,7 +207,7 @@ module ControlFlow {
203207
* on line 1.
204208
*/
205209
Node getATrueSuccessor() {
206-
result = this.getASuccessorByType(any(BooleanSuccessor t | t.getValue() = true))
210+
result = this.getASuccessor(any(BooleanSuccessor t | t.getValue() = true))
207211
}
208212

209213
/**
@@ -223,7 +227,7 @@ module ControlFlow {
223227
* on line 1.
224228
*/
225229
Node getAFalseSuccessor() {
226-
result = this.getASuccessorByType(any(BooleanSuccessor t | t.getValue() = false))
230+
result = this.getASuccessor(any(BooleanSuccessor t | t.getValue() = false))
227231
}
228232

229233
/** Gets the enclosing callable of this control flow node. */
@@ -242,9 +246,6 @@ module ControlFlow {
242246

243247
/** A node for a callable exit point, annotated with the type of exit. */
244248
class AnnotatedExitNode extends Node instanceof Impl::AnnotatedExitNode {
245-
/** Holds if this node represent a normal exit. */
246-
final predicate isNormal() { super.isNormal() }
247-
248249
/** Gets the callable that this exit applies to. */
249250
Callable getCallable() { result = this.getScope() }
250251

@@ -283,7 +284,7 @@ module ControlFlow {
283284
class ExprNode extends ElementNode {
284285
Expr e;
285286

286-
ExprNode() { e = unique(Expr e_ | e_ = this.getAstNode() | e_) }
287+
ExprNode() { e = unique(Expr e_ | e_ = this.asExpr() | e_) }
287288

288289
/** Gets the expression that this control-flow node belongs to. */
289290
Expr getExpr() { result = e }

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,10 +1185,7 @@ module Internal {
11851185
nodeIsGuardedBySameSubExprSsaDef(guarded, _, g, subCfn, sub, v, def)
11861186
|
11871187
def =
1188-
guarded
1189-
.getAstNode()
1190-
.(AccessOrCallExpr)
1191-
.getAnSsaQualifier(guarded.getBasicBlock().getANode())
1188+
guarded.asExpr().(AccessOrCallExpr).getAnSsaQualifier(guarded.getBasicBlock().getANode())
11921189
)
11931190
}
11941191
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private predicate defMaybeNull(Ssa::Definition def, ControlFlowNode node, string
222222
or
223223
// If the source of a variable is `null` then the variable may be `null`
224224
exists(AssignableDefinition adef | adef = def.(Ssa::ExplicitDefinition).getADefinition() |
225-
adef.getSource() = maybeNullExpr(node.getAstNode()) and
225+
adef.getSource() = maybeNullExpr(node.asExpr()) and
226226
reason = adef.getExpr() and
227227
msg = "because of $@ assignment"
228228
)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,9 @@ module Ssa {
426426
* This is either an expression, for example `x = 0`, a parameter, or a
427427
* callable. Phi nodes have no associated syntax element.
428428
*/
429-
Element getElement() { result = this.getControlFlowNode().getAstNode() }
429+
Element getElement() {
430+
result.(ControlFlowElement).getControlFlowNode() = this.getControlFlowNode()
431+
}
430432

431433
/** Gets the callable to which this SSA definition belongs. */
432434
final Callable getEnclosingCallable() {
@@ -561,7 +563,7 @@ module Ssa {
561563
}
562564

563565
/** Gets the callable that this entry definition belongs to. */
564-
final Callable getCallable() { result = this.getBasicBlock().getCallable() }
566+
final Callable getCallable() { result = this.getBasicBlock().getEnclosingCallable() }
565567

566568
override Element getElement() { result = this.getCallable() }
567569

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module BaseSsa {
2929
Callable c, ControlFlow::BasicBlocks::EntryBlock bb, SsaInput::SourceVariable v
3030
) {
3131
exists(ControlFlow::BasicBlocks::EntryBlock entry |
32-
c = entry.getCallable() and
32+
c = entry.getEnclosingCallable() and
3333
// In case `c` has multiple bodies, we want each body to get its own implicit
3434
// entry definition. In case `c` doesn't have multiple bodies, the line below
3535
// is simply the same as `bb = entry`, because `entry.getFirstNode().getASuccessor()`

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ private module Cached {
7575
newtype TDataFlowCall =
7676
TNonDelegateCall(ControlFlow::Nodes::ElementNode cfn, DispatchCall dc) {
7777
DataFlowImplCommon::forceCachingInSameStage() and
78-
cfn.getAstNode() = dc.getCall()
78+
cfn.asExpr() = dc.getCall()
7979
} or
8080
TExplicitDelegateLikeCall(ControlFlow::Nodes::ElementNode cfn, DelegateLikeCall dc) {
81-
cfn.getAstNode() = dc
81+
cfn.asExpr() = dc
8282
} or
8383
TSummaryCall(FlowSummary::SummarizedCallable c, FlowSummaryImpl::Private::SummaryNode receiver) {
8484
FlowSummaryImpl::Private::summaryCallbackRange(c, receiver)

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ abstract private class LocalFunctionCreationNode extends NodeImpl, TLocalFunctio
133133

134134
LocalFunctionCreationNode() {
135135
this = TLocalFunctionCreationNode(cfn, isPostUpdate) and
136-
function = cfn.getAstNode().(LocalFunctionStmt).getLocalFunction()
136+
function = cfn.asStmt().(LocalFunctionStmt).getLocalFunction()
137137
}
138138

139139
LocalFunction getFunction() { result = function }
@@ -181,7 +181,7 @@ private module ThisFlow {
181181
private predicate primaryConstructorThisAccess(Node n, BasicBlock bb, int ppos) {
182182
exists(Parameter p |
183183
n.(PrimaryConstructorThisAccessPreNode).getParameter() = p and
184-
bb.getCallable() = p.getCallable() and
184+
bb.getEnclosingCallable() = p.getCallable() and
185185
ppos = p.getPosition()
186186
)
187187
}
@@ -279,11 +279,11 @@ module VariableCapture {
279279
private import TaintTrackingPrivate as TaintTrackingPrivate
280280

281281
Callable basicBlockGetEnclosingCallable(BasicBlocks::BasicBlock bb) {
282-
result = bb.getCallable()
282+
result = bb.getEnclosingCallable()
283283
}
284284

285285
private predicate thisAccess(ControlFlowNode cfn, InstanceCallable c) {
286-
ThisFlow::thisAccessExpr(cfn.getAstNode()) and
286+
ThisFlow::thisAccessExpr(cfn.asExpr()) and
287287
cfn.getEnclosingCallable().getEnclosingCallable*() = c
288288
}
289289

@@ -369,7 +369,7 @@ module VariableCapture {
369369
CapturedVariable v;
370370

371371
VariableRead() {
372-
this.getAstNode().(AssignableRead).getTarget() = v.asLocalScopeVariable()
372+
this.asExpr().(AssignableRead).getTarget() = v.asLocalScopeVariable()
373373
or
374374
thisAccess(this, v.asThis())
375375
}
@@ -380,14 +380,16 @@ module VariableCapture {
380380
class ClosureExpr extends Expr {
381381
Callable c;
382382

383-
ClosureExpr() { lambdaCreationExpr(this.getAstNode(), c) }
383+
ClosureExpr() {
384+
lambdaCreationExpr(any(ControlFlowElement e | e.getControlFlowNode() = this), c)
385+
}
384386

385387
predicate hasBody(Callable body) { body = c }
386388

387389
predicate hasAliasedAccess(Expr f) {
388390
closureFlowStep+(this, f) and not closureFlowStep(f, _)
389391
or
390-
isLocalFunctionCallReceiver(_, f.getAstNode(), c)
392+
isLocalFunctionCallReceiver(_, f.asExpr(), c)
391393
}
392394
}
393395

@@ -1024,7 +1026,7 @@ private module Cached {
10241026

10251027
cached
10261028
newtype TNode =
1027-
TExprNode(ControlFlow::Nodes::ElementNode cfn) { cfn.getAstNode() instanceof Expr } or
1029+
TExprNode(ControlFlow::Nodes::ElementNode cfn) { exists(cfn.asExpr()) } or
10281030
TSsaNode(SsaImpl::DataFlowIntegration::SsaNode node) or
10291031
TAssignableDefinitionNode(AssignableDefinition def, ControlFlowNode cfn) {
10301032
cfn = def.getExpr().getAControlFlowNode()
@@ -1039,17 +1041,17 @@ private module Cached {
10391041
} or
10401042
TDelegateSelfReferenceNode(Callable c) { lambdaCreationExpr(_, c) } or
10411043
TLocalFunctionCreationNode(ControlFlow::Nodes::ElementNode cfn, Boolean isPostUpdate) {
1042-
cfn.getAstNode() instanceof LocalFunctionStmt
1044+
cfn.asStmt() instanceof LocalFunctionStmt
10431045
} or
10441046
TYieldReturnNode(ControlFlow::Nodes::ElementNode cfn) {
1045-
any(Callable c).canYieldReturn(cfn.getAstNode())
1047+
any(Callable c).canYieldReturn(cfn.asExpr())
10461048
} or
10471049
TAsyncReturnNode(ControlFlow::Nodes::ElementNode cfn) {
1048-
any(Callable c | c.(Modifiable).isAsync()).canReturn(cfn.getAstNode())
1050+
any(Callable c | c.(Modifiable).isAsync()).canReturn(cfn.asExpr())
10491051
} or
1050-
TMallocNode(ControlFlow::Nodes::ElementNode cfn) { cfn.getAstNode() instanceof ObjectCreation } or
1052+
TMallocNode(ControlFlow::Nodes::ElementNode cfn) { cfn.asExpr() instanceof ObjectCreation } or
10511053
TObjectInitializerNode(ControlFlow::Nodes::ElementNode cfn) {
1052-
cfn.getAstNode().(ObjectCreation).hasInitializer()
1054+
cfn.asExpr().(ObjectCreation).hasInitializer()
10531055
} or
10541056
TExprPostUpdateNode(ControlFlow::Nodes::ExprNode cfn) {
10551057
(
@@ -1521,10 +1523,10 @@ private module ArgumentNodes {
15211523
override DataFlowCallable getEnclosingCallableImpl() {
15221524
result.getAControlFlowNode() = cfn
15231525
or
1524-
result = getEnclosingStaticFieldOrProperty(cfn.getAstNode())
1526+
result = getEnclosingStaticFieldOrProperty(cfn.asExpr())
15251527
}
15261528

1527-
override Type getTypeImpl() { result = cfn.getAstNode().(Expr).getType() }
1529+
override Type getTypeImpl() { result = cfn.asExpr().getType() }
15281530

15291531
override Location getLocationImpl() { result = cfn.getLocation() }
15301532

@@ -1562,7 +1564,7 @@ private module ArgumentNodes {
15621564
override DataFlowCallable getEnclosingCallableImpl() {
15631565
result.getAControlFlowNode() = callCfn
15641566
or
1565-
result = getEnclosingStaticFieldOrProperty(callCfn.getAstNode())
1567+
result = getEnclosingStaticFieldOrProperty(callCfn.asExpr())
15661568
}
15671569

15681570
override Type getTypeImpl() { result = this.getParameter().getType() }
@@ -1665,7 +1667,7 @@ private module ReturnNodes {
16651667
private ControlFlow::Nodes::ElementNode cfn;
16661668
private Expr expr;
16671669

1668-
AsyncReturnNode() { this = TAsyncReturnNode(cfn) and expr = cfn.getAstNode() }
1670+
AsyncReturnNode() { this = TAsyncReturnNode(cfn) and expr = cfn.asExpr() }
16691671

16701672
Expr getExpr() { result = expr }
16711673

@@ -2421,10 +2423,10 @@ DataFlowType getNodeType(Node n) {
24212423
not lambdaCreation(n, _, _) and
24222424
not isLocalFunctionCallReceiver(_, n.asExpr(), _)
24232425
or
2424-
[
2425-
n.asExpr().(ControlFlowElement),
2426-
n.(LocalFunctionCreationPreNode).getUnderlyingControlFlowNode().getAstNode()
2427-
] = result.getADelegateCreation()
2426+
n.asExpr() = result.getADelegateCreation()
2427+
or
2428+
n.(LocalFunctionCreationPreNode).getUnderlyingControlFlowNode() =
2429+
result.getADelegateCreation().getControlFlowNode()
24282430
}
24292431

24302432
private class DataFlowNullType extends Gvn::GvnType {
@@ -2611,10 +2613,10 @@ module PostUpdateNodes {
26112613
override DataFlowCallable getEnclosingCallableImpl() {
26122614
result.getAControlFlowNode() = cfn
26132615
or
2614-
result = getEnclosingStaticFieldOrProperty(cfn.getAstNode())
2616+
result = getEnclosingStaticFieldOrProperty(cfn.asExpr())
26152617
}
26162618

2617-
override Type getTypeImpl() { result = cfn.getAstNode().(Expr).getType() }
2619+
override Type getTypeImpl() { result = cfn.asExpr().getType() }
26182620

26192621
override ControlFlowNode getControlFlowNodeImpl() { none() }
26202622

@@ -2766,7 +2768,7 @@ predicate lambdaCall(DataFlowCall call, LambdaCallKind kind, Node receiver) {
27662768
(
27672769
lambdaCallExpr(call, receiver.asExpr(), _) and
27682770
// local function calls can be resolved directly without a flow analysis
2769-
not call.getControlFlowNode().getAstNode() instanceof LocalFunctionCall
2771+
not call.getControlFlowNode().asExpr() instanceof LocalFunctionCall
27702772
or
27712773
receiver.(FlowSummaryNode).getSummaryNode() = call.(SummaryCall).getReceiver()
27722774
) and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class ExprNode extends Node, TExprNode {
8383
*/
8484
Expr getExprAtNode(ControlFlow::Nodes::ElementNode cfn) {
8585
this = TExprNode(cfn) and
86-
result = cfn.getAstNode()
86+
result = cfn.asExpr()
8787
}
8888
}
8989

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Consistency = Impl::Consistency;
5757
* Holds if the `i`th node of basic block `bb` reads source variable `v`.
5858
*/
5959
private predicate variableReadActual(ControlFlow::BasicBlock bb, int i, Ssa::SourceVariable v) {
60-
v.getAnAccess().(AssignableRead) = bb.getNode(i).getAstNode()
60+
v.getAnAccess().(AssignableRead) = bb.getNode(i).asExpr()
6161
}
6262

6363
private module SourceVariableImpl {
@@ -187,8 +187,7 @@ private module SourceVariableImpl {
187187
* ```
188188
*/
189189
predicate outRefExitRead(ControlFlow::BasicBlock bb, int i, LocalScopeSourceVariable v) {
190-
exists(ControlFlow::Nodes::AnnotatedExitNode exit |
191-
exit.isNormal() and
190+
exists(ControlFlow::Nodes::NormalExitNode exit |
192191
exists(LocalScopeVariable lsv |
193192
lsv = v.getAssignable() and
194193
bb.getNode(i) = exit and
@@ -820,7 +819,7 @@ private module Cached {
820819
cached
821820
predicate implicitEntryDefinition(ControlFlow::BasicBlock bb, Ssa::SourceVariable v) {
822821
exists(ControlFlow::BasicBlocks::EntryBlock entry, Callable c |
823-
c = entry.getCallable() and
822+
c = entry.getEnclosingCallable() and
824823
// In case `c` has multiple bodies, we want each body to get its own implicit
825824
// entry definition. In case `c` doesn't have multiple bodies, the line below
826825
// is simply the same as `bb = entry`, because `entry.getFirstNode().getASuccessor()`

0 commit comments

Comments
 (0)