[2/4] libstdc++: Improve output of default contract violation handler [PR107792]

Message ID 20221210094303.2180127-3-arsen@aarsen.me
State Accepted
Headers
Series c++: Small tweaks to contracts |

Checks

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

Commit Message

Arsen Arsenović Dec. 10, 2022, 9:43 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.
---
 libstdc++-v3/src/experimental/contract.cc | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)
  

Comments

Jason Merrill Dec. 15, 2022, 4:28 p.m. UTC | #1
On 12/10/22 04:43, 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.
> ---
>   libstdc++-v3/src/experimental/contract.cc | 23 +++++++++++++----------
>   1 file changed, 13 insertions(+), 10 deletions(-)
> 
> diff --git a/libstdc++-v3/src/experimental/contract.cc b/libstdc++-v3/src/experimental/contract.cc
> index c8d2697eddc..6a7f064d35e 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,21 @@
>   // <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()
> +#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
> +  const char* modes[]{ "never", "maybe" }; // Must match enumerators in header.

I'd actually suggest "off" and "on" since it's really a boolean since 
"always_continue" was dropped.

> +  std::cerr << "contract violation in function " << violation.function_name()
> +    << " at " << violation.file_name() << ':' << violation.line_number()
> +    << ": " << violation.comment()
> +    << "\n[level:" << violation.assertion_level()

Maybe omit level/role if "default"?

> +    << ", role:" << violation.assertion_role() << ", continuation mode:"
> +    << modes[(int)violation.continuation_mode()] << ']'
>       << std::endl;
> +#endif
>   }
  
Arsen Arsenović Dec. 15, 2022, 5:43 p.m. UTC | #2
Hi,

Jason Merrill <jason@redhat.com> writes:

>
> I'd actually suggest "off" and "on" since it's really a boolean since
> "always_continue" was dropped.
>
>> +  std::cerr << "contract violation in function " << violation.function_name()
>> +    << " at " << violation.file_name() << ':' << violation.line_number()
>> +    << ": " << violation.comment()
>> +    << "\n[level:" << violation.assertion_level()
>
> Maybe omit level/role if "default"?

ACK on both, sounds good.

Have a great day.
  

Patch

diff --git a/libstdc++-v3/src/experimental/contract.cc b/libstdc++-v3/src/experimental/contract.cc
index c8d2697eddc..6a7f064d35e 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,21 @@ 
 // <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()
+#if _GLIBCXX_HOSTED && _GLIBCXX_VERBOSE
+  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()
+    << "\n[level:" << violation.assertion_level()
+    << ", role:" << violation.assertion_role() << ", continuation mode:"
+    << modes[(int)violation.continuation_mode()] << ']'
     << std::endl;
+#endif
 }
-