Пример #1
0
#pragma warning restore
        public async Task AppendingUnrelatedSource_DoesNotRegenerateSource()
        {
            string source = $"namespace NS{{{CodeSnippets.BasicParametersAndModifiers<int>()}}}";

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview));

            Compilation comp1 = await TestUtils.CreateCompilation(new[] { syntaxTree });

            Microsoft.Interop.DllImportGenerator generator = new();
            GeneratorDriver driver = TestUtils.CreateDriver(comp1, null, new[] { generator });

            driver = driver.RunGenerators(comp1);

            generator.IncrementalTracker = new IncrementalityTracker();

            SyntaxTree newTree = syntaxTree.WithRootAndOptions(syntaxTree.GetCompilationUnitRoot().AddMembers(SyntaxFactory.ParseMemberDeclaration("struct Foo {}") !), syntaxTree.Options);

            Compilation comp2 = comp1.ReplaceSyntaxTree(comp1.SyntaxTrees.First(), newTree);

            driver.RunGenerators(comp2);

            Assert.Collection(generator.IncrementalTracker.ExecutedSteps,
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.CalculateStubInformation, step.Step);
            });
        }
Пример #2
0
#pragma warning restore
        public async Task ChangingMarshallingAttributes_SameStrategy_DoesNotRegenerate()
        {
            string source = CodeSnippets.BasicParametersAndModifiers <bool>();

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview));

            Compilation comp1 = await TestUtils.CreateCompilation(new[] { syntaxTree });

            Microsoft.Interop.DllImportGenerator generator = new();
            GeneratorDriver driver = TestUtils.CreateDriver(comp1, null, new[] { generator });

            driver = driver.RunGenerators(comp1);

            generator.IncrementalTracker = new IncrementalityTracker();

            SyntaxTree newTree = syntaxTree.WithRootAndOptions(
                syntaxTree.GetCompilationUnitRoot().AddMembers(
                    SyntaxFactory.ParseMemberDeclaration(
                        CodeSnippets.MarshalAsParametersAndModifiers <bool>(System.Runtime.InteropServices.UnmanagedType.Bool)) !),
                syntaxTree.Options);

            Compilation comp2 = comp1.ReplaceSyntaxTree(comp1.SyntaxTrees.First(), newTree);

            driver.RunGenerators(comp2);

            Assert.Collection(generator.IncrementalTracker.ExecutedSteps,
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.CalculateStubInformation, step.Step);
            },
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.GenerateSingleStub, step.Step);
            });
        }
Пример #3
0
        public static IEnumerable <object[]> CodeSnippetsToCompileMultipleSources()
        {
            yield return(new object[] { new[] { CodeSnippets.BasicParametersAndModifiers <int>(), CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.Bool) } });

            yield return(new object[] { new[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <int>(CharSet.Unicode), CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.Bool) } });

            yield return(new object[] { new[] { CodeSnippets.BasicParameterByValue("int[]"), CodeSnippets.BasicParameterWithByRefModifier("ref", "int") } });
        }
Пример #4
0
        public static IEnumerable <object[]> SnippetsWithBlittableTypesButNonBlittableDataToCompile()
        {
            yield return(new[] { CodeSnippets.AllGeneratedDllImportNamedArguments });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <int>() });
        }
Пример #5
0
        public static IEnumerable <object[]> CodeSnippetsToCompileMultipleSources()
        {
            yield return(new object[] { new[] { CodeSnippets.BasicParametersAndModifiers <int>(), CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.Bool) } });

            yield return(new object[] { new[] { CodeSnippets.BasicParametersAndModifiersWithStringMarshalling <int>(StringMarshalling.Utf16), CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.Bool) } });

            yield return(new object[] { new[] { CodeSnippets.BasicParameterByValue("int[]", CodeSnippets.DisableRuntimeMarshalling), CodeSnippets.BasicParameterWithByRefModifier("ref", "int") } });
        }
Пример #6
0
        public static IEnumerable <object[]> CodeSnippetsToCompileWithForwarder()
        {
            yield return(new[] { CodeSnippets.UserDefinedEntryPoint });

            yield return(new[] { CodeSnippets.AllSupportedDllImportNamedArguments });

            // Parameter / return types (supported in DllImportGenerator)
            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <byte>() });

            // Parameter / return types (not supported in DllImportGenerator)
            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <string>() });
        }
