From 56c703ec80bd2d287e89d83e82e7cd4eec6d4f2d Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Wed, 16 Oct 2024 20:11:49 +0100 Subject: [PATCH] PS: Move a bunch of predicates into ScriptBlock. --- .../semmle/code/powershell/ScriptBlock.qll | 42 ++++++++++++++++++- .../powershell/controlflow/internal/Scope.qll | 28 +------------ 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll b/powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll index 79691dbb6e5..4e6c7735223 100644 --- a/powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll +++ b/powershell/ql/lib/semmle/code/powershell/ScriptBlock.qll @@ -10,7 +10,7 @@ class ScriptBlock extends @script_block, Ast { else result = "{...}" } - override SourceLocation getLocation() { script_block_location(this, result) } + override Location getLocation() { script_block_location(this, result) } int getNumUsings() { script_block(this, result, _, _, _, _) } @@ -51,6 +51,42 @@ class ScriptBlock extends @script_block, Ast { ModuleSpecification getAModuleSpecification() { result = this.getModuleSpecification(_) } final override Scope getEnclosingScope() { result = this } + + /** + * Gets the `i`'th paramter in this scope. + * + * This may be both function paramters and parameter block parameters. + */ + Parameter getParameter(int i) { + exists(Function func | + func.getBody() = this and + result = func.getParameter(i) + ) + or + this.isTopLevel() and + result = this.getParamBlock().getParameter(i) + } + + /** + * Gets a paramter in this scope. + * + * This may be both function parameters and parameter block parameters. + */ + Parameter getAParameter() { result = this.getParameter(_) } + + Parameter getThisParameter() { + exists(Function func | + func.getBody() = this and + result = func.getThisParameter() + ) + } + + /** Gets the number of function parameters. */ + final int getNumberOfParameters() { result = count(this.getAParameter()) } + + final Parameter getParameterExcludingPiplines(int i) { + result = this.getParamBlock().getParameterExcludingPiplines(i) + } } /** A `process` block. */ @@ -69,3 +105,7 @@ class ProcessBlock extends NamedBlock { result = scriptBlock.getEnclosingFunction().getAParameter() } } + +class TopLevelScriptBlock extends ScriptBlock { + TopLevelScriptBlock() { this.isTopLevel() } +} diff --git a/powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll b/powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll index c1c1fab1c97..49d182548fd 100644 --- a/powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll +++ b/powershell/ql/lib/semmle/code/powershell/controlflow/internal/Scope.qll @@ -14,33 +14,7 @@ Scope scopeOf(Ast n) { * A variable scope. This is either a top-level (file), a module, a class, * or a callable. */ -class Scope extends Ast, @script_block { +class Scope extends Ast, ScriptBlock { /** Gets the outer scope, if any. */ Scope getOuterScope() { result = scopeOf(this) } - - /** - * Gets the `i`'th paramter in this scope. - * - * This may be both function paramters and parameter block parameters. - */ - Parameter getParameter(int i) { - exists(Function func | - func.getBody() = this and - result = func.getParameter(i) - ) - } - - /** - * Gets a paramter in this scope. - * - * This may be both function paramters and parameter block parameters. - */ - Parameter getAParameter() { result = this.getParameter(_) } - - Parameter getThisParameter() { - exists(Function func | - func.getBody() = this and - result = func.getThisParameter() - ) - } }