public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdbsupport: include cstdlib in common-defs.h
@ 2020-04-09 22:50 Simon Marchi
  2020-04-09 23:34 ` Christian Biesinger
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Marchi @ 2020-04-09 22:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Alexey Brodkin, Simon Marchi

In PR 25731 [1], the following build failure was reported:

    ../../binutils-gdb/gdb/gdbtypes.c:1254:10: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'?
                = ((std::abs (stride) * element_count) + 7) / 8;
                    ^~~~~~~~
                    abs
    /usr/include/stdlib.h:129:6: note: 'abs' declared here
    int      abs(int) __pure2;
             ^
The original report was using:

    $ gcc -v
    Apple LLVM version 8.0.0 (clang-800.0.42.1)
    Target: x86_64-apple-darwin15.6.0

Note that I was _not_ able to reproduce using:

    $ g++ --version
    Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
    Apple clang version 11.0.0 (clang-1100.0.33.17)
    Target: x86_64-apple-darwin19.3.0

The proposed fix is to include <cstdlib> in addition to <stdlib.h>.

Here's an excerpt of [2] relevant to this problem:

    These headers [speaking of the .h form] are allowed to also declare
    the same names in the std namespace, and the corresponding cxxx
    headers are allowed to also declare the same names in the global
    namespace: including <cstdlib> definitely provides std::malloc and
    may also provide ::malloc.  Including <stdlib.h> definitely provides
    ::malloc and may also provide std::malloc

Since we use std::abs, we should not assume that our include of stdlib.h
declares an `abs` function in the `std` namespace.

If we replace the include of stdlib.h with cstdlib, then we fall in the
opposite situation.  A standard C++ library may decide to only put the
declarations in the std namespace, requiring us to prefix all standard
functions with `std::`.  I'm not against that, but for the moment I think the
safest way forward is to just include both.

Note that I don't know what effect this patch can have on any stdlib.h fix
provided by gnulib.

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=25731
[2] https://en.cppreference.com/w/cpp/header#C_compatibility_headers

gdbsupport/ChangeLog:

	* common-defs.h: Include cstdlib.h.
---
 gdbsupport/common-defs.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gdbsupport/common-defs.h b/gdbsupport/common-defs.h
index e42d2b80c045..257b6279b888 100644
--- a/gdbsupport/common-defs.h
+++ b/gdbsupport/common-defs.h
@@ -84,6 +84,7 @@
 
 #include <stdarg.h>
 #include <stdio.h>
+#include <cstdlib>
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdint.h>
-- 
2.26.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gdbsupport: include cstdlib in common-defs.h
  2020-04-09 22:50 [PATCH] gdbsupport: include cstdlib in common-defs.h Simon Marchi
@ 2020-04-09 23:34 ` Christian Biesinger
  2020-04-10 14:39   ` Simon Marchi
  0 siblings, 1 reply; 5+ messages in thread
From: Christian Biesinger @ 2020-04-09 23:34 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Alexey Brodkin

On Thu, Apr 9, 2020 at 5:50 PM Simon Marchi via Gdb-patches
<gdb-patches@sourceware.org> wrote:
>
> In PR 25731 [1], the following build failure was reported:
>
>     ../../binutils-gdb/gdb/gdbtypes.c:1254:10: error: no member named 'abs' in namespace 'std'; did you mean simply 'abs'?
>                 = ((std::abs (stride) * element_count) + 7) / 8;
>                     ^~~~~~~~
>                     abs
>     /usr/include/stdlib.h:129:6: note: 'abs' declared here
>     int      abs(int) __pure2;
>              ^
> The original report was using:
>
>     $ gcc -v
>     Apple LLVM version 8.0.0 (clang-800.0.42.1)
>     Target: x86_64-apple-darwin15.6.0
>
> Note that I was _not_ able to reproduce using:
>
>     $ g++ --version
>     Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
>     Apple clang version 11.0.0 (clang-1100.0.33.17)
>     Target: x86_64-apple-darwin19.3.0
>
> The proposed fix is to include <cstdlib> in addition to <stdlib.h>.
>
> Here's an excerpt of [2] relevant to this problem:
>
>     These headers [speaking of the .h form] are allowed to also declare
>     the same names in the std namespace, and the corresponding cxxx
>     headers are allowed to also declare the same names in the global
>     namespace: including <cstdlib> definitely provides std::malloc and
>     may also provide ::malloc.  Including <stdlib.h> definitely provides
>     ::malloc and may also provide std::malloc
>
> Since we use std::abs, we should not assume that our include of stdlib.h
> declares an `abs` function in the `std` namespace.
>
> If we replace the include of stdlib.h with cstdlib, then we fall in the
> opposite situation.  A standard C++ library may decide to only put the
> declarations in the std namespace, requiring us to prefix all standard
> functions with `std::`.  I'm not against that, but for the moment I think the
> safest way forward is to just include both.
>
> Note that I don't know what effect this patch can have on any stdlib.h fix
> provided by gnulib.
>
> [1] https://sourceware.org/bugzilla/show_bug.cgi?id=25731
> [2] https://en.cppreference.com/w/cpp/header#C_compatibility_headers
>
> gdbsupport/ChangeLog:
>
>         * common-defs.h: Include cstdlib.h.
> ---
>  gdbsupport/common-defs.h | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/gdbsupport/common-defs.h b/gdbsupport/common-defs.h
> index e42d2b80c045..257b6279b888 100644
> --- a/gdbsupport/common-defs.h
> +++ b/gdbsupport/common-defs.h
> @@ -84,6 +84,7 @@
>
>  #include <stdarg.h>
>  #include <stdio.h>
> +#include <cstdlib>
>  #include <stdlib.h>
>  #include <stddef.h>
>  #include <stdint.h>

I think this is reasonable, but I would add a comment why both cstdlib
and stdlib.h are included (ie. to get the std:: version with
overloaded functions)

Christian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gdbsupport: include cstdlib in common-defs.h
  2020-04-09 23:34 ` Christian Biesinger
