[gtk/matthiasc/glshader-demo: 7/9] gl backend: Add line numbers to source in glsl compilation errors




commit 461ce87b52eb16acf6e48c87cfe574f89f86bdfd
Author: Alexander Larsson <alexl redhat com>
Date:   Mon Sep 21 15:14:40 2020 +0200

    gl backend: Add line numbers to source in glsl compilation errors
    
    Almost always the source is created by combining various sources, which
    means the line numbers in the error messages are hard to use. Adding
    the line numbers to the source in the error message helps with this.

 gsk/gl/gskglshaderbuilder.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/gsk/gl/gskglshaderbuilder.c b/gsk/gl/gskglshaderbuilder.c
index b23c81a9b1..c0e763a85e 100644
--- a/gsk/gl/gskglshaderbuilder.c
+++ b/gsk/gl/gskglshaderbuilder.c
@@ -47,7 +47,9 @@ check_shader_error (int     shader_id,
   int log_len;
   char *buffer;
   int code_len;
-  char *code;
+  int line;
+  char *code, *p;
+  GString *s;
 
   glGetShaderiv (shader_id, GL_COMPILE_STATUS, &status);
 
@@ -62,11 +64,29 @@ check_shader_error (int     shader_id,
   code = g_malloc0 (code_len + 1);
   glGetShaderSource (shader_id, code_len, NULL, code);
 
+  s = g_string_new ("");
+  p = code;
+  line = 1;
+  while (*p)
+    {
+      char *end = strchr (p, '\n');
+      if (end)
+        end = end + 1; /* Include newline */
+      else
+        end = p + strlen (p);
+
+      g_string_append_printf (s, "%3d| ", line++);
+      g_string_append_len (s, p, end - p);
+
+      p = end;
+    }
+
   g_set_error (error, GDK_GL_ERROR, GDK_GL_ERROR_COMPILATION_FAILED,
                "Compilation failure in shader.\nError message: %s\n\nSource code:\n%s\n\n",
                buffer,
-               code);
+               s->str);
 
+  g_string_free (s, TRUE);
   g_free (buffer);
   g_free (code);
 


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