GNU Fortran warns by default for code like the following:
interface
subroutine sub (n) bind (C)
integer :: n
end subroutine sub
end interface
Namely, it prints:
Warning: Variable 'n' at (1) is a parameter to the BIND(C) procedure
'sub' but may not be C interoperable
That's on one hand correct: There is no defined relation between Fortran
default kinds (or any kind number) and the C types. Thus, the proper way
is to use the parameters defined in ISO_C_BINDING, such as c_int.
On the other hand, integer and int is the same on many (but not on
all systems) and, thus, many users simply use the default type.
Compiling interfaces with hundreds of such definitions clutters the
screen with those warnings and makes it difficult to spot other warnings.
Thus, this patch adds a warning flag for this purpose - and it also
excludes those warnings from the default setting. That's a bit in line
with Fortran 2008 and TS 29113, which remove more and more constraints
and force the users to ensure themselves that the variables are
interoperable. However, keeping it as default warning is also fine with me.
Build and regtested on x86-64-linux.
OK?
Tobias
2012-03-03 Tobias Burnus bur...@net-b.de
* lang.opt (Wc-binding-type): New flag.
* options.c (gfc_init_options, gfc_handle_option): Handle it.
* invoke.texi (Wc-binding-type): Document it.
* gfortran.h (gfc_option_t): Add warn_c_binding_type.
* decl.c (verify_bind_c_sym): Handle -Wc-binding-type.
* symbol.c (gfc_set_default_type, verify_bind_c_derived_type):
Ditto.
2012-03-03 Tobias Burnus bur...@net-b.de
* gfortran.dg/bind_c_dts_4.f03: Add dg-options -Wc-binding-type.
* gfortran.dg/bind_c_implicit_vars.f03: Ditto.
* gfortran.dg/bind_c_usage_8.f03: Ditto.
* gfortran.dg/c_kind_tests_2.f03: Ditto.
* gfortran.dg/class_30.f90: Remove dg-warning line.
* gfortran.dg/bind_c_usage_25.f90: New.
diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index bdb8c39..75b8a89 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -3930,7 +3930,7 @@ verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
{
tmp_sym = tmp_sym-result;
/* Make sure it wasn't an implicitly typed result. */
- if (tmp_sym-attr.implicit_type)
+ if (tmp_sym-attr.implicit_type gfc_option.warn_c_binding_type)
{
gfc_warning (Implicitly declared BIND(C) function '%s' at
%L may not be C interoperable, tmp_sym-name,
@@ -3951,7 +3951,7 @@ verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
if (gfc_verify_c_interop ((tmp_sym-ts)) != SUCCESS)
{
/* See if we're dealing with a sym in a common block or not. */
- if (is_in_common == 1)
+ if (is_in_common == 1 gfc_option.warn_c_binding_type)
{
gfc_warning (Variable '%s' in common block '%s' at %L
may not be a C interoperable
@@ -3965,7 +3965,7 @@ verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
gfc_error (Type declaration '%s' at %L is not C
interoperable but it is BIND(C),
tmp_sym-name, (tmp_sym-declared_at));
- else
+ else if (gfc_option.warn_c_binding_type)
gfc_warning (Variable '%s' at %L
may not be a C interoperable
kind but it is bind(c),
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index a5edd13..1c242b4 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -2200,6 +2201,7 @@ typedef struct
int warn_aliasing;
int warn_ampersand;
int gfc_warn_conversion;
+ int warn_c_binding_type;
int warn_conversion_extra;
int warn_function_elimination;
int warn_implicit_interface;
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 1f6de84..38ebfe9 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -142,7 +142,7 @@ by type. Explanations are in the following sections.
@xref{Error and Warning Options,,Options to request or suppress errors
and warnings}.
@gccoptlist{-Waliasing -Wall -Wampersand -Warray-bounds
--Wcharacter-truncation @gol
+-Wc-binding-type -Wcharacter-truncation @gol
-Wconversion -Wfunction-elimination -Wimplicit-interface @gol
-Wimplicit-procedure -Wintrinsic-shadow -Wintrinsics-std @gol
-Wline-truncation -Wno-align-commons -Wno-tabs -Wreal-q-constant @gol
@@ -773,6 +773,14 @@ Warn about array temporaries generated by the compiler. The information
generated by this warning is sometimes useful in optimization, in order to
avoid such temporaries.
+@item -Wc-binding-type
+@opindex @code{Wc-binding-type}
+@cindex warning, C binding type
+Warn if the a variable might not be C interoperable. In particular, warn if
+the variable has been declared using an intrinsic type with default kind
+instead of using a kind parameter defined for C interoperability in the
+intrinsic @code{ISO_C_Binding} module.
+
@item