From 37338bd9e94806fc474023c5685a0fff3ee490b7 Mon Sep 17 00:00:00 2001
From: Martin Stjernholm <mast@lysator.liu.se>
Date: Fri, 14 Nov 2003 01:15:06 +0100
Subject: [PATCH] Cleanups in the compile exception handling.

Rev: src/cpp.c:1.125
Rev: src/cpp.h:1.8
Rev: src/language.yacc:1.326
Rev: src/preprocessor.h:1.60
---
 src/cpp.c          | 89 +++++++++++++++++++---------------------------
 src/cpp.h          |  9 +++--
 src/language.yacc  | 27 +++-----------
 src/preprocessor.h | 14 ++------
 4 files changed, 50 insertions(+), 89 deletions(-)

diff --git a/src/cpp.c b/src/cpp.c
index 66ca38475c..65302cdb47 100644
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: cpp.c,v 1.124 2003/09/30 15:55:42 grubba Exp $
+|| $Id: cpp.c,v 1.125 2003/11/14 00:15:06 mast Exp $
 */
 
 #include "global.h"
@@ -114,7 +114,7 @@ struct cpp
 struct define *defined_macro =0;
 struct define *constant_macro =0;
 
-void cpp_error(struct cpp *this,char *err)
+void cpp_error(struct cpp *this, const char *err)
 {
   this->compile_errors++;
   if(this->compile_errors > 10) return;
@@ -135,45 +135,48 @@ void cpp_error(struct cpp *this,char *err)
   }
 }
 
-void cpp_error_sprintf(struct cpp *this, char *fmt, ...)  ATTRIBUTE((format(printf,2,3)))
+void cpp_error_vsprintf (struct cpp *this, const char *fmt, va_list args)
 {
-  va_list args;
   char buf[8192];
+  VSNPRINTF (buf, sizeof (buf), fmt, args);
+  cpp_error(this, buf);
+}
 
+void cpp_error_sprintf(struct cpp *this, const char *fmt, ...)
+  ATTRIBUTE((format(printf,2,3)))
+{
+  va_list args;
   va_start(args,fmt);
-  VSNPRINTF (buf, sizeof (buf), fmt, args);
+  cpp_error_vsprintf (this, fmt, args);
   va_end(args);
-
-  cpp_error(this, buf);
 }
 
