Implement std::experimental::source_location (N4519)
authorJonathan Wakely <jwakely@redhat.com>
Tue, 16 May 2017 13:37:52 +0000 (14:37 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 16 May 2017 13:37:52 +0000 (14:37 +0100)
* configure: Regenerate.
* doc/xml/manual/status_cxx2017.xml: Update status table.
* doc/html/*: Regenerate.
* include/Makefile.am: Add new header.
* include/Makefile.in: Regenerate.
* include/experimental/source_location: New header implementing N4519.
* testsuite/experimental/source_location/1.cc: New test.

From-SVN: r248110

libstdc++-v3/ChangeLog
libstdc++-v3/doc/html/manual/status.html
libstdc++-v3/doc/xml/manual/status_cxx2017.xml
libstdc++-v3/include/Makefile.am
libstdc++-v3/include/Makefile.in
libstdc++-v3/include/experimental/source_location [new file with mode: 0644]
libstdc++-v3/testsuite/experimental/source_location/1.cc [new file with mode: 0644]

index 8926b933fc860933ded9cb9dbb147020bbce8423..5cc498b6cb7a4b0fbeb3762ba2587fa0c9586753 100644 (file)
@@ -1,5 +1,13 @@
 2017-05-16  Jonathan Wakely  <jwakely@redhat.com>
 
+       * configure: Regenerate.
+       * doc/xml/manual/status_cxx2017.xml: Update status table.
+       * doc/html/*: Regenerate.
+       * include/Makefile.am: Add new header.
+       * include/Makefile.in: Regenerate.
+       * include/experimental/source_location: New header implementing N4519.
+       * testsuite/experimental/source_location/1.cc: New test.
+
        PR libstdc++/80285
        * include/bits/shared_ptr_base.h [!__cpp_rtti] (type_info): Declare
        outside versioned namespace.
index 4d293cbe156f49755fb9340ad7a1b49f443695c1..c3b6201462aeb6f530de607f753eef67ee55a12b 100644 (file)
@@ -824,11 +824,11 @@ Feature-testing recommendations for C++</a>.
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf" target="_top">
          N4502
        </a>
-      </td><td align="left"> Support for the C++ Detection Idiom, V2 </td><td align="left">Y</td><td align="left">Library Fundamentals 2 TS</td></tr><tr bgcolor="#C8B0B0"><td align="left">
+      </td><td align="left"> Support for the C++ Detection Idiom, V2 </td><td align="left">Y</td><td align="left">Library Fundamentals 2 TS</td></tr><tr><td align="left">
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4519.pdf" target="_top">
          N4519
        </a>
-      </td><td align="left"> Source-Code Information Capture </td><td align="left">N</td><td align="left">Library Fundamentals 2 TS</td></tr><tr bgcolor="#C8B0B0"><td align="left">
+      </td><td align="left"> Source-Code Information Capture </td><td align="left">Y</td><td align="left">Library Fundamentals 2 TS</td></tr><tr bgcolor="#C8B0B0"><td align="left">
        <a class="link" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4521.html" target="_top">
          N4521
        </a>
index 0e35f757be623b0812168a5594725e8f86bdff3f..a20823800433366e2846c8dac373a5d033886792 100644 (file)
@@ -878,14 +878,13 @@ Feature-testing recommendations for C++</link>.
     </row>
 
     <row>
-      <?dbhtml bgcolor="#C8B0B0" ?>
       <entry>
        <link xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4519.pdf">
          N4519
        </link>
       </entry>
       <entry> Source-Code Information Capture </entry>
-      <entry>N</entry>
+      <entry>Y</entry>
       <entry>Library Fundamentals 2 TS</entry>
     </row>
 
index 3703bd1d3d9b68dcfc76b16c9700400d3bc851d8..a65173658271bb67d85bc4ae79caca04164dd820 100644 (file)
@@ -679,6 +679,7 @@ experimental_headers = \
        ${experimental_srcdir}/ratio \
        ${experimental_srcdir}/regex \
        ${experimental_srcdir}/set \
+       ${experimental_srcdir}/source_location \
        ${experimental_srcdir}/string \
        ${experimental_srcdir}/string_view \
        ${experimental_srcdir}/system_error \
index ae1481f05534130a9b31dbfacd6a50c61a8d9844..783c647087fb5b2c049d336e94ecf15f622afe94 100644 (file)
@@ -971,6 +971,7 @@ experimental_headers = \
        ${experimental_srcdir}/ratio \
        ${experimental_srcdir}/regex \
        ${experimental_srcdir}/set \
+       ${experimental_srcdir}/source_location \
        ${experimental_srcdir}/string \
        ${experimental_srcdir}/string_view \
        ${experimental_srcdir}/system_error \
diff --git a/libstdc++-v3/include/experimental/source_location b/libstdc++-v3/include/experimental/source_location
new file mode 100644 (file)
index 0000000..b06d8b6
--- /dev/null
@@ -0,0 +1,86 @@
+// <experimental/source_location> -*- C++ -*-
+
+// Copyright (C) 2015 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.
+
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
+
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
+
+/** @file experimental/source_location
+ *  This is a TS C++ Library header.
+ */
+
+#ifndef _GLIBCXX_EXPERIMENTAL_SRCLOC
+#define _GLIBCXX_EXPERIMENTAL_SRCLOC 1
+
+#include <cstdint>
+
+namespace std {
+namespace experimental {
+inline namespace fundamentals_v2 {
+_GLIBCXX_BEGIN_NAMESPACE_VERSION
+
+#define __cpp_lib_experimental_source_location 201505
+
+  struct source_location
+  {
+#ifndef _GLIBCXX_USE_C99_STDINT_TR1
+  private:
+    using uint_least32_t = unsigned;
+  public:
+#endif
+
+    // 14.1.2, source_location creation
+    static constexpr source_location
+    current(const char* __file = __builtin_FILE(),
+           const char* __func = __builtin_FUNCTION(),
+           int __line = __builtin_LINE(),
+           int __col = 0) noexcept
+    {
+      source_location __loc;
+      __loc._M_file = __file;
+      __loc._M_func = __func;
+      __loc._M_line = __line;
+      __loc._M_col = __col;
+      return __loc;
+    }
+
+    constexpr source_location() noexcept
+    : _M_file("unknown"), _M_func(_M_file), _M_line(0), _M_col(0)
+    { }
+
+    // 14.1.3, source_location field access
+    constexpr uint_least32_t line() const noexcept { return _M_line; }
+    constexpr uint_least32_t column() const noexcept { return _M_col; }
+    constexpr const char* file_name() const noexcept { return _M_file; }
+    constexpr const char* function_name() const noexcept { return _M_func; }
+
+  private:
+    const char* _M_file;
+    const char* _M_func;
+    uint_least32_t _M_line;
+    uint_least32_t _M_col;
+  };
+
+_GLIBCXX_END_NAMESPACE_VERSION
+} // namespace fundamentals_v2
+} // namespace experimental
+} // namespace std
+
+#endif
diff --git a/libstdc++-v3/testsuite/experimental/source_location/1.cc b/libstdc++-v3/testsuite/experimental/source_location/1.cc
new file mode 100644 (file)
index 0000000..2634ab4
--- /dev/null
@@ -0,0 +1,117 @@
+// Copyright (C) 2017 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/>.
+
+// { dg-do run { target c++14 } }
+
+#include <experimental/source_location>
+#include <experimental/string_view>
+#include <testsuite_hooks.h>
+
+using std::experimental::source_location;
+using std::experimental::string_view;
+
+void
+test01()
+{
+  constexpr source_location loc = source_location::current();
+  static_assert( loc.line() == 30 );
+  // static_assert( loc.column() == 35 );
+  VERIFY( loc.file_name() == __FILE__ );
+  VERIFY( loc.function_name() == string_view(__FUNCTION__) );
+}
+
+struct S {
+  string_view func;
+  source_location loc = source_location::current();
+
+  S(source_location loc = source_location::current())
+  : func(__FUNCTION__), loc(loc) // values of loc will be from call-site
+  {}
+
+  S(int)
+  : func(__FUNCTION__) // values of loc should be hereabouts
+  {}
+};
+
+void test02()
+{
+  S s0;
+  VERIFY( s0.loc.line() == 52 );
+  // static_assert( s0.loc.column() == 7 );
+  VERIFY( s0.loc.file_name() == __FILE__ );
+  VERIFY( s0.loc.function_name() == string_view(__FUNCTION__) );
+
+  S s1(1);
+  VERIFY( s1.loc.line() != 58 );
+  VERIFY( s1.loc.file_name() == __FILE__ );
+  // VERIFY( s1.loc.function_name() == s1.func );
+}
+
+source_location f(source_location a = source_location::current()) {
+  return a;
+}
+
+source_location g(string_view& func) {
+  source_location a = source_location::current();
+  func = __FUNCTION__;
+  return a;
+}
+
+void test03()
+{
+  auto loc = f(); // f's first argument corresponds to this line of code
+  VERIFY( loc.line() == 76 );
+  // static_assert( loc.column() == 16 );
+  VERIFY( loc.file_name() == __FILE__ );
+  VERIFY( loc.function_name() == string_view(__FUNCTION__) );
+
+  source_location c = source_location::current();
+  loc = f(c); // f's first argument gets the same values as c, above
+  VERIFY( loc.line() == 82 );
+  // static_assert( loc.column() == 23 );
+  VERIFY( loc.file_name() == __FILE__ );
+  VERIFY( loc.function_name() == string_view(__FUNCTION__) );
+
+  string_view func;
+  loc = g(func);
+  VERIFY( loc.line() == 69 );
+  // static_assert( loc.column() == 23 );
+  VERIFY( loc.file_name() == __FILE__ );
+  VERIFY( loc.function_name() == func );
+}
+
+void
+test04()
+{
+  using std::is_same;
+  using std::uint_least32_t;
+  auto loc = source_location::current();
+  static_assert(is_same<decltype(loc), source_location>::value, "");
+  static_assert(is_same<decltype(loc.line()), uint_least32_t>::value, "");
+  static_assert(is_same<decltype(loc.column()), uint_least32_t>::value, "");
+  static_assert(is_same<decltype(loc.file_name()), const char*>::value, "");
+  static_assert(is_same<decltype(loc.function_name()), const char*>::value, "");
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+}