Skip to content

Commit 5e4f52a

Browse files
authored
Merge pull request #90 from microsoft/powershell-even-more-ast-classes
PS: Add more AST classes
2 parents 3bb5582 + 830de2c commit 5e4f52a

16 files changed

+285
-18
lines changed

powershell/ql/lib/powershell.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ import semmle.code.powershell.ContinueStmt
4545
import semmle.code.powershell.BreakStmt
4646
import semmle.code.powershell.ReturnStmt
4747
import semmle.code.powershell.UsingStmt
48+
import semmle.code.powershell.ThrowStmt
49+
import semmle.code.powershell.ErrorStmt
4850
import semmle.code.powershell.Type
4951
import semmle.code.powershell.Member
5052
import semmle.code.powershell.PropertyMember
5153
import semmle.code.powershell.Function
5254
import semmle.code.powershell.TryStmt
5355
import semmle.code.powershell.IfStmt
56+
import semmle.code.powershell.SwitchStmt
5457
import semmle.code.powershell.ExitStmt
5558
import semmle.code.powershell.LabeledStmt
5659
import semmle.code.powershell.DynamicStmt
@@ -66,5 +69,11 @@ import semmle.code.powershell.ParenExpression
6669
import semmle.code.powershell.Chainable
6770
import semmle.code.powershell.Pipeline
6871
import semmle.code.powershell.StringConstantExpression
72+
import semmle.code.powershell.MemberExpr
6973
import semmle.code.powershell.InvokeMemberExpression
74+
import semmle.code.powershell.SubExpression
75+
import semmle.code.powershell.ConvertExpr
76+
import semmle.code.powershell.IndexExpr
77+
import semmle.code.powershell.HashTable
78+
import semmle.code.powershell.SplitExpr
7079
import semmle.code.powershell.CommentEntity

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

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ abstract private class AbstractCaseInsensitiveComparisonExpr extends AbstractCom
8686

8787
final class CaseInsensitiveComparisonExpr = AbstractCaseInsensitiveComparisonExpr;
8888

