Skip to content

Commit 982c998

Browse files
committed
C#: Cache the Block and ExpressionBody and streamline implementation too look for both when checking whether a body is available.
1 parent 912e457 commit 982c998

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public override void Populate(TextWriter trapFile)
7070

7171
Overrides(trapFile);
7272

73-
if (Symbol.FromSource() && Block is null)
73+
if (Symbol.FromSource() && !HasBody)
7474
{
7575
trapFile.compiler_generated(this);
7676
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,29 +89,32 @@ protected void BindComments()
8989

9090
protected virtual T BodyDeclaringSymbol => Symbol;
9191

92-
public BlockSyntax? Block
92+
private static BlockSyntax? GetBlock(T symbol)
9393
{
94-
get
95-
{
96-
return BodyDeclaringSymbol.DeclaringSyntaxReferences
94+
return symbol.DeclaringSyntaxReferences
9795
.SelectMany(r => r.GetSyntax().ChildNodes())
9896
.OfType<BlockSyntax>()
9997
.FirstOrDefault();
100-
}
10198
}
10299

103-
public ExpressionSyntax? ExpressionBody
100+
private static ExpressionSyntax? GetExpressionBody(T symbol)
104101
{
105-
get
106-
{
107-
return BodyDeclaringSymbol.DeclaringSyntaxReferences
102+
return symbol.DeclaringSyntaxReferences
108103
.SelectMany(r => r.GetSyntax().ChildNodes())
109104
.OfType<ArrowExpressionClauseSyntax>()
110105
.Select(arrow => arrow.Expression)
111106
.FirstOrDefault();
112-
}
113107
}
114108

109+
private BlockSyntax? vBlock;
110+
public BlockSyntax? Block => vBlock ??= GetBlock(BodyDeclaringSymbol);
111+
112+
private ExpressionSyntax? vExpressionBody;
113+
public ExpressionSyntax? ExpressionBody => vExpressionBody ??= GetExpressionBody(BodyDeclaringSymbol);
114+
115+
private bool? vHasBody;
116+
public bool HasBody => vHasBody ??= Block is not null || ExpressionBody is not null;
117+
115118
public virtual bool IsSourceDeclaration => Symbol.IsSourceDeclaration();
116119

117120
public override bool NeedsPopulation => Context.Defines(Symbol);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public override void Populate(TextWriter trapFile)
4242
return;
4343
}
4444

45-
if (MakeSynthetic)
45+
if (MakeSyntheticBody)
4646
{
4747
// Create a synthetic empty body for primary and default constructors.
4848
Statements.SyntheticEmptyBlock.Create(Context, this, 0, Location);
@@ -60,7 +60,7 @@ protected override void ExtractInitializers(TextWriter trapFile)
6060
// Do not extract initializers for constructed types.
6161
// Extract initializers for constructors with a body, primary constructors
6262
// and default constructors for classes and structs declared in source code.
63-
if (Block is null && ExpressionBody is null && !MakeSynthetic || Context.OnlyScaffold)
63+
if (!HasBody && !MakeSyntheticBody || Context.OnlyScaffold)
6464
{
6565
return;
6666
}
@@ -211,7 +211,7 @@ Symbol.ContainingType.TypeKind is TypeKind.Class or TypeKind.Struct &&
211211
/// </summary>
212212
private bool IsBestSourceLocation => ReportingLocation is not null && Context.IsLocationInContext(ReportingLocation);
213213

214-
private bool MakeSynthetic => (IsPrimary || (IsDefault && IsBestSourceLocation)) && !Context.OnlyScaffold;
214+
private bool MakeSyntheticBody => (IsPrimary || (IsDefault && IsBestSourceLocation)) && !Context.OnlyScaffold;
215215

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public override void Populate(TextWriter trapFile)
5959

6060
Overrides(trapFile);
6161

62-
if (Symbol.FromSource() && Block is null)
62+
if (Symbol.FromSource() && !HasBody)
6363
{
6464
trapFile.compiler_generated(this);
6565
}

0 commit comments

Comments
 (0)