190075Sobrien/* Basic error reporting routines.
2169689Skan   Copyright (C) 1999, 2000, 2001, 2003, 2004, 2005
3132718Skan   Free Software Foundation, Inc.
490075Sobrien
590075SobrienThis file is part of GCC.
690075Sobrien
790075SobrienGCC is free software; you can redistribute it and/or modify it under
890075Sobrienthe terms of the GNU General Public License as published by the Free
990075SobrienSoftware Foundation; either version 2, or (at your option) any later
1090075Sobrienversion.
1190075Sobrien
1290075SobrienGCC is distributed in the hope that it will be useful, but WITHOUT ANY
1390075SobrienWARRANTY; without even the implied warranty of MERCHANTABILITY or
1490075SobrienFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1590075Sobrienfor more details.
1690075Sobrien
1790075SobrienYou should have received a copy of the GNU General Public License
1890075Sobrienalong with GCC; see the file COPYING.  If not, write to the Free
19169689SkanSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20169689Skan02110-1301, USA.  */
2190075Sobrien
2290075Sobrien/* warning, error, and fatal.  These definitions are suitable for use
23169689Skan   in the generator programs; the compiler has a more elaborate suite
24169689Skan   of diagnostic printers, found in diagnostic.c.  */
2590075Sobrien
26169689Skan#ifdef GENERATOR_FILE
27169689Skan#include "bconfig.h"
28169689Skan#else
2990075Sobrien#include "config.h"
30169689Skan#endif
3190075Sobrien#include "system.h"
3290075Sobrien#include "errors.h"
3390075Sobrien
3490075Sobrien/* Set this to argv[0] at the beginning of main.  */
3590075Sobrien
3690075Sobrienconst char *progname;
3790075Sobrien
3890075Sobrien/* Starts out 0, set to 1 if error is called.  */
3990075Sobrien
4090075Sobrienint have_error = 0;
4190075Sobrien
4290075Sobrien/* Print a warning message - output produced, but there may be problems.  */
4390075Sobrien
4490075Sobrienvoid
45169689Skanwarning (int opt ATTRIBUTE_UNUSED, const char *format, ...)
4690075Sobrien{
47132718Skan  va_list ap;
4890075Sobrien
49132718Skan  va_start (ap, format);
5090075Sobrien  fprintf (stderr, "%s: warning: ", progname);
5190075Sobrien  vfprintf (stderr, format, ap);
52132718Skan  va_end (ap);
5390075Sobrien  fputc('\n', stderr);
5490075Sobrien}
5590075Sobrien
5690075Sobrien
5790075Sobrien/* Print an error message - we keep going but the output is unusable.  */
5890075Sobrien
5990075Sobrienvoid
60132718Skanerror (const char *format, ...)
6190075Sobrien{
62132718Skan  va_list ap;
6390075Sobrien
64132718Skan  va_start (ap, format);
6590075Sobrien  fprintf (stderr, "%s: ", progname);
6690075Sobrien  vfprintf (stderr, format, ap);
67132718Skan  va_end (ap);
6890075Sobrien  fputc('\n', stderr);
6990075Sobrien
7090075Sobrien  have_error = 1;
7190075Sobrien}
7290075Sobrien
7390075Sobrien
7490075Sobrien/* Fatal error - terminate execution immediately.  Does not return.  */
7590075Sobrien
7690075Sobrienvoid
77132718Skanfatal (const char *format, ...)
7890075Sobrien{
79132718Skan  va_list ap;
8090075Sobrien
81132718Skan  va_start (ap, format);
8290075Sobrien  fprintf (stderr, "%s: ", progname);
8390075Sobrien  vfprintf (stderr, format, ap);
84132718Skan  va_end (ap);
8590075Sobrien  fputc('\n', stderr);
8690075Sobrien  exit (FATAL_EXIT_CODE);
8790075Sobrien}
8890075Sobrien
8990075Sobrien/* Similar, but say we got an internal error.  */
9090075Sobrien
9190075Sobrienvoid
92132718Skaninternal_error (const char *format, ...)
9390075Sobrien{
94132718Skan  va_list ap;
9590075Sobrien
96132718Skan  va_start (ap, format);
9790075Sobrien  fprintf (stderr, "%s: Internal error: ", progname);
9890075Sobrien  vfprintf (stderr, format, ap);
99132718Skan  va_end (ap);
10090075Sobrien  fputc ('\n', stderr);
10190075Sobrien  exit (FATAL_EXIT_CODE);
10290075Sobrien}
10390075Sobrien
10490075Sobrien/* Given a partial pathname as input, return another pathname that
10590075Sobrien   shares no directory elements with the pathname of __FILE__.  This
10690075Sobrien   is used by fancy_abort() to print `Internal compiler error in expr.c'
10790075Sobrien   instead of `Internal compiler error in ../../GCC/gcc/expr.c'.  This
10890075Sobrien   version if for the gen* programs and so needn't handle subdirectories.  */
10990075Sobrien
11090075Sobrienconst char *
111132718Skantrim_filename (const char *name)
11290075Sobrien{
11390075Sobrien  static const char this_file[] = __FILE__;
11490075Sobrien  const char *p = name, *q = this_file;
11590075Sobrien
11690075Sobrien  /* Skip any parts the two filenames have in common.  */
11790075Sobrien  while (*p == *q && *p != 0 && *q != 0)
11890075Sobrien    p++, q++;
11990075Sobrien
12090075Sobrien  /* Now go backwards until the previous directory separator.  */
121132718Skan  while (p > name && !IS_DIR_SEPARATOR (p[-1]))
12290075Sobrien    p--;
12390075Sobrien
12490075Sobrien  return p;
12590075Sobrien}
12690075Sobrien
12790075Sobrien/* "Fancy" abort.  Reports where in the compiler someone gave up.
12890075Sobrien   This file is used only by build programs, so we're not as polite as
12990075Sobrien   the version in diagnostic.c.  */
13090075Sobrienvoid
131132718Skanfancy_abort (const char *file, int line, const char *func)
13290075Sobrien{
133169689Skan  internal_error ("abort in %s, at %s:%d", func, trim_filename (file), line);
13490075Sobrien}
135