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