2017-07-11 Prathamesh Kulkarni * doc/invoke.texi: Document -Wenum-conversion. * c-family/c.opt (Wenum-conversion): New option. * c/c-typeck.c (convert_for_assignment): Handle Wenum-conversion. libgomp/ * icv.c (omp_set_schedule): Cast kind to enum gomp_schedule_type. (omp_get_schedule): Cast icv->run_sched_var to enum omp_sched_t before. libgfortran/ * io/transfer.c (current_mode): Cast FORM_UNSPECIFIED to file_mode. (formatted_transfer_scalar_read): Cast SIGN_S, SIGN_SS, SIGN_SP to unit_sign. (formatted_transfer_scalar_write): Likewise. testsuite/ * gcc.dg/Wenum-conversion.c: New test-case. * gcc.dg/Wenum-conversion-2.c: Likewise. * gcc.dg/Wenum-conversion-3.c: Likewise. diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index 05766c47856..6b14203773e 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -500,6 +500,10 @@ Wenum-compare C ObjC C++ ObjC++ Var(warn_enum_compare) Init(-1) Warning LangEnabledBy(C ObjC,Wall || Wc++-compat) Warn about comparison of different enum types. +Wenum-conversion +C ObjC Var(warn_enum_conversion) Init(0) Warning LangEnabledBy(C ObjC,Wall) +Warn about implicit conversion of enum types. + Werror C ObjC C++ ObjC++ ; Documented in common.opt diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c index 4d067e96dd3..479380f5748 100644 --- a/gcc/c/c-typeck.c +++ b/gcc/c/c-typeck.c @@ -6314,6 +6314,32 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type, } } + if (warn_enum_conversion) + { + tree checktype = origtype != NULL_TREE ? origtype : rhstype; + if (checktype != error_mark_node + && TREE_CODE (checktype) == ENUMERAL_TYPE + && TREE_CODE (type) == ENUMERAL_TYPE + && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type)) + { + tree v1 = TYPE_VALUES (type); + tree v2 = TYPE_VALUES (checktype); + + /* Do not warn if the enums have same range of values. */ + for (; v1 && v2; v1 = TREE_CHAIN (v1), v2 = TREE_CHAIN (v2)) + if (simple_cst_equal (TREE_VALUE (v1), TREE_VALUE (v2)) != 1) + break; + + if (v1 || v2) + { + gcc_rich_location loc (location); + warning_at_rich_loc (&loc, OPT_Wenum_conversion, + "implicit conversion from %qT to %qT", + checktype, type); + } + } + } + if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype)) return rhs; diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index f2020f847ae..5fe35515852 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -276,7 +276,8 @@ Objective-C and Objective-C++ Dialects}. -Wno-discarded-qualifiers -Wno-discarded-array-qualifiers @gol -Wno-div-by-zero -Wdouble-promotion @gol -Wduplicated-branches -Wduplicated-cond @gol --Wempty-body -Wenum-compare -Wno-endif-labels -Wexpansion-to-defined @gol +-Wempty-body -Wenum-compare -Wenum-conversion @gol +-Wno-endif-labels -Wexpansion-to-defined @gol -Werror -Werror=* -Wextra-semi -Wfatal-errors @gol -Wfloat-equal -Wformat -Wformat=2 @gol -Wno-format-contains-nul -Wno-format-extra-args @gol @@ -3828,6 +3829,7 @@ Options} and @ref{Objective-C and Objective-C++ Dialect Options}. -Wcomment @gol -Wduplicate-decl-specifier @r{(C and Objective-C only)} @gol -Wenum-compare @r{(in C/ObjC; this is on by default in C++)} @gol +-Wenum-conversion @r{(in C/ObjC;} @gol -Wformat @gol -Wint-in-bool-context @gol -Wimplicit @r{(C and Objective-C only)} @gol @@ -6074,6 +6076,12 @@ In C++ enumerated type mismatches in conditional expressions are also diagnosed and the warning is enabled by default. In C this warning is enabled by @option{-Wall}. +@item -Wenum-conversion @r{(C, Objective-C only)} +@opindex Wenum-conversion +@opindex Wno-enum-conversion +Warn when an enum value of a type is implicitly converted to an enum valeu of +another type. This warning is enabled by @option{-Wall}. + @item -Wextra-semi @r{(C++, Objective-C++ only)} @opindex Wextra-semi @opindex Wno-extra-semi diff --git a/gcc/testsuite/gcc.dg/Wenum-conversion-2.c b/gcc/testsuite/gcc.dg/Wenum-conversion-2.c new file mode 100644 index 00000000000..a084ce4fd3c --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wenum-conversion-2.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-Wenum-conversion" } */ + +enum X { x1, x2 }; +enum Y { y1, y2 }; + +enum X obj = y1; /* { dg-bogus "implicit conversion from .enum Y. to .enum X." } */ +enum Y obj2 = y1; + +enum X obj3; +void foo() +{ + obj3 = y2; /* { dg-bogus "implicit conversion from .enum Y. to .enum X." } */ +} + +void bar(enum X); +void f(void) +{ + bar (y1); /* { dg-bogus "implicit conversion from .enum Y. to .enum X." } */ +} diff --git a/gcc/testsuite/gcc.dg/Wenum-conversion-3.c b/gcc/testsuite/gcc.dg/Wenum-conversion-3.c new file mode 100644 index 00000000000..9a01bdf2b60 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wenum-conversion-3.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-Wenum-conversion" } */ + +enum X { x1 = 5, x2 }; +enum Y { y1 = 10, y2 }; + +enum X obj = y1; /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +enum Y obj2 = y1; + +enum X obj3; +void foo() +{ + obj3 = y2; /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +} + +void bar(enum X); +void f(void) +{ + bar (y1); /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +} diff --git a/gcc/testsuite/gcc.dg/Wenum-conversion.c b/gcc/testsuite/gcc.dg/Wenum-conversion.c new file mode 100644 index 00000000000..177770b4115 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wenum-conversion.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-Wenum-conversion" } */ + +enum X { x1, x2 }; +enum Y { y1, y2, y3 }; + +enum X obj = y1; /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +enum Y obj2 = y1; + +enum X obj3; +void foo() +{ + obj3 = y2; /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +} + +void bar(enum X); +void f(void) +{ + bar (y1); /* { dg-warning "implicit conversion from .enum Y. to .enum X." } */ +} diff --git a/libgfortran/io/transfer.c b/libgfortran/io/transfer.c index 298b29e8d3e..ed678895059 100644 --- a/libgfortran/io/transfer.c +++ b/libgfortran/io/transfer.c @@ -196,7 +196,7 @@ current_mode (st_parameter_dt *dtp) { file_mode m; - m = FORM_UNSPECIFIED; + m = (file_mode) FORM_UNSPECIFIED; if (dtp->u.p.current_unit->flags.access == ACCESS_DIRECT) { @@ -1671,17 +1671,17 @@ formatted_transfer_scalar_read (st_parameter_dt *dtp, bt type, void *p, int kind case FMT_S: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_S; + dtp->u.p.sign_status = (unit_sign) SIGN_S; break; case FMT_SS: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_SS; + dtp->u.p.sign_status = (unit_sign) SIGN_SS; break; case FMT_SP: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_SP; + dtp->u.p.sign_status = (unit_sign) SIGN_SP; break; case FMT_BN: @@ -2130,17 +2130,17 @@ formatted_transfer_scalar_write (st_parameter_dt *dtp, bt type, void *p, int kin case FMT_S: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_S; + dtp->u.p.sign_status = (unit_sign) SIGN_S; break; case FMT_SS: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_SS; + dtp->u.p.sign_status = (unit_sign) SIGN_SS; break; case FMT_SP: consume_data_flag = 0; - dtp->u.p.sign_status = SIGN_SP; + dtp->u.p.sign_status = (unit_sign) SIGN_SP; break; case FMT_BN: diff --git a/libgomp/icv.c b/libgomp/icv.c index 233d6dbe10e..71e1f677fd7 100644 --- a/libgomp/icv.c +++ b/libgomp/icv.c @@ -87,14 +87,14 @@ omp_set_schedule (omp_sched_t kind, int chunk_size) default: return; } - icv->run_sched_var = kind; + icv->run_sched_var = (enum gomp_schedule_type) kind; } void omp_get_schedule (omp_sched_t *kind, int *chunk_size) { struct gomp_task_icv *icv = gomp_icv (false); - *kind = icv->run_sched_var; + *kind = (enum omp_sched_t) icv->run_sched_var; *chunk_size = icv->run_sched_chunk_size; }