-void cpp_describe_exception(struct cpp *this, struct svalue *thrown)
+void cpp_handle_exception(struct cpp *this, const char *cpp_error_fmt, ...)
+  ATTRIBUTE((format(printf,2,3)))
 {
-  /* FIXME: Doesn't handle wide string error messages. */
-  struct pike_string *s = 0;
-
-  if ((thrown->type == T_ARRAY) && thrown->u.array->size &&
-      (thrown->u.array->item[0].type == T_STRING)) {
-    /* Old-style backtrace */
-    s = thrown->u.array->item[0].u.string;
-  } else if (thrown->type == T_OBJECT) {
-    struct generic_error_struct *ge;
-    if ((ge = (struct generic_error_struct *)
-	 get_storage(thrown->u.object, generic_error_program))) {
-      s = ge->desc;
-    }
+  struct svalue thrown;
+  move_svalue (&thrown, &throw_value);
+  throw_value.type = T_INT;
+
+  if (cpp_error_fmt) {
+    va_list args;
+    va_start (args, cpp_error_fmt);
+    cpp_error_vsprintf (this, cpp_error_fmt, args);
+    va_end (args);
   }
 
-  if (s && !s->size_shift) {
-    extern void f_string_trim_all_whites(INT32 args);
-    ref_push_string(s);
-    f_string_trim_all_whites(1);
-    push_constant_text("\n");
-    push_constant_text(" ");
-    f_replace(3);
-    cpp_error(this, sp[-1].u.string->str);
-    pop_stack();
+  push_svalue(&thrown);
+  low_safe_apply_handler("compile_exception", error_handler, compat_handler, 1);
+
+  if (SAFE_IS_ZERO(sp-1)) {
+    /* FIXME: Doesn't handle wide string error messages. */
+    struct pike_string *s = format_exception_for_error_msg (&thrown);
+    if (!s->size_shift) cpp_error (this, s->str);
+    free_string (s);
   }
+
+  pop_stack();
+  free_svalue(&thrown);
 }
 
 /*! @class MasterObject
@@ -1363,25 +1366,15 @@ static void check_constant(struct cpp *this,
 	  res = 0;
 	}
 	else {
-	  struct svalue thrown = throw_value;
-	  throw_value.type = T_INT;
-
 	  if (!data.shift) {
 	    char *str = malloc(dlen + 1);
 	    MEMCPY(str, data.ptr, dlen);
 	    str[dlen] = 0;
-	    cpp_error_sprintf(this, "Error resolving '%s'.", str);
+	    cpp_handle_exception (this, "Error resolving '%s'.", str);
 	    free(str);
 	  }
 	  else
-	    cpp_error(this, "Error resolving identifier.");
-
-	  push_svalue(&thrown);
-	  low_safe_apply_handler("compile_exception", this->handler,
-				 this->compat_handler, 1);
-	  if (SAFE_IS_ZERO(sp-1)) cpp_describe_exception(this, &thrown);
-	  pop_stack();
-	  free_svalue(&thrown);
+	    cpp_handle_exception (this, "Error resolving identifier.");
 	  res = 0;
 	}
       }
@@ -1404,17 +1397,7 @@ static void check_constant(struct cpp *this,
 			   BIT_MAPPING|BIT_OBJECT|BIT_PROGRAM))
       res = !(SAFE_IS_ZERO(sp-1) && sp[-1].subtype == NUMBER_UNDEFINED);
     else {
-      struct svalue thrown = throw_value;
-      throw_value.type = T_INT;
-
-      cpp_error(this, "Error importing '.'.");
-
-      push_svalue(&thrown);
-      low_safe_apply_handler("compile_exception", this->handler,
-			     this->compat_handler, 1);
-      if (SAFE_IS_ZERO(sp-1)) cpp_describe_exception(this, &thrown);
-      pop_stack();
-      free_svalue(&thrown);
+      cpp_handle_exception (this, "Error importing '.'.");
       res = 0;
     }
   }
diff --git a/src/cpp.h b/src/cpp.h
index 0ef60452d3..a8752d5666 100644
--- a/src/cpp.h
+++ b/src/cpp.h
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: cpp.h,v 1.7 2002/10/11 01:39:30 nilsson Exp $
+|| $Id: cpp.h,v 1.8 2003/11/14 00:15:06 mast Exp $
 */
 
 #ifndef CPP_H
@@ -18,7 +18,12 @@ struct define_part;
 struct define_argument;
 struct define;
 struct cpp;
-void cpp_error(struct cpp *this,char *err);
+void cpp_error(struct cpp *this, const char *err);
+void cpp_error_vsprintf (struct cpp *this, const char *fmt, va_list args);
+void cpp_error_sprintf(struct cpp *this, const char *fmt, ...)
+  ATTRIBUTE((format(printf,2,3)));
+void cpp_handle_exception(struct cpp *this, const char *cpp_error_fmt, ...)
+  ATTRIBUTE((format(printf,2,3)));
 void PUSH_STRING(char *str,
 		 INT32 len,
 		 dynamic_buffer *buf);
diff --git a/src/language.yacc b/src/language.yacc
index 3170bd72e7..736714f6eb 100644
--- a/src/language.yacc
+++ b/src/language.yacc
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: language.yacc,v 1.325 2003/11/07 21:29:47 mast Exp $
+|| $Id: language.yacc,v 1.326 2003/11/14 00:15:06 mast Exp $
 */
 
 %pure_parser
@@ -113,7 +113,7 @@
 /* This is the grammar definition of Pike. */
 
 #include "global.h"
-RCSID("$Id: language.yacc,v 1.325 2003/11/07 21:29:47 mast Exp $");
+RCSID("$Id: language.yacc,v 1.326 2003/11/14 00:15:06 mast Exp $");
 #ifdef HAVE_MEMORY_H
 #include <memory.h>
 #endif
@@ -4094,17 +4094,8 @@ static void safe_inc_enum(void)
   STACK_LEVEL_START(1);
 
   if (SETJMP_SP(recovery, 1)) {
-    struct svalue s;
-    assign_svalue_no_free(&s, &throw_value);
-
-    yyerror("Bad implicit enum value (failed to add 1).");
-
-    push_svalue(&s);
-    low_safe_apply_handler("compile_exception", error_handler, compat_handler, 1);
-    if (SAFE_IS_ZERO(Pike_sp-1)) yy_describe_exception(&s);
-    pop_stack();
+    handle_compile_exception ("Bad implicit enum value (failed to add 1).");
     push_int(0);
-    free_svalue(&s);
   } else {
     push_int(1);
     f_add(2);
@@ -4137,16 +4128,8 @@ static int call_handle_import(struct pike_string *s)
       else
 	yyerror("Couldn't find module to import");
     }
-  else {
-    struct svalue thrown = throw_value;
-    throw_value.type = T_INT;
-    my_yyerror("Error finding module to import");
-    push_svalue(&thrown);
-    low_safe_apply_handler("compile_exception", error_handler, compat_handler, 1);
-    if (SAFE_IS_ZERO(Pike_sp-1)) yy_describe_exception(&thrown);
-    pop_stack();
-    free_svalue(&thrown);
-  }
+  else
+    handle_compile_exception ("Error finding module to import");
 
   return 0;
 }
diff --git a/src/preprocessor.h b/src/preprocessor.h
index 5027944794..3db20e0666 100644
--- a/src/preprocessor.h
+++ b/src/preprocessor.h
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: preprocessor.h,v 1.59 2003/07/08 13:25:45 grubba Exp $
+|| $Id: preprocessor.h,v 1.60 2003/11/14 00:15:06 mast Exp $
 */
 
 /*
@@ -801,17 +801,7 @@ static ptrdiff_t calc(struct cpp *this, WCHAR *data, ptrdiff_t len,
 
   if (SETJMP(recovery))
   {
-    struct svalue thrown = throw_value;
-    throw_value.type = T_INT;
-
-    cpp_error(this, "Error evaluating expression.");
-
-    push_svalue(&thrown);
-    low_safe_apply_handler("compile_exception", error_handler, compat_handler, 1);
-    if (SAFE_IS_ZERO(sp-1)) cpp_describe_exception(this, &thrown);
-    pop_stack();
-    free_svalue(&thrown);
-
+    cpp_handle_exception (this, "Error evaluating expression.");
     pos=tmp;
     FIND_EOL();
     push_int(0);
-- 
GitLab