aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>2019-09-04 16:25:21 +0000
committerPrathamesh Kulkarni <prathamesh3492@gcc.gnu.org>2019-09-04 16:25:21 +0000
commit68e2c1996ec6bde27363ce0db15233ac8cae1c4d (patch)
treef36d29fb8fb3e378f3c06105410c3d6b2f368994
parentbc7f7ff954679ac46771f56cbb947a4c19557d98 (diff)
downloadgcc-68e2c1996ec6bde27363ce0db15233ac8cae1c4d.zip
gcc-68e2c1996ec6bde27363ce0db15233ac8cae1c4d.tar.gz
gcc-68e2c1996ec6bde27363ce0db15233ac8cae1c4d.tar.bz2
Add warning Wenum-conversion for C and ObjC.
The patch enables warning with Wextra due to PR91593 and warnings with allmodconfig kernel build. Once these issues are resolved, we could consider promoting it to Wall. 2019-09-04 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org> PR c/78736 * doc/invoke.texi: Document -Wenum-conversion. c-family * c.opt (Wenum-conversion): New option. c/ * c-typeck.c (convert_for_assignment): Handle Wenum-conversion. testsuite/ * gcc.dg/Wenum-conversion.c: New test-case. From-SVN: r275376
-rw-r--r--gcc/ChangeLog5
-rw-r--r--gcc/c-family/ChangeLog5
-rw-r--r--gcc/c-family/c.opt4
-rw-r--r--gcc/c/ChangeLog5
-rw-r--r--gcc/c/c-typeck.c15
-rw-r--r--gcc/doc/invoke.texi10
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/gcc.dg/Wenum-conversion.c20
8 files changed, 68 insertions, 1 deletions
diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index fa47927..fc18766 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2019-09-04 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
+
+ PR c/78736
+ * doc/invoke.texi: Document -Wenum-conversion.
+
2019-09-04 Richard Biener <rguenther@suse.de>
PR rtl-optimization/36262
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 9ca5a1e..ecdaaeb 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,8 @@
+2019-09-04 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
+
+ PR c/78736
+ * c.opt (Wenum-conversion): New option.
+
2019-09-03 Jozef Lawrynowicz <jozef.l@mittosystems.com>
* c-attribs.c (handle_section_attribute): Call the
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index f8a1a1d..38a8e7d 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -541,6 +541,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,Wextra)
+Warn about implicit conversion of enum types.
+
Werror
C ObjC C++ ObjC++
; Documented in common.opt
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index b7b45f5..285ea18 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,8 @@
+2019-09-04 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
+
+ PR c/78736
+ * c-typeck.c (convert_for_assignment): Handle Wenum-conversion.
+
2019-08-23 Iain Sandoe <iain@sandoe.co.uk>
PR pch/61250
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 2bbf0e2..d4e12eb 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -6726,6 +6726,21 @@ 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))
+ {
+ gcc_rich_location loc (location);
+ warning_at (&loc, OPT_Wenum_conversion,
+ "implicit conversion from %qT to %qT",
+ checktype, type);
+ }
+ }
+
if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
{
warn_for_address_or_pointer_of_packed_member (type, orig_rhs);
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 2e353be..34d0746 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -307,7 +307,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
@@ -4434,6 +4435,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
@@ -7012,6 +7014,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 a value of enumerated type is implicitly converted to a
+different enumerated type. This warning is enabled by @option{-Wextra}.
+
@item -Wextra-semi @r{(C++, Objective-C++ only)}
@opindex Wextra-semi
@opindex Wno-extra-semi
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 7c9a9c9..5b6332a 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2019-09-04 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
+
+ PR c/78736
+ * gcc.dg/Wenum-conversion.c: New test-case.
+
2019-09-03 Jozef Lawrynowicz <jozef.l@mittosystems.com>
* gcc.target/msp430/data-attributes-2.c: New test.
diff --git a/gcc/testsuite/gcc.dg/Wenum-conversion.c b/gcc/testsuite/gcc.dg/Wenum-conversion.c
new file mode 100644
index 0000000..8603339
--- /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 };
+
+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." } */
+}