mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
add --color cli arg to override tty detection
This commit is contained in:
parent
257cf09472
commit
29f24e3c50
@ -42,6 +42,10 @@ void codegen_set_verbose(CodeGen *g, bool verbose) {
|
|||||||
g->verbose = verbose;
|
g->verbose = verbose;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void codegen_set_errmsg_color(CodeGen *g, ErrColor err_color) {
|
||||||
|
g->err_color = err_color;
|
||||||
|
}
|
||||||
|
|
||||||
void codegen_set_strip(CodeGen *g, bool strip) {
|
void codegen_set_strip(CodeGen *g, bool strip) {
|
||||||
g->strip_debug_symbols = strip;
|
g->strip_debug_symbols = strip;
|
||||||
}
|
}
|
||||||
@ -693,7 +697,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
|
|||||||
err->source = source_code;
|
err->source = source_code;
|
||||||
err->line_offsets = tokenization.line_offsets;
|
err->line_offsets = tokenization.line_offsets;
|
||||||
|
|
||||||
print_err_msg(err);
|
print_err_msg(err, g->err_color);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -709,7 +713,7 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
|
|||||||
import_entry->line_offsets = tokenization.line_offsets;
|
import_entry->line_offsets = tokenization.line_offsets;
|
||||||
import_entry->path = source_path;
|
import_entry->path = source_path;
|
||||||
import_entry->fn_table.init(32);
|
import_entry->fn_table.init(32);
|
||||||
import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry);
|
import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
|
||||||
assert(import_entry->root);
|
assert(import_entry->root);
|
||||||
if (g->verbose) {
|
if (g->verbose) {
|
||||||
ast_print(import_entry->root, 0);
|
ast_print(import_entry->root, 0);
|
||||||
@ -756,7 +760,7 @@ void codegen_add_root_code(CodeGen *g, Buf *source_path, Buf *source_code) {
|
|||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < g->errors.length; i += 1) {
|
for (int i = 0; i < g->errors.length; i += 1) {
|
||||||
ErrorMsg *err = g->errors.at(i);
|
ErrorMsg *err = g->errors.at(i);
|
||||||
print_err_msg(err);
|
print_err_msg(err, g->err_color);
|
||||||
}
|
}
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#define ZIG_CODEGEN_HPP
|
#define ZIG_CODEGEN_HPP
|
||||||
|
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
#include "errmsg.hpp"
|
||||||
|
|
||||||
struct CodeGen;
|
struct CodeGen;
|
||||||
|
|
||||||
@ -19,7 +20,6 @@ enum OutType {
|
|||||||
OutTypeObj,
|
OutTypeObj,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
CodeGen *codegen_create(Buf *root_source_dir);
|
CodeGen *codegen_create(Buf *root_source_dir);
|
||||||
|
|
||||||
enum CodeGenBuildType {
|
enum CodeGenBuildType {
|
||||||
@ -30,6 +30,7 @@ void codegen_set_build_type(CodeGen *codegen, CodeGenBuildType build_type);
|
|||||||
void codegen_set_is_static(CodeGen *codegen, bool is_static);
|
void codegen_set_is_static(CodeGen *codegen, bool is_static);
|
||||||
void codegen_set_strip(CodeGen *codegen, bool strip);
|
void codegen_set_strip(CodeGen *codegen, bool strip);
|
||||||
void codegen_set_verbose(CodeGen *codegen, bool verbose);
|
void codegen_set_verbose(CodeGen *codegen, bool verbose);
|
||||||
|
void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
|
||||||
void codegen_set_out_type(CodeGen *codegen, OutType out_type);
|
void codegen_set_out_type(CodeGen *codegen, OutType out_type);
|
||||||
void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
|
void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
|
||||||
|
|
||||||
|
|||||||
@ -8,8 +8,8 @@
|
|||||||
#define GREEN "\x1b[32;1m"
|
#define GREEN "\x1b[32;1m"
|
||||||
#define RESET "\x1b[0m"
|
#define RESET "\x1b[0m"
|
||||||
|
|
||||||
void print_err_msg(ErrorMsg *err) {
|
void print_err_msg(ErrorMsg *err, ErrColor color) {
|
||||||
if (os_stderr_tty()) {
|
if (color == ErrColorOn || (color == ErrColorAuto && os_stderr_tty())) {
|
||||||
fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n",
|
fprintf(stderr, WHITE "%s:%d:%d: " RED "error:" WHITE " %s" RESET "\n",
|
||||||
buf_ptr(err->path),
|
buf_ptr(err->path),
|
||||||
err->line_start + 1, err->column_start + 1,
|
err->line_start + 1, err->column_start + 1,
|
||||||
|
|||||||
@ -11,6 +11,12 @@
|
|||||||
#include "buffer.hpp"
|
#include "buffer.hpp"
|
||||||
#include "list.hpp"
|
#include "list.hpp"
|
||||||
|
|
||||||
|
enum ErrColor {
|
||||||
|
ErrColorAuto,
|
||||||
|
ErrColorOff,
|
||||||
|
ErrColorOn,
|
||||||
|
};
|
||||||
|
|
||||||
struct ErrorMsg {
|
struct ErrorMsg {
|
||||||
int line_start;
|
int line_start;
|
||||||
int column_start;
|
int column_start;
|
||||||
@ -22,6 +28,6 @@ struct ErrorMsg {
|
|||||||
ZigList<int> *line_offsets;
|
ZigList<int> *line_offsets;
|
||||||
};
|
};
|
||||||
|
|
||||||
void print_err_msg(ErrorMsg *msg);
|
void print_err_msg(ErrorMsg *msg, ErrColor color);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
17
src/main.cpp
17
src/main.cpp
@ -25,6 +25,7 @@ static int usage(const char *arg0) {
|
|||||||
" --name [name] override output name\n"
|
" --name [name] override output name\n"
|
||||||
" --output [file] override destination path\n"
|
" --output [file] override destination path\n"
|
||||||
" --verbose turn on compiler debug output\n"
|
" --verbose turn on compiler debug output\n"
|
||||||
|
" --color [auto|off|on] enable or disable colored error messages\n"
|
||||||
, arg0);
|
, arg0);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
@ -43,6 +44,7 @@ struct Build {
|
|||||||
OutType out_type;
|
OutType out_type;
|
||||||
const char *out_name;
|
const char *out_name;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
|
ErrColor color;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int build(const char *arg0, Build *b) {
|
static int build(const char *arg0, Build *b) {
|
||||||
@ -73,6 +75,7 @@ static int build(const char *arg0, Build *b) {
|
|||||||
if (b->out_name)
|
if (b->out_name)
|
||||||
codegen_set_out_name(g, buf_create_from_str(b->out_name));
|
codegen_set_out_name(g, buf_create_from_str(b->out_name));
|
||||||
codegen_set_verbose(g, b->verbose);
|
codegen_set_verbose(g, b->verbose);
|
||||||
|
codegen_set_errmsg_color(g, b->color);
|
||||||
codegen_add_root_code(g, &root_source_name, &root_source_code);
|
codegen_add_root_code(g, &root_source_name, &root_source_code);
|
||||||
codegen_link(g, b->out_file);
|
codegen_link(g, b->out_file);
|
||||||
|
|
||||||
@ -106,7 +109,9 @@ int main(int argc, char **argv) {
|
|||||||
return usage(arg0);
|
return usage(arg0);
|
||||||
} else {
|
} else {
|
||||||
i += 1;
|
i += 1;
|
||||||
if (strcmp(arg, "--output") == 0) {
|
if (i >= argc) {
|
||||||
|
return usage(arg0);
|
||||||
|
} else if (strcmp(arg, "--output") == 0) {
|
||||||
b.out_file = argv[i];
|
b.out_file = argv[i];
|
||||||
} else if (strcmp(arg, "--export") == 0) {
|
} else if (strcmp(arg, "--export") == 0) {
|
||||||
if (strcmp(argv[i], "exe") == 0) {
|
if (strcmp(argv[i], "exe") == 0) {
|
||||||
@ -118,6 +123,16 @@ int main(int argc, char **argv) {
|
|||||||
} else {
|
} else {
|
||||||
return usage(arg0);
|
return usage(arg0);
|
||||||
}
|
}
|
||||||
|
} else if (strcmp(arg, "--color") == 0) {
|
||||||
|
if (strcmp(argv[i], "auto") == 0) {
|
||||||
|
b.color = ErrColorAuto;
|
||||||
|
} else if (strcmp(argv[i], "on") == 0) {
|
||||||
|
b.color = ErrColorOn;
|
||||||
|
} else if (strcmp(argv[i], "off") == 0) {
|
||||||
|
b.color = ErrColorOff;
|
||||||
|
} else {
|
||||||
|
return usage(arg0);
|
||||||
|
}
|
||||||
} else if (strcmp(arg, "--name") == 0) {
|
} else if (strcmp(arg, "--name") == 0) {
|
||||||
b.out_name = argv[i];
|
b.out_name = argv[i];
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -242,6 +242,7 @@ struct ParseContext {
|
|||||||
ZigList<Token> *tokens;
|
ZigList<Token> *tokens;
|
||||||
ZigList<AstNode *> *directive_list;
|
ZigList<AstNode *> *directive_list;
|
||||||
ImportTableEntry *owner;
|
ImportTableEntry *owner;
|
||||||
|
ErrColor err_color;
|
||||||
};
|
};
|
||||||
|
|
||||||
__attribute__ ((format (printf, 3, 4)))
|
__attribute__ ((format (printf, 3, 4)))
|
||||||
@ -262,7 +263,7 @@ static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
|
|||||||
err->source = pc->owner->source_code;
|
err->source = pc->owner->source_code;
|
||||||
err->line_offsets = pc->owner->line_offsets;
|
err->line_offsets = pc->owner->line_offsets;
|
||||||
|
|
||||||
print_err_msg(err);
|
print_err_msg(err, pc->err_color);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1334,8 +1335,9 @@ static AstNode *ast_parse_root(ParseContext *pc, int *token_index) {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner) {
|
AstNode *ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color) {
|
||||||
ParseContext pc = {0};
|
ParseContext pc = {0};
|
||||||
|
pc.err_color = err_color;
|
||||||
pc.owner = owner;
|
pc.owner = owner;
|
||||||
pc.buf = buf;
|
pc.buf = buf;
|
||||||
pc.tokens = tokens;
|
pc.tokens = tokens;
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "list.hpp"
|
#include "list.hpp"
|
||||||
#include "buffer.hpp"
|
#include "buffer.hpp"
|
||||||
#include "tokenizer.hpp"
|
#include "tokenizer.hpp"
|
||||||
|
#include "errmsg.hpp"
|
||||||
|
|
||||||
struct AstNode;
|
struct AstNode;
|
||||||
struct CodeGenNode;
|
struct CodeGenNode;
|
||||||
@ -199,7 +200,7 @@ void ast_token_error(Token *token, const char *format, ...);
|
|||||||
|
|
||||||
|
|
||||||
// This function is provided by generated code, generated by parsergen.cpp
|
// This function is provided by generated code, generated by parsergen.cpp
|
||||||
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner);
|
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
|
||||||
|
|
||||||
const char *node_type_str(NodeType node_type);
|
const char *node_type_str(NodeType node_type);
|
||||||
|
|
||||||
|
|||||||
@ -89,6 +89,7 @@ struct CodeGen {
|
|||||||
int version_minor;
|
int version_minor;
|
||||||
int version_patch;
|
int version_patch;
|
||||||
bool verbose;
|
bool verbose;
|
||||||
|
ErrColor err_color;
|
||||||
ImportTableEntry *root_import;
|
ImportTableEntry *root_import;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -48,6 +48,8 @@ static void add_simple_case(const char *case_name, const char *source, const cha
|
|||||||
test_case->compiler_args.append("--release");
|
test_case->compiler_args.append("--release");
|
||||||
test_case->compiler_args.append("--strip");
|
test_case->compiler_args.append("--strip");
|
||||||
test_case->compiler_args.append("--verbose");
|
test_case->compiler_args.append("--verbose");
|
||||||
|
test_case->compiler_args.append("--color");
|
||||||
|
test_case->compiler_args.append("on");
|
||||||
|
|
||||||
test_cases.append(test_case);
|
test_cases.append(test_case);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user