Skip to content

Commit 1988397

Browse files
committed
Make shared CFG construction library a parameterized module
1 parent 5049aaf commit 1988397

File tree

17 files changed

+1553
-2409
lines changed

17 files changed

+1553
-2409
lines changed

config/identical-files.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@
486486
],
487487
"CFG": [
488488
"csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImplShared.qll",
489-
"ruby/ql/lib/codeql/ruby/controlflow/internal/ControlFlowGraphImplShared.qll",
490489
"swift/ql/lib/codeql/swift/controlflow/internal/ControlFlowGraphImplShared.qll"
491490
],
492491
"TypeTracker": [
@@ -573,4 +572,4 @@
573572
"python/ql/lib/semmle/python/security/internal/EncryptionKeySizes.qll",
574573
"java/ql/lib/semmle/code/java/security/internal/EncryptionKeySizes.qll"
575574
]
576-
}
575+
}

ruby/ql/consistency-queries/CfgConsistency.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import codeql.ruby.controlflow.internal.ControlFlowGraphImplShared::Consistency
1+
import codeql.ruby.controlflow.internal.ControlFlowGraphImpl::Consistency
22
import codeql.ruby.AST
33
import codeql.ruby.CFG
44
import codeql.ruby.controlflow.internal.Completion
5-
import codeql.ruby.controlflow.internal.ControlFlowGraphImpl
5+
import codeql.ruby.controlflow.internal.ControlFlowGraphImpl as CfgImpl
66

77
/**
88
* All `Expr` nodes are `PostOrderTree`s
@@ -14,7 +14,7 @@ query predicate nonPostOrderExpr(Expr e, string cls) {
1414
not e instanceof Namespace and
1515
not e instanceof Toplevel and
1616
exists(AstNode last, Completion c |
17-
last(e, last, c) and
17+
CfgImpl::last(e, last, c) and
1818
last != e and
1919
c instanceof NormalCompletion
2020
)

ruby/ql/lib/codeql/ruby/ast/Statement.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ private import codeql.ruby.CFG
33
private import internal.AST
44
private import internal.TreeSitter
55
private import internal.Variable
6-
private import codeql.ruby.controlflow.internal.ControlFlowGraphImpl
6+
private import codeql.ruby.controlflow.internal.ControlFlowGraphImpl as CfgImpl
77

88
/**
99
* A statement.
@@ -15,10 +15,10 @@ class Stmt extends AstNode, TStmt {
1515
CfgNodes::AstCfgNode getAControlFlowNode() { result.getNode() = this }
1616

1717
/** Gets a control-flow entry node for this statement, if any */
18-
AstNode getAControlFlowEntryNode() { result = getAControlFlowEntryNode(this) }
18+
AstNode getAControlFlowEntryNode() { result = CfgImpl::getAControlFlowEntryNode(this) }
1919

2020
/** Gets the control-flow scope of this statement, if any. */
21-
CfgScope getCfgScope() { result = getCfgScope(this) }
21+
CfgScope getCfgScope() { result = CfgImpl::getCfgScope(this) }
2222

2323
/** Gets the enclosing callable, if any. */
2424
Callable getEnclosingCallable() { result = this.getCfgScope() }

ruby/ql/lib/codeql/ruby/controlflow/BasicBlocks.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ private import codeql.ruby.AST
44
private import codeql.ruby.ast.internal.AST
55
private import codeql.ruby.ast.internal.TreeSitter
66
private import codeql.ruby.controlflow.ControlFlowGraph
7-
private import internal.ControlFlowGraphImpl
87
private import CfgNodes
98
private import SuccessorTypes
109

ruby/ql/lib/codeql/ruby/controlflow/CfgNodes.qll

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ private import codeql.ruby.dataflow.SSA
66
private import codeql.ruby.ast.internal.Constant
77
private import codeql.ruby.ast.internal.Literal
88
private import ControlFlowGraph
9-
private import internal.ControlFlowGraphImpl
9+
private import internal.ControlFlowGraphImpl as CfgImpl
1010
private import internal.Splitting
1111

1212
/** An entry node for a given scope. */
13-
class EntryNode extends CfgNode, TEntryNode {
13+
class EntryNode extends CfgNode, CfgImpl::TEntryNode {
1414
override string getAPrimaryQlClass() { result = "EntryNode" }
1515

1616
private CfgScope scope;
1717

18-
EntryNode() { this = TEntryNode(scope) }
18+
EntryNode() { this = CfgImpl::TEntryNode(scope) }
1919

2020
final override EntryBasicBlock getBasicBlock() { result = super.getBasicBlock() }
2121

@@ -25,13 +25,13 @@ class EntryNode extends CfgNode, TEntryNode {
2525
}
2626

2727
/** An exit node for a given scope, annotated with the type of exit. */
28-
class AnnotatedExitNode extends CfgNode, TAnnotatedExitNode {
28+
class AnnotatedExitNode extends CfgNode, CfgImpl::TAnnotatedExitNode {
2929
override string getAPrimaryQlClass() { result = "AnnotatedExitNode" }
3030

3131
private CfgScope scope;
3232
private boolean normal;
3333

34-
AnnotatedExitNode() { this = TAnnotatedExitNode(scope, normal) }
34+
AnnotatedExitNode() { this = CfgImpl::TAnnotatedExitNode(scope, normal) }
3535

3636
/** Holds if this node represent a normal exit. */
3737
final predicate isNormal() { normal = true }
@@ -52,12 +52,12 @@ class AnnotatedExitNode extends CfgNode, TAnnotatedExitNode {
5252
}
5353

5454
/** An exit node for a given scope. */
55-
class ExitNode extends CfgNode, TExitNode {
55+
class ExitNode extends CfgNode, CfgImpl::TExitNode {
5656
override string getAPrimaryQlClass() { result = "ExitNode" }
5757

5858
private CfgScope scope;
5959

60-
ExitNode() { this = TExitNode(scope) }
60+
ExitNode() { this = CfgImpl::TExitNode(scope) }
6161

6262
final override Location getLocation() { result = scope.getLocation() }
6363

@@ -71,14 +71,14 @@ class ExitNode extends CfgNode, TExitNode {
7171
* (dead) code or not important for control flow, and multiple when there are different
7272
* splits for the AST node.
7373
*/
74-
class AstCfgNode extends CfgNode, TElementNode {
74+
class AstCfgNode extends CfgNode, CfgImpl::TElementNode {
7575
/** Gets the name of the primary QL class for this node. */
7676
override string getAPrimaryQlClass() { result = "AstCfgNode" }
7777

78-
private Splits splits;
78+
private CfgImpl::Splits splits;
7979
AstNode e;
8080

81-
AstCfgNode() { this = TElementNode(_, e, splits) }
81+
AstCfgNode() { this = CfgImpl::TElementNode(_, e, splits) }
8282

8383
final override AstNode getNode() { result = e }
8484

ruby/ql/lib/codeql/ruby/controlflow/ControlFlowGraph.qll

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
private import codeql.ruby.AST
44
private import codeql.ruby.controlflow.BasicBlocks
55
private import SuccessorTypes
6-
private import internal.ControlFlowGraphImpl
6+
private import internal.ControlFlowGraphImpl as CfgImpl
77
private import internal.Splitting
88
private import internal.Completion
99

@@ -15,12 +15,12 @@ private import internal.Completion
1515
* Note that module declarations are not themselves CFG scopes, as they are part of
1616
* the CFG of the enclosing top-level or callable.
1717
*/
18-
class CfgScope extends Scope instanceof CfgScopeImpl {
18+
class CfgScope extends Scope instanceof CfgImpl::CfgScopeImpl {
1919
/** Gets the CFG scope that this scope is nested under, if any. */
2020
final CfgScope getOuterCfgScope() {
2121
exists(AstNode parent |
2222
parent = this.getParent() and
23-
result = getCfgScope(parent)
23+
result = CfgImpl::getCfgScope(parent)
2424
)
2525
}
2626
}
@@ -33,7 +33,7 @@ class CfgScope extends Scope instanceof CfgScopeImpl {
3333
*
3434
* Only nodes that can be reached from an entry point are included in the CFG.
3535
*/
36-
class CfgNode extends TCfgNode {
36+
class CfgNode extends CfgImpl::TCfgNode {
3737
/** Gets the name of the primary QL class for this node. */
3838
string getAPrimaryQlClass() { none() }
3939

@@ -53,13 +53,13 @@ class CfgNode extends TCfgNode {
5353
final predicate isCondition() { exists(this.getASuccessor(any(ConditionalSuccessor bs))) }
5454

5555
/** Gets the scope of this node. */
56-
final CfgScope getScope() { result = getNodeCfgScope(this) }
56+
final CfgScope getScope() { result = CfgImpl::getNodeCfgScope(this) }
5757

5858
/** Gets the basic block that this control flow node belongs to. */
5959
BasicBlock getBasicBlock() { result.getANode() = this }
6060

6161
/** Gets a successor node of a given type, if any. */
62-
final CfgNode getASuccessor(SuccessorType t) { result = getASuccessor(this, t) }
62+
final CfgNode getASuccessor(SuccessorType t) { result = CfgImpl::getASuccessor(this, t) }
6363

6464
/** Gets an immediate successor, if any. */
6565
final CfgNode getASuccessor() { result = this.getASuccessor(_) }
@@ -78,15 +78,15 @@ class CfgNode extends TCfgNode {
7878
}
7979

8080
/** The type of a control flow successor. */
81-
class SuccessorType extends TSuccessorType {
81+
class SuccessorType extends CfgImpl::TSuccessorType {
8282
/** Gets a textual representation of successor type. */
8383
string toString() { none() }
8484
}
8585

8686
/** Provides different types of control flow successor types. */
8787
module SuccessorTypes {
8888
/** A normal control flow successor. */
89-
class NormalSuccessor extends SuccessorType, TSuccessorSuccessor {
89+
class NormalSuccessor extends SuccessorType, CfgImpl::TSuccessorSuccessor {
9090
final override string toString() { result = "successor" }
9191
}
9292

@@ -99,9 +99,9 @@ module SuccessorTypes {
9999
boolean value;
100100

101101
ConditionalSuccessor() {
102-
this = TBooleanSuccessor(value) or
103-
this = TEmptinessSuccessor(value) or
104-
this = TMatchingSuccessor(value)
102+
this = CfgImpl::TBooleanSuccessor(value) or
103+
this = CfgImpl::TEmptinessSuccessor(value) or
104+
this = CfgImpl::TMatchingSuccessor(value)
105105
}
106106

107107
/** Gets the Boolean value of this successor. */
@@ -125,7 +125,7 @@ module SuccessorTypes {
125125
*
126126
* `x >= 0` has both a `true` successor and a `false` successor.
127127
*/
128-
class BooleanSuccessor extends ConditionalSuccessor, TBooleanSuccessor { }
128+
class BooleanSuccessor extends ConditionalSuccessor, CfgImpl::TBooleanSuccessor { }
129129

130130
/**
131131
* An emptiness control flow successor.
@@ -158,7 +158,7 @@ module SuccessorTypes {
158158
* \___/
159159
* ```
160160
*/
161-
class EmptinessSuccessor extends ConditionalSuccessor, TEmptinessSuccessor {
161+
class EmptinessSuccessor extends ConditionalSuccessor, CfgImpl::TEmptinessSuccessor {
162162
override string toString() { if value = true then result = "empty" else result = "non-empty" }
163163
}
164164

@@ -189,7 +189,7 @@ module SuccessorTypes {
189189
* puts "one" puts "not one"
190190
* ```
191191
*/
192-
class MatchingSuccessor extends ConditionalSuccessor, TMatchingSuccessor {
192+
class MatchingSuccessor extends ConditionalSuccessor, CfgImpl::TMatchingSuccessor {
193193
override string toString() { if value = true then result = "match" else result = "no-match" }
194194
}
195195

@@ -207,7 +207,7 @@ module SuccessorTypes {
207207
* The exit node of `sum` is a `return` successor of the `return x + y`
208208
* statement.
209209
*/
210-
class ReturnSuccessor extends SuccessorType, TReturnSuccessor {
210+
class ReturnSuccessor extends SuccessorType, CfgImpl::TReturnSuccessor {
211211
final override string toString() { result = "return" }
212212
}
213213

@@ -230,7 +230,7 @@ module SuccessorTypes {
230230
*
231231
* The node `puts "done"` is `break` successor of the node `break`.
232232
*/
233-
class BreakSuccessor extends SuccessorType, TBreakSuccessor {
233+
class BreakSuccessor extends SuccessorType, CfgImpl::TBreakSuccessor {
234234
final override string toString() { result = "break" }
235235
}
236236

@@ -253,7 +253,7 @@ module SuccessorTypes {
253253
*
254254
* The node `x >= 0` is `next` successor of the node `next`.
255255
*/
256-
class NextSuccessor extends SuccessorType, TNextSuccessor {
256+
class NextSuccessor extends SuccessorType, CfgImpl::TNextSuccessor {
257257
final override string toString() { result = "next" }
258258
}
259259

@@ -278,7 +278,7 @@ module SuccessorTypes {
278278
*
279279
* The node `x -= 1` is `redo` successor of the node `redo`.
280280
*/
281-
class RedoSuccessor extends SuccessorType, TRedoSuccessor {
281+
class RedoSuccessor extends SuccessorType, CfgImpl::TRedoSuccessor {
282282
final override string toString() { result = "redo" }
283283
}
284284

@@ -302,7 +302,7 @@ module SuccessorTypes {
302302
*
303303
* The node `puts "Retry"` is `retry` successor of the node `retry`.
304304
*/
305-
class RetrySuccessor extends SuccessorType, TRetrySuccessor {
305+
class RetrySuccessor extends SuccessorType, CfgImpl::TRetrySuccessor {
306306
final override string toString() { result = "retry" }
307307
}
308308

@@ -323,7 +323,7 @@ module SuccessorTypes {
323323
* The exit node of `m` is an exceptional successor of the node
324324
* `raise "x > 2"`.
325325
*/
326-
class RaiseSuccessor extends SuccessorType, TRaiseSuccessor {
326+
class RaiseSuccessor extends SuccessorType, CfgImpl::TRaiseSuccessor {
327327
final override string toString() { result = "raise" }
328328
}
329329

@@ -344,7 +344,7 @@ module SuccessorTypes {
344344
* The exit node of `m` is an exit successor of the node
345345
* `exit 1`.
346346
*/
347-
class ExitSuccessor extends SuccessorType, TExitSuccessor {
347+
class ExitSuccessor extends SuccessorType, CfgImpl::TExitSuccessor {
348348
final override string toString() { result = "exit" }
349349
}
350350
}

ruby/ql/lib/codeql/ruby/controlflow/internal/Completion.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ private import codeql.ruby.AST
88
private import codeql.ruby.ast.internal.AST
99
private import codeql.ruby.ast.internal.Control
1010
private import codeql.ruby.controlflow.ControlFlowGraph
11-
private import ControlFlowGraphImpl
11+
private import ControlFlowGraphImpl as CfgImpl
1212
private import NonReturning
1313
private import SuccessorTypes
1414

@@ -53,7 +53,7 @@ private predicate nestedEnsureCompletion(TCompletion outer, int nestLevel) {
5353
or
5454
outer = TExitCompletion()
5555
) and
56-
nestLevel = any(Trees::BodyStmtTree t).getNestLevel()
56+
nestLevel = any(CfgImpl::Trees::BodyStmtTree t).getNestLevel()
5757
}
5858

5959
pragma[noinline]
@@ -72,7 +72,7 @@ private predicate completionIsValidForStmt(AstNode n, Completion c) {
7272
}
7373

7474
private AstNode getARescuableBodyChild() {
75-
exists(Trees::BodyStmtTree bst | result = bst.getBodyChild(_, true) |
75+
exists(CfgImpl::Trees::BodyStmtTree bst | result = bst.getBodyChild(_, true) |
7676
exists(bst.getARescue())
7777
or
7878
exists(bst.getEnsure())
@@ -247,7 +247,7 @@ private predicate inMatchingContext(AstNode n) {
247247
or
248248
n = any(ReferencePattern p).getExpr()
249249
or
250-
n.(Trees::DefaultValueParameterTree).hasDefaultValue()
250+
n.(CfgImpl::Trees::DefaultValueParameterTree).hasDefaultValue()
251251
}
252252

253253
/**

0 commit comments

Comments
 (0)