- implemented Wfloat-equal
authorMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Sat, 15 Dec 2007 02:30:37 +0000 (02:30 +0000)
committerMichael Beck <beck@ipd.info.uni-karlsruhe.de>
Sat, 15 Dec 2007 02:30:37 +0000 (02:30 +0000)
[r18758]

parser.c
warning.c
warning.h

index 91237fd..f95d1af 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -4312,6 +4312,11 @@ static void semantic_sub(binary_expression_t *expression)
        }
 }
 
+/**
+ * Check the semantics of comparison expressions.
+ *
+ * @param expression   The expression to check.
+ */
 static void semantic_comparison(binary_expression_t *expression)
 {
        expression_t *left            = expression->left;
@@ -4328,6 +4333,13 @@ static void semantic_comparison(binary_expression_t *expression)
                expression->left  = create_implicit_cast(left, arithmetic_type);
                expression->right = create_implicit_cast(right, arithmetic_type);
                expression->expression.datatype = arithmetic_type;
+               if (warning.float_equal &&
+                   (expression->expression.kind == EXPR_BINARY_EQUAL ||
+                    expression->expression.kind == EXPR_BINARY_NOTEQUAL) &&
+                   is_type_floating(arithmetic_type)) {
+                       warningf(expression->expression.source_position,
+                                "comparing floating point with == or != is unsafe");
+               }
        } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
                /* TODO check compatibility */
        } else if (is_type_pointer(type_left)) {
@@ -4336,7 +4348,8 @@ static void semantic_comparison(binary_expression_t *expression)
                expression->left = create_implicit_cast(left, type_right);
        } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
                type_error_incompatible("invalid operands in comparison",
-                                       token.source_position, type_left, type_right);
+                                       expression->expression.source_position,
+                                       type_left, type_right);
        }
        expression->expression.datatype = type_int;
 }
index ba1a021..f39e16c 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -21,6 +21,7 @@ void set_warning_opt(const char *const opt)
        OPT("empty-statement",               empty_statement)
        OPT("error",                         s_are_errors)
        OPT("fatal-errors",                  fatal_errors)
