Implement and document -Wshadow-local.
[cparser] / warning.c
1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 #include "help.h"
24
25 warning_t warning = {
26         .other                               = true,
27
28         .address                             = true,
29         .aggregate_return                    = false,
30         .attribute                           = true,
31         .cast_qual                           = false,
32         .char_subscripts                     = true,
33         .comment                             = false,
34         .conversion                          = false,
35         .declaration_after_statement         = false,
36         .deprecated_declarations             = true,
37         .div_by_zero                         = true,
38         .empty_statement                     = false,
39         .error_implicit_function_declaration = false,
40         .fatal_errors                        = false,
41         .float_equal                         = false,
42         .format                              = true,
43         .implicit_function_declaration       = true,
44         .implicit_int                        = true,
45         .init_self                           = true,
46         .long_long                           = false,
47         .main                                = true,
48         .missing_declarations                = false,
49         .missing_noreturn                    = false,
50         .missing_prototypes                  = false,
51         .multichar                           = true,
52         .nested_externs                      = false,
53         .nonnull                             = true,
54         .old_style_definition                = false,
55         .packed                              = false,
56         .padded                              = false,
57         .parentheses                         = false,
58         .pointer_arith                       = true,
59         .redundant_decls                     = true,
60         .return_type                         = true,
61         .s_are_errors                        = false,
62         .shadow                              = false,
63         .shadow_local                        = false,
64         .sign_compare                        = false,
65         .strict_prototypes                   = true,
66         .switch_default                      = false,
67         .switch_enum                         = false,
68         .traditional                         = false,
69         .uninitialized                       = true,
70         .unknown_pragmas                     = true,
71         .unreachable_code                    = false,
72         .unused_function                     = false,
73         .unused_label                        = false,
74         .unused_parameter                    = false,
75         .unused_value                        = true,
76         .unused_variable                     = false,
77         .write_strings                       = false
78 };
79
80 void print_warning_opt_help(void)
81 {
82         /* TODO: write explanations */
83         put_help("-Waddress",                       "");
84         put_help("-Wall",                           "");
85         put_help("-Waggregate-return",              "");
86         put_help("-Wattribute",                     "");
87         put_help("-Wcast-qual",                     "");
88         put_help("-Wchar-subscripts",               "");
89         put_help("-Wcomment",                       "");
90         put_help("-Wconversion",                    "");
91         put_help("-Wdeclaration-after-statement",   "");
92         put_help("-Wdeprecated-declarations",       "");
93         put_help("-Wdiv-by-zero",                   "");
94         put_help("-Wempty-statement",               "");
95         put_help("-Werror",                         "warnings are errors");
96         put_help("-Werror-implicit-function-declaration", "");
97         put_help("-Wextra",                         "");
98         put_help("-Wfatal-errors",                  "");
99         put_help("-Wfloat-equal",                   "");
100         put_help("-Wformat",                        "");
101         put_help("-Wimplicit",                      "");
102         put_help("-Wimplicit-function-declaration", "");
103         put_help("-Wimplicit-int",                  "");
104         put_help("-Winit-self",                     "");
105         put_help("-Wlong-long",                     "");
106         put_help("-Wmain",                          "");
107         put_help("-Wmissing-declarations",          "");
108         put_help("-Wmissing-noreturn",              "");
109         put_help("-Wmissing-prototypes",            "");
110         put_help("-Wmultichar",                     "");
111         put_help("-Wnested-externs",                "");
112         put_help("-Wnonnull",                       "");
113         put_help("-Wold-style-definition",          "");
114         put_help("-Wparentheses",                   "");
115         put_help("-Wpacked",                        "");
116         put_help("-Wpadded",                        "");
117         put_help("-Wparentheses",                   "");
118         put_help("-Wpointer-arith",                 "");
119         put_help("-Wredundant-decls",               "");
120         put_help("-Wreturn-type",                   "");
121         put_help("-Wshadow",                        "");
122         put_help("-Wshadow-local",                  "");
123         put_help("-Wsign-compare",                  "");
124         put_help("-Wstrict-prototypes",             "");
125         put_help("-Wswitch-default",                "");
126         put_help("-Wswitch-enum",                   "");
127         put_help("-Wtraditional",                   "");
128         put_help("-Wuninitialized",                 "");
129         put_help("-Wunknown-pragmas",               "");
130         put_help("-Wunreachable-code",              "");
131         put_help("-Wunused",                        "");
132         put_help("-Wunused-function",               "");
133         put_help("-Wunused-label",                  "");
134         put_help("-Wunused-parameter",              "");
135         put_help("-Wunused-value",                  "");
136         put_help("-Wunused-variable",               "");
137         put_help("-Wwrite-strings",                 "");
138 }
139
140 void set_warning_opt(const char *const opt)
141 {
142         const char* s = opt;
143
144         bool state = true;
145
146         /* "no-" prefix */
147         if (s[0] == 'n' && s[1] == 'o' && s[2] == '-') {
148                 s += 3;
149                 state = false;
150         }
151
152         if (s[0] == '\0') { // -W is an alias for -Wextra
153                 goto extra;
154         }
155 #define OPTX(x)   else if (strcmp(s, x) == 0)
156 #define SET(y)    (void)(warning.y = state)
157 #define OPT(x, y) OPTX(x) SET(y)
158         OPTX("all") {
159                 /* Note: this switched on a lot more warnings than gcc's -Wall */
160                 SET(other);
161
162                 SET(address);
163                 SET(attribute);
164                 SET(char_subscripts);
165                 SET(comment);
166                 SET(empty_statement);
167                 SET(format);
168                 SET(implicit_function_declaration);
169                 SET(implicit_int);
170                 SET(init_self);
171                 SET(main);
172                 SET(nonnull);
173                 SET(parentheses);
174                 SET(pointer_arith);
175                 SET(redundant_decls);
176                 SET(return_type);
177                 SET(shadow_local);
178                 SET(sign_compare);
179                 SET(strict_prototypes);
180                 SET(switch_enum);
181                 SET(uninitialized);
182                 SET(unknown_pragmas);
183                 SET(unreachable_code);
184                 SET(unused_function);
185                 SET(unused_label);
186                 SET(unused_parameter);
187                 SET(unused_value);
188                 SET(unused_variable);
189         }
190         OPT("address",                             address);
191         OPT("aggregate-return",                    aggregate_return);
192         OPT("attribute",                           attribute);
193         OPT("cast-qual",                           cast_qual);
194         OPT("char-subscripts",                     char_subscripts);
195         OPT("comment",                             comment);
196         OPT("conversion",                          conversion);
197         OPT("declaration-after-statement",         declaration_after_statement);
198         OPT("deprecated-declarations",             deprecated_declarations);
199         OPT("div-by-zero",                         div_by_zero);
200         OPT("empty-statement",                     empty_statement);
201         OPT("error",                               s_are_errors);
202         OPT("error-implicit-function-declaration", error_implicit_function_declaration);
203         OPTX("extra") {
204 extra:
205                 /* TODO */
206                 // TODO SET(function_end_without_return);
207                 SET(empty_statement);
208                 // TODO SET(incomplete_aggregate_init);
209                 // TODO SET(missing_field_initializers);
210                 // TODO SET(pointless_comparison);
211                 SET(shadow);
212                 SET(unused_parameter);
213                 SET(unused_value);
214         }
215         OPT("fatal-errors",                        fatal_errors);
216         OPT("float-equal",                         float_equal);
217         OPTX("format") {
218                 SET(format);
219                 SET(nonnull);
220         }
221         OPTX("implicit") {
222                 SET(implicit_function_declaration);
223                 SET(implicit_int);
224         }
225         OPT("implicit-function-declaration",       implicit_function_declaration);
226         OPT("implicit-int",                        implicit_int);
227         OPT("init-self",                           init_self);
228         OPT("long-long",                           long_long);
229         OPT("main",                                main);
230         OPT("missing-declarations",                missing_declarations);
231         OPT("missing-noreturn",                    missing_noreturn);
232         OPT("missing-prototypes",                  missing_prototypes);
233         OPT("multichar",                           multichar);
234         OPT("nested-externs",                      nested_externs);
235         OPT("nonnull",                             nonnull);
236         OPT("old-style-definition",                old_style_definition);
237         OPT("packed",                              packed);
238         OPT("padded",                              padded);
239         OPT("parentheses",                         parentheses);
240         OPT("pointer-arith",                       pointer_arith);
241         OPT("redundant-decls",                     redundant_decls);
242         OPT("return-type",                         return_type);
243         OPT("shadow",                              shadow);
244         OPT("shadow-local",                        shadow_local);
245         OPT("sign-compare",                        sign_compare);
246         OPT("strict-prototypes",                   strict_prototypes);
247         OPT("switch-default",                      switch_default);
248         OPT("switch-enum",                         switch_enum);
249         OPT("traditional",                         traditional);
250         OPT("uninitialized",                       uninitialized);
251         OPT("unknown-pragmas",                     unknown_pragmas);
252         OPT("unreachable-code",                    unreachable_code);
253         OPTX("unused") {
254                 SET(unused_function);
255                 SET(unused_label);
256                 SET(unused_parameter);
257                 SET(unused_value);
258                 SET(unused_variable);
259         }
260         OPT("unused-function",                     unused_function);
261         OPT("unused-label",                        unused_label);
262         OPT("unused-parameter",                    unused_parameter);
263         OPT("unused-value",                        unused_value);
264         OPT("unused-variable",                     unused_variable);
265         OPT("write-strings",                       write_strings);
266 #undef OPT
267 #undef SET
268 #undef OPT_X
269         else {
270                 fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
271         }
272 }