C#: Add compilation errors to the debug log in BMN.

This commit is contained in:
Michael Nebel
2025-11-17 14:10:08 +01:00
parent b0dc48e393
commit f1b12203f6
3 changed files with 28 additions and 13 deletions

View File

@@ -360,5 +360,22 @@ namespace Semmle.Extraction.CSharp
return versionString.InformationalVersion; return versionString.InformationalVersion;
} }
} }
private static readonly HashSet<string> errorsToIgnore = new HashSet<string>
{
"CS7027", // Code signing failure
"CS1589", // XML referencing not supported
"CS1569" // Error writing XML documentation
};
/// <summary>
/// Retrieves the diagnostics from the compilation, filtering out those that should be ignored.
/// </summary>
protected List<Diagnostic> GetFilteredDiagnostics() =>
compilation is not null
? compilation.GetDiagnostics()
.Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id))
.ToList()
: [];
} }
} }

View File

@@ -13,6 +13,14 @@ namespace Semmle.Extraction.CSharp
{ {
} }
private void LogDiagnostics()
{
foreach (var error in GetFilteredDiagnostics())
{
Logger.LogDebug($" Compilation error: {error}");
}
}
public void Initialize(string outputPath, IEnumerable<(string, string)> compilationInfos, CSharpCompilation compilationIn, CommonOptions options) public void Initialize(string outputPath, IEnumerable<(string, string)> compilationInfos, CSharpCompilation compilationIn, CommonOptions options)
{ {
compilation = compilationIn; compilation = compilationIn;
@@ -20,6 +28,7 @@ namespace Semmle.Extraction.CSharp
this.options = options; this.options = options;
LogExtractorInfo(); LogExtractorInfo();
SetReferencePaths(); SetReferencePaths();
LogDiagnostics();
} }
} }
} }

View File

@@ -136,11 +136,7 @@ namespace Semmle.Extraction.CSharp
private int LogDiagnostics() private int LogDiagnostics()
{ {
var filteredDiagnostics = compilation! var filteredDiagnostics = GetFilteredDiagnostics();
.GetDiagnostics()
.Where(e => e.Severity >= DiagnosticSeverity.Error && !errorsToIgnore.Contains(e.Id))
.ToList();
foreach (var error in filteredDiagnostics) foreach (var error in filteredDiagnostics)
{ {
Logger.LogError($" Compilation error: {error}"); Logger.LogError($" Compilation error: {error}");
@@ -148,7 +144,7 @@ namespace Semmle.Extraction.CSharp
if (filteredDiagnostics.Count != 0) if (filteredDiagnostics.Count != 0)
{ {
foreach (var reference in compilation.References) foreach (var reference in compilation!.References)
{ {
Logger.LogInfo($" Resolved reference {reference.Display}"); Logger.LogInfo($" Resolved reference {reference.Display}");
} }
@@ -156,12 +152,5 @@ namespace Semmle.Extraction.CSharp
return filteredDiagnostics.Count; return filteredDiagnostics.Count;
} }
private static readonly HashSet<string> errorsToIgnore = new HashSet<string>
{
"CS7027", // Code signing failure
"CS1589", // XML referencing not supported
"CS1569" // Error writing XML documentation
};
} }
} }