Skip to content

Commit

Permalink
Fix #83 Move directive stack to the method level instead of a block l…
Browse files Browse the repository at this point in the history
…evel
  • Loading branch information
virzak committed Jul 21, 2024
1 parent 6c4ec93 commit 32fc8a9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
]
},
"dotnet-reportgenerator-globaltool": {
"version": "5.3.7",
"version": "5.3.8",
"commands": [
"reportgenerator"
]
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageVersion Include="System.Linq.Async" Version="6.0.1" />
<PackageVersion Include="System.Runtime.Experimental" Version="6.0.2" />
<PackageVersion Include="Verify.SourceGenerators" Version="2.2.0" />
<PackageVersion Include="Verify.XUnit" Version="25.3.1" />
<PackageVersion Include="Verify.XUnit" Version="26.0.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
Expand Down
13 changes: 6 additions & 7 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel, bool disa
private readonly Dictionary<string, string> renamedLocalFunctions = [];
private readonly ImmutableArray<ReportedDiagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<ReportedDiagnostic>();
private readonly Stack<ExpressionSyntax> replaceInInvocation = new();
private readonly DirectiveStack directiveStack = new();

private enum SyncOnlyDirectiveType
{
Expand Down Expand Up @@ -793,7 +794,7 @@ List<SyntaxTrivia> RemoveFirstEndIf(SyntaxTriviaList list)
/// <inheritdoc/>
public override SyntaxNode? VisitSwitchSection(SwitchSectionSyntax node)
{
var statementProcessor = new StatementProcessor(this, node.Statements);
var statementProcessor = new StatementProcessor(this, node.Statements, directiveStack);
if (statementProcessor.HasErrors)
{
return node;
Expand All @@ -809,7 +810,7 @@ List<SyntaxTrivia> RemoveFirstEndIf(SyntaxTriviaList list)
/// <inheritdoc/>
public override SyntaxNode? VisitBlock(BlockSyntax node)
{
var statementProcessor = new StatementProcessor(this, node.Statements);
var statementProcessor = new StatementProcessor(this, node.Statements, directiveStack);
if (statementProcessor.HasErrors)
{
return node;
Expand All @@ -821,7 +822,7 @@ List<SyntaxTrivia> RemoveFirstEndIf(SyntaxTriviaList list)
var retVal = @base.WithStatements(newStatements);

var lastToken = retVal.CloseBraceToken;
if (ProcessTrivia(node.CloseBraceToken.LeadingTrivia, statementProcessor.DirectiveStack) is var (_, newStatements2, newTrivia))
if (ProcessTrivia(node.CloseBraceToken.LeadingTrivia, directiveStack) is var (_, newStatements2, newTrivia))
{
var oldStatements = retVal.Statements.ToList();
oldStatements.AddRange([.. newStatements2]);
Expand Down Expand Up @@ -2262,15 +2263,13 @@ private sealed class StatementProcessor
{
private readonly Dictionary<int, ExtraNodeInfo> extraNodeInfoList = [];

public StatementProcessor(AsyncToSyncRewriter rewriter, SyntaxList<StatementSyntax> statements)
public StatementProcessor(AsyncToSyncRewriter rewriter, SyntaxList<StatementSyntax> statements, DirectiveStack directiveStack)
{
HasErrors = !rewriter.PreProcess(statements, extraNodeInfoList, DirectiveStack);
HasErrors = !rewriter.PreProcess(statements, extraNodeInfoList, directiveStack);
}

public bool HasErrors { get; }

public DirectiveStack DirectiveStack { get; } = new();

public SyntaxList<StatementSyntax> PostProcess(SyntaxList<StatementSyntax> statements)
{
for (var i = 0; i < statements.Count; ++i)
Expand Down
15 changes: 15 additions & 0 deletions tests/Generator.Tests/PreprocessorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Generator.Tests;

public class PreprocessorTests
{
[Fact]
public Task MacrosAroundBraces() => $$"""
#if !BLA
{
#endif

#if !BLA
}
#endif
""".Verify(sourceType: SourceType.MethodBody);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//HintName: Test.Class.MethodAsync.g.cs
#if !BLA
{
#endif

#if !BLA
}
#endif

0 comments on commit 32fc8a9

Please sign in to comment.