Skip to content

Commit af2a78e

Browse files
committed
Reduce references to Extract class
1 parent 11faf08 commit af2a78e

File tree

13 files changed

+87
-89
lines changed

13 files changed

+87
-89
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static void ExtractStandalone(
8181
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
8282
var pathTransformer = new PathTransformer(canonicalPathCache);
8383

84-
using var analyser = new StandaloneAnalyser(pm, logger, false, pathTransformer);
84+
using var analyser = new StandaloneAnalyser(pm, logger, pathTransformer, canonicalPathCache, false);
8585
try
8686
{
8787
AnalyseStandalone(analyser, extractionInput, options, pm, stopwatch);

csharp/extractor/Semmle.Extraction.CSharp/Entities/NonGeneratedSourceLocation.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Populate(TextWriter trapFile)
2727
var mapped = Symbol.GetMappedLineSpan();
2828
if (mapped.HasMappedPath && mapped.IsValid)
2929
{
30-
var path = TryAdjustRelativeMappedFilePath(mapped.Path, Position.Path, Context.Extractor.Logger);
30+
var path = Context.TryAdjustRelativeMappedFilePath(mapped.Path, Position.Path);
3131
var mappedLoc = Create(Context, Location.Create(path, default, mapped.Span));
3232

3333
trapFile.locations_mapped(this, mappedLoc);
@@ -64,25 +64,5 @@ private class SourceLocationFactory : CachedEntityFactory<Location, NonGenerated
6464

6565
public override NonGeneratedSourceLocation Create(Context cx, Location init) => new NonGeneratedSourceLocation(cx, init);
6666
}
67-
68-
public static string TryAdjustRelativeMappedFilePath(string mappedToPath, string mappedFromPath, ILogger logger)
69-
{
70-
if (!Path.IsPathRooted(mappedToPath))
71-
{
72-
try
73-
{
74-
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(mappedFromPath)!, mappedToPath));
75-
logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");
76-
77-
mappedToPath = fullPath;
78-
}
79-
catch (Exception e)
80-
{
81-
logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
82-
}
83-
}
84-
85-
return mappedToPath;
86-
}
8767
}
8868
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineOrSpanDirective.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected override void PopulatePreprocessor(TextWriter trapFile)
2828
var path = Symbol.File.ValueText;
2929
if (!string.IsNullOrWhiteSpace(path))
3030
{
31-
path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(path, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
31+
path = Context.TryAdjustRelativeMappedFilePath(path, Symbol.SyntaxTree.FilePath);
3232
var file = File.Create(Context, path);
3333
trapFile.directive_line_file(this, file);
3434
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/PragmaChecksumDirective.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private PragmaChecksumDirective(Context cx, PragmaChecksumDirectiveTriviaSyntax
1212

1313
protected override void PopulatePreprocessor(TextWriter trapFile)
1414
{
15-
var path = NonGeneratedSourceLocation.TryAdjustRelativeMappedFilePath(Symbol.File.ValueText, Symbol.SyntaxTree.FilePath, Context.Extractor.Logger);
15+
var path = Context.TryAdjustRelativeMappedFilePath(Symbol.File.ValueText, Symbol.SyntaxTree.FilePath);
1616
var file = File.Create(Context, path);
1717
trapFile.pragma_checksums(this, file, Symbol.Guid.ToString(), Symbol.Bytes.ToString());
1818
}

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Semmle.Util;
1111
using Semmle.Util.Logging;
1212
using Semmle.Extraction.CSharp.Populators;
13+
using System.Reflection;
1314

1415
namespace Semmle.Extraction.CSharp
1516
{
@@ -38,14 +39,23 @@ public class Analyser : IDisposable
3839

3940
public PathTransformer PathTransformer { get; }
4041

41-
protected Analyser(IProgressMonitor pm, ILogger logger, bool addAssemblyTrapPrefix, PathTransformer pathTransformer)
42+
public IPathCache PathCache { get; }
43+
44+
protected Analyser(
45+
IProgressMonitor pm,
46+
ILogger logger,
47+
PathTransformer pathTransformer,
48+
IPathCache pathCache,
49+
bool addAssemblyTrapPrefix)
4250
{
4351
Logger = logger;
52+
PathTransformer = pathTransformer;
53+
PathCache = pathCache;
4454
this.addAssemblyTrapPrefix = addAssemblyTrapPrefix;
55+
this.progressMonitor = pm;
56+
4557
Logger.Log(Severity.Info, "EXTRACTION STARTING at {0}", DateTime.Now);
4658
stopWatch.Start();
47-
progressMonitor = pm;
48-
PathTransformer = pathTransformer;
4959
}
5060

5161
/// <summary>
@@ -333,11 +343,26 @@ public virtual void Dispose()
333343
/// <summary>
334344
/// Logs information about the extractor.
335345
/// </summary>
336-
public void LogExtractorInfo(string extractorVersion)
346+
public void LogExtractorInfo()
337347
{
338348
Logger.Log(Severity.Info, " Extractor: {0}", Environment.GetCommandLineArgs().First());
339-
Logger.Log(Severity.Info, " Extractor version: {0}", extractorVersion);
349+
Logger.Log(Severity.Info, " Extractor version: {0}", Version);
340350
Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory());
341351
}
352+
353+
private static string Version
354+
{
355+
get
356+
{
357+
// the attribute for the git information are always attached to the entry assembly by our build system
358+
var assembly = Assembly.GetEntryAssembly();
359+
var versionString = assembly?.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
360+
if (versionString == null)
361+
{
362+
return "unknown (not built from internal bazel workspace)";
363+
}
364+
return versionString.InformationalVersion;
365+
}
366+
}
342367
}
343368
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics.CodeAnalysis;
4+
using System.IO;
45
using Microsoft.CodeAnalysis;
56
using Semmle.Extraction.Entities;
67

@@ -178,5 +179,25 @@ internal bool ExtractGenerics(CachedEntity entity)
178179
extractedGenerics.Add(entity.Label);
179180
return true;
180181
}
182+
183+
public string TryAdjustRelativeMappedFilePath(string mappedToPath, string mappedFromPath)
184+
{
185+
if (!Path.IsPathRooted(mappedToPath))
186+
{
187+
try
188+
{
189+
var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(mappedFromPath)!, mappedToPath));
190+
Extractor.Logger.LogDebug($"Found relative path in line mapping: '{mappedToPath}', interpreting it as '{fullPath}'");
191+
192+
mappedToPath = fullPath;
193+
}
194+
catch (Exception e)
195+
{
196+
Extractor.Logger.LogDebug($"Failed to compute absolute path for relative path in line mapping: '{mappedToPath}': {e}");
197+
}
198+
}
199+
200+
return mappedToPath;
201+
}
181202
}
182203
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public static ExitCode Run(string[] args)
105105
var canonicalPathCache = CanonicalPathCache.Create(logger, 1000);
106106
var pathTransformer = new PathTransformer(canonicalPathCache);
107107

