public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/2] [gdb/symtab] Remove dead code in parse_macro_definition
Date: Mon, 24 Jun 2024 10:14:26 +0200	[thread overview]
Message-ID: <20240624081427.13495-1-tdevries@suse.de> (raw)

In parse_macro_definition, there's a loop:
...
  for (p = body; *p; p++)
    if (*p == ' ' || *p == '(')
      break;
...
whose post-condition is:
...
  gdb_assert (*p == ' ' || *p == '(' || *p == '\0');
...

Consequently, in the following:
...
  if (*p == ' ' || *p == '\0')
    <BODY1>
  else if (*p == '(')
    <BODY2>
  else
    <BODY3>
...
BODY3 is dead code.

Remove it, and get rid of unnecessary indentation by using an early-exit:
....
  if (*p == ' ' || *p == '\0')
    {
      <BODY1>
      return;
    }

  gdb_assert (*p == '(');
  <BODY2>
...

Tested on aarch64-linux.
---
 gdb/dwarf2/macro.c | 117 ++++++++++++++++++++++-----------------------
 1 file changed, 56 insertions(+), 61 deletions(-)

diff --git a/gdb/dwarf2/macro.c b/gdb/dwarf2/macro.c
index a511d0a3b44..bc781c2cb92 100644
--- a/gdb/dwarf2/macro.c
+++ b/gdb/dwarf2/macro.c
@@ -154,88 +154,83 @@ parse_macro_definition (struct macro_source_file *file, int line,
 	}
 
       macro_define_object (file, line, name.c_str (), replacement);
+      return;
     }
-  else if (*p == '(')
-    {
-      /* It's a function-like macro.  */
-      std::string name (body, p - body);
-      int argc = 0;
-      int argv_size = 1;
-      char **argv = XNEWVEC (char *, argv_size);
-
-      p++;
 
-      p = consume_improper_spaces (p, body);
+  /* It's a function-like macro.  */
+  gdb_assert (*p == '(');
+  std::string name (body, p - body);
+  int argc = 0;
+  int argv_size = 1;
+  char **argv = XNEWVEC (char *, argv_size);
 
-      /* Parse the formal argument list.  */
-      while (*p && *p != ')')
-	{
-	  /* Find the extent of the current argument name.  */
-	  const char *arg_start = p;
+  p++;
 
-	  while (*p && *p != ',' && *p != ')' && *p != ' ')
-	    p++;
-
-	  if (! *p || p == arg_start)
-	    dwarf2_macro_malformed_definition_complaint (body);
-	  else
-	    {
-	      /* Make sure argv has room for the new argument.  */
-	      if (argc >= argv_size)
-		{
-		  argv_size *= 2;
-		  argv = XRESIZEVEC (char *, argv, argv_size);
-		}
+  p = consume_improper_spaces (p, body);
 
-	      argv[argc++] = savestring (arg_start, p - arg_start);
-	    }
+  /* Parse the formal argument list.  */
+  while (*p && *p != ')')
+    {
+      /* Find the extent of the current argument name.  */
+      const char *arg_start = p;
 
-	  p = consume_improper_spaces (p, body);
+      while (*p && *p != ',' && *p != ')' && *p != ' ')
+	p++;
 
-	  /* Consume the comma, if present.  */
-	  if (*p == ',')
+      if (! *p || p == arg_start)
+	dwarf2_macro_malformed_definition_complaint (body);
+      else
+	{
+	  /* Make sure argv has room for the new argument.  */
+	  if (argc >= argv_size)
 	    {
-	      p++;
-
-	      p = consume_improper_spaces (p, body);
+	      argv_size *= 2;
+	      argv = XRESIZEVEC (char *, argv, argv_size);
 	    }
+
+	  argv[argc++] = savestring (arg_start, p - arg_start);
 	}
 
-      if (*p == ')')
+      p = consume_improper_spaces (p, body);
+
+      /* Consume the comma, if present.  */
+      if (*p == ',')
 	{
 	  p++;
 
-	  if (*p == ' ')
-	    /* Perfectly formed definition, no complaints.  */
-	    macro_define_function (file, line, name.c_str (),
-				   argc, (const char **) argv,
-				   p + 1);
-	  else if (*p == '\0')
-	    {
-	      /* Complain, but do define it.  */
-	      dwarf2_macro_malformed_definition_complaint (body);
-	      macro_define_function (file, line, name.c_str (),
-				     argc, (const char **) argv,
-				     p);
-	    }
-	  else
-	    /* Just complain.  */
-	    dwarf2_macro_malformed_definition_complaint (body);
+	  p = consume_improper_spaces (p, body);
+	}
+    }
+
+  if (*p == ')')
+    {
+      p++;
+
+      if (*p == ' ')
+	/* Perfectly formed definition, no complaints.  */
+	macro_define_function (file, line, name.c_str (),
+			       argc, (const char **) argv,
+			       p + 1);
+      else if (*p == '\0')
+	{
+	  /* Complain, but do define it.  */
+	  dwarf2_macro_malformed_definition_complaint (body);
+	  macro_define_function (file, line, name.c_str (),
+				 argc, (const char **) argv,
+				 p);
 	}
       else
 	/* Just complain.  */
 	dwarf2_macro_malformed_definition_complaint (body);
-
-      {
-	int i;
-
-	for (i = 0; i < argc; i++)
-	  xfree (argv[i]);
-      }
-      xfree (argv);
     }
   else
+    /* Just complain.  */
     dwarf2_macro_malformed_definition_complaint (body);
+
+  for (int i = 0; i < argc; i++)
+    xfree (argv[i]);
+
+  xfree (argv);
 }
 
 /* Skip some bytes from BYTES according to the form given in FORM.

base-commit: 18b13d11d37c8b7e7f9d0548d54e0cb3a01e81fb
-- 
2.35.3


             reply	other threads:[~2024-06-24  8:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-24  8:14 Tom de Vries [this message]
2024-06-24  8:14 ` [PATCH 2/2] [gdb/symtab] Malloc/free less " Tom de Vries
2024-06-24 18:20   ` Tom Tromey
2024-06-26  6:33     ` Tom de Vries
2024-06-24 13:08 ` [PATCH 1/2] [gdb/symtab] Remove dead code " Alexandra Petlanova Hajkova
2024-06-24 18:23 ` Tom Tromey

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=20240624081427.13495-1-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).