[Piglit] [PATCH 6/8] shader_runner: Migrate lookup_enum_string() to new parser library.

Francisco Jerez currojerez at riseup.net
Tue Oct 18 23:17:44 UTC 2016


---
 tests/shaders/parser_utils.c  | 25 ++++++++++++++
 tests/shaders/parser_utils.h  | 14 ++++++++
 tests/shaders/shader_runner.c | 78 +++++++++++++++----------------------------
 3 files changed, 66 insertions(+), 51 deletions(-)

diff --git a/tests/shaders/parser_utils.c b/tests/shaders/parser_utils.c
index 1bd9a69..d180842 100644
--- a/tests/shaders/parser_utils.c
+++ b/tests/shaders/parser_utils.c
@@ -197,6 +197,31 @@ parse_enum_gl(const char *s, GLenum *e, const char **rest)
 }
 
 bool
+parse_enum_tab(const struct string_to_enum *tab,
+	       const char *s, unsigned *e, const char **rest)
+{
+	const char *end = s;
+	bool ret = parse_word(s, &s, &end);
+	unsigned i = 0;
+
+	if (ret) {
+		for (i = 0; tab[i].name; i++) {
+			if (!strncmp(tab[i].name, s, end - s) &&
+			    !tab[i].name[end - s])
+				break;
+		}
+
+		*e = tab[i].value;
+		ret = tab[i].name;
+	}
+
+	if (rest)
+		*rest = (ret ? end : s);
+
+	return ret;
+}
+
+bool
 parse_comparison_op(const char *s, enum comparison *t, const char **rest)
 {
 	if (parse_str(s, "==", rest)) {
diff --git a/tests/shaders/parser_utils.h b/tests/shaders/parser_utils.h
index 8fbfac4..6907a69 100644
--- a/tests/shaders/parser_utils.h
+++ b/tests/shaders/parser_utils.h
@@ -170,6 +170,20 @@ parse_word_copy(const char *s, char *t, unsigned n, const char **rest);
 bool
 parse_enum_gl(const char *s, GLenum *e, const char **rest);
 
+struct string_to_enum {
+	const char *name;
+	unsigned value;
+};
+
+/**
+ * Parse a whitespace-delimited symbolic constant from the set
+ * specified in the \p tab argument pointing to a zero-terminated
+ * array of string-value pairs.
+ */
+bool
+parse_enum_tab(const struct string_to_enum *tab,
+	       const char *s, unsigned *e, const char **rest);
+
 const char *eat_whitespace(const char *src);
 const char *eat_text(const char *src);
 bool string_match(const char *string, const char *line);
diff --git a/tests/shaders/shader_runner.c b/tests/shaders/shader_runner.c
index 148371c..9aa988d 100644
--- a/tests/shaders/shader_runner.c
+++ b/tests/shaders/shader_runner.c
@@ -82,11 +82,6 @@ struct component_version {
 
 #define ENUM_STRING(e) { #e, e }
 
-struct string_to_enum {
-	const char *name;
-	GLenum token;
-};
-
 extern float piglit_tolerance[4];
 
 static int test_num = 1;
@@ -255,25 +250,6 @@ static const struct string_to_enum all_types[] = {
 	{ NULL, 0 }
 };
 
-static GLenum
-lookup_enum_string(const struct string_to_enum *table, const char **line,
-		   const char *error_desc)
-{
-	int i;
-	*line = eat_whitespace(*line);
-	for (i = 0; table[i].name; i++) {
-		size_t len = strlen(table[i].name);
-		if (strncmp(table[i].name, *line, len) == 0 &&
-		    ((*line)[len] == '\0' || isspace((*line)[len]))) {
-			*line = eat_whitespace(*line + len);
-			return table[i].token;
-		}
-	}
-	fprintf(stderr, "Bad %s at: %s\n", error_desc, *line);
-	piglit_report_result(PIGLIT_FAIL);
-	return 0;
-}
-
 static bool
 compare(float ref, float value, enum comparison cmp);
 
@@ -2043,8 +2019,7 @@ active_uniform(const char *line)
 	const char *rest = line;
 	char name[512];
 	char name_buf[512];
-	char pname_string[512];
-	GLenum pname;
+	unsigned pname;
 	int expected;
 	int i;
 	int num_active_uniforms;
@@ -2052,13 +2027,12 @@ active_uniform(const char *line)
 	REQUIRE(parse_word_copy(rest, name, sizeof(name), &rest),
 		"Bad uniform name at: %s\n", line);
 
-	strcpy_to_space(pname_string, eat_whitespace(rest));
-	pname = lookup_enum_string(all_pnames, &rest, "glGetUniformsiv pname");
+	REQUIRE(parse_enum_tab(all_pnames, rest, &pname, &rest),
+		"Bad glGetUniformsiv pname at: %s\n", line);
 
-	if (!parse_int(rest, &expected, &rest)) {
-		rest = eat_whitespace(rest);
-		expected = lookup_enum_string(all_types, &rest, "type enum");
-	}
+	REQUIRE(parse_enum_tab(all_types, rest, (unsigned *)&expected, &rest) ||
+		parse_int(rest, &expected, &rest),
+		"Bad expected value at: %s\n", line);
 
 	glGetProgramiv(prog, GL_ACTIVE_UNIFORMS, &num_active_uniforms);
 	for (i = 0; i < num_active_uniforms; i++) {
@@ -2230,19 +2204,16 @@ active_program_interface(const char *line)
 		return;
 	}
 
-	interface_type = lookup_enum_string(all_program_interface, &line,
-					    "glGetProgramResourceiv "
-					    "programInterface");
+	REQUIRE(parse_enum_tab(all_program_interface, line,
+			       &interface_type, &line),
+		"Bad program interface at: %s\n", line);
 	REQUIRE(parse_word_copy(line, name, sizeof(name), &line),
 		"Bad program resource name at: %s\n", line);
-
-	prop = lookup_enum_string(all_props, &line,
-				  "glGetProgramResourceiv pname");
-
-	if (!parse_int(line, &expected, &line)) {
-		line = eat_whitespace(line);
-		expected = lookup_enum_string(all_types, &line, "type enum");
-	}
+	REQUIRE(parse_enum_tab(all_props, line, &prop, &line),
+		"Bad glGetProgramResourceiv pname at: %s\n", line);
+	REQUIRE(parse_enum_tab(all_types, line, (unsigned *)&expected, &line) ||
+		parse_int(line, &expected, &line),
+		"Bad expected value at: %s\n", line);
 
 	glGetProgramInterfaceiv(prog, interface_type,
 				GL_ACTIVE_RESOURCES, &num_active_buffers);
@@ -2408,8 +2379,9 @@ static const struct string_to_enum enable_table[] = {
 static void
 do_enable_disable(const char *line, bool enable_flag)
 {
-	GLenum value = lookup_enum_string(enable_table, &line,
-					  "enable/disable enum");
+	GLenum value;
+	REQUIRE(parse_enum_tab(enable_table, line, &value, NULL),
+		"Bad enable/disable enum at: %s\n", line);
 
 	if (enable_flag)
 		glEnable(value);
@@ -2434,10 +2406,12 @@ static const struct string_to_enum hint_param_table[] = {
 
 static void do_hint(const char *line)
 {
-	GLenum target = lookup_enum_string(hint_target_table, &line,
-					   "hint target");
-	GLenum param = lookup_enum_string(hint_param_table, &line,
-					  "hint param");
+	unsigned target, param;
+	REQUIRE(parse_enum_tab(hint_target_table, line, &target, &line),
+		"Bad hint target at: %s\n", line);
+	REQUIRE(parse_enum_tab(hint_param_table, line, &param, &line),
+		"Bad hint param at: %s\n", line);
+
 	glHint(target, param);
 }
 
@@ -2558,7 +2532,8 @@ handle_texparameter(const char *line)
 	const struct string_to_enum *strings = NULL;
 	unsigned value;
 
-	target = lookup_enum_string(texture_target, &line, "texture target");
+	REQUIRE(parse_enum_tab(texture_target, line, &value, NULL),
+		"Bad texture target at: %s\n", line);
 
 	if (parse_str(line, "compare_func ", &line)) {
 		parameter = GL_TEXTURE_COMPARE_FUNC;
@@ -2619,7 +2594,8 @@ handle_texparameter(const char *line)
 		piglit_report_result(PIGLIT_FAIL);
 	}
 
-	value = lookup_enum_string(strings, &line, parameter_name);
+	REQUIRE(parse_enum_tab(strings, line, &value, &line),
+		"Bad %s at: %s\n", parameter_name, line);
 
 	glTexParameteri(target, parameter, value);
 }
-- 
2.9.0



More information about the Piglit mailing list