Skip to content

Commit

Permalink
misbehaving clang (__has_include, std::bit_cast, std::source_location)
Browse files Browse the repository at this point in the history
  • Loading branch information
aremmell committed Jan 7, 2024
1 parent da12fcd commit fbea170
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
22 changes: 16 additions & 6 deletions include/bal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#ifndef _BAL_HH_INCLUDED
# define _BAL_HH_INCLUDED

# if defined(__has_include)
# define __HAS_INCLUDE(hdr) __has_include(hdr)
# else
# define __HAS_INCLUDE(hdr) false
# endif

# include "bal.h"
# include <type_traits>
# include <functional>
Expand All @@ -35,7 +41,12 @@
# include <vector>
# include <atomic>
# include <string>
# include <bit>
# if __HAS_INCLUDE(<bit>)
# include <bit>
# define bit_cast std::bit_cast
# else
# define bit_cast reinterpret_cast
# endif

/** The one and only namespace for libbal. */
namespace bal
Expand Down Expand Up @@ -653,12 +664,12 @@ namespace bal

static socket_base* from_user_data(bal_socket* s)
{
return std::bit_cast<socket_base*>(s->user_data);
return bit_cast<socket_base*>(s->user_data);
}

uintptr_t to_user_data() const
{
return std::bit_cast<uintptr_t>(this);
return bit_cast<uintptr_t>(this);
}

async_io_cb on_read;
Expand Down Expand Up @@ -706,7 +717,7 @@ namespace bal
if (self == nullptr) {
_bal_dbglog("no user_data for socket " BAL_SOCKET_SPEC " (0x%"
PRIxPTR ", mask = %08" PRIx32 ")", s->sd,
std::bit_cast<uintptr_t>(s), s->state.mask);
bit_cast<uintptr_t>(s), s->state.mask);
return;
}

Expand All @@ -715,8 +726,7 @@ namespace bal
# if defined(BAL_DBGLOG)
_bal_dbglog("early return for socket " BAL_SOCKET_SPEC " (0x%"
PRIxPTR ", evt = %08" PRIx32 ", self = 0x%" PRIxPTR ")",
s->sd, std::bit_cast<uintptr_t>(s), evt,
std::bit_cast<uintptr_t>(self));
s->sd, bit_cast<uintptr_t>(s), evt, bit_cast<uintptr_t>(self));
# else
BAL_UNUSED(s);
BAL_UNUSED(self);
Expand Down
6 changes: 2 additions & 4 deletions include/bal/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
# include "types.h"

# if defined(__cplusplus)
# if _has_include(<source_location>)
# include <source_location>
# endif
extern "C" {
# endif

Expand Down Expand Up @@ -164,7 +161,8 @@ bool __bal_validate(bool expr, int err, const char* func, const char* file,
# if defined(BAL_DBGLOG)
void __bal_dbglog(const char* func, const char* file, uint32_t line,
const char* format, ...);
# if defined(__cplusplus) && _has_include(<source_location>)
# if defined(__cplusplus) && __HAS_INCLUDE(<source_location>)
# include <source_location>
# define _bal_dbglog(...) \
do { \
std::source_location loc = std::source_location::current(); \
Expand Down
2 changes: 1 addition & 1 deletion sample/balserver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int main(int argc, char** argv)
PRINT("got connection from %s %s:%s on " BAL_SOCKET_SPEC " (0x%" PRIxPTR ");"
" now have %zu client(s)", addrinfo.get_type().c_str(),
addrinfo.get_addr().c_str(), addrinfo.get_port().c_str(),
client_sock.get_descriptor(), std::bit_cast<uintptr_t>(client_sock.get()),
client_sock.get_descriptor(), bit_cast<uintptr_t>(client_sock.get()),
_clients.size() + 1);

_clients[client_sock.get_descriptor()] = std::move(client_sock);
Expand Down

0 comments on commit fbea170

Please sign in to comment.