Skip to content

Commit 56c703e

Browse files
committed
PS: Move a bunch of predicates into ScriptBlock.
1 parent d3b9e13 commit 56c703e

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class ScriptBlock extends @script_block, Ast {
1010
else result = "{...}"
1111
}
1212

13-
override SourceLocation getLocation() { script_block_location(this, result) }
13+
override Location getLocation() { script_block_location(this, result) }
1414

1515
int getNumUsings() { script_block(this, result, _, _, _, _) }
1616

@@ -51,6 +51,42 @@ class ScriptBlock extends @script_block, Ast {
5151
ModuleSpecification getAModuleSpecification() { result = this.getModuleSpecification(_) }
5252

5353
final override Scope getEnclosingScope() { result = this }
54+
55+
/**
56+
* Gets the `i`'th paramter in this scope.
57+
*
58+
* This may be both function paramters and parameter block parameters.
59+
*/
60+
Parameter getParameter(int i) {
61+
exists(Function func |
62+
func.getBody() = this and
63+
result = func.getParameter(i)
64+
)
65+
or
66+
this.isTopLevel() and
67+
result = this.getParamBlock().getParameter(i)
68+
}
69+
70+
/**
71+
* Gets a paramter in this scope.
72+
*
73+
* This may be both function parameters and parameter block parameters.
74+
*/
75+
Parameter getAParameter() { result = this.getParameter(_) }
76+
77+
Parameter getThisParameter() {
78+
exists(Function func |
79+
func.getBody() = this and
80+
result = func.getThisParameter()
81+
)
82+
}
83+
84+
/** Gets the number of function parameters. */
85+
final int getNumberOfParameters() { result = count(this.getAParameter()) }
86+
87+
final Parameter getParameterExcludingPiplines(int i) {
88+
result = this.getParamBlock().getParameterExcludingPiplines(i)
89+
}
5490
}
5591

5692
/** A `process` block. */
@@ -69,3 +105,7 @@ class ProcessBlock extends NamedBlock {
69105
result = scriptBlock.getEnclosingFunction().getAParameter()
70106
}
71107
}
108+
109+
class TopLevelScriptBlock extends ScriptBlock {
110+
TopLevelScriptBlock() { this.isTopLevel() }
111+
}

powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,7 @@ Scope scopeOf(Ast n) {
1414
* A variable scope. This is either a top-level (file), a module, a class,
1515
* or a callable.
1616
*/
17-
class Scope extends Ast, @script_block {
17+
class Scope extends Ast, ScriptBlock {
1818
/** Gets the outer scope, if any. */
1919
Scope getOuterScope() { result = scopeOf(this) }
20-
21-
/**
22-
* Gets the `i`'th paramter in this scope.
23-
*
24-
* This may be both function paramters and parameter block parameters.
25-
*/
26-
Parameter getParameter(int i) {
27-
exists(Function func |
28-
func.getBody() = this and
29-
result = func.getParameter(i)
30-
)
31-
}
32-
33-
/**
34-
* Gets a paramter in this scope.
35-
*
36-
* This may be both function paramters and parameter block parameters.
37-
*/
38-
Parameter getAParameter() { result = this.getParameter(_) }
39-
40-
Parameter getThisParameter() {
41-
exists(Function func |
42-
func.getBody() = this and
43-
result = func.getThisParameter()
44-
)
45-
}
4620
}

0 commit comments

Comments
 (0)