Erroneous gfortran warning of unused module variable when variable is only used in namelist

gfortran-4.7.2 -Wall gives a warning about unused module variables when those variables are only used in a namelist:
Warning: Unused module variable ‘x’ which has been explicitly imported at (1)
The warning goes away when the variable is used elsewhere.

Below are a patch and a Gentoo ebuild file to apply that patch to the gcc source code before compiling. The patch was written by janus@gcc and adapted by AstroFloyd, the ebuild was adapted from gcc-4.7.2.ebuild in Gentoo portage to apply the patch. Gentoo users can obtain the patched version of gcc by installing sys-devel/gcc-4.7.2-r99 from the AstroFloyd overlay. The ebuild itself is here. See the gcc bug report and Gentoo bug report for more details.


Patch: gfortran-unused-mod-namelist.patch

        Bugfix of:  http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55352
        gfortran-4.7.2 -Wall gives a warning about unused module variables when those
        variables are only used in a namelist

        Reported by: AstroFloyd
        Patch by: janus at gcc

--- gcc/fortran/trans-decl.c.old        2012-08-06 16:34:27.000000000 +0200
+++ gcc/fortran/trans-decl.c    2012-11-18 16:48:56.521125252 +0100
@@ -4586,24 +4586,27 @@
        }
 
       /* 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
         even when not referenced.  If optimize > 0, it will be optimized
Advertisements
This entry was posted in All, Software and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s