Skip to content

Commit e3799ad

Browse files
authored
Merge pull request github#12612 from hvitved/ruby/print-ast-desugar-reorder
Ruby: Order synthetic children in PrintAST based on their index instead of location
2 parents 58c7148 + f8c28be commit e3799ad

File tree

7 files changed

+197
-188
lines changed

7 files changed

+197
-188
lines changed

ruby/ql/lib/codeql/ruby/ast/internal/Call.qll

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ class MethodCallSynth extends MethodCallImpl, TMethodCallSynth {
4343

4444
final override AstNode getReceiverImpl() { synthChild(this, 0, result) }
4545

46-
final override AstNode getArgumentImpl(int n) { synthChild(this, n + 1, result) and n >= 0 }
46+
final override AstNode getArgumentImpl(int n) {
47+
synthChild(this, n + 1, result) and
48+
n in [0 .. this.getNumberOfArgumentsImpl() - 1]
49+
}
4750

4851
final override int getNumberOfArgumentsImpl() { this = TMethodCallSynth(_, _, _, _, result) }
4952

50-
final override Block getBlockImpl() { synthChild(this, -2, result) }
53+
final override Block getBlockImpl() {
54+
synthChild(this, this.getNumberOfArgumentsImpl() + 1, result)
55+
}
5156
}
5257

