Skip to content
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

Add printf formatting macros for nk data types #773

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

PROP65
Copy link
Contributor

@PROP65 PROP65 commented Feb 8, 2025

Adds macros from formatting the nk data types

Macro Type
NK_FMT_CHAR nk_char
NK_FMT_UCHAR nk_uchar
NK_FMT_BYTE nk_byte
NK_FMT_SHORT nk_short
NK_FMT_USHORT nk_ushort
NK_FMT_INT nk_int
NK_FMT_UINT nk_uint
NK_FMT_SIZE nk_size
NK_FMT_PTR nk_ptr

This won't completely prevent issues like #727 but it should help.

Needs to be tested on Windows.

@PROP65
Copy link
Contributor Author

PROP65 commented Feb 8, 2025

Here's a test program to check for warnings.

https://gist.github.com/PROP65/88cf2ec23a5052350f2e79857dd9fd8e

@RobLoach
Copy link
Contributor

Seems like a good solution. Those warnings have been pretty annoying in the build sometimes.

@PROP65 PROP65 marked this pull request as ready for review February 10, 2025 23:36
Copy link
Contributor

@RobLoach RobLoach left a comment

Choose a reason for hiding this comment

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

Seems good to me. Going to leave this open for a bit for others to review too.

Copy link

@klei1984 klei1984 left a comment

Choose a reason for hiding this comment

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

I am using msys2/Mingw64 toolchain on Windows which has some problems with the NK_INCLUDE_FIXED_TYPES feature. See in-line review comments.

@@ -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}

@@ -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);
      |                           

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.

3 participants