PS: More cleanup of AST by fixing up toStrings and adding helper predicates.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-09-06 10:00:43 +01:00
parent 830de2c904
commit fd29c470c0
11 changed files with 78 additions and 14 deletions

View File

@@ -3,7 +3,7 @@ import powershell
class ArrayExpr extends @array_expression, Expr {
override SourceLocation getLocation() { array_expression_location(this, result) }
StmtBlock getStatementBlock() { array_expression(this, result) }
StmtBlock getStmtBlock() { array_expression(this, result) }
override string toString() { result = "ArrayExpression at: " + this.getLocation().toString() }
override string toString() { result = "@(...)" }
}

View File

@@ -15,7 +15,11 @@ class Attribute extends @attribute, AttributeBase {
NamedAttributeArgument getANamedArgument() { result = this.getNamedArgument(_) }
int getNumberOfArguments() { result = count(this.getAPositionalArgument()) }
Expr getPositionalArgument(int i) { attribute_positional_argument(this, i, result) }
Expr getAPositionalArgument() { result = this.getPositionalArgument(_) }
int getNumberOfPositionalArguments() { result = count(this.getAPositionalArgument()) }
}

View File

@@ -14,4 +14,24 @@ class CatchClause extends @catch_clause, Ast {
predicate isCatchAll() { catch_clause(this, _, true) } // TODO: Should be equivalent to not exists(this.getACatchType())
TryStmt getTryStmt() { result.getACatchClause() = this }
predicate isLast() {
exists(TryStmt ts, int last |
ts = this.getTryStmt() and
last = max(int i | exists(ts.getCatchClause(i))) and
this = ts.getCatchClause(last)
)
}
}
class GeneralCatchClause extends CatchClause {
GeneralCatchClause() { this.isCatchAll() }
override string toString() { result = "catch {...}" }
}
class SpecificCatchClause extends CatchClause {
SpecificCatchClause() { not this.isCatchAll() }
override string toString() { result = "catch[...] {...}" }
}

View File

@@ -15,9 +15,28 @@ class Cmd extends @command, CmdBase {
CmdElement getElement(int i) { command_command_element(this, i, result) }
Redirection getRedirection(int i) { command_redirection(this, i, result) }
StringConstExpr getCmdName() { result = this.getElement(0) }
CmdElement getAnElement() { result = this.getElement(_) }
Expr getArgument(int i) {
result =
rank[i + 1](CmdElement e, int j |
e = this.getElement(j) and
not e instanceof CmdParameter and
j > 0 // 0'th element is the command name itself
|
e order by j
)
}
Expr getNamedArgument(string name) {
exists(int i, CmdParameter p |
this.getElement(i) = p and
p.getName() = name and
result = p.getArgument()
)
}
Redirection getRedirection(int i) { command_redirection(this, i, result) }
Redirection getARedirection() { result = this.getRedirection(_) }
}

View File

@@ -5,7 +5,19 @@ class CmdParameter extends @command_parameter, CmdElement {
string getName() { command_parameter(this, result) }
Expr getArgument() { command_parameter_argument(this, result) }
Expr getArgument() {
// When an argumnt is of the form -Name:$var
command_parameter_argument(this, result)
or
// When an argument is of the form -Name $var
exists(int i, Cmd cmd |
cmd = this.getCmd() and
cmd.getElement(i) = this and
result = cmd.getElement(i + 1)
)
}
Cmd getCmd() { result.getElement(_) = this }
override string toString() { command_parameter(this, result) }
}

View File

@@ -13,6 +13,10 @@ class IfStmt extends @if_statement, Stmt {
StmtBlock getThen(int i) { if_statement_clause(this, i, _, result) } // TODO: Change @ast to @statement_block in dbscheme
int getNumberOfConditions() { result = count(this.getACondition()) }
StmtBlock getAThen() { result = this.getThen(_) }
/** ..., if any. */
StmtBlock getElse() { if_statement_else(this, result) } // TODO: Change @ast to @stmt_block in dbscheme

View File

@@ -1,9 +1,9 @@
import powershell
class InvokeMemberExpression extends @invoke_member_expression, MemberExprBase {
class InvokeMemberExpr extends @invoke_member_expression, MemberExprBase {
override SourceLocation getLocation() { invoke_member_expression_location(this, result) }
Expr getExpression() { invoke_member_expression(this, result, _) }
Expr getBase() { invoke_member_expression(this, result, _) }
CmdElement getMember() { invoke_member_expression(this, _, result) }
@@ -11,5 +11,5 @@ class InvokeMemberExpression extends @invoke_member_expression, MemberExprBase {
Expr getAnArgument() { invoke_member_expression_argument(this, _, result) }
override string toString() { result = "ArrayExpression at: " + this.getLocation().toString() }
override string toString() { result = "call to " + this.getMember() }
}

View File

@@ -1,9 +1,9 @@
import powershell
class NamedAttributeArgument extends @named_attribute_argument {
string toString() { result = this.getValue().toString() }
class NamedAttributeArgument extends @named_attribute_argument, Ast {
final override string toString() { result = this.getValue().toString() }
SourceLocation getLocation() { named_attribute_argument_location(this, result) }
final override SourceLocation getLocation() { named_attribute_argument_location(this, result) }
string getName() { named_attribute_argument(this, result, _) }

View File

@@ -1,10 +1,10 @@
import powershell
class StringConstExpression extends @string_constant_expression, BaseConstExpr {
class StringConstExpr extends @string_constant_expression, BaseConstExpr {
StringLiteral getValue() { string_constant_expression(this, result) }
/** Get the full string literal with all its parts concatenated */
override string toString() { result = getValue().toString() }
override string toString() { result = this.getValue().toString() }
override SourceLocation getLocation() { string_constant_expression_location(this, result) }
}

View File

@@ -7,7 +7,11 @@ class StringLiteral extends @string_literal {
/** Get the full string literal with all its parts concatenated */
string toString() {
result = concat(int i | i = [0 .. getNumContinuations()] | getContinuation(i), "\n")
result = this.getValue()
}
string getValue() {
result = concat(int i | i = [0 .. this.getNumContinuations()] | this.getContinuation(i), "\n")
}
SourceLocation getLocation() { string_literal_location(this, result) }

View File

@@ -14,4 +14,5 @@ class TryStmt extends @try_statement, Stmt {
StmtBlock getBody() { try_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
predicate hasFinally() { exists(this.getFinally()) }
}