Enforce source generation testing with OptimizationLevel

This commit is contained in:
Huo Yaoyuan
2024-11-28 01:31:27 +08:00
parent 23f1c8e409
commit 2a7d8dce8e
4 changed files with 7 additions and 9 deletions

View File

@@ -76,7 +76,7 @@ namespace osu.Framework.SourceGeneration.Tests
{
Compilation = CSharpCompilation.Create("test",
references: new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) },
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release));
}
public GeneratorDriverRunResult RunGenerators(ref GeneratorDriver driver)

View File

@@ -35,8 +35,6 @@ namespace osu.Framework.SourceGeneration.Tests.Verifiers
driver = CSharpGeneratorDriver.Create(generator = new TSourceGenerator());
driver = driver.WithUpdatedParseOptions(CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion));
generator.ForceRun = true;
this.commonSources = commonSources;
this.commonGenerated = commonGenerated;
this.multiPhaseSources = multiPhaseSources;

View File

@@ -20,6 +20,11 @@ namespace osu.Framework.SourceGeneration.Tests.Verifiers
protected override IEnumerable<Type> GetSourceGenerators() => [typeof(TSourceGenerator)];
protected override CompilationOptions CreateCompilationOptions()
{
return base.CreateCompilationOptions().WithOptimizationLevel(OptimizationLevel.Release);
}
protected override ParseOptions CreateParseOptions()
{
return ((CSharpParseOptions)base.CreateParseOptions()).WithLanguageVersion(LanguageVersion);

View File

@@ -14,11 +14,6 @@ namespace osu.Framework.SourceGeneration.Generators
{
public readonly GeneratorEventDriver EventDriver = new GeneratorEventDriver();
/// <summary>
/// Whether the generator should be forcefully run, even if building as debug.
/// </summary>
public bool ForceRun;
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Stage 1: Create SyntaxTarget objects for all classes.
@@ -28,7 +23,7 @@ namespace osu.Framework.SourceGeneration.Generators
(ctx, _) => returnWithEvent(new IncrementalSyntaxTarget((ClassDeclarationSyntax)ctx.Node, ctx.SemanticModel), EventDriver.OnSyntaxTargetCreated))
.Select((t, _) => t.WithName())
.Combine(context.CompilationProvider)
.Where(c => ForceRun || c.Right.Options.OptimizationLevel == OptimizationLevel.Release)
.Where(c => c.Right.Options.OptimizationLevel == OptimizationLevel.Release)
.Select((t, _) => t.Item1)
.Select((t, _) => returnWithEvent(t.WithSemanticTarget(CreateSemanticTarget), EventDriver.OnSemanticTargetCreated));