Hi Bruno, Bruno Haible <[email protected]> writes:
> Collin Funk wrote: >> On Windows (x86_64-w64-mingw32), I see the following warning: >> >> $ gnulib-tool --create-testdir --dir testdir1 >> $ cd testdir1 >> $ ./configure CFLAGS='-Wdiscarded-qualifiers' >> $ cd glllib >> $ make setlocale.o >> depbase=`echo setlocale.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\ >> gcc -DHAVE_CONFIG_H -DEXEEXT=\".exe\" -DEXEEXT=\".exe\" -DNO_XMALLOC >> -DEXEEXT=\".exe\" -I. -I.. -DGNULIB_STRICT_CHECKING=1 -I/mingw64/include >> -Wdiscarded-qualifiers -MT setlocale.o -MD -MP -MF $depbase.Tpo -c -o >> setlocale.o setlocale.c &&\ >> mv -f $depbase.Tpo $depbase.Po >> setlocale.c: In function 'setlocale_single': >> setlocale.c:889:12: warning: return discards 'const' qualifier from >> pointer target type [-Wdiscarded-qualifiers] >> 889 | return setlocale_messages (locale); >> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~ >> >> The attached patch fixes it by casting away the const. > > I don't like this because, although in line with the previous code, it > adds one more cast. I agree with your dislike of additional casts. I figured it was okay in this case, given the similar one in setlocale_mtsafe when the result of setlocale_null is returned. Anyway, glad I asked before committing. >> Another >> reasonable solution is changing the definition of setlocale_messages to >> return a plain "char *". > > Returning a 'char *' instead of 'const char *'? This goes in the backward > direction. > > Look: ISO C has 'const char *' since 1990. It also has 'setlocale' since 1990. > POSIX > <https://pubs.opengroup.org/onlinepubs/9799919799/functions/setlocale.html> > says: > "The application shall not modify the string returned." > Which means that the intended return type is 'const char *'. The return type > 'char *' is a historical accident. (Probably the ANSI C89 committee simply > lacked the courage to add 'const'.) > > But we can add the intended return type in Gnulib. Done as follows. This > patch removes two casts, instead of adding a cast. I considered suggesting this as well for the same reason you mention, i.e., the standard saying the returned string cannot be modified. But I mistakenly assumed you would not want to diverge from the standardized prototype. Perhaps it is worth bringing this up with the ISO C people. I have CC'd Alex in case he decides he wants more work to do. I vaguely remember POSIX being willing to change the return type of gettext to be "const char *" for similar reasons to this getlocale case. I can hunt down those links again if they will be useful for a proposal. Collin
