Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2094aa9

Browse files
authored
Merge pull request #194 from github/hvitved/desugar-child
2 parents 03ef126 + 908e9ff commit 2094aa9

17 files changed

Lines changed: 416 additions & 279 deletions

File tree

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import codeql_ruby.AST
2+
import codeql_ruby.ast.internal.Synthesis
23

34
private string getAPrimaryQlClass(AstNode node) {
45
result = node.getAPrimaryQlClass()
@@ -13,16 +14,18 @@ query predicate missingParent(AstNode node, string cls) {
1314
cls = getAPrimaryQlClass(node)
1415
}
1516

17+
pragma[noinline]
18+
private AstNode parent(AstNode child, int desugarLevel) {
19+
result = child.getParent() and
20+
desugarLevel = desugarLevel(result)
21+
}
22+
1623
query predicate multipleParents(AstNode node, AstNode parent, string cls) {
1724
parent = node.getParent() and
1825
cls = getAPrimaryQlClass(parent) and
19-
exists(AstNode one, AstNode two |
20-
one = node.getParent() and
21-
two = node.getParent() and
26+
exists(AstNode one, AstNode two, int desugarLevel |
27+
one = parent(node, desugarLevel) and
28+
two = parent(node, desugarLevel) and
2229
one != two
23-
|
24-
one.isSynthesized() and two.isSynthesized()
25-
or
26-
not one.isSynthesized() and not two.isSynthesized()
2730
)
2831
}

ql/src/codeql_ruby/AST.qll

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ class AstNode extends TAstNode {
5757
final AstNode getAChild() { result = this.getAChild(_) }
5858

5959
/** Gets the parent of this `AstNode`, if this node is not a root node. */
60-
final AstNode getParent() {
61-
result.getAChild() = this
62-
or
63-
result.getAChild().getDesugared() = this
64-
}
60+
final AstNode getParent() { result.getAChild() = this }
6561

6662
/**
6763
* Gets a child of this node, which can also be retrieved using a predicate
6864
* named `pred`.
6965
*/
7066
cached
71-
AstNode getAChild(string pred) { none() }
67+
AstNode getAChild(string pred) {
68+
pred = "getDesugared" and
69+
result = this.getDesugared()
70+
}
7271

7372
/**
7473
* Holds if this node was synthesized to represent an implicit AST node not

ql/src/codeql_ruby/ast/Call.qll

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ class Call extends Expr, TCall {
4949
*/
5050
final int getNumberOfArguments() { result = this.(CallImpl).getNumberOfArgumentsImpl() }
5151

52-
override AstNode getAChild(string pred) { pred = "getArgument" and result = this.getArgument(_) }
52+
override AstNode getAChild(string pred) {
53+
result = super.getAChild(pred)
54+
or
55+
pred = "getArgument" and result = this.getArgument(_)
56+
}
5357
}
5458

5559
/**
@@ -182,7 +186,11 @@ class BlockArgument extends Expr, TBlockArgument {
182186

183187
final override string toString() { result = "&..." }
184188

185-
final override AstNode getAChild(string pred) { pred = "getValue" and result = this.getValue() }
189+
final override AstNode getAChild(string pred) {
190+
result = super.getAChild(pred)
191+
or
192+
pred = "getValue" and result = this.getValue()
193+
}
186194
}
187195

188196
/**
@@ -209,7 +217,11 @@ class SplatArgument extends Expr, TSplatArgument {
209217

210218
final override string toString() { result = "*..." }
211219

212-
final override AstNode getAChild(string pred) { pred = "getValue" and result = this.getValue() }
220+
final override AstNode getAChild(string pred) {
221+
result = super.getAChild(pred)
222+
or
223+
pred = "getValue" and result = this.getValue()
224+
}
213225
}
214226

215227
/**
@@ -236,5 +248,9 @@ class HashSplatArgument extends Expr, THashSplatArgument {
236248

237249
final override string toString() { result = "**..." }
238250

239-
final override AstNode getAChild(string pred) { pred = "getValue" and result = this.getValue() }
251+
final override AstNode getAChild(string pred) {
252+
result = super.getAChild(pred)
253+
or
254+
pred = "getValue" and result = this.getValue()
255+
}
240256
}

ql/src/codeql_ruby/ast/Constant.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ class ConstantAccess extends Expr, TConstantAccess {
3838

3939
override string toString() { result = this.getName() }
4040

41-
override AstNode getAChild(string pred) { pred = "getScopeExpr" and result = this.getScopeExpr() }
41+
override AstNode getAChild(string pred) {
42+
result = super.getAChild(pred)
43+
or
44+
pred = "getScopeExpr" and result = this.getScopeExpr()
45+
}
4246
}
4347

4448
private class TokenConstantAccess extends ConstantAccess, TTokenConstantAccess {

ql/src/codeql_ruby/ast/Control.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ConditionalExpr extends ControlExpr, TConditionalExpr {
3535
Stmt getBranch(boolean cond) { none() }
3636

3737
override AstNode getAChild(string pred) {
38+
result = super.getAChild(pred)
39+
or
3840
pred = "getCondition" and result = this.getCondition()
3941
or
4042
pred = "getBranch" and result = this.getBranch(_)
@@ -100,7 +102,7 @@ class IfExpr extends ConditionalExpr, TIfExpr {
100102
}
101103

102104
override AstNode getAChild(string pred) {
103-
result = ConditionalExpr.super.getAChild(pred)
105+
result = super.getAChild(pred)
104106
or
105107
pred = "getThen" and result = this.getThen()
106108
or
@@ -360,6 +362,8 @@ class CaseExpr extends ControlExpr, TCaseExpr {
360362
final override string toString() { result = "case ..." }
361363

362364
override AstNode getAChild(string pred) {
365+
result = super.getAChild(pred)
366+
or
363367
pred = "getValue" and result = this.getValue()
364368
or
365369
pred = "getBranch" and result = this.getBranch(_)
@@ -410,6 +414,8 @@ class WhenExpr extends Expr, TWhenExpr {
410414
final override string toString() { result = "when ..." }
411415

412416
override AstNode getAChild(string pred) {
417+
result = super.getAChild(pred)
418+
or
413419
pred = "getBody" and result = this.getBody()
414420
or
415421
pred = "getPattern" and result = this.getPattern(_)
@@ -424,7 +430,11 @@ class Loop extends ControlExpr, TLoop {
424430
/** Gets the body of this loop. */
425431
Stmt getBody() { none() }
426432

427-
override AstNode getAChild(string pred) { pred = "getBody" and result = this.getBody() }
433+
override AstNode getAChild(string pred) {
434+
result = super.getAChild(pred)
435+
or
436+
pred = "getBody" and result = this.getBody()
437+
}
428438
}
429439

430440
/**

ql/src/codeql_ruby/ast/Expr.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class ArgumentList extends Expr, TArgumentList {
5353
final override string toString() { result = "..., ..." }
5454

5555
final override AstNode getAChild(string pred) {
56+
result = super.getAChild(pred)
57+
or
5658
pred = "getElement" and result = this.getElement(_)
5759
}
5860
}
@@ -76,7 +78,11 @@ class StmtSequence extends Expr, TStmtSequence {
7678
/** Holds if this sequence has no statements. */
7779
final predicate isEmpty() { this.getNumberOfStatements() = 0 }
7880

79-
override AstNode getAChild(string pred) { pred = "getStmt" and result = this.getStmt(_) }
81+
override AstNode getAChild(string pred) {
82+
result = super.getAChild(pred)
83+
or
84+
pred = "getStmt" and result = this.getStmt(_)
85+
}
8086
}
8187

