- implemented -Wswitch-enum
[cparser] / warning.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18  * 02111-1307, USA.
19  */
20 #include <stdio.h>
21 #include <string.h>
22 #include "warning.h"
23
24 warning_t warning = {
25         .attribute                     = true,
26         .char_subscripts               = true,
27         .declaration_after_statement   = false,
28         .deprecated_declarations       = true,
29         .empty_statement               = false,
30         .fatal_errors                  = false,
31         .float_equal                   = false,
32         .format                        = true,
33         .implicit_function_declaration = true,
34         .implicit_int                  = true,
35         .long_long                     = false,
36         .main                          = true,
37         .missing_declarations          = false,
38         .missing_noreturn              = false,
39         .missing_prototypes            = false,
40         .multichar                     = true,
41         .nested_externs                = false,
42         .nonnull                       = true,
43         .redundant_decls               = true,
44         .return_type                   = true,
45         .s_are_errors                  = false,
46         .shadow                        = false,
47         .sign_compare                  = false,
48         .strict_prototypes             = true,
49         .switch_default                = false,
50         .switch_enum                   = false,
51         .unknown_pragmas               = true,
52         .unreachable_code              = false,
53         .unused_function               = false,
54         .unused_label                  = false,
55         .unused_parameter              = false,
56         .unused_value                  = true,
57         .unused_variable               = false
58 };
59
60 void set_warning_opt(const char *const opt)
61 {
62         const char* s = opt;
63
64         bool state = true;
65
66         /* "no-" prefix */
67         if (s[0] == 'n' && s[1] == 'o' && s[2] == '-') {
68                 s += 3;
69                 state = false;
70         }
71
72         if (0) {}
73 #define OPTX(x)   else if (strcmp(s, x) == 0)
74 #define SET(y)    (void)(warning.y = state)
75 #define OPT(x, y) OPTX(x) SET(y)
76         OPTX("all") {
77                 /* Note: this switched on a lot of more warnings than gcc's -Wall */
78                 SET(attribute);
79                 SET(char_subscripts);
80                 SET(empty_statement);
81                 SET(format);
82                 SET(implicit_function_declaration);
83                 SET(implicit_int);
84                 SET(main);
85                 SET(nonnull);
86                 SET(redundant_decls);
87                 SET(return_type);
88                 SET(shadow);
89                 SET(sign_compare);
90                 SET(strict_prototypes);
91                 SET(unknown_pragmas);
92                 SET(unreachable_code);
93                 SET(unused_function);
94                 SET(unused_label);
95                 SET(unused_parameter);
96                 SET(unused_value);
97                 SET(unused_variable);
98                 SET(switch_enum);
99         }
100         OPT("attribute",                     attribute);
101         OPT("char-subscripts",               char_subscripts);
102         OPT("declaration-after-statement",   declaration_after_statement);
103         OPT("deprecated-declarations",       deprecated_declarations);
104         OPT("empty-statement",               empty_statement);
105         OPT("error",                         s_are_errors);
106         OPTX("extra") {
107                 /* TODO */
108                 // TODO SET(function_end_without_return);
109                 SET(empty_statement);
110                 // TODO SET(incomplete_aggregate_init);
111                 // TODO SET(pointless_comparison);
112                 SET(unused_parameter);
113                 SET(unused_value);
114         }
115         OPT("fatal-errors",                  fatal_errors);
116         OPT("float-equal",                   float_equal);
117         OPTX("format") {
118                 SET(format);
119                 SET(nonnull);
120         }
121         OPTX("implicit") {
122                 SET(implicit_function_declaration);
123                 SET(implicit_int);
124         }
125         OPT("implicit-function-declaration", implicit_function_declaration);
126         OPT("implicit-int",                  implicit_int);
127         OPT("long-long",                     long_long);
128         OPT("main",                          main);
129         OPT("missing-declarations",          missing_declarations);
130         OPT("missing-noreturn",              missing_noreturn);
131         OPT("missing-prototypes",            missing_prototypes);
132         OPT("multichar",                     multichar);
133         OPT("nested-externs",                nested_externs);
134         OPT("nonnull",                       nonnull);
135         OPT("redundant-decls",               redundant_decls);
136         OPT("return-type",                   return_type);
137         OPT("shadow",                        shadow);
138         OPT("sign-compare",                  sign_compare);
139         OPT("strict-prototypes",             strict_prototypes);
140         OPT("switch-default",                switch_default);
141         OPT("switch-enum",                   switch_enum);
142         OPT("unknown-pragmas",               unknown_pragmas);
143         OPT("unreachable-code",              unreachable_code);
144         OPTX("unused") {
145                 SET(unused_function);
146                 SET(unused_label);
147                 SET(unused_parameter);
148                 SET(unused_value);
149                 SET(unused_variable);
150         }
151         OPT("unused-function",               unused_function);
152         OPT("unused-label",                  unused_label);
153         OPT("unused-parameter",              unused_parameter);
154         OPT("unused-value",                  unused_value);
155         OPT("unused-variable",               unused_variable);
156 #undef OPT
157 #undef SET
158 #undef OPT_X
159         else {
160                 fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
161         }
162 }