Skip to content

Commit 0cd7c37

Browse files
committed
C#: Avoid extracting duplicate type locations.
1 parent f2b45b8 commit 0cd7c37

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

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"/>.

0 commit comments

Comments
 (0)