Skip to content

Commit 01bef7b

Browse files
authored
Merge pull request #6674 from smoogipoo/improve-source-generators
Replace compilation combination with semantic model check in source generators
2 parents d44dfb5 + f594e5c commit 01bef7b

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

osu.Framework.SourceGeneration.Tests/Dependencies/DependencyInjectionSourceGeneratorTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using System.Threading.Tasks;
5+
using Microsoft.CodeAnalysis;
56
using Xunit;
67
using VerifyCS = osu.Framework.SourceGeneration.Tests.Verifiers.CSharpSourceGeneratorVerifier<osu.Framework.SourceGeneration.Generators.Dependencies.DependencyInjectionSourceGenerator>;
78

@@ -47,5 +48,19 @@ public Task Check(string name)
4748

4849
return VerifyCS.VerifyAsync(commonSourceFiles, sourceFiles, commonGeneratedFiles, generatedFiles);
4950
}
51+
52+
[Theory]
53+
[InlineData("EmptyFile")]
54+
public Task CheckDebugNoOutput(string name)
55+
{
56+
GetTestSources(name,
57+
out (string filename, string content)[] commonSourceFiles,
58+
out (string filename, string content)[] sourceFiles,
59+
out _,
60+
out _
61+
);
62+
63+
return VerifyCS.VerifyAsync(commonSourceFiles, sourceFiles, [], [], OptimizationLevel.Debug);
64+
}
5065
}
5166
}

osu.Framework.SourceGeneration.Tests/Verifiers/CSharpSourceGeneratorVerifier.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading;
66
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.Text;
89
using osu.Framework.SourceGeneration.Generators;
910

@@ -16,9 +17,10 @@ public static async Task VerifyAsync(
1617
(string filename, string content)[] commonSources,
1718
(string filename, string content)[] sources,
1819
(string filename, string content)[] commonGenerated,
19-
(string filename, string content)[] generated)
20+
(string filename, string content)[] generated,
21+
OptimizationLevel optimizationLevel = OptimizationLevel.Release)
2022
{
21-
var test = new Test();
23+
var test = new Test(optimizationLevel);
2224

2325
foreach (var s in commonSources)
2426
test.TestState.Sources.Add((s.filename, SourceText.From(s.content, Encoding.UTF8)));

osu.Framework.SourceGeneration.Tests/Verifiers/CSharpSourceGeneratorVerifier_Test.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ public partial class CSharpSourceGeneratorVerifier<TSourceGenerator>
1616
{
1717
public class Test : CSharpSourceGeneratorTest<EmptySourceGeneratorProvider, DefaultVerifier>
1818
{
19+
private readonly OptimizationLevel optimizationLevel;
20+
21+
public Test(OptimizationLevel optimizationLevel = OptimizationLevel.Release)
22+
{
23+
this.optimizationLevel = optimizationLevel;
24+
}
25+
1926
public LanguageVersion LanguageVersion { get; set; } = LanguageVersion.Default;
2027

2128
protected override IEnumerable<Type> GetSourceGenerators() => [typeof(TSourceGenerator)];
2229

2330
protected override CompilationOptions CreateCompilationOptions()
2431
{
25-
return base.CreateCompilationOptions().WithOptimizationLevel(OptimizationLevel.Release);
32+
return base.CreateCompilationOptions().WithOptimizationLevel(optimizationLevel);
2633
}
2734

2835
protected override ParseOptions CreateParseOptions()

osu.Framework.SourceGeneration/Generators/AbstractIncrementalGenerator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2020
IncrementalValuesProvider<IncrementalSyntaxTarget> syntaxTargets =
2121
context.SyntaxProvider.CreateSyntaxProvider(
2222
(n, _) => isSyntaxTarget(n),
23-
(ctx, _) => returnWithEvent(new IncrementalSyntaxTarget((ClassDeclarationSyntax)ctx.Node, ctx.SemanticModel), EventDriver.OnSyntaxTargetCreated))
24-
.Select((t, _) => t.WithName())
25-
.Combine(context.CompilationProvider)
26-
.Where(c => c.Right.Options.OptimizationLevel == OptimizationLevel.Release)
27-
.Select((t, _) => t.Item1)
23+
(ctx, _) =>
24+
{
25+
if (ctx.SemanticModel.Compilation.Options.OptimizationLevel == OptimizationLevel.Debug)
26+
return null;
27+
28+
return returnWithEvent(new IncrementalSyntaxTarget((ClassDeclarationSyntax)ctx.Node, ctx.SemanticModel), EventDriver.OnSyntaxTargetCreated);
29+
})
30+
.Where(t => t != null)
31+
.Select((t, _) => t!.WithName())
2832
.Select((t, _) => returnWithEvent(t.WithSemanticTarget(CreateSemanticTarget), EventDriver.OnSemanticTargetCreated));
2933

3034
// Stage 2: Separate out the old and new syntax targets for the same class object.

0 commit comments

Comments
 (0)