[Mesa-dev] [PATCH 2/2] glsl: use stringbuf in the preprocessor

Nicolai Hähnle nhaehnle at gmail.com
Sat Aug 26 14:11:48 UTC 2017


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

---
 src/compiler/glsl/glcpp/glcpp-parse.y | 99 ++++++++++++++---------------------
 src/compiler/glsl/glcpp/glcpp.h       |  7 ++-
 src/compiler/glsl/glcpp/pp.c          | 45 +++++++---------
 3 files changed, 59 insertions(+), 92 deletions(-)

diff --git a/src/compiler/glsl/glcpp/glcpp-parse.y b/src/compiler/glsl/glcpp/glcpp-parse.y
index 898a26044fa..70df98ff6e1 100644
--- a/src/compiler/glsl/glcpp/glcpp-parse.y
+++ b/src/compiler/glsl/glcpp/glcpp-parse.y
@@ -205,78 +205,72 @@ input:
 ;
 
 line:
 	control_line
 |	SPACE control_line
 |	text_line {
 		_glcpp_parser_print_expanded_token_list (parser, $1);
 		const char *newline_str = "\n";
 		size_t size = strlen(newline_str);
 
-		ralloc_str_append(&parser->output, newline_str,
-				  parser->output_length, size);
-		parser->output_length += size;
+		stringbuf_append(parser->output, newline_str, size);
 	}
 |	expanded_line
 ;
 
 expanded_line:
 	IF_EXPANDED expression NEWLINE {
 		if (parser->is_gles && $2.undefined_macro)
 			glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
 		_glcpp_parser_skip_stack_push_if (parser, & @1, $2.value);
 	}
 |	ELIF_EXPANDED expression NEWLINE {
 		if (parser->is_gles && $2.undefined_macro)
 			glcpp_error(& @1, parser, "undefined macro %s in expression (illegal in GLES)", $2.undefined_macro);
 		_glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2.value);
 	}
 |	LINE_EXPANDED integer_constant NEWLINE {
 		parser->has_new_line_number = 1;
 		parser->new_line_number = $2;
-		ralloc_asprintf_rewrite_tail (&parser->output,
-					      &parser->output_length,
-					      "#line %" PRIiMAX "\n",
-					      $2);
+		stringbuf_printf(parser->output,
+				  "#line %" PRIiMAX "\n",
+				  $2);
 	}
 |	LINE_EXPANDED integer_constant integer_constant NEWLINE {
 		parser->has_new_line_number = 1;
 		parser->new_line_number = $2;
 		parser->has_new_source_number = 1;
 		parser->new_source_number = $3;
-		ralloc_asprintf_rewrite_tail (&parser->output,
-					      &parser->output_length,
-					      "#line %" PRIiMAX " %" PRIiMAX "\n",
-					      $2, $3);
+		stringbuf_printf(parser->output,
+				  "#line %" PRIiMAX " %" PRIiMAX "\n",
+				  $2, $3);
 	}
 ;
 
 define:
 	OBJ_IDENTIFIER replacement_list NEWLINE {
 		_define_object_macro (parser, & @1, $1, $2);
 	}
 |	FUNC_IDENTIFIER '(' ')' replacement_list NEWLINE {
 		_define_function_macro (parser, & @1, $1, NULL, $4);
 	}
 |	FUNC_IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
 		_define_function_macro (parser, & @1, $1, $3, $5);
 	}
 ;
 
 control_line:
 	control_line_success {
 		const char *newline_str = "\n";
 		size_t size = strlen(newline_str);
 
-		ralloc_str_append(&parser->output, newline_str,
-				  parser->output_length, size);
-		parser->output_length += size;
+		stringbuf_append(parser->output, newline_str, size);
 	}
 |	control_line_error
 |	HASH_TOKEN LINE pp_tokens NEWLINE {
 
 		if (parser->skip_stack == NULL ||
 		    parser->skip_stack->type == SKIP_NO_SKIP)
 		{
 			_glcpp_parser_expand_and_lex_from (parser,
 							   LINE_EXPANDED, $3,
 							   EXPANSION_MODE_IGNORE_DEFINED);
@@ -452,21 +446,21 @@ control_line_success:
 |	HASH_TOKEN VERSION_TOKEN version_constant IDENTIFIER NEWLINE {
 		if (parser->version_set) {
 			glcpp_error(& @1, parser, "#version must appear on the first line");
 		}
 		_glcpp_parser_handle_version_declaration(parser, $3, $4, true);
 	}
 |	HASH_TOKEN NEWLINE {
 		glcpp_parser_resolve_implicit_version(parser);
 	}
 |	HASH_TOKEN PRAGMA NEWLINE {
-		ralloc_asprintf_rewrite_tail (&parser->output, &parser->output_length, "#%s", $2);
+		stringbuf_printf(parser->output, "#%s", $2);
 	}
 ;
 
 control_line_error:
 	HASH_TOKEN ERROR_TOKEN NEWLINE {
 		glcpp_error(& @1, parser, "#%s", $2);
 	}
 |	HASH_TOKEN DEFINE_TOKEN NEWLINE {
 		glcpp_error (& @1, parser, "#define without macro name");
 	}
@@ -1130,145 +1124,130 @@ _token_list_equal_ignoring_space(token_list_t *a, token_list_t *b)
       }
 
       node_a = node_a->next;
       node_b = node_b->next;
    }
 
    return 1;
 }
 
 static void
