From 0e9d159bc2e95d71ae2051957397bc689e020e42 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 8 Jan 2014 18:20:55 -0800 Subject: [PATCH] Improvements to error reporting. In particular, we now append a newline in reader_shell_test, so that there's always a statement terminator. Otherwise commands like 'echo |' would not be considered an error (just incomplete). --- parse_execution.cpp | 34 ++++++++++++++++++++-------------- reader.cpp | 15 ++++++++++----- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/parse_execution.cpp b/parse_execution.cpp index c628b1d63..518ff479a 100644 --- a/parse_execution.cpp +++ b/parse_execution.cpp @@ -638,20 +638,26 @@ parse_execution_result_t parse_execution_context_t::run_while_statement(const pa /* Reports an error. Always returns parse_execution_errored, so you can assign the result to an 'errored' variable */ parse_execution_result_t parse_execution_context_t::report_error(const parse_node_t &node, const wchar_t *fmt, ...) { - parse_error_t error; - error.source_start = node.source_start; - error.source_length = node.source_length; - error.code = parse_error_syntax; //hackish - - va_list va; - va_start(va, fmt); - error.text = vformat_string(fmt, va); - va_end(va); - - /* Get a backtrace */ - wcstring backtrace; - const parse_error_list_t error_list = parse_error_list_t(1, error); - parser->get_backtrace(src, error_list, &backtrace); + if (parser->show_errors) + { + /* Create an error */ + parse_error_t error; + error.source_start = node.source_start; + error.source_length = node.source_length; + error.code = parse_error_syntax; //hackish + + va_list va; + va_start(va, fmt); + error.text = vformat_string(fmt, va); + va_end(va); + + /* Get a backtrace */ + wcstring backtrace_and_desc; + const parse_error_list_t error_list = parse_error_list_t(1, error); + parser->get_backtrace(src, error_list, &backtrace_and_desc); + + fprintf(stderr, "%ls", backtrace_and_desc.c_str()); + } return parse_execution_errored; } diff --git a/reader.cpp b/reader.cpp index b03b84a80..c5857f900 100644 --- a/reader.cpp +++ b/reader.cpp @@ -2470,21 +2470,26 @@ void reader_run_command(parser_t &parser, const wcstring &cmd) int reader_shell_test(const wchar_t *b) { + assert(b != NULL); wcstring bstr = b; + + /* Append a newline, to act as a statement terminator */ + bstr.push_back(L'\n'); + parse_error_list_t errors; int res = parse_util_detect_errors(bstr, &errors); if (res & PARSER_TEST_ERROR) { - wcstring sb; - parser_t::principal_parser().get_backtrace(bstr, errors, &sb); + wcstring error_desc; + parser_t::principal_parser().get_backtrace(bstr, errors, &error_desc); // ensure we end with a newline. Also add an initial newline, because it's likely the user just hit enter and so there's junk on the current line - if (! string_suffixes_string(L"\n", sb)) + if (! string_suffixes_string(L"\n", error_desc)) { - sb.push_back(L'\n'); + error_desc.push_back(L'\n'); } - fwprintf(stderr, L"\n%ls", sb.c_str()); + fwprintf(stderr, L"\n%ls", error_desc.c_str()); } return res; }