Skip to content

Commit 205d6a3

Browse files
committed
Extract total number of diagnostic per ID and compilation
1 parent fa7f437 commit 205d6a3

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.IO;
34
using System.Linq;
45
using Microsoft.CodeAnalysis;
@@ -8,6 +9,8 @@ namespace Semmle.Extraction.CSharp.Entities
89
{
910
internal class Compilation : CachedEntity<object>
1011
{
12+
internal readonly ConcurrentDictionary<string, int> messageCounts = new();
13+
1114
private static (string Cwd, string[] Args) settings;
1215
private static int hashCode;
1316

@@ -78,9 +81,11 @@ public override void Populate(TextWriter trapFile)
7881
.ForEach((file, index) => trapFile.compilation_referencing_files(this, index, file));
7982

8083
// Diagnostics
81-
Context.Compilation
82-
.GetDiagnostics()
83-
.ForEach((diag, index) => new CompilerDiagnostic(Context, diag, this, index));
84+
var diags = Context.Compilation.GetDiagnostics();
85+
diags.ForEach((diag, index) => new CompilerDiagnostic(Context, diag, this, index));
86+
87+
var diagCounts = diags.GroupBy(diag => diag.Id).ToDictionary(group => group.Key, group => group.Count());
88+
diagCounts.ForEach(pair => trapFile.compilation_info(this, $"Compiler diagnostic count for {pair.Key}", pair.Value.ToString()));
8489
}
8590

8691
public void PopulatePerformance(PerformanceMetrics p)

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

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

@@ -7,7 +6,6 @@ namespace Semmle.Extraction.CSharp.Entities
76
internal class CompilerDiagnostic : FreshEntity
87
{
98
private static readonly int limit = EnvironmentVariables.TryGetExtractorNumberOption<int>("COMPILER_DIAGNOSTIC_LIMIT") ?? 1000;
10-
private static readonly ConcurrentDictionary<string, int> messageCounts = new();
119

1210
private readonly Microsoft.CodeAnalysis.Diagnostic diagnostic;
1311
private readonly Compilation compilation;
@@ -25,12 +23,12 @@ protected override void Populate(TextWriter trapFile)
2523
{
2624
// The below doesn't limit the extractor messages to the exact limit, but it's good enough.
2725
var key = diagnostic.Id;
28-
var messageCount = messageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);
26+
var messageCount = compilation.messageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);
2927
if (messageCount > limit)
3028
{
3129
if (messageCount == limit + 1)
3230
{
33-
Context.Extractor.Logger.LogWarning($"Stopped logging {key} compiler diagnostics after reaching {limit}");
31+
Context.Extractor.Logger.LogWarning($"Stopped logging {key} compiler diagnostics for the current compilation after reaching {limit}");
3432
}
3533

3634
return;

csharp/ql/integration-tests/all-platforms/standalone/Diag.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ extractorMessages
22
| 5 |
33
compilerDiagnostics
44
| 4 |
5+
compilationInfo
6+
| Compiler diagnostic count for CS0103 | 3.0 |
7+
| Compiler diagnostic count for CS8019 | 7.0 |

csharp/ql/integration-tests/all-platforms/standalone/Diag.ql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,11 @@ import semmle.code.csharp.commons.Diagnostics
44
query predicate extractorMessages(int c) { c = count(ExtractorMessage msg) }
55

66
query predicate compilerDiagnostics(int c) { c = count(Diagnostic diag) }
7+
8+
query predicate compilationInfo(string key, float value) {
9+
exists(Compilation c, string infoValue |
10+
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%")
11+
|
12+
value = infoValue.toFloat()
13+
)
14+
}

csharp/ql/integration-tests/posix-only/standalone_dependencies_nuget_config_error/CompilationInfo.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import semmle.code.csharp.commons.Diagnostics
33

44
query predicate compilationInfo(string key, float value) {
55
key != "Resolved references" and
6+
not key.matches("Compiler diagnostic count for%") and
67
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
78
key = infoKey and
89
value = infoValue.toFloat()

csharp/ql/src/Telemetry/ExtractorInformation.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import csharp
1010
import semmle.code.csharp.commons.Diagnostics
1111

1212
predicate compilationInfo(string key, float value) {
13+
not key.matches("Compiler diagnostic count for%") and
1314
exists(Compilation c, string infoKey, string infoValue | infoValue = c.getInfo(infoKey) |
1415
key = infoKey and
1516
value = infoValue.toFloat()

0 commit comments

Comments
 (0)