www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DMD Source Guidance: Unused Return Values of Strictly Pure Function

reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
Does anybody know where in the DMD source I can figure out if the 
return value of a function call is used or not?

I'm trying to figure out how to implement automatic detection of 
unused return values from calls to strictly pure functions.

The closest I have come is the function functionParameters() in 
expression.c

Ideas anyone?
Feb 27 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Thursday, 27 February 2014 at 23:38:34 UTC, Nordlöw wrote:
 Does anybody know where in the DMD source I can figure out if 
 the return value of a function call is used or not?

 I'm trying to figure out how to implement automatic detection 
 of unused return values from calls to strictly pure functions.

 The closest I have come is the function functionParameters() in 
 expression.c

 Ideas anyone?
The return type of course also must be non-void in order for the warning to printed.
Feb 27 2014
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 Does anybody know where in the DMD source I can figure out if 
 the return value of a function call is used or not?

 I'm trying to figure out how to implement automatic detection 
 of unused return values from calls to strictly pure functions.

 The closest I have come is the function functionParameters() in 
 expression.c

 Ideas anyone?
See: https://d.puremagic.com/issues/show_bug.cgi?id=3882 Bye, bearophile
Feb 27 2014
parent reply =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 https://d.puremagic.com/issues/show_bug.cgi?id=3882

 Bye,
 bearophile
Did you set this as WONTFIX because of too many warnings from functions that may throw or just do asserts such as unittests? If so does anyone see any way to restrict warnings even further for example by checking if a function will call assert or nothrow or do a debug print?
Feb 27 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
I believe I found a good solution to Issue 3882:
https://d.puremagic.com/issues/show_bug.cgi?id=3882

It works as expected and I found two bugs in my code with it.

My current solution is to add the following at line 147 in 
dmd/src/sideeffect.c:

       case TOKcall:
             /* Issue 3882: Don't complain about calling functions 
with no effect,
              * because purity and nothrow are inferred, and 
because some of the
              * runtime library depends on it. Needs more 
investigation.
              */
             if (global.params.warnings && !global.gag)
             {
                 if (e->type->ty == Tvoid)
                 {
                     /* TODO: Restrict this message to call 
hierarchies that
                      * never call assert (and or not called from 
inside
                      * unittest blocks) */
                     // e->warning("Call %s with void return has 
no effect", e->toChars());
                 }
                 else
                 {
                     e->warning("Call %s with no effect discards 
return value", e->toChars());
                 }
             }
             return;

Does it suffice to just do a pull request referring to Issue 3882.

/Per
Feb 28 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Nordlöw:

 Did you set this as WONTFIX because of too many warnings from 
 functions that may throw or just do asserts such as unittests?
It's a "RESOLVED LATER". I think it was not me to tag it like that. And it was tagged like that because of too many warnings in templated functions. Bye, bearophile
Feb 28 2014
next sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
On Friday, 28 February 2014 at 10:40:04 UTC, bearophile wrote:
 Nordlöw:

 Did you set this as WONTFIX because of too many warnings from 
 functions that may throw or just do asserts such as unittests?
It's a "RESOLVED LATER". I think it was not me to tag it like that. And it was tagged like that because of too many warnings in templated functions. Bye, bearophile
My tested application is massive I got not "false positives". I can do some more tests... /Per
Feb 28 2014
prev sibling parent =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= <per.nordlow gmail.com> writes:
 that. And it was tagged like that because of too many warnings 
 in templated functions.
Were those warnings emitted also by non-void return functions? /Per
Feb 28 2014
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/28/2014 12:38 AM, "Nordlöw" wrote:
 Does anybody know where in the DMD source I can figure out if the return
 value of a function call is used or not?
I'm not familiar with the DMD source code, but a good way to proceed is usually to grep for existing error messages that are somehow related. In this case, DMD already complains about some cases of unused values: $ grep "no effect" *.c declaration.c: error("storage class 'auto' has no effect if type is not inferred, did you mean 'scope'?"); expression.c: e = new CastExp(loc, e, Type::tvoid); // avoid "has no effect" error mtype.c: // substitution has no effect on function pointer type. sideeffect.c: * Don't complain about an expression with no effect if it was cast to void sideeffect.c: /* Don't complain about calling functions with no effect, sideeffect.c: error("%s has no effect", toChars()); sideeffect.c: error("%s has no effect in expression (%s)", This leads directly to sideeffect.c: Expression::discardValue(), which seems to be the place where you want to insert your detection code.
Feb 27 2014