Skip to content

Commit 2451f90

Browse files
authored
Avoid ignored directive errors in disabled regions (#78158)
1 parent f276bf7 commit 2451f90

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

src/Compilers/CSharp/Portable/Parser/DirectiveParser.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -695,19 +695,22 @@ private DirectiveTriviaSyntax ParseShebangDirective(SyntaxToken hash, SyntaxToke
695695

696696
private DirectiveTriviaSyntax ParseIgnoredDirective(SyntaxToken hash, SyntaxToken colon, bool isActive, bool isFollowingToken)
697697
{
698-
if (!lexer.Options.FileBasedProgram)
698+
if (isActive)
699699
{
700-
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram);
701-
}
700+
if (!lexer.Options.FileBasedProgram)
701+
{
702+
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredNeedsFileBasedProgram);
703+
}
702704

703-
if (isFollowingToken)
704-
{
705-
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsToken);
706-
}
705+
if (isFollowingToken)
706+
{
707+
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsToken);
708+
}
707709

708-
if (_context.SeenAnyIfDirectives)
709-
{
710-
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsIf);
710+
if (_context.SeenAnyIfDirectives)
711+
{
712+
colon = this.AddError(colon, ErrorCode.ERR_PPIgnoredFollowsIf);
713+
}
711714
}
712715

713716
SyntaxToken endOfDirective = this.lexer.LexEndOfDirectiveWithOptionalContent(out SyntaxToken content);

src/Compilers/CSharp/Test/Syntax/Parsing/IgnoredDirectiveParsingTests.cs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ public void AfterToken()
287287
EOF();
288288
}
289289

290-
[Fact]
290+
[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/78157")]
291291
public void AfterIf()
292292
{
293293
var source = """
@@ -299,7 +299,7 @@ public void AfterIf()
299299
""";
300300

301301
VerifyTrivia();
302-
UsingTree(source, TestOptions.Regular.WithFeature(FeatureName),
302+
UsingTree(source, TestOptions.Regular.WithFeature(FeatureName).WithPreprocessorSymbols("X"),
303303
// (3,2): error CS9283: '#:' directives cannot be after '#if' directive
304304
// #:y
305305
Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(3, 2),
@@ -366,6 +366,71 @@ public void AfterIf()
366366
}
367367
}
368368
EOF();
369+
370+
UsingTree(source, TestOptions.Regular.WithFeature(FeatureName),
371+
// (5,2): error CS9299: '#:' directives cannot be after '#if' directive
372+
// #:z
373+
Diagnostic(ErrorCode.ERR_PPIgnoredFollowsIf, ":").WithLocation(5, 2));
374+
375+
N(SyntaxKind.CompilationUnit);
376+
{
377+
N(SyntaxKind.EndOfFileToken);
378+
{
379+
L(SyntaxKind.IgnoredDirectiveTrivia);
380+
{
381+
N(SyntaxKind.HashToken);
382+
N(SyntaxKind.ColonToken);
383+
N(SyntaxKind.StringLiteralToken, "x");
384+
N(SyntaxKind.EndOfDirectiveToken);
385+
{
386+
T(SyntaxKind.EndOfLineTrivia, "\n");
387+
}
388+
}
389+
L(SyntaxKind.IfDirectiveTrivia);
390+
{
391+
N(SyntaxKind.HashToken);
392+
N(SyntaxKind.IfKeyword);
393+
{
394+
T(SyntaxKind.WhitespaceTrivia, " ");
395+
}
396+
N(SyntaxKind.IdentifierName);
397+
{
398+
N(SyntaxKind.IdentifierToken, "X");
399+
}
400+
N(SyntaxKind.EndOfDirectiveToken);
401+
{
402+
T(SyntaxKind.EndOfLineTrivia, "\n");
403+
}
404+
}
405+
L(SyntaxKind.IgnoredDirectiveTrivia);
406+
{
407+
N(SyntaxKind.HashToken);
408+
N(SyntaxKind.ColonToken);
409+
N(SyntaxKind.StringLiteralToken, "y");
410+
N(SyntaxKind.EndOfDirectiveToken);
411+
{
412+
T(SyntaxKind.EndOfLineTrivia, "\n");
413+
}
414+
}
415+
L(SyntaxKind.EndIfDirectiveTrivia);
416+
{
417+
N(SyntaxKind.HashToken);
418+
N(SyntaxKind.EndIfKeyword);
419+
N(SyntaxKind.EndOfDirectiveToken);
420+
{
421+
T(SyntaxKind.EndOfLineTrivia, "\n");
422+
}
423+
}
424+
L(SyntaxKind.IgnoredDirectiveTrivia);
425+
{
426+
N(SyntaxKind.HashToken);
427+
N(SyntaxKind.ColonToken);
428+
N(SyntaxKind.StringLiteralToken, "z");
429+
N(SyntaxKind.EndOfDirectiveToken);
430+
}
431+
}
432+
}
433+
EOF();
369434
}
370435

371436
[Fact]

0 commit comments

Comments
 (0)