Skip to content

Commit 7c670cd

Browse files
committed
C#: Address review comments and make more early returns in Populate.
1 parent d95ebc7 commit 7c670cd

23 files changed

+148
-63
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ private CommentBlock(Context cx, Comments.CommentBlock init)
1111
public override void Populate(TextWriter trapFile)
1212
{
1313
trapFile.commentblock(this);
14-
if (!Context.OnlyScaffold)
14+
Symbol.CommentLines.ForEach((l, child) => trapFile.commentblock_child(this, l, child));
15+
if (Context.OnlyScaffold)
1516
{
16-
WriteLocationToTrap(trapFile.commentblock_location, this, Context.CreateLocation(Symbol.Location));
17+
return;
1718
}
18-
Symbol.CommentLines.ForEach((l, child) => trapFile.commentblock_child(this, l, child));
19+
WriteLocationToTrap(trapFile.commentblock_location, this, Context.CreateLocation(Symbol.Location));
1920
}
2021

2122
public override bool NeedsPopulation => true;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ private CommentLine(Context cx, Microsoft.CodeAnalysis.Location loc, CommentLine
2121

2222
public override void Populate(TextWriter trapFile)
2323
{
24-
location = Context.CreateLocation(Location);
2524
trapFile.commentline(this, Type == CommentLineType.MultilineContinuation ? CommentLineType.Multiline : Type, Text, RawText);
26-
if (!Context.OnlyScaffold)
25+
if (Context.OnlyScaffold)
2726
{
28-
WriteLocationToTrap(trapFile.commentline_location, this, location);
27+
return;
2928
}
29+
location = Context.CreateLocation(Location);
30+
WriteLocationToTrap(trapFile.commentline_location, this, location);
31+
3032
}
3133

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

csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/CompilerDiagnostic.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public CompilerDiagnostic(Context cx, Microsoft.CodeAnalysis.Diagnostic diag, Co
2121

2222
protected override void Populate(TextWriter trapFile)
2323
{
24+
if (Context.OnlyScaffold)
25+
{
26+
return;
27+
}
28+
2429
var key = diagnostic.Id;
2530
var messageCount = compilation.messageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);
2631
if (messageCount > limit)

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

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

3131
trapFile.constructors(this, Symbol.ContainingType.Name, ContainingType, (Constructor)OriginalDefinition);
32-
if (Context.ExtractLocation(Symbol) && (!IsDefault || IsBestSourceLocation))
32+
33+
if (Symbol.IsImplicitlyDeclared)
3334
{
34-
WriteLocationToTrap(trapFile.constructor_location, this, Location);
35+
var lineCounts = new LineCounts() { Total = 2, Code = 1, Comment = 0 };
36+
trapFile.numlines(this, lineCounts);
37+
}
38+
ExtractCompilerGenerated(trapFile);
39+
40+
if (Context.OnlyScaffold)
41+
{
42+
return;
3543
}
3644

3745
if (MakeSynthetic)
@@ -40,12 +48,11 @@ public override void Populate(TextWriter trapFile)
4048
Statements.SyntheticEmptyBlock.Create(Context, this, 0, Location);
4149
}
4250

43-
if (Symbol.IsImplicitlyDeclared)
51+
if (Context.ExtractLocation(Symbol) && (!IsDefault || IsBestSourceLocation))
4452
{
45-
var lineCounts = new LineCounts() { Total = 2, Code = 1, Comment = 0 };
46-
trapFile.numlines(this, lineCounts);
53+
WriteLocationToTrap(trapFile.constructor_location, this, Location);
4754
}
48-
ExtractCompilerGenerated(trapFile);
55+
4956
}
5057

5158
protected override void ExtractInitializers(TextWriter trapFile)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +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+
1819
if (Context.ExtractLocation(Symbol))
1920
{
2021
WriteLocationToTrap(trapFile.destructor_location, this, Location);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public override void Populate(TextWriter trapFile)
3737
Method.Create(Context, remover);
3838

3939
PopulateModifiers(trapFile);
40-
BindComments();
4140

4241
var declSyntaxReferences = IsSourceDeclaration
4342
? Symbol.DeclaringSyntaxReferences.Select(d => d.GetSyntax()).ToArray()
@@ -51,6 +50,13 @@ public override void Populate(TextWriter trapFile)
5150
TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier!.Name, this, explicitInterface);
5251
}
5352

