Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source generator warnings #444

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Obsidian.SourceGenerators/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

10 changes: 10 additions & 0 deletions Obsidian.SourceGenerators/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
DBG001 | SerializationMethodGeneration | Warning | DiagnosticDescriptors
DBG002 | SerializationMethodGeneration | Warning | DiagnosticDescriptors
DBG003 | SerializationMethodGeneration | Warning | DiagnosticDescriptors
9 changes: 5 additions & 4 deletions Obsidian.SourceGenerators/Extensions.CodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Obsidian.SourceGenerators.Registry;
using Obsidian.SourceGenerators.Registry.Models;
using System.Globalization;
using System.Text.Json;

namespace Obsidian.SourceGenerators;
Expand Down Expand Up @@ -102,9 +103,9 @@ internal static void AppendValueType(this CodeBuilder builder, JsonElement eleme
else if (element.TryGetInt64(out var longValue))
builder.Append($"{longValue} }},");
else if (element.TryGetSingle(out var floatValue))
builder.Append($"{floatValue}f }},");
builder.Append($"{floatValue.ToString(CultureInfo.InvariantCulture)}f }},");
else if (element.TryGetDouble(out var doubleValue))
builder.Append($"{doubleValue}d }},");
builder.Append($"{doubleValue.ToString(CultureInfo.InvariantCulture)}d }},");
break;
}
case JsonValueKind.True:
Expand Down Expand Up @@ -133,9 +134,9 @@ internal static void AppendValueType(this CodeBuilder builder, JsonElement eleme
else if (element.TryGetInt64(out var longValue))
builder.Append($"{longValue}, ");
else if (element.TryGetSingle(out var floatValue))
builder.Append($"{floatValue}f, ");
builder.Append($"{floatValue.ToString(CultureInfo.InvariantCulture)}f, ");
else if (element.TryGetDouble(out var doubleValue))
builder.Append($"{doubleValue}d, ");
builder.Append($"{doubleValue.ToString(CultureInfo.InvariantCulture)}d, ");
break;
}
case JsonValueKind.True:
Expand Down
6 changes: 6 additions & 0 deletions Obsidian.SourceGenerators/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ internal static string CompileName(this Tag tag, bool last = false)

return tag.Parent == tag.Type ? tag.Name : $"{tag.Type.ToPascalCase()}.{tag.Name}";
}

public static void Deconstruct<TKey, TValue>(this IGrouping<TKey, TValue> grouping, out TKey key, out List<TValue> values)
{
key = grouping.Key;
values = grouping.ToList();
}
}
1 change: 1 addition & 0 deletions Obsidian.SourceGenerators/Obsidian.SourceGenerators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
<IsRoslynComponent>true</IsRoslynComponent>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
Expand Down
24 changes: 20 additions & 4 deletions Obsidian.SourceGenerators/Packets/AttributeOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Obsidian.SourceGenerators.Packets;

internal abstract class AttributeOwner
internal abstract class AttributeOwner(AttributeFlags flags, AttributeBehaviorBase[] attributes)
{
public AttributeFlags Flags { get; set; }
public AttributeBehaviorBase[] Attributes { get; set; }
public AttributeFlags Flags { get; } = flags;
public AttributeBehaviorBase[] Attributes { get; } = attributes;

public bool TryGetAttribute<TAttribute>(out TAttribute attribute) where TAttribute : AttributeBehaviorBase
public bool TryGetAttribute<TAttribute>(out TAttribute? attribute) where TAttribute : AttributeBehaviorBase
{
for (int i = 0; i < Attributes.Length; i++)
{
Expand All @@ -21,4 +21,20 @@ public bool TryGetAttribute<TAttribute>(out TAttribute attribute) where TAttribu
attribute = default;
return false;
}

protected static AttributeFlags AggregateFlags(AttributeBehaviorBase[] attributes)
{
var flags = AttributeFlags.None;
foreach (AttributeBehaviorBase attribute in attributes)
{
flags |= attribute.Flag;
}
return flags;
}

protected static string GetRelativeTypeName(string typeName)
{
int dotIndex = typeName.LastIndexOf('.');
return dotIndex >= 0 ? typeName.Substring(dotIndex + 1) : typeName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,13 @@

namespace Obsidian.SourceGenerators.Packets.Attributes;

internal abstract class AttributeBehaviorBase
internal abstract class AttributeBehaviorBase(AttributeSyntax attributeSyntax)
{
public abstract string Name { get; }
public abstract AttributeFlags Flag { get; }

protected readonly AttributeSyntax syntax;
private readonly AttributeArgumentSyntax[] arguments;

public AttributeBehaviorBase(AttributeSyntax attributeSyntax)
{
syntax = attributeSyntax;
arguments = attributeSyntax?.ArgumentList?.Arguments.Select(arg => arg).ToArray() ?? [];
}
protected readonly AttributeSyntax syntax = attributeSyntax;
private readonly AttributeArgumentSyntax[] arguments = attributeSyntax?.ArgumentList?.Arguments.Select(arg => arg).ToArray() ?? [];

public virtual bool Matches(AttributeOwner other)
{
Expand Down Expand Up @@ -60,7 +54,7 @@ protected bool TryGetArgument<TSyntax>(out TSyntax syntax) where TSyntax : Expre
}
}

syntax = null;
syntax = null!;
return false;
}

Expand All @@ -79,21 +73,23 @@ protected bool TryEvaluateIntArgument(out int argument)

protected bool TryEvaluateTypeArgument(out string argument)
{
argument = null;

if (!TryGetArgument(out TypeSyntax expression))
{
argument = null!;
return false;
}

argument = expression.GetText().ToString();
return true;
}

protected bool TryEvaluateStringArgument(out string argument)
{
argument = null;

if (!TryGetArgument(out ExpressionSyntax expression))
{
argument = null!;
return false;
}

return TryEvaluateString(expression, out argument);
}
Expand All @@ -112,13 +108,13 @@ InvocationExpressionSyntax invocation when invocation.Expression.GetText().ToStr

// "A" + "B"
BinaryExpressionSyntax binaryAdd when binaryAdd.Kind() == SyntaxKind.AddExpression
=> TryEvaluateString(binaryAdd.Left, out var left) && TryEvaluateString(binaryAdd.Right, out var right) ? left + right : null,
=> TryEvaluateString(binaryAdd.Left, out var left) && TryEvaluateString(binaryAdd.Right, out var right) ? left + right : null!,

// $"A {B} C"
InterpolatedStringExpressionSyntax interpolation
=> TryEvaluateInterpolatedString(interpolation, out var interpolatedText) ? interpolatedText : null,
=> TryEvaluateInterpolatedString(interpolation, out var interpolatedText) ? interpolatedText : null!,

_ => null
_ => null!
};

return result is not null;
Expand All @@ -142,7 +138,7 @@ private static bool TryEvaluateInterpolatedString(InterpolatedStringExpressionSy
}
else
{
result = null;
result = null!;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static bool TryParse(AttributeSyntax attribute, out AttributeBehaviorBase
if (attributeName.EndsWith("Attribute"))
attributeName = attributeName.Substring(0, attributeName.Length - 9);

attributeBehaviorBase = methods.TryGetValue(attributeName, out FactoryMethod factoryMethod) ? factoryMethod(attribute) : null;
attributeBehaviorBase = methods.TryGetValue(attributeName, out FactoryMethod factoryMethod) ? factoryMethod(attribute) : null!;

return attributeBehaviorBase is not null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ private Property GetEndProperty(MethodBuildingContext context)
{
var sharedCondition = context.AllProperties
.SkipWhile(prop => prop != context.Property)
.Select(prop => new { Property = prop, Attribute = prop.TryGetAttribute(out ConditionBehavior condition) ? condition : null })
.Select(prop => new { Property = prop, Attribute = prop.TryGetAttribute(out ConditionBehavior? condition) ? condition : null })
.TakeWhile(entry => entry.Attribute?.Condition == Condition);

foreach (var shared in sharedCondition.Skip(1))
{
shared.Attribute.Skip = true;
shared.Attribute!.Skip = true;
}

return sharedCondition.Last().Property;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext
}

string getLength = $"{context.StreamName}.{readMethod}()";
context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(getLength)};");
context.CodeBuilder.Line($"{context.DataName} = {context.Property.GetNewCollectionExpression(getLength)};");
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public DataFormatBehavior(AttributeSyntax attributeSyntax) : base(attributeSynta
public override bool Matches(AttributeOwner other)
{
return other.Flags.HasFlag(Flag) &&
other.TryGetAttribute(out DataFormatBehavior format) &&
format.Type == Type;
other.TryGetAttribute(out DataFormatBehavior? format) &&
format!.Type == Type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override bool ModifyCollectionPrefixDeserialization(MethodBuildingContext
return false;
}

context.CodeBuilder.Line($"{context.DataName} = {context.Property.NewCollection(Length.ToString())}");
context.CodeBuilder.Line($"{context.DataName} = {context.Property.GetNewCollectionExpression(Length.ToString())}");
return true;
}
}
25 changes: 4 additions & 21 deletions Obsidian.SourceGenerators/Packets/Method.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,11 @@

namespace Obsidian.SourceGenerators.Packets;

internal sealed class Method : AttributeOwner
internal sealed class Method(string name, string type, AttributeBehaviorBase[] attributes)
: AttributeOwner(AggregateFlags(attributes) | AttributeFlags.Field, attributes)
{
public string Name { get; }
public string Type { get; }

public Method(string name, string type, AttributeBehaviorBase[] attributes)
{
Name = name;
Type = type;
Attributes = attributes;

if (Type.Contains('.'))
{
Type = Type.Substring(Type.LastIndexOf('.') + 1);
}

Flags = AttributeFlags.Field;
for (int i = 0; i < attributes.Length; i++)
{
Flags |= attributes[i].Flag;
}
}
public string Name { get; } = name;
public string Type { get; } = GetRelativeTypeName(type);

public override string ToString()
{
Expand Down
8 changes: 4 additions & 4 deletions Obsidian.SourceGenerators/Packets/MethodsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public bool TryGetReadMethod(Property property, bool collection, out Method meth

private bool TryGetMethod(IEnumerable<Method> source, Property property, bool collection, out Method outMethod)
{
string propertyType = collection ? property.CollectionType : property.Type;
string? propertyType = collection ? property.CollectionType : property.Type;
foreach (Method method in source)
{
if (method.Type != propertyType)
Expand All @@ -48,7 +48,7 @@ private bool TryGetMethod(IEnumerable<Method> source, Property property, bool co
return true;
}

outMethod = null;
outMethod = null!;
return false;
}

Expand Down Expand Up @@ -90,7 +90,7 @@ public void Offer(GeneratorExecutionContext context, MethodDeclarationSyntax met

private bool TryGetWriteMethodType(GeneratorExecutionContext context, MethodDeclarationSyntax method, out string type)
{
type = null;
type = null!;

var parameters = method.ParameterList.Parameters;
if (parameters.Count == 0)
Expand All @@ -111,7 +111,7 @@ private bool TryGetWriteMethodType(GeneratorExecutionContext context, MethodDecl
return false;
}

type = parameter.Type.ToString();
type = parameter.Type!.ToString();
return true;
}

Expand Down
Loading
Loading