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