Bug 5026 - __attribute__ ((unused) seems misdocumented
Summary: __attribute__ ((unused) seems misdocumented
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.0.1
: P3 normal
Target Milestone: 4.5.0
Assignee: Not yet assigned to anyone
URL:
Keywords: documentation
Depends on:
Blocks:
 
Reported: 2001-12-05 20:46 UTC by benoit.hudson
Modified: 2011-03-01 10:21 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-11 21:51:46


Attachments
a C file with and without unused tags (130 bytes, text/plain)
2003-11-05 14:15 UTC, benoit.hudson
Details

Note You need to log in before you can comment on or make changes to this bug.
Description benoit.hudson 2001-12-05 20:46:01 UTC
node (gcc.info)Function Attributes says:
`unused'
     This attribute, attached to a function, means that the function is
     meant to be possibly unused.  GCC will not produce a warning for
     this function.  GNU C++ does not currently support this attribute
     as definitions without parameters are valid in C++.


Does that last sentence make any sense at all?

Release:
3.0.1
Comment 1 benoit.hudson 2001-12-05 20:46:01 UTC
Fix:
Here's a possible rewriting which may be clearer overall:

`unused'
     GCC -Wunused will warn about functions it finds that
     are not used.  Attaching this attribute to a function
     will disable the warning for that function.
Comment 2 Wolfgang Bangerth 2002-12-10 16:53:43 UTC
State-Changed-From-To: open->analyzed
State-Changed-Why: This still applies. I agree that the last sentence of the
    attribute description is un-understandable:
    ---------------
    @cindex @code{unused} attribute.
    @item unused
    This attribute, attached to a function, means that the function is meant
    to be possibly unused.  GCC will not produce a warning for this
    function.  GNU C++ does not currently support this attribute as
    definitions without parameters are valid in C++.
    -----------------
    
    What is the supposed meaning of it? Unfortunately, the CVS
    history does not reach back far enough...
Comment 3 Ben Elliston 2003-02-18 13:42:40 UTC
Responsible-Changed-From-To: unassigned->bje
Responsible-Changed-Why: I'll take this.
Comment 4 Dara Hazeghi 2003-07-18 23:30:56 UTC
Ben, this has been assigned to you for a few months, any progress?
Comment 5 Ben Elliston 2003-07-20 23:57:24 UTC
Subject: Re:  __attribute__ ((unused) seems misdocumented

"dhazeghi at yahoo dot com" <gcc-bugzilla@gcc.gnu.org> writes:

> Ben, this has been assigned to you for a few months, any progress?

I guess I picked it off the stack to work on it, but never got
started.  Feel free to reassign it to anyone interested.

Ben

Comment 6 Andrew Pinski 2003-08-03 15:42:45 UTC
Reassign this one to no one.
Comment 7 Nathanael C. Nerode 2003-08-03 15:47:52 UTC
The last sentence is supposed to mean "attribute unused is not supported by
G++", plus some bogus rationale.  Which is false anyway, I think.
Comment 8 Andrew Pinski 2003-11-05 07:20:43 UTC
I think the last sentence means that c++ supports that definition of agruments can be 
unamed, other than that the unused is not misdocumented.
Comment 9 benoit.hudson 2003-11-05 14:15:00 UTC
Created attachment 5065 [details]
a C file with and without unused tags

Here's a test case that shows three possible uses of __attribute__((unused)). 
These are the three uses I can think of; there may be more.

The three uses are:
- suppress a warning about a static symbol that is never used
- suppress a warning about an argument that is never used
- suppress a warning about a local symbol that is never used

If you compile the same file, but with a .cpp extension to compile as C++, gcc
gives an error when we try to attach an __attribute__ to a function argument.
Comment 10 benoit.hudson 2003-11-05 14:28:01 UTC
Given that, how is the following wording:

    @cindex @code{unused} attribute.
    @item unused
    This attribute suppresses the warning about a variable or function that is
    not being used anywhere in its scope.
Comment 11 Andrew Pinski 2004-01-31 20:29:46 UTC
Actually now with 3.4.0 the part about C++ is wrong because now you can use the attribute in 
C++.  The problem is that the attribute can be put on variables also not just functions.
Comment 12 msp 2004-02-24 13:37:30 UTC
(In reply to comment #11)
> Actually now with 3.4.0 the part about C++ is wrong because now you can use
the attribute in 
> C++.  The problem is that the attribute can be put on variables also not just
functions.

Actually for C the attribute can also be applied to labels! BUT not for C++.

The following code (based on the default output from bison 1.875), will compile
in C mode, but not in C++ mode:-

void fred()
{
   int x;
   
yyerrlab1:
 __attribute__ ((__unused__))

1 x = 42;
 
}

The intention is to tell the compiler that yyerrlab1: may be unused, but in C++
mode you get the following error:-

p.cc: In function `void fred()':
p.cc:8: error: ISO C++ forbids declaration of `x' with no type
p.cc:8: error: redeclaration of `int x'
p.cc:3: error: `int x' previously declared here
p.cc:8: error: declaration of `int x'
p.cc:3: error: conflicts with previous declaration `int x'

It also allows its use And even more cunningly it can (or should be) possible to
attach it to labels.
Comment 13 msp 2004-02-24 13:41:18 UTC
(In reply to comment #12)
> (In reply to comment #11)

Sorry, my example got mangled when I copied it in....

void fred()
{
   int x;
   
yyerrlab1:
 __attribute__ ((__unused__))

 x = 42;
 
}
Comment 14 Wolfgang Bangerth 2004-02-24 14:21:26 UTC
The reason why it is not allowed in C++ on labels has been stated 
several times: in C++, a variable declaration can follow a label, 
so the attribute that you want to attach to the label really goes 
to the next statement; gcc expects it to be a declaration, and 
complains if it can't find one. 
 
W. 
Comment 15 msp 2004-02-24 14:35:37 UTC
Subject: Re:  __attribute__ ((unused) seems misdocumented

Does that mean that there is no alternative way of tell gcc, in c++ mode,
that the label might not be used?

If so, I will raise a bug/enhancement request against bison suggesting
they change their usage to be #ifndef __cplusplus or something.

Cheers
Mark



On Tue, 24 Feb 2004, bangerth at dealii dot org wrote:

>
> ------- Additional Comments From bangerth at dealii dot org  2004-02-24 14:21 -------
> The reason why it is not allowed in C++ on labels has been stated
> several times: in C++, a variable declaration can follow a label,
> so the attribute that you want to attach to the label really goes
> to the next statement; gcc expects it to be a declaration, and
> complains if it can't find one.
>
> W.
>
>

Comment 16 Wolfgang Bangerth 2004-02-24 14:52:09 UTC
There indeed doesn't seem to be a way. 
W. 
Comment 17 Giovanni Bajo 2004-02-24 18:46:13 UTC
Isn't this supposed to work:

mylabel __attribute__((unused)): 

Does it work? Maybe it should.
Comment 18 Wolfgang Bangerth 2004-02-24 19:09:39 UTC
I think it would be sensible if it would. But I checked this morning, 
and it doesn't. 
 
W. 
Comment 19 Dmitry Gorbachev 2011-03-01 06:41:57 UTC
GCC documentation has been changed. __attribute__((unused)) on labels can be used in C++ code since GCC version 4.5.0. This PR should be closed probably.
Comment 20 Manuel López-Ibáñez 2011-03-01 08:51:32 UTC
(In reply to comment #19)
> GCC documentation has been changed. __attribute__((unused)) on labels can be
> used in C++ code since GCC version 4.5.0. This PR should be closed probably.

The less, the merrier. Thanks!

(If you get a gcc.gnu.org account, you can do bug management: http://gcc.gnu.org/svnwrite.html#authenticated)