#include "config.h"
#include "system.h"
#include "cpplib.h"
#include "internal.h"
static int gcc_error_colon = 0;
static void print_location (cpp_reader *, source_location, unsigned int);
static void
print_location (cpp_reader *pfile, source_location line, unsigned int col)
{
const char *estr;
{
static int done = 0;
if ( ! done)
{
done = 1;
if (getenv ("GCC_DASH_W"))
CPP_OPTION (pfile, inhibit_warnings) = 1;
if (getenv ("GCC_ERROR_COLON"))
gcc_error_colon = 1;
}
}
estr = (gcc_error_colon) ? "error:" : "" ;
if (line == 0)
fprintf (stderr, "%s: ", progname);
else
{
const struct line_map *map;
unsigned int lin;
map = linemap_lookup (pfile->line_table, line);
linemap_print_containing_files (pfile->line_table, map);
lin = SOURCE_LINE (map, line);
if (col == 0)
{
col = SOURCE_COLUMN (map, line);
if (col == 0)
col = 1;
}
if (lin == 0)
fprintf (stderr, "%s:%s", map->to_file, estr);
else if (CPP_OPTION (pfile, show_column) == 0)
fprintf (stderr, "%s:%u:%s", map->to_file, lin, estr);
else
fprintf (stderr, "%s:%u:%u:%s", map->to_file, lin, col, estr);
fputc (' ', stderr);
}
}
int
_cpp_begin_message (cpp_reader *pfile, int code,
source_location src_loc, unsigned int column)
{
int level = CPP_DL_EXTRACT (code);
switch (level)
{
case CPP_DL_WARNING:
case CPP_DL_PEDWARN:
if (cpp_in_system_header (pfile)
&& ! CPP_OPTION (pfile, warn_system_headers))
return 0;
case CPP_DL_WARNING_SYSHDR:
if (CPP_OPTION (pfile, warnings_are_errors)
|| (level == CPP_DL_PEDWARN && CPP_OPTION (pfile, pedantic_errors)))
{
if (CPP_OPTION (pfile, inhibit_errors))
return 0;
level = CPP_DL_ERROR;
pfile->errors++;
}
else if (CPP_OPTION (pfile, inhibit_warnings))
return 0;
break;
case CPP_DL_ERROR:
if (CPP_OPTION (pfile, inhibit_errors))
return 0;
case CPP_DL_ICE:
pfile->errors++;
break;
}
print_location (pfile, src_loc, column);
if (CPP_DL_WARNING_P (level))
fputs (_("warning: "), stderr);
else if (level == CPP_DL_ICE)
fputs (_("internal error: "), stderr);
else
fputs (_("error: "), stderr);
return 1;
}
#define v_message(msgid, ap) \
do { vfprintf (stderr, _(msgid), ap); putc ('\n', stderr); } while (0)
void
cpp_error (cpp_reader * pfile, int level, const char *msgid, ...)
{
source_location src_loc;
va_list ap;
va_start (ap, msgid);
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;
}
if (_cpp_begin_message (pfile, level, src_loc, 0))
v_message (msgid, ap);
va_end (ap);
}
void
cpp_error_with_line (cpp_reader *pfile, int level,
source_location src_loc, unsigned int column,
const char *msgid, ...)
{
va_list ap;
va_start (ap, msgid);
if (_cpp_begin_message (pfile, level, src_loc, column))
v_message (msgid, ap);
va_end (ap);
}
void
cpp_errno (cpp_reader *pfile, int level, const char *msgid)
{
if (msgid[0] == '\0')
msgid = _("stdout");
cpp_error (pfile, level, "%s: %s", msgid, xstrerror (errno));
}