-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15957 from tamasvajk/feature/limit-message-extrac…
…tion C#: Limit extracted compilation and extraction messages
- Loading branch information
Showing
13 changed files
with
130 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/CompilerDiagnostic.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.IO; | ||
using Semmle.Util; | ||
|
||
namespace Semmle.Extraction.CSharp.Entities | ||
{ | ||
internal class CompilerDiagnostic : FreshEntity | ||
{ | ||
private static readonly int limit = EnvironmentVariables.TryGetExtractorNumberOption<int>("COMPILER_DIAGNOSTIC_LIMIT") ?? 1000; | ||
|
||
private readonly Microsoft.CodeAnalysis.Diagnostic diagnostic; | ||
private readonly Compilation compilation; | ||
private readonly int index; | ||
|
||
public CompilerDiagnostic(Context cx, Microsoft.CodeAnalysis.Diagnostic diag, Compilation compilation, int index) : base(cx) | ||
{ | ||
diagnostic = diag; | ||
this.compilation = compilation; | ||
this.index = index; | ||
TryPopulate(); | ||
} | ||
|
||
protected override void Populate(TextWriter trapFile) | ||
{ | ||
// The below doesn't limit the extractor messages to the exact limit, but it's good enough. | ||
var key = diagnostic.Id; | ||
var messageCount = compilation.messageCounts.AddOrUpdate(key, 1, (_, c) => c + 1); | ||
if (messageCount > limit) | ||
{ | ||
if (messageCount == limit + 1) | ||
{ | ||
Context.Extractor.Logger.LogWarning($"Stopped logging {key} compiler diagnostics for the current compilation after reaching {limit}"); | ||
} | ||
|
||
return; | ||
} | ||
|
||
trapFile.diagnostics(this, (int)diagnostic.Severity, key, diagnostic.Descriptor.Title.ToString(), | ||
diagnostic.GetMessage(), Context.CreateLocation(diagnostic.Location)); | ||
|
||
trapFile.diagnostic_for(this, compilation, 0, index); | ||
} | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
csharp/extractor/Semmle.Extraction.CSharp/Entities/Compilations/Diagnostic.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 0 additions & 21 deletions
21
csharp/extractor/Semmle.Extraction/Entities/ExtractionError.cs
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
csharp/extractor/Semmle.Extraction/Entities/ExtractionMessage.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.IO; | ||
using System.Threading; | ||
using Semmle.Util; | ||
|
||
namespace Semmle.Extraction.Entities | ||
{ | ||
internal class ExtractionMessage : FreshEntity | ||
{ | ||
private static readonly int limit = EnvironmentVariables.TryGetExtractorNumberOption<int>("MESSAGE_LIMIT") ?? 10000; | ||
private static int messageCount = 0; | ||
|
||
private readonly Message msg; | ||
|
||
public ExtractionMessage(Context cx, Message msg) : base(cx) | ||
{ | ||
this.msg = msg; | ||
TryPopulate(); | ||
} | ||
|
||
protected override void Populate(TextWriter trapFile) | ||
{ | ||
// The below doesn't limit the extractor messages to the exact limit, but it's good enough. | ||
Interlocked.Increment(ref messageCount); | ||
if (messageCount > limit) | ||
{ | ||
if (messageCount == limit + 1) | ||
{ | ||
Context.Extractor.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}"); | ||
} | ||
return; | ||
} | ||
|
||
trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText ?? string.Empty, | ||
msg.Location ?? Context.CreateLocation(), msg.StackTrace ?? string.Empty); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
csharp/ql/integration-tests/all-platforms/standalone/Diag.expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
extractorMessages | ||
| 5 | | ||
compilerDiagnostics | ||
| 4 | | ||
compilationInfo | ||
| Compiler diagnostic count for CS0103 | 3.0 | | ||
| Compiler diagnostic count for CS8019 | 7.0 | |
14 changes: 14 additions & 0 deletions
14
csharp/ql/integration-tests/all-platforms/standalone/Diag.ql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import csharp | ||
import semmle.code.csharp.commons.Diagnostics | ||
|
||
query predicate extractorMessages(int c) { c = count(ExtractorMessage msg) } | ||
|
||
query predicate compilerDiagnostics(int c) { c = count(Diagnostic diag) } | ||
|
||
query predicate compilationInfo(string key, float value) { | ||
exists(Compilation c, string infoValue | | ||
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%") | ||
| | ||
value = infoValue.toFloat() | ||
) | ||
} |
3 changes: 2 additions & 1 deletion
3
csharp/ql/integration-tests/all-platforms/standalone/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
var dummy = "dummy"; | ||
var dummy = "dummy"; | ||
dummy = M() + M() + M(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
import os | ||
from create_database_utils import * | ||
|
||
os.environ['CODEQL_EXTRACTOR_CSHARP_OPTION_COMPILER_DIAGNOSTIC_LIMIT'] = '2' | ||
os.environ['CODEQL_EXTRACTOR_CSHARP_OPTION_MESSAGE_LIMIT'] = '5' | ||
run_codeql_database_create([], lang="csharp", extra_args=["--build-mode=none"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters