Skip to content

Commit 20e76b3

Browse files
authored
Merge pull request #85 from microsoft/powershell-cfg-for-function-bodies-and-loops
PS: Control-flow for function bodies and loops
2 parents 435ee53 + 39cdf0d commit 20e76b3

27 files changed

+1557
-111
lines changed

powershell/ql/lib/powershell.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import semmle.code.powershell.ScriptBlock
2222
import semmle.code.powershell.StringLiteral
2323
import semmle.code.powershell.AssignmentStatement
2424
import semmle.code.powershell.BinaryExpression
25+
import semmle.code.powershell.UnaryExpression
2526
import semmle.code.powershell.ScriptBlockExpr
2627
import semmle.code.powershell.TernaryExpression
2728
import semmle.code.powershell.UsingExpression
@@ -47,7 +48,7 @@ import semmle.code.powershell.UsingStmt
4748
import semmle.code.powershell.Type
4849
import semmle.code.powershell.Member
4950
import semmle.code.powershell.PropertyMember
50-
import semmle.code.powershell.FunctionMember
51+
import semmle.code.powershell.Function
5152
import semmle.code.powershell.TryStmt
5253
import semmle.code.powershell.IfStmt
5354
import semmle.code.powershell.ExitStmt
@@ -65,6 +66,5 @@ import semmle.code.powershell.ParenExpression
6566
import semmle.code.powershell.Chainable
6667
import semmle.code.powershell.Pipeline
6768
import semmle.code.powershell.StringConstantExpression
68-
import semmle.code.powershell.FunctionDefinition
6969
import semmle.code.powershell.InvokeMemberExpression
7070
import semmle.code.powershell.CommentEntity

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class ArrayLiteral extends @array_literal, Expr {
77

88
Expr getAnElement() { array_literal_element(this, _, result) }
99

10-
override string toString() { result = "ArrayLiteral at: " + this.getLocation().toString() }
10+
override string toString() { result = "...,..." }
1111
}
Lines changed: 245 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,255 @@
11
import powershell
22

