Change language_defn::parser to return void

Message ID 20210914004228.3058936-1-tom@tromey.com
State New
Headers show
Series
  • Change language_defn::parser to return void
Related show

Commit Message

Tom Tromey Sept. 14, 2021, 12:42 a.m.
I noticed that language_defn::parser returns an 'int' -- but the
meaning of this return value is never defined (in practice it is
defined by yacc), and also never examined.  It seems clear to simply
return void instead.
---
 gdb/ada-exp.y    | 6 ++----
 gdb/ada-lang.c   | 4 ++--
 gdb/ada-lang.h   | 2 +-
 gdb/c-exp.y      | 6 ++----
 gdb/c-lang.h     | 2 +-
 gdb/d-exp.y      | 6 ++----
 gdb/d-lang.c     | 4 ++--
 gdb/d-lang.h     | 2 +-
 gdb/f-exp.y      | 6 ++----
 gdb/f-lang.h     | 2 +-
 gdb/go-exp.y     | 6 ++----
 gdb/go-lang.h    | 2 +-
 gdb/language.c   | 6 +++---
 gdb/language.h   | 2 +-
 gdb/m2-exp.y     | 6 ++----
 gdb/m2-lang.h    | 2 +-
 gdb/p-exp.y      | 6 ++----
 gdb/p-lang.h     | 2 +-
 gdb/rust-lang.h  | 2 +-
 gdb/rust-parse.c | 4 +---
 20 files changed, 31 insertions(+), 47 deletions(-)

-- 
2.31.1

Comments

Philippe Waroquiers via Gdb-patches Sept. 14, 2021, 10:56 a.m. | #1
[ Fix missing gdb-patches @ ]

On 9/14/21 11:35 AM, Tom de Vries wrote:
> On 9/14/21 2:42 AM, Tom Tromey wrote:

>> -int

>> +void

>>  c_parse (struct parser_state *par_state)

>>  {

>>    /* Setting up the parser state.  */

>> @@ -3440,10 +3440,8 @@ c_parse (struct parser_state *par_state)

>>    popping = 0;

>>    name_obstack.clear ();

>>  

>> -  int result = yyparse ();

>> -  if (!result)

>> +  if (!yyparse ())

>>      pstate->set_operation (pstate->pop ());

>> -  return result;

>>  }

> 

> I looked at this a little bit, and it seems ok to me.  If we're not

> using the result, then why return it indeed.

> 

> Trying to understand the meaning of the return values of yyparse, I

