1 /* Declarations for error-reporting functions.
2 Copyright (C) 1995-1997, 2003, 2006, 2008-2024 Free Software Foundation,
4 This file is part of the GNU C Library.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 #ifndef _@GUARD_PREFIX@_ERROR_H
21 /* No @PRAGMA_SYSTEM_HEADER@ here, because it would prevent
22 -Wimplicit-fallthrough warnings for missing FALLTHROUGH after error(...)
23 or error_at_line(...) invocations. */
25 /* The include_next requires a split double-inclusion guard. */
27 # @INCLUDE_NEXT@ @NEXT_ERROR_H@
30 #ifndef _@GUARD_PREFIX@_ERROR_H
31 #define _@GUARD_PREFIX@_ERROR_H
33 /* This file uses _GL_ATTRIBUTE_ALWAYS_INLINE, _GL_ATTRIBUTE_FORMAT,
34 _GL_ATTRIBUTE_MAYBE_UNUSED. */
35 #if !_GL_CONFIG_H_INCLUDED
36 #error "Please include config.h first."
39 /* Get 'unreachable'. */
42 /* Get _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD, _GL_ATTRIBUTE_SPEC_PRINTF_SYSTEM. */
45 /* The definitions of _GL_FUNCDECL_RPL etc. are copied here. */
47 #if GNULIB_VFPRINTF_POSIX
48 # define _GL_ATTRIBUTE_SPEC_PRINTF_ERROR _GL_ATTRIBUTE_SPEC_PRINTF_STANDARD
50 # define _GL_ATTRIBUTE_SPEC_PRINTF_ERROR _GL_ATTRIBUTE_SPEC_PRINTF_SYSTEM
53 /* Helper macro for supporting the compiler's control flow analysis better.
54 It evaluates its arguments only once.
55 Test case: Compile copy-file.c with "gcc -Wimplicit-fallthrough". */
56 #if defined __GNUC__ || defined __clang__
57 /* Use 'unreachable' to tell the compiler when the function call does not
59 # define __gl_error_call1(function, status, ...) \
60 ((function) (status, __VA_ARGS__), \
61 (status) != 0 ? unreachable () : (void) 0)
62 /* If STATUS is a not a constant, the function call may or may not return;
63 therefore -Wimplicit-fallthrough will produce a warning. Use a compound
64 statement in order to evaluate STATUS only once.
65 If STATUS is a constant, we don't use a compound statement, because that
66 would trigger a -Wimplicit-fallthrough warning even when STATUS is != 0,
67 when not optimizing. This causes STATUS to be evaluated twice, but
68 that's OK since it does not have side effects. */
69 # define __gl_error_call(function, status, ...) \
70 (__builtin_constant_p (status) \
71 ? __gl_error_call1 (function, status, __VA_ARGS__) \
74 int const __errstatus = status; \
75 __gl_error_call1 (function, __errstatus, __VA_ARGS__); \
78 # define __gl_error_call(function, status, ...) \
79 (function) (status, __VA_ARGS__)
86 /* Print a message with 'fprintf (stderr, FORMAT, ...)';
87 if ERRNUM is nonzero, follow it with ": " and strerror (ERRNUM).
88 If STATUS is nonzero, terminate the program with 'exit (STATUS)'. */
90 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
92 # define error rpl_error
94 _GL_FUNCDECL_RPL (error
, void,
95 (int __status
, int __errnum
, const char *__format
, ...)
96 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 3, 4)));
97 _GL_CXXALIAS_RPL (error
, void,
98 (int __status
, int __errnum
, const char *__format
, ...));
99 # ifndef _GL_NO_INLINE_ERROR
101 # define error(status, ...) \
102 __gl_error_call (rpl_error, status, __VA_ARGS__)
106 _GL_FUNCDECL_SYS (error
, void,
107 (int __status
, int __errnum
, const char *__format
, ...)
108 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 3, 4)));
110 _GL_CXXALIAS_SYS (error
, void,
111 (int __status
, int __errnum
, const char *__format
, ...));
112 # ifndef _GL_NO_INLINE_ERROR
114 /* Only gcc ≥ 4.7 has __builtin_va_arg_pack. */
115 # if _GL_GNUC_PREREQ (4, 7)
116 # pragma GCC diagnostic push
117 # pragma GCC diagnostic ignored "-Wattributes"
118 _GL_ATTRIBUTE_MAYBE_UNUSED
120 _GL_ATTRIBUTE_ALWAYS_INLINE
121 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 3, 4))
122 _gl_inline_error (int __status
, int __errnum
, const char *__format
, ...)
124 return error (__status
, __errnum
, __format
, __builtin_va_arg_pack ());
126 # pragma GCC diagnostic pop
128 # define error(status, ...) \
129 __gl_error_call (_gl_inline_error, status, __VA_ARGS__)
132 # define error(status, ...) \
133 __gl_error_call (error, status, __VA_ARGS__)
138 _GL_CXXALIASWARN (error
);
141 /* Likewise. If FILENAME is non-NULL, include FILENAME:LINENO: in the
143 #if @REPLACE_ERROR_AT_LINE@
144 # if !(defined __cplusplus && defined GNULIB_NAMESPACE)
145 # undef error_at_line
146 # define error_at_line rpl_error_at_line
148 _GL_FUNCDECL_RPL (error_at_line
, void,
149 (int __status
, int __errnum
, const char *__filename
,
150 unsigned int __lineno
, const char *__format
, ...)
151 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 5, 6)));
152 _GL_CXXALIAS_RPL (error_at_line
, void,
153 (int __status
, int __errnum
, const char *__filename
,
154 unsigned int __lineno
, const char *__format
, ...));
155 # ifndef _GL_NO_INLINE_ERROR
156 # undef error_at_line
157 # define error_at_line(status, ...) \
158 __gl_error_call (rpl_error_at_line, status, __VA_ARGS__)
161 # if ! @HAVE_ERROR_AT_LINE@
162 _GL_FUNCDECL_SYS (error_at_line
, void,
163 (int __status
, int __errnum
, const char *__filename
,
164 unsigned int __lineno
, const char *__format
, ...)
165 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 5, 6)));
167 _GL_CXXALIAS_SYS (error_at_line
, void,
168 (int __status
, int __errnum
, const char *__filename
,
169 unsigned int __lineno
, const char *__format
, ...));
170 # ifndef _GL_NO_INLINE_ERROR
171 # ifdef error_at_line
172 /* Only gcc ≥ 4.7 has __builtin_va_arg_pack. */
173 # if _GL_GNUC_PREREQ (4, 7)
174 # pragma GCC diagnostic push
175 # pragma GCC diagnostic ignored "-Wattributes"
176 _GL_ATTRIBUTE_MAYBE_UNUSED
178 _GL_ATTRIBUTE_ALWAYS_INLINE
179 _GL_ATTRIBUTE_FORMAT ((_GL_ATTRIBUTE_SPEC_PRINTF_ERROR
, 5, 6))
180 _gl_inline_error_at_line (int __status
, int __errnum
, const char *__filename
,
181 unsigned int __lineno
, const char *__format
, ...)
183 return error_at_line (__status
, __errnum
, __filename
, __lineno
, __format
,
184 __builtin_va_arg_pack ());
186 # pragma GCC diagnostic pop
187 # undef error_at_line
188 # define error_at_line(status, ...) \
189 __gl_error_call (_gl_inline_error_at_line, status, __VA_ARGS__)
192 # define error_at_line(status, ...) \
193 __gl_error_call (error_at_line, status, __VA_ARGS__)
197 _GL_CXXALIASWARN (error_at_line
);
199 /* If NULL, error will flush stdout, then print on stderr the program
200 name, a colon and a space. Otherwise, error will call this
201 function without parameters instead. */
202 extern void (*error_print_progname
) (void);
204 /* This variable is incremented each time 'error' is called. */
205 extern unsigned int error_message_count
;
207 /* Sometimes we want to have at most one error per line. This
208 variable controls whether this mode is selected or not. */
209 extern int error_one_per_line
;
215 #endif /* _@GUARD_PREFIX@_ERROR_H */
216 #endif /* _@GUARD_PREFIX@_ERROR_H */