@@ -2,10 +2,40 @@ private import powershell
2
2
private import semmle.code.powershell.controlflow.internal.Scope
3
3
private import internal.Internal as Internal
4
4
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
+
5
35
bindingset [ scope]
6
36
pragma [ inline_late]
7
37
private 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 )
9
39
or
10
40
name = "_"
11
41
}
@@ -47,17 +77,13 @@ private class InternalParameter extends ParameterImpl, TInternalParameter {
47
77
48
78
override string getName ( ) { result = p .getName ( ) }
49
79
50
- final override Scope getEnclosingScope ( ) { result = p . getEnclosingScope ( ) }
80
+ final override Scope getEnclosingScope ( ) { result = getEnclosingScopeImpl ( p ) }
51
81
52
82
override predicate hasParameterBlock ( ParamBlock block , int i ) {
53
- param_block_parameter ( block , i , p )
83
+ hasParameterBlockImpl ( p , block , i )
54
84
}
55
85
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 ) }
61
87
62
88
override Expr getDefaultValue ( ) { result = p .getDefaultValue ( ) }
63
89
}
@@ -161,4 +187,6 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
161
187
predicate hasDefaultValue ( ) { exists ( this .getDefaultValue ( ) ) }
162
188
163
189
int getIndex ( ) { this .isFunctionParameter ( _, result ) }
190
+
191
+ Function getFunction ( ) { result .getBody ( ) = this .getDeclaringScope ( ) }
164
192
}
0 commit comments