Bug 43899 - Wrong unused-variable warning with NAMELISTs
Summary: Wrong unused-variable warning with NAMELISTs
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-04-26 18:03 UTC by Tobias Burnus
Modified: 2015-10-10 13:05 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-04-26 19:05:23


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2010-04-26 18:03:16 UTC
Found at http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/4e34a377097524f2

gfortran gives a -Wall warning for a variable which is only used via namelists:
 character(4)  :: nml_string
                            1
Warning: Unused variable 'nml_string' declared at (1)

For:

  character(4)  :: nml_string
  namelist /nmlist/ nml_string
  read(7,nml=nmlist)
  write(*,nml=nmlist)
  end
Comment 1 Jerry DeLisle 2010-04-26 19:05:23 UTC
Well in a sense it is unused.  Regardless, adding a warning for the truncated string, the real issue, is straightforward and I will do so.
Comment 2 Tobias Burnus 2010-04-26 20:16:03 UTC
(In reply to comment #1)
> Well in a sense it is unused.

Well, but only in a sense - in the example a value is assigned to and then printed ...

> Regardless, adding a warning for the truncated
> string, the real issue, is straightforward and I will do so.

It might be straight forward, but it needs to be guarded by a special option as it is 100% valid code. I think printing a run-time warning by default is wrong.
Comment 3 Jerry DeLisle 2010-05-04 01:56:25 UTC
What I am thinking of is a warning if a quoted string is terminated by an end-of-line and there is no closing quote.

I would like to put this behind -Wcharacter-truncation which will be picked up with -Wall. Let's see what I can come up with.

Comment 4 Jerry DeLisle 2010-05-04 04:11:31 UTC
What about this?

$ gfc -fbounds-check pr43899.f90 
$ ./a.out 
 &NMLIST NML_STRING='123456789' /   
At line 9 of file pr43899.f90 (unit = 7, file = 'example.nml')
Fortran runtime error: Namelist object 'nml_string' exceeds length of character variable
Comment 5 Tobias Burnus 2010-05-04 05:53:28 UTC
(In reply to comment #4)
> What about this?
> 
> $ gfc -fbounds-check pr43899.f90 
> $ ./a.out 
>  &NMLIST NML_STRING='123456789' /   
> At line 9 of file pr43899.f90 (unit = 7, file = 'example.nml')
> Fortran runtime error: Namelist object 'nml_string' exceeds length of
> character variable

I like the message, but I do not like a run-time error with -fbounds-check; while it is good to have an easy-to-reach diagnostic like that, so far -fbounds-check only aborted if the program was invalid according to the Fortran standard. Here, the code probably has a problem, but the usage is valid according to the Fortran standard.

Thus, I would like to see either only a warning or a different compile-time flag.

Comment 6 Jerry DeLisle 2010-11-03 04:29:19 UTC
OK, I got back to this.  I will submit a patch shortly.

I now do this:

At line 9 of file pr43899.f90 (unit = 7, file = 'example.nml')
Fortran runtime warning: Namelist object 'nml_string' truncated on read.
Comment 7 Jerry DeLisle 2010-11-03 15:22:29 UTC
Author: jvdelisle
Date: Wed Nov  3 15:22:25 2010
New Revision: 166252

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=166252
Log:
2010-11-03  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/43899
	* runtime/error.c (generate_warning): New function to generate a run
	time warning message. Fix some whitespace.
	* libgfortran.h: Add prototype for new function.
	* io/list_read.c (nml_read_obj): Use new function to warn when a
	character namelist object is truncated.  Only warn if compiled
	with -fbounds-check.

Modified:
    trunk/libgfortran/ChangeLog
    trunk/libgfortran/io/list_read.c
    trunk/libgfortran/libgfortran.h
    trunk/libgfortran/runtime/error.c
Comment 8 Jerry DeLisle 2010-11-03 15:27:53 UTC
Author: jvdelisle
Date: Wed Nov  3 15:27:48 2010
New Revision: 166253

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=166253
Log:
2010-11-03  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR libgfortran/43899
	* gfortran.dg/namelist_67.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/namelist_67.f90
Modified:
    trunk/gcc/testsuite/ChangeLog
Comment 9 Tobias Burnus 2010-11-03 15:44:35 UTC
The patch in comment 7 and 8 adds a run-time warning (with -fcheck=bounds) if a string in a namelist gets truncated.

TODO: Comment 0, namely: Don't warn that a variable is unused, if the namelist to which they belong is used.
Comment 10 Jerry DeLisle 2010-11-03 17:47:51 UTC
This patchlet gets rid of the bogus warning.  All that remains is to check that namelists are used or not.  Also note that we do not now check to see if commons are used either.


Index: trans-decl.c
===================================================================
--- trans-decl.c	(revision 166182)
+++ trans-decl.c	(working copy)
@@ -4015,9 +4015,10 @@ generate_local_decl (gfc_symbol * sym)
 	}
 
       /* Warn for unused variables, but not if they're inside a common
-	 block or are use-associated.  */
+	 block, a namelist, or are use-associated.  */
       else if (warn_unused_variable
-	       && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
+	       && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark
+		    || sym->attr.in_namelist))
 	gfc_warning ("Unused variable '%s' declared at %L", sym->name,
 		     &sym->declared_at);
