Skip to content

Commit 2106d48

Browse files
committed
Swift: Add 'Argument.getIndex()' and use it in 'DataFlowDispatch'.
1 parent 0d8a945 commit 2106d48

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

swift/ql/lib/codeql/swift/dataflow/internal/DataFlowDispatch.qll

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ class DataFlowCallable extends TDataFlowCallable {
4444
* inside library callables with a flow summary.
4545
*/
4646
class DataFlowCall extends ExprNode {
47-
DataFlowCall() {
48-
this.asExpr() instanceof CallExpr
49-
}
47+
DataFlowCall() { this.asExpr() instanceof CallExpr }
5048

5149
/** Gets the enclosing callable. */
5250
DataFlowCallable getEnclosingCallable() { none() }
@@ -59,19 +57,20 @@ private module Cached {
5957

6058
/** Gets a viable run-time target for the call `call`. */
6159
cached
62-
DataFlowCallable viableCallable(DataFlowCall call) {
60+
DataFlowCallable viableCallable(DataFlowCall call) {
6361
result = TDataFlowFunc(call.asExpr().(CallExpr).getStaticTarget())
6462
}
6563

6664
cached
6765
newtype TArgumentPosition =
6866
TThisArgument() or
69-
TPositionalArgument(int n) { n in [0 .. 100] } // we rely on default exprs generated in the caller for ordering. TODO: compute range properly. TODO: varargs?
67+
// we rely on default exprs generated in the caller for ordering
68+
TPositionalArgument(int n) { n = any(Argument arg).getIndex() }
7069

7170
cached
72-
newtype TParameterPosition =
73-
TThisParameter() or
74-
TPositionalParameter(int n) { n in [0 .. 100] } // TODO: compute range properly
71+
newtype TParameterPosition =
72+
TThisParameter() or
73+
TPositionalParameter(int n) { n = any(Argument arg).getIndex() }
7574
}
7675

7776
import Cached
@@ -95,22 +94,17 @@ class ParameterPosition extends TParameterPosition {
9594
}
9695

9796
class PositionalParameterPosition extends ParameterPosition, TPositionalParameter {
98-
int getIndex() {
99-
this = TPositionalParameter(result)
100-
}
97+
int getIndex() { this = TPositionalParameter(result) }
10198
}
10299

103-
104100
/** An argument position. */
105101
class ArgumentPosition extends TArgumentPosition {
106102
/** Gets a textual representation of this position. */
107103
string toString() { none() }
108104
}
109105

110106
class PositionalArgumentPosition extends ArgumentPosition, TPositionalArgument {
111-
int getIndex() {
112-
this = TPositionalArgument(result)
113-
}
107+
int getIndex() { this = TPositionalArgument(result) }
114108
}
115109

116110
/** Holds if arguments at position `apos` match parameters at position `ppos`. */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
private import codeql.swift.generated.expr.Argument
2+
private import codeql.swift.elements.expr.ApplyExpr
23

34
class Argument extends ArgumentBase {
45
override string toString() { result = this.getLabel() + ": " + this.getExpr().toString() }
6+
7+
int getIndex() { any(ApplyExpr apply).getArgument(result) = this }
58
}

0 commit comments

Comments
 (0)