X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=diagnostic.c;h=7d8c0abaa2ed118a10e8f28c0ccb311660c69a13;hb=94d190a7624c8ddb2e239ff6bac9b63571098b3c;hp=c1a4ad8f57cac5631a3451fbef3f83f8424cf210;hpb=63cf7273d8b513ca93148ba80fac753145789f46;p=cparser diff --git a/diagnostic.c b/diagnostic.c index c1a4ad8..7d8c0ab 100644 --- a/diagnostic.c +++ b/diagnostic.c @@ -1,11 +1,32 @@ +/* + * This file is part of cparser. + * Copyright (C) 2007-2008 Matthias Braun + * + * 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 + * of the License, 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 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ #include #include -#include "adt/error.h" -#include "ast.h" #include "diagnostic.h" +#include "adt/error.h" +#include "symbol_t.h" #include "token_t.h" +#include "ast.h" #include "type.h" +#include "warning.h" /** Number of occurred diagnostics. */ unsigned diagnostic_count = 0; @@ -13,12 +34,18 @@ unsigned diagnostic_count = 0; unsigned error_count = 0; /** Number of occurred warnings. */ unsigned warning_count = 0; -/** true if warnings should be inhibited */ -bool inhibit_all_warnings = false; -/** true if warnings should be treated as errors */ -bool warnings_are_errors = false; -/** true if the first error should stop the compilation */ -bool fatal_errors = false; + +static const source_position_t *curr_pos = NULL; + +/** + * prints an additional source position + */ +static void print_source_position(FILE *out, const source_position_t *pos) +{ + fprintf(out, "at line %u", pos->linenr); + if (curr_pos == NULL || curr_pos->input_name != pos->input_name) + fprintf(out, " of \"%s\"", pos->input_name); +} /** * Issue a diagnostic message. @@ -42,7 +69,7 @@ static void diagnosticvf(const char *const fmt, va_list ap) case 'C': { const wint_t val = va_arg(ap, wint_t); - fputwc(val, stderr); + fprintf(stderr, "%lc", val); break; } @@ -64,9 +91,18 @@ static void diagnosticvf(const char *const fmt, va_list ap) break; } + case 'u': { + const unsigned int val = va_arg(ap, unsigned int); + fprintf(stderr, "%u", val); + break; + } + case 'Y': { const symbol_t *const symbol = va_arg(ap, const symbol_t*); - fputs(symbol->string, stderr); + if (symbol == NULL) + fputs("(null)", stderr); + else + fputs(symbol->string, stderr); break; } @@ -92,6 +128,12 @@ static void diagnosticvf(const char *const fmt, va_list ap) break; } + case 't': { + const token_t *const token = va_arg(ap, const token_t*); + print_pp_token(stderr, token); + break; + } + case 'K': { const token_t* const token = va_arg(ap, const token_t*); print_token(stderr, token); @@ -100,7 +142,7 @@ static void diagnosticvf(const char *const fmt, va_list ap) case 'k': { if (extended) { - bool first = false; + bool first = true; va_list* toks = va_arg(ap, va_list*); const char* const delimiter = va_arg(ap, const char*); for (;;) { @@ -121,6 +163,12 @@ static void diagnosticvf(const char *const fmt, va_list ap) break; } + case 'P': { + const source_position_t *pos = va_arg(ap, const source_position_t *); + print_source_position(stderr, pos); + break; + } + default: panic("unknown format specifier"); } @@ -135,52 +183,71 @@ void diagnosticf(const char *const fmt, ...) va_list ap; va_start(ap, fmt); ++diagnostic_count; + curr_pos = NULL; diagnosticvf(fmt, ap); va_end(ap); } -static void errorvf(const source_position_t pos, +static void errorvf(const source_position_t *pos, const char *const fmt, va_list ap) { - fprintf(stderr, "%s:%u: error: ", pos.input_name, pos.linenr); + fprintf(stderr, "%s:%u: error: ", pos->input_name, pos->linenr); ++error_count; + curr_pos = pos; diagnosticvf(fmt, ap); fputc('\n', stderr); - if (fatal_errors) - abort(); + if (warning.fatal_errors) + exit(EXIT_FAILURE); } -void errorf(const source_position_t pos, const char *const fmt, ...) + +void errorf(const source_position_t *pos, const char *const fmt, ...) { va_list ap; va_start(ap, fmt); + curr_pos = pos; errorvf(pos, fmt, ap); va_end(ap); - - if (fatal_errors) - exit(1); } -static void warningvf(const source_position_t pos, +static void warningvf(const source_position_t *pos, const char *const fmt, va_list ap) { - fprintf(stderr, "%s:%u: warning: ", pos.input_name, pos.linenr); + fprintf(stderr, "%s:%u: warning: ", pos->input_name, pos->linenr); ++warning_count; + curr_pos = pos; diagnosticvf(fmt, ap); fputc('\n', stderr); } -void warningf(const source_position_t pos, const char *const fmt, ...) +void warningf(const source_position_t *pos, const char *const fmt, ...) { - if (inhibit_all_warnings) - return; - va_list ap; va_start(ap, fmt); - if (warnings_are_errors) { + curr_pos = pos; + if (warning.s_are_errors) { errorvf(pos, fmt, ap); } else { warningvf(pos, fmt, ap); } va_end(ap); } + +static void internal_errorvf(const source_position_t *pos, + const char *const fmt, va_list ap) +{ + fprintf(stderr, "%s:%u: internal error: ", pos->input_name, pos->linenr); + curr_pos = pos; + diagnosticvf(fmt, ap); + fputc('\n', stderr); +} + +void internal_errorf(const source_position_t *pos, const char *const fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + curr_pos = pos; + internal_errorvf(pos, fmt, ap); + va_end(ap); + abort(); +}