From 1b1b4cebda9ceaa7a2a2b695a32ecb2588045076 Mon Sep 17 00:00:00 2001 From: Rajan Arora Date: Thu, 9 Oct 2008 19:58:30 -0400 Subject: Display source context for error and warning messages (6701) --- elaborate.cxx | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'elaborate.cxx') diff --git a/elaborate.cxx b/elaborate.cxx index afdc796e..9f0a2a74 100644 --- a/elaborate.cxx +++ b/elaborate.cxx @@ -1426,6 +1426,7 @@ void systemtap_session::print_error (const semantic_error& e) { string message_str[2]; + string align_semantic_error (" "); // NB: we don't print error messages during listing mode. if (listing_mode) return; @@ -1462,22 +1463,64 @@ systemtap_session::print_error (const semantic_error& e) { seen_errors.insert (message_str[1]); cerr << message_str[0]; + + if (e.tok1) + print_error_source (cerr, align_semantic_error, e.tok1); + + if (e.tok2) + print_error_source (cerr, align_semantic_error, e.tok2); } if (e.chain) print_error (* e.chain); } +void +systemtap_session::print_error_source (std::ostream& message, + std::string& align, const token* tok) +{ + unsigned i = 0; + unsigned line = tok->location.line; + unsigned col = tok->location.column; + string file_contents; + if (tok->location.stap_file) + file_contents = tok->location.stap_file->file_contents; + else + //No source to print, silently exit + return; + size_t start_pos = 0, end_pos = 0; + //Navigate to the appropriate line + while (i != line && end_pos != std::string::npos) + { + start_pos = end_pos; + end_pos = file_contents.find ('\n', start_pos) + 1; + i++; + } + message << align << "source: " << file_contents.substr (start_pos, end_pos-start_pos-1) << endl; + message << align << " "; + //Navigate to the appropriate column + for (i=start_pos; i