55using SimpleJson ;
66using System ;
77using System . Collections . Generic ;
8- using System . Diagnostics . CodeAnalysis ;
98using System . Linq ;
109using System . Xml . Serialization ;
1110
11+ #if NET6_0_OR_GREATER
12+ using System . Diagnostics . CodeAnalysis ;
13+ #endif
14+
15+ #nullable enable
16+
1217namespace BenchmarkDotNet . Disassemblers ;
1318
1419public abstract class SourceCode
@@ -38,8 +43,8 @@ internal virtual void Deserialize(JsonObject json)
3843
3944public sealed class Sharp : SourceCode
4045{
41- public string Text { get ; set ; }
42- public string FilePath { get ; set ; }
46+ public string Text { get ; set ; } = default ! ;
47+ public string FilePath { get ; set ; } = default ! ;
4348 public int LineNumber { get ; set ; }
4449
4550 private protected override void Serialize ( JsonObject json )
@@ -134,7 +139,7 @@ internal override void Deserialize(JsonObject json)
134139 foreach ( var kvp in ( JsonObject ) json [ nameof ( Instruction ) ] )
135140 {
136141 object value = kvp . Value ;
137- var property = typeof ( Instruction ) . GetProperty ( kvp . Key ) ;
142+ var property = typeof ( Instruction ) . GetProperty ( kvp . Key ) ! ;
138143 var propertyType = property . PropertyType ;
139144 if ( propertyType == typeof ( ulong ) )
140145 {
@@ -154,7 +159,7 @@ internal override void Deserialize(JsonObject json)
154159 }
155160 property . SetValue ( instruction , value ) ;
156161 }
157- Instruction = ( Instruction ) instruction ;
162+ Instruction = ( Instruction ) instruction ;
158163 }
159164}
160165
@@ -164,10 +169,10 @@ public sealed class Arm64Asm : Asm
164169 private const string BytesKey = "Arm64Bytes" ;
165170 private const string SyntaxKey = "Arm64Syntax" ;
166171
167- public Arm64Instruction Instruction { get ; set ; }
172+ public Arm64Instruction ? Instruction { get ; set ; }
168173 internal DisassembleSyntax DisassembleSyntax { get ; set ; }
169174
170- public override string ToString ( ) => Instruction . ToString ( ) ;
175+ public override string ToString ( ) => Instruction ? . ToString ( ) ?? "" ;
171176
172177 private protected override void Serialize ( JsonObject json )
173178 {
@@ -178,7 +183,7 @@ private protected override void Serialize(JsonObject json)
178183 {
179184 json [ AddressKey ] = Instruction . Address . ToString ( ) ;
180185 json [ BytesKey ] = Convert . ToBase64String ( Instruction . Bytes ) ;
181- json [ SyntaxKey ] = ( int ) DisassembleSyntax ;
186+ json [ SyntaxKey ] = ( int ) DisassembleSyntax ;
182187 }
183188 }
184189
@@ -191,16 +196,16 @@ internal override void Deserialize(JsonObject json)
191196 // Use the Capstone disassembler to recreate the instruction from the bytes.
192197 using var disassembler = CapstoneDisassembler . CreateArm64Disassembler ( Arm64DisassembleMode . Arm ) ;
193198 disassembler . EnableInstructionDetails = true ;
194- disassembler . DisassembleSyntax = ( DisassembleSyntax ) Convert . ToInt32 ( json [ SyntaxKey ] ) ;
195- byte [ ] bytes = Convert . FromBase64String ( ( string ) bytes64 ) ;
196- Instruction = disassembler . Disassemble ( bytes , long . Parse ( ( string ) json [ AddressKey ] ) ) . Single ( ) ;
199+ disassembler . DisassembleSyntax = ( DisassembleSyntax ) Convert . ToInt32 ( json [ SyntaxKey ] ) ;
200+ byte [ ] bytes = Convert . FromBase64String ( ( string ) bytes64 ) ;
201+ Instruction = disassembler . Disassemble ( bytes , long . Parse ( ( string ) json [ AddressKey ] ) ) . Single ( ) ;
197202 }
198203 }
199204}
200205
201206public sealed class MonoCode : SourceCode
202207{
203- public string Text { get ; set ; }
208+ public string Text { get ; set ; } = "" ;
204209
205210 private protected override void Serialize ( JsonObject json )
206211 {
@@ -223,7 +228,7 @@ public sealed class Map
223228 [ XmlArrayItem ( nameof ( SourceCode ) , typeof ( SourceCode ) ) ]
224229 [ XmlArrayItem ( nameof ( Sharp ) , typeof ( Sharp ) ) ]
225230 [ XmlArrayItem ( nameof ( IntelAsm ) , typeof ( IntelAsm ) ) ]
226- public SourceCode [ ] SourceCodes { get ; set ; }
231+ public SourceCode [ ] SourceCodes { get ; set ; } = [ ] ;
227232
228233 internal JsonObject Serialize ( )
229234 {
@@ -260,15 +265,15 @@ internal void Deserialize(JsonObject json)
260265
261266public sealed class DisassembledMethod
262267{
263- public string Name { get ; set ; }
268+ public string Name { get ; set ; } = "" ;
264269
265270 public ulong NativeCode { get ; set ; }
266271
267- public string Problem { get ; set ; }
272+ public string Problem { get ; set ; } = "" ;
268273
269274 public Map [ ] Maps { get ; set ; } = [ ] ;
270275
271- public string CommandLine { get ; set ; }
276+ public string CommandLine { get ; set ; } = "" ;
272277
273278 public static DisassembledMethod Empty ( string fullSignature , ulong nativeCode , string problem )
274279 => new ( )
@@ -325,7 +330,7 @@ public Dictionary<ulong, string> AddressToNameMapping
325330 => _addressToNameMapping ??= SerializedAddressToNameMapping . ToDictionary ( x => x . Key , x => x . Value ) ;
326331
327332 [ XmlIgnore ]
328- private Dictionary < ulong , string > _addressToNameMapping ;
333+ private Dictionary < ulong , string > ? _addressToNameMapping = null ;
329334
330335 // KeyValuePair is not serializable, because it has read-only properties
331336 // so we need to define our own...
@@ -411,7 +416,6 @@ internal State(ClrRuntime runtime, string targetFrameworkMoniker)
411416 }
412417
413418 internal ClrRuntime Runtime { get ; }
414- internal string TargetFrameworkMoniker { get ; }
415419 internal Queue < MethodInfo > Todo { get ; }
416420 internal HashSet < ClrMethod > HandledMethods { get ; }
417421 internal Dictionary < ulong , string > AddressToNameMapping { get ; }
@@ -444,7 +448,16 @@ internal static Version ParseVersion(string targetFrameworkMoniker)
444448
445449 private sealed class ClrMethodComparer : IEqualityComparer < ClrMethod >
446450 {
447- public bool Equals ( ClrMethod x , ClrMethod y ) => x . NativeCode == y . NativeCode ;
451+ public bool Equals ( ClrMethod ? x , ClrMethod ? y )
452+ {
453+ if ( ReferenceEquals ( x , y ) )
454+ return true ;
455+
456+ if ( x is null || y is null )
457+ return false ;
458+
459+ return x . NativeCode == y . NativeCode ;
460+ }
448461
449462 public int GetHashCode ( ClrMethod obj ) => ( int ) obj . NativeCode ;
450463 }
0 commit comments