From 86431b41be1ab2d869c6b0926a3206a9022d0545 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Mon, 15 Nov 2021 13:39:22 -0800 Subject: [PATCH] Ensure that top level classes/structs correctly mark themselves as unsafe and include the right namespace for tests --- .../PInvokeGenerator.VisitDecl.cs | 5 +++++ sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs index a95f8489..9fffa6d3 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs @@ -1300,6 +1300,11 @@ private void VisitRecordDecl(RecordDecl recordDecl) { _topLevelClassUsings[name] = withUsings; } + + if (desc.IsUnsafe) + { + _isTopLevelClassUnsafe[name] = true; + } } if (hasVtbl) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 759b0a40..c9498b41 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -4991,7 +4991,9 @@ void CreateTestOutputBuilder(string name) { _testOutputBuilder = _outputBuilderFactory.CreateTests(name); - if (!_topLevelClassNames.Contains(name)) + var isTopLevelStruct = _config.WithTypes.TryGetValue(name, out var withType) && (withType == "struct"); + + if (!_topLevelClassNames.Contains(name) || isTopLevelStruct) { _testOutputBuilder.AddUsingDirective("System.Runtime.InteropServices"); }