feat: 增强 AutoSceneGenerator 与 AutoRegisterExportedCollectionsGenerator 的验证与安全机制

### AutoSceneGenerator
- 引入保留成员名称集合(GeneratedMemberNames),包含:
  - SceneKeyStr
  - __autoSceneBehavior_Generated
- 实现 ReportGeneratedMemberConflicts 方法:
  - 检测用户定义成员与生成成员冲突
  - 提供清晰的诊断信息
- 在生成流程中集成冲突检测,避免重复成员导致的编译错误

### AutoRegisterExportedCollectionsGenerator
- 增强集合注册生成器的验证逻辑:
  - 新增诊断 GF_AutoExport_006:导出集合成员必须为实例可读成员
  - 新增诊断 GF_AutoExport_007:注册表成员必须为实例可读成员
- 实现 IsInstanceReadableMember 方法:
  - 校验成员为非静态字段或可读属性
- 修复符号访问性检查:
  - 确保注册方法对所有者类型可访问
- 优化生成逻辑:
  - 过滤重复的部分类声明,仅生成一次源码

### Tests
- AutoSceneGenerator
  - 覆盖保留成员冲突场景:
    - SceneKeyStr 冲突
    - __autoSceneBehavior_Generated 冲突

- AutoRegisterExportedCollectionsGenerator
  - 覆盖完整验证逻辑:
    - 不可读成员 → GF_AutoExport_006 / 007
    - 方法不可访问 → GF_AutoExport_003
    - 多个 partial class → 仅生成一个源文件
This commit is contained in:
GeWuYou 2026-04-13 13:04:00 +08:00
parent 3fadba2d79
commit be928718e3
6 changed files with 475 additions and 3 deletions

View File

@ -216,4 +216,116 @@ public class AutoSceneGeneratorTests
source, source,
("TestApp_GameplayRoot.AutoScene.g.cs", expected)); ("TestApp_GameplayRoot.AutoScene.g.cs", expected));
} }
/// <summary>
/// 验证宿主类型声明同名 <c>SceneKeyStr</c> 属性时,生成器会报告保留成员冲突并停止生成。
/// </summary>
[Test]
public async Task Reports_Diagnostic_When_SceneKeyStr_Property_Name_Conflicts()
{
const string source = """
using System;
using GFramework.Godot.SourceGenerators.Abstractions;
using Godot;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoSceneAttribute : Attribute
{
public AutoSceneAttribute(string key) { }
}
}
namespace Godot
{
public class Node { }
public class Node2D : Node { }
}
namespace TestApp
{
[AutoScene("Gameplay")]
public partial class GameplayRoot : Node2D
{
public static string {|#0:SceneKeyStr|} => "Conflict";
}
}
""";
var test = new CSharpSourceGeneratorTest<AutoSceneGenerator, DefaultVerifier>
{
TestState =
{
Sources = { source }
},
DisabledDiagnostics = { "GF_Common_Trace_001" },
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
};
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Common_Class_002", DiagnosticSeverity.Error)
.WithLocation(0)
.WithArguments("GameplayRoot", "SceneKeyStr"));
await test.RunAsync();
}
/// <summary>
/// 验证宿主类型声明同名缓存字段时,生成器会报告保留成员冲突并停止生成。
/// </summary>
[Test]
public async Task Reports_Diagnostic_When_Generated_Behavior_Field_Name_Conflicts()
{
const string source = """
using System;
using GFramework.Game.Abstractions.Scene;
using GFramework.Godot.SourceGenerators.Abstractions;
using Godot;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoSceneAttribute : Attribute
{
public AutoSceneAttribute(string key) { }
}
}
namespace Godot
{
public class Node { }
public class Node2D : Node { }
}
namespace GFramework.Game.Abstractions.Scene
{
public interface ISceneBehavior { }
}
namespace TestApp
{
[AutoScene("Gameplay")]
public partial class GameplayRoot : Node2D
{
private ISceneBehavior? {|#0:__autoSceneBehavior_Generated|};
}
}
""";
var test = new CSharpSourceGeneratorTest<AutoSceneGenerator, DefaultVerifier>
{
TestState =
{
Sources = { source }
},
DisabledDiagnostics = { "GF_Common_Trace_001" },
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
};
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_Common_Class_002", DiagnosticSeverity.Error)
.WithLocation(0)
.WithArguments("GameplayRoot", "__autoSceneBehavior_Generated"));
await test.RunAsync();
}
} }

View File

