1
1
using System . Collections . Generic ;
2
2
using System . IO ;
3
3
using System . Linq ;
4
+ using Semmle . Util . Logging ;
4
5
5
6
namespace Semmle . Extraction . CSharp . DependencyFetching
6
7
{
@@ -18,25 +19,26 @@ internal class AssemblyCache
18
19
/// Paths to search. Directories are searched recursively. Files are added directly to the
19
20
/// assembly cache.
20
21
/// </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 )
23
24
{
25
+ this . logger = logger ;
24
26
foreach ( var path in paths )
25
27
{
26
28
if ( File . Exists ( path ) )
27
29
{
28
- pendingDllsToIndex . Enqueue ( path ) ;
30
+ dllsToIndex . Add ( path ) ;
29
31
continue ;
30
32
}
31
33
32
34
if ( Directory . Exists ( path ) )
33
35
{
34
- progressMonitor . FindingFiles ( path ) ;
36
+ logger . LogInfo ( $ "Finding reference DLLs in { path } ..." ) ;
35
37
AddReferenceDirectory ( path ) ;
36
38
}
37
39
else
38
40
{
39
- progressMonitor . LogInfo ( "AssemblyCache: Path not found: " + path ) ;
41
+ logger . LogInfo ( "AssemblyCache: Path not found: " + path ) ;
40
42
}
41
43
}
42
44
IndexReferences ( frameworkPaths ) ;
@@ -52,7 +54,7 @@ private void AddReferenceDirectory(string dir)
52
54
{
53
55
foreach ( var dll in new DirectoryInfo ( dir ) . EnumerateFiles ( "*.dll" , SearchOption . AllDirectories ) )
54
56
{
55
- pendingDllsToIndex . Enqueue ( dll . FullName ) ;
57
+ dllsToIndex . Add ( dll . FullName ) ;
56
58
}
57
59
}
58
60
@@ -62,12 +64,16 @@ private void AddReferenceDirectory(string dir)
62
64
/// </summary>
63
65
private void IndexReferences ( IEnumerable < string > frameworkPaths )
64
66
{
67
+ logger . LogInfo ( $ "Indexing { dllsToIndex . Count } assemblies...") ;
68
+
65
69
// Read all of the files
66
- foreach ( var filename in pendingDllsToIndex )
70
+ foreach ( var filename in dllsToIndex )
67
71
{
68
72
IndexReference ( filename ) ;
69
73
}
70
74
75
+ logger . LogInfo ( $ "Read { assemblyInfoByFileName . Count } assembly infos") ;
76
+
71
77
foreach ( var info in assemblyInfoByFileName . Values
72
78
. OrderBy ( info => info . Name )
73
79
. OrderAssemblyInfosByPreference ( frameworkPaths ) )
@@ -83,25 +89,16 @@ private void IndexReference(string filename)
83
89
{
84
90
try
85
91
{
92
+ logger . LogDebug ( $ "Reading assembly info from { filename } ") ;
86
93
var info = AssemblyInfo . ReadFromFile ( filename ) ;
87
94
assemblyInfoByFileName [ filename ] = info ;
88
95
}
89
96
catch ( AssemblyLoadException )
90
97
{
91
- failedAssemblyInfoFileNames . Add ( filename ) ;
98
+ logger . LogInfo ( $ "Couldn't read assembly info from { filename } " ) ;
92
99
}
93
100
}
94
101
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
-
105
102
/// <summary>
106
103
/// Given an assembly id, determine its full info.
107
104
/// </summary>
@@ -113,8 +110,7 @@ public AssemblyInfo ResolveReference(string id)
113
110
if ( failedAssemblyInfoIds . Contains ( id ) )
114
111
throw new AssemblyLoadException ( ) ;
115
112
116
- string assemblyName ;
117
- ( id , assemblyName ) = AssemblyInfo . ComputeSanitizedAssemblyInfo ( id ) ;
113
+ ( id , var assemblyName ) = AssemblyInfo . ComputeSanitizedAssemblyInfo ( id ) ;
118
114
119
115
// Look up the id in our references map.
120
116
if ( assemblyInfoById . TryGetValue ( id , out var result ) )
@@ -164,17 +160,15 @@ public AssemblyInfo GetAssemblyInfo(string filepath)
164
160
throw new AssemblyLoadException ( ) ;
165
161
}
166
162
167
- private readonly Queue < string > pendingDllsToIndex = new Queue < string > ( ) ;
163
+ private readonly List < string > dllsToIndex = new List < string > ( ) ;
168
164
169
165
private readonly Dictionary < string , AssemblyInfo > assemblyInfoByFileName = new Dictionary < string , AssemblyInfo > ( ) ;
170
166
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
-
175
167
// Map from assembly id (in various formats) to the full info.
176
168
private readonly Dictionary < string , AssemblyInfo > assemblyInfoById = new Dictionary < string , AssemblyInfo > ( ) ;
177
169
178
170
private readonly HashSet < string > failedAssemblyInfoIds = new HashSet < string > ( ) ;
171
+
172
+ private readonly ILogger logger ;
179
173
}
180
174
}
0 commit comments