The bug has been introduced when changing the warning system to do more
in the front end. The problem is that for:
module m
contains
function f()
end
end
the sym->attr.referenced gets set - and no warning is printed. I now
ignore the sym->attr.referenced attribute as the RESULT == NULL_TREE
should only be reached if the variable has not been assigned a value.
Additionally, I now only set TREE_NO_WARNING() if a warning has been
printed; that way, some other useful middle-end warnings might be still
printed. I have to set the NO_WARNING also for sym != sym->result as it
otherwise prints a middle end warning into addition to the FE warning.
But the FE warning has been printed already before for the result variable.
Build and regtested on x86-64-linux.
OK for the trunk? How far should this be backported - all the way down
to 4.4?
Tobias
2011-11-30 Tobias Burnus <bur...@net-b.de>
PR fortran/50923
* trans-decl.c (generate_local_decl): Set TREE_NO_WARNING only
if the front end has printed a warning.
(gfc_generate_function_code): Fix unset-result warning.
2011-11-30 Tobias Burnus <bur...@net-b.de>
PR fortran/50923
* gfortran.dg/warn_function_without_result_2.f90: New.
diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
index 67bd3e2..b8789a4 100644
--- a/gcc/fortran/trans-decl.c
+++ b/gcc/fortran/trans-decl.c
@@ -4510,10 +4532,16 @@ generate_local_decl (gfc_symbol * sym)
"declared INTENT(OUT) but was not set and "
"does not have a default initializer",
sym->name, &sym->declared_at);
+ if (sym->backend_decl != NULL_TREE)
+ TREE_NO_WARNING(sym->backend_decl) = 1;
}
else if (gfc_option.warn_unused_dummy_argument)
- gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
+ {
+ gfc_warning ("Unused dummy argument '%s' at %L", sym->name,
&sym->declared_at);
+ if (sym->backend_decl != NULL_TREE)
+ TREE_NO_WARNING(sym->backend_decl) = 1;
+ }
}
/* Warn for unused variables, but not if they're inside a common
@@ -4521,11 +4549,19 @@ generate_local_decl (gfc_symbol * sym)
else if (warn_unused_variable
&& !(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);
+ {
+ 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);
+ {
+ 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
@@ -4561,11 +4597,6 @@ generate_local_decl (gfc_symbol * sym)
mark the symbol now, as well as in traverse_ns, to prevent
getting stuck in a circular dependency. */
sym->mark = 1;
-
- /* We do not want the middle-end to warn about unused parameters
- as this was already done above. */
- if (sym->attr.dummy && sym->backend_decl != NULL_TREE)
- TREE_NO_WARNING(sym->backend_decl) = 1;
}
else if (sym->attr.flavor == FL_PARAMETER)
{
@@ -5288,11 +5315,11 @@ gfc_generate_function_code (gfc_namespace * ns)
if (result == NULL_TREE)
{
/* TODO: move to the appropriate place in resolve.c. */
- if (warn_return_type && !sym->attr.referenced && sym == sym->result)
+ if (warn_return_type && 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;
+ if (warn_return_type)
+ TREE_NO_WARNING(sym->backend_decl) = 1;
}
else
gfc_add_expr_to_block (&body, gfc_generate_return ());
--- /dev/null 2011-11-29 07:50:43.475522632 +0100
+++ gcc/gcc/testsuite/gfortran.dg/warn_function_without_result_2.f90 2011-11-30 17:52:38.000000000 +0100
@@ -0,0 +1,19 @@
+! { dg-do compile }
+! { dg-options "-Wall" }
+!
+! PR fortran/50923
+!
+module m
+contains
+ integer pure function f() ! { dg-warning "Return value of function 'f' at .1. not set" }
+ end function f
+ integer pure function g() result(h) ! { dg-warning "Return value 'h' of function 'g' declared at .1. not set" }
+ end function g
+ integer pure function i()
+ i = 7
+ end function i
+ integer pure function j() result(k)
+ k = 8
+ end function j
+end module m
+! { dg-final { cleanup-modules "mod" } }