This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: BZ 6701 - Improve error messages (updated-patch)


Hello everyone:

First of, thanks Frank for all your detailed comments - all incorporated, except for taking care of the exceptionally long lines. In that case too, the `^' wraps up correctly along with the desired column, but the wrapped up source line may cause some issues. Anyhow, the alignment works correctly now for tabs in the source line and for that matter, spaces mixed with tabs. Please have a look at the updated patch, and any further suggestions are more than welcome.

best
-Rajan

diff --git a/ChangeLog b/ChangeLog
index 434a2e9..95544c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2008-10-02  Rajan Arora   <rarora@redhat.com>
+
+	* elaborate.cxx (systemtap_session::print_error_source): New.
+	(systemtap_session::print_error): Call it.
+	(systemtap_session::print_warning): Likewise.
+	* parse.cxx (parser::print_error): Likewise.
+	* session.h (struct systemtap_session::print_error_source):
+	Declare it.
+	* parse.cxx (lexer::get_input_contents): New.
+	(parser::parser): Call it.
+	* parse.h (class lexer::get_input_contents): Declare it.
+	* staptree.h (struct stapfile): New member file_contents.
+
 2008-09-30  Mark Wielaard  <mjw@redhat.com>
 
 	* tapsets.cxx (literal_stmt_for_local): Check if alternatives can be
diff --git a/elaborate.cxx b/elaborate.cxx
index afdc796..54892fc 100644
--- a/elaborate.cxx
+++ b/elaborate.cxx
@@ -1462,6 +1462,12 @@ systemtap_session::print_error (const semantic_error& e)
     {
       seen_errors.insert (message_str[1]);
       cerr << message_str[0];
+
+      if (e.tok1 && user_file->file_contents.size() != 0)
+        print_error_source (cerr, user_file->file_contents, e.tok1);
+
+      if (e.tok2 && user_file->file_contents.size() != 0)
+        print_error_source (cerr, user_file->file_contents, e.tok2);
     }
 
   if (e.chain)
@@ -1469,6 +1475,34 @@ systemtap_session::print_error (const semantic_error& e)
 }
 
 void
+systemtap_session::print_error_source (std::ostream& message,
+                                       std::string& file_contents, const token* tok)
+{
+  unsigned i = 0;
+  unsigned line = tok->location.line;
+  unsigned col = tok->location.column;
+  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 << "\tsource: " << file_contents.substr (start_pos, end_pos-start_pos-1) << endl;
+  message << "\t        ";
+  //Navigate to the appropriate column
+  for (i=start_pos; i<start_pos+col-1; i++)
+    {
+      if(isspace(file_contents[i]))
+	message << file_contents[i];
+      else
+	message << ' ';
+    }
+  message << "^" << endl;
+}
+
+void
 systemtap_session::print_warning (const string& message_str, const token* tok)
 {
   // Duplicate elimination
@@ -1478,6 +1512,7 @@ systemtap_session::print_warning (const string& message_str, const token* tok)
       clog << "WARNING: " << message_str;
       if (tok) { clog << ": "; print_token (clog, tok); }
       clog << endl;
+      print_error_source (clog, user_file->file_contents, tok);
     }
 }
 
diff --git a/parse.cxx b/parse.cxx
index 1c1772f..5fd871e 100644
--- a/parse.cxx
+++ b/parse.cxx
@@ -124,17 +124,22 @@ operator << (ostream& o, const token& t)
 void
 parser::print_error  (const parse_error &pe)
 {
+  string input_contents = input.get_input_contents ();
   cerr << "parse error: " << pe.what () << endl;
 
   if (pe.tok)
     {
       cerr << "\tat: " << *pe.tok << endl;
+      session.print_error_source (cerr, input_contents, pe.tok);
     }
   else
     {
       const token* t = last_t;
       if (t)
-        cerr << "\tsaw: " << *t << endl;
+	{
+	  cerr << "\tsaw: " << *t << endl;
+	  session.print_error_source (cerr, input_contents, t);
+	}
       else
         cerr << "\tsaw: " << input_name << " EOF" << endl;
     }
@@ -588,6 +593,11 @@ lexer::lexer (istream& i, const string& in, systemtap_session& s):
     input_contents.push_back(c);
 }
 
+std::string
+lexer::get_input_contents ()
+{
+  return input_contents;
+}
 
 int
 lexer::input_peek (unsigned n)
@@ -1021,6 +1031,7 @@ parser::parse ()
       delete f;
       return 0;
     }
+  f->file_contents = input.get_input_contents ();
 
   return f;
 }
diff --git a/parse.h b/parse.h
index cf31f4f..1f87dcd 100644
--- a/parse.h
+++ b/parse.h
@@ -71,6 +71,7 @@ class lexer
 public:
   token* scan (bool wildcard=false);
   lexer (std::istream&, const std::string&, systemtap_session&);
+  std::string get_input_contents ();
 
 private:
   int input_get ();
@@ -79,7 +80,7 @@ private:
   int input_peek (unsigned n=0);
   std::istream& input;
   std::string input_name;
-  std::vector<char> input_contents;
+  std::string input_contents;
   int input_pointer; // index into input_contents
   unsigned cursor_suspend_count;
   unsigned cursor_line;
diff --git a/session.h b/session.h
index b8bd971..4746422 100644
--- a/session.h
+++ b/session.h
@@ -179,6 +179,7 @@ struct systemtap_session
   const token* last_token;
   void print_token (std::ostream& o, const token* tok);
   void print_error (const semantic_error& e);
+  void print_error_source (std::ostream&, std::string&, const token* tok);
   void print_warning (const std::string& w, const token* tok = 0);
 
   // reNB: new POD members likely need to be explicitly cleared in the ctor.
diff --git a/staptree.h b/staptree.h
index 770a731..ac229c7 100644
--- a/staptree.h
+++ b/staptree.h
@@ -574,6 +574,7 @@ struct stapfile
   std::vector<functiondecl*> functions;
   std::vector<vardecl*> globals;
   std::vector<embeddedcode*> embeds;
+  std::string file_contents;
   bool privileged;
   stapfile (): privileged (false) {}
   void print (std::ostream& o) const;
diff --git a/testsuite/ChangeLog b/testsuite/ChangeLog
index 4825cd2..ec6c451 100644
--- a/testsuite/ChangeLog
+++ b/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2008-10-02  Rajan Arora   <rarora@redhat.com>
+
+	* systemtap.base/warnings.exp: Allow for source: lines.
+	* parseko/source_context.stp: New file.
+
 2008-10-01  Mark Wielaard  <mjw@redhat.com>
 
 	* semok/thirtythree.stp: Use page->mapping instead of page->inuse
diff --git a/testsuite/parseko/source_context.stp b/testsuite/parseko/source_context.stp
new file mode 100755
index 0000000..403b203
--- /dev/null
+++ b/testsuite/parseko/source_context.stp
@@ -0,0 +1,5 @@
+probe timer.ms(123)
+{
+printf("Probe successful\n")
+				   eeexit ()
+}
diff --git a/testsuite/systemtap.base/warnings.exp b/testsuite/systemtap.base/warnings.exp
index a90860d..f6bfa34 100644
--- a/testsuite/systemtap.base/warnings.exp
+++ b/testsuite/systemtap.base/warnings.exp
@@ -6,6 +6,8 @@ expect {
     -timeout 30
     -re {^WARNING:[^\r\n]*\r\n} { incr ok; exp_continue }
     -re {^[^\r\n]*.ko\r\n} { incr ok; exp_continue }
+    -re {^source:[^\r\n]*\r\n} {exp_continue}
+    -re {^[^\r\n]*\^[^\r\n]*\r\n} {exp_continue}
     timeout { fail "$test (timeout)" }
     eof { }
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]