Skip to content

Commit c728c7d

Browse files
committed
PS: Add helper predicates and classes to operations.
1 parent 34781b8 commit c728c7d

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ class BinaryExpr extends @binary_expression, Expr {
55

66
int getKind() { binary_expression(this, result, _, _) }
77

8+
/** Gets an operand of this binary expression. */
9+
Expr getAnOperand() {
10+
result = this.getLeft()
11+
or
12+
result = this.getRight()
13+
}
14+
15+
/** Holds if this binary expression has the operands `e1` and `e2`. */
16+
predicate hasOperands(Expr e1, Expr e2) {
17+
e1 = this.getLeft() and
18+
e2 = this.getRight()
19+
or
20+
e1 = this.getRight() and
21+
e2 = this.getLeft()
22+
}
23+
824
Expr getLeft() { binary_expression(this, _, result, _) }
925

1026
Expr getRight() { binary_expression(this, _, _, result) }

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,50 @@ module ExprNodes {
519519

520520
final StmtCfgNode getBase() { e.hasCfgChild(e.getBase(), this, result) }
521521
}
522+
523+
class UnaryExprChildMapping extends ExprChildMapping, UnaryExpr {
524+
override predicate relevantChild(Ast n) { n = this.getOperand() }
525+
}
526+
527+
class UnaryCfgNode extends ExprCfgNode {
528+
override string getAPrimaryQlClass() { result = "UnaryExprCfgNode" }
529+
530+
override UnaryExprChildMapping e;
531+
532+
override UnaryExpr getExpr() { result = e }
533+
534+
final ExprCfgNode getOperand() { e.hasCfgChild(e.getOperand(), this, result) }
535+
}
536+
537+
class BinaryExprChildMapping extends ExprChildMapping, BinaryExpr {
538+
override predicate relevantChild(Ast n) { n = this.getLeft() or n = this.getRight() }
539+
}
540+
541+
class BinaryCfgNode extends ExprCfgNode {
542+
override string getAPrimaryQlClass() { result = "BinaryExprCfgNode" }
543+
544+
override BinaryExprChildMapping e;
545+
546+
override BinaryExpr getExpr() { result = e }
547+
548+
final ExprCfgNode getLeft() { e.hasCfgChild(e.getLeft(), this, result) }
549+
550+
final ExprCfgNode getRight() { e.hasCfgChild(e.getRight(), this, result) }
551+
}
552+
553+
class OperationChildMapping extends ExprChildMapping instanceof Operation {
554+
override predicate relevantChild(Ast n) { n = super.getAnOperand() }
555+
}
556+
557+
class OperationCfgNode extends ExprCfgNode {
558+
override string getAPrimaryQlClass() { result = "OperationCfgNode" }
559+
560+
override OperationChildMapping e;
561+
562+
override Operation getExpr() { result = e }
563+
564+
final ExprCfgNode getAnOperand() { e.hasCfgChild(this.getExpr().getAnOperand(), this, result) }
565+
}
522566
}
523567

524568
module StmtNodes {

powershell/ql/lib/semmle/code/powershell/internal/Internal.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module Private {
22
import Parameter::Private
33
import ExplicitWrite::Private
44
import Argument::Private
5+
import Operation::Private
56
}
67

78
module Public {
89
import Parameter::Public
910
import ExplicitWrite::Public
1011
import Argument::Public
12+
import Operation::Public
1113
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import powershell
2+
3+
module Private {
4+
abstract private class AbstractOperation extends Expr {
5+
abstract Expr getAnOperand();
6+
7+
abstract int getKind();
8+
}
9+
10+
class BinaryOperation extends BinaryExpr, AbstractOperation {
11+
final override Expr getAnOperand() { result = BinaryExpr.super.getAnOperand() }
12+
13+
final override int getKind() { result = BinaryExpr.super.getKind() }
14+
}
15+
16+
class UnaryOperation extends UnaryExpr, AbstractOperation {
17+
final override Expr getAnOperand() { result = UnaryExpr.super.getOperand() }
18+
19+
final override int getKind() { result = UnaryExpr.super.getKind() }
20+
}
21+
22+
final class Operation = AbstractOperation;
23+
}
24+
25+
module Public {
26+
class Operation = Private::Operation;
27+
28+
class BinaryOperation = Private::BinaryOperation;
29+
30+
class UnaryOperation = Private::UnaryOperation;
31+
}

0 commit comments

Comments
 (0)