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