Skip to content

Commit 89681a4

Browse files
committed
C#: Only extract the unbound locations for constructors, destructors and user defined operators and use this in the QL code.
1 parent 02428fc commit 89681a4

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public override void Populate(TextWriter trapFile)
2929
ContainingType!.PopulateGenerics();
3030

3131
trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition);
32-
WriteLocationToTrap(trapFile.constructor_location, this, Location);
32+
if (Context.ExtractLocation(Symbol) && (!IsDefault || IsBestSourceLocation))
33+
{
34+
WriteLocationToTrap(trapFile.constructor_location, this, Location);
35+
}
3336

3437
if (MakeSynthetic)
3538
{
@@ -168,7 +171,15 @@ Symbol.ContainingType.TypeKind is TypeKind.Class or TypeKind.Struct &&
168171
Symbol.ContainingType.IsSourceDeclaration() &&
169172
!Symbol.ContainingType.IsAnonymousType;
170173

171-
private bool MakeSynthetic => IsPrimary || IsDefault;
174+
/// <summary>
175+
/// Returns true if we consider the reporting location of this constructor entity the best
176+
/// location of the constructor.
177+
/// For partial classes with default constructors, Roslyn consider each partial class declaration
178+
/// as the possible location for the implicit default constructor.
179+
/// </summary>
180+
private bool IsBestSourceLocation => ReportingLocation is not null && Context.IsLocationInContext(ReportingLocation);
181+
182+
private bool MakeSynthetic => IsPrimary || (IsDefault && IsBestSourceLocation);
172183

173184
[return: NotNullIfNotNull(nameof(constructor))]
174185
public static new Constructor? Create(Context cx, IMethodSymbol? constructor)

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

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

1717
trapFile.destructors(this, $"~{Symbol.ContainingType.Name}", ContainingType, OriginalDefinition(Context, this, Symbol));
18-
WriteLocationToTrap(trapFile.destructor_location, this, Location);
18+
if (Context.ExtractLocation(Symbol))
19+
{
20+
WriteLocationToTrap(trapFile.destructor_location, this, Location);
21+
}
1922
}
2023

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

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ public override void Populate(TextWriter trapFile)
2626
returnType.TypeRef,
2727
(UserOperator)OriginalDefinition);
2828

29-
WriteLocationsToTrap(trapFile.operator_location, this, Locations);
29+
if (Context.ExtractLocation(Symbol))
30+
{
31+
WriteLocationsToTrap(trapFile.operator_location, this, Locations);
32+
}
3033

3134
if (IsSourceDeclaration)
3235
{

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ class Constructor extends Callable, Member, Attributable, @constructor {
357357

358358
override Constructor getUnboundDeclaration() { constructors(this, _, _, result) }
359359

360-
override Location getALocation() { constructor_location(this, result) }
360+
override Location getALocation() { constructor_location(this.getUnboundDeclaration(), result) }
361361

362362
override predicate fromSource() { Member.super.fromSource() and not this.isCompilerGenerated() }
363363

@@ -450,7 +450,7 @@ class Destructor extends Callable, Member, Attributable, @destructor {
450450

451451
override Destructor getUnboundDeclaration() { destructors(this, _, _, result) }
452452

453-
override Location getALocation() { destructor_location(this, result) }
453+
override Location getALocation() { destructor_location(this.getUnboundDeclaration(), result) }
454454

455455
override string toString() { result = Callable.super.toString() }
456456

@@ -484,7 +484,7 @@ class Operator extends Callable, Member, Attributable, Overridable, @operator {
484484

485485
override Operator getUnboundDeclaration() { operators(this, _, _, _, _, result) }
486486

487-
override Location getALocation() { operator_location(this, result) }
487+
override Location getALocation() { operator_location(this.getUnboundDeclaration(), result) }
488488

489489
override string toString() { result = Callable.super.toString() }
490490

0 commit comments

Comments
 (0)