Skip to content

Commit 14de39a

Browse files
committed
C#: Also add synthetic bodies and inititializers for default constructors.
1 parent 9ecac04 commit 14de39a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ 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)
35+
if (MakeSynthetic)
3636
{
37-
// Create a synthetic empty body for primary constructors.
37+
// Create a synthetic empty body for primary and default constructors.
3838
Statements.SyntheticEmptyBlock.Create(Context, this, 0, Location);
3939
}
4040

@@ -49,9 +49,9 @@ public override void Populate(TextWriter trapFile)
4949
protected override void ExtractInitializers(TextWriter trapFile)
5050
{
5151
// Do not extract initializers for constructed types.
52-
// Only extract initializers for constructors with a body and primary constructors.
53-
if (Block is null && ExpressionBody is null && !IsPrimary ||
54-
!IsSourceDeclaration)
52+
// Extract initializers for constructors with a body, primary constructors
53+
// and default constructors for classes and structs declared in source code.
54+
if (Block is null && ExpressionBody is null && !MakeSynthetic)
5555
{
5656
return;
5757
}
@@ -161,6 +161,16 @@ private void ExtractSourceInitializer(TextWriter trapFile, ITypeSymbol? type, IM
161161

162162
private bool IsPrimary => PrimaryConstructorSyntax is not null;
163163

164+
// This is a default constructor in a class or struct declared in source.
165+
private bool IsDefault =>
166+
Symbol.IsImplicitlyDeclared &&
167+
Symbol.ContainingType.FromSource() &&
168+
Symbol.ContainingType.TypeKind is TypeKind.Class or TypeKind.Struct &&
169+
Symbol.ContainingType.IsSourceDeclaration() &&
170+
!Symbol.ContainingType.IsAnonymousType;
171+
172+
private bool MakeSynthetic => IsPrimary || IsDefault;
173+
164174
[return: NotNullIfNotNull(nameof(constructor))]
165175
public static new Constructor? Create(Context cx, IMethodSymbol? constructor)
166176
{

0 commit comments

Comments
 (0)