Merge pull request #20593 from michaelnebel/csharp/reducetypeparameterandtuplelocations

C#: Reduce Type Parameter- and Tuple type location extraction.
This commit is contained in:
Michael Nebel
2025-10-08 11:36:32 +02:00
committed by GitHub
9 changed files with 73 additions and 7 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;

View File

@@ -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.

View File

@@ -4,3 +4,5 @@ public abstract class Base<T>
public class InnerBase { }
}
public abstract class Base2<T> { }

View File

@@ -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;
}
}

View File

@@ -1 +1,11 @@
public partial class Multiple { }
public partial class MultipleGeneric<S> { }
public class Multiple2Specific
{
public void M()
{
(int, string) z = (0, "");
}
}

View File

@@ -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 |
@@ -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 |
@@ -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 |

View File

@@ -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) {
@@ -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 }