This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2] New mechanism for declaring const-covariant string functions.
On Tue, Nov 22, 2016 at 1:06 AM, Paul Eggert <eggert@cs.ucla.edu> wrote:
> Zack Weinberg wrote:
>>
>> This patch absorbs the entire mess into a pair of macros,
>> __CONST_COV_PROTO and __CONST_COV_BUILTIN, which are far less
>> error-prone to use, and which make the public header files much nicer
>> to read, too.
>
> Question: if I get the types wrong with a C or C++ program, what do the
> resulting diagnostics look like?
The diagnostics are unfortunately not as easy to read. For the same
program you posted, I get this with gcc 6:
strchr-wrong-types.c: In function ‘foo’:
strchr-wrong-types.c:5:17: error: passing argument 1 of ‘strchr’ from
incompatible pointer type [-Werror=incompatible-pointer-types]
return strchr (x, p);
^
In file included from ../include/bits/const-covariance.h:1:0,
from ../string/string.h:39,
from ../include/string.h:56,
from strchr-wrong-types.c:1:
../string/string.h:181:22: note: expected ‘const char *’ but argument
is of type ‘int *’
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:90:17: note: in definition of macro
‘__CONST_COV_PROTO’
extern rtype name __CC_XPROTO(const rtype, __VA_ARGS__) __THROW attrs
^~~~
../string/string.h:181:1: note: in expansion of macro ‘__CONST_COV_BUILTIN’
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^~~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors
... and this with g++ 6 ...
strchr-wrong-types.c: In function ‘char* foo(int*, int)’:
strchr-wrong-types.c:5:21: error: no matching function for call to
‘strchr(int*&, int&)’
return strchr (x, p);
^
In file included from ../include/bits/const-covariance.h:1:0,
from ../string/string.h:39,
from ../include/string.h:56,
from strchr-wrong-types.c:1:
../string/string.h:181:22: note: candidate: char* strchr(char*, int)
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:119:5: note: in definition of macro
‘__CONST_COV_BUILTIN’
name __CC_XPROTO (rtype, __VA_ARGS__) __THROW \
^~~~
../string/string.h:181:22: note: no known conversion for argument 1
from ‘int*’ to ‘char*’
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:119:5: note: in definition of macro
‘__CONST_COV_BUILTIN’
name __CC_XPROTO (rtype, __VA_ARGS__) __THROW \
^~~~
../string/string.h:181:22: note: candidate: const char* strchr(const char*, int)
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:122:5: note: in definition of macro
‘__CONST_COV_BUILTIN’
name __CC_XPROTO (const rtype, __VA_ARGS__) __THROW \
^~~~
../string/string.h:181:22: note: no known conversion for argument 1
from ‘int*’ to ‘const char*’
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:122:5: note: in definition of macro
‘__CONST_COV_BUILTIN’
name __CC_XPROTO (const rtype, __VA_ARGS__) __THROW \
^~~~
cc1plus: all warnings being treated as errors
clang 3.9 doesn't flood the screen with a bunch of macro expansions,
but what you do get is still a bit confusing:
strchr-wrong-types.c:5:17: error: incompatible pointer types passing 'int *' to
parameter of type 'const char *' [-Werror,-Wincompatible-pointer-types]
return strchr (x, p);
^
../string/string.h:182:28: note: passing argument to parameter '__s' here
char *, __s, int, __c);
^
or in C++ mode
strchr-wrong-types.c:5:9: error: no matching function for call to 'strchr'
return strchr (x, p);
^~~~~~
../string/string.h:181:22: note: candidate function not viable: no known
conversion from 'int *' to 'const char *' for 1st argument
__CONST_COV_BUILTIN (strchr, __attribute_pure__ __nonnull ((1)),
^
../string/bits/const-covariance.h:92:54: note: expanded from macro
'__CONST_COV_BUILTIN'
# define __CONST_COV_BUILTIN(...) __CONST_COV_PROTO (__VA_ARGS__)
^
../string/bits/const-covariance.h:90:17: note: expanded from macro
'__CONST_COV_PROTO'
extern rtype name __CC_XPROTO(const rtype, __VA_ARGS__) __THROW attrs
^
(Note: if testing against an uninstalled build tree you will need to
pass both -D_ISOMAC and -D__NO_STRING_INLINES to reproduce these
errors.)