]> git.saurik.com Git - bison.git/blame - src/complain.c
todo: short term
[bison.git] / src / complain.c
CommitLineData
a0f6b076 1/* Declaration for error-reporting function for Bison.
2cec9080 2
4f646c37 3 Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2009
23eb2a69 4 Free Software Foundation, Inc.
a0f6b076 5
f16b0819
PE
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
a0f6b076
AD
10
11 This program 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 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
f16b0819 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
a0f6b076
AD
18
19/* Based on error.c and error.h,
20 written by David MacKenzie <djm@gnu.ai.mit.edu>. */
21
2cec9080 22#include <config.h>
342b8b6e 23#include "system.h"
a0f6b076 24
21184140 25#include <stdarg.h>
a0f6b076 26
a0f6b076 27#include "complain.h"
4b68955b 28#include "files.h"
89eb3c76 29#include "getargs.h"
a0f6b076 30
0ae6073a 31bool complaint_issued;
a0f6b076 32
a0f6b076 33\f
a0f6b076 34
23eb2a69
AD
35/** Report an error message.
36 *
37 * \param loc the location, defaulting to the current file,
38 * or the program name.
39 * \param prefix put before the message (e.g., "warning").
40 * \param message the error message, a printf format string.
41 * \param args the arguments of the format string.
42 */
43static
e9955c83 44void
23eb2a69
AD
45error_message (location *loc,
46 const char *prefix,
47 const char *message, va_list args)
e9955c83 48{
23eb2a69
AD
49 if (loc)
50 location_print (stderr, *loc);
51 else
52 fputs (current_file ? current_file : program_name, stderr);
ee000ba4 53 fputs (": ", stderr);
e9955c83 54
23eb2a69
AD
55 if (prefix)
56 fprintf (stderr, "%s: ", prefix);
57
52489d44
AD
58 vfprintf (stderr, message, args);
59 va_end (args);
52489d44 60 putc ('\n', stderr);
23eb2a69
AD
61 fflush (stderr);
62}
63
64/** Wrap error_message() with varargs handling. */
65#define ERROR_MESSAGE(Loc, Prefix, Message) \
66{ \
67 va_list args; \
68 va_start (args, Message); \
69 error_message (Loc, Prefix, Message, args); \
70}
71
72
73/*--------------------------------.
74| Report a warning, and proceed. |
75`--------------------------------*/
76
89eb3c76
JD
77static void
78set_warning_issued (void)
79{
80 static bool warning_issued = false;
81 if (!warning_issued && (warnings_flag & warnings_error))
82 {
83 fprintf (stderr, "%s: warnings being treated as errors\n", program_name);
84 complaint_issued = true;
85 }
86 warning_issued = true;
87}
88
23eb2a69
AD
89void
90warn_at (location loc, const char *message, ...)
91{
89eb3c76 92 set_warning_issued ();
23eb2a69 93 ERROR_MESSAGE (&loc, _("warning"), message);
52489d44
AD
94}
95
96void
52489d44 97warn (const char *message, ...)
52489d44 98{
89eb3c76 99 set_warning_issued ();
23eb2a69
AD
100 ERROR_MESSAGE (NULL, _("warning"), message);
101}
52489d44 102
e9955c83 103
a0f6b076
AD
104/*-----------------------------------------------------------.
105| An error has occurred, but we can proceed, and die later. |
106`-----------------------------------------------------------*/
107
e9955c83 108void
41f83caf 109complain_at (location loc, const char *message, ...)
e9955c83 110{
23eb2a69 111 ERROR_MESSAGE (&loc, NULL, message);
0ae6073a 112 complaint_issued = true;
52489d44
AD
113}
114
115void
52489d44 116complain (const char *message, ...)
52489d44 117{
23eb2a69 118 ERROR_MESSAGE (NULL, NULL, message);
0ae6073a 119 complaint_issued = true;
e9955c83 120}
23eb2a69
AD
121
122
4f646c37
AD
123/*--------------------------------------------------------------.
124| An incompatibility with POSIX Yacc: mapped either to warn* or |
125| complain* depending on yacc_flag. |
126`--------------------------------------------------------------*/
127
128void
129yacc_at (location loc, const char *message, ...)
130{
131 if (yacc_flag)
132 {
133 ERROR_MESSAGE (&loc, NULL, message);
134 complaint_issued = true;
135 }
136 else if (warnings_flag & warnings_yacc)
137 {
138 set_warning_issued ();
139 ERROR_MESSAGE (&loc, _("warning"), message);
140 }
141}
142
143
a0f6b076
AD
144/*-------------------------------------------------.
145| A severe error has occurred, we cannot proceed. |
146`-------------------------------------------------*/
147
e9955c83 148void
41f83caf 149fatal_at (location loc, const char *message, ...)
e9955c83 150{
23eb2a69 151 ERROR_MESSAGE (&loc, _("fatal error"), message);
0ae6073a 152 exit (EXIT_FAILURE);
e9955c83
AD
153}
154
a0f6b076 155void
a0f6b076 156fatal (const char *message, ...)
a0f6b076 157{
23eb2a69 158 ERROR_MESSAGE (NULL, _("fatal error"), message);
0ae6073a 159 exit (EXIT_FAILURE);
a0f6b076 160}