33
class BinaryExpr extends @binary_expression, Expr {
4-
override string toString() {
5-
result = "...+..." // TODO
6-
}
7-
84
override SourceLocation getLocation() { binary_expression_location(this, result) }
95

10-
private int getKind() { binary_expression(this, result, _, _) }
6+
int getKind() { binary_expression(this, result, _, _) }
117

128
Expr getLeft() { binary_expression(this, _, result, _) }
139

1410
Expr getRight() { binary_expression(this, _, _, result) }
1511
}
12+
13+
abstract private class AbstractArithmeticExpr extends BinaryExpr { }
14+
15+
final class ArithmeticExpr = AbstractArithmeticExpr;
16+
17+
class AddExpr extends AbstractArithmeticExpr {
18+
AddExpr() { this.getKind() = 40 }
19+
20+
final override string toString() { result = "...+..." }
21+
}
22+
23+
class SubExpr extends AbstractArithmeticExpr {
24+
SubExpr() { this.getKind() = 41 }
25+
26+
final override string toString() { result = "...-..." }
27+
}
28+
29+
class MulExpr extends AbstractArithmeticExpr {
30+
MulExpr() { this.getKind() = 37 }
31+
32+
final override string toString() { result = "...*..." }
33+
}
34+
35+
class DivExpr extends AbstractArithmeticExpr {
36+
DivExpr() { this.getKind() = 38 }
37+
38+
final override string toString() { result = ".../..." }
39+
}
40+
41+
class RemExpr extends AbstractArithmeticExpr {
42+
RemExpr() { this.getKind() = 39 }
43+
44+
final override string toString() { result = "...%..." }
45+
}
46+
47+
abstract private class AbstractBitwiseExpr extends BinaryExpr { }
48+
49+
final class BitwiseExpr = AbstractBitwiseExpr;
50+
51+
class BitwiseAndExpr extends AbstractBitwiseExpr {
52+
BitwiseAndExpr() { this.getKind() = 56 }
53+
54+
final override string toString() { result = "...&..." }
55+
}
56+
57+
class BitwiseOrExpr extends AbstractBitwiseExpr {
58+
BitwiseOrExpr() { this.getKind() = 57 }
59+
60+
final override string toString() { result = "...|..." }
61+
}
62+
63+
class BitwiseXorExpr extends AbstractBitwiseExpr {
64+
BitwiseXorExpr() { this.getKind() = 58 }
65+
66+
final override string toString() { result = "...^..." }
67+
}
68+
69+
class ShiftLeftExpr extends AbstractBitwiseExpr {
70+
ShiftLeftExpr() { this.getKind() = 97 }
71+
72+
final override string toString() { result = "...<<..." }
73+
}
74+
75+
class ShiftRightExpr extends AbstractBitwiseExpr {
76+
ShiftRightExpr() { this.getKind() = 98 }
77+
78+
final override string toString() { result = "...>>..." }
79+
}
80+
81+
abstract private class AbstractComparisonExpr extends BinaryExpr { }
82+
83+
final class ComparisonExpr = AbstractComparisonExpr;
84+
85+
abstract private class AbstractCaseInsensitiveComparisonExpr extends AbstractComparisonExpr { }
86+
87+
final class CaseInsensitiveComparisonExpr = AbstractCaseInsensitiveComparisonExpr;
88+
89+
class EqExpr extends AbstractCaseInsensitiveComparisonExpr {
90+
EqExpr() { this.getKind() = 60 }
91+
92+
final override string toString() { result = "... -eq ..." }
93+
}
94+
95+
class NeExpr extends AbstractCaseInsensitiveComparisonExpr {
96+
NeExpr() { this.getKind() = 61 }
97+
98+
final override string toString() { result = "... -ne ..." }
99+
}
100+
101+
abstract private class AbstractRelationalExpr extends AbstractComparisonExpr { }
102+
103+
final class RelationalExpr = AbstractRelationalExpr;
104+
105+
abstract private class AbstractCaseInsensitiveRelationalExpr extends AbstractRelationalExpr { }
106+
107+
class GeExpr extends AbstractCaseInsensitiveRelationalExpr {
108+
GeExpr() { this.getKind() = 62 }
109+
110+
final override string toString() { result = "... -ge ..." }
111+
}
112+
113+
class GtExpr extends AbstractCaseInsensitiveRelationalExpr {
114+
GtExpr() { this.getKind() = 63 }
115+
116+
final override string toString() { result = "... -gt ..." }
117+
}
118+
119+
class LtExpr extends AbstractCaseInsensitiveRelationalExpr {
120+
LtExpr() { this.getKind() = 64 }
121+
122+
final override string toString() { result = "... -lt ..." }
123+
}
124+
125+
class LeExpr extends AbstractCaseInsensitiveRelationalExpr {
126+
LeExpr() { this.getKind() = 65 }
127+
128+
final override string toString() { result = "... -le ..." }
129+
}
130+
131+
class LikeExpr extends AbstractCaseInsensitiveComparisonExpr {
132+
LikeExpr() { this.getKind() = 66 }
133+
134+
final override string toString() { result = "... -like ..." }
135+
}
136+
137+
class NotLikeExpr extends AbstractCaseInsensitiveComparisonExpr {
138+
NotLikeExpr() { this.getKind() = 67 }
139+
140+
final override string toString() { result = "... -notlike ..." }
141+
}
142+
143+
class MatchExpr extends AbstractCaseInsensitiveComparisonExpr {
144+
MatchExpr() { this.getKind() = 68 }
145+
146+
final override string toString() { result = "... -match ..." }
147+
}
148+
149+
class NotMatchExpr extends AbstractCaseInsensitiveComparisonExpr {
150+
NotMatchExpr() { this.getKind() = 69 }
151+
152+
final override string toString() { result = "... -notmatch ..." }
153+
}
154+
155+
class ReplaceExpr extends AbstractCaseInsensitiveComparisonExpr {
156+
ReplaceExpr() { this.getKind() = 70 }
157+
158+
final override string toString() { result = "... -replace ..." }
159+
}
160+
161+
abstract class AbstractTypeExpr extends BinaryExpr { }
162+
163+
final class TypeExpr = AbstractTypeExpr;
164+
165+
abstract class AbstractTypeComparisonExpr extends AbstractTypeExpr { }
166+
167+
final class TypeComparisonExpr = AbstractTypeComparisonExpr;
168+
169+
class IsExpr extends AbstractTypeComparisonExpr {
170+
IsExpr() { this.getKind() = 92 }
171+
172+
final override string toString() { result = "... -is ..." }
173+
}
174+
175+
class IsNotExpr extends AbstractTypeComparisonExpr {
176+
IsNotExpr() { this.getKind() = 93 }
177+
178+
final override string toString() { result = "... -isnot ..." }
179+
}
180+
181+
class AsExpr extends AbstractTypeExpr {
182+
AsExpr() { this.getKind() = 94 }
183+
184+
final override string toString() { result = "... -as ..." }
185+
}
186+
187+
abstract private class AbstractContainmentExpr extends BinaryExpr { }
188+
189+
final class ContainmentExpr = AbstractContainmentExpr;
190+
191+
abstract private class AbstractCaseInsensitiveContainmentExpr extends AbstractContainmentExpr { }
192+
193+
final class CaseInsensitiveContainmentExpr = AbstractCaseInsensitiveContainmentExpr;
194+
195+
class ContainsExpr extends AbstractCaseInsensitiveContainmentExpr {
196+
ContainsExpr() { this.getKind() = 71 }
197+
198+
final override string toString() { result = "... -contains ..." }
199+
}
200+
201+
class NotContainsExpr extends AbstractCaseInsensitiveContainmentExpr {
202+
NotContainsExpr() { this.getKind() = 72 }
203+
204+
final override string toString() { result = "... -notcontains ..." }
205+
}
206+
207+
class InExpr extends AbstractCaseInsensitiveContainmentExpr {
208+
InExpr() { this.getKind() = 73 }
209+
210+
final override string toString() { result = "... -in ..." }
211+
}
212+
213+
class NotInExpr extends AbstractCaseInsensitiveContainmentExpr {
214+
NotInExpr() { this.getKind() = 74 }
215+
216+
final override string toString() { result = "... -notin ..." }
217+
}
218+
219+
abstract private class AbstractLogicalBinaryExpr extends BinaryExpr { }
220+
221+
final class LogicalBinaryExpr = AbstractLogicalBinaryExpr;
222+
223+
class LogicalAndExpr extends AbstractLogicalBinaryExpr {
224+
LogicalAndExpr() { this.getKind() = 53 }
225+
226+
final override string toString() { result = "... -and ..." }
227+
}
228+
229+
class LogicalOrExpr extends AbstractLogicalBinaryExpr {
230+
LogicalOrExpr() { this.getKind() = 54 }
231+
232+
final override string toString() { result = "... -or ..." }
233+
}
234+
235+
class LogicalXorExpr extends AbstractLogicalBinaryExpr {
236+
LogicalXorExpr() { this.getKind() = 55 }
237+
238+
final override string toString() { result = "... -xor ..." }
239+
}
240+
241+
abstract private class AbstractStringExpr extends BinaryExpr { }
242+
243+
final class StringExpr = AbstractStringExpr;
244+
245+
class JoinExpr extends AbstractStringExpr {
246+
JoinExpr() { this.getKind() = 59 }
247+
248+
final override string toString() { result = "... -join ..." }
249+
}
250+
251+
class SplitExpr extends AbstractStringExpr {
252+
SplitExpr() { this.getKind() = 75 }
253+
254+
final override string toString() { result = "... -split ..." }
255+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import powershell
22

3-
class BreakStmt extends GotoStmt, Stmt {
3+
class BreakStmt extends GotoStmt, @break_statement {
44
override SourceLocation getLocation() { break_statement_location(this, result) }
55

6-
override string toString() { result = "continue" }
6+
override string toString() { result = "break" }
77
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import powershell
22

3-
class ContinueStmt extends GotoStmt, Stmt {
3+
class ContinueStmt extends GotoStmt, @continue_statement {
44
override SourceLocation getLocation() { continue_statement_location(this, result) }
55

66
override string toString() { result = "continue" }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class DoUntilStmt extends @do_until_statement, LoopStmt {
77

88
PipelineBase getCondition() { do_until_statement_condition(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme
99

10-
StmtBlock getBody() { do_until_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
10+
final override StmtBlock getBody() { do_until_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
1111
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class DoWhileStmt extends @do_while_statement, LoopStmt {
77

88
PipelineBase getCondition() { do_while_statement_condition(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme
99

10-
StmtBlock getBody() { do_while_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
10+
final override StmtBlock getBody() { do_while_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
1111
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ class ForEachStmt extends @foreach_statement, LoopStmt {
55

66
override string toString() { result = "forach(... in ...)" }
77

8-
StmtBlock getBody() { foreach_statement(this, _, _, result, _) } // TODO: Change @ast to @stmt_block in dbscheme
8+
final override StmtBlock getBody() { foreach_statement(this, _, _, result, _) } // TODO: Change @ast to @stmt_block in dbscheme
99

1010
VarAccess getVariable() { foreach_statement(this, result, _, _, _) } // TODO: Change @ast to @variable_expression in dbscheme
1111

12-
/** ..., if any. */
13-
PipelineBase getCondition() { foreach_statement(this, _, result, _, _) } // TODO: Change @ast to @pipeline_base in dbscheme
12+
PipelineBase getIterableExpr() { foreach_statement(this, _, result, _, _) } // TODO: Change @ast to @pipeline_base in dbscheme
1413

1514
predicate isParallel() { foreach_statement(this, _, _, _, 1) }
1615
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ class ForStmt extends @for_statement, LoopStmt {
1111

1212
PipelineBase getIterator() { for_statement_iterator(this, result) } // TODO: Change @ast to @pipeline_base in dbscheme
1313

14-
StmtBlock getBody() { for_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
14+
final override StmtBlock getBody() { for_statement(this, result) } // TODO: Change @ast to @stmt_block in dbscheme
1515
}

0 commit comments

Comments
 (0)