Allow expresions to have unique expression prefixes:

expr_options = lldb.SBExpressionOptions()
expr_options.SetPrefix('''
struct Foo {
   int a;
   int b;
   int c;
}
'''
expr_result = frame.EvaluateExpression ("Foo foo = { 1, 2, 3}; foo", expr_options)

This fixed a current issue with ptr_refs, cstr_refs and malloc_info so that they can work. If expressions define their own types and then return expression results that use those types, those types get copied into the target's AST context so they persist and the expression results can be still printed and used in future expressions. Code was added to the expression parser to copy the context in which types are defined if they are used as the expression results. So in the case of types defined by expressions, they get defined in a lldb_expr function and that function and _all_ of its statements get copied. Many types of statements are not supported in this copy (array subscript, lambdas, etc) so this causes expressions to fail as they can't copy the result types. To work around this issue I have added code that allows expressions to specify an expression specific prefix. Then when you evaluate the expression you can pass the "expr_options" and have types that can be correctly copied out into the target. I added this as a way to work around an issue, but I also think it is nice to be allowed to specify an expression prefix that can be reused by many expressions, so this feature is very useful.

<rdar://problem/21130675>

llvm-svn: 238365
diff --git a/lldb/examples/darwin/heap_find/heap.py b/lldb/examples/darwin/heap_find/heap.py
index 2ebff60..f13a3bb 100644
--- a/lldb/examples/darwin/heap_find/heap.py
+++ b/lldb/examples/darwin/heap_find/heap.py
@@ -434,7 +434,7 @@
         result.AppendMessage('error: expression failed "%s" => %s' % (expr, expr_sbvalue.error))
 
 
-def display_match_results (result, options, arg_str_description, expr, print_no_matches = True):
+def display_match_results (result, options, arg_str_description, expr, print_no_matches, expr_prefix = None):
     frame = lldb.debugger.GetSelectedTarget().GetProcess().GetSelectedThread().GetSelectedFrame()
     if not frame:
         result.AppendMessage('error: invalid frame')
@@ -445,6 +445,8 @@
     expr_options.SetTimeoutInMicroSeconds (30*1000*1000) # 30 second timeout
     expr_options.SetTryAllThreads (False)
     expr_options.SetLanguage (lldb.eLanguageTypeObjC_plus_plus)
+    if expr_prefix:
+        expr_options.SetPrefix (expr_prefix)
     expr_sbvalue = frame.EvaluateExpression (expr, expr_options)
     if options.verbose:
         print "expression:"
@@ -621,14 +623,16 @@
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -667,7 +671,7 @@
             user_init_code = user_init_code_format % (options.max_matches, ptr_expr)
             expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
             arg_str_description = 'malloc block containing pointer %s' % ptr_expr
-            display_match_results (result, options, arg_str_description, expr)
+            display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
     else:
         result.AppendMessage('error: no pointer arguments were given')
 
@@ -713,14 +717,16 @@
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -761,7 +767,7 @@
             user_init_code = user_init_code_format % (options.max_matches, cstr)
             expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
             arg_str_description = 'malloc block containing "%s"' % cstr            
-            display_match_results (result, options, arg_str_description, expr)
+            display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
     else:
         result.AppendMessage('error: command takes one or more C string arguments')
 
@@ -798,14 +804,16 @@
     if not frame:
         result.AppendMessage('error: invalid frame')
         return
-    
-    user_init_code_format = '''
+    expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+    
+    user_init_code_format = '''
 typedef struct callback_baton_t {
     range_callback_t callback;
     unsigned num_matches;
@@ -836,7 +844,7 @@
             user_init_code = user_init_code_format % (ptr_expr)
             expr = get_iterate_memory_expr(options, process, user_init_code, 'baton.matches')          
             arg_str_description = 'malloc block that contains %s' % ptr_expr
-            total_matches += display_match_results (result, options, arg_str_description, expr)
+            total_matches += display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
         return total_matches
     else:
         result.AppendMessage('error: command takes one or more pointer expressions')
@@ -1012,14 +1020,17 @@
         # a member named "callback" whose type is "range_callback_t". This
         # will be used by our zone callbacks to call the range callback for
         # each malloc range.
-        user_init_code_format = '''
-#define MAX_MATCHES %u
+        expr_prefix = '''
 struct $malloc_match {
     void *addr;
     uintptr_t size;
     uintptr_t offset;
     uintptr_t type;
 };
+'''        
+
+        user_init_code_format = '''
+#define MAX_MATCHES %u
 typedef int (*compare_callback_t)(const void *a, const void *b);
 typedef struct callback_baton_t {
     range_callback_t callback;
@@ -1106,7 +1117,7 @@
                     user_init_code = user_init_code_format % (options.max_matches, num_objc_classes, isa)
                     expr = get_iterate_memory_expr(options, process, user_init_code, user_return_code)          
                     arg_str_description = 'objective C classes with isa 0x%x' % isa
-                    display_match_results (result, options, arg_str_description, expr)
+                    display_match_results (result, options, arg_str_description, expr, True, expr_prefix)
                 else:
                     result.AppendMessage('error: Can\'t find isa for an ObjC class named "%s"' % (class_name))
             else: