better error messages are awesome!
[pspp] / src / libpspp / message.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef MESSAGE_H
18 #define MESSAGE_H 1
19
20 #include <stdarg.h>
21 #include <stdbool.h>
22 #include <stddef.h>
23 #include "libpspp/compiler.h"
24
25 struct string;
26
27 /* What kind of message is this? */
28 enum msg_category
29   {
30     MSG_C_GENERAL,              /* General info. */
31     MSG_C_SYNTAX,               /* Messages that relate to syntax files. */
32     MSG_C_DATA,                 /* Messages that relate to data files. */
33     MSG_N_CATEGORIES
34   };
35
36 /* How important a condition is it? */
37 enum msg_severity
38   {
39     MSG_S_ERROR,
40     MSG_S_WARNING,
41     MSG_S_NOTE,
42     MSG_N_SEVERITIES
43   };
44
45 const char *msg_severity_to_string (enum msg_severity);
46
47 /* Combination of a category and a severity for convenience. */
48 enum msg_class
49   {
50     ME, MW, MN,                 /* General error/warning/note. */
51     SE, SW, SN,                 /* Script error/warning/note. */
52     DE, DW, DN,                 /* Data-file error/note. */
53     MSG_CLASS_CNT,
54   };
55
56 static inline enum msg_category
57 msg_class_to_category (enum msg_class class)
58 {
59   return class / 3;
60 }
61
62 static inline enum msg_severity
63 msg_class_to_severity (enum msg_class class)
64 {
65   return class % 3;
66 }
67
68 static inline enum msg_class
69 msg_class_from_category_and_severity (enum msg_category category,
70                                       enum msg_severity severity)
71 {
72   return category * 3 + severity;
73 }
74
75 struct msg_point
76   {
77     int line;                   /* 1-based line number, or 0 if none. */
78     int column;                 /* 1-based column number, or 0 if none. */
79   };
80
81 struct msg_location
82   {
83     const char *file_name;      /* Interned file name, or NULL. */
84     struct lex_source *src;
85     struct msg_point p[2];
86   };
87
88 void msg_location_uninit (struct msg_location *);
89 void msg_location_destroy (struct msg_location *);
90 struct msg_location *msg_location_dup (const struct msg_location *);
91
92 void msg_location_merge (struct msg_location **, const struct msg_location *);
93
94 bool msg_location_is_empty (const struct msg_location *);
95 void msg_location_format (const struct msg_location *, struct string *);
96
97 struct msg_stack
98   {
99     struct msg_location *location;
100     char *description;
101   };
102
103 void msg_stack_destroy (struct msg_stack *);
104 struct msg_stack *msg_stack_dup (const struct msg_stack *);
105
106 /* A message. */
107 struct msg
108   {
109     enum msg_category category; /* Message category. */
110     enum msg_severity severity; /* Message severity. */
111     struct msg_location *location; /* Code location. */
112     struct msg_stack **stack;
113     size_t n_stack;
114     char *command_name;         /* Name of erroneous command, or NULL.  */
115     char *text;                 /* Error text. */
116   };
117
118 /* Initialization. */
119 void msg_set_handler (void (*handler) (const struct msg *, void *lexer),
120                       void *aux);
121
122 /* Working with messages. */
123 struct msg *msg_dup (const struct msg *);
124 void msg_destroy(struct msg *);
125 char *msg_to_string (const struct msg *);
126
127 /* Emitting messages. */
128 void vmsg (enum msg_class, const struct msg_location *,
129            const char *format, va_list args)
130      PRINTF_FORMAT (3, 0);
131 void msg (enum msg_class, const char *format, ...)
132      PRINTF_FORMAT (2, 3);
133 void msg_at (enum msg_class, const struct msg_location *,
134              const char *format, ...)
135      PRINTF_FORMAT (3, 4);
136 void msg_emit (struct msg *);
137
138 void msg_error (int errnum, const char *format, ...)
139   PRINTF_FORMAT (2, 3);
140
141
142 /* Enable and disable messages. */
143 void msg_enable (void);
144 void msg_disable (void);
145
146 /* Error context. */
147 bool msg_ui_too_many_errors (void);
148 void msg_ui_reset_counts (void);
149 bool msg_ui_any_errors (void);
150 void msg_ui_disable_warnings (bool);
151
152
153 /* Used in panic situations only. */
154 const char * prepare_diagnostic_information (void);
155 const char * prepare_fatal_error_message (void);
156 void request_bug_report (const char *msg);
157
158
159 #endif /* message.h */