Skip to content

Commit 3551386

Browse files
committed
Simplify standalone extractor
1 parent cdca607 commit 3551386

File tree

4 files changed

+37
-55
lines changed

4 files changed

+37
-55
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/Extractor.cs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ private static void AnalyseStandalone(
4242
(compilation, options) => analyser.Initialize(output.FullName, extractionInput.CompilationInfos, compilation, options),
4343
() =>
4444
{
45-
foreach (var type in analyser.MissingNamespaces)
45+
foreach (var type in analyser.ExtractionContext!.MissingNamespaces)
4646
{
4747
progressMonitor.MissingNamespace(type);
4848
}
4949

50-
foreach (var type in analyser.MissingTypes)
50+
foreach (var type in analyser.ExtractionContext!.MissingTypes)
5151
{
5252
progressMonitor.MissingType(type);
5353
}
5454

55-
progressMonitor.MissingSummary(analyser.MissingTypes.Count(), analyser.MissingNamespaces.Count());
55+
progressMonitor.MissingSummary(analyser.ExtractionContext!.MissingTypes.Count(), analyser.ExtractionContext!.MissingNamespaces.Count());
5656
});
5757
}
5858
finally
@@ -69,29 +69,6 @@ private static void AnalyseStandalone(
6969
}
7070
}
7171

72-
private static void ExtractStandalone(
73-
ExtractionInput extractionInput,
74-
IProgressMonitor pm,
75-
ILogger logger,
76-
CommonOptions options)
77-
{
78-
var stopwatch = new Stopwatch();
79-
stopwatch.Start();
80-
81-
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
82-
var pathTransformer = new PathTransformer(canonicalPathCache);
83-
84-
using var analyser = new StandaloneAnalyser(pm, logger, pathTransformer, canonicalPathCache, false);
85-
try
86-
{
87-
AnalyseStandalone(analyser, extractionInput, options, pm, stopwatch);
88-
}
89-
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
90-
{
91-
analyser.Logger.Log(Severity.Error, " Unhandled exception: {0}", ex);
92-
}
93-
}
94-
9572
private class ExtractionProgress : IProgressMonitor
9673
{
9774
public ExtractionProgress(ILogger output)
@@ -141,8 +118,8 @@ public record ExtractionInput(IEnumerable<string> Sources, IEnumerable<string> R
141118

142119
public static ExitCode Run(Options options)
143120
{
144-
var stopwatch = new Stopwatch();
145-
stopwatch.Start();
121+
var overallStopwatch = new Stopwatch();
122+
overallStopwatch.Start();
146123

147124
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: true);
148125
logger.Log(Severity.Info, "Extracting C# with build-mode set to 'none'");
@@ -158,12 +135,26 @@ public static ExitCode Run(Options options)
158135

159136
logger.Log(Severity.Info, "");
160137
logger.Log(Severity.Info, "Extracting...");
161-
ExtractStandalone(
162-
new ExtractionInput(dependencyManager.AllSourceFiles, dependencyManager.ReferenceFiles, dependencyManager.CompilationInfos),
163-
new ExtractionProgress(logger),
164-
fileLogger,
165-
options);
166-
logger.Log(Severity.Info, $"Extraction completed in {stopwatch.Elapsed}");
138+
139+
var analyzerStopwatch = new Stopwatch();
140+
analyzerStopwatch.Start();
141+
142+
var canonicalPathCache = CanonicalPathCache.Create(fileLogger, 1000);
143+
var pathTransformer = new PathTransformer(canonicalPathCache);
144+
145+
var progressMonitor = new ExtractionProgress(logger);
146+
using var analyser = new StandaloneAnalyser(progressMonitor, fileLogger, pathTransformer, canonicalPathCache, false);
147+
try
148+
{
149+
var extractionInput = new ExtractionInput(dependencyManager.AllSourceFiles, dependencyManager.ReferenceFiles, dependencyManager.CompilationInfos);
150+
AnalyseStandalone(analyser, extractionInput, options, progressMonitor, analyzerStopwatch);
151+
}
152+
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
153+
{
154+
fileLogger.Log(Severity.Error, " Unhandled exception: {0}", ex);
155+
}
156+
157+
logger.Log(Severity.Info, $"Extraction completed in {overallStopwatch.Elapsed}");
167158

168159
return ExitCode.Ok;
169160
}

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Analyser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp
1919
/// </summary>
2020
public class Analyser : IDisposable
2121
{
22-
protected ExtractionContext? ExtractionContext;
22+
public ExtractionContext? ExtractionContext { get; protected set; }
2323
protected CSharpCompilation? compilation;
2424
protected CommonOptions? options;
2525
private protected Entities.Compilation? compilationEntity;

csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,13 @@ public static ILogger MakeLogger(Verbosity verbosity, bool includeConsole)
9393
/// <returns><see cref="ExitCode"/></returns>
9494
public static ExitCode Run(string[] args)
9595
{
96-
var stopwatch = new Stopwatch();
97-
stopwatch.Start();
96+
var analyzerStopwatch = new Stopwatch();
97+
analyzerStopwatch.Start();
9898

9999
var options = Options.CreateWithEnvironment(args);
100-
var workingDirectory = Directory.GetCurrentDirectory();
101-
var compilerArgs = options.CompilerArguments.ToArray();
102100

103101
using var logger = MakeLogger(options.Verbosity, options.Console);
104102

105-
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
106-
var pathTransformer = new PathTransformer(canonicalPathCache);
107-
108-
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, pathTransformer, canonicalPathCache, options.AssemblySensitiveTrap);
109-
110103
try
111104
{
112105
if (options.ProjectsToLoad.Any())
@@ -115,13 +108,20 @@ public static ExitCode Run(string[] args)
115108
}
116109

117110
var compilerVersion = new CompilerVersion(options);
118-
119111
if (compilerVersion.SkipExtraction)
120112
{
121113
logger.Log(Severity.Warning, " Unrecognized compiler '{0}' because {1}", compilerVersion.SpecifiedCompiler, compilerVersion.SkipReason);
122114
return ExitCode.Ok;
123115
}
124116

117+
var workingDirectory = Directory.GetCurrentDirectory();
118+
var compilerArgs = options.CompilerArguments.ToArray();
119+
120+
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
121+
var pathTransformer = new PathTransformer(canonicalPathCache);
122+
123+
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, pathTransformer, canonicalPathCache, options.AssemblySensitiveTrap);
124+
125125
var compilerArguments = CSharpCommandLineParser.Default.Parse(
126126
compilerVersion.ArgsWithResponse,
127127
workingDirectory,
@@ -144,7 +144,7 @@ public static ExitCode Run(string[] args)
144144
return ExitCode.Ok;
145145
}
146146

147-
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, stopwatch);
147+
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, analyzerStopwatch);
148148
}
149149
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
150150
{

csharp/extractor/Semmle.Extraction.CSharp/Extractor/StandaloneAnalyser.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.IO;
43
using Microsoft.CodeAnalysis.CSharp;
@@ -22,13 +21,5 @@ public void Initialize(string outputPath, IEnumerable<(string, string)> compilat
2221
LogExtractorInfo();
2322
SetReferencePaths();
2423
}
25-
26-
#nullable disable warnings
27-
28-
public IEnumerable<string> MissingTypes => ExtractionContext.MissingTypes;
29-
30-
public IEnumerable<string> MissingNamespaces => ExtractionContext.MissingNamespaces;
31-
32-
#nullable restore warnings
3324
}
3425
}

0 commit comments

Comments
 (0)