errors.c 5.29 KB
Newer Older
Per Bothner committed
1
/* Default error handlers for CPP Library.
2
   Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998, 1999, 2000,
3
   2001, 2002, 2004 Free Software Foundation, Inc.
Per Bothner committed
4
   Written by Per Bothner, 1994.
Jeff Law committed
5
   Based on CCCP program by Paul Rubin, June 1986
Per Bothner committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19
   Adapted to ANSI C, Richard Stallman, Jan 1987

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
20
Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Per Bothner committed
21 22 23 24 25

 In other words, you are welcome to use, share and improve this program.
 You are forbidden to forbid anyone else to use, share and improve
 what you give them.   Help stamp out software-hoarding!  */

26
#include "config.h"
27
#include "system.h"
28
#include "cpplib.h"
29
#include "internal.h"
30

31
static void print_location (cpp_reader *, source_location, unsigned int);
32

33
/* Print the logical file location (LINE, COL) in preparation for a
34 35 36
   diagnostic.  Outputs the #include chain if it has changed.  A line
   of zero suppresses the include stack, and outputs the program name
   instead.  */
37
static void
38
print_location (cpp_reader *pfile, source_location line, unsigned int col)
Per Bothner committed
39
{
40
  if (line == 0)
41
    fprintf (stderr, "%s: ", progname);
42
  else
43
    {
44
      const struct line_map *map;
Per Bothner committed
45
      unsigned int lin;
46

47 48
      map = linemap_lookup (pfile->line_table, line);
      linemap_print_containing_files (pfile->line_table, map);
49

Per Bothner committed
50
      lin = SOURCE_LINE (map, line);
51
      if (col == 0)
52 53 54 55 56
	{
	  col = SOURCE_COLUMN (map, line);
	  if (col == 0)
	    col = 1;
	}
57

Per Bothner committed
58
      if (lin == 0)
59
	fprintf (stderr, "%s:", map->to_file);
60
      else if (CPP_OPTION (pfile, show_column) == 0)
Per Bothner committed
61
	fprintf (stderr, "%s:%u:", map->to_file, lin);
62
      else
Per Bothner committed
63
	fprintf (stderr, "%s:%u:%u:", map->to_file, lin, col);
64

Neil Booth committed
65
      fputc (' ', stderr);
66
    }
Per Bothner committed
67 68
}

69
/* Set up for a diagnostic: print the file and line, bump the error
70
   counter, etc.  SRC_LOC is the logical line number; zero means to print
71
   at the location of the previously lexed token, which tends to be
72 73 74 75 76 77 78
   the correct place by default.  The column number can be specified either
   using COLUMN or (if COLUMN==0) extracting SOURCE_COLUMN from SRC_LOC.
   (This may seem redundant, but is useful when pre-scanning (cleaning) a line,
   when we haven't yet verified whether the current line_map has a
   big enough max_column_hint.)

   Returns 0 if the error has been suppressed.  */
79
int
80 81
_cpp_begin_message (cpp_reader *pfile, int code,
		    source_location src_loc, unsigned int column)
Per Bothner committed
82
{
83
  int level = CPP_DL_EXTRACT (code);
84

85
  switch (level)
86
    {
87 88
    case CPP_DL_WARNING:
    case CPP_DL_PEDWARN:
89
      if (cpp_in_system_header (pfile)
90 91
	  && ! CPP_OPTION (pfile, warn_system_headers))
	return 0;
92 93
      /* Fall through.  */

94
    case CPP_DL_WARNING_SYSHDR:
95
      if (CPP_OPTION (pfile, warnings_are_errors)
96
	  || (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
97 98 99
	{
	  if (CPP_OPTION (pfile, inhibit_errors))
	    return 0;
100
	  level = CPP_DL_ERROR;
101
	  pfile->errors++;
102
	}
103 104
      else if (CPP_OPTION (pfile, inhibit_warnings))
	return 0;
105
      break;
106

107
    case CPP_DL_ERROR:
108 109
      if (CPP_OPTION (pfile, inhibit_errors))
	return 0;
110
      /* ICEs cannot be inhibited.  */
111
    case CPP_DL_ICE:
112
      pfile->errors++;
113
      break;
114 115
    }

116
  print_location (pfile, src_loc, column);
117
  if (CPP_DL_WARNING_P (level))
118
    fputs (_("warning: "), stderr);
119
  else if (level == CPP_DL_ICE)
120
    fputs (_("internal error: "), stderr);
121 122
  else
    fputs (_("error: "), stderr);
123 124

  return 1;
Per Bothner committed
125 126
}

127 128 129 130
/* Don't remove the blank before do, as otherwise the exgettext
   script will mistake this as a function definition */
#define v_message(msgid, ap) \
 do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
131

132
/* Exported interface.  */
133

134
/* Print an error at the location of the previously lexed token.  */
135
void
136
cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
137
{
138
  source_location src_loc;
139 140 141
  va_list ap;
  
  va_start (ap, msgid);
142

143
  if (CPP_OPTION (pfile, client_diagnostic))
144
    pfile->cb.error (pfile, level, _(msgid), &ap);
145
  else
146
    {
147 148 149 150 151 152 153 154 155 156 157
      if (CPP_OPTION (pfile, traditional))
	{
	  if (pfile->state.in_directive)
	    src_loc = pfile->directive_line;
	  else
	    src_loc = pfile->line_table->highest_line;
	}
      else
	{
	  src_loc = pfile->cur_token[-1].src_loc;
	}
158

159 160 161
      if (_cpp_begin_message (pfile, level, src_loc, 0))
	v_message (msgid, ap);
    }
162

163
  va_end (ap);
164 165
}

166
/* Print an error at a specific location.  */
167
void
168
cpp_error_with_line (cpp_reader *pfile, int level,
169
		     source_location src_loc, unsigned int column,
170
		     const char *msgid, ...)
171
{
172 173 174
  va_list ap;
  
  va_start (ap, msgid);
175

176
  if (_cpp_begin_message (pfile, level, src_loc, column))
177
    v_message (msgid, ap);
178

179
  va_end (ap);
180 181 182
}

void
183
cpp_errno (cpp_reader *pfile, int level, const char *msgid)
184
{
185 186 187 188
  if (msgid[0] == '\0')
    msgid = _("stdout");

  cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
189
}