Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
4 changes: 2 additions & 2 deletions demo/common/overview.c
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ overview(struct nk_context *ctx)
}
/* progressbar combobox */
sum = prog_a + prog_b + prog_c + prog_d;
sprintf(buffer, "%lu", sum);
sprintf(buffer, "%" NK_FMT_SIZE, sum);

Choose a reason for hiding this comment

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

On Windows 64 bit, msys2/mingw64 this line gives the following warning message if demo is built in -std=c89 mode.

Compiler settings:

CFLAGS += -std=c89 -Wall -Wextra -pedantic -ggdb -O0 -DSDL_DISABLE_IMMINTRIN_H
CFLAGS += `sdl2-config --cflags`

Warning message:

In file included from main.c:63:
../../demo/common/overview.c: In function 'overview':
../../demo/common/overview.c:447:33: warning: ISO C does not support the 'I64' ms_printf length modi
fier [-Wformat=]
  447 |                 sprintf(buffer, "%" NK_FMT_SIZE, sum);
      |                                 ^~~                  

NK_INCLUDE_FIXED_TYPES is active in the above context.

Disabling the macro in the 64 bit build environment fails via assertions like:

NK_STATIC_ASSERT(sizeof(nk_size) >= sizeof(void*));
NK_STATIC_ASSERT(sizeof(nk_ptr) >= sizeof(void*));

The potential reason is this:

    #elif defined(__GNUC__) || defined(__clang__)
      #if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
        #define NK_SIZE_TYPE unsigned long
      #else

On Windows 64 bit, msys2/mingw64 unsigned long is 4 bytes.

Architecture of built software:

architecture: i386:x86-64, flags 0x0000013b:
HAS_RELOC, EXEC_P, HAS_DEBUG, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x00000001400013f0

The 32 bit build environment, using msys2/mingw32, builds without errors and warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added checks for MinGW; the static asserts should be fixed. Since there isn't any type in c89 that is guaranteed to be at least 64 bits, it may not be possible on 64 bit windows targets to format nk_size in a way that is c89 compliant.

Copy link

@klei1984 klei1984 Feb 14, 2025

Choose a reason for hiding this comment

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

I checked both NK_INCLUDE_FIXED_TYPES active and inactive in 32/64 bit and c89/c99/c11/c17/c23 combinations.

  • I can confirm that the demo I checked earlier is now not tripping on the assertion tests in any checked compilation modes.

The rest are warnings and as noted by PRP65, most are C ISO standards related.

  • c89 raises long long warnings in 64 bit mode which is perfectly fine.
  • in case fixed types macro is disabled, 32 bit mode in c89 mode warns about no support for I32 printf flag.
  • in case fixed types macro is disabled, 32/64 bit modes in c99/C11/C17/C23 modes warn about no support for I printf flag.
  • in case fixed types macro is disabled, 64 bit mode in c99/C11/C17/C23 modes warn about NK_FMT_SIZE being incompatible with size_t. Warning example below.
../../demo/common/overview.c:447:33: warning: format '%u' expects argument of type 'unsigned int', but argument 3 has type 'size_t' {aka 'long long unsigned int'} [-Wformat=]
  447 |                 sprintf(buffer, "%" NK_FMT_SIZE, sum);
      |                                 ^~~              ~~~
      |                                                  |
      |                                                  size_t {aka long long unsigned int}

