Skip to content

Commit 4b8d45c

Browse files
committed
C#: Move internal logic into internal/ControlFlowGraph.qll
1 parent a85f3cd commit 4b8d45c

3 files changed

Lines changed: 284 additions & 283 deletions

File tree

csharp/ql/lib/printCfg.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import csharp
11+
import semmle.code.csharp.controlflow.internal.ControlFlowGraph
1112

1213
external string selectedSourceFile();
1314

csharp/ql/lib/semmle/code/csharp/controlflow/ControlFlowGraph.qll

Lines changed: 1 addition & 283 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import csharp
6-
private import codeql.controlflow.ControlFlowGraph
6+
private import internal.ControlFlowGraph
77
private import codeql.controlflow.SuccessorType
88
private import semmle.code.csharp.commons.Compilation
99
private import semmle.code.csharp.controlflow.internal.NonReturning as NonReturning
@@ -19,224 +19,6 @@ private import Cfg1
1919
private import Cfg2
2020
import Public
2121

22-
/**
23-
* INTERNAL: Do not use.
24-
*
25-
* Provides an implementation of the AST signature for C#.
26-
*/
27-
module Ast implements AstSig<Location> {
28-
private import csharp as CS
29-
30-
class AstNode = ControlFlowElementOrCallable;
31-
32-
additional predicate skipControlFlow(AstNode e) {
33-
e instanceof TypeAccess and
34-
not e instanceof TypeAccessPatternExpr
35-
or
36-
not e.getFile().fromSource()
37-
}
38-
39-
private AstNode getExprChild0(Expr e, int i) {
40-
not e instanceof NameOfExpr and
41-
not e instanceof AnonymousFunctionExpr and
42-
not skipControlFlow(result) and
43-
result = e.getChild(i)
44-
}
45-
46-
private AstNode getStmtChild0(Stmt s, int i) {
47-
not s instanceof FixedStmt and
48-
not s instanceof UsingBlockStmt and
49-
result = s.getChild(i)
50-
or
51-
s =
52-
any(FixedStmt fs |
53-
result = fs.getVariableDeclExpr(i)
54-
or
55-
result = fs.getBody() and
56-
i = max(int j | exists(fs.getVariableDeclExpr(j))) + 1
57-
)
58-
or
59-
s =
60-
any(UsingBlockStmt us |
61-
result = us.getExpr() and
62-
i = 0
63-
or
64-
result = us.getVariableDeclExpr(i)
65-
or
66-
result = us.getBody() and
67-
i = max([1, count(us.getVariableDeclExpr(_))])
68-
)
69-
}
70-
71-
AstNode getChild(AstNode n, int index) {
72-
result = getStmtChild0(n, index)
73-
or
74-
result = getExprChild0(n, index)
75-
}
76-
77-
private AstNode getParent(AstNode n) { n = getChild(result, _) }
78-
79-
Callable getEnclosingCallable(AstNode node) {
80-
result = node.(ControlFlowElement).getEnclosingCallable()
81-
or
82-
result.(ObjectInitMethod).initializes(getParent*(node))
83-
or
84-
Initializers::staticMemberInitializer(result, getParent*(node))
85-
or
86-
result = node.(Parameter).getCallable()
87-
or
88-
not skipControlFlow(node) and
89-
getParent*(node) = any(Parameter p | result = p.getCallable()).getDefaultValue()
90-
}
91-
92-
class Callable extends CS::Callable {
93-
Callable() { this.isUnboundDeclaration() }
94-
}
95-
96-
AstNode callableGetBody(Callable c) {
97-
not skipControlFlow(result) and
98-
result = c.getBody()
99-
}
100-
101-
final private class ParameterFinal = CS::Parameter;
102-
103-
class Parameter extends ParameterFinal {
104-
Expr getDefaultValue() {
105-
// Avoid combinatorial explosions for callables with multiple bodies
106-
result = unique( | | super.getDefaultValue())
107-
}
108-
}
109-
110-
Parameter callableGetParameter(Callable c, int i) {
111-
not skipControlFlow(result) and
112-
result = c.getParameter(i)
113-
}
114-
115-
class Stmt = CS::Stmt;
116-
117-
class Expr = CS::Expr;
118-
119-
class BlockStmt = CS::BlockStmt;
120-
121-
class ExprStmt = CS::ExprStmt;
122-
123-
class IfStmt = CS::IfStmt;
124-
125-
class LoopStmt = CS::LoopStmt;
126-
127-
class WhileStmt = CS::WhileStmt;
128-
129-
class DoStmt = CS::DoStmt;
130-
131-
final private class FinalForStmt = CS::ForStmt;
132-
133-
class ForStmt extends FinalForStmt {
134-
Expr getInit(int index) { result = this.getInitializer(index) }
135-
}
136-
137-
final private class FinalForeachStmt = CS::ForeachStmt;
138-
139-
class ForeachStmt extends FinalForeachStmt {
140-
Expr getVariable() {
141-
result = this.getVariableDeclExpr() or result = this.getVariableDeclTuple()
142-
}
143-
144-
Expr getCollection() { result = this.getIterableExpr() }
145-
}
146-
147-
class BreakStmt = CS::BreakStmt;
148-
149-
class ContinueStmt = CS::ContinueStmt;
150-
151-
class GotoStmt = CS::GotoStmt;
152-
153-
class ReturnStmt = CS::ReturnStmt;
154-
155-
class Throw = CS::ThrowElement;
156-
157-
final private class FinalTryStmt = CS::TryStmt;
158-
159-
class TryStmt extends FinalTryStmt {
160-
Stmt getBody() { result = this.getBlock() }
161-
162-
CatchClause getCatch(int index) { result = this.getCatchClause(index) }
163-
164-
Stmt getFinally() { result = super.getFinally() }
165-
}
166-
167-
final private class FinalCatchClause = CS::CatchClause;
168-
169-
class CatchClause extends FinalCatchClause {
170-
AstNode getVariable() { result = this.(CS::SpecificCatchClause).getVariableDeclExpr() }
171-
172-
Expr getCondition() { result = this.getFilterClause() }
173-
174-
Stmt getBody() { result = this.getBlock() }
175-
}
176-
177-
final private class FinalSwitch = CS::Switch;
178-
179-
class Switch extends FinalSwitch {
180-
Case getCase(int index) { result = super.getCase(index) }
181-
182-
Stmt getStmt(int index) { result = this.(CS::SwitchStmt).getStmt(index) }
183-
}
184-
185-
final private class FinalCase = CS::Case;
186-
187-
class Case extends FinalCase {
188-
AstNode getAPattern() { result = this.getPattern() }
189-
190-
Expr getGuard() { result = this.getCondition() }
191-
192-
AstNode getBody() { result = super.getBody() }
193-
}
194-
195-
class DefaultCase extends Case instanceof CS::DefaultCase { }
196-
197-
class ConditionalExpr = CS::ConditionalExpr;
198-
199-
class BinaryExpr = CS::BinaryOperation;
200-
201-
class LogicalAndExpr = CS::LogicalAndExpr;
202-
203-
class LogicalOrExpr = CS::LogicalOrExpr;
204-
205-
class NullCoalescingExpr = CS::NullCoalescingExpr;
206-
207-
class UnaryExpr = CS::UnaryOperation;
208-
209-
class LogicalNotExpr = CS::LogicalNotExpr;
210-
211-
class Assignment = CS::Assignment;
212-
213-
class AssignExpr = CS::AssignExpr;
214-
215-
class CompoundAssignment = CS::AssignOperation;
216-
217-
class AssignLogicalAndExpr extends CompoundAssignment {
218-
AssignLogicalAndExpr() { none() }
219-
}
220-
221-
class AssignLogicalOrExpr extends CompoundAssignment {
222-
AssignLogicalOrExpr() { none() }
223-
}
224-
225-
class AssignNullCoalescingExpr = CS::AssignCoalesceExpr;
226-
227-
final private class FinalBoolLiteral = CS::BoolLiteral;
228-
229-
class BooleanLiteral extends FinalBoolLiteral {
230-
boolean getValue() { result = this.getBoolValue() }
231-
}
232-
233-
final private class FinalIsExpr = CS::IsExpr;
234-
235-
class PatternMatchExpr extends FinalIsExpr {
236-
AstNode getPattern() { result = super.getPattern() }
237-
}
238-
}
239-
24022
/**
24123
* A compilation.
24224
*
@@ -270,70 +52,6 @@ private CompilationExt getCompilation(Element e) {
27052
result = TBuildless()
27153
}
27254

273-
private module Initializers {
274-
private import semmle.code.csharp.ExprOrStmtParent as ExprOrStmtParent
275-
276-
/**
277-
* The `expr_parent_top_level_adjusted()` relation restricted to exclude relations
278-
* between properties and their getters' expression bodies in properties such as
279-
* `int P => 0`.
280-
*
281-
* This is in order to only associate the expression body with one CFG scope, namely
282-
* the getter (and not the declaration itself).
283-
*/
284-
private predicate expr_parent_top_level_adjusted2(
285-
Expr child, int i, @top_level_exprorstmt_parent parent
286-
) {
287-
ExprOrStmtParent::expr_parent_top_level_adjusted(child, i, parent) and
288-
not exists(Getter g |
289-
g.getDeclaration() = parent and
290-
i = 0
291-
)
292-
}
293-
294-
/**
295-
* Holds if `init` is a static member initializer and `staticCtor` is the
296-
* static constructor in the same declaring type. Hence, `staticCtor` can be
297-
* considered to execute `init` prior to the execution of its body.
298-
*/
299-
predicate staticMemberInitializer(Constructor staticCtor, Expr init) {
300-
exists(Assignable a |
301-
a.(Modifiable).isStatic() and
302-
expr_parent_top_level_adjusted2(init, _, a) and
303-
a.getDeclaringType() = staticCtor.getDeclaringType() and
304-
staticCtor.isStatic()
305-
)
306-
}
307-
308-
/**
309-
* Gets the `i`th static member initializer expression for static constructor `staticCtor`.
310-
*/
311-
Expr initializedStaticMemberOrder(Constructor staticCtor, int i) {
312-
result =
313-
rank[i + 1](Expr init, Location l, string filepath, int startline, int startcolumn |
314-
staticMemberInitializer(staticCtor, init) and
315-
l = init.getLocation() and
316-
l.hasLocationInfo(filepath, startline, startcolumn, _, _)
317-
|
318-
init order by startline, startcolumn, filepath
319-
)
320-
}
321-
322-
/**
323-
* Gets the `i`th member initializer expression for object initializer method `obinit`.
324-
*/
325-
AssignExpr initializedInstanceMemberOrder(ObjectInitMethod obinit, int i) {
326-
result =
327-
rank[i + 1](AssignExpr ae0, Location l, string filepath, int startline, int startcolumn |
328-
obinit.initializes(ae0) and
329-
l = ae0.getLocation() and
330-
l.hasLocationInfo(filepath, startline, startcolumn, _, _)
331-
|
332-
ae0 order by startline, startcolumn, filepath
333-
)
334-
}
335-
}
336-
33755
private module Exceptions {
33856
private import semmle.code.csharp.commons.Assertions
33957

0 commit comments

Comments
 (0)