mirror of
https://github.com/github/codeql.git
synced 2026-04-30 03:05:15 +02:00
Simplify standalone extractor
This commit is contained in:
@@ -42,17 +42,17 @@ namespace Semmle.Extraction.CSharp.Standalone
|
||||
(compilation, options) => analyser.Initialize(output.FullName, extractionInput.CompilationInfos, compilation, options),
|
||||
() =>
|
||||
{
|
||||
foreach (var type in analyser.MissingNamespaces)
|
||||
foreach (var type in analyser.ExtractionContext!.MissingNamespaces)
|
||||
{
|
||||
progressMonitor.MissingNamespace(type);
|
||||
}
|
||||
|
||||
foreach (var type in analyser.MissingTypes)
|
||||
foreach (var type in analyser.ExtractionContext!.MissingTypes)
|
||||
{
|
||||
progressMonitor.MissingType(type);
|
||||
}
|
||||
|
||||
progressMonitor.MissingSummary(analyser.MissingTypes.Count(), analyser.MissingNamespaces.Count());
|
||||
progressMonitor.MissingSummary(analyser.ExtractionContext!.MissingTypes.Count(), analyser.ExtractionContext!.MissingNamespaces.Count());
|
||||
});
|
||||
}
|
||||
finally
|
||||
@@ -69,29 +69,6 @@ namespace Semmle.Extraction.CSharp.Standalone
|
||||
}
|
||||
}
|
||||
|
||||
private static void ExtractStandalone(
|
||||
ExtractionInput extractionInput,
|
||||
IProgressMonitor pm,
|
||||
ILogger logger,
|
||||
CommonOptions options)
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
|
||||
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
|
||||
var pathTransformer = new PathTransformer(canonicalPathCache);
|
||||
|
||||
using var analyser = new StandaloneAnalyser(pm, logger, pathTransformer, canonicalPathCache, false);
|
||||
try
|
||||
{
|
||||
AnalyseStandalone(analyser, extractionInput, options, pm, stopwatch);
|
||||
}
|
||||
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
|
||||
{
|
||||
analyser.Logger.Log(Severity.Error, " Unhandled exception: {0}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private class ExtractionProgress : IProgressMonitor
|
||||
{
|
||||
public ExtractionProgress(ILogger output)
|
||||
@@ -141,8 +118,8 @@ namespace Semmle.Extraction.CSharp.Standalone
|
||||
|
||||
public static ExitCode Run(Options options)
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var overallStopwatch = new Stopwatch();
|
||||
overallStopwatch.Start();
|
||||
|
||||
using var logger = new ConsoleLogger(options.Verbosity, logThreadId: true);
|
||||
logger.Log(Severity.Info, "Extracting C# with build-mode set to 'none'");
|
||||
@@ -158,12 +135,26 @@ namespace Semmle.Extraction.CSharp.Standalone
|
||||
|
||||
logger.Log(Severity.Info, "");
|
||||
logger.Log(Severity.Info, "Extracting...");
|
||||
ExtractStandalone(
|
||||
new ExtractionInput(dependencyManager.AllSourceFiles, dependencyManager.ReferenceFiles, dependencyManager.CompilationInfos),
|
||||
new ExtractionProgress(logger),
|
||||
fileLogger,
|
||||
options);
|
||||
logger.Log(Severity.Info, $"Extraction completed in {stopwatch.Elapsed}");
|
||||
|
||||
var analyzerStopwatch = new Stopwatch();
|
||||
analyzerStopwatch.Start();
|
||||
|
||||
var canonicalPathCache = CanonicalPathCache.Create(fileLogger, 1000);
|
||||
var pathTransformer = new PathTransformer(canonicalPathCache);
|
||||
|
||||
var progressMonitor = new ExtractionProgress(logger);
|
||||
using var analyser = new StandaloneAnalyser(progressMonitor, fileLogger, pathTransformer, canonicalPathCache, false);
|
||||
try
|
||||
{
|
||||
var extractionInput = new ExtractionInput(dependencyManager.AllSourceFiles, dependencyManager.ReferenceFiles, dependencyManager.CompilationInfos);
|
||||
AnalyseStandalone(analyser, extractionInput, options, progressMonitor, analyzerStopwatch);
|
||||
}
|
||||
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
|
||||
{
|
||||
fileLogger.Log(Severity.Error, " Unhandled exception: {0}", ex);
|
||||
}
|
||||
|
||||
logger.Log(Severity.Info, $"Extraction completed in {overallStopwatch.Elapsed}");
|
||||
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Semmle.Extraction.CSharp
|
||||
/// </summary>
|
||||
public class Analyser : IDisposable
|
||||
{
|
||||
protected ExtractionContext? ExtractionContext;
|
||||
public ExtractionContext? ExtractionContext { get; protected set; }
|
||||
protected CSharpCompilation? compilation;
|
||||
protected CommonOptions? options;
|
||||
private protected Entities.Compilation? compilationEntity;
|
||||
|
||||
@@ -93,20 +93,13 @@ namespace Semmle.Extraction.CSharp
|
||||
/// <returns><see cref="ExitCode"/></returns>
|
||||
public static ExitCode Run(string[] args)
|
||||
{
|
||||
var stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
var analyzerStopwatch = new Stopwatch();
|
||||
analyzerStopwatch.Start();
|
||||
|
||||
var options = Options.CreateWithEnvironment(args);
|
||||
var workingDirectory = Directory.GetCurrentDirectory();
|
||||
var compilerArgs = options.CompilerArguments.ToArray();
|
||||
|
||||
using var logger = MakeLogger(options.Verbosity, options.Console);
|
||||
|
||||
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
|
||||
var pathTransformer = new PathTransformer(canonicalPathCache);
|
||||
|
||||
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, pathTransformer, canonicalPathCache, options.AssemblySensitiveTrap);
|
||||
|
||||
try
|
||||
{
|
||||
if (options.ProjectsToLoad.Any())
|
||||
@@ -115,13 +108,20 @@ namespace Semmle.Extraction.CSharp
|
||||
}
|
||||
|
||||
var compilerVersion = new CompilerVersion(options);
|
||||
|
||||
if (compilerVersion.SkipExtraction)
|
||||
{
|
||||
logger.Log(Severity.Warning, " Unrecognized compiler '{0}' because {1}", compilerVersion.SpecifiedCompiler, compilerVersion.SkipReason);
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
var workingDirectory = Directory.GetCurrentDirectory();
|
||||
var compilerArgs = options.CompilerArguments.ToArray();
|
||||
|
||||
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
|
||||
var pathTransformer = new PathTransformer(canonicalPathCache);
|
||||
|
||||
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, pathTransformer, canonicalPathCache, options.AssemblySensitiveTrap);
|
||||
|
||||
var compilerArguments = CSharpCommandLineParser.Default.Parse(
|
||||
compilerVersion.ArgsWithResponse,
|
||||
workingDirectory,
|
||||
@@ -144,7 +144,7 @@ namespace Semmle.Extraction.CSharp
|
||||
return ExitCode.Ok;
|
||||
}
|
||||
|
||||
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, stopwatch);
|
||||
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, analyzerStopwatch);
|
||||
}
|
||||
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
|
||||
{
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
@@ -22,13 +21,5 @@ namespace Semmle.Extraction.CSharp
|
||||
LogExtractorInfo();
|
||||
SetReferencePaths();
|
||||
}
|
||||
|
||||
#nullable disable warnings
|
||||
|
||||
public IEnumerable<string> MissingTypes => ExtractionContext.MissingTypes;
|
||||
|
||||
public IEnumerable<string> MissingNamespaces => ExtractionContext.MissingNamespaces;
|
||||
|
||||
#nullable restore warnings
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user