Skip to content

Commit

Permalink
Merge pull request #276 from adrianoc/staging
Browse files Browse the repository at this point in the history
March 2024 update
  • Loading branch information
adrianoc authored Mar 29, 2024
2 parents c8fa196 + 9e293c8 commit d66833d
Show file tree
Hide file tree
Showing 75 changed files with 1,164 additions and 485 deletions.
2 changes: 1 addition & 1 deletion Cecilifier.Common.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>12</LangVersion>
<AssemblyVersion>2.8.0</AssemblyVersion>
<AssemblyVersion>2.9.0</AssemblyVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
3 changes: 2 additions & 1 deletion Cecilifier.CommonPackages.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp"><Version>4.8.0</Version></PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0-2.final" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.10.0-2.final" />
<PackageReference Include="Mono.Cecil"><Version>0.11.5</Version></PackageReference>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions Cecilifier.Core.Tests/Cecilifier.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
<Import Project="../Cecilifier.Common.props" />
<Import Project="../Cecilifier.CommonPackages.props" />
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="NUnit" Version="4.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,11 @@ private static bool ValidateOperands(Instruction instruction, Instruction curren
if (frInstruction.Name != frCurrent.Name || frInstruction.DeclaringType.FullName != frCurrent.DeclaringType.FullName)
return false;

if (scopesToIgnore.Contains(frInstruction.DeclaringType.Scope) || scopesToIgnore.Contains(frInstruction.DeclaringType.Scope))
if (scopesToIgnore.Contains(frInstruction.DeclaringType.Scope) || scopesToIgnore.Contains(frCurrent.DeclaringType.Scope))
return true;

return (frInstruction.DeclaringType.Scope.Name == frCurrent.DeclaringType.Scope.Name ||
frInstruction.DeclaringType.Scope.Name == "System.Private.CoreLib" && frCurrent.DeclaringType.Scope.Name == "netstandard");
frInstruction.DeclaringType.Scope.Name == "System.Private.CoreLib" && frCurrent.DeclaringType.Scope.Name == "System.Runtime");
}

return true;
Expand Down
15 changes: 8 additions & 7 deletions Cecilifier.Core.Tests/Framework/ResourceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Text;
using Cecilifier.Core.Misc;
using Cecilifier.Core.Naming;
using Cecilifier.Core.Tests.Framework.AssemblyDiff;
using Cecilifier.Runtime;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected void AssertResourceTestWithExplicitExpectation(ResourceTestOptions opt
AssertResourceTestWithExplicitExpectedIL(actualAssemblyPath, expectedIL, methodSignature, options);

Console.WriteLine();
Console.WriteLine("Expected IL: {0}", expectedIL);
Console.WriteLine("Expected IL:\n{0}", expectedIL);
Console.WriteLine("Actual assembly path : {0}", actualAssemblyPath);
}

Expand Down Expand Up @@ -101,8 +102,8 @@ private static string CompileExpectedTestAssembly(string cecilifiedAssemblyPath,
var targetPath = Path.Combine(Path.GetDirectoryName(cecilifiedAssemblyPath), Path.GetFileNameWithoutExtension(cecilifiedAssemblyPath) + "_expected");

return buildType == BuildType.Exe
? CompilationServices.CompileExe(targetPath, tbc, Utils.GetTrustedAssembliesPath().ToArray())
: CompilationServices.CompileDLL(targetPath, tbc, Utils.GetTrustedAssembliesPath().ToArray());
? CompilationServices.CompileExe(targetPath, tbc, ReferencedAssemblies.GetTrustedAssembliesPath())
: CompilationServices.CompileDLL(targetPath, tbc, ReferencedAssemblies.GetTrustedAssembliesPath());
}

