diff options
author | Caolán McNamara <caolanm@redhat.com> | 2010-12-08 10:30:09 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2010-12-08 10:30:09 +0000 |
commit | 38c2ceeee6016c04282ba3cce8608d4e9ce2a621 (patch) | |
tree | ea4264547ba96a56d0eb9cf10750deb61a11d202 | |
parent | 36e2296bf8fae3c66a66eada3595cbb2628a4833 (diff) |
Use c++0x features to detect misuse of SAL_N_ELEMENTS and CONSTASCII
-rw-r--r-- | sal/inc/rtl/string.h | 4 | ||||
-rw-r--r-- | sal/inc/rtl/ustring.h | 2 | ||||
-rw-r--r-- | sal/inc/sal/macros.h | 26 |
3 files changed, 28 insertions, 4 deletions
diff --git a/sal/inc/rtl/string.h b/sal/inc/rtl/string.h index b9d1fdf42d97..f9c895dd852b 100644 --- a/sal/inc/rtl/string.h +++ b/sal/inc/rtl/string.h @@ -1088,7 +1088,7 @@ sal_Int32 SAL_CALL rtl_string_getToken( rtl_String ** newStr , rtl_String * str, its value should be 0x00. Depending on where this macro is used, the nature of the supplied expression might be further restricted. */ -#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)sizeof(constAsciiStr)-1) +#define RTL_CONSTASCII_STRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)SAL_N_ELEMENTS(constAsciiStr)-1) /** Supply the length of an ASCII string literal. @@ -1103,7 +1103,7 @@ sal_Int32 SAL_CALL rtl_string_getToken( rtl_String ** newStr , rtl_String * str, its value should be 0x00. Depending on where this macro is used, the nature of the supplied expression might be further restricted. */ -#define RTL_CONSTASCII_LENGTH( constAsciiStr ) ((sal_Int32)(sizeof(constAsciiStr)-1)) +#define RTL_CONSTASCII_LENGTH( constAsciiStr ) ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1)) /* ======================================================================= */ diff --git a/sal/inc/rtl/ustring.h b/sal/inc/rtl/ustring.h index ad55be973e91..426a672db841 100644 --- a/sal/inc/rtl/ustring.h +++ b/sal/inc/rtl/ustring.h @@ -1449,7 +1449,7 @@ sal_Int32 SAL_CALL rtl_uString_getToken( rtl_uString ** newStr , rtl_uString * s its value should be 0x00. Depending on where this macro is used, the nature of the supplied expression might be further restricted. */ -#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)(sizeof(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US +#define RTL_CONSTASCII_USTRINGPARAM( constAsciiStr ) constAsciiStr, ((sal_Int32)(SAL_N_ELEMENTS(constAsciiStr)-1)), RTL_TEXTENCODING_ASCII_US /* ======================================================================= */ diff --git a/sal/inc/sal/macros.h b/sal/inc/sal/macros.h index 1eeb165345ab..e4c35138266d 100644 --- a/sal/inc/sal/macros.h +++ b/sal/inc/sal/macros.h @@ -29,6 +29,8 @@ #ifndef _SAL_MACROS_H_ #define _SAL_MACROS_H_ +#include <stddef.h> + #ifndef SAL_MAX # define SAL_MAX(a,b) (((a) > (b)) ? (a) : (b)) #endif @@ -42,7 +44,29 @@ #endif #ifndef SAL_N_ELEMENTS -# define SAL_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) +# if defined(__cplusplus) && defined(__GXX_EXPERIMENTAL_CXX0X__) + /* + * Magic template to calculate at compile time the number of elements + * in an array. Enforcing that the argument must be a array and not + * a pointer, e.g. + * char *pFoo="foo"; + * SAL_N_ELEMENTS(pFoo); + * fails while + * SAL_N_ELEMENTS("foo"); + * or + * char aFoo[]="foo"; + * SAL_N_ELEMENTS(aFoo); + * pass + * + * Unfortunately if arr is an array of an anonymous class then we need + * C++0x, i.e. see + * http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#757 + */ + template <typename T, size_t S> char (&sal_n_array_size( T(&)[S] ))[S]; +# define SAL_N_ELEMENTS(arr) (sizeof(sal_n_array_size(arr))) +# else +# define SAL_N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0])) +# endif #endif #ifndef SAL_BOUND |