> found here (

> https://www.gnu.org/software/bison/manual/html_node/Parser-Function.html ):

> ...

> 

> Function: int yyparse (void)

> 

>     The value returned by yyparse is 0 if parsing was successful (return

>     is due to end-of-input).

> 

>     The value is 1 if parsing failed because of invalid input, i.e.,

>     input that contains a syntax error or that causes YYABORT to be

>     invoked.

> 

>     The value is 2 if parsing failed due to memory exhaustion.

> ...

> 

> So I wonder why we're not doing here:

> ...

>   int result = yyparse ();

>   if (!result ())

>       pstate->set_operation (pstate->pop ());

>   if (result == 2)

>      error ("parser ran out of memory")

>   if (result == 1)

>      error ("parser ran into unhandled error")

>   gdb_assert (result == 0);

> ...

> 

> Thanks,

> - Tom

>
Tom Tromey Sept. 18, 2021, 1:47 a.m. | #2
>> Function: int yyparse (void)

>> 

>> The value returned by yyparse is 0 if parsing was successful (return

>> is due to end-of-input).

>> 

>> The value is 1 if parsing failed because of invalid input, i.e.,

>> input that contains a syntax error or that causes YYABORT to be

>> invoked.

>> 

>> The value is 2 if parsing failed due to memory exhaustion.

>> ...

>> 

>> So I wonder why we're not doing here:

>> ...


I think neither of these is really possible in gdb, because yyerror is
defined to throw an exception, and because the parsers are set up to use
xmalloc, which also throws.  Or does it crash?  I forget since I never
see it.

Tom

Patch

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index 66d58b06cb5..45ed4e4709a 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -1073,7 +1073,7 @@  static struct obstack temp_parse_space;
 #define qsort __qsort__dummy
 #include "ada-lex.c"
 
-int
+void
 ada_parse (struct parser_state *par_state)
 {
   /* Setting up the parser state.  */
@@ -1087,15 +1087,13 @@  ada_parse (struct parser_state *par_state)
   components.clear ();
   associations.clear ();
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     {
       struct type *context_type = nullptr;
       if (par_state->void_context_p)
 	context_type = parse_type (par_state)->builtin_void;
       pstate->set_operation (ada_pop (true, context_type));
     }
-  return result;
 }
 
 static void
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 6680a4fd657..fcba1279ced 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13357,10 +13357,10 @@  class ada_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override
+  void parser (struct parser_state *ps) const override
   {
     warnings_issued = 0;
-    return ada_parse (ps);
+    ada_parse (ps);
   }
 
   /* See language.h.  */
diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index a89ed29119a..661d58edf16 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -155,7 +155,7 @@  extern int ada_get_field_index (const struct type *type,
 				const char *field_name,
 				int maybe_missing);
 
-extern int ada_parse (struct parser_state *);    /* Defined in ada-exp.y */
+extern void ada_parse (struct parser_state *);    /* Defined in ada-exp.y */
 
 			/* Defined in ada-typeprint.c */
 extern void ada_print_type (struct type *, const char *, struct ui_file *, int,
diff --git a/gdb/c-exp.y b/gdb/c-exp.y
index 93b10f05b7d..1ed42261e68 100644
--- a/gdb/c-exp.y
+++ b/gdb/c-exp.y
@@ -3404,7 +3404,7 @@  yylex (void)
   return current.token;
 }
 
-int
+void
 c_parse (struct parser_state *par_state)
 {
   /* Setting up the parser state.  */
@@ -3440,10 +3440,8 @@  c_parse (struct parser_state *par_state)
   popping = 0;
   name_obstack.clear ();
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 #ifdef YYBISON
diff --git a/gdb/c-lang.h b/gdb/c-lang.h
index b35e8b102a8..062f361f42d 100644
--- a/gdb/c-lang.h
+++ b/gdb/c-lang.h
@@ -60,7 +60,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 *);
+extern void c_parse (struct parser_state *);
 
 extern int c_parse_escape (const char **, struct obstack *);
 
diff --git a/gdb/d-exp.y b/gdb/d-exp.y
index 1d11d9f3569..4eb93c269cb 100644
--- a/gdb/d-exp.y
+++ b/gdb/d-exp.y
@@ -1602,7 +1602,7 @@  yylex (void)
   return current.token;
 }
 
-int
+void
 d_parse (struct parser_state *par_state)
 {
   /* Setting up the parser state.  */
@@ -1626,10 +1626,8 @@  d_parse (struct parser_state *par_state)
   popping = 0;
   name_obstack.clear ();
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 static void
diff --git a/gdb/d-lang.c b/gdb/d-lang.c
index 6c9b730cf41..2e4edaa121b 100644
--- a/gdb/d-lang.c
+++ b/gdb/d-lang.c
@@ -169,9 +169,9 @@  class d_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override
+  void parser (struct parser_state *ps) const override
   {
-    return d_parse (ps);
+    d_parse (ps);
   }
 
   /* See language.h.  */
diff --git a/gdb/d-lang.h b/gdb/d-lang.h
index 96b002c717d..56b2b0b11d2 100644
--- a/gdb/d-lang.h
+++ b/gdb/d-lang.h
@@ -56,7 +56,7 @@  struct builtin_d_type
 
 /* Defined in d-exp.y.  */
 
-extern int d_parse (struct parser_state *);
+extern void d_parse (struct parser_state *);
 
 /* Defined in d-lang.c  */
 
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
index 6608831a9a5..333822ae5f9 100644
--- a/gdb/f-exp.y
+++ b/gdb/f-exp.y
@@ -1530,7 +1530,7 @@  yylex (void)
   }
 }
 
