diff mbox

libiberty: {count,dup,write}argv: constify argv input slightly

Message ID 1451803183-18387-1-git-send-email-vapier@gentoo.org
State New
Headers show

Commit Message

Mike Frysinger Jan. 3, 2016, 6:39 a.m. UTC
Would be more useful if we could use "const char * const *", but there's
a long standing bug where gcc warns about incompatible pointers when you
try to pass in "char **".  We can at least constify the array itself as
gcc will not warn in that case.

include/:
2016-01-03  Mike Frysinger  <vapier@gentoo.org>

	* libiberty.h (dupargv): Change arg to char * const *.
	(writeargv, countargv): Likewise.

libiberty/:
2016-01-03  Mike Frysinger  <vapier@gentoo.org>

	* argv.c (dupargv): Change arg to char * const *.  Update comment.
	(writeargv, countargv): Likewise.
	* functions.texi (dupargv, writeargv, countargv): Likewise.
---
 include/libiberty.h      |  6 +++---
 libiberty/argv.c         | 12 ++++++------
 libiberty/functions.texi |  6 +++---
 3 files changed, 12 insertions(+), 12 deletions(-)

Comments

Ian Lance Taylor Jan. 5, 2016, 3:32 p.m. UTC | #1
On Sat, Jan 2, 2016 at 10:39 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> Would be more useful if we could use "const char * const *", but there's
> a long standing bug where gcc warns about incompatible pointers when you
> try to pass in "char **".

That's not a bug.  It's how C works.  http://c-faq.com/ansi/constmismatch.html

> We can at least constify the array itself as
> gcc will not warn in that case.
>
> include/:
> 2016-01-03  Mike Frysinger  <vapier@gentoo.org>
>
>         * libiberty.h (dupargv): Change arg to char * const *.
>         (writeargv, countargv): Likewise.
>
> libiberty/:
> 2016-01-03  Mike Frysinger  <vapier@gentoo.org>
>
>         * argv.c (dupargv): Change arg to char * const *.  Update comment.
>         (writeargv, countargv): Likewise.
>         * functions.texi (dupargv, writeargv, countargv): Likewise.

This is OK if it bootstraps.  Thanks.

Ian
Mike Frysinger Jan. 5, 2016, 8:20 p.m. UTC | #2
On 05 Jan 2016 07:32, Ian Lance Taylor wrote:
> On Sat, Jan 2, 2016 at 10:39 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> > Would be more useful if we could use "const char * const *", but there's
> > a long standing bug where gcc warns about incompatible pointers when you
> > try to pass in "char **".
> 
> That's not a bug.  It's how C works.  http://c-faq.com/ansi/constmismatch.html

i'm referring to the bugs filed/discussing the issue in the gcc tracker.
i'm aware of the (crap) standards.

> > We can at least constify the array itself as
> > gcc will not warn in that case.
> >
> > include/:
> > 2016-01-03  Mike Frysinger  <vapier@gentoo.org>
> >
> >         * libiberty.h (dupargv): Change arg to char * const *.
> >         (writeargv, countargv): Likewise.
> >
> > libiberty/:
> > 2016-01-03  Mike Frysinger  <vapier@gentoo.org>
> >
> >         * argv.c (dupargv): Change arg to char * const *.  Update comment.
> >         (writeargv, countargv): Likewise.
> >         * functions.texi (dupargv, writeargv, countargv): Likewise.
> 
> This is OK if it bootstraps.  Thanks.

i've been testing this in gdb because gcc doesn't actually use dupargv or
countargv.  it does use writeargv in all of three places.

that said, it does bootstrap
-mike
diff mbox

Patch

diff --git a/include/libiberty.h b/include/libiberty.h
index 8e096a0..a9c885f 100644
--- a/include/libiberty.h
+++ b/include/libiberty.h
@@ -80,7 +80,7 @@  extern void freeargv (char **);
 /* Duplicate an argument vector. Allocates memory using malloc.  Use
    freeargv to free the vector.  */
 
-extern char **dupargv (char **) ATTRIBUTE_MALLOC;
+extern char **dupargv (char * const *) ATTRIBUTE_MALLOC;
 
 /* Expand "@file" arguments in argv.  */
 
@@ -88,11 +88,11 @@  extern void expandargv (int *, char ***);
 
 /* Write argv to an @-file, inserting necessary quoting.  */
 
-extern int writeargv (char **, FILE *);
+extern int writeargv (char * const *, FILE *);
 
 /* Return the number of elements in argv.  */
 
-extern int countargv (char**);
+extern int countargv (char * const *);
 
 /* Return the last component of a path name.  Note that we can't use a
    prototype here because the parameter is declared inconsistently
diff --git a/libiberty/argv.c b/libiberty/argv.c
index 5c3dd70..994dd35 100644
--- a/libiberty/argv.c
+++ b/libiberty/argv.c
@@ -49,7 +49,7 @@  Boston, MA 02110-1301, USA.  */
 
 /*
 
-@deftypefn Extension char** dupargv (char **@var{vector})
+@deftypefn Extension char** dupargv (char * const *@var{vector})
 
 Duplicate an argument vector.  Simply scans through @var{vector},
 duplicating each argument until the terminating @code{NULL} is found.
@@ -62,7 +62,7 @@  argument vector.
 */
 
 char **
-dupargv (char **argv)
+dupargv (char * const *argv)
 {
   int argc;
   char **copy;
@@ -279,7 +279,7 @@  char **buildargv (const char *input)
 
 /*
 
-@deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file})
+@deftypefn Extension int writeargv (char * const *@var{argv}, FILE *@var{file})
 
 Write each member of ARGV, handling all necessary quoting, to the file
 named by FILE, separated by whitespace.  Return 0 on success, non-zero
@@ -290,7 +290,7 @@  if an error occurred while writing to FILE.
 */
 
 int
-writeargv (char **argv, FILE *f)
+writeargv (char * const *argv, FILE *f)
 {
   int status = 0;
 
@@ -463,7 +463,7 @@  expandargv (int *argcp, char ***argvp)
 
 /*
 
-@deftypefn Extension int countargv (char **@var{argv})
+@deftypefn Extension int countargv (char * const *@var{argv})
 
 Return the number of elements in @var{argv}.
 Returns zero if @var{argv} is NULL.
@@ -473,7 +473,7 @@  Returns zero if @var{argv} is NULL.
 */
 
 int
-countargv (char **argv)
+countargv (char * const *argv)
 {
   int argc;
 
diff --git a/libiberty/functions.texi b/libiberty/functions.texi
index b5f4e80..24dcc37 100644
--- a/libiberty/functions.texi
+++ b/libiberty/functions.texi
@@ -176,7 +176,7 @@  Concatenate zero or more of strings and return the result in freshly
 @end deftypefn
 
 @c argv.c:470
-@deftypefn Extension int countargv (char **@var{argv})
+@deftypefn Extension int countargv (char * const *@var{argv})
 
 Return the number of elements in @var{argv}.
 Returns zero if @var{argv} is NULL.
@@ -213,7 +213,7 @@  make it easy to compose the values of multiple blocks.
 @end deftypefn
 
 @c argv.c:52
-@deftypefn Extension char** dupargv (char **@var{vector})
+@deftypefn Extension char** dupargv (char * const *@var{vector})
 
 Duplicate an argument vector.  Simply scans through @var{vector},
 duplicating each argument until the terminating @code{NULL} is found.
@@ -1915,7 +1915,7 @@  does the return value.  The third argument is unused in @libib{}.
 @end deftypefn
 
 @c argv.c:286
-@deftypefn Extension int writeargv (const char **@var{argv}, FILE *@var{file})
+@deftypefn Extension int writeargv (char * const *@var{argv}, FILE *@var{file})
 
 Write each member of ARGV, handling all necessary quoting, to the file
 named by FILE, separated by whitespace.  Return 0 on success, non-zero