Skip to content

Commit 31f14ba

Browse files
committed
PS: Add synthesis framework for cleaning up the AST.
1 parent 0dd756d commit 31f14ba

File tree

1 file changed

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

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
private import TAst
2+
private import Ast
3+
private import Location
4+
private import Variable
5+
private import TypeConstraint
6+
private import Expr
7+
private import Parameter
8+
private import ExprStmt
9+
private import NamedBlock
10+
private import FunctionBase
11+
private import ScriptBlock
12+
private import Command
13+
private import Internal::Private
14+
private import Type
15+
private import Scopes
16+
private import BoolLiteral
17+
private import Member
18+
private import EnvVariable
19+
private import Raw.Raw as Raw
20+
private import codeql.util.Boolean
21+
private import AutomaticVariable
22+
23+
newtype VarKind =
24+
ThisVarKind() or
25+
ParamVarRealKind() or
26+
ParamVarPipelineKind() or
27+
PipelineIteratorKind() or
28+
PipelineByPropertyNameIteratorKind(string name) {
29+
exists(Raw::ProcessBlock pb |
30+
name = pb.getScriptBlock().getParamBlock().getAPipelineByPropertyNameParameter().getName()
31+
)
32+
}
33+
34+
newtype SynthKind =
35+
ExprStmtKind() or
36+
VarAccessRealKind(VariableReal v) or
37+
VarAccessSynthKind(VariableSynth v) or
38+
FunctionSynthKind() or
39+
TypeSynthKind() or
40+
BoolLiteralKind(Boolean b) or
41+
NullLiteralKind() or
42+
EnvVariableKind(string var) { Raw::isEnvVariableAccess(_, var) } or
43+
AutomaticVariableKind(string var) { Raw::isAutomaticVariableAccess(_, var) } or
44+
VarSynthKind(VarKind k)
45+
46+
newtype Child =
47+
SynthChild(SynthKind kind) or
48+
RealChildRef(TAstReal n) or
49+
SynthChildRef(TAstSynth n)
50+
51+
pragma[inline]
52+
private Child childRef(TAst n) {
53+
result = RealChildRef(n)
54+
or
55+
result = SynthChildRef(n)
56+
}
57+
58+
private newtype TSynthesis = MkSynthesis()
59+
60+
class Synthesis extends TSynthesis {
61+
predicate child(Raw::Ast parent, ChildIndex i, Child child) { none() }
62+
63+
Location getLocation(Ast n) { none() }
64+
65+
predicate isRelevant(Raw::Ast a) { none() }
66+
67+
string toString(Ast n) { none() }
68+
69+
Ast getResultAstImpl(Raw::Ast r) { none() }
70+
71+
predicate explicitAssignment(Raw::Ast dest, string name, Raw::Ast assignment) { none() }
72+
73+
predicate implicitAssignment(Raw::Ast dest, string name) { none() }
74+
75+
predicate variableSynthName(VariableSynth v, string name) { none() }
76+
77+
predicate exprStmtExpr(ExprStmt e, Expr expr) { none() }
78+
79+
predicate parameterStaticType(Parameter p, string type) { none() }
80+
81+
predicate isPipelineParameter(Parameter p) { none() }
82+
83+
predicate pipelineParameterHasIndex(ScriptBlock s, int i) { none() }
84+
85+
predicate functionName(FunctionBase f, string name) { none() }
86+
87+
predicate memberName(Member m, string name) { none() }
88+
89+
predicate typeName(Type t, string name) { none() }
90+
91+
predicate typeMember(Type t, int i, Member m) { none() }
92+
93+
predicate functionScriptBlock(FunctionBase f, ScriptBlock block) { none() }
94+
95+
predicate isNamedArgument(CmdCall call, int i, string name) { none() }
96+
97+
predicate booleanValue(BoolLiteral b, boolean value) { none() }
98+
99+
predicate envVariableName(EnvVariable var, string name) { none() }
100+
101+
predicate automaticVariableName(AutomaticVariable var, string name) { none() }
102+
103+
final string toString() { none() }
104+
}
105+
106+
/** Gets the user-facing AST element that is generated from `r`. */
107+
Ast getResultAst(Raw::Ast r) {
108+
not any(Synthesis s).isRelevant(r) and
109+
toRaw(result) = r
110+
or
111+
any(Synthesis s).isRelevant(r) and
112+
result = any(Synthesis s).getResultAstImpl(r)
113+
}
114+
115+
Raw::Ast getRawAst(Ast r) { r = getResultAst(result) }

0 commit comments

Comments
 (0)