@ 2020-04-10 14:39   ` Simon Marchi
  2020-04-10 17:29     ` Christian Biesinger
  0 siblings, 1 reply; 5+ messages in thread
From: Simon Marchi @ 2020-04-10 14:39 UTC (permalink / raw)
  To: Christian Biesinger; +Cc: gdb-patches, Alexey Brodkin

On 2020-04-09 7:34 p.m., Christian Biesinger wrote:
> I think this is reasonable, but I would add a comment why both cstdlib
> and stdlib.h are included (ie. to get the std:: version with
> overloaded functions)

A comment would certainly be useful.  Does that sound right?

/* Include both stdlib.h and cstdlib to make sure to get the standard C
   functions in the global namespace and the overloads in the std
   namespace.  */

Simon

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gdbsupport: include cstdlib in common-defs.h
  2020-04-10 14:39   ` Simon Marchi
@ 2020-04-10 17:29     ` Christian Biesinger
  2020-04-27 13:30       ` Simon Marchi
  0 siblings, 1 reply; 5+ messages in thread
From: Christian Biesinger @ 2020-04-10 17:29 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Alexey Brodkin

On Fri, Apr 10, 2020 at 9:40 AM Simon Marchi <simon.marchi@polymtl.ca> wrote:
>
> On 2020-04-09 7:34 p.m., Christian Biesinger wrote:
> > I think this is reasonable, but I would add a comment why both cstdlib
> > and stdlib.h are included (ie. to get the std:: version with
> > overloaded functions)
>
> A comment would certainly be useful.  Does that sound right?
>
> /* Include both stdlib.h and cstdlib to make sure to get the standard C
>    functions in the global namespace and the overloads in the std
>    namespace.  */

That sounds good to me.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] gdbsupport: include cstdlib in common-defs.h
  2020-04-10 17:29     ` Christian Biesinger
@ 2020-04-27 13:30       ` Simon Marchi
  0 siblings, 0 replies; 5+ messages in thread
From: Simon Marchi @ 2020-04-27 13:30 UTC (permalink / raw)
  To: Christian Biesinger; +Cc: gdb-patches, Alexey Brodkin

On 2020-04-10 1:29 p.m., Christian Biesinger wrote:
> On Fri, Apr 10, 2020 at 9:40 AM Simon Marchi <simon.marchi@polymtl.ca> wrote:
>>
>> On 2020-04-09 7:34 p.m., Christian Biesinger wrote:
>>> I think this is reasonable, but I would add a comment why both cstdlib
>>> and stdlib.h are included (ie. to get the std:: version with
>>> overloaded functions)
>>
>> A comment would certainly be useful.  Does that sound right?
>>
>> /* Include both stdlib.h and cstdlib to make sure to get the standard C
>>    functions in the global namespace and the overloads in the std
>>    namespace.  */
> 
> That sounds good to me.
> 

I pushed this patch, although with a slightly different comment, because I forgot
I had written one here.  But it says essentially the same thing.

Simon

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-04-27 13:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-09 22:50 [PATCH] gdbsupport: include cstdlib in common-defs.h Simon Marchi
2020-04-09 23:34 ` Christian Biesinger
2020-04-10 14:39   ` Simon Marchi
2020-04-10 17:29     ` Christian Biesinger
2020-04-27 13:30       ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).