@@ -2,10 +2,40 @@ private import powershell
22private import semmle.code.powershell.controlflow.internal.Scope
33private import internal.Internal as Internal
44
5+ private predicate isFunctionParameterImpl ( Internal:: Parameter p , Function f , int i ) {
6+ function_definition_parameter ( f , i , p )
7+ or
8+ function_member_parameter ( f , i , p )
9+ }
10+
11+ private predicate hasParameterBlockImpl ( Internal:: Parameter p , ParamBlock block , int i ) {
12+ param_block_parameter ( block , i , p )
13+ }
14+
15+ /**
16+ * Gets the enclosing scope of `p`.
17+ *
18+ * For a function parameter, this is the function body. For a parameter from a
19+ * parameter block, this is the enclosing scope of the parameter block.
20+ *
21+ * In both of the above cases, the enclosing scope is the function body.
22+ */
23+ private Scope getEnclosingScopeImpl ( Internal:: Parameter p ) {
24+ exists ( Function f |
25+ isFunctionParameterImpl ( p , f , _) and
26+ result = f .getBody ( )
27+ )
28+ or
29+ exists ( ParamBlock b |
30+ hasParameterBlockImpl ( p , b , _) and
31+ result = b .getEnclosingScope ( )
32+ )
33+ }
34+
535bindingset [ scope]
636pragma [ inline_late]
737private predicate isParameterImpl ( string name , Scope scope ) {
8- exists ( Internal:: Parameter p | p .getName ( ) = name and p . getEnclosingScope ( ) = scope )
38+ exists ( Internal:: Parameter p | p .getName ( ) = name and getEnclosingScopeImpl ( p ) = scope )
939 or
1040 name = "_"
1141}
@@ -47,17 +77,13 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
4777
4878 override string getName ( ) { result = p .getName ( ) }
4979
50- final override Scope getEnclosingScope ( ) { result = p . getEnclosingScope ( ) }
80+ final override Scope getEnclosingScope ( ) { result = getEnclosingScopeImpl ( p ) }
5181
5282 override predicate hasParameterBlock ( ParamBlock block , int i ) {
53- param_block_parameter ( block , i , p )
83+ hasParameterBlockImpl ( p , block , i )
5484 }
5585
56- override predicate isFunctionParameter ( Function f , int i ) {
57- function_definition_parameter ( f , i , p )
58- or
59- function_member_parameter ( f , i , p )
60- }
86+ override predicate isFunctionParameter ( Function f , int i ) { isFunctionParameterImpl ( p , f , i ) }
6187
6288 override Expr getDefaultValue ( ) { result = p .getDefaultValue ( ) }
6389}
@@ -161,4 +187,6 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
161187 predicate hasDefaultValue ( ) { exists ( this .getDefaultValue ( ) ) }
162188
163189 int getIndex ( ) { this .isFunctionParameter ( _, result ) }
190+
191+ Function getFunction ( ) { result .getBody ( ) = this .getDeclaringScope ( ) }
164192}
0 commit comments