mirror of
https://github.com/github/codeql.git
synced 2026-04-25 00:35:20 +02:00
C#: Synthesize an empty body for primary constructors.
This commit is contained in:
@@ -32,6 +32,12 @@ namespace Semmle.Extraction.CSharp.Entities
|
||||
trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition);
|
||||
trapFile.constructor_location(this, Location);
|
||||
|
||||
if (IsPrimary)
|
||||
{
|
||||
// Create a synthetic empty body for primary constructors.
|
||||
Statements.SyntheticEmptyBlock.Create(Context, this, 0, Location);
|
||||
}
|
||||
|
||||
if (Symbol.IsImplicitlyDeclared)
|
||||
{
|
||||
var lineCounts = new LineCounts() { Total = 2, Code = 1, Comment = 0 };
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Semmle.Extraction.Entities;
|
||||
using Semmle.Extraction.Kinds;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities.Statements
|
||||
{
|
||||
internal class SyntheticEmptyBlock : Statement<BlockSyntax>
|
||||
{
|
||||
private SyntheticEmptyBlock(Context cx, BlockSyntax block, IStatementParentEntity parent, int child, Location location)
|
||||
: base(cx, block, StmtKind.BLOCK, parent, child, location) { }
|
||||
|
||||
public static SyntheticEmptyBlock Create(Context cx, IStatementParentEntity parent, int child, Location location)
|
||||
{
|
||||
var block = SyntaxFactory.Block();
|
||||
var ret = new SyntheticEmptyBlock(cx, block, parent, child, location);
|
||||
ret.TryPopulate();
|
||||
return ret;
|
||||
}
|
||||
|
||||
protected override void PopulateStatement(TextWriter trapFile) { }
|
||||
}
|
||||
}
|
||||
@@ -416,7 +416,9 @@ class InstanceConstructor extends Constructor {
|
||||
*/
|
||||
class PrimaryConstructor extends Constructor {
|
||||
PrimaryConstructor() {
|
||||
not this.hasBody() and
|
||||
// In the extractor we use the constructor location as the location for the
|
||||
// synthesized empty body of the constructor.
|
||||
this.getLocation() = this.getBody().getLocation() and
|
||||
this.getDeclaringType().fromSource() and
|
||||
this.fromSource()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user