Skip to content

Commit d7cd1cf

Browse files
committed
C#: Address review comments.
1 parent de3d62b commit d7cd1cf

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,18 @@ namespace Semmle.Extraction.CSharp.Entities
88
internal class LineDirective : LineOrSpanDirective<LineDirectiveTriviaSyntax>
99
{
1010
private LineDirective(Context cx, LineDirectiveTriviaSyntax trivia)
11-
: base(cx, trivia)
12-
{
13-
}
14-
15-
protected override void PopulatePreprocessor(TextWriter trapFile)
16-
{
17-
var type = Symbol.Line.Kind() switch
11+
: base(cx, trivia, trivia.Line.Kind() switch
1812
{
1913
SyntaxKind.DefaultKeyword => LineDirectiveKind.Default,
2014
SyntaxKind.HiddenKeyword => LineDirectiveKind.Hidden,
2115
SyntaxKind.NumericLiteralToken => LineDirectiveKind.Numeric,
22-
_ => throw new InternalError(Symbol, "Unhandled line token kind")
23-
};
24-
25-
trapFile.directive_lines(this, type);
16+
_ => throw new InternalError(trivia, "Unhandled line token kind")
17+
})
18+
{
19+
}
2620

21+
protected override void PopulatePreprocessor(TextWriter trapFile)
22+
{
2723
if (Symbol.Line.IsKind(SyntaxKind.NumericLiteralToken))
2824
{
2925
var value = (int)Symbol.Line.Value!;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ internal enum LineDirectiveKind
1515

1616
internal abstract class LineOrSpanDirective<T> : PreprocessorDirective<T> where T : LineOrSpanDirectiveTriviaSyntax
1717
{
18-
protected LineOrSpanDirective(Context cx, T trivia)
18+
private readonly LineDirectiveKind kind;
19+
20+
protected LineOrSpanDirective(Context cx, T trivia, LineDirectiveKind k)
1921
: base(cx, trivia)
2022
{
23+
kind = k;
2124
}
2225

2326
protected override void PopulatePreprocessor(TextWriter trapFile)
2427
{
28+
trapFile.directive_lines(this, kind);
29+
2530
if (!string.IsNullOrWhiteSpace(Symbol.File.ValueText))
2631
{
2732
var file = File.Create(Context, Symbol.File.ValueText);

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,24 @@ namespace Semmle.Extraction.CSharp.Entities
88
internal class LineSpanDirective : LineOrSpanDirective<LineSpanDirectiveTriviaSyntax>
99
{
1010
private LineSpanDirective(Context cx, LineSpanDirectiveTriviaSyntax trivia)
11-
: base(cx, trivia) { }
11+
: base(cx, trivia, LineDirectiveKind.Span) { }
12+
1213
public static LineSpanDirective Create(Context cx, LineSpanDirectiveTriviaSyntax line) =>
1314
LineSpanDirectiveFactory.Instance.CreateEntity(cx, line, line);
1415

1516
protected override void PopulatePreprocessor(TextWriter trapFile)
1617
{
17-
trapFile.directive_lines(this, LineDirectiveKind.Span);
18-
1918
var startLine = (int)Symbol.Start.Line.Value!;
2019
var startColumn = (int)Symbol.Start.Character.Value!;
2120
var endLine = (int)Symbol.End.Line.Value!;
2221
var endColumn = (int)Symbol.End.Character.Value!;
2322
trapFile.directive_line_span(this, startLine, startColumn, endLine, endColumn);
2423

25-
var offset = Symbol.CharacterOffset.Value;
26-
if (offset is not null)
24+
if (Symbol.CharacterOffset.Value is int offset)
2725
{
28-
trapFile.directive_line_offset(this, (int)offset);
26+
trapFile.directive_line_offset(this, offset);
2927
}
28+
3029
base.PopulatePreprocessor(trapFile);
3130
}
3231

csharp/ql/lib/semmle/code/csharp/Preprocessor.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ class HiddenLineDirective extends LineDirective {
208208
override string getAPrimaryQlClass() { result = "HiddenLineDirective" }
209209
}
210210

211-
abstract private class NumericOrSpanLineDirective extends LineDirective {
211+
private class NumericOrSpanLineDirective extends LineDirective {
212+
NumericOrSpanLineDirective() { directive_lines(this, [2, 3]) }
213+
212214
/** Gets the referenced file of this directive. */
213215
File getReferencedFile() { directive_line_file(this, result) }
214216
}
@@ -234,7 +236,7 @@ class SpanLineDirective extends NumericOrSpanLineDirective {
234236
/** Gets the offset of this directive. */
235237
int getOffset() { directive_line_offset(this, result) }
236238

237-
/** Gets the span of this directive. */
239+
/** Holds if the specified start and end positions match this SpanLineDirective. */
238240
predicate span(int startLine, int startColumn, int endLine, int endColumn) {
239241
directive_line_span(this, startLine, startColumn, endLine, endColumn)
240242
}

0 commit comments

Comments
 (0)