Skip to content

Commit

Permalink
C#: Use property- and indexer implementation location and extract the…
Browse files Browse the repository at this point in the history
… accessor implementations instead of declarations.
  • Loading branch information
michaelnebel committed Jan 22, 2025
1 parent ab4deb3 commit 5eaa59b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ protected Accessor(Context cx, IMethodSymbol init, IPropertySymbol property)
return props.SingleOrDefault();
}

public override bool NeedsPopulation =>
base.NeedsPopulation &&
!Symbol.IsPartialDefinition; // Accessors always have an implementing declaration as well.

public override void Populate(TextWriter trapFile)
{
PopulateMethod(trapFile);
Expand Down
8 changes: 4 additions & 4 deletions csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public override void Populate(TextWriter trapFile)
foreach (var l in Locations)
trapFile.indexer_location(this, l);

var getter = Symbol.GetMethod;
var setter = Symbol.SetMethod;
var getter = BodyDeclaringSymbol.GetMethod;
var setter = BodyDeclaringSymbol.SetMethod;

if (getter is null && setter is null)
Context.ModelError(Symbol, "No indexer accessor defined");

if (!(getter is null))
if (getter is not null)
Method.Create(Context, getter);

if (!(setter is null))
if (setter is not null)
Method.Create(Context, setter);

for (var i = 0; i < Symbol.Parameters.Length; ++i)
Expand Down
12 changes: 8 additions & 4 deletions csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ protected Property(Context cx, IPropertySymbol init)

private Type Type => type.Value;

protected override IPropertySymbol BodyDeclaringSymbol => Symbol.PartialImplementationPart ?? Symbol;

public override Microsoft.CodeAnalysis.Location? ReportingLocation => BodyDeclaringSymbol.Locations.BestOrDefault();

public override void WriteId(EscapingTextWriter trapFile)
{
trapFile.WriteSubId(Type);
Expand All @@ -43,13 +47,13 @@ public override void Populate(TextWriter trapFile)
var type = Type;
trapFile.properties(this, Symbol.GetName(), ContainingType!, type.TypeRef, Create(Context, Symbol.OriginalDefinition));

var getter = Symbol.GetMethod;
var setter = Symbol.SetMethod;
var getter = BodyDeclaringSymbol.GetMethod;
var setter = BodyDeclaringSymbol.SetMethod;

if (!(getter is null))
if (getter is not null)
Method.Create(Context, getter);

if (!(setter is null))
if (setter is not null)
Method.Create(Context, setter);

var declSyntaxReferences = IsSourceDeclaration ?
Expand Down

0 comments on commit 5eaa59b

Please sign in to comment.