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