C#: Address review comments.

This commit is contained in:
Michael Nebel
2022-01-19 13:50:02 +01:00
parent de3d62b3f4
commit d7cd1cf0b9
4 changed files with 22 additions and 20 deletions

View File

@@ -8,22 +8,18 @@ namespace Semmle.Extraction.CSharp.Entities
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 => LineDirectiveKind.Default,
SyntaxKind.HiddenKeyword => LineDirectiveKind.Hidden,
SyntaxKind.NumericLiteralToken => LineDirectiveKind.Numeric,
_ => 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!;

View File

@@ -15,13 +15,18 @@ namespace Semmle.Extraction.CSharp.Entities
internal abstract class LineOrSpanDirective<T> : PreprocessorDirective<T> where T : LineOrSpanDirectiveTriviaSyntax
{
protected LineOrSpanDirective(Context cx, T trivia)
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);

View File

@@ -8,25 +8,24 @@ namespace Semmle.Extraction.CSharp.Entities
internal class LineSpanDirective : LineOrSpanDirective<LineSpanDirectiveTriviaSyntax>
{
private LineSpanDirective(Context cx, LineSpanDirectiveTriviaSyntax trivia)
: base(cx, 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)
{
trapFile.directive_lines(this, LineDirectiveKind.Span);
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);
var offset = Symbol.CharacterOffset.Value;
if (offset is not null)
if (Symbol.CharacterOffset.Value is int offset)
{
trapFile.directive_line_offset(this, (int)offset);
trapFile.directive_line_offset(this, offset);
}
base.PopulatePreprocessor(trapFile);
}

View File

@@ -208,7 +208,9 @@ class HiddenLineDirective extends LineDirective {
override string getAPrimaryQlClass() { result = "HiddenLineDirective" }
}
abstract private class NumericOrSpanLineDirective extends LineDirective {
private class NumericOrSpanLineDirective extends LineDirective {
NumericOrSpanLineDirective() { directive_lines(this, [2, 3]) }
/** Gets the referenced file of this directive. */
File getReferencedFile() { directive_line_file(this, result) }
}
@@ -234,7 +236,7 @@ class SpanLineDirective extends NumericOrSpanLineDirective {
/** Gets the offset of this directive. */
int getOffset() { directive_line_offset(this, result) }
/** Gets the span of this directive. */
/** Holds if the specified start and end positions match this SpanLineDirective. */
predicate span(int startLine, int startColumn, int endLine, int endColumn) {
directive_line_span(this, startLine, startColumn, endLine, endColumn)
}