Skip to content

Commit 195d40c

Browse files
committed
C#: Add new class needed for LineSpanDirective and modify existing implementation to use the new types.
1 parent a197bef commit 195d40c

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/PreprocessorDirectives/LineDirective.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Semmle.Extraction.CSharp.Entities
77
{
8-
internal class LineDirective : PreprocessorDirective<LineDirectiveTriviaSyntax>
8+
internal class LineDirective : LineOrSpanDirective<LineDirectiveTriviaSyntax>
99
{
1010
private LineDirective(Context cx, LineDirectiveTriviaSyntax trivia)
1111
: base(cx, trivia)
@@ -16,9 +16,9 @@ protected override void PopulatePreprocessor(TextWriter trapFile)
1616
{
1717
var type = Symbol.Line.Kind() switch
1818
{
19-
SyntaxKind.DefaultKeyword => 0,
20-
SyntaxKind.HiddenKeyword => 1,
21-
SyntaxKind.NumericLiteralToken => 2,
19+
SyntaxKind.DefaultKeyword => LineDirectiveKind.Default,
20+
SyntaxKind.HiddenKeyword => LineDirectiveKind.Hidden,
21+
SyntaxKind.NumericLiteralToken => LineDirectiveKind.Numeric,
2222
_ => throw new InternalError(Symbol, "Unhandled line token kind")
2323
};
2424

@@ -28,13 +28,9 @@ protected override void PopulatePreprocessor(TextWriter trapFile)
2828
{
2929
var value = (int)Symbol.Line.Value!;
3030
trapFile.directive_line_value(this, value);
31-
32-
if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
33-
{
34-
var file = File.Create(Context, Symbol.File.ValueText);
35-
trapFile.directive_line_file(this, file);
36-
}
3731
}
32+
33+
base.PopulatePreprocessor(trapFile);
3834
}
3935

4036
public static LineDirective Create(Context cx, LineDirectiveTriviaSyntax line) =>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using System.IO;
5+
6+
namespace Semmle.Extraction.CSharp.Entities
7+
{
8+
internal class LineSpanDirective : LineOrSpanDirective<LineSpanDirectiveTriviaSyntax>
9+
{
10+
private LineSpanDirective(Context cx, LineSpanDirectiveTriviaSyntax trivia)
11+
: base(cx, trivia) { }
12+
public static LineSpanDirective Create(Context cx, LineSpanDirectiveTriviaSyntax line) =>
13+
LineSpanDirectiveFactory.Instance.CreateEntity(cx, line, line);
14+
15+
protected override void PopulatePreprocessor(TextWriter trapFile)
16+
{
17+
trapFile.directive_lines(this, LineDirectiveKind.Span);
18+
19+
var startLine = (int)Symbol.Start.Line.Value!;
20+
var startColumn = (int)Symbol.Start.Character.Value!;
21+
var endLine = (int)Symbol.End.Line.Value!;
22+
var endColumn = (int)Symbol.End.Character.Value!;
23+
trapFile.directive_line_span(this, startLine, startColumn, endLine, endColumn);
24+
25+
var offset = Symbol.CharacterOffset.Value;
26+
if (offset is not null)
27+
{
28+
trapFile.directive_line_offset(this, (int)offset);
29+
}
30+
base.PopulatePreprocessor(trapFile);
31+
}
32+
33+
private class LineSpanDirectiveFactory : CachedEntityFactory<LineSpanDirectiveTriviaSyntax, LineSpanDirective>
34+
{
35+
public static LineSpanDirectiveFactory Instance { get; } = new LineSpanDirectiveFactory();
36+
37+
public override LineSpanDirective Create(Context cx, LineSpanDirectiveTriviaSyntax init) => new(cx, init);
38+
}
39+
}
40+
}

csharp/extractor/Semmle.Extraction.CSharp/Tuples.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,15 +413,21 @@ internal static void directive_errors(this TextWriter trapFile, ErrorDirective d
413413
internal static void directive_nullables(this TextWriter trapFile, NullableDirective directive, int setting, int target) =>
414414
trapFile.WriteTuple("directive_nullables", directive, setting, target);
415415

416-
internal static void directive_lines(this TextWriter trapFile, LineDirective directive, int kind) =>
417-
trapFile.WriteTuple("directive_lines", directive, kind);
416+
internal static void directive_lines<T>(this TextWriter trapFile, LineOrSpanDirective<T> directive, LineDirectiveKind kind) where T : LineOrSpanDirectiveTriviaSyntax =>
417+
trapFile.WriteTuple("directive_lines", directive, (int)kind);
418418

419419
internal static void directive_line_value(this TextWriter trapFile, LineDirective directive, int line) =>
420420
trapFile.WriteTuple("directive_line_value", directive, line);
421421

422-
internal static void directive_line_file(this TextWriter trapFile, LineDirective directive, Extraction.Entities.File file) =>
422+
internal static void directive_line_file<T>(this TextWriter trapFile, LineOrSpanDirective<T> directive, Extraction.Entities.File file) where T : LineOrSpanDirectiveTriviaSyntax =>
423423
trapFile.WriteTuple("directive_line_file", directive, file);
424424

425+
internal static void directive_line_offset(this TextWriter trapFile, LineSpanDirective directive, int offset) =>
426+
trapFile.WriteTuple("directive_line_offset", directive, offset);
427+
428+
internal static void directive_line_span(this TextWriter trapFile, LineSpanDirective directive, int startLine, int startColumn, int endLine, int endColumn) =>
429+
trapFile.WriteTuple("directive_line_span", directive, startLine, startColumn, endLine, endColumn);
430+
425431
internal static void directive_regions(this TextWriter trapFile, RegionDirective directive, string name) =>
426432
trapFile.WriteTuple("directive_regions", directive, name);
427433

0 commit comments

Comments
 (0)