public void AnalyzeProgram_TakesIgnoredFilesIntoAccount() { const string sampleProgram = @"using System; public void CreateString() { string str = new string('a', 5); }"; var analyser = new ExplicitAllocationAnalyzer(); void Check(int expectedCount, string path) { var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression), filePath: path); Assert.AreEqual(expectedCount, info.Allocations.Count); } AllocationRules.Settings.IgnoredFilesPatterns = ""; // Explicitly set to remove any default. Check(1, "test.g.cs"); AllocationRules.Settings.IgnoredFilesPatterns = "*.g.cs"; Check(0, "test.g.cs"); AllocationRules.Settings.IgnoredFilesPatterns = "*.g.cs, *.cpp"; Check(0, "test.g.cs"); Check(0, "test.cpp"); AllocationRules.Settings.IgnoredFilesPatterns = "*.?.cs"; Check(1, "test.cpp"); Check(0, "test.g.cs"); Check(0, "test.a.cs"); }
public void AnalyzeProgram_TakesIgnoredAttributesIntoAccount() { const string sampleProgram = @"using System; [System.Runtime.CompilerServices.CompilerGeneratedAttribute] public void CreateString1() { string str = new string('a', 5); } [System.CodeDom.Compiler.GeneratedCodeAttribute(""MyCompiler"", ""1.0.0.3"")] public void CreateString2() { string str = new string('a', 5); } [System.ObsoleteAttribute] public void CreateString3() { string str = new string('a', 5); }"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(1, info.Allocations.Count); }
public void AnalyzeProgram_TakesIgnoredAttributesIntoAccount() { const string sampleProgram = @"using System; [System.Runtime.CompilerServices.CompilerGeneratedAttribute] public void CreateString1() { string str = new string('a', 5); } [System.ObsoleteAttribute] public void CreateString2() { string str = new string('a', 5); }"; var analyser = new ExplicitAllocationAnalyzer(); AllocationRules.Settings.IgnoredAttributes = ""; // Explicitly set to remove any default. var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(2, info.Allocations.Count); AllocationRules.Settings.IgnoredAttributes = "System.Runtime.CompilerServices.CompilerGeneratedAttribute"; info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(1, info.Allocations.Count); AllocationRules.Settings.IgnoredAttributes = "System.Runtime.CompilerServices.CompilerGeneratedAttribute, System.ObsoleteAttribute"; info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(0, info.Allocations.Count); }
public void ExplicitAllocation_InitializerExpressionSyntax() { var sampleProgram = @"using System; var @struct = new TestStruct { Name = ""Bob"" }; var @class = new TestClass { Name = ""Bob"" }; public struct TestStruct { public string Name { get; set; } } public class TestClass { public string Name { get; set; } }"; var analyser = new ExplicitAllocationAnalyzer(); // SyntaxKind.ObjectInitializerExpression IS linked to InitializerExpressionSyntax (naming is a bit confusing) var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(2, info.Allocations.Count); // Diagnostic: (4,14): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewObjectRule.Id, line: 4, character: 14); // Diagnostic: (4,5): info HeapAnalyzerInitializerCreationRule: Initializer reference type allocation *** AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.InitializerCreationRule.Id, line: 4, character: 5); }
public void InitializeHotPathTests() { analyzer = new ExplicitAllocationAnalyzer(); AllocationRules.Settings.IgnoredAttributes = ""; // Explicitly set to remove any default. AllocationRules.Settings.HotPathAttributes = ""; // Explicitly set to remove any default. AllocationRules.Settings.OnlyReportOnHotPath = false; }
public void ExplicitAllocation_ObjectCreationExpressionSyntax2() { var sampleProgram = @"using System; using System.Collections.Generic; using HotPathAllocationAnalyzer.Support; class Foo { [HotPathAllocationAnalyzer.Support.NoAllocation] private static void Bar() { var data = new DateTime(); } [HotPathAllocationAnalyzer.Support.NoAllocation] private static void Bis() { var data = new List<int>(); } } "; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression)); Assert.AreEqual(1, info.Allocations.Count); // Diagnostic: (3,18): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: ExplicitAllocationAnalyzer.NewObjectRule.Id, line: 16, character: 24); }
public void AnalyzeProgram_AnalyzeImplementationWhenBaseClassHasRestrictedAttributes() { //language=cs const string sample = @"using System; using HotPathAllocationAnalyzer.Support; [HotPathAllocationAnalyzer.Support.NoAllocation] public class FooBase { public virtual void CreateString() {} } public class Foo : FooBase { public override void CreateString() { string str = new string('a', 5); } }"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(1, info.Allocations.Count); }
public void AnalyzeProgram_AnalyzeExplicitImplementationWhenInterfaceHasRestrictedAttributes() { //language=cs const string sample = @"using System; using HotPathAllocationAnalyzer.Support; interface IFoo { [HotPathAllocationAnalyzer.Support.NoAllocation] void CreateString(); } class Foo : IFoo { void IFoo.CreateString() { string str = new string('a', 5); } }"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(1, info.Allocations.Count); }
public void ExplicitAllocation_AllSyntax() { var sampleProgram = @"using System; using System.Collections.Generic; using System.Linq; var @struct = new TestStruct { Name = ""Bob"" }; var @class = new TestClass { Name = ""Bob"" }; int[] intDataImplicit = new[] { 123, 32, 4 }; var temp = new { A = 123, Name = ""Test"", }; int[] intDataExplicit = new int[] { 123, 32, 4 }; var allocation = new String('a', 10); var noAllocation = new DateTime(); int[] intDataLinq = new int[] { 123, 32, 4 }; var result = (from a in intDataLinq let b = a * 3 select b).ToList(); public struct TestStruct { public string Name { get; set; } } public class TestClass { public string Name { get; set; } }"; // This test is here so that we use SyntaxKindsOfInterest explicitly, to make sure it works var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression, SyntaxKind.AnonymousObjectCreationExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression, SyntaxKind.ComplexElementInitializerExpression, SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.LetClause)); Assert.AreEqual(8, info.Allocations.Count); // Diagnostic: (6,14): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewObjectRule.Id, line: 6, character: 14); // Diagnostic: (6,5): info HeapAnalyzerInitializerCreationRule: Initializer reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.InitializerCreationRule.Id, line: 6, character: 5); // Diagnostic: (8,25): info HeapAnalyzerImplicitNewArrayCreationRule: Implicit new array creation allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.ImplicitArrayCreationRule.Id, line: 8, character: 25); // Diagnostic: (10,12): info HeapAnalyzerExplicitNewAnonymousObjectRule: Explicit new anonymous object allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.AnonymousNewObjectRule.Id, line: 10, character: 12); // Diagnostic: (12,25): info HeapAnalyzerExplicitNewArrayRule: Explicit new array type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewArrayRule.Id, line: 12, character: 25); // Diagnostic: (14,18): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewObjectRule.Id, line: 14, character: 18); // Diagnostic: (17,21): info HeapAnalyzerExplicitNewArrayRule: Explicit new array type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewArrayRule.Id, line: 17, character: 21); // Diagnostic: (19,15): info HeapAnalyzerLetClauseRule: Let clause induced allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.LetCauseRule.Id, line: 19, character: 15); }
public void Converting_any_value_type_to_System_ValueType_type() { var @script = @"struct S { } System.ValueType box = new S();"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, @script, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression, SyntaxKind.AnonymousObjectCreationExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression, SyntaxKind.ComplexElementInitializerExpression, SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.LetClause)); Assert.Single(info.Allocations); // Diagnostic: (2,44): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, ExplicitAllocationAnalyzer.NewObjectRule.Id, line: 2, character: 44); }
public void Converting_any_value_type_into_interface_reference() { var @script = @"interface I { } struct S : I { } I box = new S();"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, @script, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression, SyntaxKind.AnonymousObjectCreationExpression, SyntaxKind.ArrayInitializerExpression, SyntaxKind.CollectionInitializerExpression, SyntaxKind.ComplexElementInitializerExpression, SyntaxKind.ObjectInitializerExpression, SyntaxKind.ArrayCreationExpression, SyntaxKind.ImplicitArrayCreationExpression, SyntaxKind.LetClause)); Assert.AreEqual(1, info.Allocations.Count); // Diagnostic: (3,25): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, ExplicitAllocationAnalyzer.NewObjectRule.Id, line: 3, character: 25); }
public void ExplicitAllocation_ArrayCreationExpressionSyntax() { var sampleProgram = @"using System.Collections.Generic; int[] intData = new int[] { 123, 32, 4 };"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ArrayCreationExpression)); Assert.AreEqual(1, info.Allocations.Count); // Diagnostic: (3,17): info HeapAnalyzerExplicitNewArrayRule: Implicit new array creation allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewArrayRule.Id, line: 3, character: 17); }
public void ExplicitAllocation_AnonymousObjectCreationExpressionSyntax() { var sampleProgram = @"using System; var temp = new { A = 123, Name = ""Test"", };"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.AnonymousObjectCreationExpression)); Assert.AreEqual(1, info.Allocations.Count); // Diagnostic: (3,12): info HeapAnalyzerExplicitNewAnonymousObjectRule: Explicit new anonymous object allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.AnonymousNewObjectRule.Id, line: 3, character: 12); }
public void ExplicitAllocation_ObjectCreationExpressionSyntax() { var sampleProgram = @"using System; var allocation = new String('a', 10); var noAllocation = new DateTime();"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression)); Assert.AreEqual(1, info.Allocations.Count); // Diagnostic: (3,18): info HeapAnalyzerExplicitNewObjectRule: Explicit new reference type allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.NewObjectRule.Id, line: 3, character: 18); }
public void AnalyzeProgram_IgnoreWhenThereIsNoRestrictedAttributes() { const string sample = @"using System; public void CreateString() { string str = new string('a', 5); }"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.ObjectInitializerExpression)); Assert.AreEqual(0, info.Allocations.Count); }
public void ExplicitAllocation_LetClauseSyntax() { var sampleProgram = @"using System.Collections.Generic; using System.Linq; int[] intData = new[] { 123, 32, 4 }; var result = (from a in intData let b = a * 3 select b).ToList(); "; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sampleProgram, ImmutableArray.Create(SyntaxKind.LetClause)); Assert.AreEqual(2, info.Allocations.Count); // Diagnostic: (4,17): info HeapAnalyzerImplicitNewArrayCreationRule: Implicit new array creation allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.ImplicitArrayCreationRule.Id, line: 4, character: 17); // Diagnostic: (6,15): info HeapAnalyzerLetClauseRule: Let clause induced allocation AssertEx.ContainsDiagnostic(info.Allocations, id: AllocationRules.LetCauseRule.Id, line: 6, character: 15); }
public void AnalyzeProgram_NotAllowCallingUnSafePropertyGetter() { //language=cs const string sample = @"using System; using System.Collections.Generic; using HotPathAllocationAnalyzer; public class Foo { public List<int> Data { [HotPathAllocationAnalyzer.Support.NoAllocation] get { return new List<int>(); } } } [HotPathAllocationAnalyzer.Support.NoAllocation] public List<int> PerfCritical(Foo foo) { return foo.Data; }"; var analyser = new ExplicitAllocationAnalyzer(); var info = ProcessCode(analyser, sample, ImmutableArray.Create(SyntaxKind.ObjectCreationExpression)); Assert.AreEqual(1, info.Allocations.Count); }