mirror of
https://github.com/github/codeql.git
synced 2025-12-20 10:46:30 +01:00
Extract warning and error directives
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class ErrorDirective : PreprocessorDirective<ErrorDirectiveTriviaSyntax>
|
||||
{
|
||||
public ErrorDirective(Context cx, ErrorDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
trapFile.directive_errors(this, trivia.EndOfDirectiveToken.LeadingTrivia.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class WarningDirective : PreprocessorDirective<WarningDirectiveTriviaSyntax>
|
||||
{
|
||||
public WarningDirective(Context cx, WarningDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
trapFile.directive_warnings(this, trivia.EndOfDirectiveToken.LeadingTrivia.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,5 +32,15 @@ namespace Semmle.Extraction.CSharp.Populators
|
||||
{
|
||||
new Entities.UndefineDirective(cx, node);
|
||||
}
|
||||
|
||||
public override void VisitWarningDirectiveTrivia(WarningDirectiveTriviaSyntax node)
|
||||
{
|
||||
new Entities.WarningDirective(cx, node);
|
||||
}
|
||||
|
||||
public override void VisitErrorDirectiveTrivia(ErrorDirectiveTriviaSyntax node)
|
||||
{
|
||||
new Entities.ErrorDirective(cx, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -630,5 +630,15 @@ namespace Semmle.Extraction.CSharp
|
||||
{
|
||||
trapFile.WriteTuple("directive_undefines", directive, name);
|
||||
}
|
||||
|
||||
internal static void directive_warnings(this TextWriter trapFile, WarningDirective directive, string message)
|
||||
{
|
||||
trapFile.WriteTuple("directive_warnings", directive, message);
|
||||
}
|
||||
|
||||
internal static void directive_errors(this TextWriter trapFile, ErrorDirective directive, string message)
|
||||
{
|
||||
trapFile.WriteTuple("directive_errors", directive, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +70,27 @@ class UndefineDirective extends PreprocessorDirective, @directive_undefine {
|
||||
|
||||
override string getAPrimaryQlClass() { result = "UndefineDirective" }
|
||||
}
|
||||
|
||||
/**
|
||||
* A `#warning` directive.
|
||||
*/
|
||||
class WarningDirective extends PreprocessorDirective, @directive_warning {
|
||||
/** Gets the text of the warning. */
|
||||
string getMessage() { directive_warnings(this, result) }
|
||||
|
||||
override string toString() { result = "#warning ..." }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "WarningDirective" }
|
||||
}
|
||||
|
||||
/**
|
||||
* An `#error` directive.
|
||||
*/
|
||||
class ErrorDirective extends PreprocessorDirective, @directive_error {
|
||||
/** Gets the text of the error. */
|
||||
string getMessage() { directive_errors(this, result) }
|
||||
|
||||
override string toString() { result = "#error ..." }
|
||||
|
||||
override string getAPrimaryQlClass() { result = "ErrorDirective" }
|
||||
}
|
||||
|
||||
@@ -331,7 +331,16 @@ using_directive_location(
|
||||
unique int id: @using_directive ref,
|
||||
int loc: @location ref);
|
||||
|
||||
@preprocessor_directive = @pragma_warning | @pragma_checksum | @directive_define | @directive_undefine;
|
||||
@preprocessor_directive = @pragma_warning | @pragma_checksum | @directive_define | @directive_undefine | @directive_warning
|
||||
| @directive_error;
|
||||
|
||||
directive_warnings(
|
||||
unique int id: @directive_warning,
|
||||
string message: string ref);
|
||||
|
||||
directive_errors(
|
||||
unique int id: @directive_error,
|
||||
string message: string ref);
|
||||
|
||||
directive_undefines(
|
||||
unique int id: @directive_undefine,
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
| trivia.cs:72:1:72:41 | #error ... | NOTDEBUG is defined or TEST is not |
|
||||
4
csharp/ql/test/library-tests/comments/ErrorDirectives.ql
Normal file
4
csharp/ql/test/library-tests/comments/ErrorDirectives.ql
Normal file
@@ -0,0 +1,4 @@
|
||||
import csharp
|
||||
|
||||
from ErrorDirective d
|
||||
select d, d.getMessage()
|
||||
@@ -0,0 +1 @@
|
||||
| trivia.cs:66:1:66:25 | #warning ... | DEBUG is defined |
|
||||
@@ -0,0 +1,4 @@
|
||||
import csharp
|
||||
|
||||
from WarningDirective d
|
||||
select d, d.getMessage()
|
||||
Reference in New Issue
Block a user