Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed flags enum signedness mismatch in bindings code #331

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Generator/Sources/ProjectionModel/CAbi/CAbi+writing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ extension CAbi {
}

public static func writeEnumTypedef(_ enumDefinition: EnumDefinition, to writer: CSourceFileWriter) throws {
let mangledName = try CAbi.mangleName(type: enumDefinition.bindType())
writer.writeTypedef(type: .reference(name: "int32_t"), name: mangledName)
try writer.writeTypedef(
type: .reference(name: enumDefinition.isFlags ? "uint32_t" : "int32_t"),
name: CAbi.mangleName(type: enumDefinition.bindType()))
}

public static func writeStruct(_ structDefinition: StructDefinition, to writer: CSourceFileWriter) throws {
Expand Down
8 changes: 8 additions & 0 deletions InteropTests/Tests/EnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ class EnumTests: WinRTTestCase {
XCTAssertEqual(Flags.bit16.rawValue, 0x10000)
XCTAssertEqual(Flags.all.rawValue, 0xFFFFFFFF)
}

func testFlagsSetAlgebra() throws {
XCTAssert(try Enums.hasFlags(Flags.bit0.union(Flags.bit16), Flags.bit0))
XCTAssert(try Enums.hasFlags(Flags.bit0.union(Flags.bit16), Flags.bit16))
XCTAssert(try Enums.hasFlags(Flags.all.intersection(Flags.bit0), Flags.bit0))
XCTAssertFalse(try Enums.hasFlags(Flags.all.intersection(Flags.bit0), Flags.bit16))
XCTAssertFalse(try Enums.hasFlags(Flags.bit0, Flags.bit16))
}
}
11 changes: 11 additions & 0 deletions InteropTests/WinRTComponent/Dll/Enums.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "pch.h"
#include "Enums.h"
#include "Enums.g.cpp"

namespace winrt::WinRTComponent::implementation
{
boolean Enums::HasFlags(Flags value, Flags flags)
{
return (value & flags) == flags;
}
}
18 changes: 18 additions & 0 deletions InteropTests/WinRTComponent/Dll/Enums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "Enums.g.h"

namespace winrt::WinRTComponent::implementation
{
struct Enums
{
Enums() = default;

static boolean HasFlags(Flags value, Flags flags);
};
}
namespace winrt::WinRTComponent::factory_implementation
{
struct Enums : EnumsT<Enums, implementation::Enums>
{
};
}
5 changes: 5 additions & 0 deletions InteropTests/WinRTComponent/IDL/Enums.idl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ namespace WinRTComponent
Bit16 = 0x10000,
All = 0xFFFFFFFF,
};

static runtimeclass Enums
{
static Boolean HasFlags(Flags value, Flags flags);
};
}
Loading