53+
if (Context.OnlyScaffold)
54+
{
55+
return;
56+
}
57+
58+
BindComments();
59+
5460
if (Context.ExtractLocation(Symbol))
5561
{
5662
WriteLocationsToTrap(trapFile.event_location, this, Locations);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ private ExtractionMessage(Context cx, Message msg, bool bypassLimit) : base(cx)
2828

2929
protected override void Populate(TextWriter trapFile)
3030
{
31+
if (Context.OnlyScaffold)
32+
{
33+
return;
34+
}
35+
3136
// For the time being we're counting the number of messages per severity, we could introduce other groupings in the future
3237
var key = msg.Severity.ToString();
3338
groupedMessageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,17 @@ public override void Populate(TextWriter trapFile)
4949
}
5050
}
5151

52+
if (Context.OnlyScaffold)
53+
{
54+
return;
55+
}
56+
5257
if (Context.ExtractLocation(Symbol))
5358
{
5459
WriteLocationsToTrap(trapFile.field_location, this, Locations);
5560
}
5661

57-
if (!IsSourceDeclaration || !Symbol.FromSource() || Context.OnlyScaffold)
62+
if (!IsSourceDeclaration || !Symbol.FromSource())
5863
return;
5964

6065
Context.BindComments(this, Location.Symbol);

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ public override void Populate(TextWriter trapFile)
1919

2020
var type = Type.Create(Context, Symbol.Type);
2121
trapFile.indexers(this, Symbol.GetName(useMetadataName: true), ContainingType!, type.TypeRef, OriginalDefinition);
22-
if (Context.ExtractLocation(Symbol))
23-
{
24-
WriteLocationsToTrap(trapFile.indexer_location, this, Locations);
25-
}
2622

2723
var getter = BodyDeclaringSymbol.GetMethod;
2824
var setter = BodyDeclaringSymbol.SetMethod;
@@ -42,27 +38,9 @@ public override void Populate(TextWriter trapFile)
4238
Parameter.Create(Context, Symbol.Parameters[i], this, original);
4339
}
4440

45-
if (IsSourceDeclaration && !Context.OnlyScaffold)
46-
{
47-
var expressionBody = ExpressionBody;
48-
if (expressionBody is not null)
49-
{
50-
// The expression may need to reference parameters in the getter.
51-
// So we need to arrange that the expression is populated after the getter.
52-
Context.PopulateLater(() => Expression.CreateFromNode(new ExpressionNodeInfo(Context, expressionBody, this, 0).SetType(Symbol.GetAnnotatedType())));
53-
}
54-
}
55-
5641
PopulateAttributes();
5742
PopulateModifiers(trapFile);
5843

59-
if (Context.OnlyScaffold)
60-
{
61-
return;
62-
}
63-
64-
BindComments();
65-
6644
var declSyntaxReferences = IsSourceDeclaration
6745
? Symbol.DeclaringSyntaxReferences.
6846
Select(d => d.GetSyntax()).OfType<IndexerDeclarationSyntax>().ToArray()
@@ -76,6 +54,28 @@ public override void Populate(TextWriter trapFile)
7654
TypeMention.Create(Context, syntax.ExplicitInterfaceSpecifier!.Name, this, explicitInterface);
7755
}
7856

57+
if (Context.OnlyScaffold)
58+
{
59+
return;
60+
}
61+
62+
if (Context.ExtractLocation(Symbol))
63+
{
64+
WriteLocationsToTrap(trapFile.indexer_location, this, Locations);
65+
}
66+
67+
if (IsSourceDeclaration)
68+
{
69+
var expressionBody = ExpressionBody;
70+
if (expressionBody is not null)
71+
{
72+
// The expression may need to reference parameters in the getter.
73+
// So we need to arrange that the expression is populated after the getter.
74+
Context.PopulateLater(() => Expression.CreateFromNode(new ExpressionNodeInfo(Context, expressionBody, this, 0).SetType(Symbol.GetAnnotatedType())));
75+
}
76+
}
77+
78+
BindComments();
7979

8080
foreach (var syntax in declSyntaxReferences)
8181
TypeMention.Create(Context, syntax.Type, this, type);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public void PopulateManual(Expression parent, bool isVar)
4141
trapFile.localvars(this, Kinds.VariableKind.None, Symbol.Name, @var, Type.Create(Context, parent.Type).TypeRef, parent);
4242
}
4343

44+
if (Context.OnlyScaffold)
45+
{
46+
return;
47+
}
48+
4449
WriteLocationToTrap(trapFile.localvar_location, this, Location);
4550

4651
DefineConstantValue(trapFile);

0 commit comments

Comments
 (0)