Skip to content

Commit

Permalink
Remove the C++ interop workaround for wchar_t (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Oct 30, 2023
1 parent 34ce8ed commit 546b8dd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .github/actions/windows-build/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ runs:
with:
github-repo: thebrowsercompany/swift-build
github-token: ${{ inputs.GITHUB_TOKEN }}
release-tag-name: 20231004.2 # Includes https://github.com/apple/swift/pull/68838
release-tag-name: 20231025.2
release-asset-name: installer-amd64.exe

- name: Configure CMake
Expand Down
65 changes: 4 additions & 61 deletions swiftwinrt/Resources/CWinRT/CppInteropWorkaround.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Works around limitations of Swift's C++ Interop
// when exposing wchar_t or the Windows.h GUID type to Swift.
// Works around limitations of Swift's C++ Interop when exposing the Windows.h GUID type to Swift.
#pragma once

typedef struct GUID_Workaround {
Expand All @@ -13,17 +12,9 @@ typedef GUID_Workaround CLSID_Workaround;
typedef GUID_Workaround IID_Workaround;
typedef GUID_Workaround UUID_Workaround;

typedef UINT16 WCHAR_Workaround;
typedef WCHAR_Workaround OLECHAR_Workaround;
typedef const WCHAR_Workaround* PCWSTR_Workaround;
typedef WCHAR_Workaround* LPWSTR_Workaround;
typedef const WCHAR_Workaround* LPCWSTR_Workaround;
typedef OLECHAR_Workaround* BSTR_Workaround;

// Functions depending on workaround types
typedef struct IUnknown_Workaround IUnknown_Workaround;
typedef struct IInspectable_Workaround IInspectable_Workaround;
typedef struct IRestrictedErrorInfo_Workaround IRestrictedErrorInfo_Workaround;

#include <combaseapi.h>
inline HRESULT CoCreateInstance_Workaround(
Expand All @@ -39,39 +30,19 @@ inline HRESULT CoCreateInstance_Workaround(
#endif
}

inline int StringFromGUID2_Workaround(const GUID_Workaround* rguid, UINT16* lpsz, int cchMax) {
inline int StringFromGUID2_Workaround(const GUID_Workaround* rguid, LPOLESTR lpsz, int cchMax) {
#ifdef __cplusplus
return StringFromGUID2(*(GUID*)rguid, (LPOLESTR)lpsz, cchMax);
return StringFromGUID2(*(GUID*)rguid, lpsz, cchMax);
#else
return StringFromGUID2((REFGUID)rguid, (LPOLESTR)lpsz, cchMax);
return StringFromGUID2((REFGUID)rguid, lpsz, cchMax);
#endif
}

#include <oleauto.h>
inline BSTR_Workaround SysAllocString_Workaround(const OLECHAR_Workaround *psz) {
// Swift toolchains below 202309?? will hit a build error below
// because of a bug in the WinSDK modulemap for OLE headers.
return (BSTR_Workaround)SysAllocString((const OLECHAR*)psz);
}

inline void SysFreeString_Workaround(BSTR_Workaround bstrString) {
SysFreeString((BSTR)bstrString);
}

inline UINT SysStringLen_Workaround(BSTR_Workaround pbstr) {
return SysStringLen((BSTR)pbstr);
}

#include <rpc.h>
inline RPC_STATUS UuidFromStringA_Workaround(RPC_CSTR StringUuid, UUID_Workaround* Uuid) {
return UuidFromStringA(StringUuid, (UUID*)Uuid);
}

#include <roerrorapi.h>
inline HRESULT GetRestrictedErrorInfo_Workaround(IRestrictedErrorInfo_Workaround **ppRestrictedErrorInfo) {
return GetRestrictedErrorInfo((IRestrictedErrorInfo**)ppRestrictedErrorInfo);
}

#include <roapi.h>
inline HRESULT RoActivateInstance_Workaround(
HSTRING activatableClassId,
Expand All @@ -90,20 +61,6 @@ inline HRESULT RoGetActivationFactory_Workaround(
#endif
}

#include <windows.h>
inline DWORD FormatMessageW_Workaround(DWORD dwFlags, LPCVOID lpSource, DWORD dwMessageId, DWORD dwLanguageId, LPWSTR_Workaround lpBuffer, DWORD nSize, va_list *Arguments) {
return FormatMessageW(dwFlags, lpSource, dwMessageId, dwLanguageId, (LPWSTR)lpBuffer, nSize, Arguments);
}

#include <winstring.h>
inline HRESULT WindowsCreateString_Workaround(const UINT16* sourceString, UINT32 length, HSTRING *string) {
return WindowsCreateString((LPCWSTR)sourceString, length, string);
}

inline LPCWSTR_Workaround WindowsGetStringRawBuffer_Workaround(HSTRING string, UINT32 *length) {
return (LPCWSTR_Workaround)WindowsGetStringRawBuffer(string, length);
}

//-------------------------------------------------------------------------------------------------
// Begin the great lie
// Everything below will unknowingly use Xxx_Workaround.
Expand All @@ -122,24 +79,10 @@ inline LPCWSTR_Workaround WindowsGetStringRawBuffer_Workaround(HSTRING string, U
#undef REFIID // REFIID is a #define, not a typedef
#define REFIID const IID* __MIDL_CONST

#define WCHAR WCHAR_Workaround
#define OLECHAR WCHAR_Workaround
#define PCWSTR PCWSTR_Workaround
#define LPCWSTR LPCWSTR_Workaround
#define BSTR BSTR_Workaround

#define IUnknown IUnknown_Workaround
#define IUnknownVtbl IUnknownVtbl_Workaround
#define IInspectable IInspectable_Workaround
#define IInspectableVtbl IInspectableVtbl_Workaround
#define IRestrictedErrorInfo IRestrictedErrorInfo_Workaround
#define IRestrictedErrorInfoVtbl IRestrictedErrorInfo_WorkaroundVtbl
#define IID_IRestrictedErrorInfo IID_IRestrictedErrorInfo_Workaround

// Redefine IRestrictedErrorInfo with its workaround name
#undef __IRestrictedErrorInfo_FWD_DEFINED__
#undef __IRestrictedErrorInfo_INTERFACE_DEFINED__
#include "RestrictedErrorInfo.h"

// iunknown.h
typedef struct IUnknownVtbl
Expand Down
22 changes: 0 additions & 22 deletions swiftwinrt/Resources/Support/CppInteropWorkaround.swift
Original file line number Diff line number Diff line change
@@ -1,51 +1,29 @@
import C_BINDINGS_MODULE

#if true // TODO(WIN-860): Remove workaround once C++ interop issues with WinSDK.GUID are fixed.
public typealias WCHAR = C_BINDINGS_MODULE.WCHAR_Workaround
public typealias BSTR = C_BINDINGS_MODULE.BSTR_Workaround
public typealias PCWSTR = C_BINDINGS_MODULE.PCWSTR_Workaround?
public typealias GUID = C_BINDINGS_MODULE.GUID_Workaround
public typealias IID = C_BINDINGS_MODULE.IID_Workaround
public typealias CLSID = C_BINDINGS_MODULE.CLSID_Workaround
public typealias REFIID = UnsafePointer<C_BINDINGS_MODULE.IID_Workaround>
public typealias C_IUnknown = C_BINDINGS_MODULE.IUnknown_Workaround
public typealias C_IInspectable = C_BINDINGS_MODULE.IInspectable_Workaround
public typealias C_IInspectableVtbl = C_BINDINGS_MODULE.IInspectableVtbl_Workaround
internal typealias IRestrictedErrorInfo = C_BINDINGS_MODULE.IRestrictedErrorInfo_Workaround
internal let CoCreateInstance = C_BINDINGS_MODULE.CoCreateInstance_Workaround
internal let GetRestrictedErrorInfo = C_BINDINGS_MODULE.GetRestrictedErrorInfo_Workaround
internal let FormatMessageW = C_BINDINGS_MODULE.FormatMessageW_Workaround
internal let UuidFromStringA = C_BINDINGS_MODULE.UuidFromStringA_Workaround
internal let RoActivateInstance = C_BINDINGS_MODULE.RoActivateInstance_Workaround
internal let RoGetActivationFactory = C_BINDINGS_MODULE.RoGetActivationFactory_Workaround
internal let StringFromGUID2 = C_BINDINGS_MODULE.StringFromGUID2_Workaround
internal let SysAllocString = C_BINDINGS_MODULE.SysAllocString_Workaround
internal let SysFreeString = C_BINDINGS_MODULE.SysFreeString_Workaround
internal let SysStringLen = C_BINDINGS_MODULE.SysStringLen_Workaround
internal let WindowsCreateString = C_BINDINGS_MODULE.WindowsCreateString_Workaround
internal let WindowsGetStringRawBuffer = C_BINDINGS_MODULE.WindowsGetStringRawBuffer_Workaround
#else
public typealias WCHAR = C_BINDINGS_MODULE.WCHAR
public typealias BSTR = C_BINDINGS_MODULE.BSTR
public typealias PCWSTR = C_BINDINGS_MODULE.PCWSTR?
public typealias GUID = C_BINDINGS_MODULE.GUID
public typealias IID = C_BINDINGS_MODULE.IID
public typealias CLSID = C_BINDINGS_MODULE.CLSID
public typealias REFIID = UnsafePointer<C_BINDINGS_MODULE.IID>
public typealias C_IUnknown = C_BINDINGS_MODULE.IUnknown
public typealias C_IInspectable = C_BINDINGS_MODULE.IInspectable
public typealias C_IInspectableVtbl = C_BINDINGS_MODULE.IInspectableVtbl
internal typealias IRestrictedErrorInfo = C_BINDINGS_MODULE.IRestrictedErrorInfo
internal let CoCreateInstance = C_BINDINGS_MODULE.CoCreateInstance
internal let GetRestrictedErrorInfo = C_BINDINGS_MODULE.GetRestrictedErrorInfo
internal let FormatMessageW = C_BINDINGS_MODULE.FormatMessageW
internal let UuidFromStringA = C_BINDINGS_MODULE.UuidFromStringA
internal let RoActivateInstance = C_BINDINGS_MODULE.RoActivateInstance
internal let RoGetActivationFactory = C_BINDINGS_MODULE.RoGetActivationFactory
internal let StringFromGUID2 = C_BINDINGS_MODULE.StringFromGUID2
internal let SysAllocString = C_BINDINGS_MODULE.SysAllocString
internal let SysFreeString = C_BINDINGS_MODULE.SysFreeString
internal let SysStringLen = C_BINDINGS_MODULE.SysStringLen
internal let WindowsCreateString = C_BINDINGS_MODULE.WindowsCreateString
internal let WindowsGetStringRawBuffer = C_BINDINGS_MODULE.WindowsGetStringRawBuffer
#endif

0 comments on commit 546b8dd

Please sign in to comment.