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
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.
(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.
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.
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
(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.
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.
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
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
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.
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);
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
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.
Waiting
> 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.
(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.)
(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.
> 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).