Skip to content

Commit 3bb6021

Browse files
committed
PS: Remove the CmdExpr AST elements and synthesize StmtExpr instead where needed.
1 parent 8eb5e65 commit 3bb6021

File tree

1 file changed

+69
-0
lines changed
  • powershell/ql/lib/semmle/code/powershell/ast/internal

1 file changed

+69
-0
lines changed

powershell/ql/lib/semmle/code/powershell/ast/internal/Synthesis.qll

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,72 @@ private module TypeSynth {
459459
}
460460
}
461461
}
462+
463+
/**
464+
* Remove the implicit expr-to-pipeline conversion.
465+
*/
466+
private module CmdExprRemoval {
467+
private class CmdExprRemoval extends Synthesis {
468+
final override predicate isRelevant(Raw::Ast a) { a instanceof Raw::CmdExpr }
469+
470+
override predicate child(Raw::Ast parent, ChildIndex i, Child child) {
471+
// Remove the CmdExpr. There are two cases:
472+
// - If the expression under the cmd expr exists in a place an expr is expected, then we're done
473+
// - Otherwise, we need to synthesize an expr-to-stmt conversion with the expression as a child
474+
exists(Raw::CmdExpr e, boolean exprCtx | this.parentHasCmdExpr(parent, i, e, exprCtx) |
475+
if exprCtx = true
476+
then child = childRef(getResultAst(e.getExpr()))
477+
else child = SynthChild(ExprStmtKind())
478+
)
479+
or
480+
// Synthesize the redirections from the redirections on the CmdExpr
481+
exists(int index, Raw::CmdExpr e |
482+
parent = e.getExpr() and
483+
i = exprRedirection(index) and
484+
child = childRef(getResultAst(e.getRedirection(index)))
485+
)
486+
}
487+
488+
final override predicate exprStmtExpr(ExprStmt e, Expr expr) {
489+
exists(Raw::Ast parent, ChildIndex i, Raw::CmdExpr cmd, Raw::Expr e0 |
490+
e = TExprStmtSynth(parent, i) and
491+
this.parentHasCmdExpr(parent, i, cmd, _) and
492+
e0 = cmd.getExpr() and
493+
expr = getResultAst(e0)
494+
)
495+
}
496+
497+
final override Ast getResultAstImpl(Raw::Ast r) {
498+
exists(
499+
Raw::CmdExpr cmdExpr, Raw::Expr e, Raw::ChildIndex rawIndex, Raw::Ast cmdParent,
500+
ChildIndex i
501+
|
502+
r = cmdExpr and
503+
cmdExpr.getExpr() = e and
504+
cmdParent.getChild(rawIndex) = cmdExpr and
505+
not mustHaveExprChild(cmdParent, cmdExpr) and
506+
rawIndex = toRawChildIndex(i) and
507+
result = TExprStmtSynth(cmdParent, i)
508+
)
509+
}
510+
511+
pragma[nomagic]
512+
private predicate parentHasCmdExpr(
513+
Raw::Ast parent, ChildIndex i, Raw::CmdExpr cmdExpr, boolean exprCtx
514+
) {
515+
exists(Raw::ChildIndex rawIndex |
516+
rawIndex = toRawChildIndex(i) and
517+
parent.getChild(rawIndex) = cmdExpr and
518+
if mustHaveExprChild(parent, cmdExpr) then exprCtx = true else exprCtx = false
519+
)
520+
}
521+
522+
final override Location getLocation(Ast n) {
523+
exists(Raw::Ast parent, ChildIndex i, Raw::CmdExpr cmdStmt |
524+
n = TExprStmtSynth(parent, i) and
525+
this.parentHasCmdExpr(parent, i, cmdStmt, false) and
526+
result = cmdStmt.getLocation()
527+
)
528+
}
529+
}
530+
}

0 commit comments

Comments
 (0)