]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: make macro_expand_next return a gdb::unique_xmalloc_ptr<char>
authorSimon Marchi <simon.marchi@polymtl.ca>
Fri, 3 Jul 2020 00:38:47 +0000 (20:38 -0400)
committerSimon Marchi <simon.marchi@polymtl.ca>
Sat, 4 Jul 2020 02:27:09 +0000 (22:27 -0400)
For some reason, macro_expand_next does not return a
gdb::unique_xmalloc_ptr<char>, like its counterparts macro_expand and
macro_expand_once.  This patch fixes that.

macro_buffer::release now returns a gdb::unique_xmalloc_ptr<char> too,
which required updating the other callers.  The `.release (). release
()` in macro_stringify looks a bit funny, but it's because one release
is for the macro_buffer, and the other is for the unique ptr.

I removed the ATTRIBUTE_UNUSED_RESULT on macro_buffer::release, I don't
really understand why it's there.  I don't see how this method could be
called without using the result, that would be an obvious memory leak.
The commit that introduced it (4e4a8b932b7 "Add ATTRIBUTE_UNUSED_RESULT
to macro_buffer") doesn't give any details.

gdb/ChangeLog:

* c-exp.y (scan_macro_expansion): Don't free `expansion`.
(lex_one_token): Update.
* macroexp.c (struct macro_buffer) <release>: Return
gdb::unique_xmalloc_ptr<char>.
(macro_stringify): Update.
(macro_expand): Update.
(macro_expand_next): Return gdb::unique_xmalloc_ptr<char>.
* macroexp.h (macro_expand_next): Likewise.

Change-Id: I67a74d0d479d2c20cdc82161ead7c54cea034f56

gdb/ChangeLog
gdb/c-exp.y
gdb/macroexp.c
gdb/macroexp.h

index 513f1711b259d6a324595d9fd78d53cf9c3d99f8..3cede90590652f5dca1a2455fe2bab2e4fffe1cc 100644 (file)
@@ -1,3 +1,14 @@
+2020-07-02  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       * c-exp.y (scan_macro_expansion): Don't free `expansion`.
+       (lex_one_token): Update.
+       * macroexp.c (struct macro_buffer) <release>: Return
+       gdb::unique_xmalloc_ptr<char>.
+       (macro_stringify): Update.
+       (macro_expand): Update.
+       (macro_expand_next): Return gdb::unique_xmalloc_ptr<char>.
+       * macroexp.h (macro_expand_next): Likewise.
+
 2020-07-02  Simon Marchi  <simon.marchi@efficios.com>
 
        * macroexp.h (macro_lookup_ftype): Remove.
index 61fa2fe684db009c2fe1dd5a6c19d03d05bb6328..7fc23c4c8d285e0fed492a783120f68ab1bbb6d1 100644 (file)
@@ -2551,17 +2551,13 @@ static const struct token ident_tokens[] =
 
 
 static void
-scan_macro_expansion (char *expansion)
+scan_macro_expansion (const char *expansion)
 {
-  const char *copy;
-
   /* We'd better not be trying to push the stack twice.  */
   gdb_assert (! cpstate->macro_original_text);
 
-  /* Copy to the obstack, and then free the intermediate
-     expansion.  */
-  copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
-  xfree (expansion);
+  /* Copy to the obstack.  */
+  const char *copy = obstack_strdup (&cpstate->expansion_obstack, expansion);
 
   /* Save the old lexptr value, so we can return to it when we're done
      parsing the expanded text.  */
@@ -2631,11 +2627,11 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
   /* Check if this is a macro invocation that we need to expand.  */
   if (! scanning_macro_expansion ())
     {
-      char *expanded = macro_expand_next (&pstate->lexptr,
-                                         *expression_macro_scope);
+      gdb::unique_xmalloc_ptr<char> expanded
+       = macro_expand_next (&pstate->lexptr, *expression_macro_scope);
 
-      if (expanded)
-        scan_macro_expansion (expanded);
+      if (expanded != nullptr)
+        scan_macro_expansion (expanded.get ());
     }
 
   pstate->prev_lexptr = pstate->lexptr;
index 92823807f153450d23dfef46ba0777e93cb53f1b..e1d185d30c8e2c65faf2688752fa7cfd8d69dbe2 100644 (file)
@@ -128,15 +128,14 @@ struct macro_buffer
       xfree (text);
   }
 
-  /* Release the text of the buffer to the caller, which is now
-     responsible for freeing it.  */
-  ATTRIBUTE_UNUSED_RESULT char *release ()
+  /* Release the text of the buffer to the caller.  */
+  gdb::unique_xmalloc_ptr<char> release ()
   {
     gdb_assert (! shared);
     gdb_assert (size);
     char *result = text;
     text = NULL;
-    return result;
+    return gdb::unique_xmalloc_ptr<char> (result);
   }
 
   /* Resize the buffer to be at least N bytes long.  Raise an error if
@@ -708,7 +707,7 @@ macro_stringify (const char *str)
   stringify (&buffer, str, len);
   buffer.appendc ('\0');
 
-  return buffer.release ();
+  return buffer.release ().release ();
 }
 
 \f
@@ -1429,7 +1428,7 @@ macro_expand (const char *source, const macro_scope &scope)
 
   dest.appendc ('\0');
 
-  return gdb::unique_xmalloc_ptr<char> (dest.release ());
+  return dest.release ();
 }
 
 
@@ -1439,8 +1438,7 @@ macro_expand_once (const char *source, const macro_scope &scope)
   error (_("Expand-once not implemented yet."));
 }
 
-
-char *
+gdb::unique_xmalloc_ptr<char>
 macro_expand_next (const char **lexptr, const macro_scope &scope)
 {
   struct macro_buffer tok;
@@ -1454,7 +1452,7 @@ macro_expand_next (const char **lexptr, const macro_scope &scope)
 
   /* Get the text's first preprocessing token.  */
   if (! get_token (&tok, &src))
-    return 0;
+    return nullptr;
 
   /* If it's a macro invocation, expand it.  */
   if (maybe_expand (&dest, &tok, &src, 0, scope))
@@ -1469,6 +1467,6 @@ macro_expand_next (const char **lexptr, const macro_scope &scope)
   else
     {
       /* It wasn't a macro invocation.  */
-      return 0;
+      return nullptr;
     }
 }
index ec992f22796ddaccb727d2ebbc5cc06ca040047b..511991cacd20720df59ea57791ff6d14d2d83712 100644 (file)
@@ -68,7 +68,8 @@ gdb::unique_xmalloc_ptr<char> macro_expand_once (const char *source,
    much have to do tokenization to find the end of the string that
    needs to be macro-expanded.  Our C/C++ tokenizer isn't really
    designed to be called by anything but the yacc parser engine.  */
-char *macro_expand_next (const char **lexptr, const macro_scope &scope);
+gdb::unique_xmalloc_ptr<char> macro_expand_next (const char **lexptr,
+                                                const macro_scope &scope);
 
 /* Functions to classify characters according to cpp rules.  */