1*e4b17023SJohn Marino /* Basic error reporting routines.
2*e4b17023SJohn Marino Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005, 2007, 2008
3*e4b17023SJohn Marino Free Software Foundation, Inc.
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino /* warning, error, and fatal. These definitions are suitable for use
22*e4b17023SJohn Marino in the generator programs; the compiler has a more elaborate suite
23*e4b17023SJohn Marino of diagnostic printers, found in diagnostic.c. */
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino #ifdef GENERATOR_FILE
26*e4b17023SJohn Marino #include "bconfig.h"
27*e4b17023SJohn Marino #else
28*e4b17023SJohn Marino #include "config.h"
29*e4b17023SJohn Marino #endif
30*e4b17023SJohn Marino #include "system.h"
31*e4b17023SJohn Marino #include "errors.h"
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino /* Set this to argv[0] at the beginning of main. */
34*e4b17023SJohn Marino
35*e4b17023SJohn Marino const char *progname;
36*e4b17023SJohn Marino
37*e4b17023SJohn Marino /* Starts out 0, set to 1 if error is called. */
38*e4b17023SJohn Marino
39*e4b17023SJohn Marino int have_error = 0;
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino /* Print a warning message - output produced, but there may be problems. */
42*e4b17023SJohn Marino
43*e4b17023SJohn Marino void
warning(const char * format,...)44*e4b17023SJohn Marino warning (const char *format, ...)
45*e4b17023SJohn Marino {
46*e4b17023SJohn Marino va_list ap;
47*e4b17023SJohn Marino
48*e4b17023SJohn Marino va_start (ap, format);
49*e4b17023SJohn Marino fprintf (stderr, "%s: warning: ", progname);
50*e4b17023SJohn Marino vfprintf (stderr, format, ap);
51*e4b17023SJohn Marino va_end (ap);
52*e4b17023SJohn Marino fputc('\n', stderr);
53*e4b17023SJohn Marino }
54*e4b17023SJohn Marino
55*e4b17023SJohn Marino
56*e4b17023SJohn Marino /* Print an error message - we keep going but the output is unusable. */
57*e4b17023SJohn Marino
58*e4b17023SJohn Marino void
error(const char * format,...)59*e4b17023SJohn Marino error (const char *format, ...)
60*e4b17023SJohn Marino {
61*e4b17023SJohn Marino va_list ap;
62*e4b17023SJohn Marino
63*e4b17023SJohn Marino va_start (ap, format);
64*e4b17023SJohn Marino fprintf (stderr, "%s: ", progname);
65*e4b17023SJohn Marino vfprintf (stderr, format, ap);
66*e4b17023SJohn Marino va_end (ap);
67*e4b17023SJohn Marino fputc('\n', stderr);
68*e4b17023SJohn Marino
69*e4b17023SJohn Marino have_error = 1;
70*e4b17023SJohn Marino }
71*e4b17023SJohn Marino
72*e4b17023SJohn Marino
73*e4b17023SJohn Marino /* Fatal error - terminate execution immediately. Does not return. */
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino void
fatal(const char * format,...)76*e4b17023SJohn Marino fatal (const char *format, ...)
77*e4b17023SJohn Marino {
78*e4b17023SJohn Marino va_list ap;
79*e4b17023SJohn Marino
80*e4b17023SJohn Marino va_start (ap, format);
81*e4b17023SJohn Marino fprintf (stderr, "%s: ", progname);
82*e4b17023SJohn Marino vfprintf (stderr, format, ap);
83*e4b17023SJohn Marino va_end (ap);
84*e4b17023SJohn Marino fputc('\n', stderr);
85*e4b17023SJohn Marino exit (FATAL_EXIT_CODE);
86*e4b17023SJohn Marino }
87*e4b17023SJohn Marino
88*e4b17023SJohn Marino /* Similar, but say we got an internal error. */
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino void
internal_error(const char * format,...)91*e4b17023SJohn Marino internal_error (const char *format, ...)
92*e4b17023SJohn Marino {
93*e4b17023SJohn Marino va_list ap;
94*e4b17023SJohn Marino
95*e4b17023SJohn Marino va_start (ap, format);
96*e4b17023SJohn Marino fprintf (stderr, "%s: Internal error: ", progname);
97*e4b17023SJohn Marino vfprintf (stderr, format, ap);
98*e4b17023SJohn Marino va_end (ap);
99*e4b17023SJohn Marino fputc ('\n', stderr);
100*e4b17023SJohn Marino exit (FATAL_EXIT_CODE);
101*e4b17023SJohn Marino }
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino /* Given a partial pathname as input, return another pathname that
104*e4b17023SJohn Marino shares no directory elements with the pathname of __FILE__. This
105*e4b17023SJohn Marino is used by fancy_abort() to print `Internal compiler error in expr.c'
106*e4b17023SJohn Marino instead of `Internal compiler error in ../../GCC/gcc/expr.c'. This
107*e4b17023SJohn Marino version is meant to be used for the gen* programs and therefor need not
108*e4b17023SJohn Marino handle subdirectories. */
109*e4b17023SJohn Marino
110*e4b17023SJohn Marino const char *
trim_filename(const char * name)111*e4b17023SJohn Marino trim_filename (const char *name)
112*e4b17023SJohn Marino {
113*e4b17023SJohn Marino static const char this_file[] = __FILE__;
114*e4b17023SJohn Marino const char *p = name, *q = this_file;
115*e4b17023SJohn Marino
116*e4b17023SJohn Marino /* Skip any parts the two filenames have in common. */
117*e4b17023SJohn Marino while (*p == *q && *p != 0 && *q != 0)
118*e4b17023SJohn Marino p++, q++;
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino /* Now go backwards until the previous directory separator. */
121*e4b17023SJohn Marino while (p > name && !IS_DIR_SEPARATOR (p[-1]))
122*e4b17023SJohn Marino p--;
123*e4b17023SJohn Marino
124*e4b17023SJohn Marino return p;
125*e4b17023SJohn Marino }
126*e4b17023SJohn Marino
127*e4b17023SJohn Marino /* "Fancy" abort. Reports where in the compiler someone gave up.
128*e4b17023SJohn Marino This file is used only by build programs, so we're not as polite as
129*e4b17023SJohn Marino the version in diagnostic.c. */
130*e4b17023SJohn Marino void
fancy_abort(const char * file,int line,const char * func)131*e4b17023SJohn Marino fancy_abort (const char *file, int line, const char *func)
132*e4b17023SJohn Marino {
133*e4b17023SJohn Marino internal_error ("abort in %s, at %s:%d", func, trim_filename (file), line);
134*e4b17023SJohn Marino }
135