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