Ejemplo n.º 1
0
        public void AnalyzeProgram_ConsiderAutoPropertyAndFieldsAsSafe()
        {
            //language=cs
            const string sample =
                @"using System;
                using HotPathAllocationAnalyzer.Support;
                
                public abstract class FooBase
                {
                    public string FullName;
                    public string Name { get; } = ""Hello"";
                    public int[] Data { get; } = new int[10];
                }
                
                public class Foo : FooBase
                {
                    public int[] Data { get; } = new int[10];
                }
                
                [HotPathAllocationAnalyzer.Support.NoAllocation]
                public int PerfCritical(Foo f) 
                {
                    return f.Name.Length + + f.FullName.Length + f.Data.Length;
                }";

            var analyser = new MethodCallAnalyzer();

            analyser.AddToWhiteList("string.Length");
            analyser.AddToWhiteList("System.Array.Length");

            var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.InvocationExpression));

            Assert.AreEqual(0, info.Allocations.Count);
        }
        public void AnalyzeProgram_NotAllowCallingExternalMethod_UnlessItIsWhitelisted()
        {
            //language=cs
            const string sample =
                @"using System;
                using HotPathAllocationAnalyzer.Support;

                [HotPathAllocationAnalyzer.Support.NoAllocation]
                public bool PerfCritical(string str) {
                    return str.Contains(""zig"");
                }";

            var analyser = new MethodCallAnalyzer();

            analyser.AddToWhiteList("string.IsNormalized()");
            analyser.AddToWhiteList("string.Contains(string)");

            var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.InvocationExpression, SyntaxKind.ClassDeclaration));
            Assert.AreEqual(0, info.Allocations.Count);
        }
        public void AnalyzeProgram_NotAllowCallingNonGenericWhitelistedMethods()
        {
            //language=cs
            const string sample =
                @"
                    using System;
                    using System.Collections.Generic;
                    using HotPathAllocationAnalyzer.Support;

                    [HotPathAllocationAnalyzer.Support.NoAllocation]
                    public int PerfCritical<T>(List<T> l, T val) {
                        return l.IndexOf(val);
                    }
                ";

            var analyser = new MethodCallAnalyzer();
            analyser.AddToWhiteList("System.Collections.Generic.List<double>.IndexOf(double)");

            var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.InvocationExpression));
            Assert.AreEqual(1, info.Allocations.Count);
        }
Ejemplo n.º 4
0
        public void AnalyzeProgram_AllowCallingGenericWhitelistedProperty()
        {
            //language=cs
            const string sample =
                @"using System;
                using HotPathAllocationAnalyzer;

                public class DateProvider
                {
                    public DateTime? Date { [HotPathAllocationAnalyzer.Support.NoAllocation] get; }
                }

                [HotPathAllocationAnalyzer.Support.NoAllocation]
                public DateTime PerfCritical(DateProvider dp) {
                    return dp.Date.Value;
                }";

            var analyser = new MethodCallAnalyzer();

            analyser.AddToWhiteList("System.Nullable<T>.Value");
            var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.InvocationExpression));

            Assert.AreEqual(0, info.Allocations.Count);
        }