gdb: allow duplicate enumerators in flag enums
[deliverable/binutils-gdb.git] / gdb / valprint.c
index ced0dbcd0a60c3bcef4e97c3b10e51c60a3e0633..888c9cdb577820b124e58298ddc2978bdd5d0924 100644 (file)
@@ -1,6 +1,6 @@
 /* Print values for GDB, the GNU debugger.
 
-   Copyright (C) 1986-2019 Free Software Foundation, Inc.
+   Copyright (C) 1986-2020 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -39,6 +39,7 @@
 #include "cli/cli-option.h"
 #include "gdbarch.h"
 #include "cli/cli-style.h"
+#include "count-one-bits.h"
 
 /* Maximum number of wchars returned from wchar_iterate.  */
 #define MAX_WCHARS 4
@@ -630,15 +631,21 @@ generic_val_print_enum_1 (struct type *type, LONGEST val,
     {
       int first = 1;
 
-      /* We have a "flag" enum, so we try to decompose it into
-        pieces as appropriate.  A flag enum has disjoint
-        constants by definition.  */
+      /* We have a "flag" enum, so we try to decompose it into pieces as
+        appropriate.  The enum may have multiple enumerators representing
+        the same bit, in which case we choose to only print the first one
+        we find.  */
       fputs_filtered ("(", stream);
       for (i = 0; i < len; ++i)
        {
          QUIT;
 
-         if ((val & TYPE_FIELD_ENUMVAL (type, i)) != 0)
+         ULONGEST enumval = TYPE_FIELD_ENUMVAL (type, i);
+         int nbits = count_one_bits_ll (enumval);
+
+         gdb_assert (nbits == 0 || nbits == 1);
+
+         if ((val & enumval) != 0)
            {
              if (!first)
                fputs_filtered (" | ", stream);
@@ -2897,9 +2904,9 @@ val_print_string (struct type *elttype, const char *encoding,
     {
       std::string str = memory_error_message (TARGET_XFER_E_IO, gdbarch, addr);
 
-      fprintf_filtered (stream, "<error: ");
-      fputs_filtered (str.c_str (), stream);
-      fprintf_filtered (stream, ">");
+      fprintf_filtered (stream, _("<error: %ps>"),
+                       styled_string (metadata_style.style (),
+                                      str.c_str ()));
     }
 
   return (bytes_read / width);
@@ -3198,6 +3205,16 @@ Use \"unlimited\" to print the complete structure.")
     NULL, /* help_doc */
   },
 
+  boolean_option_def {
+    "raw-values",
+    [] (value_print_options *opt) { return &opt->raw; },
+    NULL, /* show_cmd_cb */
+    N_("Set whether to print values in raw form."),
+    N_("Show whether to print values in raw form."),
+    N_("If set, values are printed in raw form, bypassing any\n\
+pretty-printers for that value.")
+  },
+
   uinteger_option_def {
     "repeats",
     [] (value_print_options *opt) { return &opt->repeat_count_threshold; },
@@ -3252,8 +3269,9 @@ make_value_print_options_def_group (value_print_options *opts)
   return {{value_print_option_defs}, opts};
 }
 
+void _initialize_valprint ();
 void
-_initialize_valprint (void)
+_initialize_valprint ()
 {
   cmd_list_element *cmd;
 
This page took 0.026557 seconds and 4 git commands to generate.