Skip to content

Commit 0bb5007

Browse files
committed
Reintroduce hidden then/else/do in AST; include all in CFG
1 parent 58ecd77 commit 0bb5007

9 files changed

Lines changed: 497 additions & 271 deletions

File tree

ql/src/codeql_ruby/ast/Expr.qll

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ class ArgumentList extends Expr, TArgumentList {
5757
class StmtSequence extends Expr, TStmtSequence {
5858
override string getAPrimaryQlClass() { result = "StmtSequence" }
5959

60-
override string toString() {
61-
exists(int c | c = this.getNumberOfStatements() |
62-
c = 0 and result = ";"
63-
or
64-
c = 1 and result = this.getStmt(0).toString()
65-
or
66-
c > 1 and result = "...; ..."
67-
)
68-
}
69-
7060
/** Gets the `n`th statement in this sequence. */
7161
Stmt getStmt(int n) { none() }
7262

@@ -91,6 +81,8 @@ private class Then extends StmtSequence, TThen {
9181
Then() { this = TThen(g) }
9282

9383
override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
84+
85+
final override string toString() { result = "then ..." }
9486
}
9587

9688
private class Else extends StmtSequence, TElse {
@@ -99,6 +91,8 @@ private class Else extends StmtSequence, TElse {
9991
Else() { this = TElse(g) }
10092

10193
override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
94+
95+
final override string toString() { result = "else ..." }
10296
}
10397

10498
private class Do extends StmtSequence, TDo {
@@ -107,6 +101,8 @@ private class Do extends StmtSequence, TDo {
107101
Do() { this = TDo(g) }
108102

109103
override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
104+
105+
final override string toString() { result = "do ..." }
110106
}
111107

112108
private class Ensure extends StmtSequence, TEnsure {
@@ -212,13 +208,7 @@ class ParenthesizedExpr extends StmtSequence, TParenthesizedExpr {
212208

213209
final override string getAPrimaryQlClass() { result = "ParenthesizedExpr" }
214210

215-
final override string toString() {
216-
exists(int c | c = this.getNumberOfStatements() |
217-
c = 0 and result = "()"
218-
or
219-
c > 0 and result = "(" + StmtSequence.super.toString() + ")"
220-
)
221-
}
211+
final override string toString() { result = "( ... )" }
222212

223213
final override Stmt getStmt(int n) { toGenerated(result) = g.getChild(n) }
224214
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ private module Cached {
9494
not strictcount(int i | exists(g.getParent().(Generated::LeftAssignmentList).getChild(i))) = 1
9595
} or
9696
TDivExpr(Generated::Binary g) { g instanceof @binary_slash } or
97-
TDo(Generated::Do g) { exists(g.getChild(_)) } or
97+
TDo(Generated::Do g) or
9898
TDoBlock(Generated::DoBlock g) { not g.getParent() instanceof Generated::Lambda } or
9999
TElementReference(Generated::ElementReference g) or
100-
TElse(Generated::Else g) { exists(g.getChild(_)) } or
100+
TElse(Generated::Else g) or
101101
TElsif(Generated::Elsif g) or
102102
TEmptyStmt(Generated::EmptyStatement g) or
103103
TEndBlock(Generated::EndBlock g) or
@@ -198,7 +198,7 @@ private module Cached {
198198
TSubshellLiteral(Generated::Subshell g) or
199199
TSymbolArrayLiteral(Generated::SymbolArray g) or
200200
TTernaryIfExpr(Generated::Conditional g) or
201-
TThen(Generated::Then g) { exists(g.getChild(_)) } or
201+
TThen(Generated::Then g) or
202202
TTokenConstantAccess(Generated::Constant g) {
203203
// A tree-sitter `constant` token is a read of that constant in any context
204204
// where an identifier would be a vcall.

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

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -643,17 +643,10 @@ module Trees {
643643

644644
final override predicate first(AstNode first) { first(this.getCondition(), first) }
645645

646-
private AstNode getConditionSucc() {
647-
result = this.getBody()
648-
or
649-
not exists(this.getBody()) and
650-
result = this.getCondition()
651-
}
652-
653646
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
654647
last(this.getCondition(), pred, c) and
655648
this.entersLoopWhenConditionIs(c.(BooleanCompletion).getValue()) and
656-
first(this.getConditionSucc(), succ)
649+
first(this.getBody(), succ)
657650
or
658651
last(this.getBody(), pred, c) and
659652
first(this.getCondition(), succ) and
@@ -1117,21 +1110,6 @@ module Trees {
11171110
}
11181111
}
11191112

1120-
private class TSimpleHiddenStmtSequenceTree =
1121-
ASTInternal::TElse or ASTInternal::TThen or ASTInternal::TDo;
1122-
1123-
private class SimpleHiddenStmtSequenceTree extends StmtSequenceTree, TSimpleHiddenStmtSequenceTree {
1124-
final override predicate propagatesAbnormal(AstNode child) { child = this.getAStmt() }
1125-
1126-
final override predicate first(AstNode first) { first(this.getStmt(0), first) }
1127-
1128-
final override predicate last(AstNode last, Completion c) { last(this.getLastStmt(), last, c) }
1129-
1130-
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
1131-
StmtSequenceTree.super.succ(pred, succ, c)
1132-
}
1133-
}
1134-
11351113
/**
11361114
* Control-flow tree for any pre-order StmtSequence that doesn't have a more
11371115
* specific implementation.
@@ -1144,8 +1122,7 @@ module Trees {
11441122
not this instanceof EndBlock and
11451123
not this instanceof StringInterpolationComponent and
11461124
not this instanceof Block and
1147-
not this instanceof ParenthesizedExpr and
1148-
not this instanceof TSimpleHiddenStmtSequenceTree
1125+
not this instanceof ParenthesizedExpr
11491126
}
11501127

11511128
final override predicate propagatesAbnormal(AstNode child) { child = this.getAStmt() }

0 commit comments

Comments
 (0)