1616
1717namespace Semmle . Extraction . CSharp
1818{
19- public static class Extractor
19+ public enum ExitCode
2020 {
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+ }
2725
26+ public static class Extractor
27+ {
2828 private class LogProgressMonitor : IProgressMonitor
2929 {
3030 private readonly ILogger logger ;
@@ -69,6 +69,14 @@ public static void SetInvariantCulture()
6969 Thread . CurrentThread . CurrentUICulture = culture ;
7070 }
7171
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+
7280 /// <summary>
7381 /// Command-line driver for the extractor.
7482 /// </summary>
@@ -89,10 +97,7 @@ public static ExitCode Run(string[] args)
8997 var options = Options . CreateWithEnvironment ( args ) ;
9098 Entities . Compilation . Settings = ( Directory . GetCurrentDirectory ( ) , options . CompilerArguments . ToArray ( ) ) ;
9199
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 ) ;
96101
97102 if ( Environment . GetEnvironmentVariable ( "SEMMLE_CLRTRACER" ) == "1" && ! options . ClrTracer )
98103 {
@@ -276,7 +281,7 @@ private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.Comm
276281 /// The constructed syntax trees will be added (thread-safely) to the supplied
277282 /// list <paramref name="ret"/>.
278283 /// </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 )
280285 {
281286 return sources . Select < string , Action > ( path => ( ) =>
282287 {
@@ -298,31 +303,7 @@ private static IEnumerable<Action> ReadSyntaxTrees(IEnumerable<string> sources,
298303 } ) ;
299304 }
300305
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 ,
326307 Func < BlockingCollection < MetadataReference > , IEnumerable < Action > > getResolvedReferenceTasks ,
327308 Func < Analyser , List < SyntaxTree > , IEnumerable < Action > > getSyntaxTreeTasks ,
328309 Func < IEnumerable < SyntaxTree > , IEnumerable < MetadataReference > , CSharpCompilation > getCompilation ,
@@ -395,37 +376,6 @@ private static ExitCode Analyse(Stopwatch stopwatch, Analyser analyser, CommonOp
395376 return analyser . TotalErrors == 0 ? ExitCode . Ok : ExitCode . Errors ;
396377 }
397378
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-
429379 private static ExitCode AnalyseTracing (
430380 TracingAnalyser analyser ,
431381 CSharpCommandLineArguments compilerArguments ,
@@ -468,15 +418,6 @@ private static ExitCode AnalyseTracing(
468418 ( ) => { } ) ;
469419 }
470420
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-
480421 /// <summary>
481422 /// Gets the path to the `csharp.log` file written to by the C# extractor.
482423 /// </summary>
0 commit comments