[gtk+/wip/otte/shader: 43/176] gskslpreprocessor: Redo error emission



commit 60c3588f7579c2486497db78ec6c258c9e37dea0
Author: Benjamin Otte <otte redhat com>
Date:   Wed Sep 27 16:09:33 2017 +0200

    gskslpreprocessor: Redo error emission
    
    Instead of an error function, we now have a macro that calls
    gsk_sl_preprocessor_emit_error().

 gsk/gskslexpression.c          |    8 ++--
 gsk/gskslpreprocessor.c        |   81 +++++++++------------------------------
 gsk/gskslpreprocessorprivate.h |   28 ++++++++++---
 3 files changed, 44 insertions(+), 73 deletions(-)
---
diff --git a/gsk/gskslexpression.c b/gsk/gskslexpression.c
index 6506f16..16eddb8 100644
--- a/gsk/gskslexpression.c
+++ b/gsk/gskslexpression.c
@@ -1023,7 +1023,7 @@ gsk_sl_expression_parse_constructor_call (GskSlScope        *scope,
   GskSlExpressionFunctionCall *call;
   const GskSlToken *token;
   GskSlType **types;
-  GError *error = NULL;
+  GError *err = NULL;
   gboolean fail = FALSE;
   guint i;
 
@@ -1068,10 +1068,10 @@ gsk_sl_expression_parse_constructor_call (GskSlScope        *scope,
   types = g_newa (GskSlType *, call->n_arguments);
   for (i = 0; i < call->n_arguments; i++)
     types[i] = gsk_sl_expression_get_return_type (call->arguments[i]);
-  if (!gsk_sl_function_matches (call->function, types, call->n_arguments, &error))
+  if (!gsk_sl_function_matches (call->function, types, call->n_arguments, &err))
     {
-      gsk_sl_preprocessor_error (stream, "%s", error->message);
-      g_clear_error (&error);
+      gsk_sl_preprocessor_error (stream, "%s", err->message);
+      g_clear_error (&err);
       fail = TRUE;
     }
 
diff --git a/gsk/gskslpreprocessor.c b/gsk/gskslpreprocessor.c
index ee5af2a..5fcad44 100644
--- a/gsk/gskslpreprocessor.c
+++ b/gsk/gskslpreprocessor.c
@@ -51,10 +51,7 @@ gsk_sl_preprocessor_error_func (GskSlTokenizer        *parser,
                                 const GError          *error,
                                 gpointer               user_data)
 {
-  g_printerr ("%3zu:%2zu: error: %3u: %s: %s\n",
-              location->lines + 1, location->line_bytes,
-              token->type, gsk_sl_token_to_string (token),
-              error->message);
+  gsk_sl_preprocessor_emit_error (user_data, TRUE, location, error);
 }
 
 static void
@@ -76,9 +73,9 @@ gsk_sl_preprocessor_new (GskSlCompiler *compiler,
   preproc->ref_count = 1;
   preproc->compiler = g_object_ref (compiler);
   preproc->tokenizer = gsk_sl_tokenizer_new (source, 
-                                            gsk_sl_preprocessor_error_func,
-                                            preproc,
-                                            NULL);
+                                             gsk_sl_preprocessor_error_func,
+                                             preproc,
+                                             NULL);
   preproc->tokens = g_array_new (FALSE, FALSE, sizeof (GskSlPpToken));
   g_array_set_clear_func (preproc->tokens, gsk_sl_preprocessor_clear_token);
   preproc->defines = gsk_sl_compiler_copy_defines (compiler);
@@ -195,13 +192,13 @@ gsk_sl_preprocessor_handle_preprocessor_directive (GskSlPreprocessor *preproc)
           gsk_sl_preprocessor_clear_token (&pp);
           if (gsk_sl_preprocessor_next_token (preproc, &pp, &was_newline))
             {
-              gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "No variable after 
#define.");
+              gsk_sl_preprocessor_error_full (preproc, &pp.location, "No variable after #define.");
               gsk_sl_preprocessor_handle_token (preproc, &pp, was_newline);
               return;
             }
           if (!gsk_sl_token_is (&pp.token, GSK_SL_TOKEN_IDENTIFIER))
             {
-              gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "Expected identifier after 
#define.");
+              gsk_sl_preprocessor_error_full (preproc, &pp.location, "Expected identifier after #define.");
               gsk_sl_preprocessor_clear_token (&pp);
             }
           else
@@ -209,7 +206,7 @@ gsk_sl_preprocessor_handle_preprocessor_directive (GskSlPreprocessor *preproc)
               GskSlDefine *define;
 
               if (g_hash_table_lookup (preproc->defines, pp.token.str))
-                gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "\"%s\" redefined.", 
pp.token.str);
+                gsk_sl_preprocessor_error_full (preproc, &pp.location, "\"%s\" redefined.", pp.token.str);
               
               define = gsk_sl_define_new (pp.token.str, NULL);
               gsk_sl_preprocessor_clear_token (&pp);
@@ -261,13 +258,13 @@ gsk_sl_preprocessor_handle_preprocessor_directive (GskSlPreprocessor *preproc)
           gsk_sl_preprocessor_clear_token (&pp);
           if (gsk_sl_preprocessor_next_token (preproc, &pp, &was_newline))
             {
-              gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "No variable after #undef.");
+              gsk_sl_preprocessor_error_full (preproc, &pp.location, "No variable after #undef.");
               gsk_sl_preprocessor_handle_token (preproc, &pp, was_newline);
               return;
             }
           if (!gsk_sl_token_is (&pp.token, GSK_SL_TOKEN_IDENTIFIER))
             {
-              gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "Expected identifier after 
#undef.");
+              gsk_sl_preprocessor_error_full (preproc, &pp.location, "Expected identifier after #undef.");
               gsk_sl_preprocessor_clear_token (&pp);
             }
           else
