Skip to content

Commit

Permalink
Add ObsoleteAttribute to extension methods of obsolete methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpl0itR committed Jul 9, 2024
1 parent c6fde9e commit 7f2dced
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions GroupedNativeMethodsGenerator/GroupedNativeMethodsGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -117,11 +118,16 @@ static void Emit(SourceProductionContext context, GeneratorAttributeSyntaxContex
var pointedType = ((IPointerTypeSymbol)firstArgument.Type).PointedAtType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
var parameterPairs = string.Join("", item.Parameters.Skip(1).Select(x => $", {x.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)} @{x.Name}"));
var parameterNames = string.Join("", item.Parameters.Skip(1).Select(x => $", @{x.Name}"));
var obsoleteAttribute = item.GetAttributes().SingleOrDefault(x => x.AttributeClass?.Name == nameof(ObsoleteAttribute));

if (summaryComment != null)
{
code.AppendLine(" " + summaryComment);
}
if (obsoleteAttribute != null)
{
code.AppendLine(" " + ObsoleteAttributeToString(obsoleteAttribute));
}
code.AppendLine(" [MethodImpl(MethodImplOptions.AggressiveInlining)]");
code.AppendLine($" public static {ret} {convertedMethodName}(this ref {pointedType} @{firstArgument.Name}{parameterPairs})");
code.AppendLine(" {");
Expand Down Expand Up @@ -177,6 +183,19 @@ static string ConvertMethodName(string typeName, string methodName, string remov
return ToCamelCase(methodName);
}

static string ObsoleteAttributeToString(AttributeData obsoleteAttribute)
{
if (obsoleteAttribute.ConstructorArguments.IsEmpty && obsoleteAttribute.NamedArguments.IsEmpty)
{
return "[Obsolete]";
}

var ctorArgs = obsoleteAttribute.ConstructorArguments.Select(x => x.ToCSharpString());
var namedArgs = obsoleteAttribute.NamedArguments.Select(x => $"{x.Key} = {x.Value.ToCSharpString()}");

return $"[Obsolete({string.Join(", ", ctorArgs.Concat(namedArgs))})]";
}

static bool TryTrimPrefix(string value, string prefix, out string result)
{
var match = value.IndexOf(prefix, StringComparison.Ordinal);
Expand Down

0 comments on commit 7f2dced

Please sign in to comment.