Skip to content

Commit 7ee9280

Browse files
authored
Additional #line span directive tests (#58761)
1 parent d54432d commit 7ee9280

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,13 @@ private static bool NeedsSeparator(SyntaxToken token, SyntaxToken next)
845845
return true;
846846
}
847847

848+
switch (token.Parent.Kind(), next.Parent.Kind())
849+
{
850+
case (SyntaxKind.LineSpanDirectiveTrivia, SyntaxKind.LineDirectivePosition):
851+
case (SyntaxKind.LineDirectivePosition, SyntaxKind.LineSpanDirectiveTrivia):
852+
return true;
853+
}
854+
848855
return false;
849856
}
850857

src/Compilers/CSharp/Test/Syntax/Diagnostics/LineSpanDirectiveTests.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ static int getTextPosition(TextLineCollection lines, LinePosition position)
508508
}
509509

510510
[Fact]
511-
public void Diagnostics()
511+
public void Diagnostics_01()
512512
{
513513
var source =
514514
@"class Program
@@ -536,6 +536,53 @@ static void Main()
536536
Diagnostic(ErrorCode.ERR_NameNotInContext, "B").WithArguments("B").WithLocation(8, 9));
537537
}
538538

539+
[Fact]
540+
public void Diagnostics_02()
541+
{
542+
var source =
543+
@"class Program
544+
{
545+
static void Main()
546+
{
547+
#line (100, 1) - (100, ) 1 ""a.txt""
548+
A();
549+
#line (200, 1) - (200, 100) 2 ""b.txt""
550+
B();
551+
#line (300, 1) - (300, 100) x ""c.txt""
552+
C();
553+
}
554+
}".NormalizeLineEndings();
555+
556+
var comp = CreateCompilation(source);
557+
comp.VerifyDiagnostics(
558+
// (5,24): error CS8938: The #line directive value is missing or out of range
559+
// #line (100, 1) - (100, ) 1 "a.txt"
560+
Diagnostic(ErrorCode.ERR_LineSpanDirectiveInvalidValue, ")").WithLocation(5, 24),
561+
// (6,9): error CS0103: The name 'A' does not exist in the current context
562+
// A();
563+
Diagnostic(ErrorCode.ERR_NameNotInContext, "A").WithArguments("A").WithLocation(6, 9),
564+
// (10,9): error CS0103: The name 'C' does not exist in the current context
565+
// C();
566+
Diagnostic(ErrorCode.ERR_NameNotInContext, "C").WithArguments("C").WithLocation(10, 9),
567+
// b.txt(200,8): error CS0103: The name 'B' does not exist in the current context
568+
// B();
569+
Diagnostic(ErrorCode.ERR_NameNotInContext, "B").WithArguments("B").WithLocation(200, 8),
570+
// b.txt(201,29): error CS1578: Quoted file name, single-line comment or end-of-line expected
571+
// #line (300, 1) - (300, 100) x "c.txt"
572+
Diagnostic(ErrorCode.ERR_MissingPPFile, "x").WithLocation(201, 29));
573+
574+
var tree = comp.SyntaxTrees[0];
575+
var actualLineMappings = GetLineMappings(tree);
576+
var expectedLineMappings = new[]
577+
{
578+
"(0,0)-(3,7) -> : (0,0)-(3,7)",
579+
"(5,0)-(5,14) -> : (5,0)-(5,14)",
580+
"(7,0)-(7,14),1 -> b.txt: (199,0)-(199,100)",
581+
"(9,0)-(11,1) -> : (9,0)-(11,1)"
582+
};
583+
AssertEx.Equal(expectedLineMappings, actualLineMappings);
584+
}
585+
539586
[Fact]
540587
public void SequencePoints()
541588
{

src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,29 @@ public void TestNormalizeLineDirectiveTrivia()
752752
// Note: the literal was formatted as a C# string literal, not as a directive string literal.
753753
}
754754

755+
[Fact]
756+
public void TestNormalizeLineSpanDirectiveNode()
757+
{
758+
TestNormalize(
759+
SyntaxFactory.LineSpanDirectiveTrivia(
760+
SyntaxFactory.Token(SyntaxKind.HashToken),
761+
SyntaxFactory.Token(SyntaxKind.LineKeyword),
762+
SyntaxFactory.LineDirectivePosition(SyntaxFactory.Literal(1), SyntaxFactory.Literal(2)),
763+
SyntaxFactory.Token(SyntaxKind.MinusToken),
764+
SyntaxFactory.LineDirectivePosition(SyntaxFactory.Literal(3), SyntaxFactory.Literal(4)),
765+
SyntaxFactory.Literal(5),
766+
SyntaxFactory.Literal("a.txt"),
767+
SyntaxFactory.Token(SyntaxKind.EndOfDirectiveToken),
768+
isActive: true),
769+
"#line (1, 2) - (3, 4) 5 \"a.txt\"\r\n");
770+
}
771+
772+
[Fact]
773+
public void TestNormalizeLineSpanDirectiveTrivia()
774+
{
775+
TestNormalizeTrivia(" # line( 1,2 )-(3,4)5\"a.txt\"", "#line (1, 2) - (3, 4) 5 \"a.txt\"\r\n");
776+
}
777+
755778
[WorkItem(538115, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538115")]
756779
[Fact]
757780
public void TestNormalizeWithinDirectives()

0 commit comments

Comments
 (0)