Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/Compilers/CSharp/Portable/Parser/DirectiveParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,19 +687,22 @@ private DirectiveTriviaSyntax ParseShebangDirective(SyntaxToken hash, SyntaxToke

private DirectiveTriviaSyntax ParseIgnoredDirective(SyntaxToken hash, SyntaxToken colon, bool isActive, bool isFollowingToken)
{
if (!lexer.Options.FileBasedProgram)
if (isActive)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram);
}
if (!lexer.Options.FileBasedProgram)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram);
}

if (isFollowingToken)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsToken);
}
if (isFollowingToken)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsToken);
}

if (_context.SeenAnyIfDirectives)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsIf);
if (_context.SeenAnyIfDirectives)
{
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsIf);
}
}

SyntaxToken endOfDirective = this.lexer.LexEndOfDirectiveWithOptionalContent(out SyntaxToken content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public void AfterToken()
EOF();
}

[Fact]
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78157")]
public void AfterIf()
{
var source = """
Expand All @@ -275,7 +275,7 @@ public void AfterIf()
""";

VerifyTrivia();
UsingTree(source, TestOptions.Regular.WithFeature(FeatureName),
UsingTree(source, TestOptions.Regular.WithFeature(FeatureName).WithPreprocessorSymbols("X"),
// (3,2): error CS9283: '#:' directives cannot be after '#if' directive
// #:y
Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(3, 2),
Expand Down Expand Up @@ -342,6 +342,71 @@ public void AfterIf()
}
}
EOF();

UsingTree(source, TestOptions.Regular.WithFeature(FeatureName),
// (5,2): error CS9299: '#:' directives cannot be after '#if' directive
// #:z
Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(5, 2));

N(SyntaxKind.CompilationUnit);
{
N(SyntaxKind.EndOfFileToken);
{
L(SyntaxKind.IgnoredDirectiveTrivia);
{
N(SyntaxKind.HashToken);
N(SyntaxKind.ColonToken);
N(SyntaxKind.StringLiteralToken, "x");
N(SyntaxKind.EndOfDirectiveToken);
{
T(SyntaxKind.EndOfLineTrivia, "\n");
}
}
L(SyntaxKind.IfDirectiveTrivia);
{
N(SyntaxKind.HashToken);
N(SyntaxKind.IfKeyword);
{
T(SyntaxKind.WhitespaceTrivia, " ");
}
N(SyntaxKind.IdentifierName);
{
N(SyntaxKind.IdentifierToken, "X");
}
N(SyntaxKind.EndOfDirectiveToken);
{
T(SyntaxKind.EndOfLineTrivia, "\n");
}
}
L(SyntaxKind.IgnoredDirectiveTrivia);
{
N(SyntaxKind.HashToken);
N(SyntaxKind.ColonToken);
N(SyntaxKind.StringLiteralToken, "y");
N(SyntaxKind.EndOfDirectiveToken);
{
T(SyntaxKind.EndOfLineTrivia, "\n");
}
}
L(SyntaxKind.EndIfDirectiveTrivia);
{
N(SyntaxKind.HashToken);
N(SyntaxKind.EndIfKeyword);
N(SyntaxKind.EndOfDirectiveToken);
{
T(SyntaxKind.EndOfLineTrivia, "\n");
}
}
L(SyntaxKind.IgnoredDirectiveTrivia);
{
N(SyntaxKind.HashToken);
N(SyntaxKind.ColonToken);
N(SyntaxKind.StringLiteralToken, "z");
N(SyntaxKind.EndOfDirectiveToken);
}
}
}
EOF();
}

[Fact]
Expand Down