33using Microsoft . CodeAnalysis . CSharp . Syntax ;
44using Semmle . Extraction . CSharp . Entities ;
55using Semmle . Extraction . Entities ;
6+ using Semmle . Util ;
67using Semmle . Util . Logging ;
8+ using System ;
79using System . Collections . Generic ;
810using System . IO ;
911using 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