Skip to content

Commit ea4d475

Browse files
authored
Merge pull request #20581 from michaelnebel/csharp/reducetyplocationtuples
C#: Reduce location tuple extraction for named types.
2 parents b0c8fcd + 584d8c5 commit ea4d475

File tree

21 files changed

+107
-18
lines changed

21 files changed

+107
-18
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ public override Microsoft.CodeAnalysis.Location? ReportingLocation
222222

223223
if (Symbol.IsImplicitlyDeclared)
224224
{
225-
return ContainingType!.ReportingLocation;
225+
var best = Symbol.Locations.Where(l => l.IsInSource).BestOrDefault();
226+
return best ?? ContainingType!.ReportingLocation;
226227
}
227228

228229
return Symbol.ContainingType.Locations.FirstOrDefault();

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,18 @@ public override IEnumerable<Location> Locations
111111
}
112112
}
113113

114-
private static IEnumerable<Microsoft.CodeAnalysis.Location> GetLocations(INamedTypeSymbol type)
114+
private IEnumerable<Microsoft.CodeAnalysis.Location> GetLocations(INamedTypeSymbol type)
115115
{
116-
return type.Locations
117-
.Where(l => l.IsInMetadata)
118-
.Concat(type.DeclaringSyntaxReferences
116+
var metadataLocations = type.Locations
117+
.Where(l => l.IsInMetadata);
118+
var sourceLocations = type.DeclaringSyntaxReferences
119119
.Select(loc => loc.GetSyntax())
120120
.OfType<CSharpSyntaxNode>()
121121
.Select(l => l.FixedLocation())
122-
);
122+
.Where(Context.IsLocationInContext);
123+
124+
return metadataLocations
125+
.Concat(sourceLocations);
123126
}
124127

125128
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).BestOrDefault();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ public override void Populate(TextWriter trapFile)
2626
var parentNs = Namespace.Create(Context, Symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : Symbol.ContainingNamespace);
2727
trapFile.parent_namespace(this, parentNs);
2828

29-
foreach (var l in Symbol.Locations)
29+
if (Context.ExtractLocation(Symbol))
3030
{
31-
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
31+
foreach (var l in Symbol.Locations)
32+
{
33+
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
34+
}
3235
}
3336

3437
if (IsSourceDeclaration)

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

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

557+
public bool IsLocationInContext(Location location) =>
558+
location.SourceTree == SourceTree;
559+
557560
/// <summary>
558561
/// Runs the given action <paramref name="a"/>, guarding for trap duplication
559562
/// based on key <paramref name="key"/>.
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 named types (classes, structs, etc.) 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/lib/semmle/code/csharp/Type.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ class NestedType extends ValueOrRefType {
394394
NestedType() { nested_types(this, _, _) }
395395

396396
override ValueOrRefType getDeclaringType() { nested_types(this, result, _) }
397+
398+
override Location getALocation() { type_location(this.getUnboundDeclaration(), result) }
397399
}
398400

