This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 1/2] convert flags to bitfields


This changes various flags struct cmd_list_element into bitfields.  In
general I think bitfields are cleaner than flag words, at least in a
case like this where there is no need to pass the flags around
independently of the enclosing struct.

2014-01-13  Tom Tromey  <tromey@redhat.com>

	* cli/cli-decode.c (add_cmd, deprecate_cmd, add_alias_cmd)
	(add_setshow_cmd_full, delete_cmd, lookup_cmd_1)
	(deprecated_cmd_warning, complete_on_cmdlist): Update.
	* cli/cli-decode.h (CMD_DEPRECATED, DEPRECATED_WARN_USER)
	(MALLOCED_REPLACEMENT, DOC_ALLOCATED): Remove.
	(struct cmd_list_element) <flags>: Remove.
	<cmd_deprecated, deprecated_warn_user, malloced_replacement,
	doc_allocated>: New fields.
	<hook_in, allow_unknown, abbrev_flag, type, var_type>: Now
	bitfields.
	* maint.c (maintenance_do_deprecate): Update.
	* top.c (execute_command): Update.
---
 gdb/ChangeLog        | 15 ++++++++++++
 gdb/cli/cli-decode.c | 36 ++++++++++++++++-------------
 gdb/cli/cli-decode.h | 65 +++++++++++++++++++++++-----------------------------
 gdb/maint.c          | 28 +++++++++++++++-------
 gdb/top.c            |  2 +-
 5 files changed, 85 insertions(+), 61 deletions(-)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index 2247c79..2862b3f 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,5 +1,20 @@
 2014-01-13  Tom Tromey  <tromey@redhat.com>
 
+	* cli/cli-decode.c (add_cmd, deprecate_cmd, add_alias_cmd)
+	(add_setshow_cmd_full, delete_cmd, lookup_cmd_1)
+	(deprecated_cmd_warning, complete_on_cmdlist): Update.
+	* cli/cli-decode.h (CMD_DEPRECATED, DEPRECATED_WARN_USER)
+	(MALLOCED_REPLACEMENT, DOC_ALLOCATED): Remove.
+	(struct cmd_list_element) <flags>: Remove.
+	<cmd_deprecated, deprecated_warn_user, malloced_replacement,
+	doc_allocated>: New fields.
+	<hook_in, allow_unknown, abbrev_flag, type, var_type>: Now
+	bitfields.
+	* maint.c (maintenance_do_deprecate): Update.
+	* top.c (execute_command): Update.
+
+2014-01-13  Tom Tromey  <tromey@redhat.com>
+
 	* defs.h (XCALLOC): Remove.
 	* bcache.c (bcache_xmalloc): Use XCNEW, not XCALLOC.
 	(print_bcache_statistics): Use XCNEWVEC, not XCALLOC.
