Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: Pedro Alves <palves@redhat.com>, <gdb-patches@sourceware.org>
Subject: Re: [PATCH] Type-safe wrapper for enum flags
Date: Sat, 31 Oct 2015 20:11:00 -0000	[thread overview]
Message-ID: <56338FA1.2060300@ericsson.com> (raw)
In-Reply-To: <56338435.6080703@redhat.com>

On 15-10-30 10:52 AM, Pedro Alves wrote:
> Looks like c-lang.h should include common/enum_flags.h.

Good point.  Include What You Use.

> Otherwise LGTM.
> 
> I'd like to collect at least one Ack on the enum flags patch
> before pushing it.

Here is the patch with the include.  When it's time to push your patch,
can you make this one follow?


From ba5b5a985d8ae440f8b34b78e176796ed4681f8e Mon Sep 17 00:00:00 2001
From: Simon Marchi <simon.marchi@polymtl.ca>
Date: Thu, 29 Oct 2015 21:54:35 -0400
Subject: [PATCH] Convert c_string_type to an enum flags type

c_string_type contains values meant to be OR'ed together (even though
some bits are mutually exclusive), so it makes sense to make it an
enum flags type.

gdb/ChangeLog:

	* c-exp.y (exp): Adjust, change enum c_string_type to
	c_string_type.
	(parse_string_or_char): Likewise.
	* c-lang.c (charset_for_string_type): Likewise.
	(classify_type): Likewise.
	(c_printchar): Likewise.
	(c_printstr): Likewise.
	(evaluate_subexp_c): Likewise.  And change cast to enum
	c_string_type_values.
	* c-lang.h (enum c_string_type): Rename to...
	(enum c_string_type_values): ...this.
	(c_string_type): Define new enum flags type.
---
 gdb/c-exp.y  |  6 +++---
 gdb/c-lang.c | 18 +++++++++---------
 gdb/c-lang.h |  4 +++-
 3 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index f2de0ae..9127756 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -864,7 +864,7 @@ string_exp:
 exp	:	string_exp
 			{
 			  int i;
-			  enum c_string_type type = C_STRING;
+			  c_string_type type = C_STRING;

 			  for (i = 0; i < $1.len; ++i)
 			    {
@@ -878,7 +878,7 @@ exp	:	string_exp
 				  if (type != C_STRING
 				      && type != $1.tokens[i].type)
 				    error (_("Undefined string concatenation."));
-				  type = (enum c_string_type) $1.tokens[i].type;
+				  type = (enum c_string_type_values) $1.tokens[i].type;
 				  break;
 				default:
 				  /* internal error */
@@ -2164,7 +2164,7 @@ parse_string_or_char (const char *tokptr, const char **outptr,
 		      struct typed_stoken *value, int *host_chars)
 {
   int quote;
-  enum c_string_type type;
+  c_string_type type;
   int is_objc = 0;

   /* Build the gdb internal form of the input string in tempbuf.  Note
diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index 384783c..c1a4c45 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -34,6 +34,7 @@
 #include "gdb_obstack.h"
 #include <ctype.h>
 #include "gdbcore.h"
+#include "common/enum_flags.h"

 extern void _initialize_c_language (void);

@@ -41,8 +42,7 @@ extern void _initialize_c_language (void);
    character set name.  */

 static const char *
-charset_for_string_type (enum c_string_type str_type,
-			 struct gdbarch *gdbarch)
+charset_for_string_type (c_string_type str_type, struct gdbarch *gdbarch)
 {
   switch (str_type & ~C_CHAR)
     {
@@ -72,11 +72,11 @@ charset_for_string_type (enum c_string_type str_type,
    characters of this type in target BYTE_ORDER to the host character
    set.  */

-static enum c_string_type
+static c_string_type
 classify_type (struct type *elttype, struct gdbarch *gdbarch,
 	       const char **encoding)
 {
-  enum c_string_type result;
+  c_string_type result;

   /* We loop because ELTTYPE may be a typedef, and we want to
      successively peel each typedef until we reach a type we
@@ -155,7 +155,7 @@ c_emit_char (int c, struct type *type,
 void
 c_printchar (int c, struct type *type, struct ui_file *stream)
 {
-  enum c_string_type str_type;
+  c_string_type str_type;

   str_type = classify_type (type, get_type_arch (type), NULL);
   switch (str_type)
@@ -191,7 +191,7 @@ c_printstr (struct ui_file *stream, struct type *type,
 	    const char *user_encoding, int force_ellipses,
 	    const struct value_print_options *options)
 {
-  enum c_string_type str_type;
+  c_string_type str_type;
   const char *type_encoding;
   const char *encoding;

@@ -577,7 +577,7 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,
 	struct obstack output;
 	struct cleanup *cleanup;
 	struct value *result;
-	enum c_string_type dest_type;
+	c_string_type dest_type;
 	const char *dest_charset;
 	int satisfy_expected = 0;

@@ -589,8 +589,8 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,

 	++*pos;
 	limit = *pos + BYTES_TO_EXP_ELEM (oplen + 1);
-	dest_type
-	  = (enum c_string_type) longest_to_int (exp->elts[*pos].longconst);
+	dest_type = ((enum c_string_type_values)
+		     longest_to_int (exp->elts[*pos].longconst));
 	switch (dest_type & ~C_CHAR)
 	  {
 	  case C_STRING:
diff --git a/gdb/c-lang.h b/gdb/c-lang.h
index 5e30481..b1823ce 100644
--- a/gdb/c-lang.h
+++ b/gdb/c-lang.h
@@ -34,7 +34,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
+enum c_string_type_values
   {
     /* An ordinary string: "value".  */
     C_STRING = 0,
@@ -56,6 +56,8 @@ enum c_string_type
     C_CHAR_32 = 7
   };

+DEF_ENUM_FLAGS_TYPE (enum c_string_type_values, c_string_type);
+
 /* Defined in c-exp.y.  */

 extern int c_parse (struct parser_state *);
-- 
2.5.1



  reply	other threads:[~2015-10-30 15:41 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-30  8:12 Pedro Alves
2015-10-30  9:48 ` Pedro Alves
2015-10-30 17:08 ` Simon Marchi
2015-10-30 17:39   ` Pedro Alves
2015-10-31 20:11     ` Simon Marchi [this message]
2015-10-31 20:28       ` Pedro Alves
2015-11-01  2:07 ` Doug Evans
2015-11-03 11:48   ` Pedro Alves
2015-11-01  4:04 ` Doug Evans
2015-11-03 12:23   ` Pedro Alves
2015-11-01 15:19 ` Patrick Palka
2015-11-03 11:18   ` Pedro Alves
2015-11-09 13:25     ` Patrick Palka
2015-11-17 13:49       ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56338FA1.2060300@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@sourceware.org \
    --cc=palves@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox