diff options
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | ext/iconv/extconf.rb | 1 | ||||
-rw-r--r-- | ext/iconv/iconv.c | 6 | ||||
-rw-r--r-- | lib/mkmf.rb | 41 |
4 files changed, 48 insertions, 6 deletions
@@ -1,4 +1,8 @@ -Sun Jan 17 14:18:01 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> +Sun Jan 17 14:23:23 2010 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * ext/iconv/iconv.c (iconv_convert): suppress a warning. + + * lib/mkmf.rb (check_signedness): new method. * lib/mkmf.rb (have_header, create_header): use String#tr_cpp. diff --git a/ext/iconv/extconf.rb b/ext/iconv/extconf.rb index 9632bf4db9..c4a57c80d6 100644 --- a/ext/iconv/extconf.rb +++ b/ext/iconv/extconf.rb @@ -7,6 +7,7 @@ conf = with_config("config-charset", enable_config("config-charset", conf)) if have_func("iconv", "iconv.h") or have_library("iconv", "iconv", "iconv.h") + check_signedness("size_t") if checking_for("const of iconv() 2nd argument") do create_tmpsrc(cpp_include("iconv.h") + "---> iconv(cd,0,0,0,0) <---") src = xpopen(cpp_command("")) {|f|f.read} diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index c4c6fd8124..cf77ee3e2c 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -464,7 +464,11 @@ iconv_convert(iconv_t cd, VALUE str, long start, long length, int toidx, struct errmsg[0] = 0; error = iconv_try(cd, &inptr, &inlen, &outptr, &outlen); - if (0 <= outlen && outlen <= sizeof(buffer)) { + if ( +#if SIGNEDNESS_OF_SIZE_T < 0 + 0 <= outlen && +#endif + outlen <= sizeof(buffer)) { outlen = sizeof(buffer) - outlen; if (NIL_P(error) || /* something converted */ outlen > (size_t)(inptr - tmpstart) || /* input can't contain output */ diff --git a/lib/mkmf.rb b/lib/mkmf.rb index 656c47314b..cccaf371d4 100644 --- a/lib/mkmf.rb +++ b/lib/mkmf.rb @@ -985,6 +985,11 @@ def have_const(const, headers = nil, opt = "", &b) end end +STRING_OR_FAILED_FORMAT = "%s" +def STRING_OR_FAILED_FORMAT.%(x) + x ? super : "failed" +end + # Returns the size of the given +type+. You may optionally specify additional # +headers+ to search in for the +type+. # @@ -1002,10 +1007,7 @@ def check_sizeof(type, headers = nil, opts = "", &b) prelude << "static rbcv_typedef_ *rbcv_ptr_;\n" prelude = [prelude] expr = "sizeof((*rbcv_ptr_)#{"." << member if member})" - fmt = "%s" - def fmt.%(x) - x ? super : "failed" - end + fmt = STRING_OR_FAILED_FORMAT checking_for checking_message("size of #{type}", headers), fmt do if UNIVERSAL_INTS.include?(type) type @@ -1021,6 +1023,37 @@ def check_sizeof(type, headers = nil, opts = "", &b) end end +# Returns the signedness of the given +type+. You may optionally +# specify additional +headers+ to search in for the +type+. +# +# If the +type+ is found and is a numeric type, a macro is passed as a +# preprocessor constant to the compiler using the +type+ name, in +# uppercase, prepended with 'SIGNEDNESS_OF_', followed by the +type+ +# name, followed by '=X' where 'X' is positive integer if the +type+ is +# unsigned, or negative integer if the +type+ is signed. +# +# For example, if size_t is defined as unsigned, then +# check_signedness('size_t') would returned +1 and the +# SIGNEDNESS_OF_SIZE_T=+1 preprocessor macro would be passed to the +# compiler, and SIGNEDNESS_OF_INT=-1 if check_signedness('int') is +# done. +# +def check_signedness(type, headers = nil) + signed = nil + checking_for("signedness of #{type}", STRING_OR_FAILED_FORMAT) do + if try_static_assert("(#{type})-1 < 0") + signed = -1 + elsif try_static_assert("(#{type})-1 > 0") + signed = +1 + else + next nil + end + $defs.push("-DSIGNEDNESS_OF_%s=%+d" % [type.tr_cpp, signed]) + signed < 0 ? "signed" : "unsigned" + end + signed +end + # :stopdoc: # Used internally by the what_type? method to determine if +type+ is a scalar |