Skip to content

Commit b5aa972

Browse files
authored
Merge pull request #20525 from michaelnebel/csharp/reducelocationtuples
C#: Reduce location tuples.
2 parents d889fa8 + 57efa05 commit b5aa972

36 files changed

+223
-48
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,10 @@ public override void Populate(TextWriter trapFile)
6363

6464
trapFile.accessors(this, kind, Symbol.Name, parent, unboundAccessor);
6565

66-
foreach (var l in Locations)
67-
trapFile.accessor_location(this, l);
66+
if (Context.ExtractLocation(Symbol))
67+
{
68+
WriteLocationsToTrap(trapFile.accessor_location, this, Locations);
69+
}
6870

6971
Overrides(trapFile);
7072

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public override void Populate(TextWriter trapFile)
5959
{
6060
var type = Type.Create(Context, Symbol.AttributeClass);
6161
trapFile.attributes(this, kind, type.TypeRef, entity);
62-
trapFile.attribute_location(this, Location);
62+
WriteLocationToTrap(trapFile.attribute_location, this, Location);
6363

6464
if (attributeSyntax is not null)
6565
{
66-
trapFile.attribute_location(this, Assembly.CreateOutputAssembly(Context));
66+
WriteLocationToTrap(trapFile.attribute_location, this, Assembly.CreateOutputAssembly(Context));
6767

6868
TypeMention.Create(Context, attributeSyntax.Name, this, type);
6969
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Base/CachedEntity.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System;
2+
using System.Collections.Generic;
13
using System.Diagnostics.CodeAnalysis;
24
using System.IO;
35
using Microsoft.CodeAnalysis;
@@ -52,6 +54,22 @@ public string DebugContents
5254
}
5355
}
5456

57+
protected static void WriteLocationToTrap<T1>(Action<T1, Location> writeAction, T1 entity, Location l)
58+
{
59+
if (l is not EmptyLocation)
60+
{
61+
writeAction(entity, l);
62+
}
63+
}
64+
65+
protected static void WriteLocationsToTrap<T1>(Action<T1, Location> writeAction, T1 entity, IEnumerable<Location> locations)
66+
{
67+
foreach (var loc in locations)
68+
{
69+
WriteLocationToTrap(writeAction, entity, loc);
70+
}
71+
}
72+
5573
public override bool NeedsPopulation { get; }
5674

5775
public override int GetHashCode() => Symbol is null ? 0 : Symbol.GetHashCode();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.IO;
34
using System.Linq;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ private CommentBlock(Context cx, Comments.CommentBlock init)
1111
public override void Populate(TextWriter trapFile)
1212
{
1313
trapFile.commentblock(this);
14-
trapFile.commentblock_location(this, Context.CreateLocation(Symbol.Location));
14+
WriteLocationToTrap(trapFile.commentblock_location, this, Context.CreateLocation(Symbol.Location));
1515
Symbol.CommentLines.ForEach((l, child) => trapFile.commentblock_child(this, l, child));
1616
}
1717

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public override void Populate(TextWriter trapFile)
2323
{
2424
location = Context.CreateLocation(Location);
2525
trapFile.commentline(this, Type == CommentLineType.MultilineContinuation ? CommentLineType.Multiline : Type, Text, RawText);
26-
trapFile.commentline_location(this, location);
26+
WriteLocationToTrap(trapFile.commentline_location, this, location);
2727
}
2828

2929
public override Microsoft.CodeAnalysis.Location? ReportingLocation => location?.Symbol;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void Populate(TextWriter trapFile)
2929
ContainingType!.PopulateGenerics();
3030

3131
trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition);
32-
trapFile.constructor_location(this, Location);
32+
WriteLocationToTrap(trapFile.constructor_location, this, Location);
3333

3434
if (MakeSynthetic)
3535
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public override void Populate(TextWriter trapFile)
1515
ContainingType!.PopulateGenerics();
1616

1717
trapFile.destructors(this, $"~{Symbol.ContainingType.Name}", ContainingType, OriginalDefinition(Context, this, Symbol));
18-
trapFile.destructor_location(this, Location);
18+
WriteLocationToTrap(trapFile.destructor_location, this, Location);
1919
}
2020

2121
private static new Destructor OriginalDefinition(Context cx, Destructor original, IMethodSymbol symbol)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ public override void Populate(TextWriter trapFile)
5151
TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier!.Name, this, explicitInterface);
5252
}
5353

54-
foreach (var l in Locations)
55-
trapFile.event_location(this, l);
54+
if (Context.ExtractLocation(Symbol))
55+
{
56+
WriteLocationsToTrap(trapFile.event_location, this, Locations);
57+
}
5658

5759
foreach (var syntaxType in declSyntaxReferences
5860
.OfType<VariableDeclaratorSyntax>()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ public override void Populate(TextWriter trapFile)
4848

4949
trapFile.event_accessors(this, kind, Symbol.Name, parent, unboundAccessor);
5050

51-
foreach (var l in Locations)
52-
trapFile.event_accessor_location(this, l);
51+
if (Context.ExtractLocation(Symbol))
52+
{
53+
WriteLocationsToTrap(trapFile.event_accessor_location, this, Locations);
54+
}
5355

5456
Overrides(trapFile);
5557

0 commit comments

Comments
 (0)