C#: Respect the context when extracting locations for type parameters and tuple typles.

This commit is contained in:
Michael Nebel
2025-10-07 15:15:34 +02:00
parent ea4d4751f3
commit f0842e430d
3 changed files with 16 additions and 6 deletions

View File

@@ -54,8 +54,8 @@ namespace Semmle.Extraction.CSharp.Entities
// Note: symbol.Locations seems to be very inconsistent
// about what locations are available for a tuple type.
// Sometimes it's the source code, and sometimes it's empty.
foreach (var l in Symbol.Locations)
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
var locations = Context.GetLocations(Symbol);
WriteLocationsToTrap(trapFile.type_location, this, locations);
}
private readonly Lazy<Field?[]> tupleElementsLazy;

View File

@@ -28,10 +28,8 @@ namespace Semmle.Extraction.CSharp.Entities
if (Context.ExtractLocation(Symbol))
{
foreach (var l in Symbol.Locations)
{
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
}
var locations = Context.GetLocations(Symbol);
WriteLocationsToTrap(trapFile.type_location, this, locations);
}
if (IsSourceDeclaration)

View File

@@ -554,6 +554,18 @@ namespace Semmle.Extraction.CSharp
SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) &&
scope.InScope(symbol);
/// <summary>
/// Gets the locations of the symbol that are either
/// (1) In assemblies.
/// (2) In the current context.
/// </summary>
/// <param name="symbol">The symbol</param>
/// <returns>List of locations</returns>
public IEnumerable<Entities.Location> GetLocations(ISymbol symbol) =>
symbol.Locations
.Where(l => !l.IsInSource || IsLocationInContext(l))
.Select(CreateLocation);
public bool IsLocationInContext(Location location) =>
location.SourceTree == SourceTree;