public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/59801] New: GCC does not warn on unused global variable
@ 2014-01-14  8:26 chengniansun at gmail dot com
  2014-01-14  9:02 ` [Bug c/59801] " jakub at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: chengniansun at gmail dot com @ 2014-01-14  8:26 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

            Bug ID: 59801
           Summary: GCC does not warn on unused global variable
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chengniansun at gmail dot com

Created attachment 31829
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=31829&action=edit
the test case to trigger the bug.

I used the following command 

$gcc -Wall -Wextra -pedantic -std=c11 -c unused-var.c

And GCC outputs no warnings.


However in the source file unused-var.c, the last global variable "g_1853" is
NOT used. 

//////////////////////////////  Start of the source file /////////////
/**
 * compile this program with the following command
 *
 * gcc -c -pedantic -std=c11 -Wall -Wextra unused-var.c
 */


static int g_37 = (-1L);
static int *g_187 = &g_37;

/**
 * this variable will NOT be reported although it is NOT used.
 */
static int ** volatile g_1853 = &g_187;/* VOLATILE GLOBAL g_1853 */

//////////////////////////////  End of the source file /////////////


The Gcc version is 4.9.0, built on 01/09/2014
gcc (GCC) 4.9.0 20140109 (experimental)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
@ 2014-01-14  9:02 ` jakub at gcc dot gnu.org
  2014-01-14 17:32 ` chengniansun at gmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-01-14  9:02 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |jakub at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Don't mark the variable volatile then.  Volatile tells the compiler the
variable may be used through means not known to the compiler, so it isn't
unused.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
  2014-01-14  9:02 ` [Bug c/59801] " jakub at gcc dot gnu.org
@ 2014-01-14 17:32 ` chengniansun at gmail dot com
  2014-01-16  6:08 ` su at cs dot ucdavis.edu
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: chengniansun at gmail dot com @ 2014-01-14 17:32 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #2 from Chengnian Sun <chengniansun at gmail dot com> ---
Thanks for your reply. One more question regarding this issue. Support I have a
closed program

static volatile int a;
int main() {return 0;}

Even though "a" is not read anywhere in this program, do you mean that it is
still possible for "a" to be used somewhere else?

I checked the C standard
draft(http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). In Page 122,
it says that what constitutes an access to an object that has volatile-qualified
type is implementation-defined. Is this the reason why GCC thinks the variable
is used, but Clang treats it unused? 

Thanks again.
>From gcc-bugs-return-440345-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Tue Jan 14 18:08:08 2014
Return-Path: <gcc-bugs-return-440345-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 16545 invoked by alias); 14 Jan 2014 18:08:07 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 15976 invoked by uid 48); 14 Jan 2014 18:08:01 -0000
From: "ppluzhnikov at google dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/59812] New: Missing aggressive loop optimization warning
Date: Tue, 14 Jan 2014 18:08:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: new
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: c
X-Bugzilla-Version: unknown
X-Bugzilla-Keywords:
X-Bugzilla-Severity: enhancement
X-Bugzilla-Who: ppluzhnikov at google dot com
X-Bugzilla-Status: UNCONFIRMED
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter
Message-ID: <bug-59812-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2014-01/txt/msg01487.txt.bz2
Content-length: 1119

http://gcc.gnu.org/bugzilla/show_bug.cgi?idY812

            Bug ID: 59812
           Summary: Missing aggressive loop optimization warning
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ppluzhnikov at google dot com

Google ref: b/12015772

The following source:

int x[4];
int foo (int a)
{
 int n = a ? 1 : 2;
 int i;
 for(i = 0; i < 4 + n; i++ )
   x[i]++;
 return 0;
}


compiles into infinite loop due to aggressive loop optimization,
but produces no warnings with

g++ (GCC) 4.9.0 20140110 (experimental)

g++ -c -O2 -Wall -Wextra t.c && objdump -d t.o

t.o:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <_Z3fooi>:
   0:   b8 00 00 00 00          mov    $0x0,%eax
   5:   83 00 01                addl   $0x1,(%rax)
   8:   48 83 c0 04             add    $0x4,%rax
   c:   eb f7                   jmp    5 <_Z3fooi+0x5>


It would be much nicer to end-user to emit compile time warning here.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
  2014-01-14  9:02 ` [Bug c/59801] " jakub at gcc dot gnu.org
  2014-01-14 17:32 ` chengniansun at gmail dot com
