Don't pass empty options to GCC
[deliverable/binutils-gdb.git] / gdb / compile / compile.c
index 3e45e762624f577ac404a7f41adbac4700f97493..5c5d51782c902e05a0bd33412089bfa7b290f9c2 100644 (file)
@@ -1,6 +1,6 @@
 /* General Compile and inject code
 
-   Copyright (C) 2014-2020 Free Software Foundation, Inc.
+   Copyright (C) 2014-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -600,8 +600,14 @@ static gdb_argv
 get_args (const compile_instance *compiler, struct gdbarch *gdbarch)
 {
   const char *cs_producer_options;
+  gdb_argv result;
 
-  gdb_argv result (gdbarch_gcc_target_options (gdbarch).c_str ());
+  std::string gcc_options = gdbarch_gcc_target_options (gdbarch);
+
+  /* Make sure we have a non-empty set of options, otherwise GCC will
+     error out trying to look for a filename that is an empty string.  */
+  if (!gcc_options.empty ())
+    result = gdb_argv (gcc_options.c_str ());
 
   cs_producer_options = get_selected_pc_producer_options ();
   if (cs_producer_options != NULL)
@@ -649,8 +655,8 @@ compile_to_object (struct command_line *cmd, const char *cmd_string,
   expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
 
   /* Set up instance and context for the compiler.  */
-  std::unique_ptr <compile_instance> compiler
-                       (current_language->get_compile_instance ());
+  std::unique_ptr<compile_instance> compiler
+    = current_language->get_compile_instance ();
   if (compiler == nullptr)
     error (_("No compiler support for language %s."),
           current_language->name ());
@@ -703,7 +709,9 @@ compile_to_object (struct command_line *cmd, const char *cmd_string,
       const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
 
       /* Allow triplets with or without vendor set.  */
-      triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
+      triplet_rx = std::string (arch_rx) + "(-[^-]*)?-";
+      if (os_rx != nullptr)
+       triplet_rx += os_rx;
       compiler->set_triplet_regexp (triplet_rx.c_str ());
     }
 
@@ -712,9 +720,8 @@ compile_to_object (struct command_line *cmd, const char *cmd_string,
   int argc = argv_holder.count ();
   char **argv = argv_holder.get ();
 
-  gdb::unique_xmalloc_ptr<char> error_message;
-  error_message.reset (compiler->set_arguments (argc, argv,
-                                               triplet_rx.c_str ()));
+  gdb::unique_xmalloc_ptr<char> error_message
+    = compiler->set_arguments (argc, argv, triplet_rx.c_str ());
 
   if (error_message != NULL)
     error ("%s", error_message.get ());
@@ -880,13 +887,14 @@ compile_instance::set_triplet_regexp (const char *regexp)
 
 /* See compile-internal.h.  */
 
-char *
+gdb::unique_xmalloc_ptr<char>
 compile_instance::set_arguments (int argc, char **argv, const char *regexp)
 {
   if (version () >= GCC_FE_VERSION_1)
-    return FORWARD (set_arguments, argc, argv);
+    return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments, argc, argv));
   else
-    return FORWARD (set_arguments_v0, regexp, argc, argv);
+    return gdb::unique_xmalloc_ptr<char> (FORWARD (set_arguments_v0, regexp,
+                                                  argc, argv));
 }
 
 /* See compile-internal.h.  */
This page took 0.038913 seconds and 4 git commands to generate.