Skip to content

Commit 28d5c11

Browse files
committed
C#: Synthesize an empty body for primary constructors.
1 parent dcde659 commit 28d5c11

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public override void Populate(TextWriter trapFile)
3232
trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition);
3333
trapFile.constructor_location(this, Location);
3434

35+
if (IsPrimary)
36+
{
37+
// Create a synthetic empty body for primary constructors.
38+
Statements.SyntheticEmptyBlock.Create(Context, this, 0, Location);
39+
}
40+
3541
if (Symbol.IsImplicitlyDeclared)
3642
{
3743
var lineCounts = new LineCounts() { Total = 2, Code = 1, Comment = 0 };
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.IO;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using Semmle.Extraction.Entities;
5+
using Semmle.Extraction.Kinds;
6+
7+
namespace Semmle.Extraction.CSharp.Entities.Statements
8+
{
9+
internal class SyntheticEmptyBlock : Statement<BlockSyntax>
10+
{
11+
private SyntheticEmptyBlock(Context cx, BlockSyntax block, IStatementParentEntity parent, int child, Location location)
12+
: base(cx, block, StmtKind.BLOCK, parent, child, location) { }
13+
14+
public static SyntheticEmptyBlock Create(Context cx, IStatementParentEntity parent, int child, Location location)
15+
{
16+
var block = SyntaxFactory.Block();
17+
var ret = new SyntheticEmptyBlock(cx, block, parent, child, location);
18+
ret.TryPopulate();
19+
return ret;
20+
}
21+
22+
protected override void PopulateStatement(TextWriter trapFile) { }
23+
}
24+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ class InstanceConstructor extends Constructor {
416416
*/
417417
class PrimaryConstructor extends Constructor {
418418
PrimaryConstructor() {
419-
not this.hasBody() and
419+
// In the extractor we use the constructor location as the location for the
420+
// synthesized empty body of the constructor.
421+
this.getLocation() = this.getBody().getLocation() and
420422
this.getDeclaringType().fromSource() and
421423
this.fromSource()
422424
}

0 commit comments

Comments
 (0)