Пример #7
0
        public async Task ChangingMarshallingStrategy_RegeneratesStub()
        {
            string stubSource = CodeSnippets.BasicParametersAndModifiers("CustomType");

            string customTypeImpl1 = "struct CustomType { System.IntPtr handle; }";

            string customTypeImpl2 = "class CustomType : Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid { public CustomType():base(true){} protected override bool ReleaseHandle(){return true;} }";


            Compilation comp1 = await TestUtils.CreateCompilation(stubSource);

            SyntaxTree customTypeImpl1Tree = CSharpSyntaxTree.ParseText(customTypeImpl1, new CSharpParseOptions(LanguageVersion.Preview));

            comp1 = comp1.AddSyntaxTrees(customTypeImpl1Tree);

            Microsoft.Interop.DllImportGenerator generator = new();
            GeneratorDriver driver = TestUtils.CreateDriver(comp1, null, new[] { generator });

            driver = driver.RunGenerators(comp1);

            generator.IncrementalTracker = new IncrementalityTracker();

            Compilation comp2 = comp1.ReplaceSyntaxTree(customTypeImpl1Tree, CSharpSyntaxTree.ParseText(customTypeImpl2, new CSharpParseOptions(LanguageVersion.Preview)));

            driver.RunGenerators(comp2);

            Assert.Collection(generator.IncrementalTracker.ExecutedSteps,
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.CalculateStubInformation, step.Step);
            },
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.GenerateSingleStub, step.Step);
            },
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.NormalizeWhitespace, step.Step);
            },
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.ConcatenateStubs, step.Step);
            },
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.OutputSourceFile, step.Step);
            });
        }
Пример #8
0
        public static IEnumerable <object[]> CodeSnippetsToCompileWithPreprocessorSymbols()
        {
            yield return(new object[] { CodeSnippets.PreprocessorIfAroundFullFunctionDefinition("Foo"), new string[] { "Foo" } });

            yield return(new object[] { CodeSnippets.PreprocessorIfAroundFullFunctionDefinition("Foo"), Array.Empty <string>() });

            yield return(new object[] { CodeSnippets.PreprocessorIfAroundFullFunctionDefinitionWithFollowingFunction("Foo"), new string[] { "Foo" } });

            yield return(new object[] { CodeSnippets.PreprocessorIfAroundFullFunctionDefinitionWithFollowingFunction("Foo"), Array.Empty <string>() });

            yield return(new object[] { CodeSnippets.PreprocessorIfAfterAttributeAroundFunction("Foo"), new string[] { "Foo" } });

            yield return(new object[] { CodeSnippets.PreprocessorIfAfterAttributeAroundFunction("Foo"), Array.Empty <string>() });

            yield return(new object[] { CodeSnippets.PreprocessorIfAfterAttributeAroundFunctionAdditionalFunctionAfter("Foo"), new string[] { "Foo" } });

            yield return(new object[] { CodeSnippets.PreprocessorIfAfterAttributeAroundFunctionAdditionalFunctionAfter("Foo"), Array.Empty <string>() });
        }
Пример #9
0
        public static IEnumerable <object[]> CodeSnippetsToValidateFallbackForwarder()
        {
            yield return(new object[] { CodeSnippets.UserDefinedEntryPoint, TestTargetFramework.Net, true });

            // Confirm that all unsupported target frameworks can be generated.
            {
                string code = CodeSnippets.BasicParametersAndModifiers <byte>(CodeSnippets.GeneratedDllImportAttributeDeclaration);
                yield return(new object[] { code, TestTargetFramework.Net5, false });

                yield return(new object[] { code, TestTargetFramework.Core, false });

                yield return(new object[] { code, TestTargetFramework.Standard, false });

                yield return(new object[] { code, TestTargetFramework.Framework, false });
            }

            // Confirm that all unsupported target frameworks fallback to a forwarder.
            {
                string code = CodeSnippets.BasicParametersAndModifiers <byte[]>(CodeSnippets.GeneratedDllImportAttributeDeclaration);
                yield return(new object[] { code, TestTargetFramework.Net5, true });

                yield return(new object[] { code, TestTargetFramework.Core, true });

                yield return(new object[] { code, TestTargetFramework.Standard, true });

                yield return(new object[] { code, TestTargetFramework.Framework, true });
            }

            // Confirm that all unsupported target frameworks fallback to a forwarder.
            {
                string code = CodeSnippets.BasicParametersAndModifiersWithCharSet <string>(CharSet.Unicode, CodeSnippets.GeneratedDllImportAttributeDeclaration);
                yield return(new object[] { code, TestTargetFramework.Net5, true });

                yield return(new object[] { code, TestTargetFramework.Core, true });

                yield return(new object[] { code, TestTargetFramework.Standard, true });

                yield return(new object[] { code, TestTargetFramework.Framework, true });
            }
        }
