From e15a47d58ce1e4de38998dad0c994a9856b70d49 Mon Sep 17 00:00:00 2001 From: Michael Nebel Date: Tue, 25 Jun 2024 15:50:44 +0200 Subject: [PATCH] C#: Update the extractor to use the BestOrDefault extension method to choose between multiple locations. --- .../Semmle.Extraction.CSharp/Entities/CachedSymbol.cs | 4 ++-- .../Semmle.Extraction.CSharp/Entities/Constructor.cs | 1 - .../extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs | 2 +- csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs | 2 +- .../extractor/Semmle.Extraction.CSharp/Entities/Property.cs | 2 +- .../Semmle.Extraction.CSharp/Entities/Types/DynamicType.cs | 2 +- .../Semmle.Extraction.CSharp/Entities/Types/NamedType.cs | 2 +- .../Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs | 2 +- .../Semmle.Extraction.CSharp/Populators/Locations.cs | 2 +- csharp/extractor/Semmle.Extraction/Context.cs | 2 +- csharp/extractor/Semmle.Extraction/InternalError.cs | 2 +- csharp/extractor/Semmle.Extraction/Message.cs | 2 +- 12 files changed, 12 insertions(+), 13 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs index 5c72aa4d90e..8dd3694a715 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/CachedSymbol.cs @@ -82,12 +82,12 @@ namespace Semmle.Extraction.CSharp.Entities /// The location which is stored in the database and is used when highlighting source code. /// It's generally short, e.g. a method name. /// - public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.FirstOrDefault(); + public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.BestOrDefault(); /// /// The full text span of the entity, e.g. for binding comments. /// - public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.FirstOrDefault(); + public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.BestOrDefault(); public virtual IEnumerable Locations { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs index 74c4c09c0f9..047a7b68ae6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Constructor.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs index eb6def9aebc..7fdd9168dca 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Conversion.cs @@ -24,7 +24,7 @@ namespace Semmle.Extraction.CSharp.Entities .OfType() .Select(s => s.FixedLocation()) .Concat(Symbol.Locations) - .FirstOrDefault(); + .BestOrDefault(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs index 1c41974a3a2..9ab2514918b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Indexer.cs @@ -93,7 +93,7 @@ namespace Semmle.Extraction.CSharp.Entities .OfType() .Select(s => s.GetLocation()) .Concat(Symbol.Locations) - .First(); + .Best(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs index 07e74e82164..99f64e46c7b 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs @@ -111,7 +111,7 @@ namespace Semmle.Extraction.CSharp.Entities .OfType() .Select(s => s.GetLocation()) .Concat(Symbol.Locations) - .First(); + .Best(); } } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/DynamicType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/DynamicType.cs index 791a1be3e45..5a840e3b9ef 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/DynamicType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/DynamicType.cs @@ -11,7 +11,7 @@ namespace Semmle.Extraction.CSharp.Entities public static DynamicType Create(Context cx, IDynamicTypeSymbol type) => DynamicTypeFactory.Instance.CreateEntityFromSymbol(cx, type); - public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.FirstOrDefault(); + public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.BestOrDefault(); public override void Populate(TextWriter trapFile) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs index 5b8eb20bb1a..75a931e2266 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Entities/Types/NamedType.cs @@ -124,7 +124,7 @@ namespace Semmle.Extraction.CSharp.Entities ); } - public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).FirstOrDefault(); + public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).BestOrDefault(); private bool IsAnonymousType() => Symbol.IsAnonymousType || Symbol.Name.Contains("__AnonymousType"); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs b/csharp/extractor/Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs index 417b45c37bb..cbed64db3a2 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Extractor/TracingAnalyser.cs @@ -130,7 +130,7 @@ namespace Semmle.Extraction.CSharp return Path.ChangeExtension(entryPointFile, ".exe"); } - var entryPointFilename = entry.Locations.First().SourceTree!.FilePath; + var entryPointFilename = entry.Locations.Best().SourceTree!.FilePath; return Path.ChangeExtension(entryPointFilename, ".exe"); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs index befeabba24e..885a19158eb 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs @@ -107,7 +107,7 @@ namespace Semmle.Extraction.CSharp.Populators { return symbol.DeclaringSyntaxReferences.Any() ? symbol.DeclaringSyntaxReferences.First().GetSyntax().FixedLocation() : - symbol.Locations.First(); + symbol.Locations.Best(); } } } diff --git a/csharp/extractor/Semmle.Extraction/Context.cs b/csharp/extractor/Semmle.Extraction/Context.cs index c83a79fbddc..a1a43034c0c 100644 --- a/csharp/extractor/Semmle.Extraction/Context.cs +++ b/csharp/extractor/Semmle.Extraction/Context.cs @@ -379,7 +379,7 @@ namespace Semmle.Extraction { if (!(optionalSymbol is null)) { - ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.FirstOrDefault())); + ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.BestOrDefault())); } else if (!(optionalEntity is null)) { diff --git a/csharp/extractor/Semmle.Extraction/InternalError.cs b/csharp/extractor/Semmle.Extraction/InternalError.cs index 7ab8050bab6..f162316618e 100644 --- a/csharp/extractor/Semmle.Extraction/InternalError.cs +++ b/csharp/extractor/Semmle.Extraction/InternalError.cs @@ -13,7 +13,7 @@ namespace Semmle.Extraction { Text = msg; EntityText = symbol.ToString() ?? ""; - Location = symbol.Locations.FirstOrDefault(); + Location = symbol.Locations.BestOrDefault(); } public InternalError(SyntaxNode node, string msg) diff --git a/csharp/extractor/Semmle.Extraction/Message.cs b/csharp/extractor/Semmle.Extraction/Message.cs index 34dd31d2ac4..7239c5880cb 100644 --- a/csharp/extractor/Semmle.Extraction/Message.cs +++ b/csharp/extractor/Semmle.Extraction/Message.cs @@ -27,7 +27,7 @@ namespace Semmle.Extraction public static Message Create(Context cx, string text, ISymbol symbol, string? stackTrace = null, Severity severity = Severity.Error) { - return new Message(text, symbol.ToString(), cx.CreateLocation(symbol.Locations.FirstOrDefault()), stackTrace, severity); + return new Message(text, symbol.ToString(), cx.CreateLocation(symbol.Locations.BestOrDefault()), stackTrace, severity); } public static Message Create(Context cx, string text, SyntaxNode node, string? stackTrace = null, Severity severity = Severity.Error)