aboutsummaryrefslogtreecommitdiff
path: root/gcc/c-family/c.opt
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2022-04-01 16:55:58 -0400
committerMarek Polacek <polacek@redhat.com>2022-05-18 17:43:56 -0400
commit7da9a089608b0ca09683332ce014fb6184842724 (patch)
treea66671c0a6edc1a4191f4ec0d97d6a5c55ff6191 /gcc/c-family/c.opt
parent1875214cd1ca3e8bd0121f703537eb98edd84027 (diff)
downloadgcc-7da9a089608b0ca09683332ce014fb6184842724.zip
gcc-7da9a089608b0ca09683332ce014fb6184842724.tar.gz
gcc-7da9a089608b0ca09683332ce014fb6184842724.tar.bz2
c: Implement new -Wenum-int-mismatch warning [PR105131]
In C, an enumerated type is compatible with char, a signed integer type, or an unsigned integer type (6.7.2.2/5). Therefore this code compiles: enum E { l = -1, z = 0, g = 1 }; int foo(void); enum E foo(void) { return z; } if the underlying type of 'enum E' is 'int' (if not, we emit an error). This is different for typedefs, where C11 permits typedefs to be redeclared to the same type, but not to compatible types. In C++, the code above is invalid. It seems desirable to emit a warning in the C case, because it is probably a mistake and definitely a portability error, given that the choice of the underlying type is implementation-defined. To that end, this patch implements a new -Wenum-int-mismatch warning. Conveniently, we already have comptypes_check_enum_int to detect such mismatches. This warning is enabled by either -Wall or -Wc++-compat. PR c/105131 gcc/c-family/ChangeLog: * c.opt (Wenum-int-mismatch): New. gcc/c/ChangeLog: * c-decl.cc (diagnose_mismatched_decls): Warn about enum/integer type mismatches. * c-tree.h (comptypes_check_enum_int): Declare. * c-typeck.cc (comptypes): No longer static. gcc/ChangeLog: * doc/invoke.texi: Document -Wenum-int-mismatch. gcc/testsuite/ChangeLog: * gcc.dg/Wenum-int-mismatch-1.c: New test. * gcc.dg/Wenum-int-mismatch-2.c: New test. * gcc.dg/Wenum-int-mismatch-3.c: New test. * gcc.dg/Wenum-int-mismatch-4.c: New test. * gcc.dg/Wenum-int-mismatch-5.c: New test.
Diffstat (limited to 'gcc/c-family/c.opt')
-rw-r--r--gcc/c-family/c.opt4
1 files changed, 4 insertions, 0 deletions
diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 035b1de..41a20bc6 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -638,6 +638,10 @@ Wenum-conversion
C ObjC C++ ObjC++ Var(warn_enum_conversion) Init(0) Warning LangEnabledBy(C ObjC,Wextra)
Warn about implicit conversion of enum types.
+Wenum-int-mismatch
+C ObjC Var(warn_enum_int_mismatch) Warning LangEnabledBy(C ObjC,Wall || Wc++-compat)
+Warn about enum/integer type mismatches.
+
Werror
C ObjC C++ ObjC++
; Documented in common.opt