16
16
17
17
namespace Semmle . Extraction . CSharp
18
18
{
19
- public static class Extractor
19
+ public enum ExitCode
20
20
{
21
- public enum ExitCode
22
- {
23
- Ok , // Everything worked perfectly
24
- Errors , // Trap was generated but there were processing errors
25
- Failed // Trap could not be generated
26
- }
21
+ Ok , // Everything worked perfectly
22
+ Errors , // Trap was generated but there were processing errors
23
+ Failed // Trap could not be generated
24
+ }
27
25
26
+ public static class Extractor
27
+ {
28
28
private class LogProgressMonitor : IProgressMonitor
29
29
{
30
30
private readonly ILogger logger ;
@@ -69,6 +69,14 @@ public static void SetInvariantCulture()
69
69
Thread . CurrentThread . CurrentUICulture = culture ;
70
70
}
71
71
72
+ public static ILogger MakeLogger ( Verbosity verbosity , bool includeConsole )
73
+ {
74
+ var fileLogger = new FileLogger ( verbosity , GetCSharpLogPath ( ) ) ;
75
+ return includeConsole
76
+ ? new CombinedLogger ( new ConsoleLogger ( verbosity ) , fileLogger )
77
+ : ( ILogger ) fileLogger ;
78
+ }
79
+
72
80
/// <summary>
73
81
/// Command-line driver for the extractor.
74
82
/// </summary>
@@ -89,10 +97,7 @@ public static ExitCode Run(string[] args)
89
97
var options = Options . CreateWithEnvironment ( args ) ;
90
98
Entities . Compilation . Settings = ( Directory . GetCurrentDirectory ( ) , options . CompilerArguments . ToArray ( ) ) ;
91
99
92
- var fileLogger = new FileLogger ( options . Verbosity , GetCSharpLogPath ( ) ) ;
93
- using var logger = options . Console
94
- ? new CombinedLogger ( new ConsoleLogger ( options . Verbosity ) , fileLogger )
95
- : ( ILogger ) fileLogger ;
100
+ using var logger = MakeLogger ( options . Verbosity , options . Console ) ;
96
101
97
102
if ( Environment . GetEnvironmentVariable ( "SEMMLE_CLRTRACER" ) == "1" && ! options . ClrTracer )
98
103
{
@@ -276,7 +281,7 @@ private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.Comm
276
281
/// The constructed syntax trees will be added (thread-safely) to the supplied
277
282
/// list <paramref name="ret"/>.
278
283
/// </summary>
279
- private static IEnumerable < Action > ReadSyntaxTrees ( IEnumerable < string > sources , Analyser analyser , CSharpParseOptions ? parseOptions , Encoding ? encoding , IList < SyntaxTree > ret )
284
+ public static IEnumerable < Action > ReadSyntaxTrees ( IEnumerable < string > sources , Analyser analyser , CSharpParseOptions ? parseOptions , Encoding ? encoding , IList < SyntaxTree > ret )
280
285
{
281
286
return sources . Select < string , Action > ( path => ( ) =>
282
287
{
@@ -298,31 +303,7 @@ private static IEnumerable<Action> ReadSyntaxTrees(IEnumerable<string> sources,
298
303
} ) ;
299
304
}
300
305
301
- public static void ExtractStandalone (
302
- IEnumerable < string > sources ,
303
- IEnumerable < string > referencePaths ,
304
- IProgressMonitor pm ,
305
- ILogger logger ,
306
- CommonOptions options )
307
- {
308
- var stopwatch = new Stopwatch ( ) ;
309
- stopwatch . Start ( ) ;
310
-
311
- var canonicalPathCache = CanonicalPathCache . Create ( logger , 1000 ) ;
312
- var pathTransformer = new PathTransformer ( canonicalPathCache ) ;
313
-
314
- using var analyser = new StandaloneAnalyser ( pm , logger , false , pathTransformer ) ;
315
- try
316
- {
317
- AnalyseStandalone ( analyser , sources , referencePaths , options , pm , stopwatch ) ;
318
- }
319
- catch ( Exception ex ) // lgtm[cs/catch-of-all-exceptions]
320
- {
321
- analyser . Logger . Log ( Severity . Error , " Unhandled exception: {0}" , ex ) ;
322
- }
323
- }
324
-
325
- private static ExitCode Analyse ( Stopwatch stopwatch , Analyser analyser , CommonOptions options ,
306
+ public static ExitCode Analyse ( Stopwatch stopwatch , Analyser analyser , CommonOptions options ,
326
307
Func < BlockingCollection < MetadataReference > , IEnumerable < Action > > getResolvedReferenceTasks ,
327
308
Func < Analyser , List < SyntaxTree > , IEnumerable < Action > > getSyntaxTreeTasks ,
328
309
Func < IEnumerable < SyntaxTree > , IEnumerable < MetadataReference > , CSharpCompilation > getCompilation ,
@@ -395,37 +376,6 @@ private static ExitCode Analyse(Stopwatch stopwatch, Analyser analyser, CommonOp
395
376
return analyser . TotalErrors == 0 ? ExitCode . Ok : ExitCode . Errors ;
396
377
}
397
378
398
- private static void AnalyseStandalone (
399
- StandaloneAnalyser analyser ,
400
- IEnumerable < string > sources ,
401
- IEnumerable < string > referencePaths ,
402
- CommonOptions options ,
403
- IProgressMonitor progressMonitor ,
404
- Stopwatch stopwatch )
405
- {
406
- Analyse ( stopwatch , analyser , options ,
407
- references => GetResolvedReferencesStandalone ( referencePaths , references ) ,
408
- ( analyser , syntaxTrees ) => ReadSyntaxTrees ( sources , analyser , null , null , syntaxTrees ) ,
409
- ( syntaxTrees , references ) => CSharpCompilation . Create ( "csharp.dll" , syntaxTrees , references ) ,
410
- ( compilation , options ) => analyser . InitializeStandalone ( compilation , options ) ,
411
- ( ) => { } ,
412
- _ => { } ,
413
- ( ) =>
414
- {
415
- foreach ( var type in analyser . MissingNamespaces )
416
- {
417
- progressMonitor . MissingNamespace ( type ) ;
418
- }
419
-
420
- foreach ( var type in analyser . MissingTypes )
421
- {
422
- progressMonitor . MissingType ( type ) ;
423
- }
424
-
425
- progressMonitor . MissingSummary ( analyser . MissingTypes . Count ( ) , analyser . MissingNamespaces . Count ( ) ) ;
426
- } ) ;
427
- }
428
-
429
379
private static ExitCode AnalyseTracing (
430
380
TracingAnalyser analyser ,
431
381
CSharpCommandLineArguments compilerArguments ,
@@ -468,15 +418,6 @@ private static ExitCode AnalyseTracing(
468
418
( ) => { } ) ;
469
419
}
470
420
471
- private static IEnumerable < Action > GetResolvedReferencesStandalone ( IEnumerable < string > referencePaths , BlockingCollection < MetadataReference > references )
472
- {
473
- return referencePaths . Select < string , Action > ( path => ( ) =>
474
- {
475
- var reference = MetadataReference . CreateFromFile ( path ) ;
476
- references . Add ( reference ) ;
477
- } ) ;
478
- }
479
-
480
421
/// <summary>
481
422
/// Gets the path to the `csharp.log` file written to by the C# extractor.
482
423
/// </summary>
0 commit comments