forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.c
More file actions
89 lines (74 loc) · 2.24 KB
/
error.c
File metadata and controls
89 lines (74 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include "wcc.h"
// ANSI color codes
#define LOCUS "\e[0;01m" // Bold
#define BRED "\e[1;31m" // Bright red
#define BMAG "\e[1;35m" // Bright magenta
#define RESET "\e[0m" // Reset
static void print_filename_and_linenumber(int is_tty) {
if (is_tty) fprintf(stderr, LOCUS);
fprintf(stderr, "%s:%d: ", cur_filename, cur_line);
if (is_tty) fprintf(stderr, RESET);
}
void panic_with_line_number(char *format, ...) {
va_list ap;
va_start(ap, format);
int is_tty = isatty(2);
print_filename_and_linenumber(is_tty);
if (is_tty) fprintf(stderr, BRED);
fprintf(stderr, "internal error: ");
if (is_tty) fprintf(stderr, RESET);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
// Report an error by itself (no filename or line number) and exit
void simple_error(char *format, ...) {
va_list ap;
va_start(ap, format);
int is_tty = isatty(2);
if (is_tty) fprintf(stderr, BRED);
fprintf(stderr, "error: ");
if (is_tty) fprintf(stderr, RESET);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
// Report an error with a filename and line number and exit
void _error(char *format, va_list ap) {
if (compile_phase == CP_PREPROCESSING) get_cpp_filename_and_line();
int is_tty = isatty(2);
print_filename_and_linenumber(is_tty);
if (is_tty) fprintf(stderr, BRED);
fprintf(stderr, "error: ");
if (is_tty) fprintf(stderr, RESET);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
void error(char *format, ...) {
va_list ap;
va_start(ap, format);
_error(format, ap);
}
// Report a warning with a filename and line number
void warning(char *format, ...) {
va_list ap;
va_start(ap, format);
if (opt_warnings_are_errors) _error(format, ap);
if (compile_phase == CP_PREPROCESSING) get_cpp_filename_and_line();
int is_tty = isatty(2);
print_filename_and_linenumber(is_tty);
if (is_tty) fprintf(stderr, BMAG);
fprintf(stderr, "warning: ");
if (is_tty) fprintf(stderr, RESET);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
va_end(ap);
}