[v3,1/2] gdb: Remove compile-related attributes from struct language

Message ID 20250325124249.4023627-2-guinevere@redhat.com
State New
Headers
Series Adding ability to disable GDB compile |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed

Commit Message

Guinevere Larsen March 25, 2025, 12:42 p.m. UTC
  The following patch will add a configure option to disable the compile
subsystem at compilation time. To do that, nearly all code that
interfaces with compile should be confined to the compile sub-folder.

This commit is the first step, removing the compile-related method from
the language struct and adding 2 new functions to compile.c that do the
same job in a slightly different way. Adding things to the language
struct is a more extendable way to add support for languages, but
considering compile is quite bit-rotted and questionably supported, I
don't think it will be extended any time soon, and using ifdefs to
handle disabling compile with configure felt like a messier solution.

There should be no visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
---
 gdb/c-lang.c                   | 32 --------------------------
 gdb/c-lang.h                   | 38 ------------------------------
 gdb/compile/compile-internal.h | 39 +++++++++++++++++++++++++++++++
 gdb/compile/compile.c          | 42 +++++++++++++++++++++++++++++++---
 gdb/language.c                 |  8 -------
 gdb/language.h                 | 32 --------------------------
 6 files changed, 78 insertions(+), 113 deletions(-)
  

Patch

diff --git a/gdb/c-lang.c b/gdb/c-lang.c
index c28493fc66b..55922344bfc 100644
--- a/gdb/c-lang.c
+++ b/gdb/c-lang.c
@@ -806,22 +806,6 @@  class c_language : public language_defn
     c_language_arch_info (gdbarch, lai);
   }
 
-  /* See language.h.  */
-  std::unique_ptr<compile_instance> get_compile_instance () const override
-  {
-    return c_get_compile_context ();
-  }
-
-  /* See language.h.  */
-  std::string compute_program (compile_instance *inst,
-			       const char *input,
-			       struct gdbarch *gdbarch,
-			       const struct block *expr_block,
-			       CORE_ADDR expr_pc) const override
-  {
-    return c_compute_program (inst, input, gdbarch, expr_block, expr_pc);
-  }
-
   /* See language.h.  */
 
   bool can_print_type_offsets () const override
@@ -942,22 +926,6 @@  class cplus_language : public language_defn
     return cp_lookup_transparent_type (name, flags);
   }
 
-  /* See language.h.  */
-  std::unique_ptr<compile_instance> get_compile_instance () const override
-  {
-    return cplus_get_compile_context ();
-  }
-
-  /* See language.h.  */
-  std::string compute_program (compile_instance *inst,
-			       const char *input,
-			       struct gdbarch *gdbarch,
-			       const struct block *expr_block,
-			       CORE_ADDR expr_pc) const override
-  {
-    return cplus_compute_program (inst, input, gdbarch, expr_block, expr_pc);
-  }
-
   /* See language.h.  */
   unsigned int search_name_hash (const char *name) const override
   {
diff --git a/gdb/c-lang.h b/gdb/c-lang.h
index 0e733d810d8..06b7ad0b8f4 100644
--- a/gdb/c-lang.h
+++ b/gdb/c-lang.h
@@ -25,7 +25,6 @@  struct ui_file;
 struct language_arch_info;
 struct type_print_options;
 struct parser_state;
-struct compile_instance;
 
 #include "compile/compile.h"
 #include "value.h"
@@ -132,43 +131,6 @@  extern bool c_is_string_type_p (struct type *type);
 
 extern int c_textual_element_type (struct type *, char);
 
-/* Create a new instance of the C compiler and return it.  This
-   function never returns NULL, but rather throws an exception on
-   failure.  This is suitable for use as the
-   language_defn::get_compile_instance method.  */
-
-extern std::unique_ptr<compile_instance> c_get_compile_context ();
-
-/* Create a new instance of the C++ compiler and return it.  This
-   function never returns NULL, but rather throws an exception on
-   failure.  This is suitable for use as the
-   language_defn::get_compile_instance method.  */
-
-extern std::unique_ptr<compile_instance> cplus_get_compile_context ();
-
-/* This takes the user-supplied text and returns a new bit of code to
-   compile.
-
-   This is used as the compute_program language method; see that
-   for a description of the arguments.  */
-
-extern std::string c_compute_program (compile_instance *inst,
-				      const char *input,
-				      struct gdbarch *gdbarch,
-				      const struct block *expr_block,
-				      CORE_ADDR expr_pc);
-
-/* This takes the user-supplied text and returns a new bit of code to compile.
-
-   This is used as the compute_program language method; see that
-   for a description of the arguments.  */
-
-extern std::string cplus_compute_program (compile_instance *inst,
-					  const char *input,
-					  struct gdbarch *gdbarch,
-					  const struct block *expr_block,
-					  CORE_ADDR expr_pc);
-
 /* Return the canonical form of the C symbol NAME.  If NAME is already
    canonical, return nullptr.  */
 
diff --git a/gdb/compile/compile-internal.h b/gdb/compile/compile-internal.h
index f4cc9ee4984..789782dccaf 100644
--- a/gdb/compile/compile-internal.h
+++ b/gdb/compile/compile-internal.h
@@ -80,4 +80,43 @@  class compile_file_names
   std::string m_object_file;
 };
 
