Summary

Class:System.ThrowHelper
Assembly:System.Memory
File(s):C:\GitHub\corefx\src\System.Memory\src\System\ThrowHelper.cs
Covered lines:52
Uncovered lines:7
Coverable lines:59
Total lines:194
Line coverage:88.1%
Branch coverage:100%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ThrowArgumentNullException(...)1000
CreateArgumentNullException(...)10100100
ThrowArgumentException_InvalidTypeWithPointersNotSupported(...)10100100
CreateArgumentException_InvalidTypeWithPointersNotSupported(...)10100100
ThrowArgumentOutOfRangeException(...)10100100
CreateArgumentOutOfRangeException(...)10100100
ThrowArgumentOutOfRangeException_PrecisionTooLarge()10100100
CreateArgumentOutOfRangeException_PrecisionTooLarge()10100100
ThrowArgumentOutOfRangeException_SymbolDoesNotFit()10100100
CreateArgumentOutOfRangeException_SymbolDoesNotFit()10100100
ThrowInvalidOperationException()10100100
CreateInvalidOperationException()10100100
ThrowInvalidOperationException_EndPositionNotReached()1000
CreateInvalidOperationException_EndPositionNotReached()1000
ThrowArgumentOutOfRangeException_PositionOutOfRange()10100100
CreateArgumentOutOfRangeException_PositionOutOfRange()10100100
ThrowArgumentOutOfRangeException_CountOutOfRange()10100100
CreateArgumentOutOfRangeException_CountOutOfRange()10100100
ThrowObjectDisposedException_ArrayMemoryPoolBuffer()10100100
CreateObjectDisposedException_ArrayMemoryPoolBuffer()10100100
ThrowFormatException_BadFormatSpecifier()10100100
CreateFormatException_BadFormatSpecifier()10100100
TryFormatThrowFormatException(...)1060100
TryParseThrowFormatException(...)1066.67100
ThrowArgumentValidationException(...)10100100
CreateArgumentValidationException(...)48100100
ThrowArgumentValidationException(...)10100100
CreateArgumentValidationException(...)34100100
ThrowArgumentValidationException(...)10100100
CreateArgumentValidationException(...)34100100

File(s)

