This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC PATCH] Add set but not used warning support for the C FE (PR c/18624)


Hi!

The attached patch is my initial attempt at adding the set but not used
warning (for C for now).  Warning during gimplification is probably too
late, because eventhough the C FE now fully folds much less than it used to,
there are inevitably references which don't make it through to the
gimplifier.  TREE_USED is usually set in build_external_ref in the C FE,
which is on the other side too early, we don't know at that point whether
the reference is "read" or only "set".  So this patch marks variables as
"read" immediately after parsing when it is already known what kind of var
reference it is.

I've bootstrapped this on x86_64-linux with --disable-werror, no make check
yet, and attached is a list of these warnings during bootstrap (many of them
would be errors with -Werror).

I'm open for better names of the new functions/macros, if you have nice
suggestions, I'd appreciate it.

I wonder whether we want to control this warning with a separate -Wunused-*
switch (and whether to include it into -Wunused or not).

I haven't looked at all the warnings in detail, just on a lot of them, and
they looked correct.  In some cases they warn about real bugs (like the ones
David/Paolo fixed last night, or e.g. in cfgrtl.c:
          if (flag_reorder_blocks_and_partition
              && targetm.have_named_sections
              && e->src != ENTRY_BLOCK_PTR
              && BB_PARTITION (e->src) == BB_COLD_PARTITION
              && !(e->flags & EDGE_CROSSING))
            {
              rtx bb_note, cur_insn;

              bb_note = NULL_RTX;
              for (cur_insn = BB_HEAD (bb); cur_insn != NEXT_INSN (BB_END (bb));
                   cur_insn = NEXT_INSN (cur_insn))
                if (NOTE_INSN_BASIC_BLOCK_P (cur_insn))
                  {
                    bb_note = cur_insn;
                    break;
                  }

              if (JUMP_P (BB_END (bb))
                  && !any_condjump_p (BB_END (bb))
                  && (single_succ_edge (bb)->flags & EDGE_CROSSING))
                add_reg_note (BB_END (bb), REG_CROSSING_JUMP, NULL_RTX);
            }
where bb_note is computed using dedicated loop but never used), in some
cases they warn about cases where the set but not used warning is caused
by use of preprocessor macros (e.g. soft-fp, libbid), or e.g. in generated
code (insn-emit.c).  In the last case, e.g. getting rid of _regs_allocated
warning would be easy, output_peephole2_scratches could print
HARD_REG_SET _regs_allocated;\nCLEAR_HARD_REG_SET (_regs_allocated);\n
only on the first MATCH_SCRATCH, not unconditionally even when there are no
MATCH_SCRATCHes, but getting rid of the other insn-emit.c warnings might
instead result in some workarounds like (void) operands0; etc. added to the
output.

If anyone has suggestions what else should be tested in the testsuite of
this warning, I'd appreciate it too.  I guess I should e.g. test vars used
from nested functions, etc.

While the code computes the DECL_READ_P flag for both VAR_DECLs and
PARM_DECLs (I'm not using a decl_lang_N bit because c-common.c needs to set
it too), currently it warns just about function scope vars; to warn also
about PARM_DECLs the warning would need to be done in function.c, which
would require all other FEs to set it (perhaps just to TREE_USED).

Comments?

	Jakub

Attachment: Y470c
Description: Text document

Attachment: N1
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]