aboutsummaryrefslogtreecommitdiffstats
path: root/doc/README.developer
diff options
context:
space:
mode:
authorJoão Valverde <j@v6e.pt>2021-12-17 16:40:28 +0000
committerWireshark GitLab Utility <gerald+gitlab-utility@wireshark.org>2021-12-18 07:24:46 +0000
commita8acda8fccb3f3611675cbf37226e7efa58d59bc (patch)
tree2176a9fa4381568ce87aa0d6fffd6a9523a82128 /doc/README.developer
parentf4123939d1b4f90e5ff35f1891703f5030f29922 (diff)
Docs: Add more info about transitioning to C99 fixed-width types
Diffstat (limited to 'doc/README.developer')
-rw-r--r--doc/README.developer30
1 files changed, 23 insertions, 7 deletions
diff --git a/doc/README.developer b/doc/README.developer
index e17ed030df..c022ae0dc3 100644
--- a/doc/README.developer
+++ b/doc/README.developer
@@ -126,7 +126,10 @@ an "int-sized" unsigned quantity, use "unsigned"; if you want a boolean,
use "bool" (defined in <stdbool.h>). You don't need to explicitly include
these headers; they are included in <wireshark.h>. Use that instead.
-To print fixed width integers you MUST use the macros provided in <inttypes.h>.
+To print fixed width integers you must use the macros provided in <inttypes.h>.
+
+ int uint32_t var;
+ printf("var = " PRIu32 "\n", var);
Don't use "long" to mean "signed 32-bit integer", and don't use
"unsigned long" to mean "unsigned 32-bit integer"; "long"s are 64 bits
@@ -185,21 +188,34 @@ C11. Sometimes it may be necessary to use an unsavory cast or two or abuse
a macro to bridge the two codebases during the transition. Such is life,
use your judgement and do the best possible under the circumstances.
+Avoid GLib synonyms like gchar and gint and especially don't
+use gpointer and gconstpointer, unless you are writing GLib callbacks
+and trying to match their signature exactly. These just obscure the
+code and gconstpointer in particular is just semantically weird and poor style.
+
When printing or displaying the values of 64-bit integral data types,
don't use "%lld", "%llu", "%llx", or "%llo" - not all platforms
-support "%ll" for printing 64-bit integral data types. Instead, for
-GLib routines, and routines that use them, such as all the routines in
-Wireshark that take format arguments, use G_GINT64_MODIFIER, for example:
+support "%ll" for printing 64-bit integral data types. Instead use
+the macros in <inttypes.h>, for example:
proto_tree_add_uint64_format_value(tree, hf_uint64, tvb, offset, len,
- val, "%" G_GINT64_MODIFIER "u", val);
+ val, "%" PRIu64, val);
+
+For GLib routines, and only those, you can choose whichever format style
+you prefer:
+
+ uint64_t val = UINT64_C(1);
+ char *str1 = g_strdup_printf("%" G_GUINT64_FORMAT, val);
+ char *str2 = g_strdup_printf("%" PRIu64, val);
+
+These format macros will be the same modulo any GLib bugs.
When specifying an integral constant that doesn't fit in 32 bits, don't
use "LL" at the end of the constant - not all compilers use "LL" for
-that. Instead, put the constant in a call to the "G_GINT64_CONSTANT()"
+that. Instead, put the constant in a call to the "INT64_C()" or "UINT64_C()"
macro, e.g.
- G_GINT64_CONSTANT(-11644473600), G_GUINT64_CONSTANT(11644473600)
+ INT64_C(-11644473600), UINT64_C(11644473600)
rather than