@ -199,4 +199,251 @@ public class AutoRegisterExportedCollectionsGeneratorTests
source, source,
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected)); ("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
} }
[Test]
public async Task Reports_Diagnostic_When_Collection_Member_Is_Not_Instance_Readable()
{
const string source = """
using System;
using System.Collections.Generic;
using GFramework.Godot.SourceGenerators.Abstractions;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class RegisterExportedCollectionAttribute : Attribute
{
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
}
}
namespace TestApp
{
public sealed class IntRegistry
{
public void Register(int value) { }
}
[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
private readonly IntRegistry _registry = new();
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
public static List<int> {|#0:StaticValues|} = new();
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
public static List<int> {|#1:StaticPropertyValues|} { get; } = new();
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
public List<int> {|#2:WriteOnlyValues|} { set { } }
}
}
""";
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
{
TestState =
{
Sources = { source }
},
DisabledDiagnostics = { "GF_Common_Trace_001" },
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
};
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
.WithLocation(0)
.WithArguments("StaticValues"));
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
.WithLocation(1)
.WithArguments("StaticPropertyValues"));
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_006", DiagnosticSeverity.Error)
.WithLocation(2)
.WithArguments("WriteOnlyValues"));
await test.RunAsync();
}
[Test]
public async Task Reports_Diagnostic_When_Registry_Member_Is_Not_Instance_Readable()
{
const string source = """
using System;
using System.Collections.Generic;
using GFramework.Godot.SourceGenerators.Abstractions;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class RegisterExportedCollectionAttribute : Attribute
{
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
}
}
namespace TestApp
{
public sealed class IntRegistry
{
public void Register(int value) { }
}
[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
private static readonly IntRegistry {|#0:_registry|} = new();
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
public List<int> Values { get; } = new();
}
}
""";
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
{
TestState =
{
Sources = { source }
},
DisabledDiagnostics = { "GF_Common_Trace_001" },
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
};
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_007", DiagnosticSeverity.Error)
.WithLocation(0)
.WithArguments("_registry", "Values"));
await test.RunAsync();
}
[Test]
public async Task Reports_Diagnostic_When_Register_Method_Is_Not_Accessible_From_Owner_Type()
{
const string source = """
using System;
using System.Collections.Generic;
using GFramework.Godot.SourceGenerators.Abstractions;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class RegisterExportedCollectionAttribute : Attribute
{
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
}
}
namespace TestApp
{
public sealed class IntRegistry
{
private void Register(int value) { }
}
[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
private readonly IntRegistry _registry = new();
[RegisterExportedCollection(nameof(_registry), "Register")]
public List<int> {|#0:Values|} { get; } = new();
}
}
""";
var test = new CSharpSourceGeneratorTest<AutoRegisterExportedCollectionsGenerator, DefaultVerifier>
{
TestState =
{
Sources = { source }
},
DisabledDiagnostics = { "GF_Common_Trace_001" },
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck
};
test.ExpectedDiagnostics.Add(new DiagnosticResult("GF_AutoExport_003", DiagnosticSeverity.Error)
.WithLocation(0)
.WithArguments("Register", "_registry", "Values"));
await test.RunAsync();
}
[Test]
public async Task Generates_Only_One_Source_When_Multiple_Partial_Declarations_Are_Annotated()
{
const string source = """
#nullable enable
using System;
using System.Collections.Generic;
using GFramework.Godot.SourceGenerators.Abstractions;
namespace GFramework.Godot.SourceGenerators.Abstractions
{
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class AutoRegisterExportedCollectionsAttribute : Attribute { }
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class RegisterExportedCollectionAttribute : Attribute
{
public RegisterExportedCollectionAttribute(string registryMemberName, string registerMethodName) { }
}
}
namespace TestApp
{
public sealed class IntRegistry
{
public void Register(int value) { }
}
[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
private readonly IntRegistry? _registry = new();
}
[AutoRegisterExportedCollections]
public partial class Bootstrapper
{
[RegisterExportedCollection(nameof(_registry), nameof(IntRegistry.Register))]
public List<int>? Values { get; } = new();
}
}
""";
const string expected = """
// <auto-generated />
#nullable enable
namespace TestApp;
partial class Bootstrapper
{
private void __RegisterExportedCollections_Generated()
{
if (this.Values is not null && this._registry is not null)
{
foreach (var __generatedItem in this.Values)
{
this._registry.Register(__generatedItem);
}
}
}
}
""";
await GeneratorTest<AutoRegisterExportedCollectionsGenerator>.RunAsync(
source,
("TestApp_Bootstrapper.AutoRegisterExportedCollections.g.cs", expected));
}
} }

View File

