C#: Update the extractor to use the BestOrDefault extension method to choose between multiple locations.

This commit is contained in:
Michael Nebel
2024-06-25 15:50:44 +02:00
parent dd65d960be
commit e15a47d58c
12 changed files with 12 additions and 13 deletions

View File

@@ -82,12 +82,12 @@ namespace Semmle.Extraction.CSharp.Entities
/// The location which is stored in the database and is used when highlighting source code.
/// It's generally short, e.g. a method name.
/// </summary>
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.FirstOrDefault();
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.BestOrDefault();
/// <summary>
/// The full text span of the entity, e.g. for binding comments.
/// </summary>
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.FirstOrDefault();
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.BestOrDefault();
public virtual IEnumerable<Extraction.Entities.Location> Locations
{

View File

@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;

View File

@@ -24,7 +24,7 @@ namespace Semmle.Extraction.CSharp.Entities
.OfType<ConversionOperatorDeclarationSyntax>()
.Select(s => s.FixedLocation())
.Concat(Symbol.Locations)
.FirstOrDefault();
.BestOrDefault();
}
}

View File

@@ -93,7 +93,7 @@ namespace Semmle.Extraction.CSharp.Entities
.OfType<IndexerDeclarationSyntax>()
.Select(s => s.GetLocation())
.Concat(Symbol.Locations)
.First();
.Best();
}
}

View File

@@ -111,7 +111,7 @@ namespace Semmle.Extraction.CSharp.Entities
.OfType<PropertyDeclarationSyntax>()
.Select(s => s.GetLocation())
.Concat(Symbol.Locations)
.First();
.Best();
}
}

View File

@@ -11,7 +11,7 @@ namespace Semmle.Extraction.CSharp.Entities
public static DynamicType Create(Context cx, IDynamicTypeSymbol type) => DynamicTypeFactory.Instance.CreateEntityFromSymbol(cx, type);
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.FirstOrDefault();
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.BestOrDefault();
public override void Populate(TextWriter trapFile)
{

View File

@@ -124,7 +124,7 @@ namespace Semmle.Extraction.CSharp.Entities
);
}
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).FirstOrDefault();
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).BestOrDefault();
private bool IsAnonymousType() => Symbol.IsAnonymousType || Symbol.Name.Contains("__AnonymousType");

View File

@@ -130,7 +130,7 @@ namespace Semmle.Extraction.CSharp
return Path.ChangeExtension(entryPointFile, ".exe");
}
var entryPointFilename = entry.Locations.First().SourceTree!.FilePath;
var entryPointFilename = entry.Locations.Best().SourceTree!.FilePath;
return Path.ChangeExtension(entryPointFilename, ".exe");
}

View File

@@ -107,7 +107,7 @@ namespace Semmle.Extraction.CSharp.Populators
{
return symbol.DeclaringSyntaxReferences.Any() ?
symbol.DeclaringSyntaxReferences.First().GetSyntax().FixedLocation() :
symbol.Locations.First();
symbol.Locations.Best();
}
}
}

View File

@@ -379,7 +379,7 @@ namespace Semmle.Extraction
{
if (!(optionalSymbol is null))
{
ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.FirstOrDefault()));
ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.BestOrDefault()));
}
else if (!(optionalEntity is null))
{

View File

@@ -13,7 +13,7 @@ namespace Semmle.Extraction
{
Text = msg;
EntityText = symbol.ToString() ?? "";
Location = symbol.Locations.FirstOrDefault();
Location = symbol.Locations.BestOrDefault();
}
public InternalError(SyntaxNode node, string msg)

View File

@@ -27,7 +27,7 @@ namespace Semmle.Extraction
public static Message Create(Context cx, string text, ISymbol symbol, string? stackTrace = null, Severity severity = Severity.Error)
{
return new Message(text, symbol.ToString(), cx.CreateLocation(symbol.Locations.FirstOrDefault()), stackTrace, severity);
return new Message(text, symbol.ToString(), cx.CreateLocation(symbol.Locations.BestOrDefault()), stackTrace, severity);
}
public static Message Create(Context cx, string text, SyntaxNode node, string? stackTrace = null, Severity severity = Severity.Error)