Comment 11 Jerry DeLisle 2010-11-09 00:08:24 UTC
Author: jvdelisle
Date: Tue Nov  9 00:08:20 2010
New Revision: 166461

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=166461
Log:
2010-11-08  Jerry DeLisle  <jvdelisle@gcc.gnu.org>

	PR fortran/43899
	* trans-decl.c (generate_local_decl): Do not generate unused warning
	for variables in namelists.

Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
Comment 12 Jerry DeLisle 2010-11-09 00:24:02 UTC
This latest patch gets rid of the bogus unused warning.  Remaining, but not real important is to warn on an unused namelist.  I think this will involve marking namelists as used when resolving or translating I/O statements and then at some point, scan for any namelists not marked.  I almost think its not worth it, but will leave this opened until others weigh in on it.

Even if a namelist is used, a particular variable within it may not be used.  For example, if the users nml file does not contain an assignment for a particular variable in the list, it will remain unused.  No value in the read operation.
Comment 13 Jerry DeLisle 2010-11-09 00:25:11 UTC
Waiting
Comment 14 Dominique d'Humieres 2013-06-20 10:15:49 UTC
> This latest patch gets rid of the bogus unused warning.  
> Remaining, but not real important is to warn on an unused namelist.  
> I think this will involve marking namelists as used when resolving 
> or translating I/O statements and then at some point, scan for any 
> namelists not marked.  I almost think its not worth it, but will 
> leave this opened until others weigh in on it.
>
> Even if a namelist is used, a particular variable within it may 
> not be used.  For example, if the users nml file does not contain 
> an assignment for a particular variable in the list, it will remain 
> unused.  No value in the read operation.

No activity since over two years. Closing as WONTFIX. Please reopen if I am wrong.
Comment 15 Tobias Burnus 2013-06-20 10:27:29 UTC
(In reply to Jerry DeLisle from comment #12)
> Remaining [...] is to warn on an unused namelist.  I think this will involve
> marking namelists as used when resolving or translating I/O statements and
> then at some point, scan for any namelists not marked.

I think the later scanning can be done when generating the DWARF debug symbols (cf. PR37132) in trans-decl.c. While not very important, I find such a warning useful. (One needs to be careful about those declared in a module or accessed via host association. Fortunately, namelists may not be declared in a BLOCK.)
Comment 16 Dominique d'Humieres 2013-06-20 14:33:27 UTC
(In reply to Tobias Burnus from comment #15)
> (In reply to Jerry DeLisle from comment #12)
> > Remaining [...] is to warn on an unused namelist.  I think this will involve
> > marking namelists as used when resolving or translating I/O statements and
> > then at some point, scan for any namelists not marked.
>
> I think the later scanning can be done when generating the DWARF debug 
> symbols (cf. PR37132) in trans-decl.c. While not very important, I find 
> such a warning useful. (One needs to be careful about those declared 
> in a module or accessed via host association. Fortunately, namelists 
> may not be declared in a BLOCK.)

If there is a real need for a warning for unused namelist or namelist items,
then a new PR should be open and this one closed. IMO it is just a waste of time: unused stuff having a strong tendency to produce false positive.
Comment 17 Dominique d'Humieres 2015-10-10 13:05:46 UTC
> If there is a real need for a warning for unused namelist or namelist items,
> then a new PR should be open and this one closed. IMO it is just a waste of time:
> unused stuff having a strong tendency to produce false positive.

No feedback for more than two years. Closing as WONTFIX. Please open new PR(s) for remaining issue(s).