-int
+void
 f_language::parser (struct parser_state *par_state) const
 {
   /* Setting up the parser state.  */
@@ -1547,10 +1547,8 @@  f_language::parser (struct parser_state *par_state) const
   scoped_restore restore_type_stack = make_scoped_restore (&type_stack,
 							   &stack);
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 static void
diff --git a/gdb/f-lang.h b/gdb/f-lang.h
index 1ccdd3978ea..11f699f52db 100644
--- a/gdb/f-lang.h
+++ b/gdb/f-lang.h
@@ -138,7 +138,7 @@  class f_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override;
+  void parser (struct parser_state *ps) const override;
 
   /* See language.h.  */
 
diff --git a/gdb/go-exp.y b/gdb/go-exp.y
index aaa65170a03..14b1c93052c 100644
--- a/gdb/go-exp.y
+++ b/gdb/go-exp.y
@@ -1559,7 +1559,7 @@  yylex (void)
 
 /* See language.h.  */
 
-int
+void
 go_language::parser (struct parser_state *par_state) const
 {
   /* Setting up the parser state.  */
@@ -1579,10 +1579,8 @@  go_language::parser (struct parser_state *par_state) const
   popping = 0;
   name_obstack.clear ();
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 static void
diff --git a/gdb/go-lang.h b/gdb/go-lang.h
index 3495d98d15d..36340d5372f 100644
--- a/gdb/go-lang.h
+++ b/gdb/go-lang.h
@@ -119,7 +119,7 @@  class go_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override;
+  void parser (struct parser_state *ps) const override;
 
   /* See language.h.  */
 
diff --git a/gdb/language.c b/gdb/language.c
index 45ce2ebcc92..7fa763b3ba4 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -642,10 +642,10 @@  language_defn::value_print (struct value *val, struct ui_file *stream,
 
 /* See language.h.  */
 
-int
+void
 language_defn::parser (struct parser_state *ps) const
 {
-  return c_parse (ps);
+  c_parse (ps);
 }
 
 /* See language.h.  */
@@ -823,7 +823,7 @@  class auto_or_unknown_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override
+  void parser (struct parser_state *ps) const override
   {
     error (_("expression parsing not implemented for language \"%s\""),
 	   natural_name ());
diff --git a/gdb/language.h b/gdb/language.h
index 21ed47b3580..f360a84dea8 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -513,7 +513,7 @@  struct language_defn
 
   /* Parser function.  */
 
-  virtual int parser (struct parser_state *ps) const;
+  virtual void parser (struct parser_state *ps) const;
 
   /* Print the character CH (of type CHTYPE) on STREAM as part of the
      contents of a literal string whose delimiter is QUOTER.  */
diff --git a/gdb/m2-exp.y b/gdb/m2-exp.y
index f4d4eecda10..437851e8887 100644
--- a/gdb/m2-exp.y
+++ b/gdb/m2-exp.y
@@ -992,7 +992,7 @@  yylex (void)
  }
 }
 
-int
+void
 m2_language::parser (struct parser_state *par_state) const
 {
   /* Setting up the parser state.  */
@@ -1001,10 +1001,8 @@  m2_language::parser (struct parser_state *par_state) const
   pstate = par_state;
   paren_depth = 0;
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 static void
diff --git a/gdb/m2-lang.h b/gdb/m2-lang.h
index 89b9d185b96..02d97e83eb2 100644
--- a/gdb/m2-lang.h
+++ b/gdb/m2-lang.h
@@ -88,7 +88,7 @@  class m2_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override;
+  void parser (struct parser_state *ps) const override;
 
   /* See language.h.  */
 
diff --git a/gdb/p-exp.y b/gdb/p-exp.y
index f496ce2016e..86662a42491 100644
--- a/gdb/p-exp.y
+++ b/gdb/p-exp.y
@@ -1685,7 +1685,7 @@  yylex (void)
 
 /* See language.h.  */
 
-int
+void
 pascal_language::parser (struct parser_state *par_state) const
 {
   /* Setting up the parser state.  */
@@ -1694,10 +1694,8 @@  pascal_language::parser (struct parser_state *par_state) const
   pstate = par_state;
   paren_depth = 0;
 
-  int result = yyparse ();
-  if (!result)
+  if (!yyparse ())
     pstate->set_operation (pstate->pop ());
-  return result;
 }
 
 static void
diff --git a/gdb/p-lang.h b/gdb/p-lang.h
index 95dca01f22e..df802fad8c7 100644
--- a/gdb/p-lang.h
+++ b/gdb/p-lang.h
@@ -105,7 +105,7 @@  class pascal_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override;
+  void parser (struct parser_state *ps) const override;
 
   /* See language.h.  */
 
diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h
index ea3f974c979..88a076ebb78 100644
--- a/gdb/rust-lang.h
+++ b/gdb/rust-lang.h
@@ -165,7 +165,7 @@  class rust_language : public language_defn
 
   /* See language.h.  */
 
-  int parser (struct parser_state *ps) const override;
+  void parser (struct parser_state *ps) const override;
 
   /* See language.h.  */
 
diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 539e1c8256d..cfc058c870d 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -2071,7 +2071,7 @@  rust_parser::parse_atom (bool required)
 
 /* The parser as exposed to gdb.  */
 
-int
+void
 rust_language::parser (struct parser_state *state) const
 {
   rust_parser parser (state);
@@ -2094,8 +2094,6 @@  rust_language::parser (struct parser_state *state) const
     }
 
   state->set_operation (std::move (result));
-
-  return 0;
 }