-_token_print(char **out, size_t *len, token_t *token)
+_token_print(struct stringbuf *sb, token_t *token)
 {
    if (token->type < 256) {
       size_t size = sizeof(char);
 
-      ralloc_str_append(out, (char *) &token->type, *len, size);
-      *len += size;
+      stringbuf_append(sb, (char *) &token->type, size);
       return;
    }
 
    switch (token->type) {
    case INTEGER:
-      ralloc_asprintf_rewrite_tail (out, len, "%" PRIiMAX, token->value.ival);
+      stringbuf_printf(sb, "%" PRIiMAX, token->value.ival);
       break;
    case IDENTIFIER:
    case INTEGER_STRING:
    case OTHER: {
       size_t size = strlen(token->value.str);
 
-      ralloc_str_append(out, token->value.str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token->value.str, size);
       break;
    }
    case SPACE: {
       const char *token_str = " ";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case LEFT_SHIFT: {
       const char *token_str = "<<";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case RIGHT_SHIFT: {
       const char *token_str = ">>";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case LESS_OR_EQUAL: {
       const char *token_str = "<=";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case GREATER_OR_EQUAL: {
       const char *token_str = ">=";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case EQUAL: {
       const char *token_str = "==";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case NOT_EQUAL: {
       const char *token_str = "!=";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case AND: {
       const char *token_str = "&&";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case OR: {
       const char *token_str = "||";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case PASTE: {
       const char *token_str = "##";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case PLUS_PLUS: {
       const char *token_str = "++";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case MINUS_MINUS: {
       const char *token_str = "--";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case DEFINED: {
       const char *token_str = "defined";
       size_t size = strlen(token_str);
 
-      ralloc_str_append(out, token_str, *len, size);
-      *len += size;
+      stringbuf_append(sb, token_str, size);
       break;
    }
    case PLACEHOLDER:
       /* Nothing to print. */
       break;
    default:
       assert(!"Error: Don't know how to print token.");
 
       break;
    }
@@ -1382,39 +1361,39 @@ _token_paste(glcpp_parser_t *parser, token_t *token, token_t *other)
       if (combined_type == INTEGER)
          combined_type = INTEGER_STRING;
 
       combined = _token_create_str (parser, combined_type, str);
       combined->location = token->location;
       return combined;
    }
 
     FAIL:
    glcpp_error (&token->location, parser, "");
-   ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "Pasting \"");
-   _token_print (&parser->info_log, &parser->info_log_length, token);
-   ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" and \"");
-   _token_print (&parser->info_log, &parser->info_log_length, other);
-   ralloc_asprintf_rewrite_tail (&parser->info_log, &parser->info_log_length, "\" does not give a valid preprocessing token.\n");
+   stringbuf_printf(parser->info_log, "Pasting \"");
+   _token_print(parser->info_log, token);
+   stringbuf_printf(parser->info_log, "\" and \"");
+   _token_print(parser->info_log, other);
+   stringbuf_printf(parser->info_log, "\" does not give a valid preprocessing token.\n");
 
    return token;
 }
 
 static void
 _token_list_print(glcpp_parser_t *parser, token_list_t *list)
 {
    token_node_t *node;
 
    if (list == NULL)
       return;
 
    for (node = list->head; node; node = node->next)
-      _token_print (&parser->output, &parser->output_length, node->token);
+      _token_print(parser->output, node->token);
 }
 
 void
 yyerror(YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
 {
    glcpp_error(locp, parser, "%s", error);
 }
 
 static void
 add_builtin_define(glcpp_parser_t *parser, const char *name, int value)
@@ -1452,24 +1431,22 @@ glcpp_parser_create(const struct gl_extensions *extension_list,
    parser->in_control_line = 0;
    parser->paren_count = 0;
    parser->commented_newlines = 0;
 
    parser->skip_stack = NULL;
    parser->skipping = 0;
 
    parser->lex_from_list = NULL;
    parser->lex_from_node = NULL;
 
-   parser->output = ralloc_strdup(parser, "");
-   parser->output_length = 0;
-   parser->info_log = ralloc_strdup(parser, "");
-   parser->info_log_length = 0;
+   parser->output = stringbuf_new(parser);
+   parser->info_log = stringbuf_new(parser);
    parser->error = 0;
 
    parser->extensions = extensions;
    parser->extension_list = extension_list;
    parser->state = state;
    parser->api = api;
    parser->version = 0;
    parser->version_set = false;
 
    parser->has_new_line_number = 0;
@@ -2446,24 +2423,24 @@ _glcpp_parser_handle_version_declaration(glcpp_parser_t *parser, intmax_t versio
          add_builtin_define(parser, "__have_builtin_builtin_sign64", 1);
          add_builtin_define(parser, "__have_builtin_builtin_umul64", 1);
          add_builtin_define(parser, "__have_builtin_builtin_udiv64", 1);
          add_builtin_define(parser, "__have_builtin_builtin_umod64", 1);
          add_builtin_define(parser, "__have_builtin_builtin_idiv64", 1);
          add_builtin_define(parser, "__have_builtin_builtin_imod64", 1);
       }
    }
 
    if (explicitly_set) {
-      ralloc_asprintf_rewrite_tail(&parser->output, &parser->output_length,
-                                   "#version %" PRIiMAX "%s%s", version,
-                                   es_identifier ? " " : "",
-                                   es_identifier ? es_identifier : "");
+      stringbuf_printf(parser->output,
+                        "#version %" PRIiMAX "%s%s", version,
+                        es_identifier ? " " : "",
+                        es_identifier ? es_identifier : "");
    }
 }
 
 /* GLSL version if no version is explicitly specified. */
 #define IMPLICIT_GLSL_VERSION 110
 
 /* GLSL ES version if no version is explicitly specified. */
 #define IMPLICIT_GLSL_ES_VERSION 100
 
 void
diff --git a/src/compiler/glsl/glcpp/glcpp.h b/src/compiler/glsl/glcpp/glcpp.h
index 2804636c30e..ed56528f5e3 100644
--- a/src/compiler/glsl/glcpp/glcpp.h
+++ b/src/compiler/glsl/glcpp/glcpp.h
@@ -23,20 +23,21 @@
 
 #ifndef GLCPP_H
 #define GLCPP_H
 
 #include <stdint.h>
 #include <stdbool.h>
 
 #include "main/mtypes.h"
 
 #include "util/ralloc.h"
+#include "util/stringbuf.h"
 
 #include "util/hash_table.h"
 
 #define yyscan_t void*
 
 /* Some data types used for parser values. */
 
 typedef struct expression_value {
 	intmax_t value;
 	char *undefined_macro;
@@ -192,24 +193,22 @@ struct glcpp_parser {
 	int last_token_was_space;
 	int first_non_space_token_this_line;
 	int newline_as_space;
 	int in_control_line;
 	int paren_count;
 	int commented_newlines;
 	skip_node_t *skip_stack;
 	int skipping;
 	token_list_t *lex_from_list;
 	token_node_t *lex_from_node;
-	char *output;
-	char *info_log;
-	size_t output_length;
-	size_t info_log_length;
+	struct stringbuf *output;
+        struct stringbuf *info_log;
 	int error;
 	glcpp_extension_iterator extensions;
 	const struct gl_extensions *extension_list;
 	void *state;
 	gl_api api;
 	unsigned version;
 
 	/**
 	 * Has the #version been set?
 	 *
diff --git a/src/compiler/glsl/glcpp/pp.c b/src/compiler/glsl/glcpp/pp.c
index 96125f2e2f1..77a68737bd9 100644
--- a/src/compiler/glsl/glcpp/pp.c
+++ b/src/compiler/glsl/glcpp/pp.c
@@ -25,55 +25,45 @@
 #include <string.h>
 #include <ctype.h>
 #include "glcpp.h"
 
 void
 glcpp_error (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
 {
 	va_list ap;
 
 	parser->error = 1;
-	ralloc_asprintf_rewrite_tail(&parser->info_log,
-				     &parser->info_log_length,
-				     "%u:%u(%u): "
-				     "preprocessor error: ",
-				     locp->source,
-				     locp->first_line,
-				     locp->first_column);
+	stringbuf_printf(parser->info_log,
+			  "%u:%u(%u): preprocessor error: ",
+			  locp->source,
+			  locp->first_line,
+			  locp->first_column);
 	va_start(ap, fmt);
-	ralloc_vasprintf_rewrite_tail(&parser->info_log,
-				      &parser->info_log_length,
-				      fmt, ap);
+	stringbuf_vprintf(parser->info_log, fmt, ap);
 	va_end(ap);
-	ralloc_asprintf_rewrite_tail(&parser->info_log,
-				     &parser->info_log_length, "\n");
+	stringbuf_printf(parser->info_log, "\n");
 }
 
 void
 glcpp_warning (YYLTYPE *locp, glcpp_parser_t *parser, const char *fmt, ...)
 {
 	va_list ap;
 
-	ralloc_asprintf_rewrite_tail(&parser->info_log,
-				     &parser->info_log_length,
-				     "%u:%u(%u): "
-				     "preprocessor warning: ",
-				     locp->source,
-				     locp->first_line,
-				     locp->first_column);
+	stringbuf_printf(parser->info_log,
+			  "%u:%u(%u): preprocessor warning: ",
+			  locp->source,
+			  locp->first_line,
+			  locp->first_column);
 	va_start(ap, fmt);
-	ralloc_vasprintf_rewrite_tail(&parser->info_log,
-				      &parser->info_log_length,
-				      fmt, ap);
+	stringbuf_vprintf(parser->info_log, fmt, ap);
 	va_end(ap);
-	ralloc_asprintf_rewrite_tail(&parser->info_log,
-				     &parser->info_log_length, "\n");
+	stringbuf_printf(parser->info_log, "\n");
 }
 
 /* Given str, (that's expected to start with a newline terminator of some
  * sort), return a pointer to the first character in str after the newline.
  *
  * A newline terminator can be any of the following sequences:
  *
  *	"\r\n"
  *	"\n\r"
  *	"\n"
@@ -231,19 +221,20 @@ glcpp_preprocess(void *ralloc_ctx, const char **shader, char **info_log,
 
 	glcpp_lex_set_source_string (parser, *shader);
 
 	glcpp_parser_parse (parser);
 
 	if (parser->skip_stack)
 		glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
 
 	glcpp_parser_resolve_implicit_version(parser);
 
-	ralloc_strcat(info_log, parser->info_log);
+	ralloc_strcat(info_log, stringbuf_build(parser->info_log));
 
-	ralloc_steal(ralloc_ctx, parser->output);
-	*shader = parser->output;
+	char *shader_out = stringbuf_build(parser->output);
+        ralloc_steal(ralloc_ctx, shader_out);
+	*shader = shader_out;
 
 	errors = parser->error;
 	glcpp_parser_destroy (parser);
 	return errors;
 }
-- 
2.11.0



More information about the mesa-dev mailing list