Skip to content

Commit 4cc6a07

Browse files
authored
Merge pull request #20593 from michaelnebel/csharp/reducetypeparameterandtuplelocations
C#: Reduce Type Parameter- and Tuple type location extraction.
2 parents 28fe20e + cdfa586 commit 4cc6a07

File tree

9 files changed

+73
-7
lines changed

9 files changed

+73
-7
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* 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.

csharp/ql/test/library-tests/locations/Base.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ public void M() { }
44

55
public class InnerBase { }
66
}
7+
8+
public abstract class Base2<T> { }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
public partial class Multiple { }
2+
3+
public partial class MultipleGeneric<S> { }
4+
5+
public class Multiple1Specific
6+
{
7+
public static (int, string) M()
8+
{
9+
(int, string) x = (0, "");
10+
(int, int) y = (0, 0);
11+
return x;
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
11
public partial class Multiple { }
2+
3+
public partial class MultipleGeneric<S> { }
4+
5+
public class Multiple2Specific
6+
{
7+
public void M()
8+
{
9+
(int, string) z = (0, "");
10+
}
11+
}

csharp/ql/test/library-tests/locations/locations.expected

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ member_locations
2828
| 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 |
2929
| 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 |
3030
| 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 |
31+
| 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 |
32+
| 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 |
3133
| 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 |
3234
accessor_location
3335
| 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
6769
| Base.cs:1:28:1:28 | T | Base.cs:1:28:1:28 | Base.cs:1:28:1:28 |
6870
| Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
6971
| Base.cs:5:18:5:26 | InnerBase | Base.cs:5:18:5:26 | Base.cs:5:18:5:26 |
72+
| Base.cs:8:23:8:30 | Base2`1 | Base.cs:8:23:8:30 | Base.cs:8:23:8:30 |
73+
| Base.cs:8:29:8:29 | T | Base.cs:8:29:8:29 | Base.cs:8:29:8:29 |
7074
| C.cs:3:7:3:7 | C | C.cs:3:7:3:7 | C.cs:3:7:3:7 |
7175
| Multiple1.cs:1:22:1:29 | Multiple | Multiple1.cs:1:22:1:29 | Multiple1.cs:1:22:1:29 |
7276
| Multiple1.cs:1:22:1:29 | Multiple | Multiple2.cs:1:22:1:29 | Multiple2.cs:1:22:1:29 |
77+
| Multiple1.cs:3:22:3:39 | MultipleGeneric`1 | Multiple1.cs:3:22:3:39 | Multiple1.cs:3:22:3:39 |
78+
| Multiple1.cs:3:22:3:39 | MultipleGeneric`1 | Multiple2.cs:3:22:3:39 | Multiple2.cs:3:22:3:39 |
79+
| Multiple1.cs:3:38:3:38 | S | Multiple1.cs:3:38:3:38 | Multiple1.cs:3:38:3:38 |
80+
| Multiple1.cs:5:14:5:30 | Multiple1Specific | Multiple1.cs:5:14:5:30 | Multiple1.cs:5:14:5:30 |
81+
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple1.cs:7:19:7:31 | Multiple1.cs:7:19:7:31 |
82+
| Multiple1.cs:10:9:10:18 | (Int32,Int32) | Multiple1.cs:10:9:10:18 | Multiple1.cs:10:9:10:18 |
7383
| Multiple2.cs:1:22:1:29 | Multiple | Multiple1.cs:1:22:1:29 | Multiple1.cs:1:22:1:29 |
7484
| Multiple2.cs:1:22:1:29 | Multiple | Multiple2.cs:1:22:1:29 | Multiple2.cs:1:22:1:29 |
85+
| Multiple2.cs:3:22:3:39 | MultipleGeneric`1 | Multiple1.cs:3:22:3:39 | Multiple1.cs:3:22:3:39 |
86+
| Multiple2.cs:3:22:3:39 | MultipleGeneric`1 | Multiple2.cs:3:22:3:39 | Multiple2.cs:3:22:3:39 |
87+
| Multiple2.cs:5:14:5:30 | Multiple2Specific | Multiple2.cs:5:14:5:30 | Multiple2.cs:5:14:5:30 |
7588
| Sub.cs:1:14:1:16 | Sub | Sub.cs:1:14:1:16 | Sub.cs:1:14:1:16 |
7689
calltype_location
7790
| 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
8194
| 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 |
8295
| 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 |
8396
| 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 |
97+
typeparameter_location
98+
| A.cs:3:25:3:25 | T | A.cs:3:25:3:25 | A.cs:3:25:3:25 |
99+
| Base.cs:1:28:1:28 | T | Base.cs:1:28:1:28 | Base.cs:1:28:1:28 |
100+
| Base.cs:8:29:8:29 | T | Base.cs:8:29:8:29 | Base.cs:8:29:8:29 |
101+
| Multiple1.cs:3:38:3:38 | S | Multiple1.cs:3:38:3:38 | Multiple1.cs:3:38:3:38 |
102+
| Multiple1.cs:3:38:3:38 | S | Multiple2.cs:3:38:3:38 | Multiple2.cs:3:38:3:38 |
103+
tupletype_location
104+
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple1.cs:7:19:7:31 | Multiple1.cs:7:19:7:31 |
105+
| Multiple1.cs:7:19:7:31 | (Int32,String) | Multiple2.cs:9:9:9:21 | Multiple2.cs:9:9:9:21 |
106+
| Multiple1.cs:10:9:10:18 | (Int32,Int32) | Multiple1.cs:10:9:10:18 | Multiple1.cs:10:9:10:18 |

csharp/ql/test/library-tests/locations/locations.ql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ query predicate member_locations(Type t, Member m, SourceLocation l) {
44
t = m.getDeclaringType() and
55
l = m.getLocation() and
66
not l instanceof EmptyLocation and
7-
not m instanceof Constructor
7+
not m instanceof Constructor and
8+
t.fromSource()
89
}
910

1011
query predicate accessor_location(Type t, Accessor a, SourceLocation l) {
@@ -21,3 +22,7 @@ query predicate calltype_location(Call call, Type t, SourceLocation l) {
2122
t = call.getType() and
2223
l = t.getALocation()
2324
}
25+
26+
query predicate typeparameter_location(TypeParameter tp, SourceLocation l) { tp.getALocation() = l }
27+
28+
query predicate tupletype_location(TupleType tt, SourceLocation l) { tt.getALocation() = l }

0 commit comments

Comments
 (0)