@ -30,3 +30,5 @@
GF_AutoExport_003 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics GF_AutoExport_003 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics
GF_AutoExport_004 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics GF_AutoExport_004 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics
GF_AutoExport_005 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics GF_AutoExport_005 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics
GF_AutoExport_006 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics
GF_AutoExport_007 | GFramework.Godot.SourceGenerators.Registration | Error | AutoRegisterExportedCollectionsDiagnostics

View File

@ -19,6 +19,11 @@ public sealed class AutoSceneGenerator : IIncrementalGenerator
{ {
private const string AutoSceneAttributeMetadataName = private const string AutoSceneAttributeMetadataName =
$"{PathContests.GodotSourceGeneratorsAbstractionsPath}.AutoSceneAttribute"; $"{PathContests.GodotSourceGeneratorsAbstractionsPath}.AutoSceneAttribute";
private static readonly string[] GeneratedMemberNames =
[
"SceneKeyStr",
"__autoSceneBehavior_Generated"
];
/// <summary> /// <summary>
/// 配置 <c>AutoScene</c> 的增量生成管线。 /// 配置 <c>AutoScene</c> 的增量生成管线。
@ -94,6 +99,15 @@ public sealed class AutoSceneGenerator : IIncrementalGenerator
continue; continue;
} }
if (ReportGeneratedMemberConflicts(
context,
candidate.TypeSymbol,
candidate.ClassDeclaration.Identifier.GetLocation(),
GeneratedMemberNames))
{
continue;
}
if (!TryGetSceneKey(context, candidate.TypeSymbol, attribute, out var key)) if (!TryGetSceneKey(context, candidate.TypeSymbol, attribute, out var key))
continue; continue;
@ -273,6 +287,43 @@ public sealed class AutoSceneGenerator : IIncrementalGenerator
} }
} }
/// <summary>
/// 报告与生成器保留成员名冲突的字段或属性,避免生成代码出现重复成员编译错误。
/// </summary>
/// <param name="context">用于上报诊断的源代码生成上下文。</param>
/// <param name="typeSymbol">当前待生成的类型符号。</param>
/// <param name="fallbackLocation">冲突成员无定位信息时的后备位置。</param>
/// <param name="memberNames">需要校验的生成器保留成员名集合。</param>
/// <returns>存在任意冲突时返回 <c>true</c>。</returns>
private static bool ReportGeneratedMemberConflicts(
SourceProductionContext context,
INamedTypeSymbol typeSymbol,
Location fallbackLocation,
string[] memberNames)
{
var hasConflict = false;
foreach (var memberName in memberNames)
{
var conflict = typeSymbol.GetMembers(memberName)
.FirstOrDefault(member =>
!member.IsImplicitlyDeclared &&
member is IPropertySymbol or IFieldSymbol);
if (conflict is null)
continue;
context.ReportDiagnostic(Diagnostic.Create(
CommonDiagnostics.GeneratedMethodNameConflict,
conflict.Locations.FirstOrDefault() ?? fallbackLocation,
typeSymbol.Name,
memberName));
hasConflict = true;
}
return hasConflict;
}
private sealed class TypeCandidate private sealed class TypeCandidate
{ {
public TypeCandidate(ClassDeclarationSyntax classDeclaration, INamedTypeSymbol typeSymbol) public TypeCandidate(ClassDeclarationSyntax classDeclaration, INamedTypeSymbol typeSymbol)

View File

@ -67,4 +67,26 @@ internal static class AutoRegisterExportedCollectionsDiagnostics
Category, Category,
DiagnosticSeverity.Error, DiagnosticSeverity.Error,
true); true);
/// <summary>
/// 报告被标记为导出集合的成员不是实例可读成员,因此无法生成 <c>this.&lt;member&gt;</c> 访问代码。
/// </summary>
public static readonly DiagnosticDescriptor CollectionMemberMustBeInstanceReadable = new(
"GF_AutoExport_006",
"Exported collection member must be an instance readable member",
"Member '{0}' must be an instance field or readable non-indexer instance property to use RegisterExportedCollection",
Category,
DiagnosticSeverity.Error,
true);
/// <summary>
/// 报告注册表成员不是实例可读成员,因此生成器无法安全读取并调用注册方法。
/// </summary>
public static readonly DiagnosticDescriptor RegistryMemberMustBeInstanceReadable = new(
"GF_AutoExport_007",
"Registry member must be an instance readable member",
"Registry member '{0}' referenced by exported collection '{1}' must be an instance field or readable non-indexer instance property",
Category,
DiagnosticSeverity.Error,
true);
} }

View File

