mirror of
https://github.com/github/codeql.git
synced 2026-05-05 13:45:19 +02:00
Merge pull request #7577 from michaelnebel/csharp/line-pragma
C#: Make support for Line span pragma
This commit is contained in:
@@ -5,36 +5,28 @@ using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class LineDirective : PreprocessorDirective<LineDirectiveTriviaSyntax>
|
||||
internal class LineDirective : LineOrSpanDirective<LineDirectiveTriviaSyntax>
|
||||
{
|
||||
private LineDirective(Context cx, LineDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia)
|
||||
: base(cx, trivia, trivia.Line.Kind() switch
|
||||
{
|
||||
SyntaxKind.DefaultKeyword => LineDirectiveKind.Default,
|
||||
SyntaxKind.HiddenKeyword => LineDirectiveKind.Hidden,
|
||||
SyntaxKind.NumericLiteralToken => LineDirectiveKind.Numeric,
|
||||
_ => throw new InternalError(trivia, "Unhandled line token kind")
|
||||
})
|
||||
{
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
var type = Symbol.Line.Kind() switch
|
||||
{
|
||||
SyntaxKind.DefaultKeyword => 0,
|
||||
SyntaxKind.HiddenKeyword => 1,
|
||||
SyntaxKind.NumericLiteralToken => 2,
|
||||
_ => throw new InternalError(Symbol, "Unhandled line token kind")
|
||||
};
|
||||
|
||||
trapFile.directive_lines(this, type);
|
||||
|
||||
if (Symbol.Line.IsKind(SyntaxKind.NumericLiteralToken))
|
||||
{
|
||||
var value = (int)Symbol.Line.Value!;
|
||||
trapFile.directive_line_value(this, value);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
|
||||
{
|
||||
var file = File.Create(Context, Symbol.File.ValueText);
|
||||
trapFile.directive_line_file(this, file);
|
||||
}
|
||||
}
|
||||
|
||||
base.PopulatePreprocessor(trapFile);
|
||||
}
|
||||
|
||||
public static LineDirective Create(Context cx, LineDirectiveTriviaSyntax line) =>
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal enum LineDirectiveKind
|
||||
{
|
||||
Default = 0,
|
||||
Hidden = 1,
|
||||
Numeric = 2,
|
||||
Span = 3
|
||||
}
|
||||
|
||||
internal abstract class LineOrSpanDirective<T> : PreprocessorDirective<T> where T : LineOrSpanDirectiveTriviaSyntax
|
||||
{
|
||||
private readonly LineDirectiveKind kind;
|
||||
|
||||
protected LineOrSpanDirective(Context cx, T trivia, LineDirectiveKind k)
|
||||
: base(cx, trivia)
|
||||
{
|
||||
kind = k;
|
||||
}
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
trapFile.directive_lines(this, kind);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
|
||||
{
|
||||
var file = File.Create(Context, Symbol.File.ValueText);
|
||||
trapFile.directive_line_file(this, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.IO;
|
||||
|
||||
namespace Semmle.Extraction.CSharp.Entities
|
||||
{
|
||||
internal class LineSpanDirective : LineOrSpanDirective<LineSpanDirectiveTriviaSyntax>
|
||||
{
|
||||
private LineSpanDirective(Context cx, LineSpanDirectiveTriviaSyntax trivia)
|
||||
: base(cx, trivia, LineDirectiveKind.Span) { }
|
||||
|
||||
public static LineSpanDirective Create(Context cx, LineSpanDirectiveTriviaSyntax line) =>
|
||||
LineSpanDirectiveFactory.Instance.CreateEntity(cx, line, line);
|
||||
|
||||
protected override void PopulatePreprocessor(TextWriter trapFile)
|
||||
{
|
||||
var startLine = (int)Symbol.Start.Line.Value!;
|
||||
var startColumn = (int)Symbol.Start.Character.Value!;
|
||||
var endLine = (int)Symbol.End.Line.Value!;
|
||||
var endColumn = (int)Symbol.End.Character.Value!;
|
||||
trapFile.directive_line_span(this, startLine, startColumn, endLine, endColumn);
|
||||
|
||||
if (Symbol.CharacterOffset.Value is int offset)
|
||||
{
|
||||
trapFile.directive_line_offset(this, offset);
|
||||
}
|
||||
|
||||
base.PopulatePreprocessor(trapFile);
|
||||
}
|
||||
|
||||
private class LineSpanDirectiveFactory : CachedEntityFactory<LineSpanDirectiveTriviaSyntax, LineSpanDirective>
|
||||
{
|
||||
public static LineSpanDirectiveFactory Instance { get; } = new LineSpanDirectiveFactory();
|
||||
|
||||
public override LineSpanDirective Create(Context cx, LineSpanDirectiveTriviaSyntax init) => new(cx, init);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user