Debug Mode ENH 1/4: Add pretty function

Message ID 3e8f3d58-2a0d-70f8-f468-831557658253@gmail.com
State New
Headers show
Series
  • Debug Mode ENH 1/4: Add pretty function
Related show

Commit Message

François Dumont May 7, 2018, 8:19 p.m.
Hi

     Here is the 1st patch of a series to improve Debug Mode feedback.

     This patch add the content of __PRETTY_FUNCTION__ in the debug 
assertion output.

     Tested under Linux x86_64.

     I'll commit tomorrow if not told otherwise.

     * include/debug/formatter.h (_Error_formatter::_M_function): New.
     (_Error_formatter(const char*, unsigned int)): Adapt.
     (_Error_formatter::_M_at): Rename in...
     (_Error_formatter::_S_at): ...that and adapt.
     * include/debug/macros.h (_GLIBCXX_DEBUG_VERIFY_AT_F): New.
     (_GLIBCXX_DEBUG_VERIFY_AT, _GLIBCXX_DEBUG_VERIFY): Adapt.
     * src/c++11/debug.cc (_Error_formatter::_M_error): Render _M_function
     when available.

François

Patch

diff --git a/libstdc++-v3/include/debug/formatter.h b/libstdc++-v3/include/debug/formatter.h
index cf05f0b..66b941e 100644
--- a/libstdc++-v3/include/debug/formatter.h
+++ b/libstdc++-v3/include/debug/formatter.h
@@ -560,8 +560,10 @@  namespace __gnu_debug
 #endif
 
   private:
-    _Error_formatter(const char* __file, unsigned int __line)
+    _Error_formatter(const char* __file, unsigned int __line,
+		     const char* __function)
     : _M_file(__file), _M_line(__line), _M_num_parameters(0), _M_text(0)
+    , _M_function(__function)
     { }
 
 #if !_GLIBCXX_INLINE_VERSION
@@ -576,12 +578,13 @@  namespace __gnu_debug
     _Parameter		_M_parameters[__max_parameters];
     unsigned int	_M_num_parameters;
     const char*		_M_text;
+    const char*		_M_function;
 
   public:
     static _Error_formatter&
-    _M_at(const char* __file, unsigned int __line)
+    _S_at(const char* __file, unsigned int __line, const char* __function)
     {
-      static _Error_formatter __formatter(__file, __line);
+      static _Error_formatter __formatter(__file, __line, __function);
       return __formatter;
     }
   };
diff --git a/libstdc++-v3/include/debug/macros.h b/libstdc++-v3/include/debug/macros.h
index 12eac2e..6f901bc 100644
--- a/libstdc++-v3/include/debug/macros.h
+++ b/libstdc++-v3/include/debug/macros.h
@@ -38,16 +38,20 @@ 
  * the user error and where the error is reported.
  *
  */
-#define _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,_File,_Line)	\
+#define _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond,_ErrMsg,_File,_Line,_Func)	\
   do									\
   {									\
-    if (! (_Condition))							\
-      __gnu_debug::_Error_formatter::_M_at(_File, _Line)		\
-	  ._ErrorMessage._M_error();					\
+    if (! (_Cond))							\
+      __gnu_debug::_Error_formatter::_S_at(_File, _Line, _Func)		\
+	._ErrMsg._M_error();						\
   } while (false)
 
-#define _GLIBCXX_DEBUG_VERIFY(_Condition,_ErrorMessage)			\
-  _GLIBCXX_DEBUG_VERIFY_AT(_Condition,_ErrorMessage,__FILE__,__LINE__)
+#define _GLIBCXX_DEBUG_VERIFY_AT(_Cond,_ErrMsg,_File,_Line)		\
+  _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond,_ErrMsg,_File,_Line,__PRETTY_FUNCTION__)
+
+#define _GLIBCXX_DEBUG_VERIFY(_Cond,_ErrMsg)				\
+  _GLIBCXX_DEBUG_VERIFY_AT_F(_Cond, _ErrMsg, __FILE__, __LINE__,	\
+			     __PRETTY_FUNCTION__)
 
 // Verify that [_First, _Last) forms a valid iterator range.
 #define __glibcxx_check_valid_range(_First,_Last)			\
diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index 8ce3dcc..3a1558b 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -928,9 +928,9 @@  namespace
 	    continue;
 	  }
 
-	if (*start != '%')
+	if (!num_parameters || *start != '%')
 	  {
-	    // Normal char.
+	    // Normal char or no parameter to look for.
 	    buf[bufindex++] = *start++;
 	    continue;
 	  }
@@ -1039,6 +1039,15 @@  namespace __gnu_debug
     if (ctx._M_max_length)
       ctx._M_wordwrap = true;
 
+    if (_M_function)
+      {
+	print_literal(ctx, "In function:\n");
+	print_string(ctx, _M_function, nullptr, 0);
+	print_literal(ctx, "\n");
+	ctx._M_first_line = true;
+	print_literal(ctx, "\n");
+      }
+
     print_literal(ctx, "Error: ");
 
     // Print the error message