Skip to content

Commit c0d623c

Browse files
authored
Merge pull request github#16567 from tamasvajk/refactor/static-extraction-state
C#: Refactor static compilation state
2 parents 4905612 + 7042f32 commit c0d623c

File tree

7 files changed

+35
-33
lines changed

7 files changed

+35
-33
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Compilation.cs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,35 @@ internal class Compilation : CachedEntity<object>
1111
{
1212
internal readonly ConcurrentDictionary<string, int> messageCounts = new();
1313

14-
private static (string Cwd, string[] Args) settings;
15-
private static int hashCode;
16-
17-
public static (string Cwd, string[] Args) Settings
18-
{
19-
get { return settings; }
20-
set
21-
{
22-
settings = value;
23-
hashCode = settings.Cwd.GetHashCode();
24-
for (var i = 0; i < settings.Args.Length; i++)
25-
{
26-
hashCode = HashCode.Combine(hashCode, settings.Args[i].GetHashCode());
27-
}
28-
}
29-
}
14+
private readonly string cwd;
15+
private readonly string[] args;
16+
private readonly int hashCode;
3017

3118
#nullable disable warnings
3219
private Compilation(Context cx) : base(cx, null)
3320
{
21+
cwd = cx.Extractor.Cwd;
22+
args = cx.Extractor.Args;
23+
hashCode = cwd.GetHashCode();
24+
for (var i = 0; i < args.Length; i++)
25+
{
26+
hashCode = HashCode.Combine(hashCode, args[i].GetHashCode());
27+
}
3428
}
3529
#nullable restore warnings
3630

3731
public override void Populate(TextWriter trapFile)
3832
{
3933
var assembly = Assembly.CreateOutputAssembly(Context);
4034

41-
trapFile.compilations(this, FileUtils.ConvertToUnix(Compilation.Settings.Cwd));
35+
trapFile.compilations(this, FileUtils.ConvertToUnix(cwd));
4236
trapFile.compilation_assembly(this, assembly);
4337

4438
// Arguments
4539
var expandedIndex = 0;
46-
for (var i = 0; i < Compilation.Settings.Args.Length; i++)
40+
for (var i = 0; i < args.Length; i++)
4741
{
48-
var arg = Compilation.Settings.Args[i];
42+
var arg = args[i];
4943
trapFile.compilation_args(this, i, arg);
5044

5145
if (CommandLineExtensions.IsFileArgument(arg))

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ public static ExitCode Run(string[] args)
9797
stopwatch.Start();
9898

9999
var options = Options.CreateWithEnvironment(args);
100-
Entities.Compilation.Settings = (Directory.GetCurrentDirectory(), options.CompilerArguments.ToArray());
100+
var workingDirectory = Directory.GetCurrentDirectory();
101+
var compilerArgs = options.CompilerArguments.ToArray();
101102

102103
using var logger = MakeLogger(options.Verbosity, options.Console);
103104

@@ -123,15 +124,15 @@ public static ExitCode Run(string[] args)
123124

124125
var compilerArguments = CSharpCommandLineParser.Default.Parse(
125126
compilerVersion.ArgsWithResponse,
126-
Entities.Compilation.Settings.Cwd,
127+
workingDirectory,
127128
compilerVersion.FrameworkPath,
128129
compilerVersion.AdditionalReferenceDirectories
129130
);
130131

131132
if (compilerArguments is null)
132133
{
133134
var sb = new StringBuilder();
134-
sb.Append(" Failed to parse command line: ").AppendList(" ", Entities.Compilation.Settings.Args);
135+
sb.Append(" Failed to parse command line: ").AppendList(" ", compilerArgs);
135136
logger.Log(Severity.Error, sb.ToString());
136137
++analyser.CompilationErrors;
137138
return ExitCode.Failed;
@@ -143,7 +144,7 @@ public static ExitCode Run(string[] args)
143144
return ExitCode.Ok;
144145
}
145146

146-
return AnalyseTracing(analyser, compilerArguments, options, canonicalPathCache, stopwatch);
147+
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, canonicalPathCache, stopwatch);
147148
}
148149
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
149150
{
@@ -376,6 +377,8 @@ public static ExitCode Analyse(Stopwatch stopwatch, Analyser analyser, CommonOpt
376377
}
377378

378379
private static ExitCode AnalyseTracing(
380+
string cwd,
381+
string[] args,
379382
TracingAnalyser analyser,
380383
CSharpCommandLineArguments compilerArguments,
381384
Options options,
@@ -420,7 +423,7 @@ private static ExitCode AnalyseTracing(
420423
.WithMetadataImportOptions(MetadataImportOptions.All)
421424
);
422425
},
423-
(compilation, options) => analyser.EndInitialize(compilerArguments, options, compilation),
426+
(compilation, options) => analyser.EndInitialize(compilerArguments, options, compilation, cwd, args),
424427
() => { });
425428
}
426429

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ public StandaloneAnalyser(IProgressMonitor pm, ILogger logger, bool addAssemblyT
1616
public void Initialize(string outputPath, IEnumerable<(string, string)> compilationInfos, CSharpCompilation compilationIn, CommonOptions options)
1717
{
1818
compilation = compilationIn;
19-
extractor = new StandaloneExtractor(outputPath, compilationInfos, Logger, PathTransformer, options);
19+
extractor = new StandaloneExtractor(Directory.GetCurrentDirectory(), outputPath, compilationInfos, Logger, PathTransformer, options);
2020
this.options = options;
2121
LogExtractorInfo(Extraction.Extractor.Version);
2222
SetReferencePaths();
23-
24-
Entities.Compilation.Settings = (Directory.GetCurrentDirectory(), Array.Empty<string>());
2523
}
2624

2725
#nullable disable warnings

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ public bool BeginInitialize(IEnumerable<string> roslynArgs)
3838
public void EndInitialize(
3939
CSharpCommandLineArguments commandLineArguments,
4040
CommonOptions options,
41-
CSharpCompilation compilation)
41+
CSharpCompilation compilation,
42+
string cwd,
43+
string[] args)
4244
{
4345
if (!init)
4446
throw new InternalError("EndInitialize called without BeginInitialize returning true");
4547
this.options = options;
4648
this.compilation = compilation;
47-
this.extractor = new TracingExtractor(GetOutputName(compilation, commandLineArguments), Logger, PathTransformer, options);
49+
this.extractor = new TracingExtractor(cwd, args, GetOutputName(compilation, commandLineArguments), Logger, PathTransformer, options);
4850
LogDiagnostics();
4951

5052
SetReferencePaths();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Semmle.Extraction
1010
/// </summary>
1111
public abstract class Extractor
1212
{
13+
public string Cwd { get; init; }
14+
public string[] Args { get; init; }
1315
public abstract ExtractorMode Mode { get; }
1416
public string OutputPath { get; }
1517
public IEnumerable<CompilationInfo> CompilationInfos { get; }
@@ -19,12 +21,14 @@ public abstract class Extractor
1921
/// </summary>
2022
/// <param name="logger">The object used for logging.</param>
2123
/// <param name="pathTransformer">The object used for path transformations.</param>
22-
protected Extractor(string outputPath, IEnumerable<CompilationInfo> compilationInfos, ILogger logger, PathTransformer pathTransformer)
24+
protected Extractor(string cwd, string[] args, string outputPath, IEnumerable<CompilationInfo> compilationInfos, ILogger logger, PathTransformer pathTransformer)
2325
{
2426
OutputPath = outputPath;
2527
Logger = logger;
2628
PathTransformer = pathTransformer;
2729
CompilationInfos = compilationInfos;
30+
Cwd = cwd;
31+
Args = args;
2832
}
2933

3034
// Limit the number of error messages in the log file

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public class StandaloneExtractor : Extractor
1212
/// </summary>
1313
/// <param name="logger">The object used for logging.</param>
1414
/// <param name="pathTransformer">The object used for path transformations.</param>
15-
public StandaloneExtractor(string outputPath, IEnumerable<(string, string)> compilationInfos, ILogger logger, PathTransformer pathTransformer, CommonOptions options) : base(outputPath, compilationInfos, logger, pathTransformer)
15+
public StandaloneExtractor(string cwd, string outputPath, IEnumerable<(string, string)> compilationInfos, ILogger logger, PathTransformer pathTransformer, CommonOptions options)
16+
: base(cwd, [], outputPath, compilationInfos, logger, pathTransformer)
1617
{
1718
Mode = ExtractorMode.Standalone;
1819
if (options.QlTest)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Linq;
21
using Semmle.Util.Logging;
32

43
namespace Semmle.Extraction
@@ -13,7 +12,8 @@ public class TracingExtractor : Extractor
1312
/// <param name="outputPath">The name of the output DLL/EXE, or null if not specified (standalone extraction).</param>
1413
/// <param name="logger">The object used for logging.</param>
1514
/// <param name="pathTransformer">The object used for path transformations.</param>
16-
public TracingExtractor(string outputPath, ILogger logger, PathTransformer pathTransformer, CommonOptions options) : base(outputPath, Enumerable.Empty<(string, string)>(), logger, pathTransformer)
15+
public TracingExtractor(string cwd, string[] args, string outputPath, ILogger logger, PathTransformer pathTransformer, CommonOptions options)
16+
: base(cwd, args, outputPath, [], logger, pathTransformer)
1717
{
1818
Mode = ExtractorMode.None;
1919
if (options.QlTest)

0 commit comments

Comments
 (0)