Skip to content

Commit

Permalink
Add nullability annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
siegfriedpammer committed Mar 29, 2024
1 parent 8acd117 commit 7e69247
Show file tree
Hide file tree
Showing 22 changed files with 137 additions and 125 deletions.
78 changes: 39 additions & 39 deletions ICSharpCode.ILSpyX/Analyzers/AnalyzerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,51 +32,51 @@ namespace ICSharpCode.ILSpyX.Analyzers
/// </summary>
public class AnalyzerContext
{
public AssemblyList AssemblyList { get; internal set; }
public required AssemblyList AssemblyList { get; init; }

/// <summary>
/// CancellationToken. Currently Analyzers do not support cancellation from the UI, but it should be checked nonetheless.
/// </summary>
public CancellationToken CancellationToken { get; internal set; }
/// <summary>
/// CancellationToken. Currently Analyzers do not support cancellation from the UI, but it should be checked nonetheless.
/// </summary>
public CancellationToken CancellationToken { get; init; }

/// <summary>
/// Currently used language.
/// </summary>
public ILanguage Language { get; internal set; }
/// <summary>
/// Currently used language.
/// </summary>
public required ILanguage Language { get; init; }

/// <summary>
/// Allows the analyzer to control whether the tree nodes will be sorted.
/// Must be set within <see cref="IAnalyzer.Analyze(ISymbol, AnalyzerContext)"/>
/// before the results are enumerated.
/// </summary>
public bool SortResults { get; set; }
/// <summary>
/// Allows the analyzer to control whether the tree nodes will be sorted.
/// Must be set within <see cref="IAnalyzer.Analyze(ISymbol, AnalyzerContext)"/>
/// before the results are enumerated.
/// </summary>
public bool SortResults { get; set; }

public MethodBodyBlock GetMethodBody(IMethod method)
{
if (!method.HasBody || method.MetadataToken.IsNil)
return null;
var module = method.ParentModule.MetadataFile;
var md = module.Metadata.GetMethodDefinition((MethodDefinitionHandle)method.MetadataToken);
try
{
return module.GetMethodBody(md.RelativeVirtualAddress);
}
catch (BadImageFormatException)
{
return null;
}
}
public MethodBodyBlock? GetMethodBody(IMethod method)
{
if (!method.HasBody || method.MetadataToken.IsNil || method.ParentModule?.MetadataFile == null)
return null;
var module = method.ParentModule.MetadataFile;
var md = module.Metadata.GetMethodDefinition((MethodDefinitionHandle)method.MetadataToken);
try
{
return module.GetMethodBody(md.RelativeVirtualAddress);
}
catch (BadImageFormatException)
{
return null;
}
}

public AnalyzerScope GetScopeOf(IEntity entity)
{
return new AnalyzerScope(AssemblyList, entity);
}
public AnalyzerScope GetScopeOf(IEntity entity)
{
return new AnalyzerScope(AssemblyList, entity);
}

readonly ConcurrentDictionary<MetadataFile, DecompilerTypeSystem> typeSystemCache = new();
readonly ConcurrentDictionary<MetadataFile, DecompilerTypeSystem> typeSystemCache = new();

public DecompilerTypeSystem GetOrCreateTypeSystem(MetadataFile module)
{
return typeSystemCache.GetOrAdd(module, m => new DecompilerTypeSystem(m, m.GetAssemblyResolver()));
}
public DecompilerTypeSystem GetOrCreateTypeSystem(MetadataFile module)
{
return typeSystemCache.GetOrAdd(module, m => new DecompilerTypeSystem(m, m.GetAssemblyResolver()));
}
}
}
4 changes: 2 additions & 2 deletions ICSharpCode.ILSpyX/Analyzers/AnalyzerHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static bool IsPossibleReferenceTo(EntityHandle member, MetadataFile modul
{
case HandleKind.MethodDefinition:
return member == analyzedMethod.MetadataToken
&& module == analyzedMethod.ParentModule.MetadataFile;
&& module == analyzedMethod.ParentModule?.MetadataFile;
case HandleKind.MemberReference:
var mr = metadata.GetMemberReference((MemberReferenceHandle)member);
if (mr.GetKind() != MemberReferenceKind.Method)
Expand All @@ -48,7 +48,7 @@ public static bool IsPossibleReferenceTo(EntityHandle member, MetadataFile modul
}
}

public static ISymbol GetParentEntity(DecompilerTypeSystem ts, CustomAttribute customAttribute)
public static ISymbol? GetParentEntity(DecompilerTypeSystem ts, CustomAttribute customAttribute)
{
var metadata = ts.MainModule.MetadataFile.Metadata;
switch (customAttribute.Parent.Kind)
Expand Down
20 changes: 10 additions & 10 deletions ICSharpCode.ILSpyX/Analyzers/AnalyzerScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.Util;
using ICSharpCode.ILSpyX;

namespace ICSharpCode.ILSpyX.Analyzers
{
Expand Down Expand Up @@ -61,19 +60,19 @@ public AnalyzerScope(AssemblyList assemblyList, IEntity entity)
public IEnumerable<MetadataFile> GetModulesInScope(CancellationToken ct)
{
if (IsLocal)
return new[] { TypeScope.ParentModule.MetadataFile };
return new[] { TypeScope.ParentModule!.MetadataFile! };

if (effectiveAccessibility.LessThanOrEqual(Accessibility.Internal))
return GetModuleAndAnyFriends(TypeScope, ct);

return GetReferencingModules(TypeScope.ParentModule.MetadataFile, ct);
return GetReferencingModules(TypeScope.ParentModule!.MetadataFile!, ct);
}

public IEnumerable<MetadataFile> GetAllModules()
{
return assemblyListSnapshot.GetAllAssembliesAsync().GetAwaiter().GetResult()
.Select(asm => asm.GetMetadataFileOrNull())
.Where(x => x != null);
.Where(x => x != null)!;
}

public DecompilerTypeSystem ConstructTypeSystem(MetadataFile module)
Expand Down Expand Up @@ -113,7 +112,7 @@ static void DetermineEffectiveAccessibility(IEntity input, out ITypeDefinition t
else
{
accessibility = input.Accessibility;
typeScope = input.DeclaringTypeDefinition;
typeScope = input.DeclaringTypeDefinition!;
}
// Once we reach a private entity, we leave the loop with typeScope set to the class that
// contains the private entity = the scope that needs to be searched.
Expand All @@ -123,7 +122,7 @@ static void DetermineEffectiveAccessibility(IEntity input, out ITypeDefinition t
{
accessibility = accessibility.Intersect(typeScope.Accessibility);
prevTypeScope = typeScope;
typeScope = prevTypeScope.DeclaringTypeDefinition;
typeScope = prevTypeScope.DeclaringTypeDefinition!;
}
if (typeScope == null)
{
Expand Down Expand Up @@ -181,7 +180,7 @@ IEnumerable<MetadataFile> GetReferencingModules(MetadataFile self, CancellationT

IEnumerable<MetadataFile> GetModuleAndAnyFriends(ITypeDefinition typeScope, CancellationToken ct)
{
var self = typeScope.ParentModule.MetadataFile;
var self = typeScope.ParentModule!.MetadataFile!;

yield return self;

Expand All @@ -191,9 +190,10 @@ IEnumerable<MetadataFile> GetModuleAndAnyFriends(ITypeDefinition typeScope, Canc
var friendAssemblies = new HashSet<string>();
foreach (var attribute in attributes)
{
string assemblyName = attribute.DecodeValue(typeProvider).FixedArguments[0].Value as string;
assemblyName = assemblyName.Split(',')[0]; // strip off any public key info
friendAssemblies.Add(assemblyName);
string? assemblyName = attribute.DecodeValue(typeProvider).FixedArguments[0].Value as string;
assemblyName = assemblyName?.Split(',')[0]; // strip off any public key info
if (assemblyName != null)
friendAssemblies.Add(assemblyName);
}

if (friendAssemblies.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;

Expand All @@ -44,17 +43,19 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont

IEnumerable<IEntity> AnalyzeType(IEvent analyzedEntity, ITypeDefinition type)
{
if (analyzedEntity.DeclaringTypeDefinition?.ParentModule?.MetadataFile == null)
yield break;
var token = analyzedEntity.MetadataToken;
var declaringTypeToken = analyzedEntity.DeclaringTypeDefinition.MetadataToken;
var module = analyzedEntity.DeclaringTypeDefinition.ParentModule.MetadataFile;
var allTypes = type.GetAllBaseTypeDefinitions();
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule.MetadataFile == module))
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule?.MetadataFile == module))
yield break;

foreach (var @event in type.Events)
{
var baseMembers = InheritanceHelper.GetBaseMembers(@event, true);
if (baseMembers.Any(m => m.MetadataToken == token && m.ParentModule.MetadataFile == module))
if (baseMembers.Any(m => m.MetadataToken == token && m.ParentModule?.MetadataFile == module))
yield return @event;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;

Expand All @@ -44,19 +43,21 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont

IEnumerable<IEntity> AnalyzeType(IEvent analyzedEntity, ITypeDefinition type)
{
if (analyzedEntity.DeclaringTypeDefinition?.ParentModule?.MetadataFile == null)
yield break;
var token = analyzedEntity.MetadataToken;
var declaringTypeToken = analyzedEntity.DeclaringTypeDefinition.MetadataToken;
var module = analyzedEntity.DeclaringTypeDefinition.ParentModule.MetadataFile;
var allTypes = type.GetAllBaseTypeDefinitions();
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule.MetadataFile == module))
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule?.MetadataFile == module))
yield break;

foreach (var @event in type.Events)
{
if (!@event.IsOverride)
continue;
var baseMembers = InheritanceHelper.GetBaseMembers(@event, false);
if (baseMembers.Any(p => p.MetadataToken == token && p.ParentModule.MetadataFile == module))
if (baseMembers.Any(p => p.MetadataToken == token && p.ParentModule?.MetadataFile == module))
{
yield return @event;
}
Expand Down
10 changes: 6 additions & 4 deletions ICSharpCode.ILSpyX/Analyzers/Builtin/FieldAccessAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont
var scope = context.GetScopeOf((IEntity)analyzedSymbol);
foreach (var type in scope.GetTypesInScope(context.CancellationToken))
{
if (type.ParentModule?.MetadataFile == null)
continue;
var mappingInfo = context.Language.GetCodeMappingInfo(type.ParentModule.MetadataFile, type.MetadataToken);
var methods = type.GetMembers(m => m is IMethod, Options).OfType<IMethod>();
foreach (var method in methods)
Expand Down Expand Up @@ -119,7 +121,7 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont

bool IsUsedInMethod(IField analyzedField, IMethod method, CodeMappingInfo mappingInfo, AnalyzerContext context)
{
if (method.MetadataToken.IsNil)
if (method.MetadataToken.IsNil || method.ParentModule?.MetadataFile == null)
return false;
var module = method.ParentModule.MetadataFile;
foreach (var part in mappingInfo.GetMethodParts((MethodDefinitionHandle)method.MetadataToken))
Expand All @@ -144,7 +146,7 @@ bool IsUsedInMethod(IField analyzedField, IMethod method, CodeMappingInfo mappin

bool ScanMethodBody(IField analyzedField, IMethod method, MethodBodyBlock methodBody)
{
if (methodBody == null)
if (methodBody == null || method.ParentModule?.MetadataFile == null)
return false;

var mainModule = (MetadataModule)method.ParentModule;
Expand All @@ -170,7 +172,7 @@ bool ScanMethodBody(IField analyzedField, IMethod method, MethodBodyBlock method
EntityHandle fieldHandle = MetadataTokenHelpers.EntityHandleOrNil(blob.ReadInt32());
if (!fieldHandle.Kind.IsMemberKind())
continue;
IField field;
IField? field;
try
{
field = mainModule.ResolveEntity(fieldHandle, genericContext) as IField;
Expand All @@ -183,7 +185,7 @@ bool ScanMethodBody(IField analyzedField, IMethod method, MethodBodyBlock method
continue;

if (field.MetadataToken == analyzedField.MetadataToken
&& field.ParentModule.MetadataFile == analyzedField.ParentModule.MetadataFile)
&& field.ParentModule?.MetadataFile == analyzedField.ParentModule!.MetadataFile)
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont
Debug.Assert(!member.IsStatic);

var baseMembers = InheritanceHelper.GetBaseMembers(member, includeImplementedInterfaces: true);
return baseMembers.Where(m => m.DeclaringTypeDefinition.Kind == TypeKind.Interface);
return baseMembers.Where(m => m.DeclaringTypeDefinition?.Kind == TypeKind.Interface);
}

public bool Show(ISymbol symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ICSharpCode.Decompiler.TypeSystem;

Expand All @@ -47,17 +43,19 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont

IEnumerable<IEntity> AnalyzeType(IMethod analyzedEntity, ITypeDefinition type)
{
if (analyzedEntity.DeclaringTypeDefinition?.ParentModule?.MetadataFile == null)
yield break;
var token = analyzedEntity.MetadataToken;
var declaringTypeToken = analyzedEntity.DeclaringTypeDefinition.MetadataToken;
var module = analyzedEntity.DeclaringTypeDefinition.ParentModule.MetadataFile;
var allTypes = type.GetAllBaseTypeDefinitions();
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule.MetadataFile == module))
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule?.MetadataFile == module))
yield break;

foreach (var method in type.Methods)
{
var baseMembers = InheritanceHelper.GetBaseMembers(method, true);
if (baseMembers.Any(m => m.MetadataToken == token && m.ParentModule.MetadataFile == module))
if (baseMembers.Any(m => m.MetadataToken == token && m.ParentModule?.MetadataFile == module))
yield return method;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// DEALINGS IN THE SOFTWARE.

using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;

Expand Down Expand Up @@ -46,19 +45,21 @@ public IEnumerable<ISymbol> Analyze(ISymbol analyzedSymbol, AnalyzerContext cont

IEnumerable<IEntity> AnalyzeType(IMethod analyzedEntity, ITypeDefinition type)
{
if (analyzedEntity.DeclaringTypeDefinition?.ParentModule?.MetadataFile == null)
yield break;
var token = analyzedEntity.MetadataToken;
var declaringTypeToken = analyzedEntity.DeclaringTypeDefinition.MetadataToken;
var module = analyzedEntity.DeclaringTypeDefinition.ParentModule.MetadataFile;
var allTypes = type.GetAllBaseTypeDefinitions();
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule.MetadataFile == module))
if (!allTypes.Any(t => t.MetadataToken == declaringTypeToken && t.ParentModule?.MetadataFile == module))
yield break;

foreach (var method in type.Methods)
{
if (!method.IsOverride)
continue;
var baseMembers = InheritanceHelper.GetBaseMembers(method, false);
if (baseMembers.Any(p => p.MetadataToken == token && p.ParentModule.MetadataFile == module))
if (baseMembers.Any(p => p.MetadataToken == token && p.ParentModule?.MetadataFile == module))
{
yield return method;
}
Expand Down
Loading

0 comments on commit 7e69247

Please sign in to comment.