Skip to content

Commit 0041479

Browse files
committed
Simplify CFG classes for StmtSequences
1 parent 94ceb3f commit 0041479

1 file changed

Lines changed: 38 additions & 57 deletions

File tree

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

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ module Trees {
303303
final override predicate first(AstNode first) { this.firstInner(first) }
304304

305305
final override predicate last(AstNode last, Completion c) { this.lastInner(last, c) }
306+
307+
final override predicate propagatesAbnormal(AstNode child) { none() }
306308
}
307309

308310
private class BinaryOperationTree extends StandardPostOrderTree, BinaryOperation {
@@ -338,11 +340,9 @@ module Trees {
338340

339341
private class BlockParameterTree extends NonDefaultValueParameterTree, BlockParameter { }
340342

341-
/**
342-
* TODO: make all StmtSequence tree classes post-order, and simplify class
343-
* hierarchy.
344-
*/
345343
abstract class BodyStmtTree extends StmtSequenceTree, BodyStmt {
344+
override predicate first(AstNode first) { first = this }
345+
346346
predicate firstInner(AstNode first) {
347347
first(this.getBodyChild(0, _), first)
348348
or
@@ -523,10 +523,6 @@ module Trees {
523523
}
524524
}
525525

526-
abstract class BodyStmtPostOrderTree extends BodyStmtTree, PostOrderTree {
527-
override predicate first(AstNode first) { first = this }
528-
}
529-
530526
private class BooleanLiteralTree extends LeafTree, BooleanLiteral { }
531527

532528
class BraceBlockTree extends ScopeTree, BraceBlock {
@@ -584,7 +580,7 @@ module Trees {
584580

585581
private class CharacterTree extends LeafTree, CharacterLiteral { }
586582

587-
private class ClassDeclarationTree extends BodyStmtPostOrderTree, ClassDeclaration {
583+
private class ClassDeclarationTree extends BodyStmtTree, ClassDeclaration {
588584
final override predicate first(AstNode first) {
589585
this.firstInner(first)
590586
or
@@ -593,7 +589,7 @@ module Trees {
593589
}
594590

595591
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
596-
BodyStmtPostOrderTree.super.succ(pred, succ, c)
592+
BodyStmtTree.super.succ(pred, succ, c)
597593
or
598594
succ = this and
599595
this.lastInner(pred, c)
@@ -728,13 +724,15 @@ module Trees {
728724
}
729725
}
730726

731-
private class DoBlockTree extends BodyStmtPostOrderTree, DoBlock {
727+
private class DoBlockTree extends BodyStmtTree, DoBlock {
732728
/** Gets the `i`th child in the body of this block. */
733729
final override AstNode getBodyChild(int i, boolean rescuable) {
734730
result = this.getParameter(i) and rescuable = false
735731
or
736-
result = BodyStmtPostOrderTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
732+
result = BodyStmtTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
737733
}
734+
735+
override predicate propagatesAbnormal(AstNode child) { none() }
738736
}
739737

740738
private class EmptyStatementTree extends LeafTree, EmptyStmt { }
@@ -850,12 +848,12 @@ module Trees {
850848
final override AstNode getAccessNode() { result = this.getDefiningAccess() }
851849
}
852850

853-
private class LambdaTree extends BodyStmtPostOrderTree, Lambda {
851+
private class LambdaTree extends BodyStmtTree, Lambda {
854852
/** Gets the `i`th child in the body of this block. */
855853
final override AstNode getBodyChild(int i, boolean rescuable) {
856854
result = this.getParameter(i) and rescuable = false
857855
or
858-
result = BodyStmtPostOrderTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
856+
result = BodyStmtTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
859857
}
860858
}
861859

@@ -925,16 +923,18 @@ module Trees {
925923

926924
private class MethodNameTree extends LeafTree, MethodName, ASTInternal::TTokenMethodName { }
927925

928-
private class MethodTree extends BodyStmtPostOrderTree, Method {
926+
private class MethodTree extends BodyStmtTree, Method {
927+
final override predicate propagatesAbnormal(AstNode child) { none() }
928+
929929
/** Gets the `i`th child in the body of this block. */
930930
final override AstNode getBodyChild(int i, boolean rescuable) {
931931
result = this.getParameter(i) and rescuable = false
932932
or
933-
result = BodyStmtPostOrderTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
933+
result = BodyStmtTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
934934
}
935935
}
936936

937-
private class ModuleDeclarationTree extends BodyStmtPostOrderTree, ModuleDeclaration {
937+
private class ModuleDeclarationTree extends BodyStmtTree, ModuleDeclaration {
938938
final override predicate first(AstNode first) {
939939
this.firstInner(first)
940940
or
@@ -943,7 +943,7 @@ module Trees {
943943
}
944944

945945
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
946-
BodyStmtPostOrderTree.super.succ(pred, succ, c)
946+
BodyStmtTree.super.succ(pred, succ, c)
947947
or
948948
succ = this and
949949
this.lastInner(pred, c)
@@ -953,7 +953,7 @@ module Trees {
953953
final override AstNode getBodyChild(int i, boolean rescuable) {
954954
result = this.getScopeExpr() and i = 0 and rescuable = false
955955
or
956-
result = BodyStmtPostOrderTree.super.getBodyChild(i - count(this.getScopeExpr()), rescuable)
956+
result = BodyStmtTree.super.getBodyChild(i - count(this.getScopeExpr()), rescuable)
957957
}
958958
}
959959

@@ -1099,37 +1099,7 @@ module Trees {
10991099
SimpleParameterTreeDupUnderscore() { not exists(this.getDefiningAccess()) }
11001100
}
11011101

1102-
/**
1103-
* Control-flow tree for any post-order StmtSequence that doesn't have a more
1104-
* specific implementation.
1105-
* TODO: make all StmtSequence tree classes post-order, and simplify class
1106-
* hierarchy.
1107-
*/
1108-
private class SimplePostOrderStmtSequenceTree extends StmtSequenceTree, PostOrderTree {
1109-
SimplePostOrderStmtSequenceTree() {
1110-
this instanceof StringInterpolationComponent or
1111-
this instanceof ParenthesizedExpr or
1112-
this instanceof BeginBlock or
1113-
this instanceof ASTInternal::TThen or
1114-
this instanceof ASTInternal::TDo or
1115-
this instanceof ASTInternal::TElse or
1116-
this instanceof ASTInternal::TEnsure
1117-
}
1118-
1119-
final override predicate first(AstNode first) { first(this.getStmt(0), first) }
1120-
1121-
final override predicate propagatesAbnormal(AstNode child) { child = this.getAStmt() }
1122-
1123-
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
1124-
succ = this and
1125-
last(this.getLastBodyChild(), pred, c) and
1126-
c instanceof NormalCompletion
1127-
or
1128-
StmtSequenceTree.super.succ(pred, succ, c)
1129-
}
1130-
}
1131-
1132-
private class SingletonClassTree extends BodyStmtPostOrderTree, SingletonClass {
1102+
private class SingletonClassTree extends BodyStmtTree, SingletonClass {
11331103
final override predicate first(AstNode first) {
11341104
this.firstInner(first)
11351105
or
@@ -1138,7 +1108,7 @@ module Trees {
11381108
}
11391109

11401110
final override predicate succ(AstNode pred, AstNode succ, Completion c) {
1141-
BodyStmtPostOrderTree.super.succ(pred, succ, c)
1111+
BodyStmtTree.super.succ(pred, succ, c)
11421112
or
11431113
succ = this and
11441114
this.lastInner(pred, c)
@@ -1149,23 +1119,23 @@ module Trees {
11491119
(
11501120
result = this.getValue() and i = 0 and rescuable = false
11511121
or
1152-
result = BodyStmtPostOrderTree.super.getBodyChild(i - 1, rescuable)
1122+
result = BodyStmtTree.super.getBodyChild(i - 1, rescuable)
11531123
)
11541124
}
11551125
}
11561126

1157-
private class SingletonMethodTree extends BodyStmtPostOrderTree, SingletonMethod {
1127+
private class SingletonMethodTree extends BodyStmtTree, SingletonMethod {
11581128
/** Gets the `i`th child in the body of this block. */
11591129
final override AstNode getBodyChild(int i, boolean rescuable) {
11601130
result = this.getParameter(i) and rescuable = false
11611131
or
1162-
result = BodyStmtPostOrderTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
1132+
result = BodyStmtTree.super.getBodyChild(i - this.getNumberOfParameters(), rescuable)
11631133
}
11641134

11651135
override predicate first(AstNode first) { first(this.getObject(), first) }
11661136

11671137
override predicate succ(AstNode pred, AstNode succ, Completion c) {
1168-
BodyStmtPostOrderTree.super.succ(pred, succ, c)
1138+
BodyStmtTree.super.succ(pred, succ, c)
11691139
or
11701140
last(this.getObject(), pred, c) and
11711141
succ = this and
@@ -1179,8 +1149,15 @@ module Trees {
11791149

11801150
private class SplatParameterTree extends NonDefaultValueParameterTree, SplatParameter { }
11811151

1182-
abstract class StmtSequenceTree extends ControlFlowTree, StmtSequence {
1183-
override predicate propagatesAbnormal(AstNode child) { none() }
1152+
class StmtSequenceTree extends PostOrderTree, StmtSequence {
1153+
StmtSequenceTree() {
1154+
not this instanceof BraceBlock and
1155+
not this instanceof EndBlock
1156+
}
1157+
1158+
override predicate propagatesAbnormal(AstNode child) { child = this.getAStmt() }
1159+
1160+
override predicate first(AstNode first) { first(this.getStmt(0), first) }
11841161

11851162
/** Gets the `i`th child in the body of this body statement. */
11861163
AstNode getBodyChild(int i, boolean rescuable) {
@@ -1202,6 +1179,10 @@ module Trees {
12021179
first(this.getBodyChild(i + 1, _), succ) and
12031180
c instanceof NormalCompletion
12041181
)
1182+
or
1183+
succ = this and
1184+
last(this.getLastBodyChild(), pred, c) and
1185+
c instanceof NormalCompletion
12051186
}
12061187
}
12071188

0 commit comments

Comments
 (0)