[PATCH]: Re: Regression with strn-stuff

Pedro Alves pedro_alves@portugalmail.pt
Tue Sep 19 10:37:00 GMT 2006


>>> The double double-quotes ("") were there to check if the parameter
>>> was a compile time const string, since it is legal in C to write a 
>>> string this way:
>>> ("string part 1" "string part 2").
>>>     Isn't the double quotes version OK?
>>>     
>>
>>>>   
> Attached is a patch to do just that.

(...)

> If this turns out to be too limiting, we will indeed have to turn
> to __builtin_constant_p for gcc...
> 
> Please (test,) review, and commit.
> 

I withdraw this patch. I found some places where this was too
limiting. It turns out that in some places the ()'s around
the passed in string literal were really needed,
making ("a string") "" uncompilable.
I don't think there is any way in C to implement the
REMOVE_PARENS macro below, is there?

#define REMOVE_PARENS(S) (?)
REMOVE_PARENS(("a string")) => "A STRING"

Cheers,
Pedro Alves


> 
> ---
> bfd/ChangeLog
> 
> 2006-09-17  Pedro Alves  <pedro_alves@portugalmail.pt>
> 
> 
>    * bfd-in.h (CONST_STRNEQ): Make it error out if STR2 is not a compile 
> time constant string.
>    (CONST_STRNCPY): Likewise.
>    * bfd-in2.h: Regenerate.
> 
> 
> ------------------------------------------------------------------------
> 
> Index: bfd-in.h
> ===================================================================
> RCS file: /cvs/src/src/bfd/bfd-in.h,v
> retrieving revision 1.117
> diff -u -p -r1.117 bfd-in.h
> --- bfd-in.h	16 Sep 2006 18:12:13 -0000	1.117
> +++ bfd-in.h	17 Sep 2006 21:59:33 -0000
> @@ -57,9 +57,13 @@ extern "C" {
>     the arguments to the strncmp() macro.  Hence this alternative
>     definition of strncmp is provided here.
>     
> -   Note - these macros do NOT work if STR2 is not a constant string.  */
> -#define CONST_STRNEQ(STR1,STR2) (strncmp ((STR1), (STR2), sizeof (STR2) - 1) == 0)
> -#define CONST_STRNCPY(STR1,STR2) strncpy ((STR1), (STR2), sizeof (STR2) - 1)
> +   Note - the double double-quotes ("") are there to ensure that
> +   STR2 is a compile-time constant string. 
> +   This works, because in C it is possible to define a string
> +   in parts like in:
> +   const char* str = "str part 1" "str part 2";  */
> +#define CONST_STRNEQ(STR1,STR2) (strncmp (STR1, STR2 "", sizeof (STR2) - 1) == 0)
> +#define CONST_STRNCPY(STR1,STR2) strncpy (STR1, STR2 "", sizeof (STR2) - 1)
>  
>  
>  /* The word size used by BFD on the host.  This may be 64 with a 32



More information about the Binutils mailing list