Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

2015-04-27 Thread Junio C Hamano
Elia Pinto gitter.spi...@gmail.com writes:

 This is the second version of this patch.  It had not been
 discussed before. In the second version, I just tried to clarify
 the comment in the commit. I resend it just in case you missed

I do not recall seeing it before.  No discussion usually means no
interest, so I'll see what happens this time on the list for a few
days before picking it up.

 +#if SUPPORT__BUILTIN_TYPES_COMPATIBLE_P
 +/* arr[0] degrades to a pointer: a different type from an array */
 +#define _array_size_chk(arr) \
 + BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
 + typeof((arr)[0])))
 +#else
 +#define _array_size_chk(arr) 0
 +#endif

Wouldn't there be a more sensible name?  _chk does not tell us
anything about what is being checked, and the only thing this name
gives us is what uses it (i.e. it is some magic used by array-size
and does not say what it checks and what for).

I think you are checking arr is an array and not a pointer.  Perhaps
#define is_an_array(arr) or something along that line may be a
more descriptive name for it.

I doubt the leading underscore is particularly a good idea, though.

 +/*
 + * ARRAY_SIZE - get the number of elements in a visible array
 + *  at x: the array whose size you want.
 + *
 + * This does not work on pointers, or arrays declared as [], or
 + * function parameters.  With correct compiler support, such usage
 + * will cause a build error (see build_assert).
 + */
 +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + _array_size_chk(x))
  #define bitsizeof(x)  (CHAR_BIT * sizeof(x))
  
  #define maximum_signed_value_of_type(a) \
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

2015-04-27 Thread Jeff King
On Mon, Apr 27, 2015 at 10:56:30AM -0700, Junio C Hamano wrote:

 Elia Pinto gitter.spi...@gmail.com writes:
 
  This is the second version of this patch.  It had not been
  discussed before. In the second version, I just tried to clarify
  the comment in the commit. I resend it just in case you missed
 
 I do not recall seeing it before.  No discussion usually means no
 interest, so I'll see what happens this time on the list for a few
 days before picking it up.

I'm in favor of any method for improving compile-time bug-checking,
provided that:

  1. It does not carry a maintenance cost that spreads throughout the
 code base (e.g., here the cost is localized to making ARRAY_SIZE a
 bit more complex, but callers do not have to care about the extra
 checking).

  2. It gracefully degrades when using older compilers (i.e., we can
 fall back to the existing ARRAY_SIZE definition when the magic
 builtin is not available).

I think this is OK for both points. It would be nice if we could
auto-detect the presence of the builtin without using autoconf. I think
most git developers do not use autoconf at all, and the feature does not
help much if nobody turns it on. Is there a __GNUC__ or similar
feature-test macro we could use for this?

  +#define _array_size_chk(arr) 0
  +#endif
 
 Wouldn't there be a more sensible name?  _chk does not tell us
 anything about what is being checked, and the only thing this name
 gives us is what uses it (i.e. it is some magic used by array-size
 and does not say what it checks and what for).
 
 I think you are checking arr is an array and not a pointer.  Perhaps
 #define is_an_array(arr) or something along that line may be a
 more descriptive name for it.
 
 I doubt the leading underscore is particularly a good idea, though.

Agreed on the naming (modulo the not from your follow-on email). Also,
should it be in ALL_CAPS like the other added macros? We are slightly
inconsistent about that in our code-base.

-Peff
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

2015-04-27 Thread Junio C Hamano
Junio C Hamano gits...@pobox.com writes:

 +#if SUPPORT__BUILTIN_TYPES_COMPATIBLE_P
 +/* arr[0] degrades to a pointer: a different type from an array */
 +#define _array_size_chk(arr)
 \
 +BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
 +typeof((arr)[0])))
 +#else
 +#define _array_size_chk(arr) 0
 +#endif

 Wouldn't there be a more sensible name?  _chk does not tell us
 anything about what is being checked, and the only thing this name
 gives us is what uses it (i.e. it is some magic used by array-size
 and does not say what it checks and what for).

 I think you are checking arr is an array and not a pointer.  Perhaps
 #define is_an_array(arr) or something along that line may be a
 more descriptive name for it.

 I doubt the leading underscore is particularly a good idea, though.

And is_an_array(arr) is probably not quite a good name, as that
sounds as if it would give 1 and adding it to sizeof(x)/sizeof(x[0])
does not make sense.  barf_if_not_an_array() is what the macro does.
--
To unsubscribe from this list: send the line unsubscribe git in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html