Make warning switch parsing compatible to GCC: -W[no-][error=]warning.
authorChristoph Mallon <christoph.mallon@gmx.de>
Tue, 21 Jun 2011 20:22:54 +0000 (22:22 +0200)
committerChristoph Mallon <christoph.mallon@gmx.de>
Tue, 21 Jun 2011 20:26:29 +0000 (22:26 +0200)
cparser.1
diagnostic.c
warning.c
warning.h

index 1b7a266..450b87c 100644 (file)
--- a/cparser.1
+++ b/cparser.1
@@ -1,5 +1,5 @@
 .\" Please adjust this date whenever revising the manpage.
-.Dd December 21, 2008
+.Dd June 21, 2011
 .Dt CPARSER 1
 .Sh NAME
 .Nm cparser
@@ -14,7 +14,7 @@
 .Op Fl - Ns Oo Cm no- Oc Ns Cm ms
 .Op Fl g
 .Op Fl O Ns Ar level
-.Op Fl W Ns Oo Cm no- Ns | Ns Cm error- Oc Ns Ar warn
+.Op Fl W Ns Oo Cm no- Oc Ns Oo Cm error= Oc Ns Ar warning
 .Op Fl w
 .Op Fl I Ar dir
 .Op Fl L Ar dir
@@ -91,14 +91,17 @@ format.
 .\" TODO expand
 Select the optimization level.
 Sensible values are between 0 and 4, inclusive.
-.It Fl W Ns Oo Cm no- Ns | Ns Cm error- Oc Ns Ar warn
 .\" TODO expand
-Disable/enable a specific warning.
-Every warning option has a corresponding
-.Cm no-
-switch to deactivate it and an
-.Cm error-
-switch to turn it into an error.
+.It Fl W Ns Ar warning
+Enable the specified warning.
+.It Fl Wno- Ns Ar warning
+Disable the specified warning.
+.It Fl Werror= Ns Ar warning
+Enable the specified warning and turn it into an error.
+.It Fl Wno-error= Ns Ar warning
+Force the specified warning to only be a warning, even in the presence of
+.Fl Werror .
+This neither enables nor disables the warning itself.
 .It Fl Waddress
 Warn about suspicious use of addresses, like using the address of a function or variable as boolean condition or comparing with the address of a string literal.
 .It Fl Waggregate-return
