diff mbox

Implement std::experimental::source_location (N4519)

Message ID 20170516134019.GA16590@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely May 16, 2017, 1:40 p.m. UTC
This is a partial implementation of <experimental/source_location>,
see the commented-out lines in the testcase for the missing pieces.

I'll be opening a PR to get the needed front-end support complete it.

	* 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.

Tested x86_64-linux, committed to trunk.
commit ddd130b3fc040f63dd89bc87fe8b7834506105d2
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue May 16 12:16:50 2017 +0100

    Implement std::experimental::source_location (N4519)
    
    	* 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.
diff mbox

Patch

diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
index 0e35f75..a208238 100644
--- a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
+++ b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml
@@ -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>
 
diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 3703bd1..a651736 100644
--- a/libstdc++-v3/include/Makefile.am
+++ b/libstdc++-v3/include/Makefile.am
@@ -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 \
diff --git a/libstdc++-v3/include/experimental/source_location b/libstdc++-v3/include/experimental/source_location
new file mode 100644
index 0000000..b06d8b6
--- /dev/null
+++ b/libstdc++-v3/include/experimental/source_location
@@ -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
index 0000000..2634ab4
--- /dev/null
+++ b/libstdc++-v3/testsuite/experimental/source_location/1.cc
@@ -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();
+}