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