1 /*
2  * This file is part of cparser.
3  * Copyright (C) 2007-2009 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 <assert.h>
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include "adt/strutil.h"
25 #include "adt/util.h"
26 #include "warning.h"
27 #include "help.h"
28 
29 static warning_switch_t warning[] = {
30 	[WARN_ADDRESS]                       = { WARN_STATE_ON,   "address"                       },
31 	[WARN_AGGREGATE_RETURN]              = { WARN_STATE_NONE, "aggregate-return"              },
32 	[WARN_ATTRIBUTE]                     = { WARN_STATE_ON,   "attribute"                     },
33 	[WARN_CAST_QUAL]                     = { WARN_STATE_NONE, "cast-qual",                    },
34 	[WARN_CHAR_SUBSCRIPTS]               = { WARN_STATE_ON,   "char-subscripts",              },
35 	[WARN_COMMENT]                       = { WARN_STATE_NONE, "comment",                      },
36 	[WARN_CONVERSION]                    = { WARN_STATE_NONE, "conversion",                   },
37 	[WARN_DECLARATION_AFTER_STATEMENT]   = { WARN_STATE_NONE, "declaration-after-statement",  },
38 	[WARN_DEPRECATED_DECLARATIONS]       = { WARN_STATE_ON,   "deprecated-declarations",      },
39 	[WARN_DIV_BY_ZERO]                   = { WARN_STATE_ON,   "div-by-zero",                  },
40 	[WARN_EMPTY_BODY]                    = { WARN_STATE_NONE, "empty-body",                   },
41 	[WARN_EMPTY_STATEMENT]               = { WARN_STATE_NONE, "empty-statement",              },
42 	[WARN_ERROR]                         = { WARN_STATE_NONE, "error"                         },
43 	[WARN_FATAL_ERRORS]                  = { WARN_STATE_NONE, "fatal-errors"                  },
44 	[WARN_FLOAT_EQUAL]                   = { WARN_STATE_NONE, "float-equal",                  },
45 	[WARN_FORMAT]                        = { WARN_STATE_ON,   "format"                        },
46 	[WARN_IGNORED_QUALIFIERS]            = { WARN_STATE_ON,   "ignored-qualifiers"            },
47 	[WARN_IMPLICIT_FUNCTION_DECLARATION] = { WARN_STATE_ON,   "implicit-function-declaration" },
48 	[WARN_IMPLICIT_INT]                  = { WARN_STATE_ON,   "implicit-int"                  },
49 	[WARN_LONG_LONG]                     = { WARN_STATE_NONE, "long-long"                     },
50 	[WARN_MAIN]                          = { WARN_STATE_ON,   "main",                         },
51 	[WARN_MISSING_DECLARATIONS]          = { WARN_STATE_NONE, "missing-declarations",         },
52 	[WARN_MISSING_NORETURN]              = { WARN_STATE_NONE, "missing-noreturn",             },
53 	[WARN_MISSING_PROTOTYPES]            = { WARN_STATE_NONE, "missing-prototypes",           },
54 	[WARN_MULTICHAR]                     = { WARN_STATE_ON,   "multichar",                    },
55 	[WARN_NESTED_EXTERNS]                = { WARN_STATE_NONE, "nested-externs"                },
56 	[WARN_NONNULL]                       = { WARN_STATE_ON,   "nonnull",                      },
57 	[WARN_OLD_STYLE_DEFINITION]          = { WARN_STATE_NONE, "old-style-definition",         },
58 	[WARN_OTHER]                         = { WARN_STATE_ON,   "other"                         },
59 	[WARN_PACKED]                        = { WARN_STATE_NONE, "packed",                       },
60 	[WARN_PADDED]                        = { WARN_STATE_NONE, "padded",                       },
61 	[WARN_PARENTHESES]                   = { WARN_STATE_NONE, "parentheses",                  },
62 	[WARN_POINTER_ARITH]                 = { WARN_STATE_ON,   "pointer-arith",                },
63 	[WARN_REDUNDANT_DECLS]               = { WARN_STATE_ON,   "redundant-decls",              },
64 	[WARN_RETURN_TYPE]                   = { WARN_STATE_ON,   "return-type",                  },
65 	[WARN_SHADOW]                        = { WARN_STATE_NONE, "shadow",                       },
66 	[WARN_SHADOW_LOCAL]                  = { WARN_STATE_NONE, "shadow-local",                 },
67 	[WARN_SIGN_COMPARE]                  = { WARN_STATE_NONE, "sign-compare",                 },
68 	[WARN_STRAY_SEMICOLON]               = { WARN_STATE_ON,   "stray-semicolon",              },
69 	[WARN_STRICT_PROTOTYPES]             = { WARN_STATE_ON,   "strict-prototypes"             },
70 	[WARN_SWITCH_DEFAULT]                = { WARN_STATE_NONE, "switch-default",               },
71 	[WARN_SWITCH_ENUM]                   = { WARN_STATE_NONE, "switch-enum",                  },
72 	[WARN_TRADITIONAL]                   = { WARN_STATE_NONE, "traditional"                   },
73 	[WARN_UNINITIALIZED]                 = { WARN_STATE_ON,   "uninitialized",                },
74 	[WARN_UNKNOWN_PRAGMAS]               = { WARN_STATE_ON,   "unknown-pragmas",              },
75 	[WARN_UNREACHABLE_CODE]              = { WARN_STATE_NONE, "unreachable-code"              },
76 	[WARN_UNUSED_FUNCTION]               = { WARN_STATE_NONE, "unused-function",              },
77 	[WARN_UNUSED_LABEL]                  = { WARN_STATE_NONE, "unused-label",                 },
78 	[WARN_UNUSED_PARAMETER]              = { WARN_STATE_NONE, "unused-parameter",             },
79 	[WARN_UNUSED_VALUE]                  = { WARN_STATE_ON,   "unused-value",                 },
80 	[WARN_UNUSED_VARIABLE]               = { WARN_STATE_NONE, "unused-variable",              },
81 	[WARN_WRITE_STRINGS]                 = { WARN_STATE_NONE, "write-strings",                },
82 };
83 
get_warn_switch(warning_t const w)84 warning_switch_t const *get_warn_switch(warning_t const w)
85 {
86 	assert((size_t)w < lengthof(warning));
87 	assert(warning[w].name);
88 	return &warning[w];
89 }
90 
print_warning_opt_help(void)91 void print_warning_opt_help(void)
92 {
93 	/* TODO: write explanations */
94 	for (warning_switch_t* i = warning; i != endof(warning); ++i) {
95 		char buf[256];
96 		snprintf(buf, sizeof(buf), "-W%s", i->name);
97 		put_help(buf, "");
98 	}
99 }
100 
set_warning_opt(const char * const opt)101 void set_warning_opt(const char *const opt)
102 {
103 	/* Process prefixes: -W[no-][error=] */
104 	char const *s     = opt;
105 	char const *rest;
106 	bool const  no    = (rest = strstart(s, "no-"))    ? s = rest, true : false;
107 	bool const  error = (rest = strstart(s, "error=")) ? s = rest, true : false;
108 
109 	warn_state_t on  = WARN_STATE_NONE;
110 	warn_state_t off = WARN_STATE_NONE;
111 	if (!no || !error)
112 		on |= WARN_STATE_ON;
113 	if (error) {
114 		on  |= WARN_STATE_ERROR;
115 		off |= WARN_STATE_NO_ERROR;
116 	}
117 	if (no) {
118 		warn_state_t const tmp = on;
119 		on  = off;
120 		off = tmp;
121 	}
122 
123 	for (warning_switch_t* i = warning; i != endof(warning); ++i) {
124 		if (streq(i->name, s)) {
125 			i->state = (i->state & ~off) | on;
126 			return;
127 		}
128 	}
129 
130 	if (s[0] == '\0') { // -W is an alias for -Wextra
131 		goto extra;
132 	}
133 #define OPTX(x)   else if (streq(s, x))
134 #define SET(y)    (void)(warning[y].state = (warning[y].state & ~off) | on)
135 	OPTX("all") {
136 		/* Note: this switched on a lot more warnings than gcc's -Wall */
137 		SET(WARN_ADDRESS);
138 		SET(WARN_ATTRIBUTE);
139 		SET(WARN_CHAR_SUBSCRIPTS);
140 		SET(WARN_COMMENT);
141 		SET(WARN_EMPTY_STATEMENT);
142 		SET(WARN_FORMAT);
143 		SET(WARN_IMPLICIT_FUNCTION_DECLARATION);
144 		SET(WARN_IMPLICIT_INT);
145 		SET(WARN_MAIN);
146 		SET(WARN_MISSING_DECLARATIONS);
147 		SET(WARN_NONNULL);
148 		SET(WARN_OTHER);
149 		SET(WARN_PARENTHESES);
150 		SET(WARN_POINTER_ARITH);
151 		SET(WARN_REDUNDANT_DECLS);
152 		SET(WARN_RETURN_TYPE);
153 		SET(WARN_SHADOW_LOCAL);
154 		SET(WARN_SIGN_COMPARE);
155 		SET(WARN_STRICT_PROTOTYPES);
156 		SET(WARN_SWITCH_ENUM);
157 		SET(WARN_UNINITIALIZED);
158 		SET(WARN_UNKNOWN_PRAGMAS);
159 		SET(WARN_UNREACHABLE_CODE);
160 		SET(WARN_UNUSED_FUNCTION);
161 		SET(WARN_UNUSED_LABEL);
162 		SET(WARN_UNUSED_PARAMETER);
163 		SET(WARN_UNUSED_VALUE);
164 		SET(WARN_UNUSED_VARIABLE);
165 	}
166 	OPTX("extra") {
167 extra:
168 		/* TODO */
169 		// TODO SET(function_end_without_return);
170 		SET(WARN_EMPTY_STATEMENT);
171 		// TODO SET(incomplete_aggregate_init);
172 		// TODO SET(missing_field_initializers);
173 		// TODO SET(pointless_comparison);
174 		SET(WARN_SHADOW);
175 		SET(WARN_UNUSED_PARAMETER);
176 		SET(WARN_UNUSED_VALUE);
177 	}
178 	OPTX("implicit") {
179 		SET(WARN_IMPLICIT_FUNCTION_DECLARATION);
180 		SET(WARN_IMPLICIT_INT);
181 	}
182 	OPTX("unused") {
183 		SET(WARN_UNUSED_FUNCTION);
184 		SET(WARN_UNUSED_LABEL);
185 		SET(WARN_UNUSED_PARAMETER);
186 		SET(WARN_UNUSED_VALUE);
187 		SET(WARN_UNUSED_VARIABLE);
188 	}
189 #undef SET
190 #undef OPT_X
191 	else if (streq(opt /* sic */, "error-implicit-function-declaration")) {
192 		/* GCC legacy: This way it only can be activated. */
193 		warning[WARN_IMPLICIT_FUNCTION_DECLARATION].state = WARN_STATE_ON | WARN_STATE_ERROR;
194 		return;
195 	}
196 	else {
197 		fprintf(stderr, "warning: ignoring unknown option -W%s\n", opt);
198 	}
199 }
200 
disable_all_warnings(void)201 void disable_all_warnings(void)
202 {
203 	for (warning_switch_t* i = warning; i != endof(warning); ++i) {
204 		if (i != &warning[WARN_ERROR] &&
205 		    i != &warning[WARN_FATAL_ERRORS]) {
206 			i->state &= ~WARN_STATE_ON;
207 		}
208 	}
209 }
210