Skip to content

Commit 3fab401

Browse files
committed
C#: Streamline the partial implementation for properties and events.
1 parent c0c6f81 commit 3fab401

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

csharp/extractor/Semmle.Extraction.CSharp/CodeAnalysisExtensions/SymbolExtensions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ public static bool IsUnboundGenericType(this INamedTypeSymbol type) =>
731731
public static IMethodSymbol GetBodyDeclaringSymbol(this IMethodSymbol method) =>
732732
method.PartialImplementationPart ?? method;
733733

734+
public static IPropertySymbol GetBodyDeclaringSymbol(this IPropertySymbol property) =>
735+
property.PartialImplementationPart ?? property;
736+
737+
public static IEventSymbol GetBodyDeclaringSymbol(this IEventSymbol symbol) =>
738+
symbol.PartialImplementationPart ?? symbol;
739+
734740
[return: NotNullIfNotNull(nameof(symbol))]
735741
public static IEntity? CreateEntity(this Context cx, ISymbol symbol)
736742
{

csharp/extractor/Semmle.Extraction.CSharp/Entities/Base/CachedSymbol.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ protected void BindComments()
8787
Context.BindComments(this, FullLocation);
8888
}
8989

90-
protected virtual T BodyDeclaringSymbol => Symbol;
91-
9290
private static BlockSyntax? GetBlock(T symbol)
9391
{
9492
return symbol.DeclaringSyntaxReferences
@@ -107,10 +105,10 @@ protected void BindComments()
107105
}
108106

109107
private BlockSyntax? vBlock;
110-
public BlockSyntax? Block => vBlock ??= GetBlock(BodyDeclaringSymbol);
108+
public BlockSyntax? Block => vBlock ??= GetBlock(Symbol);
111109

112110
private ExpressionSyntax? vExpressionBody;
113-
public ExpressionSyntax? ExpressionBody => vExpressionBody ??= GetExpressionBody(BodyDeclaringSymbol);
111+
public ExpressionSyntax? ExpressionBody => vExpressionBody ??= GetExpressionBody(Symbol);
114112

115113
private bool? vHasBody;
116114
public bool HasBody => vHasBody ??= Block is not null || ExpressionBody is not null;

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ internal class Event : CachedSymbol<IEventSymbol>
1111
private Event(Context cx, IEventSymbol init)
1212
: base(cx, init) { }
1313

14-
protected override IEventSymbol BodyDeclaringSymbol => Symbol.PartialImplementationPart ?? Symbol;
15-
16-
public override Microsoft.CodeAnalysis.Location? ReportingLocation => BodyDeclaringSymbol.Locations.BestOrDefault();
17-
1814
public override void WriteId(EscapingTextWriter trapFile)
1915
{
2016
trapFile.WriteSubId(ContainingType!);
@@ -31,8 +27,8 @@ public override void Populate(TextWriter trapFile)
3127
var type = Type.Create(Context, Symbol.Type);
3228
trapFile.events(this, Symbol.GetName(), ContainingType!, type.TypeRef, Create(Context, Symbol.OriginalDefinition));
3329

34-
var adder = BodyDeclaringSymbol.AddMethod;
35-
var remover = BodyDeclaringSymbol.RemoveMethod;
30+
var adder = Symbol.AddMethod;
31+
var remover = Symbol.RemoveMethod;
3632

3733
if (adder is not null)
3834
Method.Create(Context, adder);
@@ -76,7 +72,7 @@ public override void Populate(TextWriter trapFile)
7672
}
7773
}
7874

79-
public static Event Create(Context cx, IEventSymbol symbol) => EventFactory.Instance.CreateEntityFromSymbol(cx, symbol);
75+
public static Event Create(Context cx, IEventSymbol symbol) => EventFactory.Instance.CreateEntityFromSymbol(cx, symbol.GetBodyDeclaringSymbol());
8076

8177
private class EventFactory : CachedEntityFactory<IEventSymbol, Event>
8278
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public override void Populate(TextWriter trapFile)
2020
var type = Type.Create(Context, Symbol.Type);
2121
trapFile.indexers(this, Symbol.GetName(useMetadataName: true), ContainingType!, type.TypeRef, OriginalDefinition);
2222

23-
var getter = BodyDeclaringSymbol.GetMethod;
24-
var setter = BodyDeclaringSymbol.SetMethod;
23+
var getter = Symbol.GetMethod;
24+
var setter = Symbol.SetMethod;
2525

2626
if (getter is null && setter is null)
2727
Context.ModelError(Symbol, "No indexer accessor defined");
@@ -81,7 +81,7 @@ public override void Populate(TextWriter trapFile)
8181
TypeMention.Create(Context, syntax.Type, this, type);
8282
}
8383

84-
public static new Indexer Create(Context cx, IPropertySymbol prop) => IndexerFactory.Instance.CreateEntityFromSymbol(cx, prop);
84+
public static new Indexer Create(Context cx, IPropertySymbol prop) => IndexerFactory.Instance.CreateEntityFromSymbol(cx, prop.GetBodyDeclaringSymbol());
8585

8686
public override void WriteId(EscapingTextWriter trapFile)
8787
{

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ protected Property(Context cx, IPropertySymbol init)
2121

2222
private Type Type => type.Value;
2323

24-
protected override IPropertySymbol BodyDeclaringSymbol => Symbol.PartialImplementationPart ?? Symbol;
25-
26-
public override Microsoft.CodeAnalysis.Location? ReportingLocation => BodyDeclaringSymbol.Locations.BestOrDefault();
27-
2824
public override void WriteId(EscapingTextWriter trapFile)
2925
{
3026
trapFile.WriteSubId(Type);
@@ -46,8 +42,8 @@ public override void Populate(TextWriter trapFile)
4642
var type = Type;
4743
trapFile.properties(this, Symbol.GetName(), ContainingType!, type.TypeRef, Create(Context, Symbol.OriginalDefinition));
4844

49-
var getter = BodyDeclaringSymbol.GetMethod;
50-
var setter = BodyDeclaringSymbol.SetMethod;
45+
var getter = Symbol.GetMethod;
46+
var setter = Symbol.SetMethod;
5147

5248
if (getter is not null)
5349
Method.Create(Context, getter);
@@ -132,7 +128,7 @@ public static Property Create(Context cx, IPropertySymbol prop)
132128
{
133129
var isIndexer = prop.IsIndexer || prop.Parameters.Any();
134130

135-
return isIndexer ? Indexer.Create(cx, prop) : PropertyFactory.Instance.CreateEntityFromSymbol(cx, prop);
131+
return isIndexer ? Indexer.Create(cx, prop) : PropertyFactory.Instance.CreateEntityFromSymbol(cx, prop.GetBodyDeclaringSymbol());
136132
}
137133

138134
private class PropertyFactory : CachedEntityFactory<IPropertySymbol, Property>

0 commit comments

Comments
 (0)