From 7ee9280d04a3f5d269839351f9ca71e93f496c9e Mon Sep 17 00:00:00 2001 From: Charles Stoner <10732005+cston@users.noreply.github.com> Date: Tue, 11 Jan 2022 11:53:44 -0800 Subject: [PATCH] Additional #line span directive tests (#58761) --- .../Portable/Syntax/SyntaxNormalizer.cs | 7 +++ .../Diagnostics/LineSpanDirectiveTests.cs | 49 ++++++++++++++++++- .../Syntax/Syntax/SyntaxNormalizerTests.cs | 23 +++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs b/src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs index 11e4d3eefbd11..c38a56fce2aef 100644 --- a/src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs +++ b/src/Compilers/CSharp/Portable/Syntax/SyntaxNormalizer.cs @@ -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; } diff --git a/src/Compilers/CSharp/Test/Syntax/Diagnostics/LineSpanDirectiveTests.cs b/src/Compilers/CSharp/Test/Syntax/Diagnostics/LineSpanDirectiveTests.cs index 511f88fc20c2e..467fed76f7360 100644 --- a/src/Compilers/CSharp/Test/Syntax/Diagnostics/LineSpanDirectiveTests.cs +++ b/src/Compilers/CSharp/Test/Syntax/Diagnostics/LineSpanDirectiveTests.cs @@ -508,7 +508,7 @@ static int getTextPosition(TextLineCollection lines, LinePosition position) } [Fact] - public void Diagnostics() + public void Diagnostics_01() { var source = @"class Program @@ -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() { diff --git a/src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs b/src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs index 1861e0efeaf80..e85842637d214 100644 --- a/src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs +++ b/src/Compilers/CSharp/Test/Syntax/Syntax/SyntaxNormalizerTests.cs @@ -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()