Skip to content

Commit

Permalink
Upgraded AnalysisMode to Recommended, code quality improved, code imp…
Browse files Browse the repository at this point in the history
…erfections are more tolerated.
  • Loading branch information
RyanLamansky committed Nov 24, 2024
1 parent 87614b6 commit 15baea6
Show file tree
Hide file tree
Showing 23 changed files with 125 additions and 107 deletions.
19 changes: 14 additions & 5 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
[*.cs]

csharp_prefer_braces = when_multiline
csharp_style_namespace_declarations=file_scoped:warning
dotnet_analyzer_diagnostic.severity = suggestion

csharp_prefer_braces = when_multiline:suggestion
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_prefer_pattern_matching = true:suggestion
csharp_style_namespace_declarations = file_scoped
csharp_style_var_when_type_is_apparent = true
csharp_style_var_for_built_in_types = true
csharp_style_var_elsewhere = true

# IDE0040: Add accessibility modifiers
dotnet_diagnostic.IDE0040.severity = none
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion
dotnet_style_require_accessibility_modifiers = always:suggestion
4 changes: 4 additions & 0 deletions WebAssembly/CustomSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ internal void WriteTo(Writer writer)
{
writer.Write(this.Name);
if (this.content is byte[] bytes)
{
writer.Write(bytes);
}
else
{
foreach (var b in this.Content)
writer.Write(b);
}
}
}
8 changes: 6 additions & 2 deletions WebAssembly/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Data
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<Instruction> InitializerExpression
{
get => this.initializerExpression ??= new List<Instruction>();
get => this.initializerExpression ??= [];
set => this.initializerExpression = value ?? throw new ArgumentNullException(nameof(value));
}

Expand All @@ -37,7 +37,7 @@ public IList<Instruction> InitializerExpression
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<byte> RawData
{
get => this.rawData ??= new List<byte>();
get => this.rawData ??= [];
set => this.rawData = value ?? throw new ArgumentNullException(nameof(value));
}

Expand Down Expand Up @@ -69,9 +69,13 @@ internal void WriteTo(Writer writer)

writer.WriteVar((uint)this.RawData.Count);
if (this.RawData is byte[] bytes)
{
writer.Write(bytes);
}
else
{
foreach (var b in this.RawData)
writer.Write(b);
}
}
}
6 changes: 3 additions & 3 deletions WebAssembly/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Element
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<Instruction> InitializerExpression
{
get => this.initializerExpression ??= new List<Instruction>();
get => this.initializerExpression ??= [];
set => this.initializerExpression = value ?? throw new ArgumentNullException(nameof(value));
}

Expand All @@ -38,7 +38,7 @@ public IList<Instruction> InitializerExpression
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<uint> Elements
{
get => this.elements ??= new List<uint>();
get => this.elements ??= [];
set => this.elements = value ?? throw new ArgumentNullException(nameof(value));
}

Expand Down Expand Up @@ -80,7 +80,7 @@ internal Element(Reader reader)
this.initializerExpression = Instruction.ParseInitializerExpression(reader).ToList();

var count = checked((int)reader.ReadVarUInt32());
var elements = this.elements = new List<uint>();
var elements = this.elements = [];

for (var i = 0; i < count; i++)
elements.Add(reader.ReadVarUInt32());
Expand Down
4 changes: 2 additions & 2 deletions WebAssembly/FunctionBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class FunctionBody : IEquatable<FunctionBody>
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<Local> Locals
{
get => this.locals ??= new List<Local>();
get => this.locals ??= [];
set => this.locals = value ?? throw new ArgumentNullException(nameof(value));
}

Expand All @@ -33,7 +33,7 @@ public IList<Local> Locals
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<Instruction> Code
{
get => this.code ??= new List<Instruction>();
get => this.code ??= [];
set => this.code = value ?? throw new ArgumentNullException(nameof(value));
}