index 6376abb..5174308 100644 (file)
@@ -245,17 +245,15 @@ void warningf(warning_t const warn, source_position_t const* pos, char const *co
        va_list ap;
        va_start(ap, fmt);
        warning_switch_t const *const s = get_warn_switch(warn);
-       switch (s->level) {
-               case WARN_LEVEL_OFF:
-                       break;
-
+       switch (s->state) {
                        char const* kind;
-               case WARN_LEVEL_ON:
+               case WARN_STATE_ON:
                        if (is_warn_on(WARN_ERROR)) {
-               case WARN_LEVEL_ERROR:
+               case WARN_STATE_ON | WARN_STATE_ERROR:
                                ++error_count;
                                kind = "error";
                        } else {
+               case WARN_STATE_ON | WARN_STATE_NO_ERROR:
                                ++warning_count;
                                kind = "warning";
                        }
@@ -264,7 +262,7 @@ void warningf(warning_t const warn, source_position_t const* pos, char const *co
                        break;
 
                default:
-                       panic("invalid warning level");
+                       break;
        }
        va_end(ap);
 }
index 65e39dd..f8b3807 100644 (file)
--- a/warning.c
+++ b/warning.c
 #include "help.h"
 
 static warning_switch_t warning[] = {
-       [WARN_ADDRESS]                       = { WARN_LEVEL_ON,  "address"                       },
-       [WARN_AGGREGATE_RETURN]              = { WARN_LEVEL_OFF, "aggregate-return"              },
-       [WARN_ATTRIBUTE]                     = { WARN_LEVEL_ON,  "attribute"                     },
-       [WARN_CAST_QUAL]                     = { WARN_LEVEL_OFF, "cast-qual",                    },
-       [WARN_CHAR_SUBSCRIPTS]               = { WARN_LEVEL_ON,  "char-subscripts",              },
-       [WARN_COMMENT]                       = { WARN_LEVEL_OFF, "comment",                      },
-       [WARN_CONVERSION]                    = { WARN_LEVEL_OFF, "conversion",                   },
-       [WARN_DECLARATION_AFTER_STATEMENT]   = { WARN_LEVEL_OFF, "declaration-after-statement",  },
-       [WARN_DEPRECATED_DECLARATIONS]       = { WARN_LEVEL_ON,  "deprecated-declarations",      },
-       [WARN_DIV_BY_ZERO]                   = { WARN_LEVEL_ON,  "div-by-zero",                  },
-       [WARN_EMPTY_STATEMENT]               = { WARN_LEVEL_OFF, "empty-statement",              },
-       [WARN_ERROR]                         = { WARN_LEVEL_OFF, "error"                         },
-       [WARN_FATAL_ERRORS]                  = { WARN_LEVEL_OFF, "fatal-errors"                  },
-       [WARN_FLOAT_EQUAL]                   = { WARN_LEVEL_OFF, "float-equal",                  },
-       [WARN_FORMAT]                        = { WARN_LEVEL_ON,  "format"                        },
-       [WARN_IMPLICIT_FUNCTION_DECLARATION] = { WARN_LEVEL_ON,  "implicit-function-declaration" },
-       [WARN_IMPLICIT_INT]                  = { WARN_LEVEL_ON,  "implicit-int"                  },
-       [WARN_INIT_SELF]                     = { WARN_LEVEL_ON,  "init-self",                    },
-       [WARN_LONG_LONG]                     = { WARN_LEVEL_OFF, "long-long"                     },
-       [WARN_MAIN]                          = { WARN_LEVEL_ON,  "main",                         },
-       [WARN_MISSING_DECLARATIONS]          = { WARN_LEVEL_OFF, "missing-declarations",         },
-       [WARN_MISSING_NORETURN]              = { WARN_LEVEL_OFF, "missing-noreturn",             },
-       [WARN_MISSING_PROTOTYPES]            = { WARN_LEVEL_OFF, "missing-prototypes",           },
-       [WARN_MULTICHAR]                     = { WARN_LEVEL_ON,  "multichar",                    },
-       [WARN_NESTED_EXTERNS]                = { WARN_LEVEL_OFF, "nested-externs"                },
-       [WARN_NONNULL]                       = { WARN_LEVEL_ON,  "nonnull",                      },
-       [WARN_OLD_STYLE_DEFINITION]          = { WARN_LEVEL_OFF, "old-style-definition",         },
-       [WARN_OTHER]                         = { WARN_LEVEL_ON,  "other"                         },
-       [WARN_PACKED]                        = { WARN_LEVEL_OFF, "packed",                       },
-       [WARN_PADDED]                        = { WARN_LEVEL_OFF, "padded",                       },
-       [WARN_PARENTHESES]                   = { WARN_LEVEL_OFF, "parentheses",                  },
-       [WARN_POINTER_ARITH]                 = { WARN_LEVEL_ON,  "pointer-arith",                },
-       [WARN_REDUNDANT_DECLS]               = { WARN_LEVEL_ON,  "redundant-decls",              },
-       [WARN_RETURN_TYPE]                   = { WARN_LEVEL_ON,  "return-type",                  },
-       [WARN_SHADOW]                        = { WARN_LEVEL_OFF, "shadow",                       },
-       [WARN_SHADOW_LOCAL]                  = { WARN_LEVEL_OFF, "shadow-local",                 },
-       [WARN_SIGN_COMPARE]                  = { WARN_LEVEL_OFF, "sign-compare",                 },
-       [WARN_STRICT_PROTOTYPES]             = { WARN_LEVEL_ON,  "strict-prototypes"             },
-       [WARN_SWITCH_DEFAULT]                = { WARN_LEVEL_OFF, "switch-default",               },
-       [WARN_SWITCH_ENUM]                   = { WARN_LEVEL_OFF, "switch-enum",                  },
-       [WARN_TRADITIONAL]                   = { WARN_LEVEL_OFF, "traditional"                   },
-       [WARN_UNINITIALIZED]                 = { WARN_LEVEL_ON,  "uninitialized",                },
-       [WARN_UNKNOWN_PRAGMAS]               = { WARN_LEVEL_ON,  "unknown-pragmas",              },
-       [WARN_UNREACHABLE_CODE]              = { WARN_LEVEL_OFF, "unreachable-code"              },
-       [WARN_UNUSED_FUNCTION]               = { WARN_LEVEL_OFF, "unused-function",              },
-       [WARN_UNUSED_LABEL]                  = { WARN_LEVEL_OFF, "unused-label",                 },
-       [WARN_UNUSED_PARAMETER]              = { WARN_LEVEL_OFF, "unused-parameter",             },
-       [WARN_UNUSED_VALUE]                  = { WARN_LEVEL_ON,  "unused-value",                 },
-       [WARN_UNUSED_VARIABLE]               = { WARN_LEVEL_OFF, "unused-variable",              },
-       [WARN_WRITE_STRINGS]                 = { WARN_LEVEL_OFF, "write-strings",                },
+       [WARN_ADDRESS]                       = { WARN_STATE_ON,   "address"                       },
+       [WARN_AGGREGATE_RETURN]              = { WARN_STATE_NONE, "aggregate-return"              },
+       [WARN_ATTRIBUTE]                     = { WARN_STATE_ON,   "attribute"                     },
+       [WARN_CAST_QUAL]                     = { WARN_STATE_NONE, "cast-qual",                    },
+       [WARN_CHAR_SUBSCRIPTS]               = { WARN_STATE_ON,   "char-subscripts",              },
+       [WARN_COMMENT]                       = { WARN_STATE_NONE, "comment",                      },
+       [WARN_CONVERSION]                    = { WARN_STATE_NONE, "conversion",                   },
+       [WARN_DECLARATION_AFTER_STATEMENT]   = { WARN_STATE_NONE, "declaration-after-statement",  },
+       [WARN_DEPRECATED_DECLARATIONS]       = { WARN_STATE_ON,   "deprecated-declarations",      },
+       [WARN_DIV_BY_ZERO]                   = { WARN_STATE_ON,   "div-by-zero",                  },
+       [WARN_EMPTY_STATEMENT]               = { WARN_STATE_NONE, "empty-statement",              },
+       [WARN_ERROR]                         = { WARN_STATE_NONE, "error"                         },
+       [WARN_FATAL_ERRORS]                  = { WARN_STATE_NONE, "fatal-errors"                  },
+       [WARN_FLOAT_EQUAL]                   = { WARN_STATE_NONE, "float-equal",                  },
+       [WARN_FORMAT]                        = { WARN_STATE_ON,   "format"                        },
+       [WARN_IMPLICIT_FUNCTION_DECLARATION] = { WARN_STATE_ON,   "implicit-function-declaration" },
+       [WARN_IMPLICIT_INT]                  = { WARN_STATE_ON,   "implicit-int"                  },
+       [WARN_INIT_SELF]                     = { WARN_STATE_ON,   "init-self",                    },
+       [WARN_LONG_LONG]                     = { WARN_STATE_NONE, "long-long"                     },
+       [WARN_MAIN]                          = { WARN_STATE_ON,   "main",                         },
+       [WARN_MISSING_DECLARATIONS]          = { WARN_STATE_NONE, "missing-declarations",         },
+       [WARN_MISSING_NORETURN]              = { WARN_STATE_NONE, "missing-noreturn",             },
+       [WARN_MISSING_PROTOTYPES]            = { WARN_STATE_NONE, "missing-prototypes",           },
+       [WARN_MULTICHAR]                     = { WARN_STATE_ON,   "multichar",                    },
+       [WARN_NESTED_EXTERNS]                = { WARN_STATE_NONE, "nested-externs"                },
+       [WARN_NONNULL]                       = { WARN_STATE_ON,   "nonnull",                      },
+       [WARN_OLD_STYLE_DEFINITION]          = { WARN_STATE_NONE, "old-style-definition",         },
+       [WARN_OTHER]                         = { WARN_STATE_ON,   "other"                         },
+       [WARN_PACKED]                        = { WARN_STATE_NONE, "packed",                       },
+       [WARN_PADDED]                        = { WARN_STATE_NONE, "padded",                       },
+       [WARN_PARENTHESES]                   = { WARN_STATE_NONE, "parentheses",                  },
+       [WARN_POINTER_ARITH]                 = { WARN_STATE_ON,   "pointer-arith",                },
+       [WARN_REDUNDANT_DECLS]               = { WARN_STATE_ON,   "redundant-decls",              },
+       [WARN_RETURN_TYPE]                   = { WARN_STATE_ON,   "return-type",                  },
+       [WARN_SHADOW]                        = { WARN_STATE_NONE, "shadow",                       },
+       [WARN_SHADOW_LOCAL]                  = { WARN_STATE_NONE, "shadow-local",                 },
+       [WARN_SIGN_COMPARE]                  = { WARN_STATE_NONE, "sign-compare",                 },
+       [WARN_STRICT_PROTOTYPES]             = { WARN_STATE_ON,   "strict-prototypes"             },
+       [WARN_SWITCH_DEFAULT]                = { WARN_STATE_NONE, "switch-default",               },
+       [WARN_SWITCH_ENUM]                   = { WARN_STATE_NONE, "switch-enum",                  },
+       [WARN_TRADITIONAL]                   = { WARN_STATE_NONE, "traditional"                   },
+       [WARN_UNINITIALIZED]                 = { WARN_STATE_ON,   "uninitialized",                },
+       [WARN_UNKNOWN_PRAGMAS]               = { WARN_STATE_ON,   "unknown-pragmas",              },
+       [WARN_UNREACHABLE_CODE]              = { WARN_STATE_NONE, "unreachable-code"              },
+       [WARN_UNUSED_FUNCTION]               = { WARN_STATE_NONE, "unused-function",              },
+       [WARN_UNUSED_LABEL]                  = { WARN_STATE_NONE, "unused-label",                 },
+       [WARN_UNUSED_PARAMETER]              = { WARN_STATE_NONE, "unused-parameter",             },
+       [WARN_UNUSED_VALUE]                  = { WARN_STATE_ON,   "unused-value",                 },
+       [WARN_UNUSED_VARIABLE]               = { WARN_STATE_NONE, "unused-variable",              },
+       [WARN_WRITE_STRINGS]                 = { WARN_STATE_NONE, "write-strings",                },
 };
 
 warning_switch_t const *get_warn_switch(warning_t const w)
@@ -95,16 +95,28 @@ void print_warning_opt_help(void)
 
 void set_warning_opt(const char *const opt)
 {
-       const char* s = opt;
+       /* Process prefixes: -W[no-][error=] */
+       char const *s     = opt;
+       bool const  no    = strncmp(s, "no-",    3) == 0 ? s += 3, true : false;
+       bool const  error = strncmp(s, "error=", 6) == 0 ? s += 6, true : false;
 
-       warn_level_t const level =
-               strncmp(s, "no-",    3) == 0 ? s += 3, WARN_LEVEL_OFF   : /* "no-" prefix */
-               strncmp(s, "error-", 6) == 0 ? s += 6, WARN_LEVEL_ERROR : /* "error-" prefix */
-               WARN_LEVEL_ON;
+       warn_state_t on  = WARN_STATE_NONE;
+       warn_state_t off = WARN_STATE_NONE;
+       if (!no || !error)
+               on |= WARN_STATE_ON;
+       if (error) {
+               on  |= WARN_STATE_ERROR;
+               off |= WARN_STATE_NO_ERROR;
+       }
+       if (no) {
+               warn_state_t const tmp = on;
+               on  = off;
+               off = tmp;
+       }
 
        for (warning_switch_t* i = warning; i != endof(warning); ++i) {
                if (strcmp(i->name, s) == 0) {
-                       i->level = level;
+                       i->state = (i->state & ~off) | on;
                        return;
                }
        }
@@ -113,7 +125,7 @@ void set_warning_opt(const char *const opt)
                goto extra;
        }
 #define OPTX(x)   else if (strcmp(s, x) == 0)
-#define SET(y)    (void)(warning[y].level = level)
+#define SET(y)    (void)(warning[y].state = (warning[y].state & ~off) | on)
        OPTX("all") {
                /* Note: this switched on a lot more warnings than gcc's -Wall */
                SET(WARN_ADDRESS);
@@ -171,6 +183,11 @@ extra:
        }
 #undef SET
 #undef OPT_X
+       else if (strcmp(opt /* sic */, "error-implicit-function-declaration") == 0) {
+               /* GCC legacy: This way it only can be activated. */
+               warning[WARN_IMPLICIT_FUNCTION_DECLARATION].state = WARN_STATE_ON | WARN_STATE_ERROR;
+               return;
+       }
        else {
                fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
        }
@@ -181,7 +198,7 @@ void disable_all_warnings(void)
        for (warning_switch_t* i = warning; i != endof(warning); ++i) {
                if (i != &warning[WARN_ERROR] &&
                    i != &warning[WARN_FATAL_ERRORS]) {
-                       i->level = WARN_LEVEL_OFF;
+                       i->state &= ~WARN_STATE_ON;
                }
        }
 }
index 68ba6dd..1d3acf8 100644 (file)
--- a/warning.h
+++ b/warning.h
@@ -114,14 +114,15 @@ typedef enum warning_t {
        WARN_WRITE_STRINGS,                 /**< Give string constants the type 'const char[LENGTH]' so that copying the address of one into a 'char *' pointer will get a warning */
 } warning_t;
 
-typedef enum warn_level_t {
-       WARN_LEVEL_OFF,
-       WARN_LEVEL_ON,
-       WARN_LEVEL_ERROR
-} warn_level_t;
+typedef enum warn_state_t {
+       WARN_STATE_NONE     = 0,
+       WARN_STATE_ON       = 1U << 0,
+       WARN_STATE_ERROR    = 1U << 1,
+       WARN_STATE_NO_ERROR = 1U << 2
+} warn_state_t;
 
 typedef struct warning_switch_t {
-       warn_level_t      level;
+       warn_state_t      state;
        char const* const name;
 } warning_switch_t;
 
@@ -129,7 +130,7 @@ warning_switch_t const *get_warn_switch(warning_t);
 
 static inline bool is_warn_on(warning_t const warn)
 {
-       return get_warn_switch(warn)->level != WARN_LEVEL_OFF;
+       return get_warn_switch(warn)->state & WARN_STATE_ON;
 }
 
 #endif