Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 75c19e3

Browse files
committed
C#: Add internal queries for extractor and compiler diagnostics.
1 parent 8cd3cb5 commit 75c19e3

13 files changed

Lines changed: 88 additions & 0 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Compilation error
3+
* @description A compilation error can lead to extraction problems, and may indicate
4+
* a bug in the extractor.
5+
* @kind problem
6+
* @problem.severity recommendation
7+
* @precision medium
8+
* @id cs/compilation-error
9+
* @tags internal
10+
*/
11+
12+
import csharp
13+
import semmle.code.csharp.commons.Diagnostics
14+
15+
from CompilerError diagnostic
16+
select diagnostic,
17+
diagnostic.getSeverityText() + " " + diagnostic.getTag() + " " + diagnostic.getFullMessage()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Compilation message
3+
* @description A message emitted by the compiler.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @precision medium
7+
* @id cs/compilation-warning
8+
* @tags internal
9+
*/
10+
11+
import csharp
12+
import semmle.code.csharp.commons.Diagnostics
13+
14+
from Diagnostic diagnostic
15+
select diagnostic,
16+
diagnostic.getSeverityText() + " " + diagnostic.getTag() + " " + diagnostic.getFullMessage()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @name Extraction error
3+
* @description An error message reported by the extractor, limited to those files where there are no
4+
* compilation errors. This indicates a bug or limitation in the extractor, and could lead
5+
* to inaccurate results.
6+
* @kind problem
7+
* @problem.severity recommendation
8+
* @precision medium
9+
* @id cs/extraction-error
10+
* @tags internal
11+
*/
12+
13+
import csharp
14+
import semmle.code.csharp.commons.Diagnostics
15+
16+
from ExtractorError error
17+
where not exists(CompilerError ce | ce.getLocation().getFile() = error.getLocation().getFile())
18+
select error,
19+
"Unexpected " + error.getOrigin() + " error in element '" + error.getElementText() +
20+
"' at location " + error.getStackTrace()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Extraction message
3+
* @description An error message reported by the extractor. This could lead to inaccurate results.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @precision medium
7+
* @id cs/extraction-message
8+
* @tags internal
9+
*/
10+
11+
import csharp
12+
import semmle.code.csharp.commons.Diagnostics
13+
14+
from ExtractorMessage message
15+
select message,
16+
message.getSeverityText() + " was generated by " + message.getOrigin() + " in element '" +
17+
message.getElementText() + "' at location " + message.getStackTrace()

csharp/ql/src/semmle/code/csharp/commons/Diagnostics.qll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ class Diagnostic extends @diagnostic {
2929
*/
3030
int getSeverity() { result = severity }
3131

32+
/** Gets a string representation of the severity of this diagnostic. */
33+
string getSeverityText() {
34+
severity = 0 and result = "Hidden"
35+
or
36+
severity = 1 and result = "Info"
37+
or
38+
severity = 2 and result = "Warning"
39+
or
40+
severity = 3 and result = "Error"
41+
}
42+
3243
/** Gets the identifier of this diagnostic, for example "CS8019". */
3344
string getTag() { result = tag }
3445

csharp/ql/test/library-tests/compilations/CompilerError.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/CompilerError.ql
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| Program.cs:7:13:7:13 | CS0219: The variable 'x' is assigned but its value is never used | Warning CS0219 The variable 'x' is assigned but its value is never used |
2+
| Program.cs:9:9:9:11 | CS0162: Unreachable code detected | Warning CS0162 Unreachable code detected |
3+
| Program.cs:9:13:9:13 | CS0219: The variable 'y' is assigned but its value is never used | Warning CS0219 The variable 'y' is assigned but its value is never used |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/CompilerMessage.ql

csharp/ql/test/library-tests/compilations/ExtractorError.expected

Whitespace-only changes.

0 commit comments

Comments
 (0)