108-
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, options.AssemblySensitiveTrap, pathTransformer);
108+
using var analyser = new TracingAnalyser(new LogProgressMonitor(logger), logger, pathTransformer, canonicalPathCache, options.AssemblySensitiveTrap);
109109

110110
try
111111
{
@@ -144,7 +144,7 @@ public static ExitCode Run(string[] args)
144144
return ExitCode.Ok;
145145
}
146146

147-
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, canonicalPathCache, stopwatch);
147+
return AnalyseTracing(workingDirectory, compilerArgs, analyser, compilerArguments, options, stopwatch);
148148
}
149149
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
150150
{
@@ -226,7 +226,7 @@ private static MetadataReference MakeReference(CommandLineReference reference, s
226226
/// The resolved references will be added (thread-safely) to the supplied
227227
/// list <paramref name="ret"/>.
228228
/// </summary>
229-
private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.CommandLineArguments args, Analyser analyser, CanonicalPathCache canonicalPathCache, BlockingCollection<MetadataReference> ret)
229+
private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.CommandLineArguments args, Analyser analyser, BlockingCollection<MetadataReference> ret)
230230
{
231231
var referencePaths = new Lazy<string[]>(() => FixedReferencePaths(args).ToArray());
232232
return args.MetadataReferences.Select<CommandLineReference, Action>(clref => () =>
@@ -235,7 +235,7 @@ private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.Comm
235235
{
236236
if (File.Exists(clref.Reference))
237237
{
238-
var reference = MakeReference(clref, canonicalPathCache.GetCanonicalPath(clref.Reference));
238+
var reference = MakeReference(clref, analyser.PathCache.GetCanonicalPath(clref.Reference));
239239
ret.Add(reference);
240240
}
241241
else
@@ -252,7 +252,7 @@ private static IEnumerable<Action> ResolveReferences(Microsoft.CodeAnalysis.Comm
252252
var composed = referencePaths.Value
253253
.Select(path => Path.Combine(path, clref.Reference))
254254
.Where(path => File.Exists(path))
255-
.Select(path => canonicalPathCache.GetCanonicalPath(path))
255+
.Select(path => analyser.PathCache.GetCanonicalPath(path))
256256
.FirstOrDefault();
257257

258258
if (composed is not null)
@@ -382,11 +382,10 @@ private static ExitCode AnalyseTracing(
382382
TracingAnalyser analyser,
383383
CSharpCommandLineArguments compilerArguments,
384384
Options options,
385-
CanonicalPathCache canonicalPathCache,
386385
Stopwatch stopwatch)
387386
{
388387
return Analyse(stopwatch, analyser, options,
389-
references => ResolveReferences(compilerArguments, analyser, canonicalPathCache, references),
388+
references => ResolveReferences(compilerArguments, analyser, references),
390389
(analyser, syntaxTrees) =>
391390
{
392391
var paths = compilerArguments.SourceFiles
@@ -399,7 +398,7 @@ private static ExitCode AnalyseTracing(
399398
}
400399

401400
return ReadSyntaxTrees(
402-
paths.Select(canonicalPathCache.GetCanonicalPath),
401+
paths.Select(analyser.PathCache.GetCanonicalPath),
403402
analyser,
404403
compilerArguments.ParseOptions,
405404
compilerArguments.Encoding,

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using Microsoft.CodeAnalysis.CSharp;
5+
using Semmle.Util;
56
using Semmle.Util.Logging;
67

78
namespace Semmle.Extraction.CSharp
89
{
910
public class StandaloneAnalyser : Analyser
1011
{
11-
public StandaloneAnalyser(IProgressMonitor pm, ILogger logger, bool addAssemblyTrapPrefix, PathTransformer pathTransformer)
12-
: base(pm, logger, addAssemblyTrapPrefix, pathTransformer)
12+
public StandaloneAnalyser(IProgressMonitor pm, ILogger logger, PathTransformer pathTransformer, IPathCache pathCache, bool addAssemblyTrapPrefix)
13+
: base(pm, logger, pathTransformer, pathCache, addAssemblyTrapPrefix)
1314
{
1415
}
1516

@@ -18,7 +19,7 @@ public void Initialize(string outputPath, IEnumerable<(string, string)> compilat
1819
compilation = compilationIn;
1920
extractor = new Extraction.Extractor(Directory.GetCurrentDirectory(), [], outputPath, compilationInfos, Logger, PathTransformer, ExtractorMode.Standalone, options.QlTest);
2021
this.options = options;
21-
LogExtractorInfo(Extraction.Extractor.Version);
22+
LogExtractorInfo();
2223
SetReferencePaths();
2324
}
2425

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

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class TracingAnalyser : Analyser
1313
{
1414
private bool init;
1515

16-
public TracingAnalyser(IProgressMonitor pm, ILogger logger, bool addAssemblyTrapPrefix, PathTransformer pathTransformer)
17-
: base(pm, logger, addAssemblyTrapPrefix, pathTransformer)
16+
public TracingAnalyser(IProgressMonitor pm, ILogger logger, PathTransformer pathTransformer, IPathCache pathCache, bool addAssemblyTrapPrefix)
17+
: base(pm, logger, pathTransformer, pathCache, addAssemblyTrapPrefix)
1818
{
1919
}
2020

@@ -25,7 +25,8 @@ public TracingAnalyser(IProgressMonitor pm, ILogger logger, bool addAssemblyTrap
2525
/// <returns>A Boolean indicating whether to proceed with extraction.</returns>
2626
public bool BeginInitialize(IEnumerable<string> roslynArgs)
2727
{
28-
return init = LogRoslynArgs(roslynArgs, Extraction.Extractor.Version);
28+
LogExtractorInfo();
29+
return init = LogRoslynArgs(roslynArgs);
2930
}
3031

3132
/// <summary>
@@ -47,21 +48,20 @@ public void EndInitialize(
4748
this.options = options;
4849
this.compilation = compilation;
4950
this.extractor = new Extraction.Extractor(cwd, args, GetOutputName(compilation, commandLineArguments), [], Logger, PathTransformer, ExtractorMode.None, options.QlTest);
50-
LogDiagnostics();
51+
var errorCount = LogDiagnostics(compilation);
5152

5253
SetReferencePaths();
5354

54-
CompilationErrors += FilteredDiagnostics.Count();
55+
CompilationErrors += errorCount;
5556
}
5657

5758
/// <summary>
5859
/// Logs information about the extractor, as well as the arguments to Roslyn.
5960
/// </summary>
6061
/// <param name="roslynArgs">The arguments passed to Roslyn.</param>
6162
/// <returns>A Boolean indicating whether the same arguments have been logged previously.</returns>
62-
private bool LogRoslynArgs(IEnumerable<string> roslynArgs, string extractorVersion)
63+
private bool LogRoslynArgs(IEnumerable<string> roslynArgs)
6364
{
64-
LogExtractorInfo(extractorVersion);
6565
Logger.Log(Severity.Info, $" Arguments to Roslyn: {string.Join(' ', roslynArgs)}");
6666

6767
var tempFile = Extractor.GetCSharpArgsLogPath(Path.GetRandomFileName());
@@ -137,27 +137,27 @@ private static string GetOutputName(CSharpCompilation compilation,
137137
return Path.Combine(commandLineArguments.OutputDirectory, commandLineArguments.OutputFileName);
138138
}
139139

140-
#nullable disable warnings
141-
142-
/// <summary>
143-
/// Logs detailed information about this invocation,
144-
/// in the event that errors were detected.
145-
/// </summary>
146-
/// <returns>A Boolean indicating whether to proceed with extraction.</returns>
147-
private void LogDiagnostics()
140+
private int LogDiagnostics(CSharpCompilation compilation)
148141
{
149-
foreach (var error in FilteredDiagnostics)
142+
var filteredDiagnostics = compilation
143+
.GetDiagnostics()
144+
.Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id))
145+
.ToList();
146+
147+
foreach (var error in filteredDiagnostics)
150148
{
151149
Logger.Log(Severity.Error, " Compilation error: {0}", error);
152150
}
153151

154-
if (FilteredDiagnostics.Any())
152+
if (filteredDiagnostics.Count != 0)
155153
{
156154
foreach (var reference in compilation.References)
157155
{
158156
Logger.Log(Severity.Info, " Resolved reference {0}", reference.Display);
159157
}
160158
}
159+
160+
return filteredDiagnostics.Count;
161161
}
162162

163163
private static readonly HashSet<string> errorsToIgnore = new HashSet<string>
@@ -166,17 +166,5 @@ private void LogDiagnostics()
166166
"CS1589", // XML referencing not supported
167167
"CS1569" // Error writing XML documentation
168168
};
169-
170-
private IEnumerable<Diagnostic> FilteredDiagnostics
171-
{
172-
get
173-
{
174-
return extractor is null || extractor.Mode.HasFlag(ExtractorMode.Standalone) || compilation is null ? Enumerable.Empty<Diagnostic>() :
175-
compilation.
176-
GetDiagnostics().
177-
Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id));
178-
}
179-
}
180-
#nullable restore warnings
181169
}
182170
}

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected void DefineLabel(IEntity entity)
5151
try
5252
{
5353
writingLabel = true;
54-
entity.DefineLabel(TrapWriter.Writer, Extractor);
54+
entity.DefineLabel(TrapWriter.Writer);
5555
}
5656
finally
5757
{

0 commit comments

Comments
 (0)