Implement -Wswitch-default.
authorChristoph Mallon <christoph.mallon@gmx.de>
Fri, 14 Dec 2007 09:44:12 +0000 (09:44 +0000)
committerChristoph Mallon <christoph.mallon@gmx.de>
Fri, 14 Dec 2007 09:44:12 +0000 (09:44 +0000)
[r18737]

parser.c
warning.c
warning.h

index 65748f1..621be2d 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -5075,6 +5075,10 @@ static statement_t *parse_switch(void)
        statement->body = parse_statement();
        current_switch  = rem;
 
+       if (warning.switch_default && find_default_label(statement) == NULL) {
+               warningf(statement->statement.source_position, "switch has no default case");
+       }
+
        return (statement_t*) statement;
 }
 
index 7a718cd..963941b 100644 (file)
--- a/warning.c
+++ b/warning.c
@@ -31,6 +31,7 @@ void set_warning_opt(const char *const opt)
        OPT("missing-prototypes",            missing_prototypes)
        OPT("redundant-decls",               redundant_decls)
        OPT("strict-prototypes",             strict_prototypes)
+       OPT("switch-default",                switch_default)
 #if 0
        OPTX("unused") {
                SET(unused_function)
@@ -52,11 +53,14 @@ void set_warning_opt(const char *const opt)
 warning_t warning = {
        .char_subscripts               = true,
        .check_format                  = true,
+       .fatal_errors                  = false,
        .implicit_function_declaration = true,
        .implicit_int                  = true,
        .missing_declarations          = true,
        .missing_prototypes            = true,
-       .strict_prototypes             = true,
        .redundant_decls               = true,
+       .s_are_errors                  = false,
+       .strict_prototypes             = true,
+       .switch_default                = false,
        .unused_value                  = true
 };
index ab2acef..327cb6d 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -59,8 +59,8 @@ typedef struct warning_t {
        bool strict_aliasing:1;               /* Warn about code which might break the strict aliasing rules that the compiler is using for optimization. */
 #endif
        bool strict_prototypes:1;             /* warn if a function declaration has an unspecified parameter list */
-#if 0 // TODO
        bool switch_default:1;                /* Warn whenever a 'switch' statement does not have a 'default' case */
+#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 */