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