Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // C Includes |
| 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
| 13 | # include <sys/types.h> |
| 14 | #endif |
| 15 | |
| 16 | // C++ Includes |
| 17 | #include <cstdlib> |
| 18 | #include <string> |
| 19 | #include <map> |
| 20 | |
| 21 | #include "lldb/Core/ConstString.h" |
| 22 | #include "lldb/Core/Log.h" |
| 23 | #include "lldb/Core/StreamString.h" |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 24 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 25 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 26 | #include "lldb/Expression/ClangExpressionParser.h" |
| 27 | #include "lldb/Expression/ClangFunction.h" |
| 28 | #include "lldb/Expression/ASTResultSynthesizer.h" |
| 29 | #include "lldb/Expression/ClangUserExpression.h" |
| 30 | #include "lldb/Host/Host.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 31 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 32 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 33 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Target.h" |
| 35 | |
| 36 | using namespace lldb_private; |
| 37 | |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 38 | ClangUserExpression::ClangUserExpression (const char *expr, |
| 39 | const char *expr_prefix) : |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 40 | m_expr_text(expr), |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame^] | 41 | m_expr_prefix(expr_prefix ? expr_prefix : ""), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 42 | m_transformed_text(), |
| 43 | m_jit_addr(LLDB_INVALID_ADDRESS), |
| 44 | m_cplusplus(false), |
| 45 | m_objectivec(false), |
| 46 | m_needs_object_ptr(false) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 47 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 50 | ClangUserExpression::~ClangUserExpression () |
| 51 | { |
| 52 | } |
| 53 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 54 | clang::ASTConsumer * |
| 55 | ClangUserExpression::ASTTransformer (clang::ASTConsumer *passthrough) |
| 56 | { |
| 57 | return new ASTResultSynthesizer(passthrough); |
| 58 | } |
| 59 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 60 | void |
| 61 | ClangUserExpression::ScanContext(ExecutionContext &exe_ctx) |
| 62 | { |
| 63 | if (!exe_ctx.frame) |
| 64 | return; |
| 65 | |
| 66 | VariableList *vars = exe_ctx.frame->GetVariableList(false); |
| 67 | |
| 68 | if (!vars) |
| 69 | return; |
| 70 | |
| 71 | if (vars->FindVariable(ConstString("this")).get()) |
| 72 | m_cplusplus = true; |
| 73 | else if (vars->FindVariable(ConstString("self")).get()) |
| 74 | m_objectivec = true; |
| 75 | } |
| 76 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 77 | // This is a really nasty hack, meant to fix Objective-C expressions of the form |
| 78 | // (int)[myArray count]. Right now, because the type information for count is |
| 79 | // not available, [myArray count] returns id, which can't be directly cast to |
| 80 | // int without causing a clang error. |
| 81 | static void |
| 82 | ApplyObjcCastHack(std::string &expr) |
| 83 | { |
| 84 | #define OBJC_CAST_HACK_FROM "(int)[" |
| 85 | #define OBJC_CAST_HACK_TO "(int)(long long)[" |
| 86 | |
| 87 | size_t from_offset; |
| 88 | |
| 89 | while ((from_offset = expr.find(OBJC_CAST_HACK_FROM)) != expr.npos) |
| 90 | expr.replace(from_offset, sizeof(OBJC_CAST_HACK_FROM) - 1, OBJC_CAST_HACK_TO); |
| 91 | |
| 92 | #undef OBJC_CAST_HACK_TO |
| 93 | #undef OBJC_CAST_HACK_FROM |
| 94 | } |
| 95 | |
Sean Callanan | 3089237 | 2010-10-24 20:45:49 +0000 | [diff] [blame] | 96 | // Another hack, meant to allow use of unichar despite it not being available in |
| 97 | // the type information. Although we could special-case it in type lookup, |
| 98 | // hopefully we'll figure out a way to #include the same environment as is |
| 99 | // present in the original source file rather than try to hack specific type |
| 100 | // definitions in as needed. |
| 101 | static void |
| 102 | ApplyUnicharHack(std::string &expr) |
| 103 | { |
| 104 | #define UNICHAR_HACK_FROM "unichar" |
| 105 | #define UNICHAR_HACK_TO "unsigned short" |
| 106 | |
| 107 | size_t from_offset; |
| 108 | |
| 109 | while ((from_offset = expr.find(UNICHAR_HACK_FROM)) != expr.npos) |
| 110 | expr.replace(from_offset, sizeof(UNICHAR_HACK_FROM) - 1, UNICHAR_HACK_TO); |
| 111 | |
| 112 | #undef UNICHAR_HACK_TO |
| 113 | #undef UNICHAR_HACK_FROM |
| 114 | } |
| 115 | |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 116 | bool |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 117 | ClangUserExpression::Parse (Stream &error_stream, ExecutionContext &exe_ctx) |
| 118 | { |
| 119 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 120 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 121 | ScanContext(exe_ctx); |
| 122 | |
| 123 | StreamString m_transformed_stream; |
| 124 | |
| 125 | //////////////////////////////////// |
| 126 | // Generate the expression |
| 127 | // |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 128 | |
| 129 | ApplyObjcCastHack(m_expr_text); |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 130 | //ApplyUnicharHack(m_expr_text); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 131 | |
| 132 | if (m_cplusplus) |
| 133 | { |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 134 | m_transformed_stream.Printf("%s \n" |
| 135 | "typedef unsigned short unichar; \n" |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 136 | "void \n" |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 137 | "$__lldb_class::%s(void *$__lldb_arg) \n" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 138 | "{ \n" |
| 139 | " %s; \n" |
| 140 | "} \n", |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 141 | m_expr_prefix.c_str(), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 142 | FunctionName(), |
| 143 | m_expr_text.c_str()); |
| 144 | |
| 145 | m_needs_object_ptr = true; |
| 146 | } |
| 147 | else |
| 148 | { |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 149 | m_transformed_stream.Printf("%s \n" |
| 150 | "typedef unsigned short unichar;\n" |
Greg Clayton | f3d0b0c | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 151 | "void \n" |
Sean Callanan | 550f276 | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 152 | "%s(void *$__lldb_arg) \n" |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 153 | "{ \n" |
| 154 | " %s; \n" |
| 155 | "} \n", |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 156 | m_expr_prefix.c_str(), |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 157 | FunctionName(), |
| 158 | m_expr_text.c_str()); |
| 159 | } |
| 160 | |
| 161 | m_transformed_text = m_transformed_stream.GetData(); |
| 162 | |
| 163 | |
| 164 | if (log) |
| 165 | log->Printf("Parsing the following code:\n%s", m_transformed_text.c_str()); |
| 166 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 167 | //////////////////////////////////// |
| 168 | // Set up the target and compiler |
| 169 | // |
| 170 | |
| 171 | Target *target = exe_ctx.target; |
| 172 | |
| 173 | if (!target) |
| 174 | { |
| 175 | error_stream.PutCString ("error: invalid target\n"); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | ConstString target_triple; |
| 180 | |
| 181 | target->GetTargetTriple (target_triple); |
| 182 | |
| 183 | if (!target_triple) |
| 184 | target_triple = Host::GetTargetTriple (); |
| 185 | |
| 186 | if (!target_triple) |
| 187 | { |
| 188 | error_stream.PutCString ("error: invalid target triple\n"); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | ////////////////////////// |
| 193 | // Parse the expression |
| 194 | // |
| 195 | |
| 196 | m_expr_decl_map.reset(new ClangExpressionDeclMap(&exe_ctx)); |
| 197 | |
| 198 | ClangExpressionParser parser(target_triple.GetCString(), *this); |
| 199 | |
| 200 | unsigned num_errors = parser.Parse (error_stream); |
| 201 | |
| 202 | if (num_errors) |
| 203 | { |
| 204 | error_stream.Printf ("error: %d errors parsing expression\n", num_errors); |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | /////////////////////////////////////////////// |
| 209 | // Convert the output of the parser to DWARF |
| 210 | // |
| 211 | |
| 212 | m_dwarf_opcodes.reset(new StreamString); |
| 213 | m_dwarf_opcodes->SetByteOrder (lldb::eByteOrderHost); |
| 214 | m_dwarf_opcodes->GetFlags ().Set (Stream::eBinary); |
| 215 | |
| 216 | m_local_variables.reset(new ClangExpressionVariableStore()); |
| 217 | |
| 218 | Error dwarf_error = parser.MakeDWARF (); |
| 219 | |
| 220 | if (dwarf_error.Success()) |
| 221 | { |
| 222 | if (log) |
| 223 | log->Printf("Code can be interpreted."); |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | ////////////////////////////////// |
| 229 | // JIT the output of the parser |
| 230 | // |
| 231 | |
| 232 | m_dwarf_opcodes.reset(); |
| 233 | |
Sean Callanan | 830a903 | 2010-08-27 23:31:21 +0000 | [diff] [blame] | 234 | lldb::addr_t jit_end; |
| 235 | |
| 236 | Error jit_error = parser.MakeJIT (m_jit_addr, jit_end, exe_ctx); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 237 | |
| 238 | if (jit_error.Success()) |
| 239 | { |
| 240 | if (log) |
| 241 | { |
| 242 | log->Printf("Code can be run in the target."); |
| 243 | |
| 244 | StreamString disassembly_stream; |
| 245 | |
| 246 | Error err = parser.DisassembleFunction(disassembly_stream, exe_ctx); |
| 247 | |
| 248 | if (!err.Success()) |
| 249 | { |
| 250 | log->Printf("Couldn't disassemble function : %s", err.AsCString("unknown error")); |
| 251 | } |
| 252 | else |
| 253 | { |
| 254 | log->Printf("Function disassembly:\n%s", disassembly_stream.GetData()); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | error_stream.Printf ("error: expression can't be interpreted or run\n", num_errors); |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | bool |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 268 | ClangUserExpression::PrepareToExecuteJITExpression (Stream &error_stream, |
Sean Callanan | ab06af9 | 2010-10-19 23:57:21 +0000 | [diff] [blame] | 269 | ExecutionContext &exe_ctx, |
| 270 | lldb::addr_t &struct_address, |
| 271 | lldb::addr_t &object_ptr) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 272 | { |
| 273 | Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS); |
| 274 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 275 | if (m_jit_addr != LLDB_INVALID_ADDRESS) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 276 | { |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 277 | |
| 278 | Error materialize_error; |
| 279 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 280 | |
| 281 | if (m_needs_object_ptr && !(m_expr_decl_map->GetObjectPointer(object_ptr, &exe_ctx, materialize_error))) |
| 282 | { |
| 283 | error_stream.Printf("Couldn't get required object pointer: %s\n", materialize_error.AsCString()); |
| 284 | return false; |
| 285 | } |
| 286 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 287 | if (!m_expr_decl_map->Materialize(&exe_ctx, struct_address, materialize_error)) |
| 288 | { |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 289 | error_stream.Printf("Couldn't materialize struct: %s\n", materialize_error.AsCString()); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | |
| 293 | if (log) |
| 294 | { |
| 295 | log->Printf("Function address : 0x%llx", (uint64_t)m_jit_addr); |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 296 | |
| 297 | if (m_needs_object_ptr) |
| 298 | log->Printf("Object pointer : 0x%llx", (uint64_t)object_ptr); |
| 299 | |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 300 | log->Printf("Structure address : 0x%llx", (uint64_t)struct_address); |
| 301 | |
| 302 | StreamString args; |
| 303 | |
| 304 | Error dump_error; |
| 305 | |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 306 | if (struct_address) |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 307 | { |
Sean Callanan | e8a59a8 | 2010-09-13 21:34:21 +0000 | [diff] [blame] | 308 | if (!m_expr_decl_map->DumpMaterializedStruct(&exe_ctx, args, dump_error)) |
| 309 | { |
| 310 | log->Printf("Couldn't extract variable values : %s", dump_error.AsCString("unknown error")); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | log->Printf("Structure contents:\n%s", args.GetData()); |
| 315 | } |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 318 | } |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | ThreadPlan * |
| 323 | ClangUserExpression::GetThreadPlanToExecuteJITExpression (Stream &error_stream, |
| 324 | ExecutionContext &exe_ctx) |
| 325 | { |
| 326 | lldb::addr_t struct_address; |
| 327 | |
| 328 | lldb::addr_t object_ptr = NULL; |
| 329 | |
| 330 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
| 331 | |
| 332 | return ClangFunction::GetThreadPlanToCallFunction (exe_ctx, |
| 333 | m_jit_addr, |
| 334 | struct_address, |
| 335 | error_stream, |
| 336 | true, |
| 337 | true, |
| 338 | (m_needs_object_ptr ? &object_ptr : NULL)); |
| 339 | } |
| 340 | |
| 341 | bool |
| 342 | ClangUserExpression::FinalizeJITExecution (Stream &error_stream, |
| 343 | ExecutionContext &exe_ctx, |
| 344 | ClangExpressionVariable *&result) |
| 345 | { |
| 346 | Error expr_error; |
| 347 | |
| 348 | if (!m_expr_decl_map->Dematerialize(&exe_ctx, result, expr_error)) |
| 349 | { |
| 350 | error_stream.Printf ("Couldn't dematerialize struct : %s\n", expr_error.AsCString("unknown error")); |
| 351 | return false; |
| 352 | } |
| 353 | return true; |
| 354 | } |
| 355 | |
| 356 | bool |
| 357 | ClangUserExpression::Execute (Stream &error_stream, |
| 358 | ExecutionContext &exe_ctx, |
| 359 | ClangExpressionVariable *&result) |
| 360 | { |
| 361 | if (m_dwarf_opcodes.get()) |
| 362 | { |
| 363 | // TODO execute the JITted opcodes |
| 364 | |
| 365 | error_stream.Printf("We don't currently support executing DWARF expressions"); |
| 366 | |
| 367 | return false; |
| 368 | } |
| 369 | else if (m_jit_addr != LLDB_INVALID_ADDRESS) |
| 370 | { |
| 371 | lldb::addr_t struct_address; |
| 372 | |
| 373 | lldb::addr_t object_ptr = NULL; |
| 374 | |
| 375 | PrepareToExecuteJITExpression (error_stream, exe_ctx, struct_address, object_ptr); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 376 | |
| 377 | ClangFunction::ExecutionResults execution_result = |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 378 | ClangFunction::ExecuteFunction (exe_ctx, |
| 379 | m_jit_addr, |
| 380 | struct_address, |
| 381 | true, |
| 382 | true, |
Sean Callanan | 0027cec | 2010-10-07 18:17:31 +0000 | [diff] [blame] | 383 | 10000000, |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 384 | error_stream, |
| 385 | (m_needs_object_ptr ? &object_ptr : NULL)); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 386 | |
| 387 | if (execution_result != ClangFunction::eExecutionCompleted) |
| 388 | { |
| 389 | const char *result_name; |
| 390 | |
| 391 | switch (execution_result) |
| 392 | { |
| 393 | case ClangFunction::eExecutionCompleted: |
| 394 | result_name = "eExecutionCompleted"; |
| 395 | break; |
| 396 | case ClangFunction::eExecutionDiscarded: |
| 397 | result_name = "eExecutionDiscarded"; |
| 398 | break; |
| 399 | case ClangFunction::eExecutionInterrupted: |
| 400 | result_name = "eExecutionInterrupted"; |
| 401 | break; |
| 402 | case ClangFunction::eExecutionSetupError: |
| 403 | result_name = "eExecutionSetupError"; |
| 404 | break; |
| 405 | case ClangFunction::eExecutionTimedOut: |
| 406 | result_name = "eExecutionTimedOut"; |
| 407 | break; |
| 408 | } |
| 409 | |
| 410 | error_stream.Printf ("Couldn't execute function; result was %s\n", result_name); |
| 411 | return false; |
| 412 | } |
| 413 | |
Jim Ingham | d168690 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 414 | return FinalizeJITExecution (error_stream, exe_ctx, result); |
Sean Callanan | 65dafa8 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 415 | } |
| 416 | else |
| 417 | { |
| 418 | error_stream.Printf("Expression can't be run; neither DWARF nor a JIT compiled function are present"); |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | StreamString & |
| 424 | ClangUserExpression::DwarfOpcodeStream () |
| 425 | { |
| 426 | if (!m_dwarf_opcodes.get()) |
| 427 | m_dwarf_opcodes.reset(new StreamString()); |
| 428 | |
| 429 | return *m_dwarf_opcodes.get(); |
| 430 | } |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 431 | |
| 432 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 433 | lldb::ValueObjectSP |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 434 | ClangUserExpression::Evaluate (ExecutionContext &exe_ctx, |
| 435 | const char *expr_cstr, |
| 436 | const char *expr_prefix) |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 437 | { |
| 438 | Error error; |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 439 | lldb::ValueObjectSP result_valobj_sp; |
Sean Callanan | 77e9394 | 2010-10-29 00:29:03 +0000 | [diff] [blame] | 440 | ClangUserExpression user_expression (expr_cstr, expr_prefix); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 441 | |
| 442 | StreamString error_stream; |
| 443 | |
| 444 | if (!user_expression.Parse (error_stream, exe_ctx)) |
| 445 | { |
| 446 | if (error_stream.GetString().empty()) |
| 447 | error.SetErrorString ("expression failed to parse, unknown error"); |
| 448 | else |
| 449 | error.SetErrorString (error_stream.GetString().c_str()); |
| 450 | } |
| 451 | else |
| 452 | { |
| 453 | ClangExpressionVariable *expr_result = NULL; |
| 454 | |
| 455 | error_stream.GetString().clear(); |
| 456 | |
| 457 | if (!user_expression.Execute (error_stream, exe_ctx, expr_result)) |
| 458 | { |
| 459 | if (error_stream.GetString().empty()) |
| 460 | error.SetErrorString ("expression failed to execute, unknown error"); |
| 461 | else |
| 462 | error.SetErrorString (error_stream.GetString().c_str()); |
| 463 | } |
| 464 | else |
| 465 | { |
| 466 | // TODO: seems weird to get a pointer to a result object back from |
| 467 | // a function. Do we own it? Feels like we do, but from looking at the |
| 468 | // code we don't. Might be best to make this a reference and state |
| 469 | // explicitly that we don't own it when we get a reference back from |
| 470 | // the execute? |
| 471 | if (expr_result) |
| 472 | { |
| 473 | result_valobj_sp = expr_result->GetExpressionResult (&exe_ctx); |
| 474 | } |
| 475 | else |
| 476 | { |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 477 | error.SetErrorString ("Expression did not return a result"); |
Greg Clayton | 377e0b4 | 2010-10-05 00:31:29 +0000 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | } |
Sean Callanan | 44820ec | 2010-10-19 20:15:00 +0000 | [diff] [blame] | 481 | |
Greg Clayton | d171972 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 482 | if (result_valobj_sp.get() == NULL) |
| 483 | result_valobj_sp.reset (new ValueObjectConstResult (error)); |
| 484 | |
| 485 | return result_valobj_sp; |
Johnny Chen | b4c0f02 | 2010-10-29 20:19:44 +0000 | [diff] [blame^] | 486 | } |