Пример #10
0
        public async Task AddingFileWithNewGeneratedDllImport_DoesNotRegenerateOriginalMethod()
        {
            string source = CodeSnippets.BasicParametersAndModifiers <int>();

            Compilation comp1 = await TestUtils.CreateCompilation(source);

            Microsoft.Interop.DllImportGenerator generator = new();
            GeneratorDriver driver = TestUtils.CreateDriver(comp1, null, new[] { generator });

            driver = driver.RunGenerators(comp1);

            generator.IncrementalTracker = new IncrementalityTracker();

            Compilation comp2 = comp1.AddSyntaxTrees(CSharpSyntaxTree.ParseText(CodeSnippets.BasicParametersAndModifiers <bool>(), new CSharpParseOptions(LanguageVersion.Preview)));

            driver.RunGenerators(comp2);

            Assert.Equal(2, generator.IncrementalTracker.ExecutedSteps.Count(s => s.Step == IncrementalityTracker.StepName.CalculateStubInformation));
            Assert.Equal(1, generator.IncrementalTracker.ExecutedSteps.Count(s => s.Step == IncrementalityTracker.StepName.GenerateSingleStub));
            Assert.Equal(1, generator.IncrementalTracker.ExecutedSteps.Count(s => s.Step == IncrementalityTracker.StepName.NormalizeWhitespace));
            Assert.Equal(1, generator.IncrementalTracker.ExecutedSteps.Count(s => s.Step == IncrementalityTracker.StepName.ConcatenateStubs));
            Assert.Equal(1, generator.IncrementalTracker.ExecutedSteps.Count(s => s.Step == IncrementalityTracker.StepName.OutputSourceFile));
        }
Пример #11
0
        public async Task AddingNewUnrelatedType_DoesNotRegenerateSource()
        {
            string source = CodeSnippets.BasicParametersAndModifiers <int>();

            Compilation comp1 = await TestUtils.CreateCompilation(source);

            Microsoft.Interop.DllImportGenerator generator = new();
            GeneratorDriver driver = TestUtils.CreateDriver(comp1, null, new IIncrementalGenerator[] { generator });

            driver = driver.RunGenerators(comp1);

            generator.IncrementalTracker = new IncrementalityTracker();

            Compilation comp2 = comp1.AddSyntaxTrees(CSharpSyntaxTree.ParseText("struct Foo {}", new CSharpParseOptions(LanguageVersion.Preview)));

            driver.RunGenerators(comp2);

            Assert.Collection(generator.IncrementalTracker.ExecutedSteps,
                              step =>
            {
                Assert.Equal(IncrementalityTracker.StepName.CalculateStubInformation, step.Step);
            });
        }
Пример #12
0
        public static IEnumerable <object[]> FullyBlittableSnippetsToCompile()
        {
            yield return(new[] { CodeSnippets.UserDefinedEntryPoint });

            yield return(new[] { CodeSnippets.BasicParameterByValue("int") });
        }