private void AssertResourceTest(string actualAssemblyPath, string expectedAssemblyPath, ResourceTestOptions options)
Expand Down Expand Up @@ -181,7 +182,7 @@ private void CecilifyAndExecute(Stream tbc, string outputAssemblyPath)
{
cecilifiedCode = Cecilfy(tbc);

var references = Utils.GetTrustedAssembliesPath().Where(a => !a.Contains("mscorlib"));
var references = ReferencedAssemblies.GetTrustedAssembliesPath().Where(a => !a.Contains("mscorlib"));
var refsToCopy = new List<string>
{
typeof(ILParser).Assembly.Location,
Expand Down Expand Up @@ -213,8 +214,8 @@ private void CopyFilesNextToGeneratedExecutable(string cecilifierRunnerPath, Lis
File.Copy(fileToCopy, Path.Combine(targetPath, Path.GetFileName(fileToCopy)), true);
}

var sourceRuntimeConfigJson = Path.ChangeExtension(GetType().Assembly.Location, ".runtimeconfig.json");
var targetRuntimeConfigJson = Path.ChangeExtension(cecilifierRunnerPath, ".runtimeconfig.json");
var sourceRuntimeConfigJson = Path.ChangeExtension(GetType().Assembly.Location, Constants.Common.RuntimeConfigJsonExt);
var targetRuntimeConfigJson = Path.ChangeExtension(cecilifierRunnerPath, Constants.Common.RuntimeConfigJsonExt);

File.Copy(sourceRuntimeConfigJson, targetRuntimeConfigJson, true);
}
Expand Down Expand Up @@ -261,7 +262,7 @@ private Stream ReadResource(string path)
private string Cecilfy(Stream stream)
{
stream.Position = 0;
return Cecilifier.Process(stream, new CecilifierOptions { References = Utils.GetTrustedAssembliesPath(), Naming = new DefaultNameStrategy() }).GeneratedCode.ReadToEnd();
return Cecilifier.Process(stream, new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath(), Naming = new DefaultNameStrategy() }).GeneratedCode.ReadToEnd();
}
}
}
15 changes: 0 additions & 15 deletions Cecilifier.Core.Tests/Framework/Utils.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

class MethodInvocationOnGenericParameter
{
string[] M() => Array.Empty<string>();

int Find(int[] data) => Array.BinarySearch(data, 42);

int Find<T>(T[] data) => Array.BinarySearch(data, default(T));

void CallPartialyClosedMethod<T>(T t) => M2(t, 42);
void M2<S, R>(S s, R r) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Cecilifier.Core.Tests.TestResources.Integration.Generics;

public class GenericMethodInvocation
{
static bool CallM()
{
return M(new int[] {1, 2, 3} , 2);
}

static bool M<T>(T[] ts, T t)
{
var r = t.Equals(t);
r = ts[0].Equals(t);

return r;
}
}
12 changes: 0 additions & 12 deletions Cecilifier.Core.Tests/Tests/Integration/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ public void NestedIfStatementTest()
AssertResourceTestWithExplicitExpectation(@"CodeBlock/Conditional/NestedIfStatement", "System.Void NestedIfStatement::Foo(System.Int32)");
}

[Test]
public void NonVirtualMethodCallTest()
{
AssertResourceTest(@"CodeBlock/MethodCall/NonVirtualMethodCall");
}

[Test]
public void OutVariablesTest()
{
AssertResourceTest(@"CodeBlock/MethodCall/OutVariables");
}

[Test]
[Ignore("Not Implemented yet")]
public void NullCoalescingTest()
Expand Down
12 changes: 12 additions & 0 deletions Cecilifier.Core.Tests/Tests/Integration/GenericsTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ public void TestGenericExplicitStaticMethods()
AssertResourceTest(@"Generics/StaticExplicitMethods");
}

[Test]
public void TestGenericMethodInstanceFromAssembly()
{
AssertResourceTest("Generics/GenericMethodsFromAssembly");
}

[Test, Ignore(reason:"Known Issue: #263")]
public void TestMethodInvocationOnGenericParameter()
{
AssertResourceTest("Generics/MethodInvocationOnGenericParameter");
}

[Test]
public void TestGenericTypesAsMembers()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Cecilifier.Core.Misc;
using Cecilifier.Core.Tests.Framework;
using NUnit.Framework;

