Skip to content

Add definition for _Bool in C++ mode #4629

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

Merged

Conversation

etcwilde
Copy link
Contributor

@etcwilde etcwilde commented Aug 31, 2022

_Bool is not defined when importing Foundation with C++ interop enabled
resulting in the missing type. This patch defines _Bool to bool when
compiling for C++.

I'm not entire sure how to test this. The test itself is fairly simple, it's just

import Foundation

with the invocation swiftc -Xcc -std=c++17 -enable-experimental-cxx-interop, but I don't know how to tell XCTest to do that so I would like pointers there.

rdar://99713881

@etcwilde etcwilde requested review from compnerd and parkera August 31, 2022 23:42

#ifdef __cplusplus
#define _Bool bool
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we want to use stdbool.h here as well as we do on the other platforms.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So include stdbool.h, and then fix the uses of _Bool through the codebase to use bool?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems like a better approach - using the standard to use the proper spelling irrespective of the language.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any other uses of _Bool in CF?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the list I have. I just need to find a cycle or two to go through and change them and run the tests.

./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static inline _Bool _resizeConditionalAllocationBuffer(_ConditionalAllocationBuffer *_Nonnull buffer, size_t amt) {
./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static inline _Bool _withStackOrHeapBufferWithResultInArguments(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(void *_Nonnull memory, size_t capacity, _Bool onStack)) {
./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static _Bool const _CFHasRenameat2 = 1;
./CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h:static _Bool const _CFHasRenameat2 = 0;
./CoreFoundation/Base.subproj/CFBase.h:    typedef _Bool                   Boolean;
./CoreFoundation/Base.subproj/ForFoundationOnly.h:#define BOOL _Bool
./CoreFoundation/Base.subproj/CFOverflow.h:    static _Bool __os_warn_unused(_Bool x) __attribute__((__warn_unused_result__));
./CoreFoundation/Base.subproj/CFOverflow.h:    static _Bool __os_warn_unused(_Bool x) { return x; }
./Darwin/shims/FoundationOverlayShims.h:    _Bool onStack;
./Darwin/shims/FoundationOverlayShims.h:static inline _Bool _resizeConditionalAllocationBuffer(_ConditionalAllocationBuffer *_Nonnull buffer, size_t amt) {
./Darwin/shims/FoundationOverlayShims.h:static inline _Bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((noescape)) ^ _Nonnull applier)(_ConditionalAllocationBuffer *_Nonnull)) {
./CoreFoundation/PlugIn.subproj/CFBundle_Grok.c:static inline _Bool _CF_isspace(int c) {

@parkera
Copy link
Contributor

parkera commented Sep 1, 2022

I agree with @compnerd's approach.

@etcwilde etcwilde force-pushed the ewilde/cpp-interop-foundation-import branch from 7f947ff to c866611 Compare February 17, 2023 05:45
@etcwilde
Copy link
Contributor Author

Alright, I've gone though and replaced uses of _Bool with bool and included stdbool.h.

@etcwilde
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@compnerd
Copy link
Member

ForFoundationOnly.h L89-102 needs adjustment.

_Bool is not defined in C++, so Foundation fails to import with C++
interop enabled. This patch replaces the uses of `_Bool` with `bool`
pulled from stdbool.h.
@etcwilde etcwilde force-pushed the ewilde/cpp-interop-foundation-import branch from c866611 to 9277186 Compare February 17, 2023 16:33
@etcwilde
Copy link
Contributor Author

@swift-ci please test

@etcwilde
Copy link
Contributor Author

ForFoundationOnly.h L89-102 needs adjustment.

Yeah, the issue is that BOOL isn't always _Bool, sometimes it's unsigned char. I just added #define BOOL bool here;

#ifdef __OBJC__
#define NSISARGTYPE id _Nullable
#else
#define NSISARGTYPE void * _Nullable
#define BOOL bool
#endif

@ian-twilightcoder
Copy link

ForFoundationOnly.h L89-102 needs adjustment.

Yeah, the issue is that BOOL isn't always _Bool, sometimes it's unsigned char. I just added #define BOOL bool here;

#ifdef __OBJC__
#define NSISARGTYPE id _Nullable
#else
#define NSISARGTYPE void * _Nullable
#define BOOL bool
#endif

That seems really sketchy, why does BOOL need to be defined at all in CF?

@etcwilde
Copy link
Contributor Author

That seems really sketchy, why does BOOL need to be defined at all in CF?

I think it's for ObjC compatibility since we're also building this code without ObjC.
The ForFoundationOnly.h only defines BOOL when __OBJC__ isn't defined.

// Arguments to these are id, but this header is non-Objc
#ifdef __OBJC__
#define NSISARGTYPE id _Nullable
#else
#define NSISARGTYPE void * _Nullable
#define BOOL _Bool
#endif

@etcwilde
Copy link
Contributor Author

Yeah, the BOOL definition came in on the Monterey CoreFoundation sync-up; d8e8a8b#diff-32adebf83b3fcbdf66de2d896086ed7702b8cb7083cedb9594f092a11f513618

@ian-twilightcoder
Copy link

That seems really sketchy, why does BOOL need to be defined at all in CF?

I think it's for ObjC compatibility since we're also building this code without ObjC. The ForFoundationOnly.h only defines BOOL when __OBJC__ isn't defined.

// Arguments to these are id, but this header is non-Objc
#ifdef __OBJC__
#define NSISARGTYPE id _Nullable
#else
#define NSISARGTYPE void * _Nullable
#define BOOL _Bool
#endif

Including <objc/objc.h> would be a lot better, I think it's ok to do that from pure C. Declaring it yourself is likely to eventually cause module problems

@etcwilde
Copy link
Contributor Author

Including <objc/objc.h> would be a lot better, I think it's ok to do that from pure C. Declaring it yourself is likely to eventually cause module problems

The definition was in the original code, I'm just trying to allow C++ to work, so I think that's maybe for another PR?
Windows and Linux come with objc/objc.h and it doesn't look like corelib-foundation is providing it. How are we distributing that header for those cases?

@ian-twilightcoder
Copy link

Including <objc/objc.h> would be a lot better, I think it's ok to do that from pure C. Declaring it yourself is likely to eventually cause module problems

The definition was in the original code, I'm just trying to allow C++ to work, so I think that's maybe for another PR? Windows and Linux come with objc/objc.h and it doesn't look like corelib-foundation is providing it. How are we distributing that header for those cases?

Yeah true, not really related to this PR. I'm not sure what provides the objc runtime on Windows and Linux.

@etcwilde etcwilde merged commit f3f3a73 into swiftlang:main Feb 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants