This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: testing that a Gimple call argument is a string...


On Fri, Oct 19, 2012 at 10:59 AM, Basile Starynkevitch
<basile@starynkevitch.net> wrote:
> On Fri, Oct 19, 2012 at 10:26:58AM +0200, Richard Biener wrote:
>> On Thu, Oct 18, 2012 at 10:33 PM, Basile Starynkevitch
>> <basile@starynkevitch.net> wrote:
>> > Hello
>> >
>> > I'm coding in MELT the ex06/ of https://github.com/bstarynk/melt-examples/
>> > which should typecheck calls to variadic functions json_pack & json_unpack
>> > from http://www.digip.org/jansson (a JSON library in C).
>> >
>> > I'm working on a MELT pass on Gimple/SSA after phiopt (maybe that place is wrong?)
>> >
>> > I don't understand well how to check that a given Gimple argument is a string.
>> > I was thinking of using useless_type_conversion_p or types_compatible_p on the TREE_TYPE of some POINTER_TYPE with char_type_node, but it seems to not work as I would expect
>> >
>> > How would you (in C++ or C for a 4.6) code such a test (that argument 2 of some GIMPLE_CALL is a string, ie. a char* in C parlance)?
>>
>> You would look at the function signature of the function called, not at its
>> argument.  The argument can be a void * and still be a "string" (well, a pointer
>> to a string).  GIMPLE doesn't care about the actual type pointed to.
>
> GCC is using the syntactic type of the argument for printf. The following code
>   void f (char *s)
>   { printf ("%s\n", (void *) s); }
> gives when compiled with gcc -Wall
>   f.c:4:1: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘void *’ [-Wformat]
> and we are in the case where the argument is a string casted to (void*); I don't want to give more clever warnings.

The format warnings are output from the frontend which does not have GIMPLE.

> So I don't follow you. I want to typecheck (at least in the common simple cases) the calls to that variadic function.
> I don't care that much about its signature.
>
> To be concrete, <jansson.h> declares
>    json_t *json_pack(const char *fmt, ...);
>
> and the documentation http://www.digip.org/jansson/doc/2.4/apiref.html#building-values
> suggests that
>    /* Create the JSON integer 42 */
>    json_pack("i", 42);
>    /* Create the JSON array ["foo", "bar", true] */
>    json_pack("[ssb]", "foo", "bar", 1);
>
> I just want to extend GCC (thru a MELT extension, but that could be a GCC plugin in C++) to detect
> incorrect calls like
>
>    json_pack("i", /*that should be an int:*/ 3.14);
> or
>    json_pack("[ss]", "foo", /* that should be a string*/ 3
>              /*and the arity is wrong an argument is missing*/);
>
> I believe I should use
>    extern bool useless_type_conversion_p (tree, tree);
> from gcc/gimple.h (it is funny that function is in gimple.h, I would expected it to be in tree.h)
> with one of the argument being char_type_node

the predicate is only valid on GIMPLE, not on GENERIC.  GIMPLE ignores
all non-value-changing conversions while GENERIC does not.

> But I might have trouble in using it.

Indeed you will experience that it might do something sensible for simple
testcases.  But as I said above, you will not reliably be able to distinguish
a valid char * pointer from an invalid int * pointer just by looking at the
pointer type.

Richard.


> Thanks for reading. Regards.
> --
> Basile STARYNKEVITCH         http://starynkevitch.net/Basile/
> email: basile<at>starynkevitch<dot>net mobile: +33 6 8501 2359
> 8, rue de la Faiencerie, 92340 Bourg La Reine, France
> *** opinions {are only mines, sont seulement les miennes} ***


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]