Fix warning on gdb/compile/compile.c (C++-ify "triplet_rx")

Message ID 20180116194641.22361-1-sergiodj@redhat.com
State New, archived
Headers

Commit Message

Sergio Durigan Junior Jan. 16, 2018, 7:46 p.m. UTC
  This fixes a GCC warning that happens when compiling
gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
20180104 (Red Hat 7.2.1-6)").

It's a simple patch that converts "triplet_rx" from "char *" to
"std::string", thus guaranteeing that it will be always initialized.

I've regtested this patch and did not find any regressions.  OK to
apply on both master and 8.1 (after creating a bug for it)?

gdb/ChangeLog:
2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>

	* compile/compile.c (compile_to_object): Convert "triplet_rx"
	to "std::string".
---
 gdb/compile/compile.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
  

Comments

Eli Zaretskii Jan. 17, 2018, 3:32 p.m. UTC | #1
> From: Sergio Durigan Junior <sergiodj@redhat.com>
> Cc: Eli Zaretskii <eliz@gnu.org>,	Sergio Durigan Junior <sergiodj@redhat.com>
> Date: Tue, 16 Jan 2018 14:46:41 -0500
> 
> This fixes a GCC warning that happens when compiling
> gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
> 20180104 (Red Hat 7.2.1-6)").
> 
> It's a simple patch that converts "triplet_rx" from "char *" to
> "std::string", thus guaranteeing that it will be always initialized.
> 
> I've regtested this patch and did not find any regressions.  OK to
> apply on both master and 8.1 (after creating a bug for it)?
> 
> gdb/ChangeLog:
> 2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>
> 
> 	* compile/compile.c (compile_to_object): Convert "triplet_rx"
> 	to "std::string".

Thanks, this fixes the warning for me.
  
