Skip to content

Commit b03eecb

Browse files
committed
C#: Add support for named arguments in getRuntimeArgumentForParameter.
1 parent 85f0ad6 commit b03eecb

File tree

1 file changed

+28
-13
lines changed
  • csharp/ql/lib/semmle/code/csharp/exprs

1 file changed

+28
-13
lines changed

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,39 @@ class Call extends DotNet::Call, Expr, @call {
183183
* Gets the argument that corresponds to parameter `p` of a potential
184184
* run-time target of this call.
185185
*
186-
* Does not consider
187-
* - default arguments,
188-
* - named arguments.
186+
* This takes into account both positional and named arguments, but does not
187+
* consider default arguments.
189188
*/
189+
cached
190190
Expr getRuntimeArgumentForParameter(Parameter p) {
191-
exists(Callable c |
192-
c = this.getARuntimeTarget() and
193-
p = c.getAParameter() and
194-
(
195-
p.isParams() and
196-
result = this.getRuntimeArgument(any(int i | i >= p.getPosition()))
197-
or
198-
not p.isParams() and
199-
result = this.getRuntimeArgument(p.getPosition())
200-
)
191+
p = this.getARuntimeTarget().getAParameter() and
192+
(
193+
// Appears in the positional part of the call
194+
result = this.getImplicitRuntimeArgument(p)
195+
or
196+
// Appears in the named part of the call
197+
result = this.getExplicitRuntimeArgument(p.getName())
198+
)
199+
}
200+
201+
pragma[noinline]
202+
private Expr getImplicitRuntimeArgument(Parameter p) {
203+
not exists(result.getExplicitArgumentName()) and
204+
(
205+
p.isParams() and
206+
result = this.getRuntimeArgument(any(int i | i >= p.getPosition()))
207+
or
208+
not p.isParams() and
209+
result = this.getRuntimeArgument(p.getPosition())
201210
)
202211
}
203212

213+
pragma[nomagic]
214+
private Expr getExplicitRuntimeArgument(string name) {
215+
result = this.getARuntimeArgument() and
216+
result.getExplicitArgumentName() = name
217+
}
218+
204219
/**
205220
* Gets the argument that corresponds to a parameter named `name` of a potential
206221
* run-time target of this call.

0 commit comments

Comments
 (0)