authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-01 01:06:10-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-01 01:06:10-07:00
log29f24e3c5066e7cb28876d40a811a4a64f9d4b33
tree01d27aa3253da35af6fe8a5d3f69cffab08773bd
parent257cf09472ce5f4a51bf39808e119717fa0e4280

add --color cli arg to override tty detection


9 files changed, 43 insertions(+), 11 deletions(-)

src/codegen.cpp+7-3
......@@ -42,6 +42,10 @@ void codegen_set_verbose(CodeGen *g, bool verbose) {
4242 g->verbose = verbose;
4343}
4444
45void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) {
46 g->err_color = err_color;
47}
48
4549void codegen_set_strip(CodeGen *g, bool strip) {
4650 g->strip_debug_symbols = strip;
4751}
......@@ -693,7 +697,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
693697 err->source = source_code;
694698 err->line_offsets = tokenization.line_offsets;
695699
696 print_err_msg(err);
700 print_err_msg(err, g->err_color);
697701 exit(1);
698702 }
699703
......@@ -709,7 +713,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
709713 import_entry->line_offsets = tokenization.line_offsets;
710714 import_entry->path = source_path;
711715 import_entry->fn_table.init(32);
712 import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry);
716 import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
713717 assert(import_entry->root);
714718 if (g->verbose) {
715719 ast_print(import_entry->root, 0);
......@@ -756,7 +760,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
756760 } else {
757761 for (int i = 0; i < g->errors.length; i += 1) {
758762 ErrorMsg *err = g->errors.at(i);
759 print_err_msg(err);
763 print_err_msg(err, g->err_color);
760764 }
761765 exit(1);
762766 }
src/codegen.hpp+2-1
......@@ -9,6 +9,7 @@
99#define ZIG_CODEGEN_HPP
1010
1111#include "parser.hpp"
12#include "errmsg.hpp"
1213
1314struct CodeGen;
1415
......@@ -19,7 +20,6 @@ enum OutType {
1920 OutTypeObj,
2021};
2122
22
2323CodeGen *codegen_create(Buf *root_source_dir);
2424
2525enum CodeGenBuildType {
......@@ -30,6 +30,7 @@ void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type);
3030void codegen_set_is_static(CodeGen *codegen, bool is_static);
3131void codegen_set_strip(CodeGen *codegen, bool strip);
3232void codegen_set_verbose(CodeGen *codegen, bool verbose);
33void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
3334void codegen_set_out_type(CodeGen *codegen, OutType out_type);
3435void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
3536
src/errmsg.cpp+2-2
......@@ -8,8 +8,8 @@
88#define GREEN "\x1b[32;1m"
99#define RESET "\x1b[0m"
1010
11void print_err_msg(ErrorMsg *err) {
12 if (os_stderr_tty()) {
11void print_err_msg(ErrorMsg *err, ErrColor color) {
12 if (color == ErrColorOn || (color == ErrColorAuto && os_stderr_tty())) {
1313 fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n",
1414 buf_ptr(err->path),
1515 err->line_start + 1, err->column_start + 1,
src/errmsg.hpp+7-1
......@@ -11,6 +11,12 @@
1111#include "buffer.hpp"
1212#include "list.hpp"
1313
14enum ErrColor {
15 ErrColorAuto,
16 ErrColorOff,
17 ErrColorOn,
18};
19
1420struct ErrorMsg {
1521 int line_start;
1622 int column_start;
......@@ -22,6 +28,6 @@ struct ErrorMsg {
2228 ZigList<int> *line_offsets;
2329};
2430
25void print_err_msg(ErrorMsg *msg);
31void print_err_msg(ErrorMsg *msg, ErrColor color);
2632
2733#endif
src/main.cpp+16-1
......@@ -25,6 +25,7 @@ static int usage(const char *arg0) {
2525 " --name [name] override output name\n"
2626 " --output [file] override destination path\n"
2727 " --verbose turn on compiler debug output\n"
28 " --color [auto|off|on] enable or disable colored error messages\n"
2829 , arg0);
2930 return EXIT_FAILURE;
3031}
......@@ -43,6 +44,7 @@ struct Build {
4344 OutType out_type;
4445 const char *out_name;
4546 bool verbose;
47 ErrColor color;
4648};
4749
4850static int build(const char *arg0, Build *b) {
......@@ -73,6 +75,7 @@ static int build(const char *arg0, Build *b) {
7375 if (b->out_name)
7476 codegen_set_out_name(g, buf_create_from_str(b->out_name));
7577 codegen_set_verbose(g, b->verbose);
78 codegen_set_errmsg_color(g, b->color);
7679 codegen_add_root_code(g, &root_source_name, &root_source_code);
7780 codegen_link(g, b->out_file);
7881
......@@ -106,7 +109,9 @@ int main(int argc, char **argv) {
106109 return usage(arg0);
107110 } else {
108111 i += 1;
109 if (strcmp(arg, "--output") == 0) {
112 if (i >= argc) {
113 return usage(arg0);
114 } else if (strcmp(arg, "--output") == 0) {
110115 b.out_file = argv[i];
111116 } else if (strcmp(arg, "--export") == 0) {
112117 if (strcmp(argv[i], "exe") == 0) {
......@@ -118,6 +123,16 @@ int main(int argc, char **argv) {
118123 } else {
119124 return usage(arg0);
120125 }
126 } else if (strcmp(arg, "--color") == 0) {
127 if (strcmp(argv[i], "auto") == 0) {
128 b.color = ErrColorAuto;
129 } else if (strcmp(argv[i], "on") == 0) {
130 b.color = ErrColorOn;
131 } else if (strcmp(argv[i], "off") == 0) {
132 b.color = ErrColorOff;
133 } else {
134 return usage(arg0);
135 }
121136 } else if (strcmp(arg, "--name") == 0) {
122137 b.out_name = argv[i];
123138 } else {
src/parser.cpp+4-2
......@@ -242,6 +242,7 @@ struct ParseContext {
242242 ZigList<Token> *tokens;
243243 ZigList<AstNode *> *directive_list;
244244 ImportTableEntry *owner;
245 ErrColor err_color;
245246};
246247
247248__attribute__ ((format (printf, 3, 4)))
......@@ -262,7 +263,7 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
262263 err->source = pc->owner->source_code;
263264 err->line_offsets = pc->owner->line_offsets;
264265
265 print_err_msg(err);
266 print_err_msg(err, pc->err_color);
266267 exit(EXIT_FAILURE);
267268}
268269
......@@ -1334,8 +1335,9 @@ static AstNode *ast_parse_root(ParseContext *pc, int *token_index) {
13341335 return node;
13351336}
13361337
1337AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner) {
1338AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color) {
13381339 ParseContext pc = {0};
1340 pc.err_color = err_color;
13391341 pc.owner = owner;
13401342 pc.buf = buf;
13411343 pc.tokens = tokens;
src/parser.hpp+2-1
......@@ -11,6 +11,7 @@
1111#include "list.hpp"
1212#include "buffer.hpp"
1313#include "tokenizer.hpp"
14#include "errmsg.hpp"
1415
1516struct AstNode;
1617struct CodeGenNode;
......@@ -199,7 +200,7 @@ void ast_token_error(Token *token, const char *format, ...);
199200
200201
201202// This function is provided by generated code, generated by parsergen.cpp
202AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner);
203AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
203204
204205const char *node_type_str(NodeType node_type);
205206
src/semantic_info.hpp+1
......@@ -89,6 +89,7 @@ struct CodeGen {
8989 int version_minor;
9090 int version_patch;
9191 bool verbose;
92 ErrColor err_color;
9293 ImportTableEntry *root_import;
9394};
9495
test/run_tests.cpp+2
......@@ -48,6 +48,8 @@ static void add_simple_case(const char *case_name, const char *source, const cha
4848 test_case->compiler_args.append("--release");
4949 test_case->compiler_args.append("--strip");
5050 test_case->compiler_args.append("--verbose");
51 test_case->compiler_args.append("--color");
52 test_case->compiler_args.append("on");
5153
5254 test_cases.append(test_case);
5355}