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