Skip to content

Commit f0842e4

Browse files
committed
C#: Respect the context when extracting locations for type parameters and tuple typles.
1 parent ea4d475 commit f0842e4

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TupleType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public override void Populate(TextWriter trapFile)
5454
// Note: symbol.Locations seems to be very inconsistent
5555
// about what locations are available for a tuple type.
5656
// Sometimes it's the source code, and sometimes it's empty.
57-
foreach (var l in Symbol.Locations)
58-
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
57+
var locations = Context.GetLocations(Symbol);
58+
WriteLocationsToTrap(trapFile.type_location, this, locations);
5959
}
6060

6161
private readonly Lazy<Field?[]> tupleElementsLazy;

csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/TypeParameter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ public override void Populate(TextWriter trapFile)
2828

2929
if (Context.ExtractLocation(Symbol))
3030
{
31-
foreach (var l in Symbol.Locations)
32-
{
33-
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
34-
}
31+
var locations = Context.GetLocations(Symbol);
32+
WriteLocationsToTrap(trapFile.type_location, this, locations);
3533
}
3634

3735
if (IsSourceDeclaration)

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Context.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,18 @@ public bool ExtractLocation(ISymbol symbol) =>
554554
SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) &&
555555
scope.InScope(symbol);
556556

557+
/// <summary>
558+
/// Gets the locations of the symbol that are either
559+
/// (1) In assemblies.
560+
/// (2) In the current context.
561+
/// </summary>
562+
/// <param name="symbol">The symbol</param>
563+
/// <returns>List of locations</returns>
564+
public IEnumerable<Entities.Location> GetLocations(ISymbol symbol) =>
565+
symbol.Locations
566+
.Where(l => !l.IsInSource || IsLocationInContext(l))
567+
.Select(CreateLocation);
568+
557569
public bool IsLocationInContext(Location location) =>
558570
location.SourceTree == SourceTree;
559571

0 commit comments

Comments
 (0)