Add a DiagnosticManager replace error streams in the expression parser.

We want to do a better job presenting errors that occur when evaluating
expressions. Key to this effort is getting away from a model where all
errors are spat out onto a stream where the client has to take or leave
all of them.

To this end, this patch adds a new class, DiagnosticManager, which
contains errors produced by the compiler or by LLDB as an expression
is created. The DiagnosticManager can dump itself to a log as well as
to a string. Clients will (in the future) be able to filter out the
errors they're interested in by ID or present subsets of these errors
to the user.

This patch is not intended to change the *users* of errors - only to
thread DiagnosticManagers to all the places where streams are used. I
also attempt to standardize our use of errors a bit, removing trailing
newlines and making clients omit 'error:', 'warning:' etc. and instead
pass the Severity flag.

The patch is testsuite-neutral, with modifications to one part of the
MI tests because it relied on "error: error:" being erroneously
printed. This patch fixes the MI variable handling and the testcase.

<rdar://problem/22864976>

llvm-svn: 263859
diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
index 11f7f84..25ac0e2 100644
--- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
+++ b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
@@ -326,11 +326,9 @@
 }
 
 bool
-ClangUserExpression::Parse (Stream &error_stream,
-                            ExecutionContext &exe_ctx,
-                            lldb_private::ExecutionPolicy execution_policy,
-                            bool keep_result_in_memory,
-                            bool generate_debug_info)
+ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
+                           lldb_private::ExecutionPolicy execution_policy, bool keep_result_in_memory,
+                           bool generate_debug_info)
 {
     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
 
@@ -346,13 +344,13 @@
         }
         else
         {
-            error_stream.PutCString ("error: couldn't start parsing (no persistent data)");
+            diagnostic_manager.PutCString(eDiagnosticSeverityError, "couldn't start parsing (no persistent data)");
             return false;
         }
     }
     else
     {
-        error_stream.PutCString ("error: couldn't start parsing (no target)");
+        diagnostic_manager.PutCString(eDiagnosticSeverityError, "error: couldn't start parsing (no target)");
         return false;
     }
 
@@ -360,7 +358,7 @@
 
     if (!err.Success())
     {
-        error_stream.Printf("warning: %s\n", err.AsCString());
+        diagnostic_manager.PutCString(eDiagnosticSeverityWarning, err.AsCString());
     }
 
     StreamString m_transformed_stream;
@@ -418,7 +416,7 @@
 
     if (!source_code->GetText(m_transformed_text, lang_type, m_const_object, m_in_static_method, exe_ctx))
     {
-        error_stream.PutCString ("error: couldn't construct expression body");
+        diagnostic_manager.PutCString(eDiagnosticSeverityError, "couldn't construct expression body");
         return false;
     }
 
@@ -433,7 +431,7 @@
 
     if (!target)
     {
-        error_stream.PutCString ("error: invalid target\n");
+        diagnostic_manager.PutCString(eDiagnosticSeverityError, "invalid target");
         return false;
     }
 
@@ -467,7 +465,8 @@
 
     if (!DeclMap()->WillParse(exe_ctx, m_materializer_ap.get()))
     {
-        error_stream.PutCString ("error: current process state is unsuitable for expression parsing\n");
+        diagnostic_manager.PutCString(eDiagnosticSeverityError,
+                                      "current process state is unsuitable for expression parsing");
 
         ResetDeclMap(); // We are being careful here in the case of breakpoint conditions.
 
@@ -482,11 +481,12 @@
 
     ClangExpressionParser parser(exe_scope, *this, generate_debug_info);
 
-    unsigned num_errors = parser.Parse (error_stream);
+    unsigned num_errors = parser.Parse(diagnostic_manager);
 
     if (num_errors)
     {
-        error_stream.Printf ("error: %d errors parsing expression\n", num_errors);
+        diagnostic_manager.Printf(eDiagnosticSeverityError, "%u error%s parsing expression", num_errors,
+                                  num_errors == 1 ? "" : "s");
 
         ResetDeclMap(); // We are being careful here in the case of breakpoint conditions.
 
@@ -547,22 +547,20 @@
     {
         const char *error_cstr = jit_error.AsCString();
         if (error_cstr && error_cstr[0])
-            error_stream.Printf ("error: %s\n", error_cstr);
+            diagnostic_manager.PutCString(eDiagnosticSeverityError, error_cstr);
         else
-            error_stream.Printf ("error: expression can't be interpreted or run\n");
+            diagnostic_manager.Printf(eDiagnosticSeverityError, "expression can't be interpreted or run");
         return false;
     }
 }
 
 bool
-ClangUserExpression::AddArguments (ExecutionContext &exe_ctx,
-                                   std::vector<lldb::addr_t> &args,
-                                   lldb::addr_t struct_address,
-                                   Stream &error_stream)
+ClangUserExpression::AddArguments(ExecutionContext &exe_ctx, std::vector<lldb::addr_t> &args,
+                                  lldb::addr_t struct_address, DiagnosticManager &diagnostic_manager)
 {
     lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
-    lldb::addr_t cmd_ptr    = LLDB_INVALID_ADDRESS;
-    
+    lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
+
     if (m_needs_object_ptr)
     {
         lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
@@ -581,7 +579,7 @@
         }
         else
         {
-            error_stream.Printf("Need object pointer but don't know the language\n");
+            diagnostic_manager.PutCString(eDiagnosticSeverityError, "need object pointer but don't know the language");
             return false;
         }
 
@@ -591,7 +589,9 @@
 
         if (!object_ptr_error.Success())
         {
-            error_stream.Printf("warning: couldn't get required object pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
+            diagnostic_manager.Printf(eDiagnosticSeverityWarning,
+                                      "couldn't get required object pointer (substituting NULL): %s",
+                                      object_ptr_error.AsCString());
             object_ptr = 0;
         }
 
@@ -603,7 +603,9 @@
 
             if (!object_ptr_error.Success())
             {
-                error_stream.Printf("warning: couldn't get cmd pointer (substituting NULL): %s\n", object_ptr_error.AsCString());
+                diagnostic_manager.Printf(eDiagnosticSeverityWarning,
+                                          "couldn't get cmd pointer (substituting NULL): %s",
+                                          object_ptr_error.AsCString());
                 cmd_ptr = 0;
             }
         }