implemented -Wall, -Wextra, -Wunused, -Wunused-function with limited
[cparser] / warning.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "warning.h"
4
5 warning_t warning = {
6         .char_subscripts               = true,
7         .check_format                  = true,
8         .empty_statement               = false,
9         .fatal_errors                  = false,
10         .float_equal                   = false,
11         .implicit_function_declaration = true,
12         .implicit_int                  = true,
13         .main                          = true,
14         .missing_declarations          = false,
15         .missing_prototypes            = false,
16         .redundant_decls               = true,
17         .s_are_errors                  = false,
18         .strict_prototypes             = true,
19         .switch_default                = false,
20         .unknown_pragmas               = true,
21         .unused_function               = false,
22         .unused_label                  = false,
23         .unused_parameter              = false,
24         .unused_variable               = false,
25         .unused_value                  = true
26 };
27
28 /**
29  * Switch on options for -Wall.
30  */
31 static void set_all_options(void) {
32         warning.char_subscripts               = true;
33         warning.check_format                  = true;
34         warning.empty_statement               = true;
35         /* warning.fatal_errors */
36         /* warning.float_equal */
37         warning.implicit_function_declaration = true;
38         warning.implicit_int                  = true;
39         warning.main                          = true;
40         /* warning.missing_declarations */
41         /* warning.missing_prototypes */
42         warning.redundant_decls               = true;
43         /* warning.s_are_errors */
44         warning.strict_prototypes             = true;
45         warning.switch_default                = true;
46         warning.unknown_pragmas               = true;
47         warning.unused_function               = true;
48         warning.unused_label                  = true;
49         warning.unused_parameter              = true;
50         warning.unused_variable               = true;
51         warning.unused_value                  = true;
52 }
53
54 /**
55  * Switch on options for -Wunused.
56  */
57 static void set_unused_options(void) {
58         warning.unused_function               = true;
59         warning.unused_label                  = true;
60         warning.unused_parameter              = true;
61         warning.unused_variable               = true;
62         warning.unused_value                  = true;
63 }
64
65 /**
66  * Switch on options for -Wextra.
67  */
68 static void set_extra_options(void) {
69 }
70
71 void set_warning_opt(const char *const opt)
72 {
73         const char* s = opt;
74
75         bool state = true;
76         if (strcmp(s, "all") == 0) {
77                 set_all_options();
78                 return;
79         } else if (strcmp(s, "extra") == 0) {
80                 set_extra_options();
81                 return;
82         } else if (strcmp(s, "unused") == 0) {
83                 set_unused_options();
84                 return;
85         }
86
87         /* no- modifier */
88         if (s[0] == 'n' && s[1] == 'o' && s[2] == '-') {
89                 s += 3;
90                 state = false;
91         }
92
93         if (0) {}
94 #define OPTX(x)   else if (strcmp(s, x) == 0)
95 #define SET(y)    warning.y = state;
96 #define OPT(x, y) OPTX(x) SET(y)
97         OPT("char-subscripts",               char_subscripts)
98         OPT("empty-statement",               empty_statement)
99         OPT("error",                         s_are_errors)
100         OPT("fatal-errors",                  fatal_errors)
101         OPT("float-equal",                   float_equal)
102         OPT("format",                        check_format)
103         OPTX("implicit") {
104                 SET(implicit_function_declaration)
105                 SET(implicit_int)
106         }
107         OPT("implicit-function-declaration", implicit_function_declaration)
108         OPT("implicit-int",                  implicit_int)
109         OPT("main",                          main)
110         OPT("missing-declarations",          missing_declarations)
111         OPT("missing-prototypes",            missing_prototypes)
112         OPT("redundant-decls",               redundant_decls)
113         OPT("strict-prototypes",             strict_prototypes)
114         OPT("switch-default",                switch_default)
115         OPT("unknown-pragmas",               unknown_pragmas)
116         OPT("unused-function",               unused_function)
117         OPT("unused-label",                  unused_label)
118         OPT("unused-parameter",              unused_parameter)
119         OPT("unused-variable",               unused_variable)
120 #if 0
121         OPTX("unused") {
122                 SET(unused_function)
123                 SET(unused_label)
124                 SET(unused_parameter)
125                 SET(unused_variable)
126                 SET(unused_value)
127         }
128 #endif
129         OPT("unused-value",                  unused_value)
130 #undef OPT
131 #undef SET
132 #undef OPT_X
133         else {
134                 fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
135         }
136 }