Skip to content
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
2 changes: 2 additions & 0 deletions src/Ben.Demystifier/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ internal static class Constants
{
internal const string TrimWarning = "This class should be avoided when compiling for AOT.";
internal const string SuppressionResurfaced = "Surfaced by parent class";
internal const string AvoidAtRuntime = "Non-trimmable code is avoided at runtime";
internal const string SingleFileFallback = "Fallback functionality for single files";
}
33 changes: 29 additions & 4 deletions src/Ben.Demystifier/EnhancedStackTrace.Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

namespace System.Diagnostics
{
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
internal partial class EnhancedStackTrace
{
private static readonly Type? StackTraceHiddenAttributeType = Type.GetType("System.Diagnostics.StackTraceHiddenAttribute", false);
private static readonly Type? AsyncIteratorStateMachineAttributeType = Type.GetType("System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute", false);

[UnconditionalSuppressMessage("Trimming", "IL2026: RequiresUnreferencedCode", Justification = Constants.AvoidAtRuntime)]
static EnhancedStackTrace()
{
if (AsyncIteratorStateMachineAttributeType != null) return;
Expand All @@ -35,15 +33,18 @@ static EnhancedStackTrace()
try
{
mba = Assembly.Load("Microsoft.Bcl.AsyncInterfaces");
AsyncIteratorStateMachineAttributeType = mba.GetType("System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute", false);
}
catch
{
return;
}

AsyncIteratorStateMachineAttributeType = mba.GetType("System.Runtime.CompilerServices.AsyncIteratorStateMachineAttribute", false);
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static List<EnhancedStackFrame> GetFrames(Exception exception)
{
if (exception == null)
Expand All @@ -57,6 +58,9 @@ private static List<EnhancedStackFrame> GetFrames(Exception exception)
return GetFrames(stackTrace);
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public static List<EnhancedStackFrame> GetFrames(StackTrace stackTrace)
{
var frames = new List<EnhancedStackFrame>();
Expand Down Expand Up @@ -124,6 +128,9 @@ public static List<EnhancedStackFrame> GetFrames(StackTrace stackTrace)
return frames;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public static ResolvedMethod GetMethodDisplayString(MethodBase originMethod)
{
var method = originMethod;
Expand Down Expand Up @@ -300,6 +307,9 @@ private static bool IsFSharpAsync(MethodBase method)
return false;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static bool TryResolveGeneratedName(ref MethodBase method, out Type? type, out string methodName, out string? subMethodName, out GeneratedNameKind kind, out int? ordinal)
{
kind = GeneratedNameKind.None;
Expand Down Expand Up @@ -386,6 +396,9 @@ private static bool TryResolveGeneratedName(ref MethodBase method, out Type? typ
return false;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMethods, GeneratedNameKind kind, string? matchHint, ref MethodBase method, ref Type? type, out int? ordinal)
{
ordinal = null;
Expand Down Expand Up @@ -455,6 +468,9 @@ private static bool TryResolveSourceMethod(IEnumerable<MethodBase> candidateMeth
return false;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static void GetOrdinal(MethodBase method, ref int? ordinal)
{
var lamdaStart = method.Name.IndexOf((char)GeneratedNameKind.LambdaMethod + "__") + 3;
Expand Down Expand Up @@ -610,6 +626,9 @@ private static string GetPrefix(ParameterInfo parameter)
return string.Empty;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static ResolvedParameter GetParameter(ParameterInfo parameter)
{
var prefix = GetPrefix(parameter);
Expand Down Expand Up @@ -648,6 +667,9 @@ private static ResolvedParameter GetParameter(ParameterInfo parameter)
};
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
private static ResolvedParameter GetValueTupleParameter(IList<string?> tupleNames, string prefix, string? name, Type parameterType)
{
return new ValueTupleResolvedParameter(parameterType, tupleNames)
Expand Down Expand Up @@ -867,6 +889,9 @@ private static bool IsStackTraceHidden(MemberInfo memberInfo)
return false;
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
// https://github.com/dotnet/runtime/blob/c985bdcec2a9190e733bcada413a193d5ff60c0d/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs#L375-L430
private static bool TryResolveStateMachineMethod(ref MethodBase method, out Type declaringType)
{
Expand Down
11 changes: 10 additions & 1 deletion src/Ben.Demystifier/EnhancedStackTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
using System.Collections.Generic.Enumerable;
using System.IO;
using System.Text;
using Ben.Demystifier;

namespace System.Diagnostics
{
internal partial class EnhancedStackTrace : StackTrace, IEnumerable<EnhancedStackFrame>
{
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public static EnhancedStackTrace Current() => new EnhancedStackTrace(new StackTrace(1 /* skip this one frame */, true));

private readonly List<EnhancedStackFrame> _frames;
Expand All @@ -26,6 +30,9 @@ internal partial class EnhancedStackTrace : StackTrace, IEnumerable<EnhancedStac
// Exceptions:
// T:System.ArgumentNullException:
// The parameter e is null.
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public EnhancedStackTrace(Exception e)
{
if (e == null)
Expand All @@ -36,7 +43,9 @@ public EnhancedStackTrace(Exception e)
_frames = GetFrames(e);
}


#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public EnhancedStackTrace(StackTrace stackTrace)
{
if (stackTrace == null)
Expand Down
8 changes: 4 additions & 4 deletions src/Ben.Demystifier/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text;
using Ben.Demystifier;
using Constants = Ben.Demystifier.Constants;

namespace System.Diagnostics
{
Expand All @@ -23,9 +23,9 @@ private static void SetStackTracesString(this Exception exception, string value)
/// <summary>
/// Demystifies the given <paramref name="exception"/> and tracks the original stack traces for the whole exception tree.
/// </summary>
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = Constants.SuppressionResurfaced)]
#endif
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public static T Demystify<T>(this T exception) where T : Exception
{
try
Expand Down
10 changes: 6 additions & 4 deletions src/Ben.Demystifier/Internal/ILReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

namespace System.Diagnostics.Internal
{
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
internal class ILReader
{
private static OpCode[] singleByteOpCode;
Expand All @@ -16,13 +13,15 @@ internal class ILReader
private readonly byte[] _cil;
private int ptr;


public ILReader(byte[] cil) => _cil = cil;

public OpCode OpCode { get; private set; }
public int MetadataToken { get; private set; }
public MemberInfo? Operand { get; private set; }

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
public bool Read(MethodBase methodInfo)
{
if (ptr < _cil.Length)
Expand All @@ -43,6 +42,9 @@ OpCode ReadOpCode()
return doubleByteOpCode[ReadByte()];
}

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
MemberInfo? ReadOperand(OpCode code, MethodBase methodInfo)
{
MetadataToken = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Ben.Demystifier/Internal/PortablePdbReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class PortablePdbReader : IDisposable
new Dictionary<string, MetadataReaderProvider>(StringComparer.Ordinal);

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("SingleFile", "IL3000:Avoid accessing Assembly file path when publishing as a single file", Justification = Constants.SuppressionResurfaced)]
[UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path", Justification = Constants.SingleFileFallback)]
#endif
public void PopulateStackFrame(StackFrame frameInfo, MethodBase method, int IlOffset, out string fileName, out int row, out int column)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Ben.Demystifier/ResolvedParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ internal class ResolvedParameter
public override string ToString() => Append(new StringBuilder()).ToString();

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("SingleFile", "IL3002:Avoid calling members marked with 'RequiresAssemblyFilesAttribute' when publishing as a single-file", Justification = Constants.SuppressionResurfaced)]
[UnconditionalSuppressMessage("SingleFile", "IL3002: calling members marked with 'RequiresAssemblyFilesAttribute'", Justification = Constants.SingleFileFallback)]
#endif
public StringBuilder Append(StringBuilder sb)
{
if (ResolvedType.Assembly.ManifestModule.Name == "FSharp.Core.dll" && ResolvedType.Name == "Unit")
return sb;

if (!string.IsNullOrEmpty(Prefix))
{
sb.Append(Prefix)
Expand Down Expand Up @@ -62,7 +62,7 @@ public StringBuilder Append(StringBuilder sb)
return sb;
}

protected virtual void AppendTypeName(StringBuilder sb)
protected virtual void AppendTypeName(StringBuilder sb)
{
sb.AppendTypeDisplayName(ResolvedType, fullName: false, includeGenericParameterNames: true);
}
Expand Down
7 changes: 2 additions & 5 deletions src/Ben.Demystifier/TypeNameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
namespace System.Diagnostics
{
// Adapted from https://github.com/aspnet/Common/blob/dev/shared/Microsoft.Extensions.TypeNameHelper.Sources/TypeNameHelper.cs
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(Constants.TrimWarning)]
#endif
internal static class TypeNameHelper
{
public static readonly Dictionary<Type, string> BuiltInTypeNames = new Dictionary<Type, string>
Expand Down Expand Up @@ -79,7 +76,7 @@ public static string GetTypeNameForGenericType(Type type)
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("SingleFile", "IL3002:Avoid calling members marked with 'RequiresAssemblyFilesAttribute' when publishing as a single-file", Justification = Constants.SuppressionResurfaced)]
[UnconditionalSuppressMessage("SingleFile", "IL3002: calling members marked with 'RequiresAssemblyFilesAttribute'", Justification = Constants.SingleFileFallback)]
#endif
private static void ProcessType(StringBuilder builder, Type type, DisplayNameOptions options)
{
Expand Down Expand Up @@ -154,7 +151,7 @@ private static void ProcessArrayType(StringBuilder builder, Type type, DisplayNa
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("SingleFile", "IL3002:Avoid calling members marked with 'RequiresAssemblyFilesAttribute' when publishing as a single-file", Justification = Constants.SuppressionResurfaced)]
[UnconditionalSuppressMessage("SingleFile", "IL3002: calling members marked with 'RequiresAssemblyFilesAttribute'", Justification = Constants.SingleFileFallback)]
#endif
private static void ProcessGenericType(StringBuilder builder, Type type, Type[] genericArguments, int length, DisplayNameOptions options)
{
Expand Down