diff mbox

[v1] cxx: Make __func__, __FUNCTION__, and __PRETTY_FUNCTION__ constexpr.

Message ID 20170628125331.25540-1-snaipe@arista.com
State New
Headers show

Commit Message

Franklin “Snaipe” Mathieu June 28, 2017, 12:53 p.m. UTC
From: Snaipe <snaipe@diacritic.io>

This patch makes the forementioned definitions `contexpr` when
compiling C++11 and above with GNU extensions.

gcc/cp/ChangeLog:
2017-06-27  Franklin “Snaipe” Mathieu  <snaipe@diacritic.io>

	PR c++/66639
	* decl.c (cp_make_fname_decl): Make declaration constexpr.

gcc/testsuite/ChangeLog:
2017-06-27  Franklin “Snaipe” Mathieu  <snaipe@diacritic.io>

	PR c++/66639
	* g++.dg/pr66639.c: New test.
---
 gcc/cp/decl.c                  |  5 +++++
 gcc/testsuite/g++.dg/pr66639.C | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/pr66639.C

Comments

Franklin “Snaipe” Mathieu June 28, 2017, 12:56 p.m. UTC | #1
Sorry about that (--dry-run fail), please ignore.

On Wed, Jun 28, 2017 at 1:53 PM, Snaipe <snaipe@arista.com> wrote:
> From: Snaipe <snaipe@diacritic.io>
>
> This patch makes the forementioned definitions `contexpr` when
> compiling C++11 and above with GNU extensions.
>
> gcc/cp/ChangeLog:
> 2017-06-27  Franklin “Snaipe” Mathieu  <snaipe@diacritic.io>
>
>         PR c++/66639
>         * decl.c (cp_make_fname_decl): Make declaration constexpr.
>
> gcc/testsuite/ChangeLog:
> 2017-06-27  Franklin “Snaipe” Mathieu  <snaipe@diacritic.io>
>
>         PR c++/66639
>         * g++.dg/pr66639.c: New test.
> ---
>  gcc/cp/decl.c                  |  5 +++++
>  gcc/testsuite/g++.dg/pr66639.C | 19 +++++++++++++++++++
>  2 files changed, 24 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/pr66639.C
>
> diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
> index 8e9a466..740ab71 100644
> --- a/gcc/cp/decl.c
> +++ b/gcc/cp/decl.c
> @@ -4384,6 +4384,11 @@ cp_make_fname_decl (location_t loc, tree id, int type_dep)
>    TREE_READONLY (decl) = 1;
>    DECL_ARTIFICIAL (decl) = 1;
>
> +  /* extension: declare __func__, __FUNCTION__, and __PRETTY_FUNCTION__ as
> +     constexpr.  */
> +  if (!flag_iso && cxx_dialect >= cxx11)
> +    DECL_DECLARED_CONSTEXPR_P (decl) = 1;
> +
>    TREE_USED (decl) = 1;
>
>    if (current_function_decl)
> diff --git a/gcc/testsuite/g++.dg/pr66639.C b/gcc/testsuite/g++.dg/pr66639.C
> new file mode 100644
> index 0000000..51a92f9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/pr66639.C
> @@ -0,0 +1,19 @@
> +// PR c++/66639
> +// { dg-do compile { target c++11 } }
> +// { dg-options "" }
> +
> +constexpr bool
> +streq(char const *lhs, char const *rhs)
> +{
> +  return *lhs && *rhs
> +     ? *lhs == *rhs && streq(lhs + 1, rhs + 1)
> +     : !*lhs && !*rhs;
> +}
> +
> +int
> +main()
> +{
> +   static_assert (streq(__func__, "main"), "");
> +   static_assert (streq(__FUNCTION__, "main"), "");
> +   static_assert (streq(__PRETTY_FUNCTION__, "int main()"), "");
> +}
> --
> Franklin "Snaipe" Mathieu
> Arista Networks, Ltd
>
diff mbox

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 8e9a466..740ab71 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -4384,6 +4384,11 @@  cp_make_fname_decl (location_t loc, tree id, int type_dep)
   TREE_READONLY (decl) = 1;
   DECL_ARTIFICIAL (decl) = 1;
 
+  /* extension: declare __func__, __FUNCTION__, and __PRETTY_FUNCTION__ as
+     constexpr.  */
+  if (!flag_iso && cxx_dialect >= cxx11)
+    DECL_DECLARED_CONSTEXPR_P (decl) = 1;
+
   TREE_USED (decl) = 1;
 
   if (current_function_decl)
diff --git a/gcc/testsuite/g++.dg/pr66639.C b/gcc/testsuite/g++.dg/pr66639.C
new file mode 100644
index 0000000..51a92f9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr66639.C
@@ -0,0 +1,19 @@ 
+// PR c++/66639
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+constexpr bool
+streq(char const *lhs, char const *rhs)
+{
+  return *lhs && *rhs
+     ? *lhs == *rhs && streq(lhs + 1, rhs + 1)
+     : !*lhs && !*rhs;
+}
+
+int
+main()
+{
+   static_assert (streq(__func__, "main"), "");
+   static_assert (streq(__FUNCTION__, "main"), "");
+   static_assert (streq(__PRETTY_FUNCTION__, "int main()"), "");
+}