This patch modifies the global types.h header to define a number of
macros used for the formatted output of fixed-length integers like
grub_uint64_t. Currently we use the traditional "%llu" format
specifiers, adding casts as required to assuage the loud GCC.

However, casts to shut the compiler up are generally a bad idea, and the
fact that this kind of output occurs mostly in debug code (printing an
inode number, for example) which is dispersed about the GRUB codebase
makes it very likely that the eventual breakage of one of these
assumptions will result in either a stream of warnings/errors or, given
that we are using casts to shut the compiler up, runtime weirdness.

Using these C99-like format specifiers will allow us to have the
relevant assumptions centralized in a single file, namely
<grub/types.h>. Thus, if and when one is broken - for example, when
sizeof(void*)!=sizeof(long), as it happens in mingw64 - the fix will be
way simpler to develop and deploy.

For example, using a fictional MyFS:

typedef struct {
    grub_uint64_t ino_num;
    grub_uint16_t perm_flags;
} myfs_ino_t;
myfs_ino_t *cur = ...

The patch will turn something like this:

    grub_dprintf("myfs", "Inode number %llu has permissions %03o",
                 (unsigned long) grub_be_to_cpu64(cur->ino_num),
                 grub_be_to_cpu16(cur->perm_flags));

Into this:

    grub_dprintf("myfs", "Inode number %"GRUB_PRI64u" has permissions "
                 "%03"GRUB_PRI16o, grub_be_to_cpu64(cur->ino_num),
                 grub_be_to_cpu16(cur->perm_flags));

-- 
-- Lazy, Oblivious, Recurrent Disaster -- Habbit
Index: include/grub/types.h
===================================================================
--- include/grub/types.h	(revision 2438)
+++ include/grub/types.h	(working copy)
@@ -58,23 +58,53 @@
 # endif
 #endif
 
-/* Define various wide integers.  */
+/* Define various wide integers and their format specifiers.  */
 typedef signed char		grub_int8_t;
 typedef short			grub_int16_t;
 typedef int			grub_int32_t;
-#if GRUB_CPU_SIZEOF_VOID_P == 8
+#define GRUB_PRIi8 i
+#define GRUB_PRIi16 i
+#define GRUB_PRIi32 i
+#define GRUB_PRId8 d
+#define GRUB_PRId16 d
+#define GRUB_PRId32 d
+#if GRUB_CPU_SIZEOF_LONG == 8
 typedef long			grub_int64_t;
+#define GRUB_PRIi64 li
+#define GRUB_PRId64 ld
 #else
 typedef long long		grub_int64_t;
+#define GRUB_PRIi64 lli
+#define GRUB_PRId64 lld
 #endif
 
 typedef unsigned char		grub_uint8_t;
 typedef unsigned short		grub_uint16_t;
 typedef unsigned		grub_uint32_t;
-#if GRUB_CPU_SIZEOF_VOID_P == 8
+#define GRUB_PRIu8 u
+#define GRUB_PRIu16 u
+#define GRUB_PRIu32 u
+#define GRUB_PRIo8 o
+#define GRUB_PRIo16 o
+#define GRUB_PRIo32 o
+#define GRUB_PRIx8 x
+#define GRUB_PRIx16 x
+#define GRUB_PRIx32 x
+#define GRUB_PRIX8 X
+#define GRUB_PRIX16 X
+#define GRUB_PRIX32 X
+#if GRUB_CPU_SIZEOF_LONG == 8
 typedef unsigned long		grub_uint64_t;
+#define GRUB_PRIu64 lu
+#define GRUB_PRIo64 lo
+#define GRUB_PRIx64 lx
+#define GRUB_PRIX64 lX
 #else
 typedef unsigned long long	grub_uint64_t;
+#define GRUB_PRIu64 llu
+#define GRUB_PRIo64 llo
+#define GRUB_PRIx64 llx
+#define GRUB_PRIX64 llX
 #endif
 
 /* Misc types.  */
@@ -100,7 +130,7 @@
 typedef grub_int32_t	grub_ssize_t;
 #endif
 
-#if GRUB_CPU_SIZEOF_VOID_P == 8
+#if GRUB_CPU_SIZEOF_LONG == 8
 # define GRUB_ULONG_MAX 18446744073709551615UL
 # define GRUB_LONG_MAX 9223372036854775807L
 # define GRUB_LONG_MIN (-9223372036854775807L - 1)

Attachment: signature.asc
Description: Esto es una parte de mensaje firmado digitalmente

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to