From patchwork Fri Dec 27 19:27:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: JeanHeyd Meneide X-Patchwork-Id: 1215787 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-516499-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="XPXaR5F+"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="q7GH7z1u"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 47kxg90XN0z9sPK for ; Sat, 28 Dec 2019 06:27:46 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=uIN0cD0ufmng+pIOd56D7YhdYQbmfRrrmYLXd9BOP39InI /05mkZcM3+tE6+v3LMM/LLPcooC/FTa/4v2zE8z9dWOTvf1BrUdemyBQeUaQM047 pCTN+S0s6dzbLuJf4HEuI7gPbFvT2Pu4mcylRDU4y6gPu4wxORSeDdgmszh5I= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=PFKWQy8ogX0fGEdMZEgtJ3qwdG0=; b=XPXaR5F+gMLgE9WITyLp o9rnztdVyirYFVMan4W0zo4QsK8noZiyQEiRx8b41XoZAQCpywikevaqftnj6izm thO8kkzA9zlTF8hSJOoY/acJ8oPNN91BkH9F43xT83yalwoMJDWI6o2NaoK5usMS BN/VaTyZsfGtx7Eka/nFSdA= Received: (qmail 34653 invoked by alias); 27 Dec 2019 19:27:30 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 34638 invoked by uid 89); 27 Dec 2019 19:27:30 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.9 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=claims, Draft, November, november X-HELO: mail-ua1-f65.google.com Received: from mail-ua1-f65.google.com (HELO mail-ua1-f65.google.com) (209.85.222.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 27 Dec 2019 19:27:26 +0000 Received: by mail-ua1-f65.google.com with SMTP id o42so9342488uad.10; Fri, 27 Dec 2019 11:27:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=QWEO1alqxj6ithgU59jyfq1JeADUkiJC7dvNjQqOHsU=; b=q7GH7z1uVZDhCeNr1QcROgbZu9mD7OBKz/M5TZLO1k2kpK0mHk55Fdt2iyPD7obSy2 MvAxvBCcH/sqld8GCSJE/rjucEvGtgX3IztFM/MT/m10ngge90QMwpLkvYeSKXEGk7CU nOgGVxc5Tm9+8iaSvjaOUEdCHbu1XiVxFjbCaJUTE/7D70qs/fA291fDiuviM5HBgrfK y+Lhh2chw/fR7rwpSPvGQ83m+gN6M222rWsVulsFolyLR5iG1nZQw5To4Igor411dE6K ILPqih8RBSLtJyqNJQ6F6MNsAoOffF9iOhnYz+7l+uWWIBlYt2U9UXkHNbpdcjLeWqpg cVKg== MIME-Version: 1.0 From: JeanHeyd Meneide Date: Fri, 27 Dec 2019 14:27:12 -0500 Message-ID: Subject: [ PATCH ] [ C++ ] [ libstdc++ ] P1208r6 Merge source_location To: gcc-patches@gcc.gnu.org, "libstdc++" , Jonathan Wakely , Jakub Jelinek X-IsSubscribed: yes This patch implements std::source_location. There's a couple cases where the builtin implemented does not do what is expected of it, and so the bottom 3 batches of test cases fails. I'm still including the patch so that others can pick up on what might need to change about the __builtin_source_location implementation (CC: Jonathan Wakely, Jakub Jelinek). 2019-12-27 JeanHeyd "ThePhD" Meneide * include/Makefile.in: add source_location header * include/bits/c++config: Add new detection macros for LINE, COLUMN, and SOURCE_LOCATION builtins. * include/std/source_location: New. * testuite/std/support/srcloc/std.n4842.C (new): test source_location * testuite/std/support/srcloc/std.n4842.h (new): test source_location diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am index 6300de9e96d..fc593c7c461 100644 --- a/libstdc++-v3/include/Makefile.am +++ b/libstdc++-v3/include/Makefile.am @@ -70,6 +70,7 @@ std_headers = \ ${std_srcdir}/scoped_allocator \ ${std_srcdir}/set \ ${std_srcdir}/shared_mutex \ + ${std_srcdir}/source_location \ ${std_srcdir}/span \ ${std_srcdir}/sstream \ ${std_srcdir}/stack \ diff --git a/libstdc++-v3/include/Makefile.in b/libstdc++-v3/include/Makefile.in index ae4a493ea65..c104ffb28ab 100644 --- a/libstdc++-v3/include/Makefile.in +++ b/libstdc++-v3/include/Makefile.in @@ -414,6 +414,7 @@ std_headers = \ ${std_srcdir}/scoped_allocator \ ${std_srcdir}/set \ ${std_srcdir}/shared_mutex \ + ${std_srcdir}/source_location \ ${std_srcdir}/span \ ${std_srcdir}/sstream \ ${std_srcdir}/stack \ diff --git a/libstdc++-v3/include/bits/c++config b/libstdc++-v3/include/bits/c++config index 7ccfc5f199d..2d2ec96a925 100644 --- a/libstdc++-v3/include/bits/c++config +++ b/libstdc++-v3/include/bits/c++config @@ -630,6 +630,7 @@ namespace std #if __GNUC__ >= 7 // Assume these are available if the compiler claims to be a recent GCC: +# define _GLIBCXX_HAVE_BUILTIN_LINE 1 # define _GLIBCXX_HAVE_BUILTIN_HAS_UNIQ_OBJ_REP 1 # define _GLIBCXX_HAVE_BUILTIN_IS_AGGREGATE 1 # define _GLIBCXX_HAVE_BUILTIN_LAUNDER 1 @@ -637,6 +638,9 @@ namespace std # if __GNUC__ >= 9 # define _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED 1 # endif +# if __GNUC__ >= 10 +# define _GLIBCXX_HAVE_BUILTIN_SOURCE_LOCATION 1 +# endif #elif defined(__is_identifier) && defined(__has_builtin) // For non-GNU compilers: # if ! __is_identifier(__has_unique_object_representations) @@ -654,6 +658,15 @@ namespace std # if ! __is_identifier(__is_same) # define _GLIBCXX_BUILTIN_IS_SAME_AS(T, U) __is_same(T, U) # endif +# if __has_builtin(__builtin_source_location) +# define _GLIBCXX_HAVE_BUILTIN_SOURCE_LOCATION 1 +# endif +# if __has_builtin(__builtin_LINE) +# define _GLIBCXX_HAVE_BUILTIN_LINE 1 +# endif +# if __has_builtin(__builtin_COLUMN) +# define _GLIBCXX_HAVE_BUILTIN_COLUMN 1 +# endif #endif // GCC // PSTL configuration diff --git a/libstdc++-v3/include/std/source_location b/libstdc++-v3/include/std/source_location new file mode 100644 index 00000000000..3bf219efa81 --- /dev/null +++ b/libstdc++-v3/include/std/source_location @@ -0,0 +1,172 @@ +// Component for retrieving function, line and column source info. -*- C++ -*- + +// Copyright (C) 2019 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 source_location + * This is a Standard C++ Library header. + */ + +// +// P1208 source_location library +// Contributed by ThePhD with <3 +// + +#ifndef _GLIBCXX_SOURCE_LOCATION +#define _GLIBCXX_SOURCE_LOCATION 1 + +#pragma GCC system_header + +#if __cplusplus > 201703L + +#include +#include + +namespace std _GLIBCXX_VISIBILITY(default) +{ +_GLIBCXX_BEGIN_NAMESPACE_VERSION + +#define __cpp_lib_source_location 201907L + +// use by-pointer for GCC-style __builtin_source_location; +// use by-value for Clang-style builtins. +#ifdef _GLIBCXX_HAVE_BUILTIN_SOURCE_LOCATION + + struct source_location + { + private: + struct __impl + { + const char *_M_file_name; + const char *_M_function_name; + unsigned int _M_line, _M_column; + }; + + public: + static consteval source_location + current (const void *__src_loc_impl = + __builtin_source_location ()) noexcept + { + source_location __ret; + __ret._M_data = static_cast(__src_loc_impl); + return __ret; + } + + constexpr + source_location () noexcept + : _M_data () + { } + + constexpr const char * + file_name () const noexcept + { return _M_data ? _M_data->_M_file_name : ""; } + + constexpr const char * + function_name () const noexcept + { return _M_data ? _M_data->_M_function_name : ""; } + + constexpr uint_least32_t + line () const noexcept + { return _M_data ? _M_data->_M_line : 0; } + + constexpr uint_least32_t + column () const noexcept + { return _M_data ? _M_data->_M_column : 0; } + + private: + const __impl *_M_data; + }; + +#elif defined(_GLIBCXX_HAVE_BUILTIN_LINE) + + struct source_location + { + private: + struct __impl + { + const char *_M_file_name; + const char *_M_function_name; + uint_least32_t _M_line, _M_column; + }; + + public: + static consteval source_location + current (uint_least32_t __line = __builtin_LINE (), +#ifdef _GLIBCXX_HAVE_BUILTIN_COLUMN + uint_least32_t __column = __builtin_COLUMN (), +#else + uint_least32_t __column = 0, +#endif // __builtin_COLUMN from Clang 9+ + const char* __file = __builtin_FILE (), + const char* __function = __builtin_FUNCTION ()) noexcept + { + return source_location (__line, __column, __file, __function); + } + + constexpr + source_location () noexcept + : _M_data() + { + _M_data._M_file_name = ""; + _M_data._M_function_name = ""; + _M_data._M_line = 0; + _M_data._M_column = 0; + } + + constexpr const char * + file_name () const + { return _M_data._M_file_name; } + + constexpr const char * + function_name () const + { return _M_data._M_function_name; } + + constexpr uint_least32_t + line () const + { return _M_data._M_line; } + + constexpr uint_least32_t + column () const + { return _M_data._M_column; } + + private: + constexpr + source_location (uint_least32_t __line, uint_least32_t __column, + const char* __file, const char* __function) noexcept + : _M_data () + { + this->_M_data._M_file_name = __file; + this->_M_data._M_function_name = __function; + this->_M_data._M_line = __line; + this->_M_data._M_column = __column; + } + + const __impl _M_data; + }; + +#endif // __builtin_source_location vs. __builtin_LINE + __builtin_COLUMN + + +_GLIBCXX_END_NAMESPACE_VERSION +} // namespace std +#endif // C++20 +#endif // _GLIBCXX_SOURCE_LOCATION diff --git a/libstdc++-v3/include/std/version b/libstdc++-v3/include/std/version index e55a092eb89..4357d7de71c 100644 --- a/libstdc++-v3/include/std/version +++ b/libstdc++-v3/include/std/version @@ -196,6 +196,11 @@ #endif #define __cpp_lib_to_array 201907L #endif +#if defined(_GLIBCXX_HAVE_BUILTIN_SOURCE_LOCATION) +# define __cpp_lib_is_constant_evaluated 201907L +#elif defined(_GLIBCXX_HAVE_BUILTIN_LINE) && defined(_GLIBCXX_HAVE_BUILTIN_COLUMN) +# define __cpp_lib_is_constant_evaluated 201907L +#endif #endif // C++2a #endif // C++17 #endif // C++14 diff --git a/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.C b/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.C new file mode 100644 index 00000000000..b3b6e5c5125 --- /dev/null +++ b/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.C @@ -0,0 +1,137 @@ +// Copyright (C) 2019 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 +// . + +// Example from C++ Standard Working Draft N4842, November 2019 Mailing +// Adapted for testing. + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include +#include + +struct s { + std::source_location member = std::source_location::current(); + int other_member = 1; + + constexpr s(std::source_location loc = std::source_location::current()) + : member(loc) // values of member refer to calling function + { } + + constexpr s(int blather) : // values of member refer to this location + other_member(blather) + { } +}; + +constexpr std::source_location +f(std::source_location a = std::source_location::current()) +{ return a; } + +constexpr std::source_location +g() +{ + std::source_location c = std::source_location::current(); + return f(c); +} + +#include "std.n4842.h" +#include +int main () +{ + constexpr std::source_location main_sl = std::source_location::current(); + constexpr std::source_location f_arg_sl = f(main_sl); + constexpr std::source_location g_sl = g(); + constexpr std::source_location f_sl = f(); + constexpr std::source_location h_sl = h(); + constexpr s member_main_sl(main_sl); + constexpr s member_defaulted_sl(1); + constexpr s member_sl = s{}; + + using namespace std::string_view_literals; + + constexpr std::string_view main_sl_fn_name(main_sl.function_name()); + constexpr std::string_view main_sl_fi_name(main_sl.file_name()); + static_assert(main_sl.line() == 55); + // closing paren of call + static_assert(main_sl.column() == 74); + static_assert(main_sl_fn_name.ends_with("main"sv)); + static_assert(main_sl_fi_name.ends_with("std.n4842.C"sv)); + + constexpr std::string_view f_arg_sl_fn_name(f_arg_sl.function_name()); + constexpr std::string_view f_arg_sl_fi_name(f_arg_sl.file_name()); + static_assert(f_arg_sl.line() == 55); + // closing paren of call + static_assert(f_arg_sl.column() == 74); + static_assert(f_arg_sl_fn_name.ends_with("main"sv)); + static_assert(f_arg_sl_fi_name.ends_with("std.n4842.C"sv)); + + constexpr std::string_view g_sl_fn_name(g_sl.function_name()); + constexpr std::string_view g_sl_fi_name(g_sl.file_name()); + static_assert(g_sl.line() == 47); + static_assert(g_sl.column() == 58); // closing paren of call + static_assert(g_sl_fn_name.ends_with("g"sv)); + static_assert(g_sl_fi_name.ends_with("std.n4842.C"sv)); + + constexpr std::string_view h_sl_fn_name(h_sl.function_name()); + constexpr std::string_view h_sl_fi_name(h_sl.file_name()); + static_assert(h_sl.line() == 23); + static_assert(h_sl.column() == 58); // closing paren of call + static_assert(h_sl_fn_name.ends_with("h"sv)); + static_assert(h_sl_fi_name.ends_with("std.n4842.h"sv)); + + constexpr std::string_view member_main_sl_fn_name(member_main_sl.member.function_name()); + constexpr std::string_view member_main_sl_fi_name(member_main_sl.member.file_name()); + static_assert(member_main_sl.member.line() == 55); + static_assert(member_main_sl.member.column() == 74); + static_assert(member_main_sl_fn_name.ends_with("main"sv)); + static_assert(member_main_sl_fi_name.ends_with("std.n4842.C"sv)); + + // FIXME: Jakub's patch does not + // properly get the name of the calling function + // when used as a default argument, + // nor does it follow the example in the std + // FIXME: All 3 cases below are busted + constexpr std::string_view member_defaulted_sl_fn_name( + member_defaulted_sl.member.function_name()); + constexpr std::string_view member_defaulted_sl_fi_name( + member_defaulted_sl.member.file_name()); + static_assert(member_defaulted_sl.member.line() == 59); + // closing brace/paren of constructor + static_assert(member_defaulted_sl.member.column() == 29); + static_assert(member_defaulted_sl_fn_name.ends_with("main"sv)); + static_assert(member_defaulted_sl_fi_name.ends_with("std.n4842.C"sv)); + + constexpr std::string_view member_sl_fn_name( + member_sl.member.function_name()); + constexpr std::string_view member_sl_fi_name( + member_sl.member.file_name()); + static_assert(member_sl.member.line() == 59); + // closing brace/paren of constructor + static_assert(member_sl.member.column() == 29); + static_assert(member_sl_fn_name.ends_with("main"sv)); + static_assert(member_sl_fi_name.ends_with("std.n4842.C"sv)); + + constexpr std::string_view f_sl_fn_name(f_sl.function_name()); + constexpr std::string_view f_sl_fi_name(f_sl.file_name()); + static_assert(f_sl.line() == 58); + // closing paren of call + static_assert(f_sl.column() == 43); + static_assert(f_sl_fn_name.ends_with("main"sv)); + static_assert(f_sl_fi_name.ends_with("std.n4842.C"sv)); + + return 0; +} diff --git a/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.h b/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.h new file mode 100644 index 00000000000..50ff5c41bea --- /dev/null +++ b/libstdc++-v3/testsuite/std/support/srcloc/std.n4842.h @@ -0,0 +1,25 @@ +// Copyright (C) 2019 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 +// . + +#include + +constexpr std::source_location +h () +{ + std::source_location a = std::source_location::current(); + return a; +}