Skip to content

Commit 938730b

Browse files
committed
C#: Include parameters and their defaults in the CFG
1 parent e02c0f6 commit 938730b

6 files changed

Lines changed: 510 additions & 217 deletions

File tree

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ private import internal.Location
1313
* An element that can have a child statement or expression.
1414
*/
1515
class ExprOrStmtParent extends Element, @exprorstmt_parent {
16-
final override ControlFlowElement getChild(int i) {
16+
override ControlFlowElement getChild(int i) {
1717
result = this.getChildExpr(i) or
1818
result = this.getChildStmt(i)
1919
}
@@ -42,14 +42,8 @@ class ExprOrStmtParent extends Element, @exprorstmt_parent {
4242
*
4343
* An element that can have a child top-level expression.
4444
*/
45-
class TopLevelExprParent extends Element, @top_level_expr_parent {
45+
class TopLevelExprParent extends ExprOrStmtParent, @top_level_expr_parent {
4646
final override Expr getChild(int i) { result = this.getChildExpr(i) }
47-
48-
/** Gets the `i`th child expression of this element (zero-based). */
49-
final Expr getChildExpr(int i) { expr_parent_top_level_adjusted(result, i, this) }
50-
51-
/** Gets a child expression of this element, if any. */
52-
final Expr getAChildExpr() { result = this.getChildExpr(_) }
5347
}
5448

5549
/** INTERNAL: Do not use. */

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ class LocalScopeVariable extends Variable, @local_scope_variable {
8787
* }
8888
* ```
8989
*/
90-
class Parameter extends LocalScopeVariable, Attributable, TopLevelExprParent, @parameter {
90+
class Parameter extends LocalScopeVariable, Attributable, TopLevelExprParent, ControlFlowElement,
91+
@parameter
92+
{
9193
/** Gets the raw position of this parameter, including the `this` parameter at index 0. */
9294
final int getRawPosition() { this = this.getDeclaringElement().getRawParameter(result) }
9395

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ private module Ast implements AstSig<Location> {
7575
Callable getEnclosingCallable(AstNode node) {
7676
result = node.(ControlFlowElement).getEnclosingCallable() or
7777
result.(ObjectInitMethod).initializes(getParent*(node)) or
78-
Initializers::staticMemberInitializer(result, getParent*(node))
78+
Initializers::staticMemberInitializer(result, getParent*(node)) or
79+
result = node.(Parameter).getCallable() or
80+
node = any(Parameter p | result = p.getCallable()).getDefaultValue()
7981
}
8082

8183
class Callable = CS::Callable;
@@ -85,6 +87,17 @@ private module Ast implements AstSig<Location> {
8587
result = c.getBody()
8688
}
8789

90+
final private class ParameterFinal = CS::Parameter;
91+
92+
class Parameter extends ParameterFinal {
93+
Expr getDefaultValue() {
94+
result = super.getDefaultValue() and
95+
getCompilation(result.getFile()) = getCompilation(this.getFile())
96+
}
97+
}
98+
99+
Parameter callableGetParameter(Callable c, int i) { result = c.getParameter(i) }
100+
88101
class Stmt = CS::Stmt;
89102

90103
class Expr = CS::Expr;
@@ -234,7 +247,7 @@ private class CompilationExt extends TCompilationExt {
234247
/** Gets the compilation that source file `f` belongs to. */
235248
private CompilationExt getCompilation(File f) {
236249
exists(Compilation c |
237-
f = c.getAFileCompiled() and
250+
f = [c.getAFileCompiled(), c.getAReference()] and
238251
result = TCompilation(c)
239252
)
240253
or
@@ -415,10 +428,10 @@ private module Input implements InputSig1, InputSig2 {
415428
l = TLblGoto(n.(LabelStmt).getLabel())
416429
}
417430

418-
class CallableBodyPartContext = CompilationExt;
431+
class CallableContext = CompilationExt;
419432

420433
pragma[nomagic]
421-
Ast::AstNode callableGetBodyPart(Callable c, CallableBodyPartContext ctx, int index) {
434+
Ast::AstNode callableGetBodyPart(Callable c, CallableContext ctx, int index) {
422435
not Ast::skipControlFlow(result) and
423436
ctx = getCompilation(result.getFile()) and
424437
(
@@ -437,9 +450,19 @@ private module Input implements InputSig1, InputSig2 {
437450
or
438451
i = 2 and result = ctor.getBody()
439452
)
453+
or
454+
not c instanceof Constructor and
455+
result = c.getBody() and
456+
index = 0
440457
)
441458
}
442459

460+
pragma[nomagic]
461+
Ast::Parameter callableGetParameter(Callable c, CallableContext ctx, int index) {
462+
result = Ast::callableGetParameter(c, index) and
463+
ctx = getCompilation(result.getFile())
464+
}
465+
443466
private Expr getQualifier(QualifiableExpr qe) {
444467
result = qe.getQualifier() or
445468
result = qe.(ExtensionMethodCall).getArgument(0)

csharp/ql/lib/semmlecode.csharp.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ compiler_generated(unique int id: @element ref);
13621362

13631363
/** CONTROL/DATA FLOW **/
13641364

1365-
@control_flow_element = @stmt | @expr;
1365+
@control_flow_element = @stmt | @expr | @parameter;
13661366

13671367
/* XML Files */
13681368

0 commit comments

Comments
 (0)