/* * Copyright (c) 2019, Vaclav Bubnik * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ /* * Frequently used C compiler warning flags: * -std=c90 * -pedantic * -Wall * -Wextra * * Additional C compiler warning flags with examples: * -Wshadow * -Wconversion * -Wfloat-equal * -Wswitch-default * -Wswitch-enum * -Wcast-align * -Wbad-function-cast * -Wwrite-strings * -Wredundant-decls */ int main(void) { return 0; } /* -Wshadow, GCC 4.8.4, LLVM 3.5 */ void w_shadow(void) { int i = 1; { int i = 2; /* declaration shadows a local variable */ (void)i; } (void)i; } /* -Wsign-conversion, -Wconversion, GCC 4.8.4, LLVM 3.5 */ void w_sign_conversion(void) { int si = 1; unsigned int ui; ui = si; /* implicit conversion changes signedness: 'int' to 'unsigned int' */ si = 0x7fffffff; ui = 0x7fffffff; si = 0x80000000; /* implicit conversion changes signedness: 'unsigned int' to 'int' [-Wsign-conversion] */ si = 0xffffffff; /* implicit conversion changes signedness: 'unsigned int' to 'int' [-Wsign-conversion] */ si = 2147483648; /* implicit conversion changes signedness: 'long' to 'int' [-Wsign-conversion] */ (void)ui; } /* -Wfloat-conversion, -Wconversion, GCC 4.8.4, LLVM 3.5 */ void w_float_conversion(void) { double d = 3.14; float f; f = d; /* implicit conversion loses floating-point precision: 'double' to 'float' */ (void)f; } /* -Wfloat-equal, GCC 4.8.4, LLVM 3.5 */ void w_float_equal(void) { float f = 1.2f; if (f == 1.2f) /* comparing floating point with == or != is unsafe */ { /* empty */ } } /* -Wswitch-default, GCC 4.8.4 */ void w_switch_default(int x) { switch (x) /* switch missing default case */ { case 0: break; } } /* -Wswitch-enum, GCC 4.8.4, LLVM 3.5 */ void w_switch_enum(void) { enum Color { Red, Green, Blue } color = Red; switch (color) /* enumeration value 'Blue' not explicitly handled in switch */ { case Red: break; case Green: break; default: break; } } /* -Wcast-align, LLVM 3.5 */ void w_cast_align(void) { const char *cstr = "abcd"; const int *i = (const int *)cstr; /* cast from 'const char *' to 'const int *' increases required alignment from 1 to 4 */ (void)i; } /* -Wbad-function-cast, LLVM 3.5 */ /* -Wpointer-to-int-cast in GCC, default in GCC 4.8.4 */ static const char *getstr(void) { return "abc"; } void w_bad_function_cast(void) { int i = (int)getstr(); /* cast from function call of type 'const char *' to non-matching type 'int' */ (void)i; } /* -Wwrite-strings, GCC 4.8.4, LLVM 3.5 */ void w_write_strings(void) { char *str = "abc"; /* initializing 'char *' with an expression of type 'const char [4]' discards qualifiers */ (void)str; } /* -Wredundant-decls, GCC 4.8.4 */ void w_redundant_decls(void); void w_redundant_decls(void); /* redundant redeclaration of '..' */