make the uninitialized warning depend on the -wuninitialized switch
[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
24 warning_t warning = {
25         .other                               = true,
26
27         .address                             = true,
28         .aggregate_return                    = false,
29         .attribute                           = true,
30         .cast_qual                           = false,
31         .char_subscripts                     = true,
32         .comment                             = false,
33         .conversion                          = false,
34         .declaration_after_statement         = false,
35         .deprecated_declarations             = true,
36         .div_by_zero                         = true,
37         .empty_statement                     = false,
38         .error_implicit_function_declaration = false,
39         .fatal_errors                        = false,
40         .float_equal                         = false,
41         .format                              = true,
42         .implicit_function_declaration       = true,
43         .implicit_int                        = true,
44         .init_self                           = true,
45         .long_long                           = false,
46         .main                                = true,
47         .missing_declarations                = false,
48         .missing_noreturn                    = false,
49         .missing_prototypes                  = false,
50         .multichar                           = true,
51         .nested_externs                      = false,
52         .nonnull                             = true,
53         .old_style_definition                = false,
54         .packed                              = false,
55         .padded                              = false,
56         .parentheses                         = false,
57         .pointer_arith                       = true,
58         .redundant_decls                     = true,
59         .return_type                         = true,
60         .s_are_errors                        = false,
61         .shadow                              = false,
62         .sign_compare                        = false,
63         .strict_prototypes                   = true,
64         .switch_default                      = false,
65         .switch_enum                         = false,
66         .traditional                         = false,
67         .uninitialized                       = true,
68         .unknown_pragmas                     = true,
69         .unreachable_code                    = false,
70         .unused_function                     = false,
71         .unused_label                        = false,
72         .unused_parameter                    = false,
73         .unused_value                        = true,
74         .unused_variable                     = false,
75         .write_strings                       = false
76 };
77
78 void set_warning_opt(const char *const opt)
79 {
80         const char* s = opt;
81
82         bool state = true;
83
84         /* "no-" prefix */
85         if (s[0] == 'n' && s[1] == 'o' && s[2] == '-') {
86                 s += 3;
87                 state = false;
88         }
89
90         if (0) {}
91 #define OPTX(x)   else if (strcmp(s, x) == 0)
92 #define SET(y)    (void)(warning.y = state)
93 #define OPT(x, y) OPTX(x) SET(y)
94         OPTX("all") {
95                 /* Note: this switched on a lot more warnings than gcc's -Wall */
96                 SET(other);
97
98                 SET(address);
99                 SET(attribute);
100                 SET(char_subscripts);
101                 SET(comment);
102                 SET(empty_statement);
103                 SET(format);
104                 SET(implicit_function_declaration);
105                 SET(implicit_int);
106                 SET(init_self);
107                 SET(main);
108                 SET(nonnull);
109                 SET(parentheses);
110                 SET(pointer_arith);
111                 SET(redundant_decls);
112                 SET(return_type);
113                 SET(shadow);
114                 SET(sign_compare);
115                 SET(strict_prototypes);
116                 SET(switch_enum);
117                 SET(uninitialized);
118                 SET(unknown_pragmas);
119                 SET(unreachable_code);
120                 SET(unused_function);
121                 SET(unused_label);
122                 SET(unused_parameter);
123                 SET(unused_value);
124                 SET(unused_variable);
125         }
126         OPT("address",                             address);
127         OPT("aggregate-return",                    aggregate_return);
128         OPT("attribute",                           attribute);
129         OPT("cast-qual",                           cast_qual);
130         OPT("char-subscripts",                     char_subscripts);
131         OPT("comment",                             comment);
132         OPT("conversion",                          conversion);
133         OPT("declaration-after-statement",         declaration_after_statement);
134         OPT("deprecated-declarations",             deprecated_declarations);
135         OPT("div-by-zero",                         div_by_zero);
136         OPT("empty-statement",                     empty_statement);
137         OPT("error",                               s_are_errors);
138         OPT("error-implicit-function-declaration", error_implicit_function_declaration);
139         OPTX("extra") {
140                 /* TODO */
141                 // TODO SET(function_end_without_return);
142                 SET(empty_statement);
143                 // TODO SET(incomplete_aggregate_init);
144                 // TODO SET(missing_field_initializers);
145                 // TODO SET(pointless_comparison);
146                 SET(unused_parameter);
147                 SET(unused_value);
148         }
149         OPT("fatal-errors",                        fatal_errors);
150         OPT("float-equal",                         float_equal);
151         OPTX("format") {
152                 SET(format);
153                 SET(nonnull);
154         }
155         OPTX("implicit") {
156                 SET(implicit_function_declaration);
157                 SET(implicit_int);
158         }
159         OPT("implicit-function-declaration",       implicit_function_declaration);
160         OPT("implicit-int",                        implicit_int);
161         OPT("init-self",                           init_self);
162         OPT("long-long",                           long_long);
163         OPT("main",                                main);
164         OPT("missing-declarations",                missing_declarations);
165         OPT("missing-noreturn",                    missing_noreturn);
166         OPT("missing-prototypes",                  missing_prototypes);
167         OPT("multichar",                           multichar);
168         OPT("nested-externs",                      nested_externs);
169         OPT("nonnull",                             nonnull);
170         OPT("old-style-definition",                old_style_definition);
171         OPT("packed",                              packed);
172         OPT("padded",                              padded);
173         OPT("parentheses",                         parentheses);
174         OPT("pointer-arith",                       pointer_arith);
175         OPT("redundant-decls",                     redundant_decls);
176         OPT("return-type",                         return_type);
177         OPT("shadow",                              shadow);
178         OPT("sign-compare",                        sign_compare);
179         OPT("strict-prototypes",                   strict_prototypes);
180         OPT("switch-default",                      switch_default);
181         OPT("switch-enum",                         switch_enum);
182         OPT("traditional",                         traditional);
183         OPT("uninitialized",                       uninitialized);
184         OPT("unknown-pragmas",                     unknown_pragmas);
185         OPT("unreachable-code",                    unreachable_code);
186         OPTX("unused") {
187                 SET(unused_function);
188                 SET(unused_label);
189                 SET(unused_parameter);
190                 SET(unused_value);
191                 SET(unused_variable);
192         }
193         OPT("unused-function",                     unused_function);
194         OPT("unused-label",                        unused_label);
195         OPT("unused-parameter",                    unused_parameter);
196         OPT("unused-value",                        unused_value);
197         OPT("unused-variable",                     unused_variable);
198         OPT("write-strings",                       write_strings);
199 #undef OPT
200 #undef SET
201 #undef OPT_X
202         else {
203                 fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
204         }
205 }