Skip to content

Commit df6d86b

Browse files
michaelnebelhvitved
authored andcommitted
C#: Use getUnderlyingCallable instead of asCallable.
1 parent 6f7af11 commit df6d86b

File tree

7 files changed

+45
-47
lines changed

7 files changed

+45
-47
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module SummaryComponent {
6969
SummaryComponent jump(Callable c) {
7070
result =
7171
return(any(DataFlowDispatch::JumpReturnKind jrk |
72-
jrk.getTarget().asCallable() = c.getUnboundDeclaration() and
72+
jrk.getTarget().getUnderlyingCallable() = c.getUnboundDeclaration() and
7373
jrk.getTargetReturnKind() instanceof DataFlowDispatch::NormalReturnKind
7474
))
7575
}

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ newtype TReturnKind =
7373
TJumpReturnKind(DataFlowCallable target, ReturnKind rk) {
7474
rk instanceof NormalReturnKind and
7575
(
76-
target.asCallable() instanceof Constructor or
77-
not target.asCallable().getReturnType() instanceof VoidType
76+
target.getUnderlyingCallable() instanceof Constructor or
77+
not target.getUnderlyingCallable().getReturnType() instanceof VoidType
7878
)
7979
or
80-
exists(target.asCallable().getParameter(rk.(OutRefReturnKind).getPosition()))
80+
exists(target.getUnderlyingCallable().getParameter(rk.(OutRefReturnKind).getPosition()))
8181
}
8282

8383
private module Cached {
@@ -143,7 +143,7 @@ private module DispatchImpl {
143143
* restricted to those `call`s for which a context might make a difference.
144144
*/
145145
DataFlowCallable viableImplInCallContext(NonDelegateDataFlowCall call, DataFlowCall ctx) {
146-
result.asCallable() =
146+
result.getUnderlyingCallable() =
147147
call.getDispatchCall()
148148
.getADynamicTargetInCallContext(ctx.(NonDelegateDataFlowCall).getDispatchCall())
149149
.getUnboundDeclaration()
@@ -240,7 +240,9 @@ class JumpReturnKind extends ReturnKind, TJumpReturnKind {
240240
* A type for modeling dataflow callables.
241241
*/
242242
newtype TDataFlowCallable =
243-
TDotNetCallable(DotNet::Callable c) { c.isUnboundDeclaration() } or
243+
TDotNetCallable(DotNet::Callable c) {
244+
c.isUnboundDeclaration() and not c instanceof FlowSummary::SummarizedCallable
245+
} or
244246
TSummarizedCallable(FlowSummary::SummarizedCallable c)
245247

246248
class DataFlowCallable extends TDataFlowCallable {
@@ -250,15 +252,15 @@ class DataFlowCallable extends TDataFlowCallable {
250252
/** Get the underlying summarized callable, if any. */
251253
FlowSummary::SummarizedCallable asSummarizedCallable() { this = TSummarizedCallable(result) }
252254

253-
/** Gets a textual representation of this dataflow callable. */
254-
string toString() {
255-
result = [this.asCallable().toString(), this.asSummarizedCallable().toString()]
255+
DotNet::Callable getUnderlyingCallable() {
256+
result = this.asCallable() or result = this.asSummarizedCallable()
256257
}
257258

259+
/** Gets a textual representation of this dataflow callable. */
260+
string toString() { result = this.getUnderlyingCallable().toString() }
261+
258262
/** Get the location of this dataflow callable, if any. */
259-
Location getLocation() {
260-
result = this.asCallable().getLocation() or result = this.asSummarizedCallable().getLocation()
261-
}
263+
Location getLocation() { result = this.getUnderlyingCallable().getLocation() }
262264
}
263265

264266
/** A call relevant for data flow. */
@@ -330,7 +332,7 @@ class NonDelegateDataFlowCall extends DataFlowCall, TNonDelegateCall {
330332
override DataFlow::ExprNode getNode() { result.getControlFlowNode() = cfn }
331333

332334
override DataFlowCallable getEnclosingCallable() {
333-
result.asCallable() = cfn.getEnclosingCallable()
335+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
334336
}
335337

336338
override string toString() { result = cfn.toString() }
@@ -360,7 +362,7 @@ class ExplicitDelegateLikeDataFlowCall extends DelegateDataFlowCall, TExplicitDe
360362
override DataFlow::ExprNode getNode() { result.getControlFlowNode() = cfn }
361363

362364
override DataFlowCallable getEnclosingCallable() {
363-
result.asCallable() = cfn.getEnclosingCallable()
365+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
364366
}
365367

366368
override string toString() { result = cfn.toString() }
@@ -379,14 +381,14 @@ class TransitiveCapturedDataFlowCall extends DataFlowCall, TTransitiveCapturedCa
379381

380382
TransitiveCapturedDataFlowCall() { this = TTransitiveCapturedCall(cfn, target) }
381383

382-
override DataFlowCallable getARuntimeTarget() { result.asCallable() = target }
384+
override DataFlowCallable getARuntimeTarget() { result.getUnderlyingCallable() = target }
383385

384386
override ControlFlow::Nodes::ElementNode getControlFlowNode() { result = cfn }
385387

386388
override DataFlow::ExprNode getNode() { none() }
387389

388390
override DataFlowCallable getEnclosingCallable() {
389-
result.asCallable() = cfn.getEnclosingCallable()
391+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
390392
}
391393

392394
override string toString() { result = "[transitive] " + cfn.toString() }
@@ -402,15 +404,15 @@ class CilDataFlowCall extends DataFlowCall, TCilCall {
402404

403405
override DataFlowCallable getARuntimeTarget() {
404406
// There is no dispatch library for CIL, so do not consider overrides for now
405-
result.asCallable() = getCallableForDataFlow(call.getTarget())
407+
result.getUnderlyingCallable() = getCallableForDataFlow(call.getTarget())
406408
}
407409

408410
override ControlFlow::Nodes::ElementNode getControlFlowNode() { none() }
409411

410412
override DataFlow::ExprNode getNode() { result.getExpr() = call }
411413

412414
override DataFlowCallable getEnclosingCallable() {
413-
result.asCallable() = call.getEnclosingCallable()
415+
result.getUnderlyingCallable() = call.getEnclosingCallable()
414416
}
415417

416418
override string toString() { result = call.toString() }

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ abstract class NodeImpl extends Node {
6565

6666
private class ExprNodeImpl extends ExprNode, NodeImpl {
6767
override DataFlowCallable getEnclosingCallableImpl() {
68-
result.asCallable() = this.getExpr().(CIL::Expr).getEnclosingCallable()
68+
result.getUnderlyingCallable() = this.getExpr().(CIL::Expr).getEnclosingCallable()
6969
or
70-
result.asCallable() = this.getControlFlowNodeImpl().getEnclosingCallable()
70+
result.getUnderlyingCallable() = this.getControlFlowNodeImpl().getEnclosingCallable()
7171
}
7272

7373
override DotNet::Type getTypeImpl() {
@@ -852,7 +852,7 @@ class SsaDefinitionNode extends NodeImpl, TSsaDefinitionNode {
852852
Ssa::Definition getDefinition() { result = def }
853853

854854
override DataFlowCallable getEnclosingCallableImpl() {
855-
result.asCallable() = def.getEnclosingCallable()
855+
result.getUnderlyingCallable() = def.getEnclosingCallable()
856856
}
857857

858858
override Type getTypeImpl() { result = def.getSourceVariable().getType() }
@@ -911,10 +911,12 @@ private module ParameterNodes {
911911
Callable getCallable() { result = callable }
912912

913913
override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
914-
callable = c.asCallable() and pos.isThisParameter()
914+
callable = c.getUnderlyingCallable() and pos.isThisParameter()
915915
}
916916

917-
override DataFlowCallable getEnclosingCallableImpl() { result.asCallable() = callable }
917+
override DataFlowCallable getEnclosingCallableImpl() {
918+
result.getUnderlyingCallable() = callable
919+
}
918920

919921
override Type getTypeImpl() { result = callable.getDeclaringType() }
920922

@@ -961,7 +963,7 @@ private module ParameterNodes {
961963

962964
override predicate isParameterOf(DataFlowCallable c, ParameterPosition pos) {
963965
pos.isImplicitCapturedParameterPosition(def.getSourceVariable().getAssignable()) and
964-
c.asCallable() = this.getEnclosingCallable()
966+
c.getUnderlyingCallable() = this.getEnclosingCallable()
965967
}
966968
}
967969

@@ -1078,7 +1080,7 @@ private module ArgumentNodes {
10781080
}
10791081

10801082
override DataFlowCallable getEnclosingCallableImpl() {
1081-
result.asCallable() = cfn.getEnclosingCallable()
1083+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
10821084
}
10831085

10841086
override Type getTypeImpl() { result = v.getType() }
@@ -1107,7 +1109,7 @@ private module ArgumentNodes {
11071109
override ControlFlow::Node getControlFlowNodeImpl() { result = cfn }
11081110

11091111
override DataFlowCallable getEnclosingCallableImpl() {
1110-
result.asCallable() = cfn.getEnclosingCallable()
1112+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
11111113
}
11121114

11131115
override Type getTypeImpl() { result = cfn.getElement().(Expr).getType() }
@@ -1146,7 +1148,7 @@ private module ArgumentNodes {
11461148
}
11471149

11481150
override DataFlowCallable getEnclosingCallableImpl() {
1149-
result.asCallable() = callCfn.getEnclosingCallable()
1151+
result.getUnderlyingCallable() = callCfn.getEnclosingCallable()
11501152
}
11511153

11521154
override Type getTypeImpl() { result = this.getParameter().getType() }
@@ -1227,7 +1229,7 @@ private module ReturnNodes {
12271229
override NormalReturnKind getKind() { any() }
12281230

12291231
override DataFlowCallable getEnclosingCallableImpl() {
1230-
result.asCallable() = yrs.getEnclosingCallable()
1232+
result.getUnderlyingCallable() = yrs.getEnclosingCallable()
12311233
}
12321234

12331235
override Type getTypeImpl() { result = yrs.getEnclosingCallable().getReturnType() }
@@ -1253,7 +1255,7 @@ private module ReturnNodes {
12531255
override NormalReturnKind getKind() { any() }
12541256

12551257
override DataFlowCallable getEnclosingCallableImpl() {
1256-
result.asCallable() = expr.getEnclosingCallable()
1258+
result.getUnderlyingCallable() = expr.getEnclosingCallable()
12571259
}
12581260

12591261
override Type getTypeImpl() { result = expr.getEnclosingCallable().getReturnType() }
@@ -1903,7 +1905,7 @@ private module PostUpdateNodes {
19031905
}
19041906

19051907
override DataFlowCallable getEnclosingCallableImpl() {
1906-
result.asCallable() = cfn.getEnclosingCallable()
1908+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
19071909
}
19081910

19091911
override DotNet::Type getTypeImpl() { result = oc.getType() }
@@ -1923,7 +1925,7 @@ private module PostUpdateNodes {
19231925
override ExprNode getPreUpdateNode() { cfn = result.getControlFlowNode() }
19241926

19251927
override DataFlowCallable getEnclosingCallableImpl() {
1926-
result.asCallable() = cfn.getEnclosingCallable()
1928+
result.getUnderlyingCallable() = cfn.getEnclosingCallable()
19271929
}
19281930

19291931
override Type getTypeImpl() { result = cfn.getElement().(Expr).getType() }
@@ -2012,11 +2014,11 @@ class LambdaCallKind = Unit;
20122014
/** Holds if `creation` is an expression that creates a delegate for `c`. */
20132015
predicate lambdaCreation(ExprNode creation, LambdaCallKind kind, DataFlowCallable c) {
20142016
exists(Expr e | e = creation.getExpr() |
2015-
c.asCallable() = e.(AnonymousFunctionExpr)
2017+
c.getUnderlyingCallable() = e.(AnonymousFunctionExpr)
20162018
or
2017-
c.asCallable() = e.(CallableAccess).getTarget().getUnboundDeclaration()
2019+
c.getUnderlyingCallable() = e.(CallableAccess).getTarget().getUnboundDeclaration()
20182020
or
2019-
c.asCallable() =
2021+
c.getUnderlyingCallable() =
20202022
e.(AddressOfExpr).getOperand().(CallableAccess).getTarget().getUnboundDeclaration()
20212023
) and
20222024
kind = TMkUnit()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Node extends TNode {
4141

4242
/** Gets the enclosing callable of this node. */
4343
final Callable getEnclosingCallable() {
44-
result = this.(NodeImpl).getEnclosingCallableImpl().asCallable()
44+
result = this.(NodeImpl).getEnclosingCallableImpl().getUnderlyingCallable()
4545
}
4646

4747
/** Gets the control flow node corresponding to this node, if any. */
@@ -103,7 +103,7 @@ class ParameterNode extends Node instanceof ParameterNodeImpl {
103103
DotNet::Parameter getParameter() {
104104
exists(DataFlowCallable c, ParameterPosition ppos |
105105
super.isParameterOf(c, ppos) and
106-
result = c.asCallable().getParameter(ppos.getPosition())
106+
result = c.getUnderlyingCallable().getParameter(ppos.getPosition())
107107
)
108108
}
109109

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DataFlowType getReturnType(SummarizedCallable c, ReturnKind rk) {
5959
or
6060
rk =
6161
any(JumpReturnKind jrk |
62-
result = getReturnTypeBase(jrk.getTarget().asCallable(), jrk.getTargetReturnKind())
62+
result = getReturnTypeBase(jrk.getTarget().getUnderlyingCallable(), jrk.getTargetReturnKind())
6363
)
6464
}
6565

@@ -256,16 +256,10 @@ class InterpretNode extends TInterpretNode {
256256
DataFlowCall asCall() { this = TDataFlowCall_(result) }
257257

258258
/** Gets the callable that this node corresponds to, if any. */
259-
DataFlowCallable asCallable() {
260-
result.asCallable() = this.asElement() or
261-
result.asSummarizedCallable() = this.asElement()
262-
}
259+
DataFlowCallable asCallable() { result.getUnderlyingCallable() = this.asElement() }
263260

264261
/** Gets the target of this call, if any. */
265-
Callable getCallTarget() {
266-
result = viableCallable(this.asCall()).asSummarizedCallable() or
267-
result = viableCallable(this.asCall()).asCallable()
268-
}
262+
Callable getCallTarget() { result = viableCallable(this.asCall()).getUnderlyingCallable() }
269263

270264
/** Gets a textual representation of this node. */
271265
string toString() {

csharp/ql/lib/semmle/code/csharp/exprs/Call.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ class DelegateLikeCall extends Call, DelegateLikeCall_ {
568568
final override Callable getARuntimeTarget() {
569569
exists(ExplicitDelegateLikeDataFlowCall call |
570570
this = call.getCall() and
571-
result = viableCallableLambda(call, _).asCallable()
571+
result = viableCallableLambda(call, _).getUnderlyingCallable()
572572
)
573573
}
574574

csharp/ql/src/utils/model-generator/internal/CaptureModelsSpecific.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ string returnNodeAsOutput(DataFlowImplCommon::ReturnNodeExt node) {
112112
* Gets the enclosing callable of `ret`.
113113
*/
114114
CS::Callable returnNodeEnclosingCallable(DataFlowImplCommon::ReturnNodeExt ret) {
115-
result = DataFlowImplCommon::getNodeEnclosingCallable(ret).asCallable()
115+
result = DataFlowImplCommon::getNodeEnclosingCallable(ret).getUnderlyingCallable()
116116
}
117117

118118
/**

0 commit comments

Comments
 (0)