This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[patch, fortran] PR31463 - fix inconsistent warnings if function return value is not set
- From: Daniel Franke <franke dot daniel at gmail dot com>
- To: fortran at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Tue, 29 Jan 2008 02:24:02 +0100
- Subject: [patch, fortran] PR31463 - fix inconsistent warnings if function return value is not set
Depending on return types and whether a RESULT-statement was used to define a
function, gfortran issued various kinds of warnings (including none at all)
if the return value was not set. This patch aims to unify these messages and
to remove any middle-end style warnings that where given.
For now, all warnings are default warnings. In C, "warning: control reaches
end of non-void function" is bound to -Wreturn-type which is enabled
by -Wall. Although the docs state that "For C++, a function without return
type always produces a diagnostic message, even when -Wno-return-type is
specified", I could not verify this. Should we hide these warnings behind an
option? If yes, which? Suggestions are welcome!
gcc/fortran:
2008-01-29 Daniel Franke <franke.daniel@gmail.com>
PR fortran/31463
* trans-decl.c (gfc_trans_deferred_vars): Emit warnings for funtions
where the result value is not set
(gfc_generate_function_code): Likewise.
(generate_local_decl): Emit warnings for funtions whose RESULT
variable is not set.
gcc/testsuite:
2008-01-29 Daniel Franke <franke.daniel@gmail.com>
PR fortran/31463
* gfortran.dg/arrayio_11.f90: Fixed test.
* gfortran.dg/arrayio_12.f90: Likewise.
* gfortran.dg/module_read_1.f90: Added warning-directives.
* gfortran.dg/pr32242.f90: Likewise.
* gfortran.dg/result_in_spec_3.f90: Likewise.
* gfortran.dg/use_12.f90: Likewise.
* gfortran.dg/warn_function_without_result.f90 : New test.
Bootstrapped and regression tested on i686-pc-linux-gnu.
Ok for trunk when re-entering stage1? (No backport, this is not a regression.)
Regards
Daniel
Index: fortran/trans-decl.c
===================================================================
--- fortran/trans-decl.c (revision 131923)
+++ fortran/trans-decl.c (working copy)
@@ -2668,7 +2668,8 @@ gfc_trans_deferred_vars (gfc_symbol * pr
break;
}
if (el == NULL)
- warning (0, "Function does not return a value");
+ gfc_warning ("Return value of function '%s' at %L not set",
+ proc_sym->name, &proc_sym->declared_at);
}
else if (proc_sym->as)
{
@@ -3012,7 +3013,7 @@ generate_local_decl (gfc_symbol * sym)
/* Warn for unused variables, but not if they're inside a common
block or are use-associated. */
else if (warn_unused_variable
- && !(sym->attr.in_common || sym->attr.use_assoc))
+ && !(sym->attr.in_common || sym->attr.use_assoc || sym->mark))
gfc_warning ("Unused variable '%s' declared at %L", sym->name,
&sym->declared_at);
/* For variable length CHARACTER parameters, the PARM_DECL already
@@ -3042,6 +3043,23 @@ generate_local_decl (gfc_symbol * sym)
gfc_warning ("Unused parameter '%s' declared at %L", sym->name,
&sym->declared_at);
}
+ else if (sym->attr.flavor == FL_PROCEDURE)
+ {
+ if (sym->attr.function
+ && sym->result
+ && sym != sym->result
+ && !sym->result->attr.referenced
+ && !sym->attr.use_assoc
+ && sym->attr.if_source != IFSRC_IFBODY)
+ {
+ gfc_warning ("Return value '%s' of function '%s' declared at "
+ "%L not set", sym->result->name, sym->name,
+ &sym->result->declared_at);
+
+ /* Prevents "Unused variable" warning for RESULT variables. */
+ sym->mark = sym->result->mark = 1;
+ }
+ }
if (sym->attr.dummy == 1)
{
@@ -3335,10 +3353,16 @@ gfc_generate_function_code (gfc_namespac
gfc_add_expr_to_block (&block, tmp2);
}
- gfc_add_expr_to_block (&block, tmp);
+ gfc_add_expr_to_block (&block, tmp);
- if (result == NULL_TREE)
- warning (0, "Function return value not set");
+ if (result == NULL_TREE)
+ {
+ if (!sym->attr.referenced && sym == sym->result)
+ gfc_warning ("Return value of function '%s' at %L not set",
+ sym->name, &sym->declared_at);
+
+ TREE_NO_WARNING(sym->backend_decl) = 1;
+ }
else
{
/* Set the return value to the dummy result variable. The
Index: testsuite/gfortran.dg/arrayio_11.f90
===================================================================
--- testsuite/gfortran.dg/arrayio_11.f90 (revision 131923)
+++ testsuite/gfortran.dg/arrayio_11.f90 (working copy)
@@ -21,7 +21,7 @@ program gfcbug51
FILE%date = (/'200612231200', '200712231200', &
'200812231200'/)
- time = date_to_year (FILE)
+ call date_to_year (FILE)
if (any (time%year .ne. (/2006, 2007, 2008/))) call abort ()
call month_to_date ((/8, 9, 10/), FILE)
@@ -30,11 +30,10 @@ program gfcbug51
contains
- function date_to_year (d) result (y)
+ subroutine date_to_year (d)
type(date_t) :: d(3)
- type(year_t) :: y(size (d, 1))
- read (d%date(1:4),'(i4)') time% year
- end function date_to_year
+ read (d%date(1:4),'(i4)') time%year
+ end subroutine
subroutine month_to_date (m, d)
type(date_t) :: d(3)
Index: testsuite/gfortran.dg/arrayio_12.f90
===================================================================
--- testsuite/gfortran.dg/arrayio_12.f90 (revision 131923)
+++ testsuite/gfortran.dg/arrayio_12.f90 (working copy)
@@ -18,7 +18,7 @@ program gfcbug51
cdate = (/'200612231200', '200712231200', &
'200812231200'/)
- time = date_to_year (cdate)
+ call date_to_year (cdate)
if (any (time%year .ne. (/2006, 2007, 2008/))) call abort ()
call month_to_date ((/8, 9, 10/), cdate)
@@ -27,11 +27,10 @@ program gfcbug51
contains
- function date_to_year (d) result (y)
+ subroutine date_to_year (d)
character(len=12) :: d(3)
- type(year_t) :: y(size (d, 1))
- read (cdate(:)(1:4),'(i4)') time% year
- end function date_to_year
+ read (cdate(:)(1:4),'(i4)') time%year
+ end subroutine
subroutine month_to_date (m, d)
character(len=12) :: d(3)
Index: testsuite/gfortran.dg/module_read_1.f90
===================================================================
--- testsuite/gfortran.dg/module_read_1.f90 (revision 131923)
+++ testsuite/gfortran.dg/module_read_1.f90 (working copy)
@@ -9,11 +9,11 @@
module foo
contains
- function pop(n) result(item)
+ function pop(n) result(item) ! { dg-warning "not set" }
integer :: n
character(len=merge(1, 0, n > 0)) :: item
end function pop
- function push(n) result(item)
+ function push(n) result(item) ! { dg-warning "not set" }
integer :: n
character(len=merge(1, 0, n /= 0)) :: item
end function push
Index: testsuite/gfortran.dg/pr32242.f90
===================================================================
--- testsuite/gfortran.dg/pr32242.f90 (revision 131923)
+++ testsuite/gfortran.dg/pr32242.f90 (working copy)
@@ -16,13 +16,13 @@ MODULE kahan_sum
TYPE ( pw_grid_type ), POINTER :: pw_grid
END TYPE pw_type
CONTAINS
- FUNCTION kahan_sum_d1(array,mask) RESULT(ks)
+ FUNCTION kahan_sum_d1(array,mask) RESULT(ks) ! { dg-warning "not set" }
REAL(KIND=dp), DIMENSION(:), INTENT(IN) :: array
LOGICAL, DIMENSION(:), INTENT(IN), &
OPTIONAL :: mask
REAL(KIND=dp) :: ks
END FUNCTION kahan_sum_d1
- FUNCTION kahan_sum_z1(array,mask) RESULT(ks)
+ FUNCTION kahan_sum_z1(array,mask) RESULT(ks) ! { dg-warning "not set" }
COMPLEX(KIND=dp), DIMENSION(:), &
INTENT(IN) :: array
LOGICAL, DIMENSION(:), INTENT(IN), &
@@ -34,6 +34,6 @@ FUNCTION pw_integral_a2b ( pw1, pw2 ) RE
TYPE(pw_type), INTENT(IN) :: pw1, pw2
REAL(KIND=dp) :: integral_value
integral_value = accurate_sum ( REAL ( CONJG ( pw1 % cc ( : ) ) &
- * pw2 % cc ( : ) ,KIND=dp) * pw1 % pw_grid % gsq ( : ) ) ! { dg-warning "Function return value not set" }
+ * pw2 % cc ( : ) ,KIND=dp) * pw1 % pw_grid % gsq ( : ) )
END FUNCTION pw_integral_a2b
END MODULE
Index: testsuite/gfortran.dg/result_in_spec_3.f90
===================================================================
--- testsuite/gfortran.dg/result_in_spec_3.f90 (revision 131923)
+++ testsuite/gfortran.dg/result_in_spec_3.f90 (working copy)
@@ -10,6 +10,6 @@ character(*) FUNCTION test() RESULT(ctab
ctab = "Hello"
END function test
-FUNCTION test2() RESULT(res)
+FUNCTION test2() RESULT(res) ! { dg-warning "not set" }
character(*) :: res
END function test2
Index: testsuite/gfortran.dg/use_12.f90
===================================================================
--- testsuite/gfortran.dg/use_12.f90 (revision 131923)
+++ testsuite/gfortran.dg/use_12.f90 (working copy)
@@ -10,7 +10,7 @@ end module m1
module m2
contains
- function get_nfirst( ) result(fnres)
+ function get_nfirst( ) result(fnres) ! { dg-warning "not set" }
use m1, only: numclusters
real :: fnres(numclusters) ! change to REAL and it works!!
end function get_nfirst
Index: testsuite/gfortran.dg/warn_function_without_result.f90
===================================================================
--- testsuite/gfortran.dg/warn_function_without_result.f90 (revision 0)
+++ testsuite/gfortran.dg/warn_function_without_result.f90 (revision 0)
@@ -0,0 +1,43 @@
+! { dg-do compile }
+! PR fortran/31463 - inconsistent warnings if function return value is not set
+
+FUNCTION f1() ! { dg-warning "not set" }
+REAL :: f1
+END FUNCTION
+
+FUNCTION f2() ! { dg-warning "not set" }
+REAL, DIMENSION(1) :: f2
+END FUNCTION
+
+FUNCTION f3() ! { dg-warning "not set" }
+REAL, POINTER :: f3
+END FUNCTION
+
+FUNCTION f4() ! { dg-warning "not set" }
+REAL, DIMENSION(:), POINTER :: f4
+END FUNCTION
+
+FUNCTION f5() ! { dg-warning "not set" }
+REAL, DIMENSION(:), ALLOCATABLE :: f5
+END FUNCTION
+
+
+FUNCTION g1() RESULT(h) ! { dg-warning "not set" }
+REAL :: h
+END FUNCTION
+
+FUNCTION g2() RESULT(h) ! { dg-warning "not set" }
+REAL, DIMENSION(1) :: h
+END FUNCTION
+
+FUNCTION g3() RESULT(h) ! { dg-warning "not set" }
+REAL, POINTER :: h
+END FUNCTION
+
+FUNCTION g4() RESULT(h) ! { dg-warning "not set" }
+REAL, DIMENSION(:), POINTER :: h
+END FUNCTION
+
+FUNCTION g5() RESULT(h) ! { dg-warning "not set" }
+REAL, DIMENSION(:), ALLOCATABLE :: h
+END FUNCTION