Skip to content

Commit fb9c587

Browse files
authored
Merge pull request #1126 from hvitved/csharp/performance-tweaks
C#: Fix a few minor performance regressions
2 parents 17e8b64 + 6cd8775 commit fb9c587

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

csharp/ql/src/API Abuse/DisposeNotCalledOnException.ql

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,28 @@ predicate isTriedAgainstException(ControlFlowElement cfe, ExceptionClass ec) {
4949
)
5050
}
5151

52+
private class DisposeCall extends MethodCall {
53+
DisposeCall() { this.getTarget() instanceof DisposeMethod }
54+
}
55+
56+
private predicate reachesDisposeCall(DisposeCall disposeCall, DataFlow::Node node) {
57+
DataFlow::localFlowStep(node, DataFlow::exprNode(disposeCall.getQualifier()))
58+
or
59+
exists(DataFlow::Node mid | reachesDisposeCall(disposeCall, mid) |
60+
DataFlow::localFlowStep(node, mid)
61+
)
62+
}
63+
5264
/**
5365
* Holds if `disposeCall` disposes the object created by `disposableCreation`.
5466
*/
55-
predicate disposeReachableFromDisposableCreation(MethodCall disposeCall, Expr disposableCreation) {
67+
predicate disposeReachableFromDisposableCreation(DisposeCall disposeCall, Expr disposableCreation) {
5668
// The qualifier of the Dispose call flows from something that introduced a disposable into scope
5769
(
5870
disposableCreation instanceof LocalScopeDisposableCreation or
5971
disposableCreation instanceof MethodCall
6072
) and
61-
DataFlow::localFlowStep+(DataFlow::exprNode(disposableCreation),
62-
DataFlow::exprNode(disposeCall.getQualifier())) and
63-
disposeCall.getTarget() instanceof DisposeMethod
73+
reachesDisposeCall(disposeCall, DataFlow::exprNode(disposableCreation))
6474
}
6575

6676
class MethodCallThatMayThrow extends MethodCall {
@@ -73,7 +83,7 @@ ControlFlowElement getACatchOrFinallyClauseChild() {
7383
result = getACatchOrFinallyClauseChild().getAChild()
7484
}
7585

76-
from MethodCall disposeCall, Expr disposableCreation, MethodCallThatMayThrow callThatThrows
86+
from DisposeCall disposeCall, Expr disposableCreation, MethodCallThatMayThrow callThatThrows
7787
where
7888
disposeReachableFromDisposableCreation(disposeCall, disposableCreation) and
7989
// The dispose call is not, itself, within a dispose method.

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class AssignableRead extends AssignableAccess {
7676
not nameOfChild(_, this)
7777
}
7878

79+
pragma[noinline]
80+
private ControlFlow::Node getAnAdjacentReadSameVar() {
81+
Ssa::Internal::adjacentReadPairSameVar(this.getAControlFlowNode(), result)
82+
}
83+
7984
/**
8085
* Gets a next read of the same underlying assignable. That is, a read
8186
* that can be reached from this read without passing through any other reads,
@@ -102,7 +107,7 @@ class AssignableRead extends AssignableAccess {
102107
*/
103108
AssignableRead getANextRead() {
104109
forex(ControlFlow::Node cfn | cfn = result.getAControlFlowNode() |
105-
Ssa::Internal::adjacentReadPairSameVar(this.getAControlFlowNode(), cfn)
110+
cfn = this.getAnAdjacentReadSameVar()
106111
)
107112
}
108113

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ module DataFlow {
704704
)
705705
}
706706

707+
pragma[nomagic]
707708
private ControlFlowElement getANonExactScopeChild(ControlFlowElement scope) {
708709
scope = getAScope(false) and
709710
result = scope

0 commit comments

Comments
 (0)