Support custom deprecation messages in bindings generator

This commit is contained in:
2024-06-17 20:00:30 +03:00
parent 6e0dd2064a
commit e08b57e814
21 changed files with 98 additions and 43 deletions

View File

@@ -21,7 +21,7 @@ namespace Flax.Build.Bindings
public string Attributes;
public string[] Comment;
public bool IsInBuild;
public bool IsDeprecated;
public string DeprecatedMessage;
public TypeInfo MarshalAs;
internal bool IsInited;
internal TypedefInfo Instigator;
@@ -34,6 +34,7 @@ namespace Flax.Build.Bindings
public virtual bool IsScriptingObject => false;
public virtual bool IsPod => false;
public virtual bool SkipGeneration => IsInBuild;
public virtual bool IsDeprecated => DeprecatedMessage != null;
public FileInfo File
{
@@ -154,7 +155,7 @@ namespace Flax.Build.Bindings
BindingsGenerator.Write(writer, Comment);
BindingsGenerator.Write(writer, MarshalAs);
writer.Write(IsInBuild);
writer.Write(IsDeprecated);
BindingsGenerator.Write(writer, DeprecatedMessage);
BindingsGenerator.Write(writer, Tags);
BindingsGenerator.Write(writer, Children);
}
@@ -168,7 +169,7 @@ namespace Flax.Build.Bindings
Comment = BindingsGenerator.Read(reader, Comment);
MarshalAs = BindingsGenerator.Read(reader, MarshalAs);
IsInBuild = reader.ReadBoolean();
IsDeprecated = reader.ReadBoolean();
DeprecatedMessage = BindingsGenerator.Read(reader, DeprecatedMessage);
Tags = BindingsGenerator.Read(reader, Tags);
Children = BindingsGenerator.Read(reader, Children);

View File

@@ -829,7 +829,7 @@ namespace Flax.Build.Bindings
}
}
private static void GenerateCSharpAttributes(BuildData buildData, StringBuilder contents, string indent, ApiTypeInfo apiTypeInfo, string attributes = null, string[] comment = null, bool canUseTooltip = false, bool useUnmanaged = false, string defaultValue = null, bool isDeprecated = false, TypeInfo defaultValueType = null)
private static void GenerateCSharpAttributes(BuildData buildData, StringBuilder contents, string indent, ApiTypeInfo apiTypeInfo, string attributes = null, string[] comment = null, bool canUseTooltip = false, bool useUnmanaged = false, string defaultValue = null, string deprecatedMessage = null, TypeInfo defaultValueType = null)
{
#if AUTO_DOC_TOOLTIPS
var writeTooltip = true;
@@ -853,10 +853,15 @@ namespace Flax.Build.Bindings
// Skip boilerplate code when using debugger
//contents.Append(indent).AppendLine("[System.Diagnostics.DebuggerStepThrough]");
}
if (isDeprecated || apiTypeInfo.IsDeprecated)
if (deprecatedMessage != null || apiTypeInfo.IsDeprecated)
{
// Deprecated type
contents.Append(indent).AppendLine("[Obsolete]");
if (!string.IsNullOrEmpty(apiTypeInfo.DeprecatedMessage))
contents.Append(indent).AppendLine($"[Obsolete(\"{apiTypeInfo.DeprecatedMessage}\")]");
else if (!string.IsNullOrEmpty(deprecatedMessage))
contents.Append(indent).AppendLine($"[Obsolete(\"{deprecatedMessage}\")]");
else
contents.Append(indent).AppendLine("[Obsolete]");
}
#if AUTO_DOC_TOOLTIPS
@@ -901,12 +906,12 @@ namespace Flax.Build.Bindings
private static void GenerateCSharpAttributes(BuildData buildData, StringBuilder contents, string indent, ApiTypeInfo apiTypeInfo, bool useUnmanaged, string defaultValue = null, TypeInfo defaultValueType = null)
{
GenerateCSharpAttributes(buildData, contents, indent, apiTypeInfo, apiTypeInfo.Attributes, apiTypeInfo.Comment, true, useUnmanaged, defaultValue, false, defaultValueType);
GenerateCSharpAttributes(buildData, contents, indent, apiTypeInfo, apiTypeInfo.Attributes, apiTypeInfo.Comment, true, useUnmanaged, defaultValue, null, defaultValueType);
}
private static void GenerateCSharpAttributes(BuildData buildData, StringBuilder contents, string indent, ApiTypeInfo apiTypeInfo, MemberInfo memberInfo, bool useUnmanaged, string defaultValue = null, TypeInfo defaultValueType = null)
{
GenerateCSharpAttributes(buildData, contents, indent, apiTypeInfo, memberInfo.Attributes, memberInfo.Comment, true, useUnmanaged, defaultValue, memberInfo.IsDeprecated, defaultValueType);
GenerateCSharpAttributes(buildData, contents, indent, apiTypeInfo, memberInfo.Attributes, memberInfo.Comment, true, useUnmanaged, defaultValue, memberInfo.DeprecatedMessage, defaultValueType);
}
private static bool GenerateCSharpStructureUseDefaultInitialize(BuildData buildData, StructureInfo structureInfo)

View File