Expand Down
2 changes: 1 addition & 1 deletion WebAssembly/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Global
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<Instruction> InitializerExpression
{
get => this.initializerExpression ??= new List<Instruction>();
get => this.initializerExpression ??= [];
set => this.initializerExpression = value ?? throw new ArgumentNullException(nameof(value));
}

Expand Down
4 changes: 2 additions & 2 deletions WebAssembly/HashCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace WebAssembly;

static class HashCode
internal static class HashCode
{
public static int Combine(this System.Collections.Generic.IEnumerable<int?> hashCodes)
=> Combine(hashCodes.Select(code => code.GetValueOrDefault()));
Expand All @@ -26,7 +26,7 @@ public static int Combine(this System.Collections.Generic.IEnumerable<int> hashC
return hash1 + (hash2 * 1566083941);
}

public static int Combine(int h1, int h2) => (((h1 << 5) + h1) ^ h2);
public static int Combine(int h1, int h2) => ((h1 << 5) + h1) ^ h2;

public static int Combine(int h1, int h2, int h3) => Combine(Combine(h1, h2), h3);
}
2 changes: 1 addition & 1 deletion WebAssembly/Instructions/BranchTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BranchTable : Instruction, IEquatable<BranchTable>
/// <exception cref="ArgumentNullException">Value cannot be set to null.</exception>
public IList<uint> Labels
{
get => this.labels ??= new List<uint>();
get => this.labels ??= [];
set => this.labels = value ?? throw new ArgumentNullException(nameof(value));
}

