gdbsupport: include cstdlib in common-defs.h
authorSimon Marchi <simon.marchi@polymtl.ca>
Mon, 27 Apr 2020 13:19:48 +0000 (09:19 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 27 Apr 2020 13:28:03 +0000 (09:28 -0400)
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/ChangeLog
gdbsupport/common-defs.h

index 78fbbe65c70e1d655ba4c29ce53cfcb52c93317c..b071049ae73b975e6b1168872476513d1f67432b 100644 (file)
@@ -1,3 +1,7 @@
+2020-04-27  Simon Marchi  <simon.marchi@polymtl.ca>
+
+       * common-defs.h: Include cstdlib.h.
+
 2020-04-20  Tom Tromey  <tromey@adacore.com>
 
        * scoped_mmap.h (scoped_mmap): Mark move constructor as noexcept.
index e42d2b80c045361cfa19f52cb146ae32c58f61b3..d3f5eafa4555fb014d2f9140127a6fb846d6574c 100644 (file)
 
 #include <stdarg.h>
 #include <stdio.h>
+
+/* Include both cstdlib and stdlib.h to ensure we have standard functions
+   defined both in the std:: namespace and in the global namespace.  */
+#include <cstdlib>
 #include <stdlib.h>
+
 #include <stddef.h>
 #include <stdint.h>
 #include <string.h>
This page took 0.026634 seconds and 4 git commands to generate.