@ 2014-01-16  6:08 ` su at cs dot ucdavis.edu
  2014-01-16  6:33 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: su at cs dot ucdavis.edu @ 2014-01-16  6:08 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

Zhendong Su <su at cs dot ucdavis.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |su at cs dot ucdavis.edu

--- Comment #3 from Zhendong Su <su at cs dot ucdavis.edu> ---
For the given code, it does seem make sense to issue a warning (whether or not
"volatile" is used) because this is likely a bug in the code. 

$ gcc-trunk -Wunused-variable small.c
$ clang-trunk -Wunused-variable small.c
small.c:1:21: warning: unused variable 'a' [-Wunused-variable]
static volatile int a;
                    ^
1 warning generated.
$ cat small.c
static volatile int a;

int 
main () 
{
  return 0;
}

----------------------

This also seems to be a missed optimization opportunity: 

$ gcc-trunk -O3 -S small.c
$ cat small.s
    .file    "small.c"
    .section    .text.unlikely,"ax",@progbits
.LCOLDB0:
    .section    .text.startup,"ax",@progbits
.LHOTB0:
    .p2align 4,,15
    .globl    main
    .type    main, @function
main:
.LFB0:
    .cfi_startproc
    xorl    %eax, %eax
    ret
    .cfi_endproc
.LFE0:
    .size    main, .-main
    .section    .text.unlikely
.LCOLDE0:
    .section    .text.startup
.LHOTE0:
    .local    a
    .comm    a,4,4
    .ident    "GCC: (GNU) 4.9.0 20140115 (experimental) [trunk revision
206638]"
    .section    .note.GNU-stack,"",@progbits


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (2 preceding siblings ...)
  2014-01-16  6:08 ` su at cs dot ucdavis.edu
@ 2014-01-16  6:33 ` pinskia at gcc dot gnu.org
  2014-01-16  6:34 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-16  6:33 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---


*** This bug has been marked as a duplicate of bug 57258 ***


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (3 preceding siblings ...)
  2014-01-16  6:33 ` pinskia at gcc dot gnu.org
@ 2014-01-16  6:34 ` pinskia at gcc dot gnu.org
  2014-01-16  6:52 ` su at cs dot ucdavis.edu
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2014-01-16  6:34 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This change was done on purpose.  See the patch and thread at
http://gcc.gnu.org/ml/gcc-patches/2013-11/msg00847.html


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (4 preceding siblings ...)
  2014-01-16  6:34 ` pinskia at gcc dot gnu.org
@ 2014-01-16  6:52 ` su at cs dot ucdavis.edu
  2014-01-16  8:38 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: su at cs dot ucdavis.edu @ 2014-01-16  6:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #6 from Zhendong Su <su at cs dot ucdavis.edu> ---
> *** This bug has been marked as a duplicate of bug 57258 ***

Andrew, I'm confused. The two are different, right?  57258 is about a possible
extra warning, and this one is about a missing warning.  What did I miss?
Thanks.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (5 preceding siblings ...)
  2014-01-16  6:52 ` su at cs dot ucdavis.edu
@ 2014-01-16  8:38 ` jakub at gcc dot gnu.org
  2014-01-16 21:18 ` su at cs dot ucdavis.edu
  2014-01-16 21:27 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-01-16  8:38 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Those two cases are different, the other PR was about automatic volatile
variables, where if they are unused it is really hard to come up with a way how
they could be modified behind compiler's back.  While for this case, variable
in .data/.bss etc. section it is not so hard, the variable has a name through
which it can be referenced, eventhough only from within the same assembly file.
 User could inject some assembly which will access it, the var can use section
attribute that places it into some magic section that is e.g. IO mapped, etc.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (6 preceding siblings ...)
  2014-01-16  8:38 ` jakub at gcc dot gnu.org
@ 2014-01-16 21:18 ` su at cs dot ucdavis.edu
  2014-01-16 21:27 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: su at cs dot ucdavis.edu @ 2014-01-16 21:18 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #8 from Zhendong Su <su at cs dot ucdavis.edu> ---
(In reply to Jakub Jelinek from comment #7)
> Those two cases are different, the other PR was about automatic volatile
> variables, where if they are unused it is really hard to come up with a way
> how they could be modified behind compiler's back.  While for this case,
> variable in .data/.bss etc. section it is not so hard, the variable has a
> name through which it can be referenced, eventhough only from within the
> same assembly file.  User could inject some assembly which will access it,
> the var can use section attribute that places it into some magic section
> that is e.g. IO mapped, etc.

Thanks Jakub. So, this is confirmed then?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/59801] GCC does not warn on unused global variable
  2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
                   ` (7 preceding siblings ...)
  2014-01-16 21:18 ` su at cs dot ucdavis.edu
@ 2014-01-16 21:27 ` jakub at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2014-01-16 21:27 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59801

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
It is IMHO working correctly and looks like clang bug to me.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2014-01-16 21:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-01-14  8:26 [Bug c/59801] New: GCC does not warn on unused global variable chengniansun at gmail dot com
2014-01-14  9:02 ` [Bug c/59801] " jakub at gcc dot gnu.org
2014-01-14 17:32 ` chengniansun at gmail dot com
2014-01-16  6:08 ` su at cs dot ucdavis.edu
2014-01-16  6:33 ` pinskia at gcc dot gnu.org
2014-01-16  6:34 ` pinskia at gcc dot gnu.org
2014-01-16  6:52 ` su at cs dot ucdavis.edu
2014-01-16  8:38 ` jakub at gcc dot gnu.org
2014-01-16 21:18 ` su at cs dot ucdavis.edu
2014-01-16 21:27 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).