Skip to content

Commit

Permalink
uchar: fix conditional include/typedef (#10)
Browse files Browse the repository at this point in the history
Fix incorrect check: if `__has_include(..)` was supported, but `uchar.h` did not exist, `char16_t` never got `typedef`ed.

It needs to be `typedef`ed in (at least) two cases:

1. `uchar.h` does not exist, and
2. `__has_include(..)` is not supported

In case of the former, the type is most likely missing. In case of the latter, `uchar.h` could exist, but existence can't be checked.

Note: checking for `__has_include(..)` support and use of the macro in a combined conditional is non-portable according to the documentation, hence the split.
  • Loading branch information
gavanderhoorn authored and clalancette committed Sep 13, 2023
1 parent 409a33c commit 6e3ff9f
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions include/rosidl_dynamic_typesupport/uchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ extern "C" {
#if defined(__cplusplus) && __cplusplus >= 201103L
// Nothing to do here, C++11 and beyond have char16_t as a keyword:
// https://en.cppreference.com/w/cpp/keyword/char16_t
#elif defined(__has_include)
# if __has_include(<uchar.h>)
#else
// According to https://gcc.gnu.org/onlinedocs/cpp/_005f_005fhas_005finclude.html,
// short-circuit style use of __has_include is not supported, so split the conditional
# if defined(__has_include)
# if __has_include(<uchar.h>)
// If the compiler has __has_include, and uchar.h exists, include that as it will have char16_t
// as a typedef.
# include <uchar.h>
# endif
#else
# include <uchar.h>
# else
// Otherwise assume that char16_t isn't defined anywhere, and define it ourselves as uint_least16_t.
# define __ROSIDL_DYNAMIC_TYPESUPPORT__UCHAR_H__NEEDS_CHAR16_T_DECL
# endif
# else
# define __ROSIDL_DYNAMIC_TYPESUPPORT__UCHAR_H__NEEDS_CHAR16_T_DECL
# endif
#endif

#if defined(__ROSIDL_DYNAMIC_TYPESUPPORT__UCHAR_H__NEEDS_CHAR16_T_DECL)
# undef __ROSIDL_DYNAMIC_TYPESUPPORT__UCHAR_H__NEEDS_CHAR16_T_DECL
# include <stdint.h>
typedef uint_least16_t char16_t;
#endif
Expand Down

0 comments on commit 6e3ff9f

Please sign in to comment.