From f8db5d5d1c1898961f27d1040f87b052238cb8a8 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Fri, 25 Mar 2011 15:04:11 -0700 Subject: [PATCH] more C89-fixes going down to strict -ansi c89 is quite overkill (no 'inline', and /* */-style comments). But with these fixes, the code compiles with the stringent flags: -Wextra -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations and -Wdeclaration-after-statement Change-Id: I36222f8f505bcba3d9d1309ad98b5ccb04ec17e3 --- examples/cwebp.c | 11 ++++++----- examples/dwebp.c | 11 ++++++----- makefile.unix | 8 +++++++- src/dec/dsp.c | 8 +++++--- src/dec/idec.c | 14 ++++++++------ src/dec/vp8.c | 4 ++-- src/dec/vp8i.h | 4 ++-- src/dec/webp.c | 3 ++- src/dec/yuv.c | 2 +- src/dec/yuv.h | 2 +- src/enc/Makefile.in | 2 +- src/enc/dsp.c | 10 ++++++---- src/enc/filter.c | 2 +- src/enc/iterator.c | 2 +- src/enc/picture.c | 4 ++-- src/enc/vp8enci.h | 2 +- src/enc/webpenc.c | 6 +++++- src/webp/decode.h | 4 +++- src/webp/decode_vp8.h | 4 ++-- src/webp/encode.h | 2 +- 20 files changed, 63 insertions(+), 42 deletions(-) diff --git a/examples/cwebp.c b/examples/cwebp.c index 0e8235d6..6a8778d6 100644 --- a/examples/cwebp.c +++ b/examples/cwebp.c @@ -276,6 +276,7 @@ static int ReadJPEG(FILE* in_file, WebPPicture* const pic) { #ifdef WEBP_HAVE_PNG static void PNGAPI error_function(png_structp png, png_const_charp dummy) { + (void)dummy; // remove variable-unused warning longjmp(png_jmpbuf(png), 1); } @@ -284,9 +285,9 @@ static int ReadPNG(FILE* in_file, WebPPicture* const pic) { png_infop info; int color_type, bit_depth, interlaced; int num_passes; - int p, y; + int p; int ok = 0; - png_uint_32 width, height; + png_uint_32 width, height, y; int stride; uint8_t* rgb = NULL; @@ -446,7 +447,7 @@ static void PrintValues(const int values[4]) { fprintf(stderr,"|\n"); } -void PrintExtraInfo(const WebPPicture* const pic, int short_output) { +static void PrintExtraInfo(const WebPPicture* const pic, int short_output) { const WebPAuxStats* const stats = pic->stats; if (short_output) { fprintf(stderr, "%7d %2.2f\n", stats->coded_size, stats->PSNR[3]); @@ -553,7 +554,7 @@ static int DumpPicture(const WebPPicture* const picture, const char* PGM_name) { //----------------------------------------------------------------------------- -static void HelpShort() { +static void HelpShort(void) { printf("Usage:\n\n"); printf(" cwebp [options] -q quality input.png -o output.webp\n\n"); printf("where quality is between 0 (poor) to 100 (very good).\n"); @@ -561,7 +562,7 @@ static void HelpShort() { printf("Try -longhelp for an exhaustive list of advanced options.\n"); } -static void HelpLong() { +static void HelpLong(void) { printf("Usage:\n"); printf(" cwebp [-preset <...>] [options] in_file [-o out_file]\n\n"); printf("If input size (-s) for an image is not specified, " diff --git a/examples/dwebp.c b/examples/dwebp.c index 65e6427c..45673862 100644 --- a/examples/dwebp.c +++ b/examples/dwebp.c @@ -117,6 +117,7 @@ static int WritePNG(const char* out_file_name, unsigned char* rgb, int stride, #elif defined(WEBP_HAVE_PNG) // !WIN32 static void PNGAPI error_function(png_structp png, png_const_charp dummy) { + (void)dummy; // remove variable-unused warning longjmp(png_jmpbuf(png), 1); } @@ -124,7 +125,7 @@ static int WritePNG(FILE* out_file, unsigned char* rgb, int stride, png_uint_32 width, png_uint_32 height) { png_structp png; png_infop info; - int y; + png_uint_32 y; png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, error_function, NULL); @@ -203,7 +204,7 @@ typedef enum { PGM, } OutputFileFormat; -static void help(const char *s) { +static void Help(void) { printf("Usage: dwebp " "[in_file] [-h] [-v] [-ppm] [-pgm] [-version] [-o out_file]\n\n" "Decodes the WebP image file to PNG format [Default]\n" @@ -227,7 +228,7 @@ int main(int argc, const char *argv[]) { int c; for (c = 1; c < argc; ++c) { if (!strcmp(argv[c], "-h") || !strcmp(argv[c], "-help")) { - help(argv[0]); + Help(); return 0; } else if (!strcmp(argv[c], "-o") && c < argc - 1) { out_file = argv[++c]; @@ -244,7 +245,7 @@ int main(int argc, const char *argv[]) { verbose = 1; } else if (argv[c][0] == '-') { printf("Unknown option '%s'\n", argv[c]); - help(argv[0]); + Help(); return -1; } else { in_file = argv[c]; @@ -253,7 +254,7 @@ int main(int argc, const char *argv[]) { if (in_file == NULL) { printf("missing input file!!\n"); - help(argv[0]); + Help(); return -1; } diff --git a/makefile.unix b/makefile.unix index 58d266f3..c342b816 100644 --- a/makefile.unix +++ b/makefile.unix @@ -31,7 +31,13 @@ endif # Uncomment for build for 32bit platform # Alternatively, you can just use the command # 'make -f makefile.unix EXTRA_FLAGS=-m32' to that effect. -# EXTRA_FLAGS= -m32 +# EXTRA_FLAGS += -m32 + +# Extra flags to emulate C89 strictness with the full ANSI +EXTRA_FLAGS += -Wextra -Wold-style-definition +EXTRA_FLAGS += -Wmissing-prototypes +EXTRA_FLAGS += -Wmissing-declarations +EXTRA_FLAGS += -Wdeclaration-after-statement #### Nothing should normally be changed below this line #### diff --git a/src/dec/dsp.c b/src/dec/dsp.c index 2737cb47..efde49d9 100644 --- a/src/dec/dsp.c +++ b/src/dec/dsp.c @@ -28,9 +28,11 @@ static int8_t sclip1[1020 + 1020 + 1]; // clips [-1020, 1020] to [-128, 127] static int8_t sclip2[112 + 112 + 1]; // clips [-112, 112] to [-16, 15] static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255] -static int tables_ok = 0; +// We declare this variable 'volatile' to prevent instruction reordering +// and make sure it's set to true _last_ (so as to be thread-safe) +static volatile int tables_ok = 0; -void VP8DspInitTables() { +void VP8DspInitTables(void) { if (!tables_ok) { int i; for (i = -255; i <= 255; ++i) { @@ -685,7 +687,7 @@ void (*VP8SimpleHFilter16i)(uint8_t*, int, int) = SimpleHFilter16i; //----------------------------------------------------------------------------- -void VP8DspInit() { +void VP8DspInit(void) { // later we'll plug some SSE2 variant here } diff --git a/src/dec/idec.c b/src/dec/idec.c index 39cbc177..d49ceb0a 100644 --- a/src/dec/idec.c +++ b/src/dec/idec.c @@ -496,10 +496,11 @@ static VP8StatusCode IDecCheckStatus(const WebPIDecoder* const idec) { VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data, uint32_t data_size) { + VP8StatusCode status; if (idec == NULL || data == NULL) { return VP8_STATUS_INVALID_PARAM; } - const VP8StatusCode status = IDecCheckStatus(idec); + status = IDecCheckStatus(idec); if (status != VP8_STATUS_SUSPENDED) { return status; } @@ -516,10 +517,11 @@ VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data, VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data, uint32_t data_size) { + VP8StatusCode status; if (idec == NULL || data == NULL) { return VP8_STATUS_INVALID_PARAM; } - const VP8StatusCode status = IDecCheckStatus(idec); + status = IDecCheckStatus(idec); if (status != VP8_STATUS_SUSPENDED) { return status; } @@ -536,7 +538,7 @@ VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data, //------------------------------------------------------------------------------ -uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int *last_y, +uint8_t* WebPIDecGetRGB(const WebPIDecoder* const idec, int *last_y, int* width, int* height, int* stride) { if (!idec || !idec->dec_ || idec->params_.mode != MODE_RGB || idec->state_ <= STATE_PARTS0) { @@ -551,9 +553,9 @@ uint8_t* WebPIDecGetRGB(const WebPIDecoder* idec, int *last_y, return idec->params_.output; } -uint8_t* WebPIDecGetYUV(const WebPIDecoder* idec, int *last_y, uint8_t** u, - uint8_t** v, int* width, int* height, int *stride, - int* uv_stride) { +uint8_t* WebPIDecGetYUV(const WebPIDecoder* const idec, int *last_y, + uint8_t** u, uint8_t** v, int* width, int* height, + int *stride, int* uv_stride) { if (!idec || !idec->dec_ || idec->params_.mode != MODE_YUV || idec->state_ <= STATE_PARTS0) { return NULL; diff --git a/src/dec/vp8.c b/src/dec/vp8.c index aa47b531..43a0c354 100644 --- a/src/dec/vp8.c +++ b/src/dec/vp8.c @@ -18,7 +18,7 @@ extern "C" { //----------------------------------------------------------------------------- -int WebPGetDecoderVersion() { +int WebPGetDecoderVersion(void) { return (DEC_MAJ_VERSION << 16) | (DEC_MIN_VERSION << 8) | DEC_REV_VERSION; } @@ -39,7 +39,7 @@ int VP8InitIoInternal(VP8Io* const io, int version) { return 1; } -VP8Decoder* VP8New() { +VP8Decoder* VP8New(void) { VP8Decoder* dec = (VP8Decoder*)calloc(1, sizeof(VP8Decoder)); if (dec) { SetOk(dec); diff --git a/src/dec/vp8i.h b/src/dec/vp8i.h index 6ed053da..b2ad9a33 100644 --- a/src/dec/vp8i.h +++ b/src/dec/vp8i.h @@ -289,8 +289,8 @@ extern VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES]; extern VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES]; extern VP8PredFunc VP8PredLuma4[NUM_BMODES]; -void VP8DspInit(); // must be called before anything using the above -void VP8DspInitTables(); // needs to be called no matter what. +void VP8DspInit(void); // must be called before anything using the above +void VP8DspInitTables(void); // needs to be called no matter what. // simple filter (only for luma) typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh); diff --git a/src/dec/webp.c b/src/dec/webp.c index d51a066a..3bf6f55b 100644 --- a/src/dec/webp.c +++ b/src/dec/webp.c @@ -533,8 +533,9 @@ static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* data, uint32_t data_size, int* width, int* height, WebPDecParams* params_out) { uint8_t* output; - WebPDecParams params = { 0 }; + WebPDecParams params; + memset(¶ms, 0, sizeof(params)); params.mode = mode; if (!WebPInitDecParams(data, data_size, width, height, ¶ms)) { return NULL; diff --git a/src/dec/yuv.c b/src/dec/yuv.c index 30f59c13..ac448eec 100644 --- a/src/dec/yuv.c +++ b/src/dec/yuv.c @@ -23,7 +23,7 @@ uint8_t VP8kClip[YUV_RANGE_MAX - YUV_RANGE_MIN]; static int done = 0; -void VP8YUVInit() { +void VP8YUVInit(void) { int i; if (done) { return; diff --git a/src/dec/yuv.h b/src/dec/yuv.h index 039427ba..50e63f9b 100644 --- a/src/dec/yuv.h +++ b/src/dec/yuv.h @@ -57,7 +57,7 @@ inline static void VP8YuvToBgra(int y, int u, int v, uint8_t* const bgra) { } // Must be called before everything, to initialize the tables. -void VP8YUVInit(); +void VP8YUVInit(void); #if defined(__cplusplus) || defined(c_plusplus) } // extern "C" diff --git a/src/enc/Makefile.in b/src/enc/Makefile.in index 3f5fc11b..10f33347 100644 --- a/src/enc/Makefile.in +++ b/src/enc/Makefile.in @@ -266,7 +266,7 @@ clean-noinstLTLIBRARIES: echo "rm -f \"$${dir}/so_locations\""; \ rm -f "$${dir}/so_locations"; \ done -libwebpencode.la: $(libwebpencode_la_OBJECTS) $(libwebpencode_la_DEPENDENCIES) +libwebpencode.la: $(libwebpencode_la_OBJECTS) $(libwebpencode_la_DEPENDENCIES) $(libwebpencode_la_LINK) $(libwebpencode_la_OBJECTS) $(libwebpencode_la_LIBADD) $(LIBS) mostlyclean-compile: diff --git a/src/enc/dsp.c b/src/enc/dsp.c index 0aee6e5f..45f977c9 100644 --- a/src/enc/dsp.c +++ b/src/enc/dsp.c @@ -21,9 +21,11 @@ extern "C" { static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255] -static int tables_ok = 0; +// We declare this variable 'volatile' to prevent instruction reordering +// and make sure it's set to true _last_ (so as to be thread-safe) +static volatile int tables_ok = 0; -static void InitTables() { +static void InitTables(void) { if (!tables_ok) { int i; for (i = -255; i <= 255 + 255; ++i) { @@ -79,7 +81,7 @@ static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst) { } } -void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) { +static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) { int i; int tmp[16]; for (i = 0; i < 4; ++i, src += BPS, ref += BPS) { @@ -636,7 +638,7 @@ VP8BlockCopy VP8Copy16x16 = Copy16x16; //----------------------------------------------------------------------------- -void VP8EncDspInit() { +void VP8EncDspInit(void) { InitTables(); } diff --git a/src/enc/filter.c b/src/enc/filter.c index 5a243b17..a0a42b07 100644 --- a/src/enc/filter.c +++ b/src/enc/filter.c @@ -25,7 +25,7 @@ static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255] static int tables_ok = 0; -static void InitTables() { +static void InitTables(void) { if (!tables_ok) { int i; for (i = -255; i <= 255; ++i) { diff --git a/src/enc/iterator.c b/src/enc/iterator.c index 7cd9f060..991644d0 100644 --- a/src/enc/iterator.c +++ b/src/enc/iterator.c @@ -270,7 +270,7 @@ int VP8IteratorNext(VP8EncIterator* const it, //----------------------------------------------------------------------------- // Helper function to set mode properties -void VP8SetIntra16Mode(const VP8EncIterator* it, int mode) { +void VP8SetIntra16Mode(const VP8EncIterator* const it, int mode) { int y; uint8_t* preds = it->preds_; for (y = 0; y < 4; ++y) { diff --git a/src/enc/picture.c b/src/enc/picture.c index 5b8d98b9..6c12ea48 100644 --- a/src/enc/picture.c +++ b/src/enc/picture.c @@ -38,7 +38,7 @@ int WebPPictureAlloc(WebPPicture* const picture) { picture->y_stride = width; picture->uv_stride = uv_width; WebPPictureFree(picture); // erase previous buffer - picture->y = (uint8_t*)malloc(total_size); + picture->y = (uint8_t*)malloc((size_t)total_size); if (picture->y == NULL) return 0; picture->u = picture->y + y_size; picture->v = picture->u + uv_size; @@ -163,7 +163,7 @@ enum { YUV_FRAC = 16 }; static inline int clip_uv(int v) { v = (v + (257 << (YUV_FRAC + 2 - 1))) >> (YUV_FRAC + 2); - return ((v & ~0xff) == 0) ? v : (v < 0) ? 0u : 255u; + return ((v & ~0xff) == 0) ? v : (v < 0) ? 0 : 255; } static inline int rgb_to_y(int r, int g, int b) { diff --git a/src/enc/vp8enci.h b/src/enc/vp8enci.h index ae7cb78c..b19450d3 100644 --- a/src/enc/vp8enci.h +++ b/src/enc/vp8enci.h @@ -455,7 +455,7 @@ typedef enum { typedef int (*VP8CPUInfo)(CPUFeature feature); extern VP8CPUInfo CPUInfo; -void VP8EncDspInit(); // must be called before using anything from the above. +void VP8EncDspInit(void); // must be called before using any of the above // in filter.c extern void VP8InitFilter(VP8EncIterator* const it); diff --git a/src/enc/webpenc.c b/src/enc/webpenc.c index c1c5f071..59221d7f 100644 --- a/src/enc/webpenc.c +++ b/src/enc/webpenc.c @@ -29,7 +29,7 @@ extern "C" { //----------------------------------------------------------------------------- -int WebPGetEncoderVersion() { +int WebPGetEncoderVersion(void) { return (ENC_MAJ_VERSION << 16) | (ENC_MIN_VERSION << 8) | ENC_REV_VERSION; } @@ -39,6 +39,10 @@ int WebPGetEncoderVersion() { static int DummyWriter(const uint8_t* data, size_t data_size, const WebPPicture* const picture) { + // The following are to prevent 'unused variable' error message. + (void)data; + (void)data_size; + (void)picture; return 1; } diff --git a/src/webp/decode.h b/src/webp/decode.h index 790b5500..6c63d54b 100644 --- a/src/webp/decode.h +++ b/src/webp/decode.h @@ -20,7 +20,7 @@ extern "C" { // Return the decoder's version number, packed in hexadecimal using 8bits for // each of major/minor/revision. E.g: v2.5.7 is 0x020507. -int WebPGetDecoderVersion(); +int WebPGetDecoderVersion(void); // Retrieve basic header information: width, height. // This function will also validate the header and return 0 in @@ -173,6 +173,8 @@ VP8StatusCode WebPIAppend(WebPIDecoder* const idec, const uint8_t* data, // A variant of the above function to be used when data buffer contains // partial data from the beginning. In this case data buffer is not copied // to the internal memory. +// Note that the value of the 'data' pointer can change between calls to +// WebPIUpdate, for instance when the data buffer is resized to fit larger data. VP8StatusCode WebPIUpdate(WebPIDecoder* const idec, const uint8_t* data, uint32_t data_size); diff --git a/src/webp/decode_vp8.h b/src/webp/decode_vp8.h index efcf649d..153a4c54 100644 --- a/src/webp/decode_vp8.h +++ b/src/webp/decode_vp8.h @@ -83,13 +83,13 @@ struct VP8Io { }; // Internal, version-checked, entry point -extern int VP8InitIoInternal(VP8Io* const, int); +int VP8InitIoInternal(VP8Io* const, int); // Main decoding object. This is an opaque structure. typedef struct VP8Decoder VP8Decoder; // Create a new decoder object. -VP8Decoder* VP8New(); +VP8Decoder* VP8New(void); // Must be called to make sure 'io' is initialized properly. // Returns false in case of version mismatch. Upon such failure, no other diff --git a/src/webp/encode.h b/src/webp/encode.h index 50d93657..e0cc5dc3 100644 --- a/src/webp/encode.h +++ b/src/webp/encode.h @@ -24,7 +24,7 @@ extern "C" { // Return the encoder's version number, packed in hexadecimal using 8bits for // each of major/minor/revision. E.g: v2.5.7 is 0x020507. -int WebPGetEncoderVersion(); +int WebPGetEncoderVersion(void); //----------------------------------------------------------------------------- // One-stop-shop call! No questions asked: