[1/3] libstdc++: Improve output of default contract violation handler [PR107792]

Message ID 20221220104916.3000540-1-arsen@aarsen.me
State Accepted
Headers
Series [1/3] libstdc++: Improve output of default contract violation handler [PR107792] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Arsen Arsenović Dec. 20, 2022, 10:49 a.m. UTC
  From: Jonathan Wakely <jwakely@redhat.com>

Make the output more readable. Don't output anything unless verbose
termination is enabled at configure-time.

libstdc++-v3/ChangeLog:

	PR libstdc++/107792
	PR libstdc++/107778
	* src/experimental/contract.cc (handle_contract_violation): Make
	output more readable.
---
Alright, updated to omit information that's defaulted.

Turns out your suggestion for dg-output was quite clever - because {} is
special in ARE, it's always prefixed by \ when escaping with curlies,
and so it never counds towards the "paired curly braces" rules, and
requires no extra escaping code at all!  That's quite a bit neater.

 libstdc++-v3/src/experimental/contract.cc | 50 ++++++++++++++++++-----
 1 file changed, 39 insertions(+), 11 deletions(-)
  

Comments

Jason Merrill Dec. 20, 2022, 5:23 p.m. UTC | #1
On 12/20/22 05:49, Arsen Arsenović wrote:
> From: Jonathan Wakely <jwakely@redhat.com>
> 
> Make the output more readable. Don't output anything unless verbose
> termination is enabled at configure-time.
> 
> libstdc++-v3/ChangeLog:
> 
> 	PR libstdc++/107792
> 	PR libstdc++/107778
> 	* src/experimental/contract.cc (handle_contract_violation): Make
> 	output more readable.
> ---
> Alright, updated to omit information that's defaulted.
> 
> Turns out your suggestion for dg-output was quite clever - because {} is
> special in ARE, it's always prefixed by \ when escaping with curlies,
> and so it never counds towards the "paired curly braces" rules, and
> requires no extra escaping code at all!  That's quite a bit neater.
> 
>   libstdc++-v3/src/experimental/contract.cc | 50 ++++++++++++++++++-----
>   1 file changed, 39 insertions(+), 11 deletions(-)
> 
> diff --git a/libstdc++-v3/src/experimental/contract.cc b/libstdc++-v3/src/experimental/contract.cc
> index c8d2697eddc..fbe8815a5c2 100644
> --- a/libstdc++-v3/src/experimental/contract.cc
> +++ b/libstdc++-v3/src/experimental/contract.cc
> @@ -1,4 +1,5 @@
>   // -*- C++ -*- std::experimental::contract_violation and friends
> +
>   // Copyright (C) 2019-2022 Free Software Foundation, Inc.
>   //
>   // This file is part of GCC.
> @@ -23,19 +24,46 @@
>   // <http://www.gnu.org/licenses/>.
>   
>   #include <experimental/contract>
> -#include <iostream>
> +#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
> +# include <iostream>
> +#endif
>   
>   __attribute__ ((weak)) void
>   handle_contract_violation (const std::experimental::contract_violation &violation)
>   {
> -  std::cerr << "default std::handle_contract_violation called: \n"
> -    << " " << violation.file_name()
> -    << " " << violation.line_number()
> -    << " " << violation.function_name()
> -    << " " << violation.comment()
> -    << " " << violation.assertion_level()
> -    << " " << violation.assertion_role()
> -    << " " << (int)violation.continuation_mode()
> -    << std::endl;
> +#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
> +  bool level_default_p = violation.assertion_level() == "default";
> +  bool role_default_p = violation.assertion_role() == "default";
> +  bool cont_mode_default_p = violation.continuation_mode()
> +    == std::experimental::contract_violation_continuation_mode::never_continue;
> +
> +  const char* modes[]{ "never", "maybe" }; // Must match enumerators in header.
> +  std::cerr << "contract violation in function " << violation.function_name()
> +    << " at " << violation.file_name() << ':' << violation.line_number()
> +    << ": " << violation.comment();
> +
> +  const char* delimiter = "\n[";
> +
> +  if (!level_default_p)
> +    {
> +      std::cerr << delimiter << "level:" << violation.assertion_level();
> +      delimiter = ", ";
> +    }
> +  if (!role_default_p)
> +    {
> +      std::cerr << delimiter << "role:" << violation.assertion_role();
> +      delimiter = ", ";
> +    }
> +  if (!cont_mode_default_p)
> +    {
> +      std::cerr << delimiter << "mode:"
> +		<< modes[(int)violation.continuation_mode() & 1];

I still think this should be "continuation: on/off".

Jason
  

Patch

diff --git a/libstdc++-v3/src/experimental/contract.cc b/libstdc++-v3/src/experimental/contract.cc
index c8d2697eddc..fbe8815a5c2 100644
--- a/libstdc++-v3/src/experimental/contract.cc
+++ b/libstdc++-v3/src/experimental/contract.cc
@@ -1,4 +1,5 @@ 
 // -*- C++ -*- std::experimental::contract_violation and friends
+
 // Copyright (C) 2019-2022 Free Software Foundation, Inc.
 //
 // This file is part of GCC.
@@ -23,19 +24,46 @@ 
 // <http://www.gnu.org/licenses/>.
 
 #include <experimental/contract>
-#include <iostream>
+#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
+# include <iostream>
+#endif
 
 __attribute__ ((weak)) void
 handle_contract_violation (const std::experimental::contract_violation &violation)
 {
-  std::cerr << "default std::handle_contract_violation called: \n"
-    << " " << violation.file_name()
-    << " " << violation.line_number()
-    << " " << violation.function_name()
-    << " " << violation.comment()
-    << " " << violation.assertion_level()
-    << " " << violation.assertion_role()
-    << " " << (int)violation.continuation_mode()
-    << std::endl;
+#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
+  bool level_default_p = violation.assertion_level() == "default";
+  bool role_default_p = violation.assertion_role() == "default";
+  bool cont_mode_default_p = violation.continuation_mode()
+    == std::experimental::contract_violation_continuation_mode::never_continue;
+
+  const char* modes[]{ "never", "maybe" }; // Must match enumerators in header.
+  std::cerr << "contract violation in function " << violation.function_name()
+    << " at " << violation.file_name() << ':' << violation.line_number()
+    << ": " << violation.comment();
+
+  const char* delimiter = "\n[";
+
+  if (!level_default_p)
+    {
+      std::cerr << delimiter << "level:" << violation.assertion_level();
+      delimiter = ", ";
+    }
+  if (!role_default_p)
+    {
+      std::cerr << delimiter << "role:" << violation.assertion_role();
+      delimiter = ", ";
+    }
+  if (!cont_mode_default_p)
+    {
+      std::cerr << delimiter << "mode:"
+		<< modes[(int)violation.continuation_mode() & 1];
+      delimiter = ", ";
+    }
+
+  if (delimiter[0] == ',')
+    std::cerr << ']';
+
+  std::cerr << std::endl;
+#endif
 }
-