+struct compile_instance;
+
+/* Create a new instance of the C compiler and return it.  This
+   function never returns NULL, but rather throws an exception on
+   failure.  This is suitable for use as the
+   language_defn::get_compile_instance method.  */
+
+extern std::unique_ptr<compile_instance> c_get_compile_context ();
+
+/* Create a new instance of the C++ compiler and return it.  This
+   function never returns NULL, but rather throws an exception on
+   failure.  This is suitable for use as the
+   language_defn::get_compile_instance method.  */
+
+extern std::unique_ptr<compile_instance> cplus_get_compile_context ();
+
+/* This takes the user-supplied text and returns a new bit of code to
+   compile.
+
+   This is used as the compute_program language method; see that
+   for a description of the arguments.  */
+
+extern std::string c_compute_program (compile_instance *inst,
+				      const char *input,
+				      struct gdbarch *gdbarch,
+				      const struct block *expr_block,
+				      CORE_ADDR expr_pc);
+
+/* This takes the user-supplied text and returns a new bit of code to compile.
+
+   This is used as the compute_program language method; see that
+   for a description of the arguments.  */
+
+extern std::string cplus_compute_program (compile_instance *inst,
+					  const char *input,
+					  struct gdbarch *gdbarch,
+					  const struct block *expr_block,
+					  CORE_ADDR expr_pc);
+
 #endif /* GDB_COMPILE_COMPILE_INTERNAL_H */
diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index d6bcc1f61f1..43987f9b2b7 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -527,6 +527,41 @@  print_callback (void *ignore, const char *message)
   gdb_puts (message, gdb_stderr);
 }
 
+/* Helper for compile_to_object, to find the compile context
+   based on the current language.  */
+static std::unique_ptr<compile_instance>
+get_language_compile_context ()
+{
+  switch (current_language->la_language)
+    {
+    case language_c:
+      return c_get_compile_context ();
+    case language_cplus:
+      return cplus_get_compile_context ();
+    default:
+      return {};
+    }
+}
+
+/* Helper for compile_to_object, to call the correct
+   compute_program based on the current language.  */
+static std::string
+compute_program_language (compile_instance *inst, const char *input,
+			  struct gdbarch *gdbarch,
+			  const struct block *block,
+			  CORE_ADDR pc)
+{
+  switch (current_language->la_language)
+    {
+    case language_c:
+      return c_compute_program (inst, input, gdbarch, block, pc);
+    case language_cplus:
+      return cplus_compute_program (inst, input, gdbarch, block, pc);
+    default:
+      gdb_assert_not_reached ("Unsupported language");
+    }
+}
+
 /* Process the compilation request.  On success it returns the object
    and source file names.  On an error condition, error () is
    called.  */
@@ -550,7 +585,8 @@  compile_to_object (struct command_line *cmd, const char *cmd_string,
 
   /* Set up instance and context for the compiler.  */
   std::unique_ptr<compile_instance> compiler
-    = current_language->get_compile_instance ();
+    = get_language_compile_context ();
+
   if (compiler == nullptr)
     error (_("No compiler support for language %s."),
 	   current_language->name ());
@@ -582,8 +618,8 @@  compile_to_object (struct command_line *cmd, const char *cmd_string,
     error (_("Neither a simple expression, or a multi-line specified."));
 
   std::string code
-    = current_language->compute_program (compiler.get (), input, gdbarch,
-					 expr_block, expr_pc);
+    = compute_program_language (compiler.get (), input, gdbarch,
+				expr_block, expr_pc);
   if (compile_debug)
     gdb_printf (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
 
diff --git a/gdb/language.c b/gdb/language.c
index a8548a2e82f..4208c23b723 100644
--- a/gdb/language.c
+++ b/gdb/language.c
@@ -677,14 +677,6 @@  language_defn::is_string_type_p (struct type *type) const
   return c_is_string_type_p (type);
 }
 
-/* See language.h.  */
-
-std::unique_ptr<compile_instance>
-language_defn::get_compile_instance () const
-{
-  return {};
-}
-
 /* The default implementation of the get_symbol_name_matcher_inner method
    from the language_defn class.  Matches with strncmp_iw.  */
 
diff --git a/gdb/language.h b/gdb/language.h
index e6bfa3c68f1..5e9599df25c 100644
--- a/gdb/language.h
+++ b/gdb/language.h
@@ -36,7 +36,6 @@  struct value_print_options;
 struct type_print_options;
 struct lang_varobj_ops;
 struct parser_state;
-class compile_instance;
 struct completion_match_for_lcd;
 class innermost_block_tracker;
 
@@ -390,37 +389,6 @@  struct language_defn
   symbol_name_matcher_ftype *get_symbol_name_matcher
 	(const lookup_name_info &lookup_name) const;
 
-  /* If this language allows compilation from the gdb command line,
-     then this method will return an instance of struct gcc_context
-     appropriate to the language.  If compilation for this language is
-     generally supported, but something goes wrong then an exception
-     is thrown.  If compilation is not supported for this language
-     then this method returns NULL.  */
-
-  virtual std::unique_ptr<compile_instance> get_compile_instance () const;
-
-  /* This method must be overridden if 'get_compile_instance' is
-     overridden.
-
-     This takes the user-supplied text and returns a new bit of code
-     to compile.
-
-     INST is the compiler instance being used.
-     INPUT is the user's input text.
-     GDBARCH is the architecture to use.
-     EXPR_BLOCK is the block in which the expression is being
-     parsed.
-     EXPR_PC is the PC at which the expression is being parsed.  */
-
-  virtual std::string compute_program (compile_instance *inst,
-				       const char *input,
-				       struct gdbarch *gdbarch,
-				       const struct block *expr_block,
-				       CORE_ADDR expr_pc) const
-  {
-    gdb_assert_not_reached ("language_defn::compute_program");
-  }
-
   /* Hash the given symbol search name.  */
   virtual unsigned int search_name_hash (const char *name) const;