Skip to content

Commit

Permalink
Additional #line span directive tests (#58761)
Browse files Browse the repository at this point in the history
  • Loading branch information
cston authored Jan 11, 2022
1 parent d54432d commit 7ee9280
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,13 @@ private static bool NeedsSeparator(SyntaxToken token, SyntaxToken next)
return true;
}

switch (token.Parent.Kind(), next.Parent.Kind())
{
case (SyntaxKind.LineSpanDirectiveTrivia, SyntaxKind.LineDirectivePosition):
case (SyntaxKind.LineDirectivePosition, SyntaxKind.LineSpanDirectiveTrivia):
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ static int getTextPosition(TextLineCollection lines, LinePosition position)
}

[Fact]
public void Diagnostics()
public void Diagnostics_01()
{
var source =
@"class Program
Expand Down Expand Up @@ -536,6 +536,53 @@ static void Main()
Diagnostic(ErrorCode.ERR_NameNotInContext, "B").WithArguments("B").WithLocation(8, 9));
}

[Fact]
public void Diagnostics_02()
{
var source =
@"class Program
{
static void Main()
{
#line (100, 1) - (100, ) 1 ""a.txt""
A();
#line (200, 1) - (200, 100) 2 ""b.txt""
B();
#line (300, 1) - (300, 100) x ""c.txt""
C();
}
}".NormalizeLineEndings();

var comp = CreateCompilation(source);
comp.VerifyDiagnostics(
// (5,24): error CS8938: The #line directive value is missing or out of range
// #line (100, 1) - (100, ) 1 "a.txt"
Diagnostic(ErrorCode.ERR_LineSpanDirectiveInvalidValue, ")").WithLocation(5, 24),
// (6,9): error CS0103: The name 'A' does not exist in the current context
// A();
Diagnostic(ErrorCode.ERR_NameNotInContext, "A").WithArguments("A").WithLocation(6, 9),
// (10,9): error CS0103: The name 'C' does not exist in the current context
// C();
Diagnostic(ErrorCode.ERR_NameNotInContext, "C").WithArguments("C").WithLocation(10, 9),
// b.txt(200,8): error CS0103: The name 'B' does not exist in the current context
// B();
Diagnostic(ErrorCode.ERR_NameNotInContext, "B").WithArguments("B").WithLocation(200, 8),
// b.txt(201,29): error CS1578: Quoted file name, single-line comment or end-of-line expected
// #line (300, 1) - (300, 100) x "c.txt"
Diagnostic(ErrorCode.ERR_MissingPPFile, "x").WithLocation(201, 29));

var tree = comp.SyntaxTrees[0];
var actualLineMappings = GetLineMappings(tree);
var expectedLineMappings = new[]
{
"(0,0)-(3,7) -> : (0,0)-(3,7)",
"(5,0)-(5,14) -> : (5,0)-(5,14)",
"(7,0)-(7,14),1 -> b.txt: (199,0)-(199,100)",
"(9,0)-(11,1) -> : (9,0)-(11,1)"
};
AssertEx.Equal(expectedLineMappings, actualLineMappings);
}

[Fact]
public void SequencePoints()
{
Expand Down
23 changes: 23 additions & 0 deletions src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,29 @@ public void TestNormalizeLineDirectiveTrivia()
// Note: the literal was formatted as a C# string literal, not as a directive string literal.
}

[Fact]
public void TestNormalizeLineSpanDirectiveNode()
{
TestNormalize(
SyntaxFactory.LineSpanDirectiveTrivia(
SyntaxFactory.Token(SyntaxKind.HashToken),
SyntaxFactory.Token(SyntaxKind.LineKeyword),
SyntaxFactory.LineDirectivePosition(SyntaxFactory.Literal(1), SyntaxFactory.Literal(2)),
SyntaxFactory.Token(SyntaxKind.MinusToken),
SyntaxFactory.LineDirectivePosition(SyntaxFactory.Literal(3), SyntaxFactory.Literal(4)),
SyntaxFactory.Literal(5),
SyntaxFactory.Literal("a.txt"),
SyntaxFactory.Token(SyntaxKind.EndOfDirectiveToken),
isActive: true),
"#line (1, 2) - (3, 4) 5 \"a.txt\"\r\n");
}

[Fact]
public void TestNormalizeLineSpanDirectiveTrivia()
{
TestNormalizeTrivia(" # line( 1,2 )-(3,4)5\"a.txt\"", "#line (1, 2) - (3, 4) 5 \"a.txt\"\r\n");
}

[WorkItem(538115, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/538115")]
[Fact]
public void TestNormalizeWithinDirectives()
Expand Down

0 comments on commit 7ee9280

Please sign in to comment.