Skip to content

Commit 19faf8f

Browse files
committed
C#: Add ObjectInitMethod as enclosing callable for the instance initializers.
1 parent c24b43d commit 19faf8f

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,22 @@ class ExtensionTypeExtensionMethod extends ExtensionMethodImpl {
336336
ExtensionTypeExtensionMethod() { this.isInExtension() }
337337
}
338338

339+
/**
340+
* A non-static member with an initializer, for example a field `int Field = 0`.
341+
*/
342+
private class InitializedInstanceMember extends Member {
343+
private AssignExpr ae;
344+
345+
InitializedInstanceMember() {
346+
not this.isStatic() and
347+
expr_parent_top_level(ae, _, this) and
348+
not ae = getExpressionBody(_)
349+
}
350+
351+
/** Gets the initializer expression. */
352+
AssignExpr getInitializer() { result = ae }
353+
}
354+
339355
/**
340356
* An object initializer method.
341357
*
@@ -347,6 +363,17 @@ class ExtensionTypeExtensionMethod extends ExtensionMethodImpl {
347363
*/
348364
class ObjectInitMethod extends Method {
349365
ObjectInitMethod() { this.getName() = "<object initializer>" }
366+
367+
/**
368+
* Holds if this object initializer method performs the initialization
369+
* of a member via assignment `init`.
370+
*/
371+
predicate initializes(AssignExpr init) {
372+
exists(InitializedInstanceMember m |
373+
this.getDeclaringType().getAMember() = m and
374+
init = m.getInitializer()
375+
)
376+
}
350377
}
351378

352379
/**

csharp/ql/lib/semmle/code/csharp/ExprOrStmtParent.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ private module Cached {
214214
parent*(enclosingStart(cfe), c.(Constructor).getInitializer())
215215
or
216216
parent*(cfe, c.(Constructor).getObjectInitializerCall())
217+
or
218+
parent*(cfe, any(AssignExpr init | c.(ObjectInitMethod).initializes(init)))
217219
}
218220

219221
/** Holds if the enclosing statement of expression `e` is `s`. */

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraphImpl.qll

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,15 @@ private import semmle.code.csharp.ExprOrStmtParent
1010
private import semmle.code.csharp.commons.Compilation
1111

1212
private module Initializers {
13-
/**
14-
* A non-static member with an initializer, for example a field `int Field = 0`.
15-
*/
16-
class InitializedInstanceMember extends Member {
17-
private AssignExpr ae;
18-
19-
InitializedInstanceMember() {
20-
not this.isStatic() and
21-
expr_parent_top_level(ae, _, this) and
22-
not ae = any(Callable c).getExpressionBody()
23-
}
24-
25-
/** Gets the initializer expression. */
26-
AssignExpr getInitializer() { result = ae }
27-
}
28-
29-
/**
30-
* Holds if `obinit` is an object initializer method that performs the initialization
31-
* of a member via assignment `init`.
32-
*/
33-
predicate obinitInitializes(ObjectInitMethod obinit, AssignExpr init) {
34-
exists(InitializedInstanceMember m |
35-
obinit.getDeclaringType().getAMember() = m and
36-
init = m.getInitializer()
37-
)
38-
}
39-
4013
/**
4114
* Gets the `i`th member initializer expression for object initializer method `obinit`
4215
* in compilation `comp`.
4316
*/
4417
AssignExpr initializedInstanceMemberOrder(ObjectInitMethod obinit, CompilationExt comp, int i) {
45-
obinitInitializes(obinit, result) and
18+
obinit.initializes(result) and
4619
result =
4720
rank[i + 1](AssignExpr ae0, Location l |
48-
obinitInitializes(obinit, ae0) and
21+
obinit.initializes(ae0) and
4922
l = ae0.getLocation() and
5023
getCompilation(l.getFile()) = comp
5124
|
@@ -74,7 +47,7 @@ class CfgScope extends Element, @top_level_exprorstmt_parent {
7447
any(Callable c |
7548
c.(Constructor).hasInitializer()
7649
or
77-
Initializers::obinitInitializes(c, _)
50+
c.(ObjectInitMethod).initializes(_)
7851
or
7952
c.hasBody()
8053
)

0 commit comments

Comments
 (0)