+       OPT("float-equal",                   float_equal)
        OPT("format",                        check_format)
        OPTX("implicit") {
                SET(implicit_function_declaration)
@@ -58,6 +59,7 @@ warning_t warning = {
        .check_format                  = true,
        .empty_statement               = false,
        .fatal_errors                  = false,
+       .float_equal                   = true,
        .implicit_function_declaration = true,
        .implicit_int                  = true,
        .main                          = true,
index 4bacbb5..6ebcd40 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -7,81 +7,79 @@ void set_warning_opt(const char *opt);
 
 typedef struct warning_t {
 #if 0 // TODO
-       bool aggregate_return:1;              /* Warn if any functions that return structures or unions are defined or called */
-       bool bad_function_cast:1;             /* Warn whenever a function call is cast to a non-matching type */
+       bool aggregate_return:1;              /**< Warn if any functions that return structures or unions are defined or called */
+       bool bad_function_cast:1;             /**< Warn whenever a function call is cast to a non-matching type */
 #endif
-       bool char_subscripts:1;               /* Warn if an array subscript has the type 'char' */
-       bool check_format:1;                  /* Check printf-style format strings */
+       bool char_subscripts:1;               /**< Warn if an array subscript has the type 'char' */
+       bool check_format:1;                  /**< Check printf-style format strings */
 #if 0 // TODO
-       bool cast_align:1;                    /* Warn whenever a pointer is cast such that the required alignment of the target is increased */
-       bool cast_qual:1;                     /* Warn whenever a pointer is cast so as to remove a type qualifier from the target type */
-       bool conversion:1;                    /* Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype */
-       bool declaration_after_statement:1;   /* Warn when a declaration is found after a statement in a block */
-       bool deprecated_declarations:1;       /* Warn about uses of functions, variables and types marked as deprecated by using the 'deprecated' attribute */
-       bool div_by_zero:1;                   /* Warn about compile-time integer division by zero */
+       bool cast_align:1;                    /**< Warn whenever a pointer is cast such that the required alignment of the target is increased */
+       bool cast_qual:1;                     /**< Warn whenever a pointer is cast so as to remove a type qualifier from the target type */
+       bool conversion:1;                    /**< Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype */
+       bool declaration_after_statement:1;   /**< Warn when a declaration is found after a statement in a block */
+       bool deprecated_declarations:1;       /**< Warn about uses of functions, variables and types marked as deprecated by using the 'deprecated' attribute */
+       bool div_by_zero:1;                   /**< Warn about compile-time integer division by zero */
 #endif
-       bool empty_statement:1;               /* Warn about empty statements, i.e. lone ';'  */
+       bool empty_statement:1;               /**< Warn about empty statements, i.e. lone ';'  */
 #if 0 // TODO
-       bool endif_labels:1;                  /* Warn whenever an '#else' or an '#endif' are followed by text */
+       bool endif_labels:1;                  /**< Warn whenever an '#else' or an '#endif' are followed by text */
 #endif
-       bool fatal_errors:1;                  /* First error stops the compilation */
+       bool fatal_errors:1;                  /**< First error stops the compilation */
+       bool float_equal:1;                   /**< Warn if floating point values are used in equality comparisons */
+       bool implicit_function_declaration:1; /**< Warn whenever a function is used before being declared */
+       bool implicit_int:1;                  /**< Warn when a declaration does not specify a type */
 #if 0 // TODO
-       bool float_equal:1;                   /* Warn if floating point values are used in equality comparisons */
+       bool inline:1;                        /**< Warn if a function can not be inlined and it was declared as inline */
+       bool long_long:1;                     /**< Warn if 'long long' type is used */
 #endif
-       bool implicit_function_declaration:1; /* Warn whenever a function is used before being declared */
-       bool implicit_int:1;                  /* Warn when a declaration does not specify a type */
+       bool main:1;                          /**< Warn if the type of 'main' is suspicious */
 #if 0 // TODO
-       bool inline:1;                        /* Warn if a function can not be inlined and it was declared as inline */
-       bool long_long:1;                     /* Warn if 'long long' type is used */
+       bool missing_braces:1;                /**< Warn if an aggregate or union initializer is not fully bracketed */
 #endif
-       bool main:1;                          /* Warn if the type of 'main' is suspicious */
+       bool missing_declarations:1;          /**< Warn if a global function is defined without a previous declaration */
 #if 0 // TODO
-       bool missing_braces:1;                /* Warn if an aggregate or union initializer is not fully bracketed */
+       bool missing_format_attribute:1;      /**< If '-Wformat' is enabled, also warn about functions which might be candidates for 'format' attributes */
+       bool missing_noreturn:1;              /**< Warn about functions which might be candidates for attribute 'noreturn' */
 #endif
-       bool missing_declarations:1;          /* Warn if a global function is defined without a previous declaration */
+       bool missing_prototypes:1;            /**< Warn if a global function is defined without a previous prototype declaration */
 #if 0 // TODO
-       bool missing_format_attribute:1;      /* If '-Wformat' is enabled, also warn about functions which might be candidates for 'format' attributes */
-       bool missing_noreturn:1;              /* Warn about functions which might be candidates for attribute 'noreturn' */
+       bool multichar:1;                     /**< Warn if a multicharacter constant ('FOOF') is used. */
+       bool nested_externs:1;                /**< Warn if an 'extern' declaration is encountered within a function */
+       bool packed:1;                        /**< Warn if a structure is given the packed attribute, but the packed attribute has no effect on the layout or size of the structure */
+       bool padded:1;                        /**< Warn if padding is included in a structure, either to align an element of the structure or to align the whole structure */
+       bool parentheses:1;                   /**< Warn if parentheses are omitted in certain contexts (assignment where truth value is expected, if-else-braces) */
+       bool pointer_arith:1;                 /**< Warn about anything that depends on the "size of" a function type or of 'void' */
 #endif
-       bool missing_prototypes:1;            /* Warn if a global function is defined without a previous prototype declaration */
+       bool redundant_decls:1;               /**< Warn about redundant declarations */
 #if 0 // TODO
-       bool multichar:1;                     /* Warn if a multicharacter constant ('FOOF') is used. */
-       bool nested_externs:1;                /* Warn if an 'extern' declaration is encountered within a function */
-       bool packed:1;                        /* Warn if a structure is given the packed attribute, but the packed attribute has no effect on the layout or size of the structure */
-       bool padded:1;                        /* Warn if padding is included in a structure, either to align an element of the structure or to align the whole structure */
-       bool parentheses:1;                   /* Warn if parentheses are omitted in certain contexts (assignment where truth value is expected, if-else-braces) */
-       bool pointer_arith:1;                 /* Warn about anything that depends on the "size of" a function type or of 'void' */
+       bool return_type:1;                   /**< Warn about function definitions with a return-type that defaults to 'int'.  Also warn about any 'return' statement with no return-value in a function whose return-type is not 'void'. */
 #endif
-       bool redundant_decls:1;               /* Warn about redundant declarations */
+       bool s_are_errors:1;                  /**< Treat warnings as errors */
 #if 0 // TODO
-       bool return_type:1;                   /* Warn about function definitions with a return-type that defaults to 'int'.  Also warn about any 'return' statement with no return-value in a function whose return-type is not 'void'. */
+       bool sequence_point:1;                /**< Warn about code that may have undefined semantics because of violations of sequence point rules */
+       bool shadow:1;                        /**< Warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed */
+       bool sign_compare:1;                  /**< Warn when a comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned */
+       bool strict_aliasing:1;               /**< Warn about code which might break the strict aliasing rules that the compiler is using for optimization. */
 #endif
-       bool s_are_errors:1;                  /* Treat warnings as errors */
+       bool strict_prototypes:1;             /**< Warn if a function declaration has an unspecified parameter list */
+       bool switch_default:1;                /**< Warn whenever a 'switch' statement does not have a 'default' case */
 #if 0 // TODO
-       bool sequence_point:1;                /* Warn about code that may have undefined semantics because of violations of sequence point rules */
-       bool shadow:1;                        /* Warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed */
-       bool sign_compare:1;                  /* Warn when a comparison between signed and unsigned values could produce an incorrect result when the signed value is converted to unsigned */
-       bool strict_aliasing:1;               /* Warn about code which might break the strict aliasing rules that the compiler is using for optimization. */
+       bool switch_enum:1;                   /**< Warn about 'switch' statements with an enum as index type and missing case labels or case labels outside the enum range TODO has an alias -Wswitch? */
+       bool traditional:1;                   /**< Warn about certain constructs that behave differently in traditional and ISO C */
+       bool undef:1;                         /**< Warn if an undefined identifier is evaluated in an '#if' directive */
+       bool uninitialized:1;                 /**< Warn if an automatic variable is used without being initialized or if a variable may be clobbered by a 'setjmp' call. */
 #endif
-       bool strict_prototypes:1;             /* warn if a function declaration has an unspecified parameter list */
-       bool switch_default:1;                /* Warn whenever a 'switch' statement does not have a 'default' case */
+       bool unknown_pragmas:1;               /**< Warn when a #pragma directive is encountered which is not understood */
 #if 0 // TODO
-       bool switch_enum:1;                   /* Warn about 'switch' statements with an enum as index type and missing case labels or case labels outside the enum range TODO has an alias -Wswitch? */
-       bool traditional:1;                   /* Warn about certain constructs that behave differently in traditional and ISO C */
-       bool undef:1;                         /* Warn if an undefined identifier is evaluated in an '#if' directive */
-       bool uninitialized:1;                 /* Warn if an automatic variable is used without being initialized or if a variable may be clobbered by a 'setjmp' call. */
+       bool unreachable_code:1;              /**< Warn if the compiler detects that code will never be executed */
+       bool unused_function:1;               /**< Warn whenever a static function is declared but not defined or a non-inline static function is unused */
+       bool unused_label:1;                  /**< Warn whenever a label is declared but not used */
+       bool unused_parameter:1;              /**< Warn whenever a function parameter is unused aside from its declaration */
+       bool unused_variable:1;               /**< Warn whenever a local variable or non-constant static variable is unused aside from its declaration */
 #endif
-       bool unknown_pragmas:1;               /* Warn when a #pragma directive is encountered which is not understood */
+       bool unused_value:1;                  /**< Warn whenever a statement computes a result that is explicitly not used */
 #if 0 // TODO
-       bool unreachable_code:1;              /* Warn if the compiler detects that code will never be executed */
-       bool unused_function:1;               /* Warn whenever a static function is declared but not defined or a non-inline static function is unused */
-       bool unused_label:1;                  /* Warn whenever a label is declared but not used */
-       bool unused_parameter:1;              /* Warn whenever a function parameter is unused aside from its declaration */
-       bool unused_variable:1;               /* Warn whenever a local variable or non-constant static variable is unused aside from its declaration */
-#endif
-       bool unused_value:1;                  /* Warn whenever a statement computes a result that is explicitly not used */
-#if 0 // TODO
-       bool write_strings:1;                 /* Give string constants the type 'const char[LENGTH]' so that copying the address of one into a 'char *' pointer will get a warning */
+       bool write_strings:1;                 /**< Give string constants the type 'const char[LENGTH]' so that copying the address of one into a 'char *' pointer will get a warning */
 #endif
 } warning_t;