]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Use unsigned as base type for some enums
authorTom Tromey <tom@tromey.com>
Fri, 27 Jul 2018 01:52:47 +0000 (19:52 -0600)
committerTom Tromey <tom@tromey.com>
Wed, 3 Oct 2018 21:19:06 +0000 (15:19 -0600)
-fsanitize=undefined complains about using operator~ on various enum
types that are used with DEF_ENUM_FLAGS_TYPE.  This patch fixes these
problems by explicitly setting the base type for these enums to
unsigned.  It also adds a static assert to enum_flags to ensure that
future enums used this way have an unsigned underlying type.

gdb/ChangeLog
2018-10-03  Tom Tromey  <tom@tromey.com>

* common/enum-flags.h (enum_flags::operator~): Add static assert.
* symfile-add-flags.h (enum symfile_add_flag): Use unsigned as
base type.
* objfile-flags.h (enum objfile_flag): Use unsigned as base type.
* gdbtypes.h (enum type_instance_flag_value): Use unsigned as base
type.
* c-lang.h (enum c_string_type_values): Use unsigned as base
type.
* btrace.h (enum btrace_thread_flag): Use unsigned as base type.

gdb/ChangeLog
gdb/btrace.h
gdb/c-lang.h
gdb/common/enum-flags.h
gdb/gdbtypes.h
gdb/objfile-flags.h
gdb/symfile-add-flags.h

index 07aecd5addccd0e08cadc5c953b3df441287092c..36c44932743cbea75401801e9e244528abc9b4c7 100644 (file)
@@ -1,3 +1,15 @@
+2018-10-03  Tom Tromey  <tom@tromey.com>
+
+       * common/enum-flags.h (enum_flags::operator~): Add static assert.
+       * symfile-add-flags.h (enum symfile_add_flag): Use unsigned as
+       base type.
+       * objfile-flags.h (enum objfile_flag): Use unsigned as base type.
+       * gdbtypes.h (enum type_instance_flag_value): Use unsigned as base
+       type.
+       * c-lang.h (enum c_string_type_values): Use unsigned as base
+       type.
+       * btrace.h (enum btrace_thread_flag): Use unsigned as base type.
+
 2018-10-03  Tom Tromey  <tom@tromey.com>
 
        * dwarf2-frame.h (dwarf2_frame_state_reg_info)
index bfb0b9f278752a2fcab8e9581f458778422d7502..0448bd6d498a0b222a14dbc12f4c2ba95522d46f 100644 (file)
@@ -228,7 +228,7 @@ struct btrace_call_history
 };
 
 /* Branch trace thread flags.  */
-enum btrace_thread_flag
+enum btrace_thread_flag : unsigned
 {
   /* The thread is to be stepped forwards.  */
   BTHR_STEP = (1 << 0),
index ae17abd20f90799030d87cf59bf93ddfdac5d603..f9eab04730cb5e1ad1c78c77b50c00f9ad47eb9d 100644 (file)
@@ -35,7 +35,7 @@ struct parser_state;
 /* The various kinds of C string and character.  Note that these
    values are chosen so that they may be or'd together in certain
    ways.  */
-enum c_string_type_values
+enum c_string_type_values : unsigned
   {
     /* An ordinary string: "value".  */
     C_STRING = 0,
index 82568a5a3d9ae0ec4bcf9c07f22b61565826a784..f7ca8d8679d926f40240b0a79246d975bbd82839 100644 (file)
@@ -164,6 +164,12 @@ public:
   }
   enum_flags operator~ () const
   {
+    // We only the underlying type to be unsigned when actually using
+    // operator~ -- if it were not unsigned, undefined behavior could
+    // result.  However, asserting this in the class itself would
+    // require too many unnecessary changes to otherwise ok enum
+    // types.
+    gdb_static_assert (std::is_unsigned<underlying_type>::value);
     return (enum_type) ~underlying_value ();
   }
 
index 32b58dcc6186703ef588e1b8100f3741dbdae4a6..a115857c0ad24db2125872b0b46fac75b39d1db3 100644 (file)
@@ -193,7 +193,7 @@ enum type_code
 /* * Some bits for the type's instance_flags word.  See the macros
    below for documentation on each bit.  */
 
-enum type_instance_flag_value
+enum type_instance_flag_value : unsigned
 {
   TYPE_INSTANCE_FLAG_CONST = (1 << 0),
   TYPE_INSTANCE_FLAG_VOLATILE = (1 << 1),
index aeaa8fbc04d092d85d7e5dc5b43b34c7e834ee1a..6f5760d021daf9f9bac0b3ea9c22963da5c398bc 100644 (file)
@@ -25,7 +25,7 @@
 /* Defines for the objfile flags field.  Defined in a separate file to
    break circular header dependencies.  */
 
-enum objfile_flag
+enum objfile_flag : unsigned
   {
     /* When an object file has its functions reordered (currently
        Irix-5.2 shared libraries exhibit this behaviour), we will need
index 3c07513e395e0e8795a25c40d7b408b477e382bf..35241fe4a041b237c14a10c33aa594e22ff95c8d 100644 (file)
@@ -26,7 +26,7 @@
    symbol_file_add, etc.  Defined in a separate file to break circular
    header dependencies.  */
 
-enum symfile_add_flag
+enum symfile_add_flag : unsigned
   {
     /* Be chatty about what you are doing.  */
     SYMFILE_VERBOSE = 1 << 1,