Skip to content

Commit 9107075

Browse files
committed
PS: Fixup CFG after the introduction of variables into the AST.
1 parent 810978d commit 9107075

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

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

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,10 @@ predicate succExit(CfgScope scope, Ast last, Completion c) { scope.exit(last, c)
7676

7777
/** Defines the CFG by dispatch on the various AST types. */
7878
module Trees {
79-
class NonDefaultParameterTree extends LeafTree instanceof Parameter {
80-
NonDefaultParameterTree() { not exists(super.getDefaultValue()) }
81-
}
82-
83-
class DefaultParameterTree extends StandardPostOrderTree instanceof Parameter {
84-
DefaultParameterTree() { exists(super.getDefaultValue()) }
85-
79+
class ParameterBlockTree extends StandardPostOrderTree instanceof ParamBlock {
8680
override AstNode getChildNode(int i) {
87-
i = 0 and
88-
result = super.getDefaultValue()
81+
exists(Parameter p | p = super.getParameter(i) | result = p.getDefaultValue())
8982
}
90-
91-
final override predicate propagatesAbnormal(AstNode child) { child = super.getDefaultValue() }
92-
}
93-
94-
class ParameterBlockTree extends StandardPostOrderTree instanceof ParamBlock {
95-
override AstNode getChildNode(int i) { result = super.getParameter(i) }
9683
}
9784

9885
abstract class ScriptBlockTree extends ControlFlowTree instanceof ScriptBlock {
@@ -185,21 +172,31 @@ module Trees {
185172

186173
FunctionScriptBlockTree() { func.getBody() = this }
187174

188-
AstNode getParameter(int i) { result = func.getFunctionParameter(i) }
175+
Expr getDefaultValue(int i) {
176+
exists(Parameter p |
177+
p =
178+
rank[i + 1](Parameter cand, int j |
179+
cand.hasDefaultValue() and j = cand.getIndex()
180+
|
181+
cand order by j
182+
) and
183+
result = p.getDefaultValue()
184+
)
185+
}
189186

190-
int getNumberOfParameters() { result = func.getNumberOfFunctionParameters() }
187+
int getNumberOfDefaultValues() { result = count(int i | exists(this.getDefaultValue(i))) }
191188

192189
override predicate succ(AstNode pred, AstNode succ, Completion c) {
193190
// Step to the first parameter
194191
pred = this and
195-
first(this.getParameter(0), succ) and
192+
first(this.getDefaultValue(0), succ) and
196193
completionIsSimple(c)
197194
or
198195
// Step to the next parameter
199196
exists(int i |
200-
last(this.getParameter(i), pred, c) and
197+
last(this.getDefaultValue(i), pred, c) and
201198
completionIsNormal(c) and
202-
first(this.getParameter(i + 1), succ)
199+
first(this.getDefaultValue(i + 1), succ)
203200
)
204201
or
205202
// Body steps
@@ -208,12 +205,12 @@ module Trees {
208205

209206
final override predicate succEntry(AstNode n, Completion c) {
210207
// If there are no paramters we enter the body directly
211-
not exists(this.getParameter(0)) and
208+
not exists(this.getDefaultValue(_)) and
212209
n = this and
213210
completionIsSimple(c)
214211
or
215212
// Once we are done with the last parameter we enter the body
216-
last(this.getParameter(this.getNumberOfParameters() - 1), n, c) and
213+
last(this.getDefaultValue(this.getNumberOfDefaultValues() - 1), n, c) and
217214
completionIsNormal(c)
218215
}
219216
}
@@ -380,11 +377,11 @@ module Trees {
380377
or
381378
// Emptiness test to variable declaration
382379
pred = this and
383-
first(super.getVariable(), succ) and
380+
first(super.getVarAccess(), succ) and
384381
completionIsSimple(c)
385382
or
386383
// Variable declaration to body
387-
last(super.getVariable(), succ, c) and
384+
last(super.getVarAccess(), succ, c) and
388385
completionIsNormal(c) and
389386
first(this.getBody(), succ)
390387
or

0 commit comments

Comments
 (0)