@ -12,7 +12,7 @@ namespace GFramework.Godot.SourceGenerators.Registration;
/// 该生成器会扫描标记了 <c>AutoRegisterExportedCollectionsAttribute</c> 的 <c>partial</c> 类型, /// 该生成器会扫描标记了 <c>AutoRegisterExportedCollectionsAttribute</c> 的 <c>partial</c> 类型,
/// 为其中使用 <c>RegisterExportedCollectionAttribute</c> 声明的集合成员生成集中注册方法。 /// 为其中使用 <c>RegisterExportedCollectionAttribute</c> 声明的集合成员生成集中注册方法。
/// 仅当集合可枚举、元素类型可推导、注册表成员存在且可找到兼容的实例注册方法时才会输出代码; /// 仅当集合可枚举、元素类型可推导、注册表成员存在且可找到兼容的实例注册方法时才会输出代码;
/// 否则通过 <c>GF_AutoExport_001</c> 到 <c>GF_AutoExport_005</c> 以及公共 <c>ClassMustBePartial</c> 诊断显式阻止生成。 /// 否则通过 <c>GF_AutoExport_001</c> 到 <c>GF_AutoExport_007</c> 以及公共 <c>ClassMustBePartial</c> 诊断显式阻止生成。
/// </remarks> /// </remarks>
[Generator] [Generator]
public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGenerator public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGenerator
@ -82,8 +82,11 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
if (autoRegisterAttribute is null || registerCollectionAttribute is null || enumerableType is null) if (autoRegisterAttribute is null || registerCollectionAttribute is null || enumerableType is null)
return; return;
foreach (var candidate in candidates.Where(static candidate => candidate is not null) foreach (var candidate in candidates
.Select(static candidate => candidate!)) .Where(static candidate => candidate is not null)
.Select(static candidate => candidate!)
.GroupBy(static candidate => candidate.TypeSymbol, SymbolEqualityComparer.Default)
.Select(static group => group.First()))
{ {
if (!candidate.TypeSymbol.GetAttributes().Any(attribute => if (!candidate.TypeSymbol.GetAttributes().Any(attribute =>
SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, autoRegisterAttribute))) SymbolEqualityComparer.Default.Equals(attribute.AttributeClass, autoRegisterAttribute)))
@ -187,6 +190,15 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
{ {
registration = null!; registration = null!;
if (!IsInstanceReadableMember(collectionMember))
{
context.ReportDiagnostic(Diagnostic.Create(
AutoRegisterExportedCollectionsDiagnostics.CollectionMemberMustBeInstanceReadable,
collectionMember.Locations.FirstOrDefault() ?? Location.None,
collectionMember.Name));
return false;
}
var collectionType = collectionMember switch var collectionType = collectionMember switch
{ {
IFieldSymbol field => field.Type, IFieldSymbol field => field.Type,
@ -223,6 +235,16 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
return false; return false;
} }
if (!IsInstanceReadableMember(registryMember))
{
context.ReportDiagnostic(Diagnostic.Create(
AutoRegisterExportedCollectionsDiagnostics.RegistryMemberMustBeInstanceReadable,
registryMember.Locations.FirstOrDefault() ?? Location.None,
registryMemberName,
collectionMember.Name));
return false;
}
var registryType = registryMember switch var registryType = registryMember switch
{ {
IFieldSymbol field => field.Type as INamedTypeSymbol, IFieldSymbol field => field.Type as INamedTypeSymbol,
@ -250,6 +272,7 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
.Any(method => .Any(method =>
!method.IsStatic && !method.IsStatic &&
method.Parameters.Length == 1 && method.Parameters.Length == 1 &&
compilation.IsSymbolAccessibleWithin(method, ownerType) &&
CanAcceptElementType(compilation, elementType, method.Parameters[0].Type)); CanAcceptElementType(compilation, elementType, method.Parameters[0].Type));
if (!hasCompatibleMethod) if (!hasCompatibleMethod)
@ -267,6 +290,21 @@ public sealed class AutoRegisterExportedCollectionsGenerator : IIncrementalGener
return true; return true;
} }
private static bool IsInstanceReadableMember(ISymbol member)
{
// Generated code always reads through `this.<member>`, so only instance fields and
// readable non-indexer instance properties are valid targets.
return member switch
{
IFieldSymbol field => !field.IsStatic,
IPropertySymbol property =>
!property.IsStatic &&
property.Parameters.Length == 0 &&
property.GetMethod is not null,
_ => false
};
}
private static bool CanAcceptElementType( private static bool CanAcceptElementType(
Compilation compilation, Compilation compilation,
ITypeSymbol elementType, ITypeSymbol elementType,