Debian Codegraph @

src/errors.h

raw symbols SCIP
1 #ifndef _DASM_ERRORS_H
2 #define _DASM_ERRORS_H
3
4 /*
5 the DASM macro assembler (aka small systems cross assembler)
6
7 Copyright (c) 1988-2002 by Matthew Dillon.
8 Copyright (c) 1995 by Olaf "Rhialto" Seibert.
9 Copyright (c) 2003-2008 by Andrew Davie.
10 Copyright (c) 2008 by Peter H. Froehlich.
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 */
26
27 /**
28 * @file
29 *
30 * @brief Error handling for DASM.
31 *
32 * The idea for separate error levels came from syslog(3)
33 * and vague memories of Ralf Michl's Java version of same.
34 * The idea for printf-style interfaces came from Thomas Mathys
35 * as well as from Brian W. Kernighan's and Rob Pike's great
36 * little book The Practice of Programming. None of them are
37 * to blame for this implementation of course...
38 */
39
40 #include <stdbool.h>
41 #include <stdlib.h>
42
43 /* remove cool GNU stuff for other compilers */
44 /* TODO: find more permanent place for this, more .h might need it! */
45 #ifndef __GNUC__
46 #define __attribute__(x) /* GNU C __attribute__ removed */
47 #endif
48
49 /**
50 * @brief Format of error messages for -E option.
51 */
52 typedef enum
53 {
54 /* actual error formats */
55 ERRORFORMAT_WOE,
56 ERRORFORMAT_DILLON,
57 ERRORFORMAT_GNU,
58 /* meta data */
59 ERRORFORMAT_MIN = ERRORFORMAT_WOE,
60 ERRORFORMAT_DEFAULT = ERRORFORMAT_WOE,
61 ERRORFORMAT_MAX = ERRORFORMAT_GNU
62 }
63 error_format_t;
64
65 /**
66 * @brief Valid error format for -E option?
67 */
68 bool valid_error_format(int format);
69
70 /**
71 * @brief Set error format, -E option.
72 */
73 void set_error_format(error_format_t format);
74
75 /**
76 * @brief How many fatals so far, tells us whether to stop after the
77 * current pass.
78 */
79 size_t number_of_fatals(void);
80
81 /**
82 * @brief How many errors so far?
83 */
84 size_t number_of_errors(void);
85
86 /**
87 * @brief How many warnings so far?
88 */
89 size_t number_of_warnings(void);
90
91 /**
92 * @brief Severity of error messages.
93 */
94 typedef enum
95 {
96 /* actual error levels */
97
98 /** displayed in debug mode, -d option */
99 ERRORLEVEL_DEBUG,
100 /** displayed in high verbose mode, -v option */
101 ERRORLEVEL_INFO,
102 /** displayed in low verbose mode, -v option */
103 ERRORLEVEL_NOTICE,
104 /** displayed if warnings enabled, -w option */
105 ERRORLEVEL_WARNING,
106 /** regular error, always displayed, assembly continues */
107 ERRORLEVEL_ERROR,
108 /** fatal error, always displayed, assembly stops after current pass */
109 ERRORLEVEL_FATAL,
110 /** panic insanity, always displayed, breaks out right away */
111 ERRORLEVEL_PANIC,
112
113 /* meta data */
114 ERRORLEVEL_MIN = ERRORLEVEL_DEBUG,
115 ERRORLEVEL_DEFAULT = ERRORLEVEL_WARNING,
116 ERRORLEVEL_MAX = ERRORLEVEL_PANIC
117 }
118 error_level_t;
119
120 /**
121 * @brief Valid error level?
122 */
123 bool valid_error_level(int level);
124
125 /**
126 * @brief Set error level, messages down to and including
127 * this level are printed.
128 * @todo Currently command line options don't affect this
129 * thing yet.
130 */
131 void set_error_level(error_level_t level);
132
133 /**
134 * @brief Length of buffer for source locations.
135 */
136 #define SOURCE_LOCATION_LENGTH 256
137
138 /**
139 * @brief Global buffer for source locations.
140 */
141 extern char source_location_buffer[SOURCE_LOCATION_LENGTH];
142
143 /**
144 * @brief Macro to capture current location in the C source
145 * into a string buffer, sometimes useful for certain errors.
146 * @example SOURCE_LOCATION printf("You're at %s\n", SOURCE_LOCATION);
147 * @warning Since there is only one global buffer, our
148 * SOURCE_LOCATION macro is not "reentrant"; never use
149 * SOURCE_LOCATION more than once in a single evaluation
150 * context!
151 * @note We have to use snprintf() and a global buffer
152 * because __func__ is not expanded into a string literal
153 * by the preprocessor! :-/
154 * @todo the example tag above doesn't work as I thought it would...
155 * also, should this use sane_snprintf()?
156 */
157 #define SOURCE_LOCATION \
158 (snprintf(source_location_buffer, SOURCE_LOCATION_LENGTH, \
159 "%s/%s()/%d", __FILE__, __func__, __LINE__), \
160 source_location_buffer)
161
162 /**
163 * @brief Generic interface for printf(3)-style error handling
164 * framework.
165 * @see http://www.unixwiz.net/techtips/gnu-c-attributes.html
166 */
167
168 void notify_fmt(error_level_t level, const char *fmt, ...)
169 __attribute__((format(printf, 2, 3)));
170
171 /**
172 * @brief Helpers to make common levels easier to read.
173 */
174
175 void debug_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
176 void info_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
177 void notice_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
178 void warning_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
179 void error_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
180 void fatal_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
181
182 /**
183 * @brief An insane problem occurred, print message and terminate
184 * DASM with EXIT_FAILURE immediately.
185 *
186 * @todo the old panic, need to refactor to new use where an error
187 * code is required
188 *
189 * @warning You really don't want to call this. Not ever. We should
190 * have REAL error handling instead.
191 */
192
193 void panic_fmt(const char *fmt, ...) __attribute__((format(printf, 1, 2)))
194 /* TODO: __attribute__((noreturn)) leads to bus error? :-/ */;
195
196 /**
197 * @brief Standard messages DASM spits out under certain conditions.
198 *
199 * Note that there's nothing special about these, you can add
200 * messages here or write your own literal messages. However,
201 * it is highly recommended that you use standard messages if
202 * at all possible.
203 *
204 * The messages are named according to the severity they are
205 * most often associated with, however there's nothing wrong
206 * with passing PANIC_MEMORY to warning_fmt() when you want
207 * to make the user aware of the condition but have a backup
208 * plan of how to handle it. (The symbol table sorting done
209 * in main.c provides an example of this.)
210 *
211 * @todo As of right now, the above is not entirely true. It
212 * will be when I am done integrating the new log system with
213 * the rest of DASM. :-) [phf]
214 */
215
216 /* unclassified messages (so far) */
217 #define LOG_NOTHING "Nothing to tell you, which is a bug you should report!"
218 #define LOG_GENERIC "%s (generic)."
219
220 /* messages that support debugging, *very* low-level stuff. */
221 #define DEBUG_ENTER "<<< Entered function %s."
222 #define DEBUG_LEAVE ">>> Left function %s."
223 #define DEBUG_HASH_COLLISIONS "%d mnemonic collisions, %d symbol collisions."
224
225 /* warnings related to assembly source code */
226 #define WARNING_RANGE "The %s value in '%s' should be between %d and %d!"
227
228 /* errors related to assembly source code */
229 #define ERROR_SYNTAX_NONE "Syntax error!"
230 #define ERROR_SYNTAX_ONE "Syntax error in '%s'!"
231 #define ERROR_SYNTAX_TWO "Syntax error in '%s %s'!"
232 #define ERROR_VALUE_RANGE "The %s value in '%s' should be between %d and %d!"
233 #define ERROR_VALUE_ONEOF "The %s value in '%s' should one of %s!"
234 #define ERROR_BRANCH_RANGE "Branch out of range (%ld bytes)!"
235 #define ERROR_ADDRESS_RANGE_DETAIL "The %s address in '%s' should be between %d and %d!"
236 #define ERROR_ADDRESS_RANGE "Address should be between %d and %d!"
237 #define ERROR_INVALID_BIT "Invalid bit specification in '%s' must be <8!"
238 #define ERROR_INVALID_ARGS "Not enough arguments!"
239
240 /* messages that usually indicate terminal conditions :-) */
241 #define PANIC_MEMORY "Failed to allocate %zu bytes of memory in %s!"
242 #define PANIC_SMALL_MEMORY "Failed to allocate %zu bytes of small memory in %s!"
243
244 #endif /* _DASM_ERRORS_H */
245
246 /* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4 autoindent: */