11using System . Collections . Generic ;
22using System . IO ;
33using System . Linq ;
4+ using Semmle . Util . Logging ;
45
56namespace Semmle . Extraction . CSharp . DependencyFetching
67{
@@ -18,25 +19,26 @@ internal class AssemblyCache
1819 /// Paths to search. Directories are searched recursively. Files are added directly to the
1920 /// assembly cache.
2021 /// </param>
21- /// <param name="progressMonitor ">Callback for progress.</param>
22- public AssemblyCache ( IEnumerable < string > paths , IEnumerable < string > frameworkPaths , ProgressMonitor progressMonitor )
22+ /// <param name="logger ">Callback for progress.</param>
23+ public AssemblyCache ( IEnumerable < string > paths , IEnumerable < string > frameworkPaths , ILogger logger )
2324 {
25+ this . logger = logger ;
2426 foreach ( var path in paths )
2527 {
2628 if ( File . Exists ( path ) )
2729 {
28- pendingDllsToIndex . Enqueue ( path ) ;
30+ dllsToIndex . Add ( path ) ;
2931 continue ;
3032 }
3133
3234 if ( Directory . Exists ( path ) )
3335 {
34- progressMonitor . FindingFiles ( path ) ;
36+ logger . LogInfo ( $ "Finding reference DLLs in { path } ..." ) ;
3537 AddReferenceDirectory ( path ) ;
3638 }
3739 else
3840 {
39- progressMonitor . LogInfo ( "AssemblyCache: Path not found: " + path ) ;
41+ logger . LogInfo ( "AssemblyCache: Path not found: " + path ) ;
4042 }
4143 }
4244 IndexReferences ( frameworkPaths ) ;
@@ -52,7 +54,7 @@ private void AddReferenceDirectory(string dir)
5254 {
5355 foreach ( var dll in new DirectoryInfo ( dir ) . EnumerateFiles ( "*.dll" , SearchOption . AllDirectories ) )
5456 {
55- pendingDllsToIndex . Enqueue ( dll . FullName ) ;
57+ dllsToIndex . Add ( dll . FullName ) ;
5658 }
5759 }
5860
@@ -62,12 +64,16 @@ private void AddReferenceDirectory(string dir)
6264 /// </summary>
6365 private void IndexReferences ( IEnumerable < string > frameworkPaths )
6466 {
67+ logger . LogInfo ( $ "Indexing { dllsToIndex . Count } assemblies...") ;
68+
6569 // Read all of the files
66- foreach ( var filename in pendingDllsToIndex )
70+ foreach ( var filename in dllsToIndex )
6771 {
6872 IndexReference ( filename ) ;
6973 }
7074
75+ logger . LogInfo ( $ "Read { assemblyInfoByFileName . Count } assembly infos") ;
76+
7177 foreach ( var info in assemblyInfoByFileName . Values
7278 . OrderBy ( info => info . Name )
7379 . OrderAssemblyInfosByPreference ( frameworkPaths ) )
@@ -83,25 +89,16 @@ private void IndexReference(string filename)
8389 {
8490 try
8591 {
92+ logger . LogDebug ( $ "Reading assembly info from { filename } ") ;
8693 var info = AssemblyInfo . ReadFromFile ( filename ) ;
8794 assemblyInfoByFileName [ filename ] = info ;
8895 }
8996 catch ( AssemblyLoadException )
9097 {
91- failedAssemblyInfoFileNames . Add ( filename ) ;
98+ logger . LogInfo ( $ "Couldn't read assembly info from { filename } " ) ;
9299 }
93100 }
94101
95- /// <summary>
96- /// The number of DLLs which are assemblies.
97- /// </summary>
98- public int AssemblyCount => assemblyInfoByFileName . Count ;
99-
100- /// <summary>
101- /// The number of DLLs which weren't assemblies. (E.g. C++).
102- /// </summary>
103- public int NonAssemblyCount => failedAssemblyInfoFileNames . Count ;
104-
105102 /// <summary>
106103 /// Given an assembly id, determine its full info.
107104 /// </summary>
@@ -113,8 +110,7 @@ public AssemblyInfo ResolveReference(string id)
113110 if ( failedAssemblyInfoIds . Contains ( id ) )
114111 throw new AssemblyLoadException ( ) ;
115112
116- string assemblyName ;
117- ( id , assemblyName ) = AssemblyInfo . ComputeSanitizedAssemblyInfo ( id ) ;
113+ ( id , var assemblyName ) = AssemblyInfo . ComputeSanitizedAssemblyInfo ( id ) ;
118114
119115 // Look up the id in our references map.
120116 if ( assemblyInfoById . TryGetValue ( id , out var result ) )
@@ -164,17 +160,15 @@ public AssemblyInfo GetAssemblyInfo(string filepath)
164160 throw new AssemblyLoadException ( ) ;
165161 }
166162
167- private readonly Queue < string > pendingDllsToIndex = new Queue < string > ( ) ;
163+ private readonly List < string > dllsToIndex = new List < string > ( ) ;
168164
169165 private readonly Dictionary < string , AssemblyInfo > assemblyInfoByFileName = new Dictionary < string , AssemblyInfo > ( ) ;
170166
171- // List of DLLs which are not assemblies.
172- // We probably don't need to keep this
173- private readonly List < string > failedAssemblyInfoFileNames = new List < string > ( ) ;
174-
175167 // Map from assembly id (in various formats) to the full info.
176168 private readonly Dictionary < string , AssemblyInfo > assemblyInfoById = new Dictionary < string , AssemblyInfo > ( ) ;
177169
178170 private readonly HashSet < string > failedAssemblyInfoIds = new HashSet < string > ( ) ;
171+
172+ private readonly ILogger logger ;
179173 }
180174}
0 commit comments