Skip to content

Commit 28384dd

Browse files
committed
C#: Do not extract empty locations for methods, constructors and parameters.
1 parent b4d984a commit 28384dd

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

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

Lines changed: 4 additions & 1 deletion
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-
trapFile.constructor_location(this, Location);
32+
if (Context.ExtractLocation(Symbol) && Location is not EmptyLocation)
33+
{
34+
trapFile.constructor_location(this, Location);
35+
}
3336

3437
if (MakeSynthetic)
3538
{

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ public override void Populate(TextWriter trapFile)
4343
}
4444
}
4545

46-
foreach (var l in Locations)
47-
trapFile.method_location(this, l);
46+
if (Context.ExtractLocation(Symbol))
47+
{
48+
foreach (var l in Locations)
49+
{
50+
if (l is not EmptyLocation)
51+
{
52+
trapFile.method_location(this, l);
53+
}
54+
}
55+
}
4856

4957
PopulateGenerics(trapFile);
5058
Overrides(trapFile);

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,24 @@ public override void Populate(TextWriter trapFile)
116116
trapFile.@params(this, Name, type.TypeRef, Ordinal, ParamKind, Parent!, Original);
117117

118118
foreach (var l in Symbol.Locations)
119-
trapFile.param_location(this, Context.CreateLocation(l));
119+
{
120+
var location = Context.CreateLocation(l);
121+
if (location is not EmptyLocation)
122+
{
123+
trapFile.param_location(this, location);
124+
}
125+
}
120126

121127
if (!Symbol.Locations.Any() &&
122128
Symbol.ContainingSymbol is IMethodSymbol ms &&
123129
ms.Name == WellKnownMemberNames.TopLevelStatementsEntryPointMethodName &&
124130
ms.ContainingType.Name == WellKnownMemberNames.TopLevelStatementsEntryPointTypeName)
125131
{
126-
trapFile.param_location(this, Context.CreateLocation());
132+
var location = Context.CreateLocation();
133+
if (location is not EmptyLocation)
134+
{
135+
trapFile.param_location(this, location);
136+
}
127137
}
128138

129139
if (Symbol.HasExplicitDefaultValue && Context.Defines(Symbol))
@@ -247,7 +257,6 @@ public override void Populate(TextWriter trapFile)
247257
var typeKey = VarargsType.Create(Context);
248258
// !! Maybe originaldefinition is wrong
249259
trapFile.@params(this, "", typeKey, Ordinal, Kind.None, Parent!, this);
250-
trapFile.param_location(this, GeneratedLocation.Create(Context));
251260
}
252261

253262
protected override int Ordinal => ((Method)Parent!).OriginalDefinition.Symbol.Parameters.Length;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,13 @@ class Method extends Callable, Virtualizable, Attributable, @method {
265265
result = Virtualizable.super.getAnUltimateImplementor()
266266
}
267267

268-
override Location getALocation() { method_location(this, result) }
268+
override Location getALocation() {
269+
exists(Method m | m = this.getUnboundDeclaration() |
270+
method_location(m, result)
271+
or
272+
not method_location(m, _) and result instanceof EmptyLocation
273+
)
274+
}
269275

270276
/** Holds if this method is an extension method. */
271277
predicate isExtensionMethod() { this.getParameter(0).hasExtensionMethodModifier() }
@@ -357,7 +363,11 @@ class Constructor extends Callable, Member, Attributable, @constructor {
357363

358364
override Constructor getUnboundDeclaration() { constructors(this, _, _, result) }
359365

360-
override Location getALocation() { constructor_location(this, result) }
366+
override Location getALocation() {
367+
constructor_location(this, result)
368+
or
369+
not constructor_location(this, _) and result instanceof EmptyLocation
370+
}
361371

362372
override predicate fromSource() { Member.super.fromSource() and not this.isCompilerGenerated() }
363373

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,11 @@ class Parameter extends LocalScopeVariable, Attributable, TopLevelExprParent, @p
213213
params(this, _, getTypeRef(result), _, _, _, _)
214214
}
215215

216-
override Location getALocation() { param_location(this, result) }
216+
override Location getALocation() {
217+
param_location(this, result)
218+
or
219+
not param_location(this, _) and result instanceof EmptyLocation
220+
}
217221

218222
override string toString() { result = this.getName() }
219223

0 commit comments

Comments
 (0)