Skip to content

Commit 4e02e34

Browse files
authored
Merge pull request #17214 from tamasvajk/fix/message-limit
C#: Minor adjustment to limiting the number of extractor messages
2 parents a782952 + 5f74ead commit 4e02e34

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

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

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public CompilerDiagnostic(Context cx, Microsoft.CodeAnalysis.Diagnostic diag, Co
2121

2222
protected override void Populate(TextWriter trapFile)
2323
{
24-
// The below doesn't limit the extractor messages to the exact limit, but it's good enough.
2524
var key = diagnostic.Id;
2625
var messageCount = compilation.messageCounts.AddOrUpdate(key, 1, (_, c) => c + 1);
2726
if (messageCount > limit)

csharp/extractor/Semmle.Extraction/Entities/ExtractionMessage.cs

+17-8
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,36 @@ internal class ExtractionMessage : FreshEntity
1010
private static int messageCount = 0;
1111

1212
private readonly Message msg;
13+
private readonly bool bypassLimit;
1314

14-
public ExtractionMessage(Context cx, Message msg) : base(cx)
15+
public ExtractionMessage(Context cx, Message msg) : this(cx, msg, bypassLimit: false)
1516
{
17+
}
18+
19+
private ExtractionMessage(Context cx, Message msg, bool bypassLimit) : base(cx)
20+
{
21+
this.bypassLimit = bypassLimit;
1622
this.msg = msg;
1723
TryPopulate();
1824
}
1925

2026
protected override void Populate(TextWriter trapFile)
2127
{
22-
// The below doesn't limit the extractor messages to the exact limit, but it's good enough.
23-
Interlocked.Increment(ref messageCount);
24-
if (messageCount > limit)
28+
if (!bypassLimit)
2529
{
26-
if (messageCount == limit + 1)
30+
var val = Interlocked.Increment(ref messageCount);
31+
if (val > limit)
2732
{
28-
Context.ExtractionContext.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
33+
if (val == limit + 1)
34+
{
35+
Context.ExtractionContext.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
36+
_ = new ExtractionMessage(Context, new Message($"Stopped logging extractor messages after reaching {limit}", null, null, null, Util.Logging.Severity.Warning), bypassLimit: true);
37+
}
38+
return;
2939
}
30-
return;
3140
}
3241

33-
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText ?? string.Empty,
42+
trapFile.extractor_messages(this, msg.Severity, msg.Text, msg.EntityText ?? string.Empty,
3443
msg.Location ?? Context.CreateLocation(), msg.StackTrace ?? string.Empty);
3544
}
3645
}

csharp/extractor/Semmle.Extraction/Tuples.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public static void containerparent(this System.IO.TextWriter trapFile, Folder pa
1212
trapFile.WriteTuple("containerparent", parent, child);
1313
}
1414

15-
internal static void extractor_messages(this System.IO.TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string origin, string errorMessage, string entityText, Location location, string stackTrace)
15+
internal static void extractor_messages(this System.IO.TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string errorMessage, string entityText, Location location, string stackTrace)
1616
{
17-
trapFile.WriteTuple("extractor_messages", error, (int)severity, origin, errorMessage, entityText, location, stackTrace);
17+
trapFile.WriteTuple("extractor_messages", error, (int)severity, "C# extractor", errorMessage, entityText, location, stackTrace);
1818
}
1919

2020
public static void files(this System.IO.TextWriter trapFile, File file, string fullName)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
extractorMessages
2-
| 5 |
2+
| 6 |
33
compilerDiagnostics
44
| 4 |
5+
extractorMessagesLeachedLimit
6+
| Program.cs:1:1:1:0 | Stopped logging extractor messages after reaching 5 |
57
compilationInfo
68
| Compiler diagnostic count for CS0103 | 3.0 |
79
| Compiler diagnostic count for CS8019 | 7.0 |

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

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ query predicate extractorMessages(int c) { c = count(ExtractorMessage msg) }
55

66
query predicate compilerDiagnostics(int c) { c = count(Diagnostic diag) }
77

8+
query predicate extractorMessagesLeachedLimit(ExtractorMessage msg) {
9+
msg.getText().indexOf("Stopped logging") = 0
10+
}
11+
812
query predicate compilationInfo(string key, float value) {
913
exists(Compilation c, string infoValue |
1014
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%")

0 commit comments

Comments
 (0)