399401
/**

csharp/ql/test/library-tests/csharp8/NullableRefTypes.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ returnTypes
178178
| NullableRefTypes.cs:51:12:51:15 | Q<MyClass> | object |
179179
| NullableRefTypes.cs:51:12:51:15 | Q<MyClass> | object! |
180180
| NullableRefTypes.cs:51:12:51:15 | Q`1 | object! |
181-
| NullableRefTypes.cs:54:11:54:33 | Generic | Void! |
182-
| NullableRefTypes.cs:58:11:58:26 | Generic2 | Void! |
181+
| NullableRefTypes.cs:54:11:54:17 | Generic | Void! |
182+
| NullableRefTypes.cs:58:11:58:18 | Generic2 | Void! |
183183
| NullableRefTypes.cs:67:10:67:21 | GenericFn<MyClass> | Void |
184184
| NullableRefTypes.cs:67:10:67:21 | GenericFn<MyClass> | Void! |
185185
| NullableRefTypes.cs:67:10:67:21 | GenericFn`1 | Void! |
@@ -271,8 +271,8 @@ expressionTypes
271271
| NullableRefTypes.cs:40:26:40:30 | ref ... | MyClass |
272272
| NullableRefTypes.cs:40:30:40:30 | access to local variable b | MyClass? |
273273
| NullableRefTypes.cs:51:44:51:47 | null | null |
274-
| NullableRefTypes.cs:54:11:54:33 | call to constructor Object | object |
275-
| NullableRefTypes.cs:58:11:58:26 | call to constructor Object | object |
274+
| NullableRefTypes.cs:54:11:54:17 | call to constructor Object | object |
275+
| NullableRefTypes.cs:58:11:58:18 | call to constructor Object | object |
276276
| NullableRefTypes.cs:73:18:73:18 | access to local variable x | MyClass! |
277277
| NullableRefTypes.cs:73:18:73:25 | MyClass x = ... | MyClass! |
278278
| NullableRefTypes.cs:73:22:73:25 | null | null |

csharp/ql/test/library-tests/csharp9/withExpr.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ withExpr
44
| Record.cs:77:21:77:31 | ... with { ... } | Person1 | Record.cs:77:21:77:22 | access to local variable p1 | Record.cs:77:29:77:31 | { ..., ... } | Person1.<Clone>$() |
55
| Record.cs:84:16:84:33 | ... with { ... } | R1 | Record.cs:84:16:84:16 | access to local variable b | Record.cs:84:23:84:33 | { ..., ... } | R1.<Clone>$() |
66
withTarget
7-
| Record.cs:75:18:75:47 | ... with { ... } | Record.cs:27:1:27:57 | <Clone>$ | Record.cs:27:1:27:57 | Person1 |
8-
| Record.cs:76:18:76:81 | ... with { ... } | Record.cs:29:1:30:35 | <Clone>$ | Record.cs:29:1:30:35 | Teacher1 |
9-
| Record.cs:77:21:77:31 | ... with { ... } | Record.cs:27:1:27:57 | <Clone>$ | Record.cs:27:1:27:57 | Person1 |
10-
| Record.cs:84:16:84:33 | ... with { ... } | Record.cs:54:1:54:39 | <Clone>$ | Record.cs:54:1:54:39 | R1 |
7+
| Record.cs:75:18:75:47 | ... with { ... } | Record.cs:27:1:27:57 | <Clone>$ | Record.cs:27:15:27:21 | Person1 |
8+
| Record.cs:76:18:76:81 | ... with { ... } | Record.cs:29:1:30:35 | <Clone>$ | Record.cs:29:15:29:22 | Teacher1 |
9+
| Record.cs:77:21:77:31 | ... with { ... } | Record.cs:27:1:27:57 | <Clone>$ | Record.cs:27:15:27:21 | Person1 |
10+
| Record.cs:84:16:84:33 | ... with { ... } | Record.cs:54:1:54:39 | <Clone>$ | Record.cs:54:24:54:25 | R1 |
1111
cloneOverrides
1212
| Person1.<Clone>$() | Student1.<Clone>$() |
1313
| Person1.<Clone>$() | Teacher1.<Clone>$() |

csharp/ql/test/library-tests/expressions/ConstructorInitializers.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
| file://:0:0:0:0 | TestCreations | expressions.cs:383:18:383:30 | call to constructor Object | file://:0:0:0:0 | Object |
2828
| file://:0:0:0:0 | TestUnaryOperator | expressions.cs:292:11:292:27 | call to constructor Object | file://:0:0:0:0 | Object |
2929
| file://:0:0:0:0 | TupleExprs | expressions.cs:501:11:501:20 | call to constructor Object | file://:0:0:0:0 | Object |
30-
| file://:0:0:0:0 | X | expressions.cs:108:15:108:18 | call to constructor Object | file://:0:0:0:0 | Object |
30+
| file://:0:0:0:0 | X | expressions.cs:108:15:108:15 | call to constructor Object | file://:0:0:0:0 | Object |
3131
| file://:0:0:0:0 | X | expressions.cs:216:18:216:18 | call to constructor Object | file://:0:0:0:0 | Object |
32-
| file://:0:0:0:0 | Y | expressions.cs:104:15:104:21 | call to constructor Object | file://:0:0:0:0 | Object |
32+
| file://:0:0:0:0 | Y | expressions.cs:104:15:104:15 | call to constructor Object | file://:0:0:0:0 | Object |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public abstract class Base<T>
2+
{
3+
public void M() { }
4+
5+
public class InnerBase { }
6+
}

0 commit comments

Comments
 (0)