Skip to content

Commit dd2c5ef

Browse files
authored
Merge pull request #101 from microsoft/add-inline-expectations-test-for-dataflow
PS: Add `InineExpectationsTest` library for dataflow tests
2 parents be8a763 + 6beb8ee commit dd2c5ef

File tree

18 files changed

+143
-14
lines changed

18 files changed

+143
-14
lines changed

powershell/ql/lib/semmle/code/powershell/AssignmentStatement.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 AssignStmt extends @assignment_statement, Stmt {
3+
class AssignStmt extends @assignment_statement, PipelineBase {
44
override SourceLocation getLocation() { assignment_statement_location(this, result) }
55

66
int getKind() { assignment_statement(this, result, _, _) }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ abstract private class AbstractCall extends Ast {
77

88
Expr getNamedArgument(string name) { none() }
99

10+
Expr getAnArgument() { result = this.getArgument(_) or result = this.getNamedArgument(_) }
11+
1012
Expr getQualifier() { none() }
1113
}
1214

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

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

33
class Cmd extends @command, CmdBase {
4-
override string toString() { result = this.getName() }
4+
override string toString() { result = this.getCommandName() }
55

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

8-
string getName() { command(this, result, _, _, _) }
8+
string getCommandName() { command(this, result, _, _, _) }
99

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

@@ -19,6 +19,8 @@ class Cmd extends @command, CmdBase {
1919

2020
StringConstExpr getCmdName() { result = this.getElement(0) }
2121

22+
Expr getAnArgument() { result = this.getArgument(_) or result = this.getNamedArgument(_) }
23+
2224
Expr getArgument(int i) {
2325
result =
2426
rank[i + 1](CmdElement e, int j |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import powershell
22

3-
class CmdBase extends @command_base, Stmt { }
3+
class CmdBase extends @command_base, Chainable { }
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import powershell
22

33
class Comment extends @comment_entity {
4-
SourceLocation getLocation() { comment_entity_location(this, result) }
4+
Location getLocation() { comment_entity_location(this, result) }
55

66
StringLiteral getCommentContents() { comment_entity(this, result) }
77

8-
string toString() { result = "Comment at: " + this.getLocation().toString() }
8+
string toString() { result = this.getCommentContents().toString() }
9+
}
10+
11+
class SingleLineComment extends Comment {
12+
SingleLineComment() { this.getCommentContents().getNumContinuations() = 1 }
13+
}
14+
15+
class MultiLineComment extends Comment {
16+
MultiLineComment() { this.getCommentContents().getNumContinuations() > 1 }
917
}

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

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

33
class StringLiteral extends @string_literal {
4-
int getNumContinuations() { string_literal_line(this, result, _) }
4+
int getNumContinuations() { result = strictcount(int i | exists(this.getContinuation(i))) }
55

66
string getContinuation(int index) { string_literal_line(this, index, result) }
77

88
/** Get the full string literal with all its parts concatenated */
9-
string toString() {
10-
result = this.getValue()
11-
}
9+
string toString() { result = this.getValue() }
1210

1311
string getValue() {
1412
result = concat(int i | i = [0 .. this.getNumContinuations()] | this.getContinuation(i), "\n")

powershell/ql/lib/semmle/code/powershell/controlflow/internal/Completion.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ private newtype TCompletion =
2323

2424
private predicate commandThrows(Cmd c, boolean unconditional) {
2525
c.getNamedArgument("ErrorAction").(StringConstExpr).getValue().getValue() = "Stop" and
26-
if c.getName() = "Write-Error" then unconditional = true else unconditional = false
26+
if c.getCommandName() = "Write-Error" then unconditional = true else unconditional = false
2727
}
2828

2929
pragma[noinline]

powershell/ql/lib/semmle/code/powershell/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ module Trees {
184184
)
185185
}
186186

187-
int getNumberOfDefaultValues() { result = count(int i | exists(this.getDefaultValue(i))) }
187+
int getNumberOfDefaultValues() { result = count(int i | exists(this.getDefaultValue(i))) }
188188

189189
override predicate succ(AstNode pred, AstNode succ, Completion c) {
190190
// Step to the first parameter

powershell/ql/lib/semmle/code/powershell/dataflow/internal/DataFlowPrivate.qll

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ private class ExprNodeImpl extends ExprNode, NodeImpl {
4141
override string toStringImpl() { result = this.getExprNode().toString() }
4242
}
4343

44+
private class StmtNodeImpl extends StmtNode, NodeImpl {
45+
override CfgScope getCfgScope() { none() /* TODO */ }
46+
47+
override Location getLocationImpl() { result = this.getStmtNode().getLocation() }
48+
49+
override string toStringImpl() { result = this.getStmtNode().toString() }
50+
}
51+
4452
/** Gets the SSA definition node corresponding to parameter `p`. */
4553
pragma[nomagic]
4654
SsaImpl::DefinitionExt getParameterDef(Parameter p) {
@@ -61,7 +69,7 @@ module SsaFlow {
6169
Impl::Node asNode(Node n) {
6270
n = TSsaNode(result)
6371
or
64-
result.(Impl::ExprNode).getExpr() = n.asExpr()
72+
result.(Impl::ExprNode).getExpr() = n.asExpr() // TODO: Statement nodes?
6573
or
6674
result.(Impl::ExprPostUpdateNode).getExpr() = n.(PostUpdateNode).getPreUpdateNode().asExpr()
6775
or
@@ -98,6 +106,7 @@ private module Cached {
98106
cached
99107
newtype TNode =
100108
TExprNode(CfgNodes::ExprCfgNode n) or
109+
TStmtNode(CfgNodes::StmtCfgNode n) or
101110
TSsaNode(SsaImpl::DataFlowIntegration::SsaNode node) or
102111
TNormalParameterNode(Parameter p) or
103112
TExprPostUpdateNode(CfgNodes::ExprCfgNode n) {

powershell/ql/lib/semmle/code/powershell/dataflow/internal/DataFlowPublic.qll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class Node extends TNode {
1111
/** Gets the expression corresponding to this node, if any. */
1212
CfgNodes::ExprCfgNode asExpr() { result = this.(ExprNode).getExprNode() }
1313

14+
CfgNodes::StmtCfgNode asStmt() { result = this.(StmtNode).getStmtNode() }
15+
1416
/** Gets the parameter corresponding to this node, if any. */
1517
Parameter asParameter() { result = this.(ParameterNode).getParameter() }
1618

@@ -47,6 +49,22 @@ class ExprNode extends Node, TExprNode {
4749
CfgNodes::ExprCfgNode getExprNode() { result = n }
4850
}
4951

52+
/**
53+
* A statement, viewed as a node in a data flow graph.
54+
*
55+
* Note that because of control-flow splitting, one `Stmt` may correspond
56+
* to multiple `StmtNode`s, just like it may correspond to multiple
57+
* `ControlFlow::Node`s.
58+
*/
59+
class StmtNode extends Node, TStmtNode {
60+
private CfgNodes::StmtCfgNode n;
61+
62+
StmtNode() { this = TStmtNode(n) }
63+
64+
/** Gets the expression corresponding to this node. */
65+
CfgNodes::StmtCfgNode getStmtNode() { result = n }
66+
}
67+
5068
/**
5169
* The value of a parameter at function entry, viewed as a node in a data
5270
* flow graph.
@@ -103,6 +121,9 @@ private import Cached
103121
/** Gets a node corresponding to expression `e`. */
104122
ExprNode exprNode(CfgNodes::ExprCfgNode e) { result.getExprNode() = e }
105123

124+
/** Gets a node corresponding to statement `s`. */
125+
StmtNode stmtNode(CfgNodes::StmtCfgNode e) { result.getStmtNode() = e }
126+
106127
/**
107128
* Gets the node corresponding to the value of parameter `p` at function entry.
108129
*/

0 commit comments

Comments
 (0)