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