Пример #13
0
        public static IEnumerable <object[]> CodeSnippetsToCompile()
        {
            yield return(new[] { CodeSnippets.TrivialClassDeclarations });

            yield return(new[] { CodeSnippets.TrivialStructDeclarations });

            yield return(new[] { CodeSnippets.MultipleAttributes });

            yield return(new[] { CodeSnippets.NestedNamespace });

            yield return(new[] { CodeSnippets.NestedTypes });

            yield return(new[] { CodeSnippets.UnsafeContext });

            yield return(new[] { CodeSnippets.UserDefinedEntryPoint });

            yield return(new[] { CodeSnippets.AllGeneratedDllImportNamedArguments });

            yield return(new[] { CodeSnippets.DefaultParameters });

            yield return(new[] { CodeSnippets.UseCSharpFeaturesForConstants });

            // Parameter / return types
            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <byte>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <sbyte>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <short>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <ushort>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <uint>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <long>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <ulong>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <float>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <double>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <bool>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <IntPtr>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiers <UIntPtr>() });

            // Arrays
            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <byte>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <sbyte>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <short>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <ushort>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <uint>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <long>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <ulong>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <float>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <double>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <bool>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <IntPtr>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParametersAndModifiers <UIntPtr>() });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <byte>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <sbyte>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <short>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <ushort>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <int>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <uint>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <long>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <ulong>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <IntPtr>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <UIntPtr>(isByRef: false) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <byte>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <sbyte>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <short>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <ushort>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <int>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <uint>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <long>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <ulong>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <IntPtr>(isByRef: true) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <UIntPtr>(isByRef: true) });

            // CharSet
            yield return(new[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <char>(CharSet.Unicode) });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <string>(CharSet.Unicode) });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <string>(CharSet.Ansi) });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <string>(CharSet.Auto) });

            // MarshalAs
            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.Bool) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.VariantBool) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <bool>(UnmanagedType.I1) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <char>(UnmanagedType.I2) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <char>(UnmanagedType.U2) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <IntPtr>(UnmanagedType.SysInt) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <UIntPtr>(UnmanagedType.SysUInt) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <string>(UnmanagedType.LPWStr) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <string>(UnmanagedType.LPTStr) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <string>(UnmanagedType.LPUTF8Str) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiers <string>(UnmanagedType.LPStr) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithNestedMarshalInfo <string>(UnmanagedType.LPWStr) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithNestedMarshalInfo <string>(UnmanagedType.LPUTF8Str) });

            yield return(new[] { CodeSnippets.MarshalAsArrayParameterWithNestedMarshalInfo <string>(UnmanagedType.LPStr) });

            // [In, Out] attributes
            // By value non-blittable array
            yield return(new[] { CodeSnippets.ByValueParameterWithModifier <bool[]>("Out") });

            yield return(new[] { CodeSnippets.ByValueParameterWithModifier <bool[]>("In, Out") });

            // Enums
            yield return(new[] { CodeSnippets.EnumParameters });

            // Pointers
            yield return(new[] { CodeSnippets.PointerParameters <byte>() });

            yield return(new[] { CodeSnippets.PointerParameters <sbyte>() });

            yield return(new[] { CodeSnippets.PointerParameters <short>() });

            yield return(new[] { CodeSnippets.PointerParameters <ushort>() });

            yield return(new[] { CodeSnippets.PointerParameters <int>() });

            yield return(new[] { CodeSnippets.PointerParameters <uint>() });

            yield return(new[] { CodeSnippets.PointerParameters <long>() });

            yield return(new[] { CodeSnippets.PointerParameters <ulong>() });

            yield return(new[] { CodeSnippets.PointerParameters <float>() });

            yield return(new[] { CodeSnippets.PointerParameters <double>() });

            yield return(new[] { CodeSnippets.PointerParameters <bool>() });

            yield return(new[] { CodeSnippets.PointerParameters <IntPtr>() });

            yield return(new[] { CodeSnippets.PointerParameters <UIntPtr>() });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersUnsafe("void*") });

            // Delegates
            yield return(new[] { CodeSnippets.DelegateParametersAndModifiers });

            yield return(new[] { CodeSnippets.DelegateMarshalAsParametersAndModifiers });

            // Function pointers
            yield return(new[] { CodeSnippets.BasicParametersAndModifiersUnsafe("delegate* <void>") });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersUnsafe("delegate* unmanaged<void>") });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersUnsafe("delegate* unmanaged<int, int>") });

            yield return(new[] { CodeSnippets.BasicParametersAndModifiersUnsafe("delegate* unmanaged[Stdcall]<int, int>") });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiersUnsafe("delegate* <int>", UnmanagedType.FunctionPtr) });

            yield return(new[] { CodeSnippets.MarshalAsParametersAndModifiersUnsafe("delegate* unmanaged<int>", UnmanagedType.FunctionPtr) });

            // Structs
            yield return(new[] { CodeSnippets.BlittableStructParametersAndModifiers });

            // SafeHandle
            yield return(new[] { CodeSnippets.BasicParametersAndModifiers("Microsoft.Win32.SafeHandles.SafeFileHandle") });

            yield return(new[] { CodeSnippets.BasicParameterByValue("System.Runtime.InteropServices.SafeHandle") });

            yield return(new[] { CodeSnippets.SafeHandleWithCustomDefaultConstructorAccessibility(privateCtor: false) });

            yield return(new[] { CodeSnippets.SafeHandleWithCustomDefaultConstructorAccessibility(privateCtor: true) });

            // PreserveSig
            yield return(new[] { CodeSnippets.PreserveSigFalseVoidReturn });

            yield return(new[] { CodeSnippets.PreserveSigFalse <byte>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <sbyte>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <short>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <ushort>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <int>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <uint>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <long>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <ulong>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <float>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <double>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <bool>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <IntPtr>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse <UIntPtr>() });

            yield return(new[] { CodeSnippets.PreserveSigFalse("Microsoft.Win32.SafeHandles.SafeFileHandle") });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <byte>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <sbyte>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <short>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <ushort>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <int>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <uint>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <long>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <ulong>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <float>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <double>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <bool>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <IntPtr>() });

            yield return(new[] { CodeSnippets.ArrayPreserveSigFalse <UIntPtr>() });

            // Custom type marshalling
            yield return(new[] { CodeSnippets.CustomStructMarshallingParametersAndModifiers });

            yield return(new[] { CodeSnippets.CustomStructMarshallingStackallocParametersAndModifiersNoRef });

            yield return(new[] { CodeSnippets.CustomStructMarshallingStackallocValuePropertyParametersAndModifiersNoRef });

            yield return(new[] { CodeSnippets.CustomStructMarshallingOptionalStackallocParametersAndModifiers });

            yield return(new[] { CodeSnippets.CustomStructMarshallingValuePropertyParametersAndModifiers });

            yield return(new[] { CodeSnippets.CustomStructMarshallingPinnableParametersAndModifiers });

            yield return(new[] { CodeSnippets.CustomStructMarshallingNativeTypePinnable });

            yield return(new[] { CodeSnippets.CustomStructMarshallingMarshalUsingParametersAndModifiers });

            yield return(new[] { CodeSnippets.ArrayMarshallingWithCustomStructElement });

            yield return(new[] { CodeSnippets.ArrayMarshallingWithCustomStructElementWithValueProperty });

            // Escaped C# keyword identifiers
            yield return(new[] { CodeSnippets.ByValueParameterWithName("Method", "@event") });

            yield return(new[] { CodeSnippets.ByValueParameterWithName("Method", "@var") });

            yield return(new[] { CodeSnippets.ByValueParameterWithName("@params", "i") });

            // Generics
            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <byte>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <sbyte>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <short>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <ushort>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <uint>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <long>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <ulong>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <float>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <double>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <IntPtr>() });

            yield return(new[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <UIntPtr>() });

            // Implicit blittable types
            yield return(new[] { CodeSnippets.ImplicitlyBlittableStructParametersAndModifiers() });

            yield return(new[] { CodeSnippets.ImplicitlyBlittableStructParametersAndModifiers("internal") });

            yield return(new[] { CodeSnippets.ImplicitlyBlittableGenericTypeParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.ImplicitlyBlittableGenericTypeParametersAndModifiers <int>("internal") });

            // Custom collection marshalling
            yield return(new[] { CodeSnippets.CollectionByValue <byte>() });

            yield return(new[] { CodeSnippets.CollectionByValue <sbyte>() });

            yield return(new[] { CodeSnippets.CollectionByValue <short>() });

            yield return(new[] { CodeSnippets.CollectionByValue <ushort>() });

            yield return(new[] { CodeSnippets.CollectionByValue <int>() });

            yield return(new[] { CodeSnippets.CollectionByValue <uint>() });

            yield return(new[] { CodeSnippets.CollectionByValue <long>() });

            yield return(new[] { CodeSnippets.CollectionByValue <ulong>() });

            yield return(new[] { CodeSnippets.CollectionByValue <float>() });

            yield return(new[] { CodeSnippets.CollectionByValue <double>() });

            yield return(new[] { CodeSnippets.CollectionByValue <bool>() });

            yield return(new[] { CodeSnippets.CollectionByValue <IntPtr>() });

            yield return(new[] { CodeSnippets.CollectionByValue <UIntPtr>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <byte[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <sbyte[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <short[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <ushort[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <int[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <uint[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <long[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <ulong[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <float[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <double[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <bool[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <IntPtr[]>() });

            yield return(new[] { CodeSnippets.MarshalUsingCollectionCountInfoParametersAndModifiers <UIntPtr[]>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <byte>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <sbyte>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <short>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <ushort>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <uint>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <long>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <ulong>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <float>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <double>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <bool>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <IntPtr>() });

            yield return(new[] { CodeSnippets.CustomCollectionDefaultMarshallerParametersAndModifiers <UIntPtr>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <byte>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <sbyte>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <short>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <ushort>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <int>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <uint>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <long>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <ulong>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <float>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <double>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <bool>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <IntPtr>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerParametersAndModifiers <UIntPtr>() });

            yield return(new[] { CodeSnippets.CustomCollectionCustomMarshallerReturnValueLength <int>() });

            yield return(new[] { CodeSnippets.GenericCollectionWithCustomElementMarshalling });

            yield return(new[] { CodeSnippets.CollectionsOfCollectionsStress });
        }
Пример #14
0
        public static IEnumerable <object[]> CodeSnippetsToCompile()
        {
            // Not GeneratedDllImportAttribute
            yield return(new object[] { CodeSnippets.UserDefinedPrefixedAttributes, 0, 3 });

            // No explicit marshalling for char or string
            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <char>(), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <string>(), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <char[]>(), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <string[]>(), 5, 0 });

            yield return(new object[] { CodeSnippets.PreserveSigFalse <char>(), 3, 0 });

            yield return(new object[] { CodeSnippets.PreserveSigFalse <string>(), 3, 0 });

            // Unsupported CharSet
            yield return(new object[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <char>(CharSet.Auto), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <char>(CharSet.Ansi), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <char>(CharSet.None), 5, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiersWithCharSet <string>(CharSet.None), 5, 0 });

            // Unsupported UnmanagedType
            yield return(new object[] { CodeSnippets.MarshalAsParametersAndModifiers <char>(UnmanagedType.I1), 5, 0 });

            yield return(new object[] { CodeSnippets.MarshalAsParametersAndModifiers <char>(UnmanagedType.U1), 5, 0 });

            yield return(new object[] { CodeSnippets.MarshalAsParametersAndModifiers <int[]>(UnmanagedType.SafeArray), 10, 0 });

            // Unsupported MarshalAsAttribute usage
            //  * UnmanagedType.CustomMarshaler, MarshalTypeRef, MarshalType, MarshalCookie
            yield return(new object[] { CodeSnippets.MarshalAsCustomMarshalerOnTypes, 16, 0 });

            // Unsupported [In, Out] attributes usage
            // Blittable array
            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier <int[]>("Out"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier <int[]>("In, Out"), 1, 0 });

            // By ref with [In, Out] attributes
            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier("in int", "In"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier("ref int", "In"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier("ref int", "In, Out"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier("out int", "Out"), 1, 0 });

            // By value non-array with [In, Out] attributes
            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier <byte>("In"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier <byte>("Out"), 1, 0 });

            yield return(new object[] { CodeSnippets.ByValueParameterWithModifier <byte>("In, Out"), 1, 0 });

            // Unsupported named arguments
            //  * BestFitMapping, ThrowOnUnmappableChar, CallingConvention
            yield return(new object[] { CodeSnippets.AllDllImportNamedArguments, 3, 0 });

            // LCIDConversion
            yield return(new object[] { CodeSnippets.LCIDConversionAttribute, 1, 0 });

            // No size information for array marshalling from unmanaged to managed
            //   * return, out, ref
            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <byte[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <sbyte[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <short[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <ushort[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <int[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <uint[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <long[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <ulong[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <float[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <double[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <bool[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <IntPtr[]>(), 3, 0 });

            yield return(new object[] { CodeSnippets.BasicParametersAndModifiers <UIntPtr[]>(), 3, 0 });

            // Collection with non-integer size param
            yield return(new object[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <float>(isByRef: false), 1, 0 });

            yield return(new object[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <double>(isByRef: false), 1, 0 });

            yield return(new object[] { CodeSnippets.MarshalAsArrayParameterWithSizeParam <bool>(isByRef: false), 1, 0 });

            yield return(new object[] { CodeSnippets.MarshalUsingArrayParameterWithSizeParam <float>(isByRef: false), 1, 0 });

            yield return(new object[] { CodeSnippets.MarshalUsingArrayParameterWithSizeParam <double>(isByRef: false), 1, 0 });

            yield return(new object[] { CodeSnippets.MarshalUsingArrayParameterWithSizeParam <bool>(isByRef: false), 1, 0 });


            // Custom type marshalling with invalid members
            yield return(new object[] { CodeSnippets.CustomStructMarshallingByRefValueProperty, 3, 0 });

            yield return(new object[] { CodeSnippets.CustomStructMarshallingManagedToNativeOnlyOutParameter, 1, 0 });

            yield return(new object[] { CodeSnippets.CustomStructMarshallingManagedToNativeOnlyReturnValue, 1, 0 });

            yield return(new object[] { CodeSnippets.CustomStructMarshallingNativeToManagedOnlyInParameter, 1, 0 });

            yield return(new object[] { CodeSnippets.CustomStructMarshallingStackallocOnlyRefParameter, 1, 0 });

            // Abstract SafeHandle type by reference
            yield return(new object[] { CodeSnippets.BasicParameterWithByRefModifier("ref", "System.Runtime.InteropServices.SafeHandle"), 1, 0 });

            // Non-blittable instantiation of generic type
            yield return(new object[] { CodeSnippets.MaybeBlittableGenericTypeParametersAndModifiers <bool>(), 5, 0 });

            // No marshalling annotations

            yield return(new object[] { CodeSnippets.ImplicitlyBlittableStructParametersAndModifiers("public"), 5, 0 });

            yield return(new object[] { CodeSnippets.ImplicitlyBlittableGenericTypeParametersAndModifiers <bool>(), 5, 0 });

            yield return(new object[] { CodeSnippets.ImplicitlyBlittableGenericTypeParametersAndModifiers <int>("public"), 5, 0 });

            // Collection with constant and element size parameter
            yield return(new object[] { CodeSnippets.MarshalUsingCollectionWithConstantAndElementCount, 2, 0 });

            // Collection with null element size parameter name
            yield return(new object[] { CodeSnippets.MarshalUsingCollectionWithNullElementName, 2, 0 });

            // Generic collection marshaller has different arity than collection.
            yield return(new object[] { CodeSnippets.GenericCollectionMarshallingArityMismatch, 2, 0 });

            yield return(new object[] { CodeSnippets.MarshalAsAndMarshalUsingOnReturnValue, 2, 0 });

            yield return(new object[] { CodeSnippets.GenericCollectionWithCustomElementMarshallingDuplicateElementIndirectionLevel, 2, 0 });

            yield return(new object[] { CodeSnippets.GenericCollectionWithCustomElementMarshallingUnusedElementIndirectionLevel, 1, 0 });

            yield return(new object[] { CodeSnippets.RecursiveCountElementNameOnReturnValue, 2, 0 });

            yield return(new object[] { CodeSnippets.RecursiveCountElementNameOnParameter, 2, 0 });

            yield return(new object[] { CodeSnippets.MutuallyRecursiveCountElementNameOnParameter, 4, 0 });

            yield return(new object[] { CodeSnippets.MutuallyRecursiveSizeParamIndexOnParameter, 4, 0 });

            // Ref returns
            yield return(new object[] { CodeSnippets.RefReturn("int"), 2, 2 });
        }