diff --git a/gdb/cli/cli-decode.c b/gdb/cli/cli-decode.c
index 853ec88..d36ab4e 100644
--- a/gdb/cli/cli-decode.c
+++ b/gdb/cli/cli-decode.c
@@ -226,7 +226,10 @@ add_cmd (const char *name, enum command_class class, void (*fun) (char *, int),
   set_cmd_cfunc (c, fun);
   set_cmd_context (c, NULL);
   c->doc = doc;
-  c->flags = 0;
+  c->cmd_deprecated = 0;
+  c->deprecated_warn_user = 0;
+  c->malloced_replacement = 0;
+  c->doc_allocated = 0;
   c->replacement = NULL;
   c->pre_show_hook = NULL;
   c->hook_in = 0;
@@ -261,7 +264,8 @@ add_cmd (const char *name, enum command_class class, void (*fun) (char *, int),
 struct cmd_list_element *
 deprecate_cmd (struct cmd_list_element *cmd, char *replacement)
 {
-  cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER);
+  cmd->cmd_deprecated = 1;
+  cmd->deprecated_warn_user = 1;
 
   if (replacement != NULL)
     cmd->replacement = replacement;
@@ -298,10 +302,10 @@ add_alias_cmd (const char *name, const char *oldname, enum command_class class,
   c = add_cmd (name, class, NULL, old->doc, list);
 
   /* If OLD->DOC can be freed, we should make another copy.  */
-  if ((old->flags & DOC_ALLOCATED) != 0)
+  if (old->doc_allocated)
     {
       c->doc = xstrdup (old->doc);
-      c->flags |= DOC_ALLOCATED;
+      c->doc_allocated = 1;
     }
   /* NOTE: Both FUNC and all the FUNCTIONs need to be copied.  */
   c->func = old->func;
@@ -448,7 +452,7 @@ add_setshow_cmd_full (const char *name,
     }
   set = add_set_or_show_cmd (name, set_cmd, class, var_type, var,
 			     full_set_doc, set_list);
-  set->flags |= DOC_ALLOCATED;
+  set->doc_allocated = 1;
 
   if (set_func != NULL)
     set_cmd_sfunc (set, set_func);
@@ -457,7 +461,7 @@ add_setshow_cmd_full (const char *name,
 
   show = add_set_or_show_cmd (name, show_cmd, class, var_type, var,
 			      full_show_doc, show_list);
-  show->flags |= DOC_ALLOCATED;
+  show->doc_allocated = 1;
   show->show_value_func = show_func;
 
   if (set_result != NULL)
@@ -800,7 +804,7 @@ delete_cmd (const char *name, struct cmd_list_element **list,
 	  *prehookee = iter->hookee_pre;
 	  if (iter->hookee_post)
 	    iter->hookee_post->hook_post = 0;
-	  if (iter->doc && (iter->flags & DOC_ALLOCATED) != 0)
+	  if (iter->doc && iter->doc_allocated)
 	    xfree (iter->doc);
 	  *posthook = iter->hook_post;
 	  *posthookee = iter->hookee_post;
@@ -1395,7 +1399,7 @@ lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
        itself and we will adjust the appropriate DEPRECATED_WARN_USER
        flags.  */
       
-      if (found->flags & DEPRECATED_WARN_USER)
+      if (found->deprecated_warn_user)
 	deprecated_cmd_warning (line);
       found = found->cmd_pointer;
     }
@@ -1600,14 +1604,14 @@ deprecated_cmd_warning (const char *text)
     /* Return if text doesn't evaluate to a command.  */
     return;
 
-  if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0)
-      || (cmd->flags & DEPRECATED_WARN_USER) ) ) 
+  if (!((alias ? alias->deprecated_warn_user : 0)
+      || cmd->deprecated_warn_user) ) 
     /* Return if nothing is deprecated.  */
     return;
   
   printf_filtered ("Warning:");
   
-  if (alias && !(cmd->flags & CMD_DEPRECATED))
+  if (alias && !cmd->cmd_deprecated)
     printf_filtered (" '%s', an alias for the", alias->name);
     
   printf_filtered (" command '");
@@ -1617,7 +1621,7 @@ deprecated_cmd_warning (const char *text)
   
   printf_filtered ("%s", cmd->name);
 
-  if (alias && (cmd->flags & CMD_DEPRECATED))
+  if (alias && cmd->cmd_deprecated)
     printf_filtered ("' (%s) is deprecated.\n", alias->name);
   else
     printf_filtered ("' is deprecated.\n"); 
@@ -1626,7 +1630,7 @@ deprecated_cmd_warning (const char *text)
   /* If it is only the alias that is deprecated, we want to indicate
      the new alias, otherwise we'll indicate the new command.  */
 
-  if (alias && !(cmd->flags & CMD_DEPRECATED))
+  if (alias && !cmd->cmd_deprecated)
     {
       if (alias->replacement)
 	printf_filtered ("Use '%s'.\n\n", alias->replacement);
@@ -1643,9 +1647,9 @@ deprecated_cmd_warning (const char *text)
 
   /* We've warned you, now we'll keep quiet.  */
   if (alias)
-    alias->flags &= ~DEPRECATED_WARN_USER;
+    alias->deprecated_warn_user = 0;
   
-  cmd->flags &= ~DEPRECATED_WARN_USER;
+  cmd->deprecated_warn_user = 0;
 }
 
 
@@ -1787,7 +1791,7 @@ complete_on_cmdlist (struct cmd_list_element *list,
 
 	    if (pass == 0)
 	      {
-		if ((ptr->flags & CMD_DEPRECATED) != 0)
+		if (ptr->cmd_deprecated)
 		  {
 		    saw_deprecated_match = 1;
 		    continue;
diff --git a/gdb/cli/cli-decode.h b/gdb/cli/cli-decode.h
index 4496eb0..10ff36d 100644
--- a/gdb/cli/cli-decode.h
+++ b/gdb/cli/cli-decode.h
@@ -44,15 +44,6 @@ cmd_types;
 /* This structure records one command'd definition.  */
 
 
-/* This flag is used by the code executing commands to warn the user
-   the first time a deprecated command is used, see the 'flags' field
-   in the following struct.
-*/
-#define CMD_DEPRECATED            0x1
-#define DEPRECATED_WARN_USER      0x2
-#define MALLOCED_REPLACEMENT      0x4
-#define DOC_ALLOCATED             0x8
-
 struct cmd_list_element
   {
     /* Points to next command in this list.  */
@@ -95,29 +86,31 @@ struct cmd_list_element
        specified stream.  */
     show_value_ftype *show_value_func;
 
-    /* flags : a bitfield 
-       
-       bit 0: (LSB) CMD_DEPRECATED, when 1 indicated that this command
-       is deprecated.  It may be removed from gdb's command set in the
-       future.
+    /* When 1 indicated that this command is deprecated.  It may be
+       removed from gdb's command set in the future.  */
+
+    unsigned int cmd_deprecated : 1;
 
-       bit 1: DEPRECATED_WARN_USER, the user needs to be warned that
-       this is a deprecated command.  The user should only be warned
-       the first time a command is used.
+    /* The user needs to be warned that this is a deprecated command.
+       The user should only be warned the first time a command is
+       used.  */
         
-       bit 2: MALLOCED_REPLACEMENT, when functions are deprecated at
-       compile time (this is the way it should, in general, be done)
-       the memory containing the replacement string is statically
-       allocated.  In some cases it makes sense to deprecate commands
-       at runtime (the testsuite is one example).  In this case the
-       memory for replacement is malloc'ed.  When a command is
-       undeprecated or re-deprecated at runtime we don't want to risk
-       calling free on statically allocated memory, so we check this
-       flag.
+    unsigned int deprecated_warn_user : 1;
+
+    /* When functions are deprecated at compile time (this is the way
+       it should, in general, be done) the memory containing the
+       replacement string is statically allocated.  In some cases it
+       makes sense to deprecate commands at runtime (the testsuite is
+       one example).  In this case the memory for replacement is
+       malloc'ed.  When a command is undeprecated or re-deprecated at
+       runtime we don't want to risk calling free on statically
+       allocated memory, so we check this flag.  */
 
-       bit 3: DOC_ALLOCATED, set if the doc field should be xfree'd.  */
+    unsigned int malloced_replacement : 1;
 
-    int flags;
+    /* Set if the doc field should be xfree'd.  */
+
+    unsigned int doc_allocated : 1;
 
     /* If this command is deprecated, this is the replacement name.  */
     char *replacement;
@@ -129,12 +122,12 @@ struct cmd_list_element
     /* Hook for another command to be executed before this command.  */
     struct cmd_list_element *hook_pre;
 
-    /* Hook for another command to be executed after this command.  */
-    struct cmd_list_element *hook_post;
-
     /* Flag that specifies if this command is already running its hook.  */
     /* Prevents the possibility of hook recursion.  */
-    int hook_in;
+    unsigned int hook_in : 1;
+
+    /* Hook for another command to be executed after this command.  */
+    struct cmd_list_element *hook_post;
 
     /* Nonzero identifies a prefix command.  For them, the address
        of the variable containing the list of subcommands.  */
@@ -150,7 +143,7 @@ struct cmd_list_element
     /* For prefix commands only:
        nonzero means do not get an error if subcommand is not
        recognized; call the prefix's own function in that case.  */
-    char allow_unknown;
+    unsigned int allow_unknown : 1;
 
     /* The prefix command of this command.  */
     struct cmd_list_element *prefix;
@@ -159,7 +152,7 @@ struct cmd_list_element
        be mentioned in lists of commands.
        This allows "br<tab>" to complete to "break", which it
        otherwise wouldn't.  */
-    char abbrev_flag;
+    unsigned int abbrev_flag : 1;
 
     /* Completion routine for this command.  TEXT is the text beyond
        what was matched for the command itself (leading whitespace is
@@ -183,14 +176,14 @@ struct cmd_list_element
 
     /* Type of "set" or "show" command (or SET_NOT_SET if not "set"
        or "show").  */
-    cmd_types type;
+    ENUM_BITFIELD (cmd_types) type : 2;
 
     /* Pointer to variable affected by "set" and "show".  Doesn't
        matter if type is not_set.  */
     void *var;
 
     /* What kind of variable is *VAR?  */
-    var_types var_type;
+    ENUM_BITFIELD (var_types) var_type : 4;
 
     /* Pointer to NULL terminated list of enumerated values (like
        argv).  */
diff --git a/gdb/maint.c b/gdb/maint.c
index 41a39e7..2fa6add 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -615,28 +615,40 @@ maintenance_do_deprecate (char *text, int deprecate)
      memory.  */
   if (alias)
     {
-      if (alias->flags & MALLOCED_REPLACEMENT)
+      if (alias->malloced_replacement)
 	xfree (alias->replacement);
 
       if (deprecate)
-	alias->flags |= (DEPRECATED_WARN_USER | CMD_DEPRECATED);
+	{
+	  alias->deprecated_warn_user = 1;
+	  alias->cmd_deprecated = 1;
+	}
       else
-	alias->flags &= ~(DEPRECATED_WARN_USER | CMD_DEPRECATED);
+	{
+	  alias->deprecated_warn_user = 0;
+	  alias->cmd_deprecated = 0;
+	}
       alias->replacement = replacement;
-      alias->flags |= MALLOCED_REPLACEMENT;
+      alias->malloced_replacement = 1;
       return;
     }
   else if (cmd)
     {
-      if (cmd->flags & MALLOCED_REPLACEMENT)
+      if (cmd->malloced_replacement)
 	xfree (cmd->replacement);
 
       if (deprecate)
-	cmd->flags |= (DEPRECATED_WARN_USER | CMD_DEPRECATED);
+	{
+	  cmd->deprecated_warn_user = 1;
+	  cmd->cmd_deprecated = 1;
+	}
       else
-	cmd->flags &= ~(DEPRECATED_WARN_USER | CMD_DEPRECATED);
+	{
+	  cmd->deprecated_warn_user = 0;
+	  cmd->cmd_deprecated = 0;
+	}
       cmd->replacement = replacement;
-      cmd->flags |= MALLOCED_REPLACEMENT;
+      cmd->malloced_replacement = 1;
       return;
     }
   xfree (replacement);
diff --git a/gdb/top.c b/gdb/top.c
index d068450..42950ea 100644
--- a/gdb/top.c
+++ b/gdb/top.c
@@ -450,7 +450,7 @@ execute_command (char *p, int from_tty)
       /* If this command has been pre-hooked, run the hook first.  */
       execute_cmd_pre_hook (c);
 
-      if (c->flags & DEPRECATED_WARN_USER)
+      if (c->deprecated_warn_user)
 	deprecated_cmd_warning (line);
 
       /* c->user_commands would be NULL in the case of a python command.  */
-- 
1.8.1.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]