Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public override void Populate(TextWriter trapFile)
// 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ public override void Populate(TextWriter trapFile)

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)
Expand Down
12 changes: 12 additions & 0 deletions csharp/extractor/Semmle.Extraction.CSharp/Extractor/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,18 @@ public bool ExtractLocation(ISymbol symbol) =>
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;

Expand Down
4 changes: 4 additions & 0 deletions csharp/ql/lib/change-notes/2025-10-08-entity-locations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The extraction of location information for type parameters and tuples types has been optimized. Previously, location information was extracted multiple times for each type when it was declared across multiple files. Now, the extraction context is respected during the extraction phase, ensuring locations are only extracted within the appropriate context. This change should be transparent to end-users but may improve extraction performance in some cases.
2 changes: 2 additions & 0 deletions csharp/ql/test/library-tests/locations/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ public void M() { }

public class InnerBase { }
}

public abstract class Base2<T> { }
12 changes: 12 additions & 0 deletions csharp/ql/test/library-tests/locations/Multiple1.cs
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
public partial class Multiple { }

public partial class MultipleGeneric<S> { }

public class Multiple1Specific
{
public static (int, string) M()
{
(int, string) x = (0, "");
(int, int) y = (0, 0);
return x;
}
}
10 changes: 10 additions & 0 deletions csharp/ql/test/library-tests/locations/Multiple2.cs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
public partial class Multiple { }

public partial class MultipleGeneric<S> { }

public class Multiple2Specific
{
public void M()
{
(int, string) z = (0, "");
}
}
23 changes: 23 additions & 0 deletions csharp/ql/test/library-tests/locations/locations.expected
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ member_locations
| Base.cs:1:23:1:29 | Base`1 | Base.cs:3:17:3:17 | M | Base.cs:3:17:3:17 | Base.cs:3:17:3:17 |
| Base.cs:1:23:1:29 | Base`1 | Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
| C.cs:3:7:3:7 | C | C.cs:5:17:5:17 | M | C.cs:5:17:5:17 | C.cs:5:17:5:17 |
| Multiple1.cs:5:14:5:30 | Multiple1Specific | Multiple1.cs:7:33:7:33 | M | Multiple1.cs:7:33:7:33 | Multiple1.cs:7:33:7:33 |
| Multiple2.cs:5:14:5:30 | Multiple2Specific | Multiple2.cs:7:17:7:17 | M | Multiple2.cs:7:17:7:17 | Multiple2.cs:7:17:7:17 |
| Sub.cs:1:14:1:16 | Sub | Sub.cs:3:17:3:20 | SubM | Sub.cs:3:17:3:20 | Sub.cs:3:17:3:20 |
accessor_location
| A.cs:3:23:3:26 | A<Int32> | A.cs:5:30:5:32 | get_Prop | A.cs:5:30:5:32 | A.cs:5:30:5:32 |
Expand Down Expand Up @@ -67,11 +69,22 @@ type_location
| Base.cs:1:28:1:28 | T | Base.cs:1:28:1:28 | Base.cs:1:28:1:28 |
| Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
| Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
| Base.cs:8:23:8:30 | Base2`1 | Base.cs:8:23:8:30 | Base.cs:8:23:8:30 |
| Base.cs:8:29:8:29 | T | Base.cs:8:29:8:29 | Base.cs:8:29:8:29 |
| C.cs:3:7:3:7 | C | C.cs:3:7:3:7 | C.cs:3:7:3:7 |
| Multiple1.cs:1:22:1:29 | Multiple | Multiple1.cs:1:22:1:29 | Multiple1.cs:1:22:1:29 |
| Multiple1.cs:1:22:1:29 | Multiple | Multiple2.cs:1:22:1:29 | Multiple2.cs:1:22:1:29 |
| Multiple1.cs:3:22:3:39 | MultipleGeneric`1 | Multiple1.cs:3:22:3:39 | Multiple1.cs:3:22:3:39 |
| Multiple1.cs:3:22:3:39 | MultipleGeneric`1 | Multiple2.cs:3:22:3:39 | Multiple2.cs:3:22:3:39 |
| Multiple1.cs:3:38:3:38 | S | Multiple1.cs:3:38:3:38 | Multiple1.cs:3:38:3:38 |
| Multiple1.cs:5:14:5:30 | Multiple1Specific | Multiple1.cs:5:14:5:30 | Multiple1.cs:5:14:5:30 |
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple1.cs:7:19:7:31 | Multiple1.cs:7:19:7:31 |
| Multiple1.cs:10:9:10:18 | (Int32,Int32) | Multiple1.cs:10:9:10:18 | Multiple1.cs:10:9:10:18 |
| Multiple2.cs:1:22:1:29 | Multiple | Multiple1.cs:1:22:1:29 | Multiple1.cs:1:22:1:29 |
| Multiple2.cs:1:22:1:29 | Multiple | Multiple2.cs:1:22:1:29 | Multiple2.cs:1:22:1:29 |
| Multiple2.cs:3:22:3:39 | MultipleGeneric`1 | Multiple1.cs:3:22:3:39 | Multiple1.cs:3:22:3:39 |
| Multiple2.cs:3:22:3:39 | MultipleGeneric`1 | Multiple2.cs:3:22:3:39 | Multiple2.cs:3:22:3:39 |
| Multiple2.cs:5:14:5:30 | Multiple2Specific | Multiple2.cs:5:14:5:30 | Multiple2.cs:5:14:5:30 |
| Sub.cs:1:14:1:16 | Sub | Sub.cs:1:14:1:16 | Sub.cs:1:14:1:16 |
calltype_location
| A.cs:12:14:12:15 | call to constructor A | A.cs:3:23:3:26 | A<String> | A.cs:3:23:3:26 | A.cs:3:23:3:26 |
Expand All @@ -81,3 +94,13 @@ calltype_location
| C.cs:9:17:9:24 | object creation of type A2 | A.cs:12:14:12:15 | A2 | A.cs:12:14:12:15 | A.cs:12:14:12:15 |
| Sub.cs:1:14:1:16 | call to constructor Base | Base.cs:1:23:1:29 | Base<Int32> | Base.cs:1:23:1:29 | Base.cs:1:23:1:29 |
| Sub.cs:6:17:6:31 | object creation of type InnerBase | Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
typeparameter_location
| A.cs:3:25:3:25 | T | A.cs:3:25:3:25 | A.cs:3:25:3:25 |
| Base.cs:1:28:1:28 | T | Base.cs:1:28:1:28 | Base.cs:1:28:1:28 |
| Base.cs:8:29:8:29 | T | Base.cs:8:29:8:29 | Base.cs:8:29:8:29 |
| Multiple1.cs:3:38:3:38 | S | Multiple1.cs:3:38:3:38 | Multiple1.cs:3:38:3:38 |
| Multiple1.cs:3:38:3:38 | S | Multiple2.cs:3:38:3:38 | Multiple2.cs:3:38:3:38 |
tupletype_location
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple1.cs:7:19:7:31 | Multiple1.cs:7:19:7:31 |
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple2.cs:9:9:9:21 | Multiple2.cs:9:9:9:21 |
| Multiple1.cs:10:9:10:18 | (Int32,Int32) | Multiple1.cs:10:9:10:18 | Multiple1.cs:10:9:10:18 |
7 changes: 6 additions & 1 deletion csharp/ql/test/library-tests/locations/locations.ql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ query predicate member_locations(Type t, Member m, SourceLocation l) {
t = m.getDeclaringType() and
l = m.getLocation() and
not l instanceof EmptyLocation and
not m instanceof Constructor
not m instanceof Constructor and
t.fromSource()
}

query predicate accessor_location(Type t, Accessor a, SourceLocation l) {
Expand All @@ -21,3 +22,7 @@ query predicate calltype_location(Call call, Type t, SourceLocation l) {
t = call.getType() and
l = t.getALocation()
}

query predicate typeparameter_location(TypeParameter tp, SourceLocation l) { tp.getALocation() = l }

query predicate tupletype_location(TupleType tt, SourceLocation l) { tt.getALocation() = l }