3
3
using Microsoft . CodeAnalysis . CSharp . Syntax ;
4
4
using Semmle . Extraction . CSharp . Entities ;
5
5
using Semmle . Extraction . Entities ;
6
+ using Semmle . Util ;
6
7
using Semmle . Util . Logging ;
8
+ using System ;
7
9
using System . Collections . Generic ;
8
10
using System . IO ;
9
11
using System . Linq ;
@@ -15,12 +17,23 @@ public class TypeContainerVisitor : CSharpSyntaxVisitor
15
17
protected Context cx { get ; }
16
18
protected IEntity parent { get ; }
17
19
protected TextWriter trapFile { get ; }
20
+ private readonly Lazy < Func < SyntaxNode , AttributeData > > attributeLookup ;
18
21
19
22
public TypeContainerVisitor ( Context cx , TextWriter trapFile , IEntity parent )
20
23
{
21
24
this . cx = cx ;
22
25
this . parent = parent ;
23
26
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
+ } ) ;
24
37
}
25
38
26
39
public override void DefaultVisit ( SyntaxNode node )
@@ -59,17 +72,11 @@ public override void VisitAttributeList(AttributeListSyntax node)
59
72
return ;
60
73
61
74
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
- }
68
75
foreach ( var attribute in node . Attributes )
69
76
{
70
- if ( attributeLookup . TryGetValue ( attribute , out var attributeData ) )
77
+ if ( attributeLookup . Value ( attribute ) is AttributeData attributeData )
71
78
{
72
- var ae = Attribute . Create ( cx , attributeData , outputAssembly ) ;
79
+ var ae = Semmle . Extraction . CSharp . Entities . Attribute . Create ( cx , attributeData , outputAssembly ) ;
73
80
cx . BindComments ( ae , attribute . GetLocation ( ) ) ;
74
81
}
75
82
}
0 commit comments