https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71456

            Bug ID: 71456
           Summary: missing -Wunused-variable on a static global
                    initialized with another
           Product: gcc
           Version: 7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

While looking into bug 71402 I noticed that -Wunused-variable doesn't diagnose
unused static namespace scope variables that are dynamically initialized, while
emitting the dynamic initialization for them (see the test case below).

I think this is a problem (defect) for a number of reasons:

1) It doesn't match what the documentation promises:
  -Wunused-variable -- Warn whenever a local or static variable is unused aside
from its declaration.
2) The dynamic initialization (even when mostly optimized away at -O) isn't
without cost that users might want to know might be taking place unnecessarily
(in general, whether or not it is necessary depends on the initializer which
may be a function call, but in trivial cases like the one below it could be
eliminated altogether).
3) The behavior differs from that of Clang which does issue a warning in the
case below and also optimizes the variable out.  (Clang only diagnoses the
dynamic initialization with potential side-effects with -Wglobal-constructors,
and regardless of whether the static variable is used or not.)

I think the subset of (2) where the dynamic initialization has (or could have)
side-effects could be viewed as an enhancement request for a new warning
pointing out that it takes places even though the variable is unused.

I view the rest of this report (i.e., the mismatch with documentation noted in
(1), and the rest of (2) where the dynamic initializer is known not to have
side-effects and could be diagnosed and omitted) as a bug.

$ cat zzz.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc -B
/home/msebor/build/gcc-trunk-svn/gcc -S -Wall -Wextra -Wpedantic
-fdump-tree-optimized=/dev/stdout -xc++ zzz.c

static int i;
static int j = i;   // -Wunused-variable warning expected

;; Function void __static_initialization_and_destruction_0(int, int)
(_Z41__static_initialization_and_destruction_0ii, funcdef_no=0, decl_uid=2245,
cgraph_uid=0, symbol_order=2)

void __static_initialization_and_destruction_0(int, int) (int __initialize_p,
int __priority)
{
  int i.0_1;

  <bb 2>:
  if (__initialize_p_3(D) == 1)
    goto <bb 3>;
  else
    goto <bb 5>;

  <bb 3>:
  if (__priority_5(D) == 65535)
    goto <bb 4>;
  else
    goto <bb 5>;

  <bb 4>:
  i.0_1 = i;
  j = i.0_1;

  <bb 5>:
  return;

}



;; Function (static initializers for zzz.c) (_GLOBAL__sub_I_zzz.c,
funcdef_no=1, decl_uid=2249, cgraph_uid=1, symbol_order=3)

(static initializers for zzz.c) ()
{
  <bb 2>:
  __static_initialization_and_destruction_0 (1, 65535);
  return;

}

Reply via email to