@@ -19,7 +19,7 @@ namespace Flax.Build.Bindings
partial class BindingsGenerator
{
private static readonly Dictionary<string, Type> TypeCache = new Dictionary<string, Type>();
private const int CacheVersion = 22;
private const int CacheVersion = 23;
internal static void Write(BinaryWriter writer, string e)
{

View File

@@ -589,7 +589,13 @@ namespace Flax.Build.Bindings
token = context.Tokenizer.NextToken();
if (!desc.IsDeprecated && token.Value == "DEPRECATED")
{
desc.IsDeprecated = true;
token = context.Tokenizer.NextToken();
string message = "";
if (token.Type == TokenType.LeftParent)
context.Tokenizer.SkipUntil(TokenType.RightParent, out message);
else
context.Tokenizer.PreviousToken();
desc.DeprecatedMessage = message.Trim('"');
}
else
{
@@ -698,7 +704,13 @@ namespace Flax.Build.Bindings
token = context.Tokenizer.NextToken();
if (!desc.IsDeprecated && token.Value == "DEPRECATED")
{
desc.IsDeprecated = true;
token = context.Tokenizer.NextToken();
string message = "";
if (token.Type == TokenType.LeftParent)
context.Tokenizer.SkipUntil(TokenType.RightParent, out message);
else
context.Tokenizer.PreviousToken();
desc.DeprecatedMessage = message.Trim('"');
}
else
{
@@ -797,8 +809,14 @@ namespace Flax.Build.Bindings
}
else if (!desc.IsDeprecated && token.Value == "DEPRECATED")
{
desc.IsDeprecated = true;
context.Tokenizer.NextToken();
token = context.Tokenizer.NextToken();
string message = "";
if (token.Type == TokenType.LeftParent)
{
context.Tokenizer.SkipUntil(TokenType.RightParent, out message);
context.Tokenizer.NextToken();
}
desc.DeprecatedMessage = message.Trim('"');
}
else
{
@@ -960,7 +978,7 @@ namespace Flax.Build.Bindings
propertyInfo.Getter = functionInfo;
else
propertyInfo.Setter = functionInfo;
propertyInfo.IsDeprecated |= functionInfo.IsDeprecated;
propertyInfo.DeprecatedMessage = functionInfo.DeprecatedMessage;
propertyInfo.IsHidden |= functionInfo.IsHidden;
if (propertyInfo.Getter != null && propertyInfo.Setter != null)
@@ -1025,7 +1043,13 @@ namespace Flax.Build.Bindings
token = context.Tokenizer.NextToken();
if (!desc.IsDeprecated && token.Value == "DEPRECATED")
{
desc.IsDeprecated = true;
token = context.Tokenizer.NextToken();
string message = "";
if (token.Type == TokenType.LeftParent)
context.Tokenizer.SkipUntil(TokenType.RightParent, out message);
else
context.Tokenizer.PreviousToken();
desc.DeprecatedMessage = message.Trim('"');
}
else
{
@@ -1300,8 +1324,14 @@ namespace Flax.Build.Bindings
}
else if (!desc.IsDeprecated && token.Value == "DEPRECATED")
{
desc.IsDeprecated = true;
context.Tokenizer.NextToken();
token = context.Tokenizer.NextToken();
string message = "";
if (token.Type == TokenType.LeftParent)
{
context.Tokenizer.SkipUntil(TokenType.RightParent, out message);
context.Tokenizer.NextToken();
}
desc.DeprecatedMessage = message.Trim('"');
}
else
{

View File

@@ -14,12 +14,14 @@ namespace Flax.Build.Bindings
public string[] Comment;
public bool IsStatic;
public bool IsConstexpr;
public bool IsDeprecated;
public string DeprecatedMessage;
public bool IsHidden;
public AccessLevel Access;
public string Attributes;
public Dictionary<string, string> Tags;
public virtual bool IsDeprecated => DeprecatedMessage != null;
public bool HasAttribute(string name)
{
return Attributes != null && Attributes.Contains(name);
@@ -46,7 +48,7 @@ namespace Flax.Build.Bindings
BindingsGenerator.Write(writer, Comment);
writer.Write(IsStatic);
writer.Write(IsConstexpr);
writer.Write(IsDeprecated);
BindingsGenerator.Write(writer, DeprecatedMessage);
writer.Write(IsHidden);
writer.Write((byte)Access);
BindingsGenerator.Write(writer, Attributes);
@@ -59,7 +61,7 @@ namespace Flax.Build.Bindings
Comment = BindingsGenerator.Read(reader, Comment);
IsStatic = reader.ReadBoolean();
IsConstexpr = reader.ReadBoolean();
IsDeprecated = reader.ReadBoolean();
DeprecatedMessage = BindingsGenerator.Read(reader, DeprecatedMessage);
IsHidden = reader.ReadBoolean();
Access = (AccessLevel)reader.ReadByte();
Attributes = BindingsGenerator.Read(reader, Attributes);

View File

@@ -76,7 +76,7 @@ namespace Flax.Build.Bindings
if (Comment != null && Comment.Length != 0)
typedef.Comment = Comment;
typedef.IsInBuild |= IsInBuild;
typedef.IsDeprecated |= IsDeprecated;
typedef.DeprecatedMessage = DeprecatedMessage;
if (typedef is ClassStructInfo typedefClassStruct && typedefClassStruct.IsTemplate)
{
// Inflate template type