From adffc4fd68a84d3b4403dd1a60fdb7d0233554a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20C=C3=A1ceres?= Date: Mon, 18 Nov 2024 02:53:26 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=96=EF=B8=8F=20Make=20sure=20pre-packed?= =?UTF-8?q?=20values=20are=20added=20in=20comments=20to=20generated=20sour?= =?UTF-8?q?ces=20(#1176)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Lynx.Generator/GeneratedPackGenerator.cs | 48 ++++++++++--------- .../EvaluationParams.g.cs | 18 +++---- ...ayTest.StaticClass#TestClass.g.verified.cs | 1 + ...est.NonStaticClass#TestClass.g.verified.cs | 2 +- ...taticReadonlyField#TestClass.g.verified.cs | 2 +- ...ckTest.StaticClass#TestClass.g.verified.cs | 2 +- 6 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 tests/Lynx.Generator.Test/Snapshots/GeneratedPackArrayTest.StaticClass#TestClass.g.verified.cs diff --git a/src/Lynx.Generator/GeneratedPackGenerator.cs b/src/Lynx.Generator/GeneratedPackGenerator.cs index 90db30e7f..13a059bf0 100644 --- a/src/Lynx.Generator/GeneratedPackGenerator.cs +++ b/src/Lynx.Generator/GeneratedPackGenerator.cs @@ -54,9 +54,9 @@ private static bool IsGeneratedPackAttribute(SyntaxNode syntaxNode) => private static Model? GetModel(GeneratorAttributeSyntaxContext context) { - var value = ExtractPackedValueFromAttribute(context.TargetNode, context.SemanticModel); + var (mg, eg) = ExtractPackedValueFromAttribute(context.TargetNode, context.SemanticModel); - if (value != Model.DefaultValue) + if (mg != Model.DefaultShortValue && eg != Model.DefaultShortValue) { var classSymbol = context.TargetSymbol.ContainingSymbol; @@ -64,7 +64,8 @@ private static bool IsGeneratedPackAttribute(SyntaxNode syntaxNode) => Namespace: classSymbol.ContainingNamespace?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted)) ?? string.Empty, ClassName: classSymbol.Name, PropertyName: context.TargetSymbol.Name[1..], - PackedValue: value); + MG: mg, + EG: eg); } return null; @@ -73,22 +74,22 @@ private static bool IsGeneratedPackAttribute(SyntaxNode syntaxNode) => /// Heavily inspired by https://andrewlock.net/creating-a-source-generator-part-4-customising-generated-code-with-marker-attributes/ /// Code in https://github.com/andrewlock/blog-examples/tree/c35edf1c1f0e1f9adf84c215e2ce7ab644b374f5/NetEscapades.EnumGenerators4 /// - static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, SemanticModel semanticModel) + static (short MG, short EG) ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, SemanticModel semanticModel) { - const short DefaultShortValue = 32_323; + (short, short)defaultResult = (Model.DefaultShortValue, Model.DefaultShortValue); - short mg = DefaultShortValue; - short eg = DefaultShortValue; + short mg = Model.DefaultShortValue; + short eg = Model.DefaultShortValue; if (semanticModel.GetDeclaredSymbol(variableDeclarationSyntax) is not IFieldSymbol variableSymbol) { - return Model.DefaultValue; + return defaultResult; } INamedTypeSymbol? ourAttribute = semanticModel.Compilation.GetTypeByMetadataName(OurAttribute); if (ourAttribute is null) { - return Model.DefaultValue; + return defaultResult; } // Loop through all of the attributes on the enum until we find the [EnumExtensions] attribute @@ -103,7 +104,7 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, // More than one of our attributes -> invalid for our use case if(group.Count() > 1) { - return Model.DefaultValue; + return defaultResult; } AttributeData attributeData = group.First(); @@ -118,14 +119,14 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, { if (arg.Kind == TypedConstantKind.Error) { - return Model.DefaultValue; + return defaultResult; } } // Make sure both arguments are present if (args.Length != 2 || args[0].IsNull || args[1].IsNull) { - return Model.DefaultValue; + return defaultResult; } mg = (short)args[0].Value!; @@ -142,7 +143,7 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, // Make sure we don't have any errors, otherwise don't do generation if (typedConstant.Kind == TypedConstantKind.Error) { - return Model.DefaultValue; + return defaultResult; } else { @@ -151,12 +152,12 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, { case "mg": mg = typedConstant.IsNull - ? DefaultShortValue + ? Model.DefaultShortValue : (short)typedConstant.Value!; break; case "eg": eg = typedConstant.IsNull - ? DefaultShortValue + ? Model.DefaultShortValue : (short)typedConstant.Value!; break; } @@ -167,12 +168,12 @@ static int ExtractPackedValueFromAttribute(SyntaxNode variableDeclarationSyntax, break; } - if (mg == DefaultShortValue || eg == DefaultShortValue) + if (mg == Model.DefaultShortValue || eg == Model.DefaultShortValue) { - return Model.DefaultValue; + return defaultResult; } - return Utils.Pack(mg, eg); + return (mg, eg); } } @@ -203,9 +204,10 @@ private static void Execute(SourceProductionContext context, ImmutableArray