Skip to content

Commit 61d5f44

Browse files
committed
PS: AST support for 'this'.
1 parent a429485 commit 61d5f44

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,28 @@ abstract private class AbstractFunction extends Ast {
3333
/** Gets the number of function parameters. */
3434
final int getNumberOfFunctionParameters() { result = count(this.getAFunctionParameter()) }
3535

36-
/** Gets the number of parameters (both function and block). */
36+
/**
37+
* Gets the number of parameters (both function and block).
38+
* Note: This excludes the `this` parameter.
39+
*/
3740
final int getNumberOfParameters() { result = count(this.getAParameter()) }
3841

39-
/** Gets the i'th parameter of this function, if any. */
42+
/**
43+
* Gets the i'th parameter of this function, if any.
44+
*
45+
* This does not include the `this` parameter.
46+
*/
4047
final Parameter getParameter(int i) {
4148
result = this.getFunctionParameter(i)
4249
or
4350
result = this.getBody().getParamBlock().getParameter(i)
4451
}
4552

53+
final Parameter getThisParameter() {
54+
result.isThis() and
55+
result.getFunction() = this
56+
}
57+
4658
/** Gets any parameter of this function. */
4759
final Parameter getAParameter() { result = this.getParameter(_) }
4860

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ private newtype TParameterImpl =
4242
TInternalParameter(Internal::Parameter p) or
4343
TUnderscore(Scope scope) {
4444
exists(VarAccess va | va.getUserPath() = "_" and scope = va.getEnclosingScope())
45-
}
45+
} or
46+
TThisParameter(Scope scope) { exists(scope.getEnclosingFunction().getDeclaringType()) }
4647

4748
private class ParameterImpl extends TParameterImpl {
4849
abstract Location getLocation();
@@ -109,9 +110,22 @@ private class Underscore extends ParameterImpl, TUnderscore {
109110
final override Scope getEnclosingScope() { result = scope }
110111
}
111112

113+
private class ThisParameter extends ParameterImpl, TThisParameter {
114+
Scope scope;
115+
116+
ThisParameter() { this = TThisParameter(scope) }
117+
118+
override Location getLocation() { result = scope.getLocation() }
119+
120+
override string getName() { result = "this" }
121+
122+
final override Scope getEnclosingScope() { result = scope }
123+
}
124+
112125
private newtype TVariable =
113126
TLocalVariable(string name, Scope scope) {
114127
not isParameterImpl(name, scope) and
128+
not name = "this" and // This is modeled as a parameter
115129
exists(VarAccess va | va.getUserPath() = name and scope = va.getEnclosingScope())
116130
} or
117131
TParameter(ParameterImpl p)
@@ -186,8 +200,11 @@ class Parameter extends AbstractLocalScopeVariable, TParameter {
186200

187201
predicate hasDefaultValue() { exists(this.getDefaultValue()) }
188202

203+
/** Holds if this is the `this` parameter. */
204+
predicate isThis() { p instanceof ThisParameter }
205+
189206
/**
190-
* Gets the index of this parameter.
207+
* Gets the index of this parameter, if any.
191208
*
192209
* The parameter may be in a parameter block or a function parameter.
193210
*/

powershell/ql/lib/semmle/code/powershell/controlflow/CfgNodes.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,11 @@ module ExprNodes {
217217
/** Gets the name of this argument, if any. */
218218
string getName() { result = e.getName() }
219219

220-
StmtNodes::CmdCfgNode getCmd() { result.getAnArgument() = this }
220+
/** Holds if `this` is a qualifier to a call. */
221+
predicate isQualifier() { e.isQualifier() }
222+
223+
/** Gets the call for which this is an argument. */
224+
CallCfgNode getCall() { result.getAnArgument() = this or result.getQualifier() = this }
221225
}
222226

223227
private class InvokeMemberChildMapping extends ExprChildMapping, InvokeMemberExpr {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ class Scope extends Ast, @script_block {
3636
* This may be both function paramters and parameter block parameters.
3737
*/
3838
Parameter getAParameter() { result = this.getParameter(_) }
39+
40+
Parameter getThisParameter() {
41+
exists(Function func |
42+
func.getBody() = this and
43+
result = func.getThisParameter()
44+
)
45+
}
3946
}

0 commit comments

Comments
 (0)