Skip to content

Commit bd13e11

Browse files
committed
Ensure empty test files aren't emitted
1 parent 0d07529 commit bd13e11

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

ClangSharp.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{2F80
5858
scripts\build.sh = scripts\build.sh
5959
scripts\cibuild.cmd = scripts\cibuild.cmd
6060
scripts\cibuild.sh = scripts\cibuild.sh
61+
scripts\SignClientFileList.txt = scripts\SignClientFileList.txt
6162
EndProjectSection
6263
EndProject
6364
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "packages", "packages", "{AE6CF12F-5CC3-463B-A74B-6CCAE26EE4EF}"

sources/ClangSharp.Interop/clang.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
using System;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
78

9+
[assembly: DisableRuntimeMarshalling]
10+
811
namespace ClangSharp.Interop;
912

1013
public static unsafe partial class @clang

sources/ClangSharp.Interop/clangsharp/clangsharp.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace ClangSharp.Interop;
88

9-
public static partial class clangsharp
9+
public static partial class @clangsharp
1010
{
1111
[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getArgument", ExactSpelling = true)]
1212
public static extern CXCursor Cursor_getArgument(CXCursor C, [NativeTypeName("unsigned int")] uint i);

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,20 +1357,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
13571357

13581358
var isTopLevelStruct = _config.WithTypes.TryGetValue(name, out var withType) && withType.Equals("struct", StringComparison.Ordinal);
13591359
var generateTestsClass = !recordDecl.IsAnonymousStructOrUnion && recordDecl.DeclContext is not RecordDecl;
1360-
1361-
if ((_testOutputBuilder is not null) && generateTestsClass && !isTopLevelStruct)
1362-
{
1363-
_testOutputBuilder.WriteIndented("/// <summary>Provides validation of the <see cref=\"");
1364-
_testOutputBuilder.Write(escapedName);
1365-
_testOutputBuilder.WriteLine("\" /> struct.</summary>");
1366-
1367-
WithAttributes(recordDecl, onlySupportedOSPlatform: true, isTestOutput: true);
1368-
1369-
_testOutputBuilder.WriteIndented("public static unsafe partial class ");
1370-
_testOutputBuilder.Write(escapedName);
1371-
_testOutputBuilder.WriteLine("Tests");
1372-
_testOutputBuilder.WriteBlockStart();
1373-
}
1360+
var testOutputStarted = false;
13741361

13751362
var nullableUuid = (Guid?)null;
13761363
var uuidName = "";
@@ -1384,6 +1371,8 @@ private void VisitRecordDecl(RecordDecl recordDecl)
13841371

13851372
if ((_testOutputBuilder is not null) && (uuid != Guid.Empty))
13861373
{
1374+
StartTestOutput(ref testOutputStarted, generateTestsClass, isTopLevelStruct);
1375+
13871376
var className = GetClass(uuidName);
13881377

13891378
_testOutputBuilder.AddUsingDirective("System");
@@ -1676,6 +1665,8 @@ private void VisitRecordDecl(RecordDecl recordDecl)
16761665

16771666
if ((_testOutputBuilder is not null) && generateTestsClass && !_config.GenerateDisableRuntimeMarshalling)
16781667
{
1668+
StartTestOutput(ref testOutputStarted, generateTestsClass, isTopLevelStruct);
1669+
16791670
_testOutputBuilder.WriteIndented("/// <summary>Validates that the <see cref=\"");
16801671
_testOutputBuilder.Write(escapedName);
16811672
_testOutputBuilder.WriteLine("\" /> struct is blittable.</summary>");
@@ -1857,7 +1848,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)
18571848
{
18581849
_outputBuilder.EndStruct(in desc);
18591850

1860-
if ((_testOutputBuilder is not null) && generateTestsClass)
1851+
if ((_testOutputBuilder is not null) && generateTestsClass && testOutputStarted)
18611852
{
18621853
_testOutputBuilder.WriteBlockEnd();
18631854
}
@@ -3125,6 +3116,25 @@ void VisitConstantOrIncompleteArrayFieldDecl(RecordDecl recordDecl, FieldDecl co
31253116

31263117
_outputBuilder.EndStruct(in desc);
31273118
}
3119+
3120+
void StartTestOutput(ref bool testOutputStarted, bool generateTestsClass, bool isTopLevelStruct)
3121+
{
3122+
if ((_testOutputBuilder is not null) && generateTestsClass && !isTopLevelStruct && !testOutputStarted)
3123+
{
3124+
_testOutputBuilder.WriteIndented("/// <summary>Provides validation of the <see cref=\"");
3125+
_testOutputBuilder.Write(escapedName);
3126+
_testOutputBuilder.WriteLine("\" /> struct.</summary>");
3127+
3128+
WithAttributes(recordDecl, onlySupportedOSPlatform: true, isTestOutput: true);
3129+
3130+
_testOutputBuilder.WriteIndented("public static unsafe partial class ");
3131+
_testOutputBuilder.Write(escapedName);
3132+
_testOutputBuilder.WriteLine("Tests");
3133+
_testOutputBuilder.WriteBlockStart();
3134+
3135+
testOutputStarted = true;
3136+
}
3137+
}
31283138
}
31293139

31303140
private void VisitTranslationUnitDecl(TranslationUnitDecl translationUnitDecl)

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,21 @@ public void Close()
323323
var outputPath = outputBuilder.IsTestOutput ? _config.TestOutputLocation : _config.OutputLocation;
324324
var isMethodClass = _topLevelClassNames.Contains(outputBuilder.Name);
325325

326+
if (outputBuilder is CSharpOutputBuilder csharpOutputBuilder)
327+
{
328+
if (!csharpOutputBuilder.Contents.Any())
329+
{
330+
continue;
331+
}
332+
}
333+
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
334+
{
335+
if (!xmlOutputBuilder.Contents.Any())
336+
{
337+
continue;
338+
}
339+
}
340+
326341
if (_config.GenerateMultipleFiles)
327342
{
328343
outputPath = Path.Combine(outputPath, $"{outputBuilder.Name}{outputBuilder.Extension}");
@@ -1894,7 +1909,7 @@ private void CloseOutputBuilder(Stream stream, IOutputBuilder outputBuilder, boo
18941909
sw.WriteLine();
18951910
}
18961911
}
1897-
else if ((outputBuilder is XmlOutputBuilder xmlOutputBuilder) && xmlOutputBuilder.Contents.Any())
1912+
else if (outputBuilder is XmlOutputBuilder xmlOutputBuilder)
18981913
{
18991914
sw.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
19001915
sw.WriteLine("<bindings>");

0 commit comments

Comments
 (0)