]> git.xonotic.org Git - xonotic/gmqcc.git/blob - conout.cpp
ir: fix vector negation using the nil value
[xonotic/gmqcc.git] / conout.cpp
1 #include <stdio.h>
2 #include "gmqcc.h"
3
4 #define GMQCC_IS_STDOUT(X) ((X) == stdout)
5 #define GMQCC_IS_STDERR(X) ((X) == stderr)
6 #define GMQCC_IS_DEFINE(X) (GMQCC_IS_STDERR(X) || GMQCC_IS_STDOUT(X))
7
8 struct con_t {
9     FILE *handle_err;
10     FILE *handle_out;
11     int color_err;
12     int color_out;
13 };
14
15 static con_t console;
16
17 /*
18  * Enables color on output if supported.
19  * NOTE: The support for checking colors is nullptr.  On windows this will
20  * always work, on *nix it depends if the term has colors.
21  *
22  * NOTE: This prevents colored output to piped stdout/err via isatty
23  * checks.
24  */
25 static void con_enablecolor(void) {
26     console.color_err = util_isatty(console.handle_err);
27     console.color_out = util_isatty(console.handle_out);
28 }
29
30 /*
31  * Does a write to the handle with the format string and list of
32  * arguments.  This colorizes for windows as well via translate
33  * step.
34  */
35 static int con_write(FILE *handle, const char *fmt, va_list va) {
36     return vfprintf(handle, fmt, va);
37 }
38
39 /**********************************************************************
40  * EXPOSED INTERFACE BEGINS
41  *********************************************************************/
42
43 void con_close() {
44     if (!GMQCC_IS_DEFINE(console.handle_err))
45         fclose(console.handle_err);
46     if (!GMQCC_IS_DEFINE(console.handle_out))
47         fclose(console.handle_out);
48 }
49
50 void con_color(int state) {
51     if (state)
52         con_enablecolor();
53     else {
54         console.color_err = 0;
55         console.color_out = 0;
56     }
57 }
58
59 void con_init() {
60     console.handle_err = stderr;
61     console.handle_out = stdout;
62     con_enablecolor();
63 }
64
65 void con_reset() {
66     con_close();
67     con_init();
68 }
69
70 /*
71  * Defaultizer because stdio.h shouldn't be used anywhere except here
72  * and inside file.c To prevent mis-match of wrapper-interfaces.
73  */
74 FILE *con_default_out() {
75     return console.handle_out = stdout;
76 }
77
78 FILE *con_default_err() {
79     return console.handle_err = stderr;
80 }
81
82 int con_verr(const char *fmt, va_list va) {
83     return con_write(console.handle_err, fmt, va);
84 }
85 int con_vout(const char *fmt, va_list va) {
86     return con_write(console.handle_out, fmt, va);
87 }
88
89 /*
90  * Standard stdout/stderr printf functions used generally where they need
91  * to be used.
92  */
93 int con_err(const char *fmt, ...) {
94     va_list va;
95     int ln = 0;
96     va_start(va, fmt);
97     con_verr(fmt, va);
98     va_end(va);
99     return ln;
100 }
101 int con_out(const char *fmt, ...) {
102     va_list va;
103     int ln = 0;
104     va_start(va, fmt);
105     con_vout(fmt, va);
106     va_end (va);
107     return ln;
108 }
109
110 /*
111  * Utility console message writes for lexer contexts.  These will allow
112  * for reporting of file:line based on lexer context, These are used
113  * heavily in the parser/ir/ast.
114  */
115 static void con_vprintmsg_c(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap, const char *condname) {
116     /* color selection table */
117     static int sel[] = {
118         CON_WHITE,
119         CON_CYAN,
120         CON_RED
121     };
122
123     int  err                             = !!(level == LVL_ERROR);
124     int  color                           = (err) ? console.color_err : console.color_out;
125     int (*print) (const char *, ...)     = (err) ? &con_err          : &con_out;
126     int (*vprint)(const char *, va_list) = (err) ? &con_verr         : &con_vout;
127
128     if (color)
129         print("\033[0;%dm%s:%d:%d: \033[0;%dm%s: \033[0m", CON_CYAN, name, (int)line, (int)column, sel[level], msgtype);
130     else
131         print("%s:%d:%d: %s: ", name, (int)line, (int)column, msgtype);
132
133     vprint(msg, ap);
134     if (condname)
135         print(" [%s]\n", condname);
136     else
137         print("\n");
138 }
139
140 void con_vprintmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, va_list ap) {
141     con_vprintmsg_c(level, name, line, column, msgtype, msg, ap, nullptr);
142 }
143
144 void con_printmsg(int level, const char *name, size_t line, size_t column, const char *msgtype, const char *msg, ...) {
145     va_list   va;
146     va_start(va, msg);
147     con_vprintmsg(level, name, line, column, msgtype, msg, va);
148     va_end  (va);
149 }
150
151 void con_cvprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, va_list ap) {
152     con_vprintmsg(lvl, ctx.file, ctx.line, ctx.column, msgtype, msg, ap);
153 }
154
155 void con_cprintmsg(lex_ctx_t ctx, int lvl, const char *msgtype, const char *msg, ...) {
156     va_list   va;
157     va_start(va, msg);
158     con_cvprintmsg(ctx, lvl, msgtype, msg, va);
159     va_end  (va);
160 }
161
162 /* General error interface: TODO seperate as part of the compiler front-end */
163 size_t compile_errors   = 0;
164 size_t compile_warnings = 0;
165 size_t compile_Werrors  = 0;
166 static lex_ctx_t first_werror;
167
168 void compile_show_werrors()
169 {
170     con_cprintmsg(first_werror, LVL_ERROR, "first warning", "was here");
171 }
172
173 void vcompile_error(lex_ctx_t ctx, const char *msg, va_list ap)
174 {
175     ++compile_errors;
176     con_cvprintmsg(ctx, LVL_ERROR, "error", msg, ap);
177 }
178
179 void compile_error_(lex_ctx_t ctx, const char *msg, ...)
180 {
181     va_list ap;
182     va_start(ap, msg);
183     vcompile_error(ctx, msg, ap);
184     va_end(ap);
185 }
186
187 bool GMQCC_WARN vcompile_warning(lex_ctx_t ctx, int warntype, const char *fmt, va_list ap)
188 {
189     const char *msgtype = "warning";
190     int         lvl     = LVL_WARNING;
191     char        warn_name[1024];
192
193     if (!OPTS_WARN(warntype))
194         return false;
195
196     warn_name[0] = '-';
197     warn_name[1] = 'W';
198     (void)util_strtononcmd(opts_warn_list[warntype].name, warn_name+2, sizeof(warn_name)-2);
199
200     ++compile_warnings;
201     if (OPTS_WERROR(warntype)) {
202         if (!compile_Werrors)
203             first_werror = ctx;
204         ++compile_Werrors;
205         msgtype = "Werror";
206         if (OPTS_FLAG(BAIL_ON_WERROR)) {
207             msgtype = "error";
208             ++compile_errors;
209         }
210         lvl = LVL_ERROR;
211     }
212
213     con_vprintmsg_c(lvl, ctx.file, ctx.line, ctx.column, msgtype, fmt, ap, warn_name);
214
215     return OPTS_WERROR(warntype) && OPTS_FLAG(BAIL_ON_WERROR);
216 }
217
218 bool GMQCC_WARN compile_warning_(lex_ctx_t ctx, int warntype, const char *fmt, ...)
219 {
220     bool r;
221     va_list ap;
222     va_start(ap, fmt);
223     r = vcompile_warning(ctx, warntype, fmt, ap);
224     va_end(ap);
225     return r;
226 }