Skip to content

Commit 1007f2a

Browse files
committed
Rename (Hash)SplatArgument to (Hash)SplatExpr and make them UnaryOperations
1 parent 372f864 commit 1007f2a

13 files changed

Lines changed: 89 additions & 114 deletions

File tree

ql/src/codeql_ruby/ast/Call.qll

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -192,65 +192,3 @@ class BlockArgument extends Expr, TBlockArgument {
192192
pred = "getValue" and result = this.getValue()
193193
}
194194
}
195-
196-
/**
197-
* A splat argument in a method call.
198-
* ```rb
199-
* foo(*args)
200-
* ```
201-
*/
202-
class SplatArgument extends Expr, TSplatArgument {
203-
private Generated::SplatArgument g;
204-
205-
SplatArgument() { this = TSplatArgument(g) }
206-
207-
final override string getAPrimaryQlClass() { result = "SplatArgument" }
208-
209-
/**
210-
* Gets the underlying expression. In the following example, the result is
211-
* the `Expr` for `bar`:
212-
* ```rb
213-
* foo(*bar)
214-
* ```
215-
*/
216-
final Expr getValue() { toGenerated(result) = g.getChild() }
217-
218-
final override string toString() { result = "*..." }
219-
220-
final override AstNode getAChild(string pred) {
221-
result = super.getAChild(pred)
222-
or
223-
pred = "getValue" and result = this.getValue()
224-
}
225-
}
226-
227-
/**
228-
* A hash-splat (or 'double-splat') argument in a method call.
229-
* ```rb
230-
* foo(**options)
231-
* ```
232-
*/
233-
class HashSplatArgument extends Expr, THashSplatArgument {
234-
private Generated::HashSplatArgument g;
235-
236-
HashSplatArgument() { this = THashSplatArgument(g) }
237-
238-
final override string getAPrimaryQlClass() { result = "HashSplatArgument" }
239-
240-
/**
241-
* Gets the underlying expression. In the following example, the result is
242-
* the `Expr` for `bar`:
243-
* ```rb
244-
* foo(**bar)
245-
* ```
246-
*/
247-
final Expr getValue() { toGenerated(result) = g.getChild() }
248-
249-
final override string toString() { result = "**..." }
250-
251-
final override AstNode getAChild(string pred) {
252-
result = super.getAChild(pred)
253-
or
254-
pred = "getValue" and result = this.getValue()
255-
}
256-
}