@@ -276,7 +273,7 @@ gsk_sl_preprocessor_handle_preprocessor_directive (GskSlPreprocessor *preproc)
               
               if (!gsk_sl_preprocessor_next_token (preproc, &pp, &was_newline))
                 {
-                  gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "Expected newline after 
#undef.");
+                  gsk_sl_preprocessor_error_full (preproc, &pp.location, "Expected newline after #undef.");
                   gsk_sl_preprocessor_clear_token (&pp);
                 }
               else
@@ -293,13 +290,13 @@ gsk_sl_preprocessor_handle_preprocessor_directive (GskSlPreprocessor *preproc)
 #endif
       else
         {
-          gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "Unknown preprocessor directive 
#%s.", pp.token.str);
+          gsk_sl_preprocessor_error_full (preproc, &pp.location, "Unknown preprocessor directive #%s.", 
pp.token.str);
           gsk_sl_preprocessor_clear_token (&pp);
         }
     }
   else
     {
-      gsk_sl_preprocessor_error_full (preproc, &pp.location, &pp.token, "Missing identifier for preprocessor 
directive.");
+      gsk_sl_preprocessor_error_full (preproc, &pp.location, "Missing identifier for preprocessor 
directive.");
       gsk_sl_preprocessor_clear_token (&pp);
     }
   
@@ -373,54 +370,14 @@ gsk_sl_preprocessor_consume (GskSlPreprocessor *preproc,
 }
 
 void
-gsk_sl_preprocessor_error_full (GskSlPreprocessor     *preproc,
+gsk_sl_preprocessor_emit_error (GskSlPreprocessor     *preproc,
+                                gboolean               fatal,
                                 const GskCodeLocation *location,
-                                const GskSlToken      *token,
-                                const char            *format,
-                                ...)
+                                const GError          *error)
 {
-  GError *error;
-  va_list args;
-
-  va_start (args, format);
-  error = g_error_new_valist (G_FILE_ERROR,
-                              G_FILE_ERROR_FAILED,
-                              format,
-                              args);
-  va_end (args);
-
-  gsk_sl_preprocessor_error_func (preproc->tokenizer,
-                                  TRUE,
-                                  location,
-                                  token,
-                                  error,
-                                  NULL);
-
-  g_error_free (error);
+  g_printerr ("%3zu:%2zu: %s: %s\n",
+              location->lines + 1, location->line_bytes,
+              fatal ? "error" : "warn",
+              error->message);
 }
-                                
-void
-gsk_sl_preprocessor_error (GskSlPreprocessor *preproc,
-                           const char        *format,
-                           ...)
-{
-  GError *error;
-  va_list args;
-
-  va_start (args, format);
-  error = g_error_new_valist (G_FILE_ERROR,
-                              G_FILE_ERROR_FAILED,
-                              format,
-                              args);
-  va_end (args);
 
-  gsk_sl_preprocessor_ensure (preproc);
-  gsk_sl_preprocessor_error_func (preproc->tokenizer,
-                                  TRUE,
-                                  gsk_sl_preprocessor_get_location (preproc),
-                                  gsk_sl_preprocessor_get (preproc),
-                                  error,
-                                  NULL);
-
-  g_error_free (error);
-}
diff --git a/gsk/gskslpreprocessorprivate.h b/gsk/gskslpreprocessorprivate.h
index b188b23..9ac1265 100644
--- a/gsk/gskslpreprocessorprivate.h
+++ b/gsk/gskslpreprocessorprivate.h
@@ -37,14 +37,28 @@ const GskCodeLocation * gsk_sl_preprocessor_get_location        (GskSlPreprocess
 void                    gsk_sl_preprocessor_consume             (GskSlPreprocessor   *preproc,
                                                                  gpointer             consumer);
 
-void                    gsk_sl_preprocessor_error               (GskSlPreprocessor   *preproc,
-                                                                 const char          *format,
-                                                                 ...) G_GNUC_PRINTF(2, 3);
-void                    gsk_sl_preprocessor_error_full          (GskSlPreprocessor   *preproc,
+void                    gsk_sl_preprocessor_emit_error          (GskSlPreprocessor   *preproc,
+                                                                 gboolean             fatal,
                                                                  const GskCodeLocation *location,
-                                                                 const GskSlToken    *token,
-                                                                 const char          *format,
-                                                                 ...) G_GNUC_PRINTF(4, 5);
+                                                                 const GError        *error);
+
+#define gsk_sl_preprocessor_error(preproc, ...) \
+  gsk_sl_preprocessor_error_full (preproc, gsk_sl_preprocessor_get_location (preproc), __VA_ARGS__)
+
+#define gsk_sl_preprocessor_error_full(preproc, location, ...) G_STMT_START{\
+  GError *error; \
+\
+  error = g_error_new (G_FILE_ERROR, \
+                       G_FILE_ERROR_FAILED, \
+                       __VA_ARGS__); \
+\
+  gsk_sl_preprocessor_emit_error (preproc, \
+                                  TRUE, \
+                                  location, \
+                                  error); \
+\
+  g_error_free (error);\
+}G_STMT_END
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]