Expand All @@ -14,7 +16,7 @@ public void InvalidIdentifier()
var codeString = "class C { void F(int i) { sitch(i) {} } }";
using (var code = new MemoryStream(Encoding.ASCII.GetBytes(codeString)))
{
Assert.Throws<SyntaxErrorException>(() => Cecilifier.Process(code, new CecilifierOptions { References = Utils.GetTrustedAssembliesPath() }).GeneratedCode.ReadToEnd());
Assert.Throws<SyntaxErrorException>(() => Cecilifier.Process(code, new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath() }).GeneratedCode.ReadToEnd());
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions Cecilifier.Core.Tests/Tests/Integration/MethodTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,22 @@ public void TestRefProperties()
{
AssertResourceTest("Members/Methods/RefProperties");
}

[Test]
public void OutVariablesTest()
{
AssertResourceTest("Members/Methods/OutVariables");
}

[Test]
public void TestOverloads()
{
AssertResourceTest("Members/Methods/Overloads");
}
[Test]
public void NonVirtualMethodCallTest()
{
AssertResourceTest("Members/Methods/Invocation/NonVirtualMethodCall");
}
}
}
4 changes: 2 additions & 2 deletions Cecilifier.Core.Tests/Tests/Integration/MiscTestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MiscTestCase : ResourceTestBase
[TestCase("OnFields", Ignore = "Generates invalid code. See https://github.com/adrianoc/cecilifier/issues/61")]
public void TestDelegateInvocation(string storageType)
{
AssertResourceTest($@"Misc/DelegateInvocation_{storageType}");
AssertResourceTest($"Misc/DelegateInvocation_{storageType}");
}

[Test]
Expand All @@ -24,7 +24,7 @@ public void TestAccessibilityModifiers()
[Test]
public void TestNamespaces()
{
AssertResourceTest(@"Misc/Namespaces");
AssertResourceTest("Misc/Namespaces");
}