ql/src/codeql_ruby/ast/Literal.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@ class HashLiteral extends Literal, THashLiteral {
717717
* Gets the `n`th element in this array literal.
718718
*
719719
* In the following example, the 0th element is a `Pair`, and the 1st element
720-
* is a `HashSplatArgument`.
720+
* is a `HashSplatExpr`.
721721
*
722722
* ```rb
723723
* { foo: 123, **bar }

ql/src/codeql_ruby/ast/Operation.qll

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ class Operation extends Expr, TOperation {
2424

2525
/** A unary operation. */
2626
class UnaryOperation extends Operation, TUnaryOperation {
27-
private Generated::Unary g;
28-
29-
UnaryOperation() { g = toGenerated(this) }
30-
3127
/** Gets the operand of this unary operation. */
32-
final Expr getOperand() { toGenerated(result) = g.getOperand() }
33-
34-
final override string getOperator() { result = g.getOperator() }
28+
Expr getOperand() { none() }
3529

3630
final override Expr getAnOperand() { result = this.getOperand() }
3731

@@ -44,8 +38,18 @@ class UnaryOperation extends Operation, TUnaryOperation {
4438
final override string toString() { result = this.getOperator() + " ..." }
4539
}
4640

41+
private class UnaryOperationGenerated extends UnaryOperation, TUnaryOperation {
42+
private Generated::Unary g;
43+
44+
UnaryOperationGenerated() { g = toGenerated(this) }
45+
46+
final override Expr getOperand() { toGenerated(result) = g.getOperand() }
47+
48+
final override string getOperator() { result = g.getOperator() }
49+
}
50+
4751
/** A unary logical operation. */
48-
class UnaryLogicalOperation extends UnaryOperation, TUnaryLogicalOperation { }
52+
class UnaryLogicalOperation extends UnaryOperationGenerated, TUnaryLogicalOperation { }
4953

5054
/**
5155
* A logical NOT operation, using either `!` or `not`.
@@ -59,7 +63,7 @@ class NotExpr extends UnaryLogicalOperation, TNotExpr {
5963
}
6064

6165
/** A unary arithmetic operation. */
62-
class UnaryArithmeticOperation extends UnaryOperation, TUnaryArithmeticOperation { }
66+
class UnaryArithmeticOperation extends UnaryOperationGenerated, TUnaryArithmeticOperation { }
6367

6468
/**
6569
* A unary plus expression.
@@ -81,6 +85,42 @@ class UnaryMinusExpr extends UnaryArithmeticOperation, TUnaryMinusExpr {
8185
final override string getAPrimaryQlClass() { result = "UnaryMinusExpr" }
8286
}
8387

88+
/**
89+
* A splat expression.
90+
* ```rb
91+
* foo(*args)
92+
* ```
93+
*/
94+
class SplatExpr extends UnaryOperation, TSplatArgument {
95+
private Generated::SplatArgument g;
96+
97+
SplatExpr() { this = TSplatArgument(g) }
98+
99+
final override Expr getOperand() { toGenerated(result) = g.getChild() }
100+
101+
final override string getOperator() { result = "*" }
102+
103+
final override string getAPrimaryQlClass() { result = "SplatExpr" }
104+
}
105+
106+
/**
107+
* A hash-splat (or 'double-splat') expression.
108+
* ```rb
109+
* foo(**options)
110+
* ```
111+
*/
112+
class HashSplatExpr extends UnaryOperation, THashSplatArgument {
113+
private Generated::HashSplatArgument g;
114+
115+
HashSplatExpr() { this = THashSplatArgument(g) }
116+
117+
final override Expr getOperand() { toGenerated(result) = g.getChild() }
118+
119+
final override string getOperator() { result = "**" }
120+
121+
final override string getAPrimaryQlClass() { result = "HashSplatExpr" }
122+
}
123+
84124
/** A unary bitwise operation. */
85125
class UnaryBitwiseOperation extends UnaryOperation, TUnaryBitwiseOperation { }
86126

ql/src/codeql_ruby/ast/internal/AST.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,8 @@ class TNamespace = TClassDeclaration or TModuleDeclaration;
585585
class TOperation = TUnaryOperation or TBinaryOperation or TAssignment;
586586

587587
class TUnaryOperation =
588-
TUnaryLogicalOperation or TUnaryArithmeticOperation or TUnaryBitwiseOperation or TDefinedExpr;
588+
TUnaryLogicalOperation or TUnaryArithmeticOperation or TUnaryBitwiseOperation or TDefinedExpr or
589+
TSplatArgument or THashSplatArgument;
589590

590591
class TUnaryLogicalOperation = TNotExpr;
591592

ql/src/codeql_ruby/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,6 @@ module Trees {
853853
final override ControlFlowTree getChildNode(int i) { result = this.getElement(i) }
854854
}
855855

856-
private class HashSplatArgumentTree extends StandardPostOrderTree, HashSplatArgument {
857-
final override ControlFlowTree getChildNode(int i) { result = this.getValue() and i = 0 }
858-
}
859-
860856
private class HashSplatParameterTree extends NonDefaultValueParameterTree, HashSplatParameter { }
861857

862858
private class HereDocTree extends StandardPreOrderTree, HereDoc {
@@ -1172,10 +1168,6 @@ module Trees {
11721168
}
11731169
}
11741170

1175-
private class SplatArgumentTree extends StandardPostOrderTree, SplatArgument {
1176-
final override ControlFlowTree getChildNode(int i) { result = this.getValue() and i = 0 }
1177-
}
1178-
11791171
private class SplatParameterTree extends NonDefaultValueParameterTree, SplatParameter { }
11801172

11811173
class StmtSequenceTree extends PostOrderTree, StmtSequence {

ql/test/library-tests/ast/Ast.expected

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -432,23 +432,23 @@ calls/calls.rb:
432432
# 267| getReceiver: [ConstantReadAccess] X
433433
# 270| getStmt: [MethodCall] call to foo
434434
# 270| getReceiver: [Self] self
435-
# 270| getArgument: [SplatArgument] *...
436-
# 270| getValue: [MethodCall] call to bar
435+
# 270| getArgument: [SplatExpr] * ...
436+
# 270| getAnOperand/getOperand: [MethodCall] call to bar
437437
# 270| getReceiver: [Self] self
438438
# 271| getStmt: [MethodCall] call to foo
439439
# 271| getReceiver: [Self] self
440-
# 271| getArgument: [SplatArgument] *...
441-
# 271| getValue: [MethodCall] call to bar
440+
# 271| getArgument: [SplatExpr] * ...
441+
# 271| getAnOperand/getOperand: [MethodCall] call to bar
442442
# 271| getReceiver: [ConstantReadAccess] X
443443
# 274| getStmt: [MethodCall] call to foo
444444
# 274| getReceiver: [Self] self
445-
# 274| getArgument: [HashSplatArgument] **...
446-
# 274| getValue: [MethodCall] call to bar
445+
# 274| getArgument: [HashSplatExpr] ** ...
446+
# 274| getAnOperand/getOperand: [MethodCall] call to bar
447447
# 274| getReceiver: [Self] self
448448
# 275| getStmt: [MethodCall] call to foo
449449
# 275| getReceiver: [Self] self
450-
# 275| getArgument: [HashSplatArgument] **...
451-
# 275| getValue: [MethodCall] call to bar
450+
# 275| getArgument: [HashSplatExpr] ** ...
451+
# 275| getAnOperand/getOperand: [MethodCall] call to bar
452452
# 275| getReceiver: [ConstantReadAccess] X
453453
# 278| getStmt: [MethodCall] call to foo
454454
# 278| getReceiver: [Self] self
@@ -1102,8 +1102,8 @@ literals/literals.rb:
11021102
# 114| getElement: [Pair] Pair
11031103
# 114| getKey: [SymbolLiteral] :foo
11041104
# 114| getValue: [IntegerLiteral] 7
1105-
# 114| getElement: [HashSplatArgument] **...
1106-
# 114| getValue: [MethodCall] call to bar
1105+
# 114| getElement: [HashSplatExpr] ** ...
1106+
# 114| getAnOperand/getOperand: [MethodCall] call to bar
11071107
# 114| getReceiver: [Self] self
11081108
# 117| getStmt: [ParenthesizedExpr] ( ... )
11091109
# 117| getStmt: [RangeLiteral] _ .. _
@@ -1621,14 +1621,14 @@ operations/operations.rb:
16211621
# 29| getStmt: [ReturnStmt] return
16221622
# 29| getValue: [ArgumentList] ..., ...
16231623
# 29| getElement: [IntegerLiteral] 1
1624-
# 29| getElement: [SplatArgument] *...
1625-
# 29| getValue: [ArrayLiteral] [...]
1624+
# 29| getElement: [SplatExpr] * ...
1625+
# 29| getAnOperand/getOperand: [ArrayLiteral] [...]
16261626
# 29| getElement: [IntegerLiteral] 2
16271627
# 29| getElement: [Pair] Pair
16281628
# 29| getKey: [SymbolLiteral] :a
16291629
# 29| getValue: [IntegerLiteral] 3
1630-
# 29| getElement: [HashSplatArgument] **...
1631-
# 29| getValue: [HashLiteral] {...}
1630+
# 29| getElement: [HashSplatExpr] ** ...
1631+
# 29| getAnOperand/getOperand: [HashLiteral] {...}
16321632
# 29| getElement: [Pair] Pair
16331633
# 29| getKey: [SymbolLiteral] :b
16341634
# 29| getValue: [IntegerLiteral] 4

ql/test/library-tests/ast/calls/arguments.expected

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
blockArguments
22
| calls.rb:266:5:266:8 | &... | calls.rb:266:6:266:8 | call to bar |
33
| calls.rb:267:5:267:11 | &... | calls.rb:267:6:267:11 | call to bar |
4-
splatArguments
5-
| calls.rb:270:5:270:8 | *... | calls.rb:270:6:270:8 | call to bar |
6-
| calls.rb:271:5:271:11 | *... | calls.rb:271:6:271:11 | call to bar |
7-
hashSplatArguments
8-
| calls.rb:274:5:274:9 | **... | calls.rb:274:7:274:9 | call to bar |
9-
| calls.rb:275:5:275:12 | **... | calls.rb:275:7:275:12 | call to bar |
4+
splatExpr
5+
| calls.rb:270:5:270:8 | * ... | calls.rb:270:6:270:8 | call to bar |
6+
| calls.rb:271:5:271:11 | * ... | calls.rb:271:6:271:11 | call to bar |
7+
hashSplatExpr
8+
| calls.rb:274:5:274:9 | ** ... | calls.rb:274:7:274:9 | call to bar |
9+
| calls.rb:275:5:275:12 | ** ... | calls.rb:275:7:275:12 | call to bar |
1010
keywordArguments
1111
| calls.rb:278:5:278:13 | Pair | calls.rb:278:5:278:8 | :blah | calls.rb:278:11:278:13 | call to bar |
1212
| calls.rb:279:5:279:16 | Pair | calls.rb:279:5:279:8 | :blah | calls.rb:279:11:279:16 | call to bar |

ql/test/library-tests/ast/calls/arguments.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import ruby
22

33
query predicate blockArguments(BlockArgument a, Expr e) { e = a.getValue() }
44

5-
query predicate splatArguments(SplatArgument a, Expr e) { e = a.getValue() }
5+
query predicate splatExpr(SplatExpr a, Expr e) { e = a.getOperand() }
66

7-
query predicate hashSplatArguments(HashSplatArgument a, Expr e) { e = a.getValue() }
7+
query predicate hashSplatExpr(HashSplatExpr a, Expr e) { e = a.getOperand() }
88

99
query predicate keywordArguments(Pair a, Expr key, Expr value) {
1010
exists(Call c | c.getAnArgument() = a and key = a.getKey() and value = a.getValue())

ql/test/library-tests/ast/calls/calls.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ callsWithArguments
1616
| calls.rb:235:1:235:14 | ...[...] | [] | 0 | calls.rb:235:8:235:13 | call to bar |
1717
| calls.rb:266:1:266:9 | call to foo | foo | 0 | calls.rb:266:5:266:8 | &... |
1818
| calls.rb:267:1:267:12 | call to foo | foo | 0 | calls.rb:267:5:267:11 | &... |
19-
| calls.rb:270:1:270:9 | call to foo | foo | 0 | calls.rb:270:5:270:8 | *... |
20-
| calls.rb:271:1:271:12 | call to foo | foo | 0 | calls.rb:271:5:271:11 | *... |
21-
| calls.rb:274:1:274:10 | call to foo | foo | 0 | calls.rb:274:5:274:9 | **... |
22-
| calls.rb:275:1:275:13 | call to foo | foo | 0 | calls.rb:275:5:275:12 | **... |
19+
| calls.rb:270:1:270:9 | call to foo | foo | 0 | calls.rb:270:5:270:8 | * ... |
20+
| calls.rb:271:1:271:12 | call to foo | foo | 0 | calls.rb:271:5:271:11 | * ... |
21+
| calls.rb:274:1:274:10 | call to foo | foo | 0 | calls.rb:274:5:274:9 | ** ... |
22+
| calls.rb:275:1:275:13 | call to foo | foo | 0 | calls.rb:275:5:275:12 | ** ... |
2323
| calls.rb:278:1:278:14 | call to foo | foo | 0 | calls.rb:278:5:278:13 | Pair |
2424
| calls.rb:279:1:279:17 | call to foo | foo | 0 | calls.rb:279:5:279:16 | Pair |
2525
| calls.rb:288:5:288:16 | call to super | super | 0 | calls.rb:288:11:288:16 | "blah" |

ql/test/library-tests/ast/literals/literals.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ hashLiteralElements
622622
| literals.rb:113:1:113:33 | {...} | 1 | literals.rb:113:11:113:19 | Pair | Pair |
623623
| literals.rb:113:1:113:33 | {...} | 2 | literals.rb:113:22:113:31 | Pair | Pair |
624624
| literals.rb:114:1:114:17 | {...} | 0 | literals.rb:114:3:114:8 | Pair | Pair |
625-
| literals.rb:114:1:114:17 | {...} | 1 | literals.rb:114:11:114:15 | **... | HashSplatArgument |
625+
| literals.rb:114:1:114:17 | {...} | 1 | literals.rb:114:11:114:15 | ** ... | HashSplatExpr |
626626
hashLiteralKeyValuePairs
627627
| literals.rb:84:1:84:14 | {...} | literals.rb:84:3:84:12 | Pair | literals.rb:84:3:84:5 | :foo | literals.rb:84:8:84:12 | "bar" |
628628
| literals.rb:113:1:113:33 | {...} | literals.rb:113:3:113:8 | Pair | literals.rb:113:3:113:5 | :foo | literals.rb:113:8:113:8 | 1 |

0 commit comments

Comments
 (0)