From: Jonathan Wakely Date: Tue, 16 May 2017 13:37:52 +0000 (+0100) Subject: Implement std::experimental::source_location (N4519) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=2673bae9d22a343c2df4cb635b818956b95ff1cf;p=gcc.git 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. From-SVN: r248110 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 8926b933fc8..5cc498b6cb7 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,13 @@ 2017-05-16 Jonathan Wakely + * 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. diff --git a/libstdc++-v3/doc/html/manual/status.html b/libstdc++-v3/doc/html/manual/status.html index 4d293cbe156..c3b6201462a 100644 --- a/libstdc++-v3/doc/html/manual/status.html +++ b/libstdc++-v3/doc/html/manual/status.html @@ -824,11 +824,11 @@ Feature-testing recommendations for C++. N4502 - Support for the C++ Detection Idiom, V2 YLibrary Fundamentals 2 TS + Support for the C++ Detection Idiom, V2 YLibrary Fundamentals 2 TS N4519 - Source-Code Information Capture NLibrary Fundamentals 2 TS + Source-Code Information Capture YLibrary Fundamentals 2 TS N4521 diff --git a/libstdc++-v3/doc/xml/manual/status_cxx2017.xml b/libstdc++-v3/doc/xml/manual/status_cxx2017.xml index 0e35f757be6..a2082380043 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++. - N4519 Source-Code Information Capture - N + Y Library Fundamentals 2 TS diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index 3703bd1d3d9..a6517365827 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/Makefile.in b/libstdc++-v3/include/Makefile.in index ae1481f0553..783c647087f 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -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 index 00000000000..b06d8b651ae --- /dev/null +++ b/libstdc++-v3/include/experimental/source_location @@ -0,0 +1,86 @@ +// -*- 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 +// . + +/** @file experimental/source_location + * This is a TS C++ Library header. + */ + +#ifndef _GLIBCXX_EXPERIMENTAL_SRCLOC +#define _GLIBCXX_EXPERIMENTAL_SRCLOC 1 + +#include + +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 00000000000..2634ab465a1 --- /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 +// . + +// { dg-do run { target c++14 } } + +#include +#include +#include + +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::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); + static_assert(is_same::value, ""); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +}