Skip to content

Commit ef21ee5

Browse files
authored
Merge pull request #17169 from tamasvajk/buildless/db-quality-query
C#: Add diagnostic query indicating low database quality
2 parents d9ff4ef + 0263cc1 commit ef21ee5

File tree

13 files changed

+136
-70
lines changed

13 files changed

+136
-70
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/NugetPackageRestorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ private bool CheckFeeds(out HashSet<string> explicitFeeds)
667667
"Found unreachable Nuget feed in C# analysis with build-mode 'none'",
668668
visibility: new DiagnosticMessage.TspVisibility(statusPage: true, cliSummaryTable: true, telemetry: true),
669669
markdownMessage: "Found unreachable Nuget feed in C# analysis with build-mode 'none'. This may cause missing dependencies in the analysis.",
670-
severity: DiagnosticMessage.TspSeverity.Warning
670+
severity: DiagnosticMessage.TspSeverity.Note
671671
));
672672
}
673673
compilationInfoContainer.CompilationInfos.Add(("All Nuget feeds reachable", allFeedsReachable ? "1" : "0"));
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
diagnosticAttributes
2+
| Scanning C# code completed successfully, but the scan encountered issues. This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes). | visibilityCliSummaryTable | true |
3+
| Scanning C# code completed successfully, but the scan encountered issues. This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes). | visibilityStatusPage | true |
4+
| Scanning C# code completed successfully, but the scan encountered issues. This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes). | visibilityTelemetry | true |
5+
#select
6+
| Scanning C# code completed successfully, but the scan encountered issues. This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes). | Scanning C# code completed successfully, but the scan encountered issues. This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes). | 1 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Telemetry/DatabaseQualityDiagnostics.ql
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
diagnosticAttributes
2+
#select
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Telemetry/DatabaseQualityDiagnostics.ql

csharp/ql/integration-tests/posix-only/standalone_dependencies_nuget_config_error_timeout/diagnostics.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
{
3030
"markdownMessage": "Found unreachable Nuget feed in C# analysis with build-mode 'none'. This may cause missing dependencies in the analysis.",
31-
"severity": "warning",
31+
"severity": "note",
3232
"source": {
3333
"extractorName": "csharp",
3434
"id": "csharp/autobuilder/buildless/unreachable-feed",

csharp/ql/integration-tests/posix-only/standalone_dependencies_nuget_config_fallback/diagnostics.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
{
3030
"markdownMessage": "Found unreachable Nuget feed in C# analysis with build-mode 'none'. This may cause missing dependencies in the analysis.",
31-
"severity": "warning",
31+
"severity": "note",
3232
"source": {
3333
"extractorName": "csharp",
3434
"id": "csharp/autobuilder/buildless/unreachable-feed",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
dependencies:
22
codeql/csharp-all: '*'
3+
codeql/csharp-queries: '*'
34
warnOnImplicitThis: true
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Provides database quality statistics that are reported by csharp/telemetry/extractor-information
3+
* and perhaps warned about by csharp/diagnostics/database-quality.
4+
*/
5+
6+
import csharp
7+
8+
signature module StatsSig {
9+
int getNumberOfOk();
10+
11+
int getNumberOfNotOk();
12+
13+
string getOkText();
14+
15+
string getNotOkText();
16+
}
17+
18+
module ReportStats<StatsSig Stats> {
19+
predicate numberOfOk(string key, int value) {
20+
value = Stats::getNumberOfOk() and
21+
key = "Number of " + Stats::getOkText()
22+
}
23+
24+
predicate numberOfNotOk(string key, int value) {
25+
value = Stats::getNumberOfNotOk() and
26+
key = "Number of " + Stats::getNotOkText()
27+
}
28+
29+
predicate percentageOfOk(string key, float value) {
30+
value = Stats::getNumberOfOk() * 100.0 / (Stats::getNumberOfOk() + Stats::getNumberOfNotOk()) and
31+
key = "Percentage of " + Stats::getOkText()
32+
}
33+
}
34+
35+
module CallTargetStats implements StatsSig {
36+
int getNumberOfOk() { result = count(Call c | exists(c.getTarget())) }
37+
38+
int getNumberOfNotOk() {
39+
result =
40+
count(Call c |
41+
not exists(c.getTarget()) and
42+
not c instanceof DelegateCall and
43+
not c instanceof DynamicExpr
44+
)
45+
}
46+
47+
string getOkText() { result = "calls with call target" }
48+
49+
string getNotOkText() { result = "calls with missing call target" }
50+
}
51+
52+
private class SourceExpr extends Expr {
53+
SourceExpr() { this.getFile().fromSource() }
54+
}
55+
56+
private predicate hasGoodType(Expr e) {
57+
exists(e.getType()) and not e.getType() instanceof UnknownType
58+
}
59+
60+
module ExprTypeStats implements StatsSig {
61+
int getNumberOfOk() { result = count(SourceExpr e | hasGoodType(e)) }
62+
63+
int getNumberOfNotOk() { result = count(SourceExpr e | not hasGoodType(e)) }
64+
65+
string getOkText() { result = "expressions with known type" }
66+
67+
string getNotOkText() { result = "expressions with unknown type" }
68+
}
69+
70+
module CallTargetStatsReport = ReportStats<CallTargetStats>;
71+
72+
module ExprTypeStatsReport = ReportStats<ExprTypeStats>;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @name Low C# analysis quality
3+
* @description Low C# analysis quality
4+
* @kind diagnostic
5+
* @id csharp/diagnostic/database-quality
6+
*/
7+
8+
import csharp
9+
import DatabaseQuality
10+
11+
private newtype TDbQualityDiagnostic =
12+
TTheDbQualityDiagnostic() {
13+
exists(float percentageGood |
14+
CallTargetStatsReport::percentageOfOk(_, percentageGood)
15+
or
16+
ExprTypeStatsReport::percentageOfOk(_, percentageGood)
17+
|
18+
percentageGood < 95
19+
)
20+
}
21+
22+
class DbQualityDiagnostic extends TDbQualityDiagnostic {
23+
string toString() {
24+
result =
25+
"Scanning C# code completed successfully, but the scan encountered issues. " +
26+
"This may be caused by problems identifying dependencies or use of generated source code, among other reasons -- "
27+
+
28+
"see other CodeQL diagnostics reported on the CodeQL status page for more details of possible causes. "
29+
+
30+
"Addressing these warnings is advisable to avoid false-positive or missing results. If they cannot be addressed, consider scanning C# "
31+
+
32+
"using either the `autobuild` or `manual` [build modes](https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages#comparison-of-the-build-modes)."
33+
}
34+
}
35+
36+
query predicate diagnosticAttributes(DbQualityDiagnostic e, string key, string value) {
37+
exists(e) and // Quieten warning about unconstrained 'e'
38+
key = ["visibilityCliSummaryTable", "visibilityTelemetry", "visibilityStatusPage"] and
39+
value = "true"
40+
}
41+
42+
from DbQualityDiagnostic d
43+
select d, d.toString(), 1
44+
/* Warning severity */

0 commit comments

Comments
 (0)