Expand Down
2 changes: 1 addition & 1 deletion WebAssembly/Instructions/LocalGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal LocalGet(Reader reader)
internal sealed override void Compile(CompilationContext context)
{
if (this.Index >= context.CheckedLocals.Length)
throw new System.IndexOutOfRangeException($"Attempt to get local at index {this.Index} but only {context.CheckedLocals.Length} {(context.CheckedLocals.Length == 1 ? "local was" : "locals were")} defined.");
throw new System.InvalidOperationException($"Attempt to get local at index {this.Index} but only {context.CheckedLocals.Length} {(context.CheckedLocals.Length == 1 ? "local was" : "locals were")} defined.");
context.Stack.Push(context.CheckedLocals[this.Index]);

var localIndex = this.Index - context.CheckedSignature.ParameterTypes.Length;
Expand Down
2 changes: 1 addition & 1 deletion WebAssembly/Instructions/LocalSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal LocalSet(Reader reader)
internal sealed override void Compile(CompilationContext context)
{
if (this.Index >= context.CheckedLocals.Length)
throw new System.IndexOutOfRangeException($"Attempt to set local at index {this.Index} but only {context.CheckedLocals.Length} {(context.CheckedLocals.Length == 1 ? "local was" : "locals were")} defined.");
throw new System.InvalidOperationException($"Attempt to set local at index {this.Index} but only {context.CheckedLocals.Length} {(context.CheckedLocals.Length == 1 ? "local was" : "locals were")} defined.");
context.PopStackNoReturn(OpCode.LocalSet, context.CheckedLocals[this.Index]);

var localIndex = this.Index - context.CheckedSignature.ParameterTypes.Length;
Expand Down
17 changes: 7 additions & 10 deletions WebAssembly/Instructions/MemoryImmediateInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,14 @@ public bool Equals(MemoryImmediateInstruction? other) =>

private protected abstract System.Reflection.Emit.OpCode EmittedOpCode { get; }

private protected HelperMethod RangeCheckHelper
private protected HelperMethod RangeCheckHelper => this.Size switch
{
get => this.Size switch
{
1 => HelperMethod.RangeCheck8,
2 => HelperMethod.RangeCheck16,
4 => HelperMethod.RangeCheck32,
8 => HelperMethod.RangeCheck64,
_ => throw new InvalidOperationException(),// Shouldn't be possible.
};
}
1 => HelperMethod.RangeCheck8,
2 => HelperMethod.RangeCheck16,
4 => HelperMethod.RangeCheck32,
8 => HelperMethod.RangeCheck64,
_ => throw new InvalidOperationException(),// Shouldn't be possible.
};

private protected void EmitRangeCheck(CompilationContext context)
{
Expand Down
2 changes: 1 addition & 1 deletion WebAssembly/Instructions/Select.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal sealed override void Compile(CompilationContext context)

static MethodBuilder CreateSelectHelper(HelperMethod helper, CompilationContext context)
{
MethodBuilder builder = helper switch
var builder = helper switch
{
HelperMethod.SelectInt32 => context.CheckedExportsBuilder.DefineMethod(
"☣ Select Int32",
Expand Down
66 changes: 33 additions & 33 deletions WebAssembly/Instructions/TruncateSaturateInstruction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,61 +35,61 @@ internal sealed override void Compile(CompilationContext context)
[InputType]
);

// The following code is an IL version of this C#,
// with "float" and "int" replaced by "double" and "uint"
// as appropriate for the overriding class:
//
// public static int Int32TruncateSaturateFloat32Signed(float f)
// {
// if (f >= int.MaxValue)
// return int.MaxValue;
//
// if (f <= int.MinValue)
// return int.MinValue;
//
// if (float.IsNaN(f))
// return 0;
//
// return (int)f;
// }

var il = builder.GetILGenerator();
// The following code is an IL version of this C#,
// with "float" and "int" replaced by "double" and "uint"
// as appropriate for the overriding class:
//
// public static int Int32TruncateSaturateFloat32Signed(float f)
// {
// if (f >= int.MaxValue)
// return int.MaxValue;
//
// if (f <= int.MinValue)
// return int.MinValue;
//
// if (float.IsNaN(f))
// return 0;
//
// return (int)f;
// }

var il = builder.GetILGenerator();

var notAboveRangeLabel = il.DefineLabel();
var notBelowRangeLabel = il.DefineLabel();
var notNaNLabel = il.DefineLabel();

// if (f >= int.MaxValue)
il.Emit(OpCodes.Ldarg_0);
// if (f >= int.MaxValue)
il.Emit(OpCodes.Ldarg_0);
EmitLoadFloatMaxValue(il);
il.Emit(OpCodes.Blt_Un_S, notAboveRangeLabel);

// return int.MaxValue
EmitLoadIntegerMaxValue(il);
// return int.MaxValue
EmitLoadIntegerMaxValue(il);
il.Emit(OpCodes.Ret);

// if (f <= int.MinValue)
il.MarkLabel(notAboveRangeLabel);
// if (f <= int.MinValue)
il.MarkLabel(notAboveRangeLabel);
il.Emit(OpCodes.Ldarg_0);
EmitLoadFloatMinValue(il);
il.Emit(OpCodes.Bgt_Un_S, notBelowRangeLabel);

// return int.MinValue
EmitLoadIntegerMinValue(il);
// return int.MinValue
EmitLoadIntegerMinValue(il);
il.Emit(OpCodes.Ret);

// if (float.IsNaN(f))
il.MarkLabel(notBelowRangeLabel);
// if (float.IsNaN(f))
il.MarkLabel(notBelowRangeLabel);
il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Call, InputType.GetMethod(nameof(float.IsNaN))!, null);
il.Emit(OpCodes.Brfalse_S, notNaNLabel);

// return 0
EmitLoadIntegerZero(il);
// return 0
EmitLoadIntegerZero(il);
il.Emit(OpCodes.Ret);

// return (int)f
il.MarkLabel(notNaNLabel);
// return (int)f
il.MarkLabel(notNaNLabel);
il.Emit(OpCodes.Ldarg_0);
EmitConvert(il);
il.Emit(OpCodes.Ret);
Expand Down
Loading

0 comments on commit 15baea6

Please sign in to comment.