diff --git a/src/vrend_shader.c b/src/vrend_shader.c index 8e2d174..4ea0a8f 100644 --- a/src/vrend_shader.c +++ b/src/vrend_shader.c @@ -143,6 +143,7 @@ struct dump_ctx { int prog_type; int size; struct vrend_strbuf glsl_main; + int indent_level; struct vrend_strbuf glsl_hdr; struct vrend_strbuf glsl_ver_ext; uint instno; @@ -547,19 +548,36 @@ static void require_glsl_ver(struct dump_ctx *ctx, int glsl_ver) ctx->glsl_ver_required = glsl_ver; } +static void emit_indent(struct dump_ctx *ctx) +{ + if (ctx->indent_level > 0) { + /* very high levels of indentation doesn't improve readability */ + int indent_level = MIN2(ctx->indent_level, 15); + char buf[16]; + memset(buf, '\t', indent_level); + buf[indent_level] = '\0'; + strbuf_append(&ctx->glsl_main, buf); + } +} + static void emit_buf(struct dump_ctx *ctx, const char *buf) { + emit_indent(ctx); strbuf_append(&ctx->glsl_main, buf); } static void indent_buf(struct dump_ctx *ctx) { - return strbuf_indent(&ctx->glsl_main); + ctx->indent_level++; } static void outdent_buf(struct dump_ctx *ctx) { - return strbuf_outdent(&ctx->glsl_main); + if (ctx->indent_level <= 0) { + strbuf_set_error(&ctx->glsl_main); + return; + } + ctx->indent_level--; } static void set_buf_error(struct dump_ctx *ctx) @@ -572,6 +590,7 @@ static void emit_buff(struct dump_ctx *ctx, const char *fmt, ...) { va_list va; va_start(va, fmt); + emit_indent(ctx); strbuf_vappendf(&ctx->glsl_main, fmt, va); va_end(va); } diff --git a/src/vrend_strbuf.h b/src/vrend_strbuf.h index 3899f83..080e798 100644 --- a/src/vrend_strbuf.h +++ b/src/vrend_strbuf.h @@ -42,7 +42,6 @@ struct vrend_strbuf { /* size of string stored without terminating NULL */ size_t size; bool error_state; - int indent_level; }; static inline void strbuf_set_error(struct vrend_strbuf *sb) @@ -55,20 +54,6 @@ static inline bool strbuf_get_error(struct vrend_strbuf *sb) return sb->error_state; } -static inline void strbuf_indent(struct vrend_strbuf *sb) -{ - sb->indent_level++; -} - -static inline void strbuf_outdent(struct vrend_strbuf *sb) -{ - if (sb->indent_level <= 0) { - strbuf_set_error(sb); - return; - } - sb->indent_level--; -} - static inline size_t strbuf_get_len(struct vrend_strbuf *sb) { return sb->size; @@ -88,7 +73,6 @@ static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size) sb->buf[0] = 0; sb->error_state = false; sb->size = 0; - sb->indent_level = 0; return true; } @@ -98,14 +82,13 @@ static inline bool strbuf_alloc(struct vrend_strbuf *sb, int initial_size) static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *data, size_t len) { assert(!memchr(data, '\0', len)); - int new_len = len + sb->indent_level; if (strbuf_get_error(sb)) return; - if (sb->size + new_len + 1 > sb->alloc_size) { + if (sb->size + len + 1 > sb->alloc_size) { /* Reallocate to the larger size of current alloc + min realloc, * or the resulting string size if larger. */ - size_t new_size = MAX2(sb->size + new_len + 1, sb->alloc_size + STRBUF_MIN_MALLOC); + size_t new_size = MAX2(sb->size + len + 1, sb->alloc_size + STRBUF_MIN_MALLOC); char *new = realloc(sb->buf, new_size); if (!new) { strbuf_set_error(sb); @@ -114,14 +97,8 @@ static inline void strbuf_append_buffer(struct vrend_strbuf *sb, const char *dat sb->buf = new; sb->alloc_size = new_size; } - if (sb->indent_level) { - memset(sb->buf + sb->size, '\t', sb->indent_level); - sb->size += sb->indent_level; - sb->buf[sb->size] = '\0'; - new_len -= sb->indent_level; - } - memcpy(sb->buf + sb->size, data, new_len); - sb->size += new_len; + memcpy(sb->buf + sb->size, data, len); + sb->size += len; sb->buf[sb->size] = '\0'; } diff --git a/tests/test_virgl_strbuf.c b/tests/test_virgl_strbuf.c index ef5f902..ff348cd 100644 --- a/tests/test_virgl_strbuf.c +++ b/tests/test_virgl_strbuf.c @@ -159,57 +159,6 @@ START_TEST(strbuf_test_boundary2) } END_TEST -START_TEST(strbuf_test_indent) -{ - struct vrend_strbuf sb; - bool ret; - char str[9]; - ret = strbuf_alloc(&sb, 1024); - ck_assert_int_eq(ret, true); - - for (int i = 0; i < 8; i++) - str[i] = 'a' + i; - str[8] = 0; - - strbuf_indent(&sb); - strbuf_append(&sb, str); - strbuf_outdent(&sb); - strbuf_append(&sb, str); - /* make sure the TAB got added */ - ck_assert_int_eq(strbuf_get_error(&sb), false); - - ck_assert_str_eq(sb.buf, "\tabcdefghabcdefgh"); - strbuf_free(&sb); -} -END_TEST - -START_TEST(strbuf_test_indent2) -{ - struct vrend_strbuf sb; - bool ret; - char str[513]; - ret = strbuf_alloc(&sb, 1024); - ck_assert_int_eq(ret, true); - - strbuf_indent(&sb); - for (int i = 0; i < 512; i++) - str[i] = 'a' + (i % 26); - str[512] = 0; - strbuf_append(&sb, str); - strbuf_outdent(&sb); - /* make sure the TAB got added */ - ck_assert_int_eq(strbuf_get_len(&sb), strlen(str) + 1); - strbuf_append(&sb, str); - ck_assert_int_eq(strbuf_get_error(&sb), false); - ck_assert_int_eq(strbuf_get_len(&sb), strlen(sb.buf)); - /* we should have 512 + 512 + 1 at least */ - ck_assert_int_ge(sb.alloc_size, strbuf_get_len(&sb) + 1); - ck_assert_int_gt(sb.alloc_size, 1024); - - strbuf_free(&sb); -} -END_TEST - START_TEST(strbuf_test_appendf) { struct vrend_strbuf sb; @@ -251,8 +200,6 @@ static Suite *init_suite(void) tcase_add_test(tc_core, strbuf_add_huge_string); tcase_add_test(tc_core, strbuf_test_boundary); tcase_add_test(tc_core, strbuf_test_boundary2); - tcase_add_test(tc_core, strbuf_test_indent); - tcase_add_test(tc_core, strbuf_test_indent2); tcase_add_test(tc_core, strbuf_test_appendf); tcase_add_test(tc_core, strbuf_test_appendf_str); return s;