Skip to content

Commit e57134e

Browse files
hvitvedtamasvajk
authored andcommitted
C#: Address comments
1 parent d6515e7 commit e57134e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static bool ContainsPattern(SyntaxNode node) =>
128128
node is PatternSyntax || node is VariableDesignationSyntax || node.ChildNodes().Any(ContainsPattern);
129129

130130
/// <summary>
131-
/// Creates a generated expression from a typed constant..
131+
/// Creates a generated expression from a typed constant.
132132
/// </summary>
133133
public static Expression CreateGenerated(Context cx, TypedConstant constant, IExpressionParentEntity parent,
134134
int childIndex, Semmle.Extraction.Entities.Location ___location)

csharp/extractor/Semmle.Extraction.CSharp/Populators/CompilationUnit.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Microsoft.CodeAnalysis.CSharp.Syntax;
44
using Semmle.Extraction.CSharp.Entities;
55
using Semmle.Extraction.Entities;
6+
using Semmle.Util;
67
using Semmle.Util.Logging;
8+
using System;
79
using System.Collections.Generic;
810
using System.IO;
911
using System.Linq;
@@ -15,12 +17,23 @@ public class TypeContainerVisitor : CSharpSyntaxVisitor
1517
protected Context cx { get; }
1618
protected IEntity parent { get; }
1719
protected TextWriter trapFile { get; }
20+
private readonly Lazy<Func<SyntaxNode, AttributeData>> attributeLookup;
1821

1922
public TypeContainerVisitor(Context cx, TextWriter trapFile, IEntity parent)
2023
{
2124
this.cx = cx;
2225
this.parent = parent;
2326
this.trapFile = trapFile;
27+
attributeLookup = new Lazy<Func<SyntaxNode, AttributeData>>(() =>
28+
{
29+
var dict = new Dictionary<SyntaxNode, AttributeData>();
30+
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())))
31+
{
32+
if (attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax)
33+
dict.Add(syntax, attributeData);
34+
}
35+
return dict.GetValueOrDefault;
36+
});
2437
}
2538

2639
public override void DefaultVisit(SyntaxNode node)
@@ -59,17 +72,11 @@ public override void VisitAttributeList(AttributeListSyntax node)
5972
return;
6073

6174
var outputAssembly = Assembly.CreateOutputAssembly(cx);
62-
var attributeLookup = new Dictionary<SyntaxNode, AttributeData>();
63-
foreach (var attributeData in cx.Compilation.Assembly.GetAttributes().Concat(cx.Compilation.Assembly.Modules.SelectMany(m => m.GetAttributes())))
64-
{
65-
if (attributeData.ApplicationSyntaxReference?.GetSyntax() is SyntaxNode syntax)
66-
attributeLookup.Add(syntax, attributeData);
67-
}
6875
foreach (var attribute in node.Attributes)
6976
{
70-
if (attributeLookup.TryGetValue(attribute, out var attributeData))
77+
if (attributeLookup.Value(attribute) is AttributeData attributeData)
7178
{
72-
var ae = Attribute.Create(cx, attributeData, outputAssembly);
79+
var ae = Semmle.Extraction.CSharp.Entities.Attribute.Create(cx, attributeData, outputAssembly);
7380
cx.BindComments(ae, attribute.GetLocation());
7481
}
7582
}

0 commit comments

Comments
 (0)