if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,200))) {
nk_layout_row_dynamic(ctx, 30, 1);
nk_progress(ctx, &prog_a, 100, NK_MODIFIABLE);
Expand All @@ -456,7 +456,7 @@ overview(struct nk_context *ctx)

/* checkbox combobox */
sum = (size_t)(check_values[0] + check_values[1] + check_values[2] + check_values[3] + check_values[4]);
sprintf(buffer, "%lu", sum);
sprintf(buffer, "%" NK_FMT_SIZE, sum);

Choose a reason for hiding this comment

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

Warning message:

../../demo/common/overview.c:459:33: warning: ISO C does not support the 'I64' ms_printf length modi
fier [-Wformat=]
  459 |                 sprintf(buffer, "%" NK_FMT_SIZE, sum);
      |                           

if (nk_combo_begin_label(ctx, buffer, nk_vec2(200,200))) {
nk_layout_row_dynamic(ctx, 30, 1);
nk_checkbox_label(ctx, weapons[0], &check_values[0]);
Expand Down
78 changes: 77 additions & 1 deletion nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,9 @@ extern "C" {
*
* ===============================================================
*/
#ifdef NK_INCLUDE_FIXED_TYPES
#ifdef NK_INCLUDE_FIXED_TYPES
#include <stdint.h>
#include <inttypes.h>
#define NK_INT8 int8_t
#define NK_UINT8 uint8_t
#define NK_INT16 int16_t
Expand All @@ -347,33 +348,67 @@ extern "C" {
#define NK_UINT32 uint32_t
#define NK_SIZE_TYPE uintptr_t
#define NK_POINTER_TYPE uintptr_t
#define NK_INT8_FMT PRIi8
#define NK_UINT8_FMT PRIu8
#define NK_INT16_FMT PRIi16
#define NK_UINT16_FMT PRIu16
#define NK_INT32_FMT PRIi32
#define NK_UINT32_FMT PRIu32
#define NK_SIZE_TYPE_FMT PRIuPTR
#define NK_POINTER_TYPE_FMT PRIxPTR
#else
#ifndef NK_INT8
#define NK_INT8 signed char
#endif
#ifndef NK_INT8_FMT
#define NK_INT8_FMT "hi"
#endif
#ifndef NK_UINT8
#define NK_UINT8 unsigned char
#endif
#ifndef NK_UINT8_FMT
#define NK_UINT8_FMT "hu"
#endif
#ifndef NK_INT16
#define NK_INT16 signed short
#endif
#ifndef NK_INT16_FMT
#define NK_INT16_FMT "hi"
#endif
#ifndef NK_UINT16
#define NK_UINT16 unsigned short
#endif
#ifndef NK_UINT16_FMT
#define NK_UINT16_FMT "hu"
#endif
#ifndef NK_INT32
#if defined(_MSC_VER)
#define NK_INT32 __int32
#else
#define NK_INT32 signed int
#endif
#endif
#ifndef NK_INT32_FMT
#if defined(_MSC_VER)
#define NK_INT32_FMT "I32d"
#else
#define NK_INT32_FMT "i"
#endif
#endif
#ifndef NK_UINT32
#if defined(_MSC_VER)
#define NK_UINT32 unsigned __int32
#else
#define NK_UINT32 unsigned int
#endif
#endif
#ifndef NK_UINT32_FMT
#if defined(_MSC_VER)
#define NK_UINT32_FMT "I32u"
#else
#define NK_UINT32_FMT "u"
#endif
#endif
#ifndef NK_SIZE_TYPE
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_SIZE_TYPE unsigned __int64
Expand All @@ -389,6 +424,21 @@ extern "C" {
#define NK_SIZE_TYPE unsigned long
#endif
#endif
#ifndef NK_SIZE_TYPE_FMT
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_SIZE_TYPE_FMT "I64u"
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
#define NK_SIZE_TYPE_FMT "I32u"
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
#define NK_SIZE_TYPE_FMT "lu"
#else
#define NK_SIZE_TYPE_FMT "u"
#endif
#else
#define NK_SIZE_TYPE_FMT "lu"
#endif
#endif
#ifndef NK_POINTER_TYPE
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_POINTER_TYPE unsigned __int64
Expand All @@ -404,6 +454,21 @@ extern "C" {
#define NK_POINTER_TYPE unsigned long
#endif
#endif
#ifndef NK_POINTER_TYPE_FMT
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_POINTER_TYPE_FMT "I64x"
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
#define NK_POINTER_TYPE_FMT "I32x"
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
#define NK_POINTER_TYPE_FMT "lx"
#else
#define NK_POINTER_TYPE_FMT "x"
#endif
#else
#define NK_POINTER_TYPE_FMT "lx"
#endif
#endif
#endif

#ifndef NK_BOOL
Expand All @@ -415,6 +480,16 @@ extern "C" {
#endif
#endif

#define NK_FMT_CHAR NK_INT8_FMT
#define NK_FMT_UCHAR NK_UINT8_FMT
#define NK_FMT_BYTE NK_UINT8_FMT
#define NK_FMT_SHORT NK_INT16_FMT
#define NK_FMT_USHORT NK_UINT16_FMT
#define NK_FMT_INT NK_INT32_FMT
#define NK_FMT_UINT NK_UINT32_FMT
#define NK_FMT_SIZE NK_SIZE_TYPE_FMT
#define NK_FMT_PTR NK_POINTER_TYPE_FMT

typedef NK_INT8 nk_char;
typedef NK_UINT8 nk_uchar;
typedef NK_UINT8 nk_byte;
Expand Down Expand Up @@ -30698,6 +30773,7 @@ nk_tooltipfv(struct nk_context *ctx, const char *fmt, va_list args)
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2025/02/11 (4.12.5) - Add printf formatting macros for nk data types
/// - 2024/12/11 (4.12.4) - Fix array subscript [0, 0] is outside array bounds of ‘char[1]’
/// - 2024/12/11 (4.12.3) - Fix border color for property widgets
/// - 2024/11/20 (4.12.2) - Fix int/float type conversion warnings in `nk_roundf`
Expand Down
1 change: 1 addition & 0 deletions src/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/// - [y]: Minor version with non-breaking API and library changes
/// - [z]: Patch version with no direct changes to the API
///
/// - 2025/02/11 (4.12.5) - Add printf formatting macros for nk data types
/// - 2024/12/11 (4.12.4) - Fix array subscript [0, 0] is outside array bounds of ‘char[1]’
/// - 2024/12/11 (4.12.3) - Fix border color for property widgets
/// - 2024/11/20 (4.12.2) - Fix int/float type conversion warnings in `nk_roundf`
Expand Down
77 changes: 76 additions & 1 deletion src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ extern "C" {
*
* ===============================================================
*/
#ifdef NK_INCLUDE_FIXED_TYPES
#ifdef NK_INCLUDE_FIXED_TYPES
#include <stdint.h>
#include <inttypes.h>
#define NK_INT8 int8_t
#define NK_UINT8 uint8_t
#define NK_INT16 int16_t
Expand All @@ -125,33 +126,67 @@ extern "C" {
#define NK_UINT32 uint32_t
#define NK_SIZE_TYPE uintptr_t
#define NK_POINTER_TYPE uintptr_t
#define NK_INT8_FMT PRIi8
#define NK_UINT8_FMT PRIu8
#define NK_INT16_FMT PRIi16
#define NK_UINT16_FMT PRIu16
#define NK_INT32_FMT PRIi32
#define NK_UINT32_FMT PRIu32
#define NK_SIZE_TYPE_FMT PRIuPTR
#define NK_POINTER_TYPE_FMT PRIxPTR
#else
#ifndef NK_INT8
#define NK_INT8 signed char
#endif
#ifndef NK_INT8_FMT
#define NK_INT8_FMT "hi"
#endif
#ifndef NK_UINT8
#define NK_UINT8 unsigned char
#endif
#ifndef NK_UINT8_FMT
#define NK_UINT8_FMT "hu"
#endif
#ifndef NK_INT16
#define NK_INT16 signed short
#endif
#ifndef NK_INT16_FMT
#define NK_INT16_FMT "hi"
#endif
#ifndef NK_UINT16
#define NK_UINT16 unsigned short
#endif
#ifndef NK_UINT16_FMT
#define NK_UINT16_FMT "hu"
#endif
#ifndef NK_INT32
#if defined(_MSC_VER)
#define NK_INT32 __int32
#else
#define NK_INT32 signed int
#endif
#endif
#ifndef NK_INT32_FMT
#if defined(_MSC_VER)
#define NK_INT32_FMT "I32d"
#else
#define NK_INT32_FMT "i"
#endif
#endif
#ifndef NK_UINT32
#if defined(_MSC_VER)
#define NK_UINT32 unsigned __int32
#else
#define NK_UINT32 unsigned int
#endif
#endif
#ifndef NK_UINT32_FMT
#if defined(_MSC_VER)
#define NK_UINT32_FMT "I32u"
#else
#define NK_UINT32_FMT "u"
#endif
#endif
#ifndef NK_SIZE_TYPE
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_SIZE_TYPE unsigned __int64
Expand All @@ -167,6 +202,21 @@ extern "C" {
#define NK_SIZE_TYPE unsigned long
#endif
#endif
#ifndef NK_SIZE_TYPE_FMT
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_SIZE_TYPE_FMT "I64u"
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
#define NK_SIZE_TYPE_FMT "I32u"
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
#define NK_SIZE_TYPE_FMT "lu"
#else
#define NK_SIZE_TYPE_FMT "u"
#endif
#else
#define NK_SIZE_TYPE_FMT "lu"
#endif
#endif
#ifndef NK_POINTER_TYPE
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_POINTER_TYPE unsigned __int64
Expand All @@ -182,6 +232,21 @@ extern "C" {
#define NK_POINTER_TYPE unsigned long
#endif
#endif
#ifndef NK_POINTER_TYPE_FMT
#if defined(_WIN64) && defined(_MSC_VER)
#define NK_POINTER_TYPE_FMT "I64x"
#elif (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
#define NK_POINTER_TYPE_FMT "I32x"
#elif defined(__GNUC__) || defined(__clang__)
#if defined(__x86_64__) || defined(__ppc64__) || defined(__PPC64__) || defined(__aarch64__)
#define NK_POINTER_TYPE_FMT "lx"
#else
#define NK_POINTER_TYPE_FMT "x"
#endif
#else
#define NK_POINTER_TYPE_FMT "lx"
#endif
#endif
#endif

#ifndef NK_BOOL
Expand All @@ -193,6 +258,16 @@ extern "C" {
#endif
#endif

#define NK_FMT_CHAR NK_INT8_FMT
#define NK_FMT_UCHAR NK_UINT8_FMT
#define NK_FMT_BYTE NK_UINT8_FMT
#define NK_FMT_SHORT NK_INT16_FMT
#define NK_FMT_USHORT NK_UINT16_FMT
#define NK_FMT_INT NK_INT32_FMT
#define NK_FMT_UINT NK_UINT32_FMT
#define NK_FMT_SIZE NK_SIZE_TYPE_FMT
#define NK_FMT_PTR NK_POINTER_TYPE_FMT

typedef NK_INT8 nk_char;
typedef NK_UINT8 nk_uchar;
typedef NK_UINT8 nk_byte;
Expand Down