This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is there any way to give a warning when a nested function use its parent function's local variable value.
- From: Mark Farnell <mark dot farnell at gmail dot com>
- To: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Sun, 23 Nov 2014 21:00:34 +1300
- Subject: Re: Is there any way to give a warning when a nested function use its parent function's local variable value.
- Authentication-results: sourceware.org; auth=none
- References: <CAHQM7829YBL1OJyWeWFZ=sc3LsVeq-jZi=rqM46O+Zs9aW30Ng at mail dot gmail dot com>
I don't think you need a warning in this particular case, perhaps a
better way would be for the compiler to enforce a rule that forbids
access of function pointers of inner functions that referencing local
variables of its parent function.
For example, both:
void *foo(int x)
{
int a;
void foo2(int i)
{
// I want warning for the below code.
if (i == a){}
x += i;
a += i;
}
foo2(a);
return foo2;
}
and:
void *bar;
void foo(int x)
{
int a;
void foo2(int i)
{
// I want warning for the below code.
if (i == a){}
x += i;
a += i;
}
foo2(a);
bar = foo2;
}
should return an error.
On Sun, Nov 23, 2014 at 7:25 PM, xinglp <xinglp@gmail.com> wrote:
> Such as this code
>
> void foo(int x)
> {
> int a;
> void foo2(int i)
> {
> // I want warning for the below code.
> if (i == a){}
> x += i;
> a += i;
> }
> foo2(a);
> }
>
> Because I use nested function for better code orgnization and callback. Thanks.