[TestCase("AddressOfParameters", TestName = "Parameters")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;
using System.Text;
using Cecilifier.Core.Tests.Framework;
using Cecilifier.Core.Misc;
using NUnit.Framework;

namespace Cecilifier.Core.Tests.Integration
Expand All @@ -16,7 +16,7 @@ public class UnsupportedFeaturesTestCase
public void EnumeratorBlocks(string statement)
{
var code = new MemoryStream(Encoding.ASCII.GetBytes($"class Test {{ System.Collections.IEnumerable Do() {{ {statement}; }} }} "));
using (var stream = Cecilifier.Process(code, new CecilifierOptions { References = Utils.GetTrustedAssembliesPath() }).GeneratedCode)
using (var stream = Cecilifier.Process(code, new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath() }).GeneratedCode)
{
var cecilifiedCode = stream.ReadToEnd();
Assert.That(cecilifiedCode, Does.Match("Syntax 'Yield(Return|Break)Statement' is not supported"));
Expand All @@ -41,7 +41,7 @@ public void TestStatements(string statement, string expectedInError)
private static void AssertUnsupportedFeature(string codeString, string expectedMessage)
{
var code = new MemoryStream(Encoding.ASCII.GetBytes(codeString));
using (var stream = Cecilifier.Process(code, new CecilifierOptions { References = Utils.GetTrustedAssembliesPath() }).GeneratedCode)
using (var stream = Cecilifier.Process(code, new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath() }).GeneratedCode)
{
var cecilifiedCode = stream.ReadToEnd();
Assert.That(cecilifiedCode, Contains.Substring(expectedMessage));
Expand Down
2 changes: 1 addition & 1 deletion Cecilifier.Core.Tests/Tests/Unit/ArrayTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void InitializationOptimized(string code)
\s+assembly.MainModule.Types.Add\(\1\);
\s+//__StaticArrayInitTypeSize=12 struct.
\s+//This struct is emitted by the compiler and is used to hold raw data used in arrays/span initialization optimizations
\s+var (st_rawDataTypeVar_\d+) = new TypeDefinition\("", "__StaticArrayInitTypeSize=12", TypeAttributes.NestedPrivate \| TypeAttributes.Sealed \| TypeAttributes.AnsiClass \| TypeAttributes.ExplicitLayout.+\) \{ ClassSize = 12,PackingSize = 1 \};
\s+var (st_rawDataTypeVar_\d+) = new TypeDefinition\("", "__StaticArrayInitTypeSize=12", TypeAttributes.NestedAssembly \| TypeAttributes.Sealed \| TypeAttributes.AnsiClass \| TypeAttributes.ExplicitLayout.+\) \{ ClassSize = 12,PackingSize = 1 \};
\s+\1.NestedTypes.Add\(\2\);
\s+var (fld_arrayInitializerData_\d+) = new FieldDefinition\("[A-Z0-9]+", FieldAttributes.Assembly \| FieldAttributes.Static \| FieldAttributes.InitOnly, \2\);
\s+\1.Fields.Add\(\3\);
Expand Down
4 changes: 3 additions & 1 deletion Cecilifier.Core.Tests/Tests/Unit/CecilifiedOutputTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using Cecilifier.Core.Misc;
using Cecilifier.Core.Tests.Framework;
using NUnit.Framework;

Expand Down Expand Up @@ -83,7 +85,7 @@ public void LocalVariableDeclarations_Are_CommentedOut()

private void AssertCecilifiedCodeContainsSnippet(string code, string expectedSnippet)
{
var cecilifier = Cecilifier.Process(new MemoryStream(Encoding.UTF8.GetBytes(code)), new CecilifierOptions { References = Utils.GetTrustedAssembliesPath() });
var cecilifier = Cecilifier.Process(new MemoryStream(Encoding.UTF8.GetBytes(code)), new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath() });
var generated = cecilifier.GeneratedCode.ReadToEnd();

Assert.That(generated, Does.Contain(expectedSnippet), "Expected snippet not found");
Expand Down
14 changes: 13 additions & 1 deletion Cecilifier.Core.Tests/Tests/Unit/CecilifierUnitTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using Cecilifier.Core.Misc;
using Cecilifier.Core.Naming;
using Cecilifier.Core.Tests.Framework;
using NUnit.Framework;

namespace Cecilifier.Core.Tests.Tests.Unit
{
Expand All @@ -13,7 +17,15 @@ protected static CecilifierResult RunCecilifier(string code)
memoryStream.Write(System.Text.Encoding.ASCII.GetBytes(code));
memoryStream.Position = 0;

return Cecilifier.Process(memoryStream, new CecilifierOptions { References = Utils.GetTrustedAssembliesPath(), Naming = nameStrategy });
try
{
return Cecilifier.Process(memoryStream, new CecilifierOptions { References = ReferencedAssemblies.GetTrustedAssembliesPath(), Naming = nameStrategy });
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
throw;
}
}
}
}
1 change: 0 additions & 1 deletion Cecilifier.Core.Tests/Tests/Unit/ConstTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Text.RegularExpressions;
using Mono.Cecil.Cil;
using NuGet.Frameworks;
using NUnit.Framework;

namespace Cecilifier.Core.Tests.Tests.Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,20 @@ public void ArrayElementAssignment(string arrayVariable)
[TestCaseSource(nameof(InvocationOnObjectCreationExpressionTestScenarios))]
public void InvocationOnObjectCreationExpression(string invocationStatement, string expectedILSnippet)
{
var result = RunCecilifier($$"""
using System;
{{invocationStatement}};
struct Test : IDisposable
{
public Test(int i) {}
public void M() {}
public void Dispose() {}
}
""");

Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match(expectedILSnippet));
var code = $$"""
using System;
{{invocationStatement}};
struct Test : IDisposable
{
public Test(int i) {}
public void M() {}
public void Dispose() {}
}
""";
var result = RunCecilifier(code);
Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match(expectedILSnippet), $"Snippet:\n{code}");
}

[TestCaseSource(nameof(InvocationExpressionAsParametersTestScenarios))]
Expand Down Expand Up @@ -264,7 +264,7 @@ static TestCaseData[] InvocationOnObjectCreationExpressionTestScenarios()
\s+\1Constrained, st_test_\d+\);
\s+\1Callvirt, .+"GetHashCode".+\);
\s+\1Pop\);
""").SetName("Explicit: direct object method call"), // Only missing constrained
""").SetName("Explicit: direct object method call"),

new TestCaseData(
"((object)new Test(1)).GetHashCode()",
Expand Down
Loading

0 comments on commit d66833d

Please sign in to comment.