mirror of
https://github.com/github/codeql.git
synced 2025-12-16 16:53:25 +01:00
C#: Avoid extracting duplicate type locations.
This commit is contained in:
@@ -111,15 +111,18 @@ namespace Semmle.Extraction.CSharp.Entities
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<Microsoft.CodeAnalysis.Location> GetLocations(INamedTypeSymbol type)
|
||||
private IEnumerable<Microsoft.CodeAnalysis.Location> GetLocations(INamedTypeSymbol type)
|
||||
{
|
||||
return type.Locations
|
||||
.Where(l => l.IsInMetadata)
|
||||
.Concat(type.DeclaringSyntaxReferences
|
||||
var metadataLocations = type.Locations
|
||||
.Where(l => l.IsInMetadata);
|
||||
var sourceLocations = type.DeclaringSyntaxReferences
|
||||
.Select(loc => loc.GetSyntax())
|
||||
.OfType<CSharpSyntaxNode>()
|
||||
.Select(l => l.FixedLocation())
|
||||
);
|
||||
.Where(Context.IsLocationInContext);
|
||||
|
||||
return metadataLocations
|
||||
.Concat(sourceLocations);
|
||||
}
|
||||
|
||||
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).BestOrDefault();
|
||||
|
||||
@@ -26,9 +26,12 @@ namespace Semmle.Extraction.CSharp.Entities
|
||||
var parentNs = Namespace.Create(Context, Symbol.TypeParameterKind == TypeParameterKind.Method ? Context.Compilation.GlobalNamespace : Symbol.ContainingNamespace);
|
||||
trapFile.parent_namespace(this, parentNs);
|
||||
|
||||
foreach (var l in Symbol.Locations)
|
||||
if (Context.ExtractLocation(Symbol))
|
||||
{
|
||||
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
|
||||
foreach (var l in Symbol.Locations)
|
||||
{
|
||||
WriteLocationToTrap(trapFile.type_location, this, Context.CreateLocation(l));
|
||||
}
|
||||
}
|
||||
|
||||
if (IsSourceDeclaration)
|
||||
|
||||
@@ -554,6 +554,9 @@ namespace Semmle.Extraction.CSharp
|
||||
SymbolEqualityComparer.Default.Equals(symbol, symbol.OriginalDefinition) &&
|
||||
scope.InScope(symbol);
|
||||
|
||||
public bool IsLocationInContext(Location location) =>
|
||||
location.SourceTree == SourceTree;
|
||||
|
||||
/// <summary>
|
||||
/// Runs the given action <paramref name="a"/>, guarding for trap duplication
|
||||
/// based on key <paramref name="key"/>.
|
||||
|
||||
Reference in New Issue
Block a user