Author Topic: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c  (Read 1018 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6070
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
%I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« on: February 04, 2025, 05:35:09 am »
Code
		/* C::B patch: Compatibility with 64 bit compiler / OS */
#if defined(_WIN64)
sscanf(buf, "%I64d %n", &timeval, &n);
#else
sscanf(buf, "%lld %n", &timeval, &n);
#endif

When I build the C::B, I see some code in the "plugins\compilergcc\depslib\src\cache.c" cause some warnings:

Code
D:\code\cb\cb_sf_git\cccrash2019\src\plugins\compilergcc\depslib\src\cache.c: In function 'cache_write':
D:\code\cb\cb_sf_git\cccrash2019\src\plugins\compilergcc\depslib\src\cache.c:173:33: warning: format '%d' expects argument of type 'int', but argument 3 has type 'time_t' {aka 'long long int'} [-Wformat=]
  173 |                 fprintf(f, "%I64d %s\n", h->time, h->file);
      |                             ~~~~^        ~~~~~~~
      |                                 |         |
      |                                 int       time_t {aka long long int}
      |                             %I64lld


I'm using msys2/mingw64's g++ compiler, and the "#if defined(_WIN64)" is true, so my question is:

why not just use the %lld?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Wkerry

  • Multiple posting newcomer
  • *
  • Posts: 77
Re: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« Reply #1 on: February 04, 2025, 08:39:58 am »
Try the following:
      #if  (_USE_LONG_TIME_T)
         sscanf(buf, "%ld %n", &timeval, &n);
      #else
         #if defined(PRId64)
            sscanf(buf, "%" PRId64 " %n", &timeval, &n);
         #else
            sscanf(buf, "%lld %n", &timeval, &n);
         #endif
      #endif

and
      #if  (_USE_LONG_TIME_T)
         fprintf(f, "%ld %s\n", h->time, h->file);
      #else
         #if defined(PRId64)
            fprintf(f, "%" PRId64 " %s\n", h->time, h->file);
         #else
            fprintf(f, "%lld %s\n", h->time, h->file);
         #endif
      #endif

This works better than the existing code. Give it a quick hack/ try, but you may find a better way.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6070
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« Reply #2 on: February 06, 2025, 02:27:42 pm »
Try the following:
      #if  (_USE_LONG_TIME_T)
         sscanf(buf, "%ld %n", &timeval, &n);
      #else
         #if defined(PRId64)
            sscanf(buf, "%" PRId64 " %n", &timeval, &n);
         #else
            sscanf(buf, "%lld %n", &timeval, &n);
         #endif
      #endif

and
      #if  (_USE_LONG_TIME_T)
         fprintf(f, "%ld %s\n", h->time, h->file);
      #else
         #if defined(PRId64)
            fprintf(f, "%" PRId64 " %s\n", h->time, h->file);
         #else
            fprintf(f, "%lld %s\n", h->time, h->file);
         #endif
      #endif

This works better than the existing code. Give it a quick hack/ try, but you may find a better way.

Hi, thanks, I just tested it. I think your suggested code is better than the current code.  :)
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 6070
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: %I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
« Reply #3 on: February 08, 2025, 02:11:40 pm »
Can anyone help to test the below patch, in my 64bit gcc compiler under windows system, it remove the build warnings:

Code
From 38063feccee0827493c153f71c4e102f9d15428c Mon Sep 17 00:00:00 2001
From: asmwarrior <hidden@example.com>
Date: Sat, 8 Feb 2025 11:09:06 +0800
Subject: [PATCH] - compilergcc plugin: fix build warning in the depslib, see
 discussion here:

%I64d or %lld in the plugins\compilergcc\depslib\src\cache.c
https://forums.codeblocks.org/index.php/topic,25960.0.html
---
 src/plugins/compilergcc/depslib/src/cache.c | 23 +++++++++++++++------
 src/plugins/compilergcc/depslib/src/hash.c  |  7 +++++--
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/plugins/compilergcc/depslib/src/cache.c b/src/plugins/compilergcc/depslib/src/cache.c
index 8e90756f..1d2f8211 100644
--- a/src/plugins/compilergcc/depslib/src/cache.c
+++ b/src/plugins/compilergcc/depslib/src/cache.c
@@ -16,6 +16,9 @@
 #include "newstr.h"
 #include "cache.h"
 
+/* C::B patch: Compatibility with 64 bit compiler / OS*/
+# include "inttypes.h" // PRId64
+
 #include "depslib.h" /* for struct depsStats */
 extern struct depsStats g_stats;
 
@@ -138,10 +141,14 @@ void cache_read(const char *path)
  }
 
  /* C::B patch: Compatibility with 64 bit compiler / OS */
- #if defined(_WIN64)
- sscanf(buf, "%I64d %n", &timeval, &n);
+ #if  (_USE_LONG_TIME_T)
+ sscanf(buf, "%ld %n", &timeval, &n);
  #else
- sscanf(buf, "%lld %n", &timeval, &n);
+ #if defined(PRId64)
+ sscanf(buf, "%" PRId64 " %n", &timeval, &n);
+ #else
+ sscanf(buf, "%lld %n", &timeval, &n);
+ #endif
  #endif
  h = hdr_enter (buf + n);
  h->time = timeval;
@@ -169,10 +176,14 @@ void cache_write(const char *path)
  {
  LIST *l;
  /* C::B patch: Compatibility with 64 bit compiler / OS */
- #if defined(_WIN64)
- fprintf(f, "%I64d %s\n", h->time, h->file);
+ #if  (_USE_LONG_TIME_T)
+ fprintf(f, "%ld %s\n", h->time, h->file);
  #else
- fprintf(f, "%lld %s\n", h->time, h->file);
+ #if defined(PRId64)
+ fprintf(f, "%" PRId64 " %s\n", h->time, h->file);
+ #else
+ fprintf(f, "%lld %s\n", h->time, h->file);
+ #endif
  #endif
  for (l = h->includes; l; l = list_next (l))
  {
diff --git a/src/plugins/compilergcc/depslib/src/hash.c b/src/plugins/compilergcc/depslib/src/hash.c
index 78823e43..57e00297 100644
--- a/src/plugins/compilergcc/depslib/src/hash.c
+++ b/src/plugins/compilergcc/depslib/src/hash.c
@@ -42,6 +42,9 @@
 # include "jam.h"
 # include "hash.h"
 
+/* C::B patch: Compatibility with 64 bit compiler / OS*/
+# include "inttypes.h" // PRId64
+
 /* Header attached to all data items entered into a hash table. */
 
 struct hashhdr {
@@ -266,8 +269,8 @@ hashstat( struct hash *hp )
 #ifdef __LP64__ /* avoid warning on 64-bit machines */
  printf( "%s table: %d+%d+%d (%dK+%luK) items+table+hash, %f density\n",
 /* C::B patch: Compatibility with 64 bit compiler / OS*/
-#elif defined(_WIN64)
- printf( "%s table: %d+%d+%d (%dK+%I64dK) items+table+hash, %f density\n",
+#elif defined(PRId64)
+ printf( "%s table: %d+%d+%d (%dK+%" PRId64 "K) items+table+hash, %f density\n",
 #else
  printf( "%s table: %d+%d+%d (%dK+%dK) items+table+hash, %f density\n",
 #endif
--
2.42.0.windows.2


If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.