89+
abstract private class AbstractCaseSensitiveComparisonExpr extends AbstractComparisonExpr { }
90+
91+
final class CaseSensitiveComparisonExpr = AbstractCaseSensitiveComparisonExpr;
92+
8993
class EqExpr extends AbstractCaseInsensitiveComparisonExpr {
9094
EqExpr() { this.getKind() = 60 }
9195

@@ -98,12 +102,30 @@ class NeExpr extends AbstractCaseInsensitiveComparisonExpr {
98102
final override string toString() { result = "... -ne ..." }
99103
}
100104

105+
class CEqExpr extends AbstractCaseSensitiveComparisonExpr {
106+
CEqExpr() { this.getKind() = 76 }
107+
108+
final override string toString() { result = "... -ceq ..." }
109+
}
110+
111+
class CNeExpr extends AbstractCaseSensitiveComparisonExpr {
112+
CNeExpr() { this.getKind() = 77 }
113+
114+
final override string toString() { result = "... -cne ..." }
115+
}
116+
101117
abstract private class AbstractRelationalExpr extends AbstractComparisonExpr { }
102118

103119
final class RelationalExpr = AbstractRelationalExpr;
104120

105121
abstract private class AbstractCaseInsensitiveRelationalExpr extends AbstractRelationalExpr { }
106122

123+
final class CaseInsensitiveRelationalExpr = AbstractCaseInsensitiveRelationalExpr;
124+
125+
abstract private class AbstractCaseSensitiveRelationalExpr extends AbstractRelationalExpr { }
126+
127+
final class CaseSensitiveRelationalExpr = AbstractCaseSensitiveRelationalExpr;
128+
107129
class GeExpr extends AbstractCaseInsensitiveRelationalExpr {
108130
GeExpr() { this.getKind() = 62 }
109131

@@ -128,6 +150,30 @@ class LeExpr extends AbstractCaseInsensitiveRelationalExpr {
128150
final override string toString() { result = "... -le ..." }
129151
}
130152

153+
class CGeExpr extends AbstractCaseSensitiveRelationalExpr {
154+
CGeExpr() { this.getKind() = 78 }
155+
156+
final override string toString() { result = "... -cge ..." }
157+
}
158+
159+
class CGtExpr extends AbstractCaseSensitiveRelationalExpr {
160+
CGtExpr() { this.getKind() = 79 }
161+
162+
final override string toString() { result = "... -cgt ..." }
163+
}
164+
165+
class CLtExpr extends AbstractCaseSensitiveRelationalExpr {
166+
CLtExpr() { this.getKind() = 80 }
167+
168+
final override string toString() { result = "... -clt ..." }
169+
}
170+
171+
class CLeExpr extends AbstractCaseSensitiveRelationalExpr {
172+
CLeExpr() { this.getKind() = 81 }
173+
174+
final override string toString() { result = "... -cle ..." }
175+
}
176+
131177
class LikeExpr extends AbstractCaseInsensitiveComparisonExpr {
132178
LikeExpr() { this.getKind() = 66 }
133179

@@ -238,18 +284,20 @@ class LogicalXorExpr extends AbstractLogicalBinaryExpr {
238284
final override string toString() { result = "... -xor ..." }
239285
}
240286

241-
abstract private class AbstractStringExpr extends BinaryExpr { }
242-
243-
final class StringExpr = AbstractStringExpr;
244-
245-
class JoinExpr extends AbstractStringExpr {
287+
class JoinExpr extends BinaryExpr {
246288
JoinExpr() { this.getKind() = 59 }
247289

248290
final override string toString() { result = "... -join ..." }
249291
}
250292

251-
class SplitExpr extends AbstractStringExpr {
252-
SplitExpr() { this.getKind() = 75 }
293+
class SequenceExpr extends BinaryExpr {
294+
SequenceExpr() { this.getKind() = 33 }
295+
296+
final override string toString() { result = "[..]" }
297+
}
298+
299+
class FormatExpr extends BinaryExpr {
300+
FormatExpr() { this.getKind() = 50 }
253301

254-
final override string toString() { result = "... -split ..." }
302+
final override string toString() { result = "... -f ..." }
255303
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import powershell
2+
3+
class ConvertExpr extends @convert_expression, Expr {
4+
override string toString() { result = "[...]..." }
5+
6+
override SourceLocation getLocation() { convert_expression_location(this, result) }
7+
8+
Expr getExpr() { convert_expression(this, _, result, _, _) }
9+
10+
TypeConstraint getType() { convert_expression(this, _, _, result, _) }
11+
12+
AttributeBase getAttribute() { convert_expression(this, result, _, _, _) }
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import powershell
2+
3+
class ErrorStmt extends @error_statement, PipelineBase {
4+
final override SourceLocation getLocation() { error_statement_location(this, result) }
5+
6+
final override string toString() { result = "error" }
7+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import powershell
22

3-
class ExpandableStringExpression extends @expandable_string_expression, Expr {
3+
class ExpandableStringExpr extends @expandable_string_expression, Expr {
44
override SourceLocation getLocation() { expandable_string_expression_location(this, result) }
55

6-
override string toString() {
7-
result = "ExpandableStringExpression at: " + this.getLocation().toString()
8-
}
6+
override string toString() { result = this.getUnexpandedValue().toString() }
7+
8+
StringLiteral getUnexpandedValue() { expandable_string_expression(this, result, _, _) }
99

1010
private int getKind() { expandable_string_expression(this, _, result, _) }
1111

12-
int getNumExprs() { expandable_string_expression(this, _, _, result) }
12+
int getNumExprs() { result = count(this.getAnExpr()) }
1313

1414
Expr getExpr(int i) { expandable_string_expression_nested_expression(this, i, result) }
1515

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import powershell
2+
3+
class HashTableExpr extends @hash_table, Expr {
4+
final override Location getLocation() { hash_table_location(this, result) }
5+
6+
final override string toString() { result = "${...}" }
7+
8+
Stmt getExprWithKey(Expr key) { hash_table_key_value_pairs(this, _, key, result) } // TODO: Change @ast to @expr in db scheme
9+
10+
predicate hasKey(Expr key) { exists(this.getExprWithKey(key)) }
11+
12+
Stmt getAnExpr() { result = this.getExprWithKey(_) }
13+
14+
predicate hasEntry(int index, Expr key, Stmt value) {
15+
hash_table_key_value_pairs(this, index, key, value)
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import powershell
2+
3+
class IndexExpr extends @index_expression, Expr {
4+
override string toString() { result = "...[...]" }
5+
6+
override SourceLocation getLocation() { index_expression_location(this, result) }
7+
8+
Expr getIndex() { index_expression(this, result, _, _) } // TODO: Change @ast to @expr in the dbscheme
9+
10+
Expr getBase() { index_expression(this, _, result, _) } // TODO: Change @ast to @expr in the dbscheme
11+
12+
predicate isNullConditional() { index_expression(this, _, _, true) }
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import powershell
2+
3+
class MemberExpr extends @member_expression, MemberExprBase {
4+
final override Location getLocation() { member_expression_location(this, result) }
5+
6+
Expr getExpr() { member_expression(this, result, _, _, _) }
7+
8+
CmdElement getMember() { member_expression(this, _, result, _, _) }
9+
10+
predicate isNullConditional() { member_expression(this, _, _, true, _) }
11+
12+
predicate isStatic() { member_expression(this, _, _, _, true) }
13+
14+
final override string toString() { result = this.getMember().toString() }
15+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import powershell
22

3-
class ParenExpression extends @paren_expression, Expr {
4-
PipelineBase getExpression() { paren_expression(this, result) }
3+
class ParenExpr extends @paren_expression, Expr {
4+
PipelineBase getExpr() { paren_expression(this, result) }
55

66
override SourceLocation getLocation() { paren_expression_location(this, result) }
77

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import powershell
2+
3+
class PipelineChain extends @pipeline_chain, Chainable {
4+
final override SourceLocation getLocation() { pipeline_chain_location(this, result) }
5+
6+
predicate isBackground() { pipeline_chain(this, true, _, _, _) }
7+
8+
Chainable getLeft() { pipeline_chain(this, _, _, result, _) }
9+
10+
Pipeline getRight() { pipeline_chain(this, _, _, _, result) }
11+
}

0 commit comments

Comments
 (0)