C:\GitHub\corefx\src\System.Memory\src\System\ThrowHelper.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Runtime.CompilerServices;
 6using System.Buffers;
 7
 8namespace System
 9{
 10    //
 11    // This pattern of easily inlinable "void Throw" routines that stack on top of NoInlining factory methods
 12    // is a compromise between older JITs and newer JITs (RyuJIT in Core CLR 1.1.0+ and desktop CLR in 4.6.3+).
 13    // This package is explicitly targeted at older JITs as newer runtimes expect to implement Span intrinsically for
 14    // best performance.
 15    //
 16    // The aim of this pattern is three-fold
 17    // 1. Extracting the throw makes the method preforming the throw in a conditional branch smaller and more inlinable
 18    // 2. Extracting the throw from generic method to non-generic method reduces the repeated codegen size for value typ
 19    // 3a. Newer JITs will not inline the methods that only throw and also recognise them, move the call to cold section
 20    //     and not add stack prep and unwind before calling https://github.com/dotnet/coreclr/pull/6103
 21    // 3b. Older JITs will inline the throw itself and move to cold section; but not inline the non-inlinable exception
 22    //     factory methods - still maintaining advantages 1 & 2
 23    //
 24
 25    internal static class ThrowHelper
 26    {
 027        internal static void ThrowArgumentNullException(ExceptionArgument argument) { throw CreateArgumentNullException(
 28        [MethodImpl(MethodImplOptions.NoInlining)]
 329        private static Exception CreateArgumentNullException(ExceptionArgument argument) { return new ArgumentNullExcept
 30
 31        internal static void ThrowArrayTypeMismatchException() { throw CreateArrayTypeMismatchException(); }
 32        [MethodImpl(MethodImplOptions.NoInlining)]
 33        private static Exception CreateArrayTypeMismatchException() { return new ArrayTypeMismatchException(); }
 34
 235        internal static void ThrowArgumentException_InvalidTypeWithPointersNotSupported(Type type) { throw CreateArgumen
 36        [MethodImpl(MethodImplOptions.NoInlining)]
 337        private static Exception CreateArgumentException_InvalidTypeWithPointersNotSupported(Type type) { return new Arg
 38
 39        internal static void ThrowArgumentException_DestinationTooShort() { throw CreateArgumentException_DestinationToo
 40        [MethodImpl(MethodImplOptions.NoInlining)]
 41        private static Exception CreateArgumentException_DestinationTooShort() { return new ArgumentException(SR.Argumen
 42
 43        internal static void ThrowIndexOutOfRangeException() { throw CreateIndexOutOfRangeException(); }
 44        [MethodImpl(MethodImplOptions.NoInlining)]
 45        private static Exception CreateIndexOutOfRangeException() { return new IndexOutOfRangeException(); }
 46
 47        internal static void ThrowArgumentOutOfRangeException() { throw CreateArgumentOutOfRangeException(); }
 48        [MethodImpl(MethodImplOptions.NoInlining)]
 49        private static Exception CreateArgumentOutOfRangeException() { return new ArgumentOutOfRangeException(); }
 50
 251        internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) { throw CreateArgumentOutOfRan
 52        [MethodImpl(MethodImplOptions.NoInlining)]
 353        private static Exception CreateArgumentOutOfRangeException(ExceptionArgument argument) { return new ArgumentOutO
 54
 255        internal static void ThrowArgumentOutOfRangeException_PrecisionTooLarge() { throw CreateArgumentOutOfRangeExcept
 56        [MethodImpl(MethodImplOptions.NoInlining)]
 357        private static Exception CreateArgumentOutOfRangeException_PrecisionTooLarge() { return new ArgumentOutOfRangeEx
 58
 259        internal static void ThrowArgumentOutOfRangeException_SymbolDoesNotFit() { throw CreateArgumentOutOfRangeExcepti
 60        [MethodImpl(MethodImplOptions.NoInlining)]
 361        private static Exception CreateArgumentOutOfRangeException_SymbolDoesNotFit() { return new ArgumentOutOfRangeExc
 62
 263        internal static void ThrowInvalidOperationException() { throw CreateInvalidOperationException(); }
 64        [MethodImpl(MethodImplOptions.NoInlining)]
 365        private static Exception CreateInvalidOperationException() { return new InvalidOperationException(); }
 66
 67        internal static void ThrowInvalidOperationException_OutstandingReferences() { throw CreateInvalidOperationExcept
 68        [MethodImpl(MethodImplOptions.NoInlining)]
 69        private static Exception CreateInvalidOperationException_OutstandingReferences() { return new InvalidOperationEx
 70
 71        internal static void ThrowInvalidOperationException_UnexpectedSegmentType() { throw CreateInvalidOperationExcept
 72        [MethodImpl(MethodImplOptions.NoInlining)]
 73        private static Exception CreateInvalidOperationException_UnexpectedSegmentType() { return new InvalidOperationEx
 74
 075        internal static void ThrowInvalidOperationException_EndPositionNotReached() { throw CreateInvalidOperationExcept
 76        [MethodImpl(MethodImplOptions.NoInlining)]
 077        private static Exception CreateInvalidOperationException_EndPositionNotReached() { return new InvalidOperationEx
 78
 279        internal static void ThrowArgumentOutOfRangeException_PositionOutOfRange() { throw CreateArgumentOutOfRangeExcep
 80        [MethodImpl(MethodImplOptions.NoInlining)]
 381        private static Exception CreateArgumentOutOfRangeException_PositionOutOfRange() { return new ArgumentOutOfRangeE
 82
 283        internal static void ThrowArgumentOutOfRangeException_CountOutOfRange() { throw CreateArgumentOutOfRangeExceptio
 84        [MethodImpl(MethodImplOptions.NoInlining)]
 385        private static Exception CreateArgumentOutOfRangeException_CountOutOfRange() { return new ArgumentOutOfRangeExce
 86
 287        internal static void ThrowObjectDisposedException_ArrayMemoryPoolBuffer() { throw CreateObjectDisposedException_
 88        [MethodImpl(MethodImplOptions.NoInlining)]
 389        private static Exception CreateObjectDisposedException_ArrayMemoryPoolBuffer() { return new ObjectDisposedExcept
 90
 91        internal static void ThrowObjectDisposedException_MemoryDisposed() { throw CreateObjectDisposedException_MemoryD
 92        [MethodImpl(MethodImplOptions.NoInlining)]
 93        private static Exception CreateObjectDisposedException_MemoryDisposed() { return new ObjectDisposedException("Ow
 94
 295        internal static void ThrowFormatException_BadFormatSpecifier() { throw CreateFormatException_BadFormatSpecifier(
 96        [MethodImpl(MethodImplOptions.NoInlining)]
 397        private static Exception CreateFormatException_BadFormatSpecifier() { return new FormatException(SR.Argument_Bad
 98
 99        internal static void ThrowArgumentException_OverlapAlignmentMismatch() { throw CreateArgumentException_OverlapAl
 100        [MethodImpl(MethodImplOptions.NoInlining)]
 101        private static Exception CreateArgumentException_OverlapAlignmentMismatch() { return new ArgumentException(SR.Ar
 102
 103        internal static void ThrowNotSupportedException() { throw CreateThrowNotSupportedException(); }
 104        [MethodImpl(MethodImplOptions.NoInlining)]
 105        private static Exception CreateThrowNotSupportedException() { return new NotSupportedException(); }
 106
 107        //
 108        // Enable use of ThrowHelper from TryFormat() routines without introducing dozens of non-code-coveraged "bytesWr
 109        //
 110        public static bool TryFormatThrowFormatException(out int bytesWritten)
 1111        {
 1112            bytesWritten = 0;
 1113            ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 0114            return false;
 0115        }
 116
 117        //
 118        // Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= d
 119        //
 120        public static bool TryParseThrowFormatException<T>(out T value, out int bytesConsumed)
 1121        {
 1122            value = default;
 1123            bytesConsumed = 0;
 1124            ThrowHelper.ThrowFormatException_BadFormatSpecifier();
 0125            return false;
 0126        }
 127
 128        //
 129        // ReadOnlySequence .ctor validation Throws coalesced to enable inlining of the .ctor
 130        //
 131        public static void ThrowArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int startIndex, 
 1132            => throw CreateArgumentValidationException(startSegment, startIndex, endSegment);
 133
 134        private static Exception CreateArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int start
 1135        {
 1136            if (startSegment == null)
 1137                return CreateArgumentNullException(ExceptionArgument.startSegment);
 1138            else if (endSegment == null)
 1139                return CreateArgumentNullException(ExceptionArgument.endSegment);
 1140            else if ((uint)startSegment.Memory.Length < (uint)startIndex)
 1141                return CreateArgumentOutOfRangeException(ExceptionArgument.startIndex);
 142            else
 1143                return CreateArgumentOutOfRangeException(ExceptionArgument.endIndex);
 1144        }
 145
 146        public static void ThrowArgumentValidationException(Array array, int start)
 1147            => throw CreateArgumentValidationException(array, start);
 148
 149        private static Exception CreateArgumentValidationException(Array array, int start)
 1150        {
 1151            if (array == null)
 1152                return CreateArgumentNullException(ExceptionArgument.array);
 1153            else if ((uint)start > (uint)array.Length)
 1154                return CreateArgumentOutOfRangeException(ExceptionArgument.start);
 155            else
 1156                return CreateArgumentOutOfRangeException(ExceptionArgument.length);
 1157        }
 158
 159        public static void ThrowArgumentValidationException<T>(OwnedMemory<T> ownedMemory, int start)
 1160            => throw CreateArgumentValidationException(ownedMemory, start);
 161
 162        private static Exception CreateArgumentValidationException<T>(OwnedMemory<T> ownedMemory, int start)
 1163        {
 1164            if (ownedMemory == null)
 1165                return CreateArgumentNullException(ExceptionArgument.ownedMemory);
 1166            else if ((uint)start > (uint)ownedMemory.Length)
 1167                return CreateArgumentOutOfRangeException(ExceptionArgument.start);
 168            else
 1169                return CreateArgumentOutOfRangeException(ExceptionArgument.length);
 1170        }
 171    }
 172
 173    //
 174    // The convention for this enum is using the argument name as the enum name
 175    //
 176    internal enum ExceptionArgument
 177    {
 178        length,
 179        start,
 180        minimumBufferSize,
 181        byteOffset,
 182        comparable,
 183        comparer,
 184        destination,
 185        offset,
 186        startSegment,
 187        endSegment,
 188        startIndex,
 189        endIndex,
 190        array,
 191        ownedMemory,
 192        culture
 193    }
 194}

Methods/Properties

ThrowArgumentNullException(System.ExceptionArgument)
CreateArgumentNullException(System.ExceptionArgument)
ThrowArgumentException_InvalidTypeWithPointersNotSupported(System.Type)
CreateArgumentException_InvalidTypeWithPointersNotSupported(System.Type)
ThrowArgumentOutOfRangeException(System.ExceptionArgument)
CreateArgumentOutOfRangeException(System.ExceptionArgument)
ThrowArgumentOutOfRangeException_PrecisionTooLarge()
CreateArgumentOutOfRangeException_PrecisionTooLarge()
ThrowArgumentOutOfRangeException_SymbolDoesNotFit()
CreateArgumentOutOfRangeException_SymbolDoesNotFit()
ThrowInvalidOperationException()
CreateInvalidOperationException()
ThrowInvalidOperationException_EndPositionNotReached()
CreateInvalidOperationException_EndPositionNotReached()
ThrowArgumentOutOfRangeException_PositionOutOfRange()
CreateArgumentOutOfRangeException_PositionOutOfRange()
ThrowArgumentOutOfRangeException_CountOutOfRange()
CreateArgumentOutOfRangeException_CountOutOfRange()
ThrowObjectDisposedException_ArrayMemoryPoolBuffer()
CreateObjectDisposedException_ArrayMemoryPoolBuffer()
ThrowFormatException_BadFormatSpecifier()
CreateFormatException_BadFormatSpecifier()
TryFormatThrowFormatException(System.Int32&)
TryParseThrowFormatException(T&,System.Int32&)
ThrowArgumentValidationException(System.Buffers.ReadOnlySequenceSegment`1<T>,System.Int32,System.Buffers.ReadOnlySequenceSegment`1<T>)
CreateArgumentValidationException(System.Buffers.ReadOnlySequenceSegment`1<T>,System.Int32,System.Buffers.ReadOnlySequenceSegment`1<T>)
ThrowArgumentValidationException(System.Array,System.Int32)
CreateArgumentValidationException(System.Array,System.Int32)
ThrowArgumentValidationException(System.Buffers.OwnedMemory`1<T>,System.Int32)
CreateArgumentValidationException(System.Buffers.OwnedMemory`1<T>,System.Int32)