Skip to content

Commit 68b52e8

Browse files
committed
C#: Only extract locations in context.
1 parent f78935e commit 68b52e8

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public override void Populate(TextWriter trapFile)
8181
}
8282

8383
// Class location
84-
if ((!Symbol.IsGenericType || Symbol.IsReallyUnbound()) && Context.ExtractLocation(Symbol))
84+
if (!Symbol.IsGenericType || Symbol.IsReallyUnbound())
8585
{
8686
WriteLocationsToTrap(trapFile.type_location, this, Locations);
8787
}
@@ -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/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)