From fda696bf95534d67dcb76de3055061eeda20170d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 22 Sep 2017 12:00:14 +1000 Subject: [PATCH] Add PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE. --- cmd/puffs-c/internal/cgen/base-impl.h | 2 ++ cmd/puffs-c/internal/cgen/data.go | 4 ++-- cmd/puffs-c/internal/cgen/statement.go | 3 +-- cmd/puffs-c/internal/cgen/var.go | 3 +-- gen/c/std/flate.c | 19 +++++++------------ gen/c/std/gif.c | 5 +++-- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/cmd/puffs-c/internal/cgen/base-impl.h b/cmd/puffs-c/internal/cgen/base-impl.h index 793f5b54..f676d43a 100644 --- a/cmd/puffs-c/internal/cgen/base-impl.h +++ b/cmd/puffs-c/internal/cgen/base-impl.h @@ -6,6 +6,8 @@ // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. +#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) + // TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so // if a benchmark justifies the additional code path. #define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) diff --git a/cmd/puffs-c/internal/cgen/data.go b/cmd/puffs-c/internal/cgen/data.go index 2836af21..7422bd43 100644 --- a/cmd/puffs-c/internal/cgen/data.go +++ b/cmd/puffs-c/internal/cgen/data.go @@ -12,8 +12,8 @@ const baseHeader = "" + "" const baseImpl = "" + - "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro instead of an explicit assignment and case statement\n// so that c" + - "lang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" + + "#ifndef PUFFS_BASE_IMPL_H\n#define PUFFS_BASE_IMPL_H\n\n// Use of this source code is governed by a BSD-style license that can be found\n// in the LICENSE file.\n\n#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x)\n\n// TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so\n// if a benchmark justifies the additional code path.\n#define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0))\n#define PUFFS_U16LE(p) (((uint16_t)(p[0]) << 0) | ((uint16_t)(p[1]) << 8))\n#define PUFFS_U32BE(p) \\\n (((uint32_t)(p[0]) << 24) | ((uint32_t)(p[1]) << 16) | \\\n ((uint32_t)(p[2]) << 8) | ((uint32_t)(p[3]) << 0))\n#define PUFFS_U32LE(p) \\\n (((uint32_t)(p[0]) << 0) | ((uint32_t)(p[1]) << 8) | \\\n ((uint32_t)(p[2]) << 16) | ((uint32_t)(p[3]) << 24))\n\n// Use switch cases for coroutine suspension points, similar to the technique\n// in https://www.chiark.greenend.org.uk/~sgtatham/coroutines.html\n//\n// We use a trivial macro in" + + "stead of an explicit assignment and case statement\n// so that clang-format doesn't get confused by the unusual \"case\"s.\n#define PUFFS_COROUTINE_SUSPENSION_POINT(n) \\\n coro_susp_point = n; \\\n case n:\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define PUFFS_LIKELY(expr) (__builtin_expect(!!(expr), 1))\n#define PUFFS_UNLIKELY(expr) (__builtin_expect(!!(expr), 0))\n// Declare the printf prototype. The generated code shouldn't need this at all,\n// but it's useful for manual printf debugging.\nextern int printf(const char* __restrict __format, ...);\n#else\n#define PUFFS_LIKELY(expr) (expr)\n#define PUFFS_UNLIKELY(expr) (expr)\n#endif\n\n#endif // PUFFS_BASE_IMPL_H\n" + "" type template_args_short_read struct { diff --git a/cmd/puffs-c/internal/cgen/statement.go b/cmd/puffs-c/internal/cgen/statement.go index d13c7a64..c1818083 100644 --- a/cmd/puffs-c/internal/cgen/statement.go +++ b/cmd/puffs-c/internal/cgen/statement.go @@ -199,8 +199,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error { // TODO: remove this. if n.XType().Decorator().Key() == t.KeyColon { - b.printf("\n/* Avoid the \"unused variable\" warning. */\n") - b.printf("if (%s%s.ptr) {}\n", vPrefix, n.Name().String(g.tm)) + b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%s%s);\n", vPrefix, n.Name().String(g.tm)) } } return nil diff --git a/cmd/puffs-c/internal/cgen/var.go b/cmd/puffs-c/internal/cgen/var.go index d2c2b85a..e51ad112 100644 --- a/cmd/puffs-c/internal/cgen/var.go +++ b/cmd/puffs-c/internal/cgen/var.go @@ -149,8 +149,7 @@ func (g *gen) writeSaveDerivedVar(b *buffer, name t.ID, typ *a.TypeExpr) error { b.printf("}") // Not all writer1 methods use the wend variable. - b.printf("\n/* Avoid the \"unused variable\" warning. */\n") - b.printf("(void)(%swend_%s);", bPrefix, nameStr) + b.printf("PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(%swend_%s);", bPrefix, nameStr) b.printf("}\n") } diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index 1f41c366..470bc047 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -290,6 +290,8 @@ puffs_flate_status puffs_flate_zlib_decoder_decode( // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. +#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) + // TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so // if a benchmark justifies the additional code path. #define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) @@ -567,8 +569,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, *lim->ptr_to_len -= n; } } - /* Avoid the "unused variable" warning. */ - (void)(b_wend_dst); + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } puffs_flate_status t_0 = puffs_flate_decoder_decode_blocks(self, a_dst, a_src); @@ -587,10 +588,7 @@ puffs_flate_status puffs_flate_decoder_decode(puffs_flate_decoder* self, if (v_z > 0) { v_written = ((puffs_base_slice_u8){.ptr = a_dst.buf->ptr, .len = b_wptr_dst - a_dst.buf->ptr}); - - /* Avoid the "unused variable" warning. */ - if (v_written.ptr) { - } + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(v_written); } status = v_z; goto suspend; @@ -614,8 +612,7 @@ exit: *lim->ptr_to_len -= n; } } - /* Avoid the "unused variable" warning. */ - (void)(b_wend_dst); + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } self->private_impl.status = status; @@ -919,8 +916,7 @@ exit: *lim->ptr_to_len -= n; } } - /* Avoid the "unused variable" warning. */ - (void)(b_wend_dst); + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); @@ -1255,8 +1251,7 @@ exit: *lim->ptr_to_len -= n; } } - /* Avoid the "unused variable" warning. */ - (void)(b_wend_dst); + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri); diff --git a/gen/c/std/gif.c b/gen/c/std/gif.c index bd12111e..35fbbc35 100644 --- a/gen/c/std/gif.c +++ b/gen/c/std/gif.c @@ -263,6 +263,8 @@ puffs_gif_status puffs_gif_lzw_decoder_decode(puffs_gif_lzw_decoder* self, // Use of this source code is governed by a BSD-style license that can be found // in the LICENSE file. +#define PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(x) (void)(x) + // TODO: look for (ifdef) the x86 architecture and cast the pointer? Only do so // if a benchmark justifies the additional code path. #define PUFFS_U16BE(p) (((uint16_t)(p[0]) << 8) | ((uint16_t)(p[1]) << 0)) @@ -1288,8 +1290,7 @@ exit: *lim->ptr_to_len -= n; } } - /* Avoid the "unused variable" warning. */ - (void)(b_wend_dst); + PUFFS_IGNORE_POTENTIALLY_UNUSED_VARIABLE(b_wend_dst); } if (a_src.buf) { size_t n = b_rptr_src - (a_src.buf->ptr + a_src.buf->ri);