RE: No warning on unused variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrew Haley wrote:
> Dave Williss writes:
>  > It might be nice if g++ could add an __attribute__ setting 
> so you could 
>  > tag a class with something like ctor_has_no_useful_side_effects.  
>  > Classes with this attribute could then be allowed to raise 
> the "declared 
>  > but not used" warning.  It would be most useful on things like 
>  > std::string, which you also wouldn't want just laying around.
> 
> It's an interesting idea, for sure.  I can't immediately think of any
> big downside, but I'm not sure if such a patch would be accepted.

This attribute could generalize to things other than constructors.

The name is wrong, because the side effect of initializing an object
is usually useful. It's only not useful when there is no subsequent use
of the object.

The general attribute would assert that the function's only effect is
to write to the object(s) that are passed into it. In other words,
it would promise that the function does not cause the pointer to
escape.

And in fact, this attribute would be better on a parameter than on the
function. Of course, in C++ constructors you don't have an explicit
parameter, so the attribute would go on the function, but be understood
to be on the object.

Examples:

  // 1
  void foo(int *p __attribute__((noescape)), int *q);

  // 2: noescape refers to ``bar *this'' parameter.
  bar::bar(int *q) __attribute__ ((noescape));

If you know that the pointer does not escape (the function does not
cause it to be remembered after it returns) that is exactly what you
need to know to determine whether the referent object has a next use.

This could have other uses besides generating warnings:

  {
    int i, j;
    noescape_arg(&i);
    i = 3;
    external_function();

    /* i can still be cached in a register
       because we know that external_function
       has no access to i! */

    noescape_arg(&j);
    /* Elided call, j has no next use */
  }


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux