[gcc(refs/vendors/ARM/heads/morello)] libstdc++: iostream operator{<<|>>}(__intcap) enablement

Matthew Malcomson matmal01@gcc.gnu.org
Fri Oct 21 13:28:35 GMT 2022


https://gcc.gnu.org/g:977fc61dbcf2124725f9fee2a67cac6df2ff4ebb

commit 977fc61dbcf2124725f9fee2a67cac6df2ff4ebb
Author: Victor Do Nascimento <Victor.DoNascimento@arm.com>
Date:   Fri Oct 21 14:27:24 2022 +0100

    libstdc++: iostream operator{<<|>>}(__intcap) enablement
    
    Add overloads of basic_ostream::operator<< and
    basic_istream::operator<< for the signed and unsigned variants of the
    CHERI __intcap type.
    
    libstdc++-v3/Changelog:
        * include/std/ostream (basic_ostream): define operator<<
        overload for signed and unsigned __intptr.
        * include/std/istream (basic_istream): define operator>>
        overload for signed and unsigned __intptr.
        * testsuite/27_io/basic_ostream/inserters_arithmetic/char/cheri_types.cc: New.

Diff:
---
 libstdc++-v3/include/std/istream                   | 20 ++++++++
 libstdc++-v3/include/std/ostream                   | 11 +++++
 .../inserters_arithmetic/char/cheri_types.cc       | 53 ++++++++++++++++++++++
 3 files changed, 84 insertions(+)

diff --git a/libstdc++-v3/include/std/istream b/libstdc++-v3/include/std/istream
index 20a455a0ef1..9db2bc91b9a 100644
--- a/libstdc++-v3/include/std/istream
+++ b/libstdc++-v3/include/std/istream
@@ -190,6 +190,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator>>(unsigned long& __n)
       { return _M_extract(__n); }
 
+#ifdef __CHERI__
+      __istream_type&
+      operator>>(__intcap& __n)
+      {
+	long __tmp;
+	__istream_type& __istr_tmp = _M_extract(__tmp);
+	__n = __tmp;
+	return __istr_tmp;
+      }
+
+      __istream_type&
+      operator>>(unsigned __intcap& __n)
+      {
+	unsigned long __tmp;
+	__istream_type& __istr_tmp = _M_extract(__tmp);
+	__n = __tmp;
+	return __istr_tmp;
+      }
+#endif
+
 #ifdef _GLIBCXX_USE_LONG_LONG
       __istream_type&
       operator>>(long long& __n)
diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream
index 9a80adf3a5a..48a1b74d5ae 100644
--- a/libstdc++-v3/include/std/ostream
+++ b/libstdc++-v3/include/std/ostream
@@ -162,6 +162,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        *  These functions use the stream's current locale (specifically, the
        *  @c num_get facet) to perform numeric formatting.
       */
+
+#ifdef __CHERI__
+      __ostream_type&
+      operator<<(__intcap __n)
+      { return _M_insert(static_cast<long>(__n)); }
+
+      __ostream_type&
+      operator<<(unsigned __intcap __n)
+      { return _M_insert(static_cast<unsigned long>(__n)); }
+#endif
+
       __ostream_type&
       operator<<(long __n)
       { return _M_insert(__n); }
diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/cheri_types.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/cheri_types.cc
new file mode 100644
index 00000000000..a27675c8c7f
--- /dev/null
+++ b/libstdc++-v3/testsuite/27_io/basic_ostream/inserters_arithmetic/char/cheri_types.cc
@@ -0,0 +1,53 @@
+// { dg-do run { target c++11 } }
+
+// Test the CHERI intcap inserters.
+
+// Copyright (C) 1999-2022 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <iostream>
+#include <sstream>
+#include <climits>
+#include <testsuite_hooks.h>
+
+void
+test01()
+{
+  using namespace std;
+  stringstream ss;
+
+  // Make sure istream operator>> and ostream operator<< are
+  // correctly overloaded to handle signed and unsigned __intcap
+  // for CHERI.
+  intptr_t smax_out, smax_in = LONG_MAX;
+  intptr_t smin_out, smin_in = LONG_MIN;
+  uintptr_t umax_out, umax_in = ULONG_MAX;
+
+  ss << smax_in << " " << smin_in << " " << umax_in;
+  ss >> smax_out >> smin_out >> umax_out;
+
+  VERIFY( smin_in == smin_out );
+  VERIFY( smax_in == smax_out );
+  VERIFY( umax_in == umax_out );
+}
+
+int
+main()
+{
+  test01();
+  return 0;
+}


More information about the Gcc-cvs mailing list