Simon Marchi Jan. 17, 2018, 5:17 p.m. UTC | #2
On 2018-01-16 14:46, Sergio Durigan Junior wrote:
> This fixes a GCC warning that happens when compiling
> gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
> 20180104 (Red Hat 7.2.1-6)").
> 
> It's a simple patch that converts "triplet_rx" from "char *" to
> "std::string", thus guaranteeing that it will be always initialized.
> 
> I've regtested this patch and did not find any regressions.  OK to
> apply on both master and 8.1 (after creating a bug for it)?
> 
> gdb/ChangeLog:
> 2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>
> 
> 	* compile/compile.c (compile_to_object): Convert "triplet_rx"
> 	to "std::string".
> ---
>  gdb/compile/compile.c | 15 ++++++++-------
>  1 file changed, 8 insertions(+), 7 deletions(-)
> 
> diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
> index 2ee75930ac..47646169c8 100644
> --- a/gdb/compile/compile.c
> +++ b/gdb/compile/compile.c
> @@ -463,7 +463,7 @@ compile_to_object (struct command_line *cmd, const
> char *cmd_string,
>    char **argv;
>    int ok;
>    struct gdbarch *gdbarch = get_current_arch ();
> -  char *triplet_rx;
> +  std::string triplet_rx;
>    char *error_message;
> 
>    if (!target_has_execution)
> @@ -527,15 +527,15 @@ compile_to_object (struct command_line *cmd,
> const char *cmd_string,
>      }
>    else
>      {
> -      const char *os_rx = osabi_triplet_regexp (gdbarch_osabi 
> (gdbarch));
> -      const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
> +      std::string os_rx = osabi_triplet_regexp (gdbarch_osabi 
> (gdbarch));
> +      std::string arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);

Making these std::string makes unnecessary copies.

You can write the line below like this:

   triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;

Otherwise, LGTM.

Simon
  
Sergio Durigan Junior Jan. 17, 2018, 11:07 p.m. UTC | #3
On Wednesday, January 17 2018, Simon Marchi wrote:

> On 2018-01-16 14:46, Sergio Durigan Junior wrote:
>> This fixes a GCC warning that happens when compiling
>> gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
>> 20180104 (Red Hat 7.2.1-6)").
>>
>> It's a simple patch that converts "triplet_rx" from "char *" to
>> "std::string", thus guaranteeing that it will be always initialized.
>>
>> I've regtested this patch and did not find any regressions.  OK to
>> apply on both master and 8.1 (after creating a bug for it)?
>>
>> gdb/ChangeLog:
>> 2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>
>>
>> 	* compile/compile.c (compile_to_object): Convert "triplet_rx"
>> 	to "std::string".
>> ---
>>  gdb/compile/compile.c | 15 ++++++++-------
>>  1 file changed, 8 insertions(+), 7 deletions(-)
>>
>> diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
>> index 2ee75930ac..47646169c8 100644
>> --- a/gdb/compile/compile.c
>> +++ b/gdb/compile/compile.c
>> @@ -463,7 +463,7 @@ compile_to_object (struct command_line *cmd, const
>> char *cmd_string,
>>    char **argv;
>>    int ok;
>>    struct gdbarch *gdbarch = get_current_arch ();
>> -  char *triplet_rx;
>> +  std::string triplet_rx;
>>    char *error_message;
>>
>>    if (!target_has_execution)
>> @@ -527,15 +527,15 @@ compile_to_object (struct command_line *cmd,
>> const char *cmd_string,
>>      }
>>    else
>>      {
>> -      const char *os_rx = osabi_triplet_regexp (gdbarch_osabi
>> (gdbarch));
>> -      const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>> +      std::string os_rx = osabi_triplet_regexp (gdbarch_osabi
>> (gdbarch));
>> +      std::string arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>
> Making these std::string makes unnecessary copies.
>
> You can write the line below like this:
>
>   triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
>
> Otherwise, LGTM.

Thanks, Simon.

Pushed to master:

7d937cad0acdccd0ff485435fbe16f005e994c66

Is it OK to push this to the 8.1 branch as well?  If so, I'm not sure I
should create a bug for this, or just go ahead and push it.

Thanks,
  
Simon Marchi Jan. 17, 2018, 11:42 p.m. UTC | #4
On 2018-01-17 18:07, Sergio Durigan Junior wrote:
> On Wednesday, January 17 2018, Simon Marchi wrote:
> 
>> On 2018-01-16 14:46, Sergio Durigan Junior wrote:
>>> This fixes a GCC warning that happens when compiling
>>> gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
>>> 20180104 (Red Hat 7.2.1-6)").
>>> 
>>> It's a simple patch that converts "triplet_rx" from "char *" to
>>> "std::string", thus guaranteeing that it will be always initialized.
>>> 
>>> I've regtested this patch and did not find any regressions.  OK to
>>> apply on both master and 8.1 (after creating a bug for it)?
>>> 
>>> gdb/ChangeLog:
>>> 2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>
>>> 
>>> 	* compile/compile.c (compile_to_object): Convert "triplet_rx"
>>> 	to "std::string".
>>> ---
>>>  gdb/compile/compile.c | 15 ++++++++-------
>>>  1 file changed, 8 insertions(+), 7 deletions(-)
>>> 
>>> diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
>>> index 2ee75930ac..47646169c8 100644
>>> --- a/gdb/compile/compile.c
>>> +++ b/gdb/compile/compile.c
>>> @@ -463,7 +463,7 @@ compile_to_object (struct command_line *cmd, 
>>> const
>>> char *cmd_string,
>>>    char **argv;
>>>    int ok;
>>>    struct gdbarch *gdbarch = get_current_arch ();
>>> -  char *triplet_rx;
>>> +  std::string triplet_rx;
>>>    char *error_message;
>>> 
>>>    if (!target_has_execution)
>>> @@ -527,15 +527,15 @@ compile_to_object (struct command_line *cmd,
>>> const char *cmd_string,
>>>      }
>>>    else
>>>      {
>>> -      const char *os_rx = osabi_triplet_regexp (gdbarch_osabi
>>> (gdbarch));
>>> -      const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>>> +      std::string os_rx = osabi_triplet_regexp (gdbarch_osabi
>>> (gdbarch));
>>> +      std::string arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>> 
>> Making these std::string makes unnecessary copies.
>> 
>> You can write the line below like this:
>> 
>>   triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
>> 
>> Otherwise, LGTM.
> 
> Thanks, Simon.
> 
> Pushed to master:
> 
> 7d937cad0acdccd0ff485435fbe16f005e994c66
> 
> Is it OK to push this to the 8.1 branch as well?  If so, I'm not sure I
> should create a bug for this, or just go ahead and push it.

Yes, it is fine to go to the 8.1 branch.  A PR is not necessary right 
now.  They are only necessary after there was an initial release on the 
branch, to track what fixes were made on top of that initial version.

Simon
  
Sergio Durigan Junior Jan. 17, 2018, 11:48 p.m. UTC | #5
On Wednesday, January 17 2018, Simon Marchi wrote:

> On 2018-01-17 18:07, Sergio Durigan Junior wrote:
>> On Wednesday, January 17 2018, Simon Marchi wrote:
>>
>>> On 2018-01-16 14:46, Sergio Durigan Junior wrote:
>>>> This fixes a GCC warning that happens when compiling
>>>> gdb/compile/compile.c on some GCC versions (e.g., "gcc (GCC) 7.2.1
>>>> 20180104 (Red Hat 7.2.1-6)").
>>>>
>>>> It's a simple patch that converts "triplet_rx" from "char *" to
>>>> "std::string", thus guaranteeing that it will be always initialized.
>>>>
>>>> I've regtested this patch and did not find any regressions.  OK to
>>>> apply on both master and 8.1 (after creating a bug for it)?
>>>>
>>>> gdb/ChangeLog:
>>>> 2018-01-16  Sergio Durigan Junior  <sergiodj@redhat.com>
>>>>
>>>> 	* compile/compile.c (compile_to_object): Convert "triplet_rx"
>>>> 	to "std::string".
>>>> ---
>>>>  gdb/compile/compile.c | 15 ++++++++-------
>>>>  1 file changed, 8 insertions(+), 7 deletions(-)
>>>>
>>>> diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
>>>> index 2ee75930ac..47646169c8 100644
>>>> --- a/gdb/compile/compile.c
>>>> +++ b/gdb/compile/compile.c
>>>> @@ -463,7 +463,7 @@ compile_to_object (struct command_line *cmd,
>>>> const
>>>> char *cmd_string,
>>>>    char **argv;
>>>>    int ok;
>>>>    struct gdbarch *gdbarch = get_current_arch ();
>>>> -  char *triplet_rx;
>>>> +  std::string triplet_rx;
>>>>    char *error_message;
>>>>
>>>>    if (!target_has_execution)
>>>> @@ -527,15 +527,15 @@ compile_to_object (struct command_line *cmd,
>>>> const char *cmd_string,
>>>>      }
>>>>    else
>>>>      {
>>>> -      const char *os_rx = osabi_triplet_regexp (gdbarch_osabi
>>>> (gdbarch));
>>>> -      const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>>>> +      std::string os_rx = osabi_triplet_regexp (gdbarch_osabi
>>>> (gdbarch));
>>>> +      std::string arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
>>>
>>> Making these std::string makes unnecessary copies.
>>>
>>> You can write the line below like this:
>>>
>>>   triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
>>>
>>> Otherwise, LGTM.
>>
>> Thanks, Simon.
>>
>> Pushed to master:
>>
>> 7d937cad0acdccd0ff485435fbe16f005e994c66
>>
>> Is it OK to push this to the 8.1 branch as well?  If so, I'm not sure I
>> should create a bug for this, or just go ahead and push it.
>
> Yes, it is fine to go to the 8.1 branch.  A PR is not necessary right
> now.  They are only necessary after there was an initial release on
> the branch, to track what fixes were made on top of that initial
> version.

Awesome.  In this case, pushed to the 8.1 branch:

2a54d2158beeb2833cb3fb4da68e7c55e341159a

Thanks,
  

Patch

diff --git a/gdb/compile/compile.c b/gdb/compile/compile.c
index 2ee75930ac..47646169c8 100644
--- a/gdb/compile/compile.c
+++ b/gdb/compile/compile.c
@@ -463,7 +463,7 @@  compile_to_object (struct command_line *cmd, const char *cmd_string,
   char **argv;
   int ok;
   struct gdbarch *gdbarch = get_current_arch ();
-  char *triplet_rx;
+  std::string triplet_rx;
   char *error_message;
 
   if (!target_has_execution)
@@ -527,15 +527,15 @@  compile_to_object (struct command_line *cmd, const char *cmd_string,
     }
   else
     {
-      const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
-      const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
+      std::string os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
+      std::string arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
 
       /* Allow triplets with or without vendor set.  */
-      triplet_rx = concat (arch_rx, "(-[^-]*)?-", os_rx, (char *) NULL);
-      make_cleanup (xfree, triplet_rx);
+      triplet_rx = arch_rx + "(-[^-]*)?-" + os_rx;
 
       if (compiler->fe->ops->version >= GCC_FE_VERSION_1)
-	compiler->fe->ops->set_triplet_regexp (compiler->fe, triplet_rx);
+	compiler->fe->ops->set_triplet_regexp (compiler->fe,
+					       triplet_rx.c_str ());
     }
 
   /* Set compiler command-line arguments.  */
@@ -545,7 +545,8 @@  compile_to_object (struct command_line *cmd, const char *cmd_string,
   if (compiler->fe->ops->version >= GCC_FE_VERSION_1)
     error_message = compiler->fe->ops->set_arguments (compiler->fe, argc, argv);
   else
-    error_message = compiler->fe->ops->set_arguments_v0 (compiler->fe, triplet_rx,
+    error_message = compiler->fe->ops->set_arguments_v0 (compiler->fe,
+							 triplet_rx.c_str (),
 							 argc, argv);
   if (error_message != NULL)
     {