eserte / perl-tk

the perl module Tk
https://metacpan.org/release/Tk
Other
44 stars 31 forks source link

Tk/typemap: prevent compiler warnings #58

Closed chrstphrchvz closed 3 years ago

chrstphrchvz commented 5 years ago

Add casts to UV, and use "%"UVuf format specifiers instead of "%d", to prevent the following compiler warnings:

Tk.c: In function 'XS_Tk__FontRankInfo_encoding':
Tk.c:416:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:416:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_foundary':
Tk.c:444:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:444:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_Xname':
Tk.c:472:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:472:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_family':
Tk.c:500:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:500:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_size':
Tk.c:529:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:529:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_bold':
Tk.c:556:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:556:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
Tk.c: In function 'XS_Tk__FontRankInfo_italic':
Tk.c:583:13: warning: format '%d' expects argument of type 'int', but argument 2 has type 'STRLEN' {aka 'long unsigned int'} [-Wformat=]
       croak("ST(0) too small (%d) for p LangFontInfo * (%d)",sz,sizeof(*p));
             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~
Tk.c:583:13: warning: format '%d' expects argument of type 'int', but argument 3 has type 'long unsigned int' [-Wformat=]
eserte commented 5 years ago

I wonder if the casts are necessary. A smaller change seems to be sufficient to cease the warnings:

diff --git i/Tk/typemap w/Tk/typemap
index 2913885..56b581f 100644
--- i/Tk/typemap
+++ w/Tk/typemap
@@ -30,7 +30,7 @@ T_PVOBJ
            STRLEN sz;
            $var = ($type) SvPV((SV*)SvRV($arg),sz);
            if (sz != sizeof(*$var))
-            croak(\"$arg too small (%d) for $var $type (%d)\",sz,sizeof(*$var));
+            croak(\"$arg too small (%\"UVuf\") for $var $type (%\"UVuf\")\",sz,sizeof(*$var));
        }
        else
            croak(\"$var is not an object\")
chrstphrchvz commented 5 years ago

I think you have a point; UV should be large enough for implicit casts (promotion) to work. I have revised this to remove the explicit casts.

I had read https://perldoc.perl.org/perlguts.html#Formatted-Printing-of-Size_t-and-SSize_t (since STRLEN is MEM_SIZE a.k.a. Size_t), and had gotten the impression it was suggesting explicit casts, but now I wonder if that is not what it meant:

Formatted Printing of Size_t and SSize_t

The most general way to do this is to cast them to a UV or IV …

chrstphrchvz commented 5 years ago

UV should be large enough for implicit casts (promotion) to work

Actually, I don't think that's what's happening here. I found that a compiler will still complain if promotion were needed, e.g. if %lu is given an unsigned int rather than a long unsigned int. I guess that in this case, the size of UV is just right, and no cast is necessary.