5358
class IdentifierMethodCall extends MethodCallImpl, TIdentifierMethodCall {

ruby/ql/lib/codeql/ruby/ast/internal/Synthesis.qll

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ private module AssignOperationDesugar {
818818
i in [0 .. sao.getNumberOfArguments()]
819819
or
820820
parent = setter and
821-
i = opAssignIndex + 1 and
821+
i = opAssignIndex and
822822
child =
823823
SynthChild(LocalVariableAccessSynthKind(TLocalVariableSynth(sao, opAssignIndex)))
824824
)
@@ -975,7 +975,7 @@ private module DestructuredAssignDesugar {
975975

976976
pragma[nomagic]
977977
private predicate destructuredAssignSynthesis(AstNode parent, int i, Child child) {
978-
exists(DestructuredAssignExpr tae |
978+
exists(DestructuredAssignExpr tae, int total | total = tae.getNumberOfElements() |
979979
parent = tae and
980980
i = -1 and
981981
child = SynthChild(StmtSequenceKind())
@@ -998,15 +998,13 @@ private module DestructuredAssignDesugar {
998998
)
999999
or
10001000
parent = seq and
1001-
i = tae.getNumberOfElements() and
1001+
i = total and
10021002
child = SynthChild(AssignExprKind())
10031003
or
1004-
exists(AstNode assign | assign = TAssignExprSynth(seq, tae.getNumberOfElements()) |
1004+
exists(AstNode assign | assign = TAssignExprSynth(seq, total) |
10051005
parent = assign and
10061006
i = 0 and
1007-
child =
1008-
SynthChild(LocalVariableAccessSynthKind(TLocalVariableSynth(tae,
1009-
tae.getNumberOfElements())))
1007+
child = SynthChild(LocalVariableAccessSynthKind(TLocalVariableSynth(tae, total)))
10101008
or
10111009
parent = assign and
10121010
i = 1 and
@@ -1022,12 +1020,10 @@ private module DestructuredAssignDesugar {
10221020
restIndex = tae.getRestIndexOrNumberOfElements()
10231021
|
10241022
parent = seq and
1025-
i = j + 1 + tae.getNumberOfElements() and
1023+
i = j + 1 + total and
10261024
child = SynthChild(AssignExprKind())
10271025
or
1028-
exists(AstNode assign |
1029-
assign = TAssignExprSynth(seq, j + 1 + tae.getNumberOfElements())
1030-
|
1026+
exists(AstNode assign | assign = TAssignExprSynth(seq, j + 1 + total) |
10311027
exists(LhsWithReceiver mc | mc = elem |
10321028
parent = assign and
10331029
i = 0 and
@@ -1063,9 +1059,7 @@ private module DestructuredAssignDesugar {
10631059
or
10641060
parent = TMethodCallSynth(assign, 1, _, _, _) and
10651061
i = 0 and
1066-
child =
1067-
SynthChild(LocalVariableAccessSynthKind(TLocalVariableSynth(tae,
1068-
tae.getNumberOfElements())))
1062+
child = SynthChild(LocalVariableAccessSynthKind(TLocalVariableSynth(tae, total)))
10691063
or
10701064
j < restIndex and
10711065
parent = TMethodCallSynth(assign, 1, _, _, _) and
@@ -1086,14 +1080,14 @@ private module DestructuredAssignDesugar {
10861080
child = SynthChild(IntegerLiteralKind(j))
10871081
or
10881082
i = 1 and
1089-
child = SynthChild(IntegerLiteralKind(restIndex - tae.getNumberOfElements()))
1083+
child = SynthChild(IntegerLiteralKind(restIndex - total))
10901084
)
10911085
)
10921086
or
10931087
j > restIndex and
10941088
parent = TMethodCallSynth(assign, 1, _, _, _) and
10951089
i = 1 and
1096-
child = SynthChild(IntegerLiteralKind(j - tae.getNumberOfElements()))
1090+
child = SynthChild(IntegerLiteralKind(j - total))
10971091
)
10981092
)
10991093
)
@@ -1284,10 +1278,10 @@ private module ForLoopDesugar {
12841278
child = childRef(for.getValue()) // value is the Enumerable
12851279
or
12861280
parent = eachCall and
1287-
i = -2 and
1281+
i = 1 and
12881282
child = SynthChild(BraceBlockKind())
12891283
or
1290-
exists(Block block | block = TBraceBlockSynth(eachCall, -2) |
1284+
exists(Block block | block = TBraceBlockSynth(eachCall, 1) |
12911285
// block params
12921286
parent = block and
12931287
i = 0 and
@@ -1534,14 +1528,13 @@ private module SafeNavigationCallDesugar {
15341528
i = 1
15351529
)
15361530
or
1537-
parent = TMethodCallSynth(ifExpr, 2, _, _, _) and
1538-
(
1531+
exists(int arity | parent = TMethodCallSynth(ifExpr, 2, _, _, arity) |
15391532
i = 0 and
15401533
child = SynthChild(local)
15411534
or
15421535
child = childRef(call.getArgumentImpl(i - 1))
15431536
or
1544-
child = childRef(call.getBlockImpl()) and i = -2
1537+
child = childRef(call.getBlockImpl()) and i = arity + 1
15451538
)
15461539
)
15471540
)

ruby/ql/lib/codeql/ruby/printAst.qll

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ private predicate shouldPrintAstEdge(AstNode parent, string edgeName, AstNode ch
3636
any(PrintAstConfiguration config).shouldPrintAstEdge(parent, edgeName, child)
3737
}
3838

39-
private int nonSynthIndex() { result = min([-1, any(int i | exists(getSynthChild(_, i)))]) - 1 }
40-
4139
newtype TPrintNode =
4240
TPrintRegularAstNode(AstNode n) { shouldPrintNode(n) } or
4341
TPrintRegExpNode(RE::RegExpTerm term) {
@@ -115,10 +113,23 @@ class PrintRegularAstNode extends PrintAstNode, TPrintRegularAstNode {
115113
)
116114
}
117115

116+
private predicate parentIsSynthesized() {
117+
exists(AstNode parent |
118+
shouldPrintAstEdge(parent, _, astNode) and
119+
parent.isSynthesized()
120+
)
121+
}
122+
118123
private int getSynthAstNodeIndex() {
119-
not astNode.isSynthesized() and result = nonSynthIndex()
124+
this.parentIsSynthesized() and
125+
exists(AstNode parent |
126+
shouldPrintAstEdge(parent, _, astNode) and
127+
parent.isSynthesized() and
128+
synthChild(parent, result, astNode)
129+
)
120130
or
121-
astNode = getSynthChild(astNode.getParent(), result)
131+
not this.parentIsSynthesized() and
132+
result = 0
122133
}
123134

124135
override int getOrder() {
@@ -129,8 +140,8 @@ class PrintRegularAstNode extends PrintAstNode, TPrintRegularAstNode {
129140
|
130141
p
131142
order by
132-
f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), l.getStartColumn(),
133-
l.getEndLine(), l.getEndColumn(), p.getSynthAstNodeIndex()
143+
f.getBaseName(), f.getAbsolutePath(), l.getStartLine(), p.getSynthAstNodeIndex(),
144+
l.getStartColumn(), l.getEndLine(), l.getEndColumn()
134145
)
135146
}
136147

0 commit comments

Comments
 (0)