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