diff mbox series

[committed] libstdc++: Support printing volatile pointers (P1147R1)

Message ID YVwO/8GxaXEVx4rO@redhat.com
State New
Headers show
Series [committed] libstdc++: Support printing volatile pointers (P1147R1) | expand

Commit Message

Jonathan Wakely Oct. 5, 2021, 8:38 a.m. UTC
To avoid needing to export a new symbol from the library (for now) the
new member function uses __attribute__((always_inline)).

libstdc++-v3/ChangeLog:

	* include/std/ostream (operator<<(const volatile void*)):
	Add new overload, as per P1147R1.
	* testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc:
	New test.

Tested powerpc64le-linux. Committed to trunk.
commit 96955a82f0e1624a20ea2c9953d76a20ea433c24
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Mon Oct 4 15:22:00 2021

    libstdc++: Support printing volatile pointers (P1147R1)
    
    To avoid needing to export a new symbol from the library (for now) the
    new member function uses __attribute__((always_inline)).
    
    libstdc++-v3/ChangeLog:
    
            * include/std/ostream (operator<<(const volatile void*)):
            Add new overload, as per P1147R1.
            * testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc:
            New test.

Comments

Daniel Krügler Oct. 5, 2021, 9:28 a.m. UTC | #1
Am Di., 5. Okt. 2021 um 10:55 Uhr schrieb Jonathan Wakely via
Libstdc++ <libstdc++@gcc.gnu.org>:
>
> To avoid needing to export a new symbol from the library (for now) the
> new member function uses __attribute__((always_inline)).
>
> libstdc++-v3/ChangeLog:
>
>         * include/std/ostream (operator<<(const volatile void*)):
>         Add new overload, as per P1147R1.
>         * testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc:
>         New test.
>
> Tested powerpc64le-linux. Committed to trunk.

I think the test is insufficient, because it will succeed on every
library implementation regardless of the new feature. Without the new
feature it will select the unexpected operator<<(bool) overload and
just print "1".

- Daniel
Jonathan Wakely Oct. 5, 2021, 3:07 p.m. UTC | #2
On Tue, 5 Oct 2021 at 10:29, Daniel Krügler wrote:
>
> Am Di., 5. Okt. 2021 um 10:55 Uhr schrieb Jonathan Wakely via
> Libstdc++ <libstdc++@gcc.gnu.org>:
> >
> > To avoid needing to export a new symbol from the library (for now) the
> > new member function uses __attribute__((always_inline)).
> >
> > libstdc++-v3/ChangeLog:
> >
> >         * include/std/ostream (operator<<(const volatile void*)):
> >         Add new overload, as per P1147R1.
> >         * testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc:
> >         New test.
> >
> > Tested powerpc64le-linux. Committed to trunk.
>
> I think the test is insufficient, because it will succeed on every
> library implementation regardless of the new feature. Without the new
> feature it will select the unexpected operator<<(bool) overload and
> just print "1".

Yes, that's true. I did test it locally (and the function is
ridiculously simple), and the main purpose of that test was to ensure
we don't fail to link due to the new member not being explicitly
instantiated in the library. But we might as well make the test do
something more useful.

Done by the attached patch, tested x86_64-linux, pushed to trunk.
commit 313193edfc3986c40dedce3d0b41455d0bcdbe43
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Oct 5 14:45:11 2021

    libstdc++: Improve test for printing volatile pointers
    
    libstdc++-v3/ChangeLog:
    
            * testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc:
            Check result matches non-volatile pointer.

diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc
index 1b1a9434a95..151e13d3bdd 100644
--- a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc
+++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc
@@ -1,11 +1,15 @@
 // { dg-options "-std=gnu++23 -fno-inline" }
-// { dg-do link { target c++23 } }
+// { dg-do run { target c++23 } }
 
-#include <iostream>
+#include <sstream>
+#include <testsuite_hooks.h>
 
 int main()
 {
   int i = 0;
-  volatile void* p = &i;
-  std::cout << p << std::endl;
+  volatile void* vp = &i;
+  std::ostringstream s1, s2;
+  s1 << &i;
+  s2 << vp;
+  VERIFY( s1.str() == s2.str() );
 }
diff mbox series

Patch

diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream
index ddb33feb12f..7d39c5706d5 100644
--- a/libstdc++-v3/include/std/ostream
+++ b/libstdc++-v3/include/std/ostream
@@ -251,6 +251,13 @@  _GLIBCXX_BEGIN_NAMESPACE_VERSION
       { return *this << "nullptr"; }
 #endif
 
+#if __cplusplus > 202002L
+      __attribute__((__always_inline__))
+      __ostream_type&
+      operator<<(const volatile void* __p)
+      { return _M_insert(const_cast<const void*>(__p)); }
+#endif
+
       /**
        *  @brief  Extracting from another streambuf.
        *  @param  __sb  A pointer to a streambuf
diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc
new file mode 100644
index 00000000000..1b1a9434a95
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_other/char/volatile_ptr.cc
@@ -0,0 +1,11 @@ 
+// { dg-options "-std=gnu++23 -fno-inline" }
+// { dg-do link { target c++23 } }
+
+#include <iostream>
+
+int main()
+{
+  int i = 0;
+  volatile void* p = &i;
+  std::cout << p << std::endl;
+}