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