mirror of
https://github.com/github/codeql.git
synced 2026-04-28 10:15:14 +02:00
Merge pull request #5740 from tamasvajk/feature/diag
C#: Add extraction error diagnostic query
This commit is contained in:
2
csharp/change-notes/2021-04-23-model-error-extraction.md
Normal file
2
csharp/change-notes/2021-04-23-model-error-extraction.md
Normal file
@@ -0,0 +1,2 @@
|
||||
lgtm,codescanning
|
||||
* Program model errors are now extracted in standalone extraction.
|
||||
@@ -376,6 +376,19 @@ namespace Semmle.Extraction
|
||||
Extractor.Message(msg);
|
||||
}
|
||||
|
||||
private void ExtractionError(InternalError error)
|
||||
{
|
||||
ExtractionError(new Message(error.Message, error.EntityText, CreateLocation(error.Location), error.StackTrace, Severity.Error));
|
||||
}
|
||||
|
||||
private void ReportError(InternalError error)
|
||||
{
|
||||
if (!Extractor.Standalone)
|
||||
throw error;
|
||||
|
||||
ExtractionError(error);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Signal an error in the program model.
|
||||
/// </summary>
|
||||
@@ -383,8 +396,7 @@ namespace Semmle.Extraction
|
||||
/// <param name="msg">The error message.</param>
|
||||
public void ModelError(SyntaxNode node, string msg)
|
||||
{
|
||||
if (!Extractor.Standalone)
|
||||
throw new InternalError(node, msg);
|
||||
ReportError(new InternalError(node, msg));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -394,8 +406,7 @@ namespace Semmle.Extraction
|
||||
/// <param name="msg">The error message.</param>
|
||||
public void ModelError(ISymbol symbol, string msg)
|
||||
{
|
||||
if (!Extractor.Standalone)
|
||||
throw new InternalError(symbol, msg);
|
||||
ReportError(new InternalError(symbol, msg));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -404,8 +415,7 @@ namespace Semmle.Extraction
|
||||
/// <param name="msg">The error message.</param>
|
||||
public void ModelError(string msg)
|
||||
{
|
||||
if (!Extractor.Standalone)
|
||||
throw new InternalError(msg);
|
||||
ReportError(new InternalError(msg));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
63
csharp/ql/src/Diagnostics/DiagnosticExtractionErrors.ql
Normal file
63
csharp/ql/src/Diagnostics/DiagnosticExtractionErrors.ql
Normal file
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* @name Extraction errors
|
||||
* @description List all errors reported by the extractor or the compiler. Extractor errors are
|
||||
* limited to those files where there are no compilation errors.
|
||||
* @kind diagnostic
|
||||
* @id cs/diagnostics/extraction-errors
|
||||
*/
|
||||
|
||||
import csharp
|
||||
import semmle.code.csharp.commons.Diagnostics
|
||||
|
||||
private newtype TDiagnosticError =
|
||||
TCompilerError(CompilerError c) or
|
||||
TExtractorError(ExtractorError e)
|
||||
|
||||
abstract private class DiagnosticError extends TDiagnosticError {
|
||||
abstract string getMessage();
|
||||
|
||||
abstract string toString();
|
||||
|
||||
abstract Location getLocation();
|
||||
|
||||
string getLocationMessage() {
|
||||
if getLocation().getFile().fromSource()
|
||||
then result = " in " + getLocation().getFile()
|
||||
else result = ""
|
||||
}
|
||||
}
|
||||
|
||||
private class DiagnosticCompilerError extends DiagnosticError {
|
||||
CompilerError c;
|
||||
|
||||
DiagnosticCompilerError() { this = TCompilerError(c) }
|
||||
|
||||
override string getMessage() {
|
||||
result = "Compiler error" + this.getLocationMessage() + ": " + c.getMessage()
|
||||
}
|
||||
|
||||
override string toString() { result = c.toString() }
|
||||
|
||||
override Location getLocation() { result = c.getLocation() }
|
||||
}
|
||||
|
||||
private class DiagnosticExtractorError extends DiagnosticError {
|
||||
ExtractorError e;
|
||||
|
||||
DiagnosticExtractorError() {
|
||||
this = TExtractorError(e) and
|
||||
not exists(CompilerError ce | ce.getLocation().getFile() = e.getLocation().getFile())
|
||||
}
|
||||
|
||||
override string getMessage() {
|
||||
result =
|
||||
"Unexpected " + e.getOrigin() + " error" + this.getLocationMessage() + ": " + e.getText()
|
||||
}
|
||||
|
||||
override string toString() { result = e.toString() }
|
||||
|
||||
override Location getLocation() { result = e.getLocation() }
|
||||
}
|
||||
|
||||
from DiagnosticError error
|
||||
select error.getMessage(), 2
|
||||
17
csharp/ql/src/Diagnostics/DiagnosticNoExtractionErrors.ql
Normal file
17
csharp/ql/src/Diagnostics/DiagnosticNoExtractionErrors.ql
Normal file
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* @name Successfully extracted files
|
||||
* @description A list of all files in the source code directory that were extracted
|
||||
* without encountering an extraction or compiler error in the file.
|
||||
* @kind diagnostic
|
||||
* @id cs/diagnostics/successfully-extracted-files
|
||||
*/
|
||||
|
||||
import csharp
|
||||
import semmle.code.csharp.commons.Diagnostics
|
||||
|
||||
from File file
|
||||
where
|
||||
file.fromSource() and
|
||||
not exists(ExtractorError e | e.getLocation().getFile() = file) and
|
||||
not exists(CompilerError e | e.getLocation().getFile() = file)
|
||||
select file, ""
|
||||
4
csharp/ql/test/library-tests/diagnostics/A.cs
Normal file
4
csharp/ql/test/library-tests/diagnostics/A.cs
Normal file
@@ -0,0 +1,4 @@
|
||||
public class A
|
||||
{
|
||||
public void M() { }
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
| Unexpected C# extractor error in Program.cs: Unable to resolve target for call. (Compilation error?) | 2 |
|
||||
@@ -0,0 +1 @@
|
||||
Diagnostics/DiagnosticExtractionErrors.ql
|
||||
@@ -0,0 +1 @@
|
||||
| A.cs:0:0:0:0 | A.cs | |
|
||||
@@ -0,0 +1 @@
|
||||
Diagnostics/DiagnosticNoExtractionErrors.ql
|
||||
17
csharp/ql/test/library-tests/diagnostics/Program.cs
Normal file
17
csharp/ql/test/library-tests/diagnostics/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
// semmle-extractor-options: --standalone
|
||||
|
||||
using System;
|
||||
|
||||
class Class
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
int z = GetParamLength(__arglist(1, 2));
|
||||
}
|
||||
|
||||
public static int GetParamLength(__arglist)
|
||||
{
|
||||
ArgIterator iterator = new ArgIterator(__arglist);
|
||||
return iterator.GetRemainingCount();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,13 @@
|
||||
compilationMessages
|
||||
extractorMessages
|
||||
| errors.cs:8:1:8:22 | Namespace not found |
|
||||
| errors.cs:24:31:24:32 | Failed to determine type |
|
||||
| errors.cs:24:31:24:40 | Failed to determine type |
|
||||
| errors.cs:24:31:24:40 | Unable to resolve target for call. (Compilation error?) |
|
||||
| errors.cs:24:38:24:39 | Failed to determine type |
|
||||
| errors.cs:57:20:57:20 | Failed to determine type |
|
||||
| errors.cs:93:45:93:45 | Failed to determine type |
|
||||
| errors.cs:93:45:93:45 | Failed to resolve name |
|
||||
| errors.cs:94:45:94:45 | Failed to determine type |
|
||||
| errors.cs:94:45:94:45 | Failed to resolve name |
|
||||
| file://:0:0:0:0 | Extracting default argument value 'object RecordNumber = default' instead of 'object RecordNumber = -1'. The latter is not supported in C#. |
|
||||
|
||||
Reference in New Issue
Block a user