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