Skip to content

Commit e1f65d1

Browse files
authored
Merge pull request github#16836 from michaelnebel/csharp/bestlocation
C#: Be more consistent when picking between locations.
2 parents c4cc30f + e258d9f commit e1f65d1

File tree

14 files changed

+58
-15
lines changed

14 files changed

+58
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ protected void ExtractCompilerGenerated(TextWriter trapFile)
8282
/// The location which is stored in the database and is used when highlighting source code.
8383
/// It's generally short, e.g. a method name.
8484
/// </summary>
85-
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.FirstOrDefault();
85+
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Symbol.Locations.BestOrDefault();
8686

8787
/// <summary>
8888
/// The full text span of the entity, e.g. for binding comments.
8989
/// </summary>
90-
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.FirstOrDefault();
90+
public virtual Microsoft.CodeAnalysis.Location? FullLocation => Symbol.Locations.BestOrDefault();
9191

9292
public virtual IEnumerable<Extraction.Entities.Location> Locations
9393
{

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.Diagnostics.CodeAnalysis;
43
using System.IO;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public override Microsoft.CodeAnalysis.Location? ReportingLocation
2424
.OfType<ConversionOperatorDeclarationSyntax>()
2525
.Select(s => s.FixedLocation())
2626
.Concat(Symbol.Locations)
27-
.FirstOrDefault();
27+
.BestOrDefault();
2828
}
2929
}
3030

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public override Microsoft.CodeAnalysis.Location FullLocation
9393
.OfType<IndexerDeclarationSyntax>()
9494
.Select(s => s.GetLocation())
9595
.Concat(Symbol.Locations)
96-
.First();
96+
.Best();
9797
}
9898
}
9999

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public override Microsoft.CodeAnalysis.Location FullLocation
111111
.OfType<PropertyDeclarationSyntax>()
112112
.Select(s => s.GetLocation())
113113
.Concat(Symbol.Locations)
114-
.First();
114+
.Best();
115115
}
116116
}
117117

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private DynamicType(Context cx, IDynamicTypeSymbol init)
1111

1212
public static DynamicType Create(Context cx, IDynamicTypeSymbol type) => DynamicTypeFactory.Instance.CreateEntityFromSymbol(cx, type);
1313

14-
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.FirstOrDefault();
14+
public override Microsoft.CodeAnalysis.Location? ReportingLocation => Context.Compilation.ObjectType.Locations.BestOrDefault();
1515

1616
public override void Populate(TextWriter trapFile)
1717
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public override IEnumerable<Extraction.Entities.Location> Locations
124124
);
125125
}
126126

127-
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).FirstOrDefault();
127+
public override Microsoft.CodeAnalysis.Location? ReportingLocation => GetLocations(Symbol).BestOrDefault();
128128

129129
private bool IsAnonymousType() => Symbol.IsAnonymousType || Symbol.Name.Contains("__AnonymousType");
130130

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private static string GetOutputName(CSharpCompilation compilation,
130130
return Path.ChangeExtension(entryPointFile, ".exe");
131131
}
132132

133-
var entryPointFilename = entry.Locations.First().SourceTree!.FilePath;
133+
var entryPointFilename = entry.Locations.Best().SourceTree!.FilePath;
134134
return Path.ChangeExtension(entryPointFilename, ".exe");
135135
}
136136

csharp/extractor/Semmle.Extraction.CSharp/Populators/Locations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static Location GetSymbolLocation(this ISymbol symbol)
107107
{
108108
return symbol.DeclaringSyntaxReferences.Any() ?
109109
symbol.DeclaringSyntaxReferences.First().GetSyntax().FixedLocation() :
110-
symbol.Locations.First();
110+
symbol.Locations.Best();
111111
}
112112
}
113113
}

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private void ExtractionError(string message, ISymbol? optionalSymbol, Entity opt
379379
{
380380
if (!(optionalSymbol is null))
381381
{
382-
ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.FirstOrDefault()));
382+
ExtractionError(message, optionalSymbol.ToDisplayString(), CreateLocation(optionalSymbol.Locations.BestOrDefault()));
383383
}
384384
else if (!(optionalEntity is null))
385385
{

0 commit comments

Comments
 (0)