Created attachment 28709 [details] Minimal example source code gfortran-4.7.2 -Wall gives a warning about unused module variables when those variables are only used in a namelist. Test: compile the attached minimal example source file test.f90 with: gfortran -v -Wall -Werror test.f90 -o test The following warning is printed: test.f90:14.6: use data, only: a 1 Warning: Unused module variable 'a' which has been explicitly imported at (1) The warning goes away when print*,a is uncommented in line 20. Verbose output is in the attached file compile_output.txt Get: warning about unused module variable Expect: no warning Reproduceable: always gfortran version: gcc version 4.7.2 (Gentoo 4.7.2 p1.3, pie-0.5.5)
Created attachment 28710 [details] Verbose output from compilation of example source file
Confirmed. I guess we should either set attr.referenced in gfc_match_namelist (match.c), or check for attr.in_namelist in generate_local_decl (trans-decl.c).
(In reply to comment #2) > I guess we should either set attr.referenced in gfc_match_namelist (match.c), > or check for attr.in_namelist in generate_local_decl (trans-decl.c). The latter is what we do for related cases. Proposed patch: Index: gcc/fortran/trans-decl.c =================================================================== --- gcc/fortran/trans-decl.c (revision 193567) +++ gcc/fortran/trans-decl.c (working copy) @@ -4589,23 +4589,26 @@ generate_local_decl (gfc_symbol * sym) } /* Warn for unused variables, but not if they're inside a common - block, a namelist, or are use-associated. */ + block or a namelist. */ else if (warn_unused_variable - && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark - || sym->attr.in_namelist)) + && !(sym->attr.in_common || sym->mark || sym->attr.in_namelist)) { - gfc_warning ("Unused variable '%s' declared at %L", sym->name, - &sym->declared_at); - if (sym->backend_decl != NULL_TREE) - TREE_NO_WARNING(sym->backend_decl) = 1; + if (sym->attr.use_only) + { + gfc_warning ("Unused module variable '%s' which has been " + "explicitly imported at %L", sym->name, + &sym->declared_at); + if (sym->backend_decl != NULL_TREE) + TREE_NO_WARNING(sym->backend_decl) = 1; + } + else if (!sym->attr.use_assoc) + { + gfc_warning ("Unused variable '%s' declared at %L", + sym->name, &sym->declared_at); + if (sym->backend_decl != NULL_TREE) + TREE_NO_WARNING(sym->backend_decl) = 1; + } } - else if (warn_unused_variable && sym->attr.use_only) - { - gfc_warning ("Unused module variable '%s' which has been explicitly " - "imported at %L", sym->name, &sym->declared_at); - if (sym->backend_decl != NULL_TREE) - TREE_NO_WARNING(sym->backend_decl) = 1; - } /* For variable length CHARACTER parameters, the PARM_DECL already references the length variable, so force gfc_get_symbol_decl
(In reply to comment #3) > Proposed patch: ... regtests cleanly.
Created attachment 28726 [details] My adaptation of the patch in #3 This solves the problem for me, thank you very much - I'm impressed by your quick and competent work :-) Somehow, your patch didn't work in my Gentoo version; I applied your changes manually and created the attached patch, which can be added to the Gentoo ebuild for sys-devel/gcc-4.7.2.
(In reply to comment #5) > This solves the problem for me, thank you very much You're welcome ... > I'm impressed by your quick and competent work :-) Thanks! In terms of gfortran bugs, this is certainly one of the easier ones to fix. > Somehow, your patch didn't work in my Gentoo version; My patch was against trunk, so it might not apply cleanly to the 4.7 branch. The fix will presumably make it into the 4.8 release, but will probably not be backported to 4.7 (unless you can show that the bug is a regression, i.e. did not occur in a previous release of gfortran - I haven't checked this).
(In reply to comment #6) > The fix will presumably make it into the 4.8 release, but will probably not be > backported to 4.7 (unless you can show that the bug is a regression, i.e. did > not occur in a previous release of gfortran - I haven't checked this). Just checked: The bogus warning with -Wall reported here seems to be a 4.7 regression indeed. I verified that it does not occur with 4.3.4, 4.5.3 and 4.6.0.
Author: janus Date: Wed Nov 21 22:19:51 2012 New Revision: 193711 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193711 Log: 2012-11-21 Janus Weil <janus@gcc.gnu.org> PR fortran/55352 * trans-decl.c (generate_local_decl): Don't warn for explicitly imported but unused module variables which are in a namelist or common block. 2012-11-21 Janus Weil <janus@gcc.gnu.org> PR fortran/55352 * gfortran.dg/namelist_76.f90: New. Added: trunk/gcc/testsuite/gfortran.dg/namelist_76.f90 Modified: trunk/gcc/fortran/ChangeLog trunk/gcc/fortran/trans-decl.c trunk/gcc/testsuite/ChangeLog
Author: janus Date: Fri Nov 23 19:05:14 2012 New Revision: 193766 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=193766 Log: 2012-11-23 Janus Weil <janus@gcc.gnu.org> PR fortran/55352 * trans-decl.c (generate_local_decl): Don't warn for explicitly imported but unused module variables which are in a namelist or common block. 2012-11-23 Janus Weil <janus@gcc.gnu.org> PR fortran/55352 * gfortran.dg/namelist_76.f90: New. Added: branches/gcc-4_7-branch/gcc/testsuite/gfortran.dg/namelist_76.f90 Modified: branches/gcc-4_7-branch/gcc/fortran/ChangeLog branches/gcc-4_7-branch/gcc/fortran/trans-decl.c branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
The patch in comment 3 has been applied to trunk and the 4.7 branch, which means the bug will be fixed in the 4.7.3 and 4.8.0 releases. Closing as fixed. Thanks for the bugreport!