2011-01-24 12:54:57 +00:00
|
|
|
/*
|
|
|
|
diagnostic.c
|
|
|
|
|
|
|
|
Diagnostic messages.
|
|
|
|
|
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2011/01/24
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <QF/dstring.h>
|
|
|
|
|
|
|
|
#include "qfcc.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "diagnostic.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "strpool.h"
|
|
|
|
|
2018-10-09 03:46:13 +00:00
|
|
|
diagnostic_hook bug_hook;
|
|
|
|
diagnostic_hook error_hook;
|
|
|
|
diagnostic_hook warning_hook;
|
|
|
|
diagnostic_hook notice_hook;
|
|
|
|
|
2011-01-24 12:54:57 +00:00
|
|
|
static void
|
|
|
|
report_function (expr_t *e)
|
|
|
|
{
|
|
|
|
static function_t *last_func = (function_t *)-1L;
|
|
|
|
static string_t last_file;
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
srcline_t *srcline;
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
file = e->file;
|
|
|
|
|
|
|
|
if (file != last_file) {
|
|
|
|
for (srcline = pr.srcline_stack; srcline; srcline = srcline->next)
|
|
|
|
fprintf (stderr, "In file included from %s:%d:\n",
|
|
|
|
GETSTR (srcline->source_file), srcline->source_line);
|
|
|
|
}
|
|
|
|
last_file = file;
|
|
|
|
if (current_func != last_func) {
|
|
|
|
if (current_func) {
|
|
|
|
fprintf (stderr, "%s: In function `%s':\n", GETSTR (file),
|
|
|
|
current_func->name);
|
|
|
|
} else if (current_class) {
|
|
|
|
fprintf (stderr, "%s: In class `%s':\n", GETSTR (file),
|
|
|
|
get_class_name (current_class, 1));
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s: At top level:\n", GETSTR (file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_func = current_func;
|
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static __attribute__((format(printf, 4, 0))) void
|
2018-09-09 09:09:32 +00:00
|
|
|
format_message (dstring_t *message, const char *msg_type, expr_t *e,
|
|
|
|
const char *fmt, va_list args)
|
2011-01-24 12:54:57 +00:00
|
|
|
{
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
int line = pr.source_line;
|
2018-09-09 09:09:32 +00:00
|
|
|
const char *colon = fmt ? ": " : "";
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
|
|
|
dsprintf (message, "%s:%d: %s%s", GETSTR (file), line, msg_type, colon);
|
|
|
|
if (fmt) {
|
|
|
|
davsprintf (message, fmt, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 00:31:00 +00:00
|
|
|
static __attribute__((format(printf, 4, 0))) void
|
|
|
|
__warning (expr_t *e, const char *file, int line,
|
|
|
|
const char *fmt, va_list args)
|
2018-09-09 09:09:32 +00:00
|
|
|
{
|
2020-02-23 14:10:56 +00:00
|
|
|
static int promoted = 0;
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_t *message = dstring_new ();
|
2011-01-24 12:54:57 +00:00
|
|
|
|
|
|
|
report_function (e);
|
|
|
|
if (options.warnings.promote) {
|
2020-02-23 14:10:56 +00:00
|
|
|
if (!promoted) {
|
|
|
|
promoted = 1; // want to do this only once
|
|
|
|
fprintf (stderr, "%s: warnings treated as errors\n", "qfcc");
|
|
|
|
}
|
2011-01-24 12:54:57 +00:00
|
|
|
pr.error_count++;
|
2020-02-23 14:10:56 +00:00
|
|
|
format_message (message, "error", e, fmt, args);
|
|
|
|
} else {
|
|
|
|
format_message (message, "warning", e, fmt, args);
|
2011-01-24 12:54:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 15:22:13 +00:00
|
|
|
if (options.verbosity) {
|
2018-10-13 00:31:00 +00:00
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
|
|
|
}
|
2018-10-09 03:46:13 +00:00
|
|
|
if (warning_hook) {
|
|
|
|
warning_hook (message->str);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
}
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_delete (message);
|
2011-01-24 12:54:57 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 04:28:45 +00:00
|
|
|
void
|
2018-10-12 07:01:11 +00:00
|
|
|
_debug (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2011-01-28 04:28:45 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
2020-02-23 15:22:13 +00:00
|
|
|
if (options.verbosity < 2)
|
2011-01-28 04:28:45 +00:00
|
|
|
return;
|
|
|
|
|
2018-09-09 09:09:32 +00:00
|
|
|
report_function (e);
|
2011-01-28 04:28:45 +00:00
|
|
|
va_start (args, fmt);
|
2016-01-07 11:08:07 +00:00
|
|
|
{
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_t *message = dstring_new ();
|
2011-01-28 04:28:45 +00:00
|
|
|
|
2018-09-09 09:09:32 +00:00
|
|
|
format_message (message, "debug", e, fmt, args);
|
2018-10-12 07:01:11 +00:00
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
2018-09-09 09:09:32 +00:00
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
dstring_delete (message);
|
2011-01-28 04:28:45 +00:00
|
|
|
}
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2020-02-15 09:44:53 +00:00
|
|
|
static __attribute__((noreturn, format(printf, 4, 0))) void
|
|
|
|
__internal_error (expr_t *e, const char *file, int line,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
dstring_t *message = dstring_new ();
|
|
|
|
|
|
|
|
report_function (e);
|
|
|
|
|
|
|
|
format_message (message, "internal error", e, fmt, args);
|
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
dstring_delete (message);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2012-05-03 04:24:24 +00:00
|
|
|
void
|
2018-10-12 07:01:11 +00:00
|
|
|
_bug (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2012-05-03 04:24:24 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
2018-09-09 09:09:32 +00:00
|
|
|
|
2020-02-15 09:44:53 +00:00
|
|
|
if (options.bug.silent)
|
|
|
|
return;
|
2012-05-03 04:24:24 +00:00
|
|
|
|
|
|
|
va_start (args, fmt);
|
2020-02-15 09:44:53 +00:00
|
|
|
if (options.bug.promote) {
|
|
|
|
__internal_error (e, file, line, fmt, args);
|
|
|
|
}
|
|
|
|
|
2018-09-09 09:09:32 +00:00
|
|
|
{
|
|
|
|
dstring_t *message = dstring_new ();
|
2012-05-03 04:24:24 +00:00
|
|
|
|
2020-02-15 09:44:53 +00:00
|
|
|
report_function (e);
|
|
|
|
|
2018-09-09 09:09:32 +00:00
|
|
|
format_message (message, "BUG", e, fmt, args);
|
2018-10-12 07:01:11 +00:00
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
2018-10-09 03:46:13 +00:00
|
|
|
if (bug_hook) {
|
|
|
|
bug_hook (message->str);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
}
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_delete (message);
|
2012-05-03 04:24:24 +00:00
|
|
|
}
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
2011-01-24 12:54:57 +00:00
|
|
|
expr_t *
|
2018-10-13 00:31:00 +00:00
|
|
|
_notice (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2011-01-24 12:54:57 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (options.notices.silent)
|
|
|
|
return e;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
if (options.notices.promote) {
|
2018-10-13 00:31:00 +00:00
|
|
|
__warning (e, file, line, fmt, args);
|
2011-01-24 12:54:57 +00:00
|
|
|
} else {
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_t *message = dstring_new ();
|
2011-01-24 12:54:57 +00:00
|
|
|
|
|
|
|
report_function (e);
|
2018-09-09 09:09:32 +00:00
|
|
|
|
|
|
|
format_message (message, "notice", e, fmt, args);
|
2020-02-23 15:22:13 +00:00
|
|
|
if (options.verbosity) {
|
2018-10-13 00:31:00 +00:00
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
|
|
|
}
|
2018-10-09 03:46:13 +00:00
|
|
|
if (notice_hook) {
|
|
|
|
notice_hook (message->str);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
}
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_delete (message);
|
2011-01-24 12:54:57 +00:00
|
|
|
}
|
|
|
|
va_end (args);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
2018-10-13 00:31:00 +00:00
|
|
|
_warning (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2011-01-24 12:54:57 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2018-10-13 00:31:00 +00:00
|
|
|
__warning (e, file, line, fmt, args);
|
2011-01-24 12:54:57 +00:00
|
|
|
va_end (args);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-10-12 04:57:59 +00:00
|
|
|
_internal_error (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2011-01-24 12:54:57 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
2020-02-15 09:44:53 +00:00
|
|
|
__internal_error (e, file, line, fmt, args);
|
2011-01-24 12:54:57 +00:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
2018-10-13 00:31:00 +00:00
|
|
|
_error (expr_t *e, const char *file, int line, const char *fmt, ...)
|
2011-01-24 12:54:57 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
2018-10-11 13:03:14 +00:00
|
|
|
pr.error_count++;
|
|
|
|
|
2018-09-09 09:09:32 +00:00
|
|
|
report_function (e);
|
|
|
|
|
2011-01-24 12:54:57 +00:00
|
|
|
va_start (args, fmt);
|
2018-09-09 09:09:32 +00:00
|
|
|
{
|
|
|
|
dstring_t *message = dstring_new ();
|
|
|
|
|
|
|
|
format_message (message, "error", e, fmt, args);
|
2020-02-23 15:22:13 +00:00
|
|
|
if (options.verbosity) {
|
2018-10-13 00:31:00 +00:00
|
|
|
dasprintf (message, " (%s:%d)", file, line);
|
|
|
|
}
|
2018-10-09 03:46:13 +00:00
|
|
|
if (error_hook) {
|
|
|
|
error_hook (message->str);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s\n", message->str);
|
|
|
|
}
|
2018-09-09 09:09:32 +00:00
|
|
|
dstring_delete (message);
|
|
|
|
}
|
2011-01-24 12:54:57 +00:00
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
if (!e)
|
|
|
|
e = new_expr ();
|
|
|
|
e->type = ex_error;
|
|
|
|
return e;
|
|
|
|
}
|