8288
private class StmtSequenceSynth extends StmtSequence, TStmtSequenceSynth {
@@ -265,6 +271,8 @@ class Pair extends Expr, TPair {
265271
final override string toString() { result = "Pair" }
266272

267273
override AstNode getAChild(string pred) {
274+
result = super.getAChild(pred)
275+
or
268276
pred = "getKey" and result = this.getKey()
269277
or
270278
pred = "getValue" and result = this.getValue()
@@ -332,6 +340,8 @@ class RescueClause extends Expr, TRescueClause {
332340
final override string toString() { result = "rescue ..." }
333341

334342
override AstNode getAChild(string pred) {
343+
result = super.getAChild(pred)
344+
or
335345
pred = "getException" and result = this.getException(_)
336346
or
337347
pred = "getVariableExpr" and result = this.getVariableExpr()
@@ -372,6 +382,8 @@ class RescueModifierExpr extends Expr, TRescueModifierExpr {
372382
final override string toString() { result = "... rescue ..." }
373383

374384
override AstNode getAChild(string pred) {
385+
result = super.getAChild(pred)
386+
or
375387
pred = "getBody" and result = this.getBody()
376388
or
377389
pred = "getHandler" and result = this.getHandler()
@@ -430,5 +442,9 @@ class StringConcatenation extends Expr, TStringConcatenation {
430442

431443
final override string toString() { result = "\"...\" \"...\"" }
432444

433-
override AstNode getAChild(string pred) { pred = "getString" and result = this.getString(_) }
445+
override AstNode getAChild(string pred) {
446+
result = super.getAChild(pred)
447+
or
448+
pred = "getString" and result = this.getString(_)
449+
}
434450
}

ql/src/codeql_ruby/ast/Literal.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ class StringlikeLiteral extends Literal, TStringlikeLiteral {
404404
}
405405

406406
final override AstNode getAChild(string pred) {
407+
result = super.getAChild(pred)
408+
or
407409
pred = "getComponent" and result = this.getComponent(_)
408410
}
409411
}
@@ -661,6 +663,8 @@ class ArrayLiteral extends Literal, TArrayLiteral {
661663
final int getNumberOfElements() { result = count(this.getAnElement()) }
662664

663665
final override AstNode getAChild(string pred) {
666+
result = super.getAChild(pred)
667+
or
664668
pred = "getElement" and result = this.getElement(_)
665669
}
666670
}
@@ -733,6 +737,8 @@ class HashLiteral extends Literal, THashLiteral {
733737
final override string toString() { result = "{...}" }
734738

735739
final override AstNode getAChild(string pred) {
740+
result = super.getAChild(pred)
741+
or
736742
pred = "getElement" and result = this.getElement(_)
737743
}
738744
}
@@ -773,6 +779,8 @@ class RangeLiteral extends Literal, TRangeLiteral {
773779
final override string toString() { result = "_ " + g.getOperator() + " _" }
774780

775781
final override AstNode getAChild(string pred) {
782+
result = super.getAChild(pred)
783+
or
776784
pred = "getBegin" and result = this.getBegin()
777785
or
778786
pred = "getEnd" and result = this.getEnd()

ql/src/codeql_ruby/ast/Method.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class Callable extends Expr, Scope, TCallable {
1515
Parameter getParameter(int n) { none() }
1616

1717
override AstNode getAChild(string pred) {
18+
result = super.getAChild(pred)
19+
or
1820
pred = "getParameter" and result = this.getParameter(_)
1921
}
2022
}
@@ -87,7 +89,7 @@ class SingletonMethod extends MethodBase, TSingletonMethod {
8789
final override string toString() { result = this.getName() }
8890

8991
final override AstNode getAChild(string pred) {
90-
result = MethodBase.super.getAChild(pred)
92+
result = super.getAChild(pred)
9193
or
9294
pred = "getObject" and result = this.getObject()
9395
}

ql/src/codeql_ruby/ast/Module.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class Toplevel extends ModuleBase, TToplevel {
109109
final BeginBlock getABeginBlock() { result = getBeginBlock(_) }
110110

111111
final override AstNode getAChild(string pred) {
112-
result = ModuleBase.super.getAChild(pred)
112+
result = super.getAChild(pred)
113113
or
114114
pred = "getBeginBlock" and result = this.getBeginBlock(_)
115115
}
@@ -252,7 +252,7 @@ class ClassDeclaration extends Namespace, TClassDeclaration {
252252
}
253253

254254
final override AstNode getAChild(string pred) {
255-
result = Namespace.super.getAChild(pred)
255+
result = super.getAChild(pred)
256256
or
257257
pred = "getSuperclassExpr" and result = this.getSuperclassExpr()
258258
}
@@ -290,7 +290,7 @@ class SingletonClass extends ModuleBase, TSingletonClass {
290290
final override string toString() { result = "class << ..." }
291291

292292
final override AstNode getAChild(string pred) {
293-
result = ModuleBase.super.getAChild(pred)
293+
result = super.getAChild(pred)
294294
or
295295
pred = "getValue" and result = this.getValue()
296296
}

ql/src/codeql_ruby/ast/Operation.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ class Operation extends Expr, TOperation {
1515
/** Gets an operand of this operation. */
1616
Expr getAnOperand() { none() }
1717

18-
override AstNode getAChild(string pred) { pred = "getAnOperand" and result = this.getAnOperand() }
18+
override AstNode getAChild(string pred) {
19+
result = super.getAChild(pred)
20+
or
21+
pred = "getAnOperand" and result = this.getAnOperand()
22+
}
1923
}
2024

2125
/** A unary operation. */
@@ -32,7 +36,7 @@ class UnaryOperation extends Operation, TUnaryOperation {
3236
final override Expr getAnOperand() { result = this.getOperand() }
3337

3438
final override AstNode getAChild(string pred) {
35-
result = Operation.super.getAChild(pred)
39+
result = super.getAChild(pred)
3640
or
3741
pred = "getOperand" and result = this.getOperand()
3842
}
@@ -109,7 +113,7 @@ class BinaryOperation extends Operation, TBinaryOperation {
109113
final override string toString() { result = "... " + this.getOperator() + " ..." }
110114

111115
override AstNode getAChild(string pred) {
112-
result = Operation.super.getAChild(pred)
116+
result = super.getAChild(pred)
113117
or
114118
pred = "getLeftOperand" and result = this.getLeftOperand()
115119
or
@@ -392,7 +396,7 @@ class RelationalOperation extends ComparisonOperation, TRelationalOperation {
392396
Expr getLesserOperand() { none() }
393397

394398
final override AstNode getAChild(string pred) {
395-
result = ComparisonOperation.super.getAChild(pred)
399+
result = super.getAChild(pred)
396400
or
397401
pred = "getGreaterOperand" and result = this.getGreaterOperand()
398402
or
@@ -505,7 +509,7 @@ class Assignment extends Operation, TAssignment {
505509
final override string toString() { result = "... " + this.getOperator() + " ..." }
506510

507511
override AstNode getAChild(string pred) {
508-
result = Operation.super.getAChild(pred)
512+
result = super.getAChild(pred)
509513
or
510514
pred = "getLeftOperand" and result = getLeftOperand()
511515
or

0 commit comments

Comments
 (0)