Skip to content

Commit 310baab

Browse files
authored
Merge pull request #5740 from tamasvajk/feature/diag
C#: Add extraction error diagnostic query
2 parents 2b9fb79 + 51e08d4 commit 310baab

File tree

11 files changed

+133
-6
lines changed

11 files changed

+133
-6
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Program model errors are now extracted in standalone extraction.

csharp/extractor/Semmle.Extraction/Context.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,27 @@ private void ExtractionError(Message msg)
376376
Extractor.Message(msg);
377377
}
378378

379+
private void ExtractionError(InternalError error)
380+
{
381+
ExtractionError(new Message(error.Message, error.EntityText, CreateLocation(error.Location), error.StackTrace, Severity.Error));
382+
}
383+
384+
private void ReportError(InternalError error)
385+
{
386+
if (!Extractor.Standalone)
387+
throw error;
388+
389+
ExtractionError(error);
390+
}
391+
379392
/// <summary>
380393
/// Signal an error in the program model.
381394
/// </summary>
382395
/// <param name="node">The syntax node causing the failure.</param>
383396
/// <param name="msg">The error message.</param>
384397
public void ModelError(SyntaxNode node, string msg)
385398
{
386-
if (!Extractor.Standalone)
387-
throw new InternalError(node, msg);
399+
ReportError(new InternalError(node, msg));
388400
}
389401

390402
/// <summary>
@@ -394,8 +406,7 @@ public void ModelError(SyntaxNode node, string msg)
394406
/// <param name="msg">The error message.</param>
395407
public void ModelError(ISymbol symbol, string msg)
396408
{
397-
if (!Extractor.Standalone)
398-
throw new InternalError(symbol, msg);
409+
ReportError(new InternalError(symbol, msg));
399410
}
400411

401412
/// <summary>
@@ -404,8 +415,7 @@ public void ModelError(ISymbol symbol, string msg)
404415
/// <param name="msg">The error message.</param>
405416
public void ModelError(string msg)
406417
{
407-
if (!Extractor.Standalone)
408-
throw new InternalError(msg);
418+
ReportError(new InternalError(msg));
409419
}
410420

411421
/// <summary>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* @name Extraction errors
3+
* @description List all errors reported by the extractor or the compiler. Extractor errors are
4+
* limited to those files where there are no compilation errors.
5+
* @kind diagnostic
6+
* @id cs/diagnostics/extraction-errors
7+
*/
8+
9+
import csharp
10+
import semmle.code.csharp.commons.Diagnostics
11+
12+
private newtype TDiagnosticError =
13+
TCompilerError(CompilerError c) or
14+
TExtractorError(ExtractorError e)
15+
16+
abstract private class DiagnosticError extends TDiagnosticError {
17+
abstract string getMessage();
18+
19+
abstract string toString();
20+
21+
abstract Location getLocation();
22+
23+
string getLocationMessage() {
24+
if getLocation().getFile().fromSource()
25+
then result = " in " + getLocation().getFile()
26+
else result = ""
27+
}
28+
}
29+
30+
private class DiagnosticCompilerError extends DiagnosticError {
31+
CompilerError c;
32+
33+
DiagnosticCompilerError() { this = TCompilerError(c) }
34+
35+
override string getMessage() {
36+
result = "Compiler error" + this.getLocationMessage() + ": " + c.getMessage()
37+
}
38+
39+
override string toString() { result = c.toString() }
40+
41+
override Location getLocation() { result = c.getLocation() }
42+
}
43+
44+
private class DiagnosticExtractorError extends DiagnosticError {
45+
ExtractorError e;
46+
47+
DiagnosticExtractorError() {
48+
this = TExtractorError(e) and
49+
not exists(CompilerError ce | ce.getLocation().getFile() = e.getLocation().getFile())
50+
}
51+
52+
override string getMessage() {
53+
result =
54+
"Unexpected " + e.getOrigin() + " error" + this.getLocationMessage() + ": " + e.getText()
55+
}
56+
57+
override string toString() { result = e.toString() }
58+
59+
override Location getLocation() { result = e.getLocation() }
60+
}
61+
62+
from DiagnosticError error
63+
select error.getMessage(), 2
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @name Successfully extracted files
3+
* @description A list of all files in the source code directory that were extracted
4+
* without encountering an extraction or compiler error in the file.
5+
* @kind diagnostic
6+
* @id cs/diagnostics/successfully-extracted-files
7+
*/
8+
9+
import csharp
10+
import semmle.code.csharp.commons.Diagnostics
11+
12+
from File file
13+
where
14+
file.fromSource() and
15+
not exists(ExtractorError e | e.getLocation().getFile() = file) and
16+
not exists(CompilerError e | e.getLocation().getFile() = file)
17+
select file, ""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class A
2+
{
3+
public void M() { }
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| Unexpected C# extractor error in Program.cs: Unable to resolve target for call. (Compilation error?) | 2 |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/DiagnosticExtractionErrors.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| A.cs:0:0:0:0 | A.cs | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Diagnostics/DiagnosticNoExtractionErrors.ql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// semmle-extractor-options: --standalone
2+
3+
using System;
4+
5+
class Class
6+
{
7+
static void Main(string[] args)
8+
{
9+
int z = GetParamLength(__arglist(1, 2));
10+
}
11+
12+
public static int GetParamLength(__arglist)
13+
{
14+
ArgIterator iterator = new ArgIterator(__arglist);
15+
return iterator.GetRemainingCount();
16+
}
17+
}

0 commit comments

Comments
 (0)