Raphael Isemann | 8081428 | 2020-01-24 08:23:27 +0100 | [diff] [blame] | 1 | //===-- ClangUserExpression.cpp -------------------------------------------===// |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Haibo Huang | c6551bf | 2019-08-07 06:15:01 +0000 | [diff] [blame] | 9 | #include "lldb/Host/Config.h" |
| 10 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #if HAVE_SYS_TYPES_H |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include <sys/types.h> |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 16 | #include <cstdlib> |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 17 | #include <map> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 18 | #include <string> |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 19 | |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 20 | #include "ClangUserExpression.h" |
| 21 | |
| 22 | #include "ASTResultSynthesizer.h" |
Alex Langford | 8be3021 | 2020-01-29 11:59:28 -0800 | [diff] [blame^] | 23 | #include "ClangASTMetadata.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | #include "ClangDiagnostic.h" |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 25 | #include "ClangExpressionDeclMap.h" |
| 26 | #include "ClangExpressionParser.h" |
| 27 | #include "ClangModulesDeclVendor.h" |
| 28 | #include "ClangPersistentVariables.h" |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 29 | #include "CppModuleConfiguration.h" |
Sean Callanan | 4dbb271 | 2015-09-25 20:35:58 +0000 | [diff] [blame] | 30 | |
Alex Langford | 8be3021 | 2020-01-29 11:59:28 -0800 | [diff] [blame^] | 31 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
Sean Callanan | bda7ef8 | 2016-06-22 17:32:17 +0000 | [diff] [blame] | 32 | #include "lldb/Core/Debugger.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 33 | #include "lldb/Core/Module.h" |
Greg Clayton | c4e411f | 2011-01-18 19:36:39 +0000 | [diff] [blame] | 34 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | b71f384 | 2010-10-05 03:13:51 +0000 | [diff] [blame] | 35 | #include "lldb/Core/ValueObjectConstResult.h" |
Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 36 | #include "lldb/Expression/ExpressionSourceCode.h" |
Sean Callanan | 14b1bae | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 37 | #include "lldb/Expression/IRExecutionUnit.h" |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 38 | #include "lldb/Expression/IRInterpreter.h" |
Sean Callanan | 96d2730 | 2013-04-11 00:09:05 +0000 | [diff] [blame] | 39 | #include "lldb/Expression/Materializer.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 40 | #include "lldb/Host/HostInfo.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 41 | #include "lldb/Symbol/Block.h" |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 42 | #include "lldb/Symbol/CompileUnit.h" |
Jim Ingham | 5fdeed4 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 43 | #include "lldb/Symbol/Function.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 44 | #include "lldb/Symbol/ObjectFile.h" |
Adrian Prantl | 0e45e60 | 2019-11-14 08:57:32 -0800 | [diff] [blame] | 45 | #include "lldb/Symbol/SymbolFile.h" |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 46 | #include "lldb/Symbol/SymbolVendor.h" |
Jim Ingham | 5fdeed4 | 2012-10-30 23:35:54 +0000 | [diff] [blame] | 47 | #include "lldb/Symbol/Type.h" |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 48 | #include "lldb/Symbol/VariableList.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 49 | #include "lldb/Target/ExecutionContext.h" |
Greg Clayton | 8f343b0 | 2010-11-04 01:54:29 +0000 | [diff] [blame] | 50 | #include "lldb/Target/Process.h" |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 51 | #include "lldb/Target/StackFrame.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 52 | #include "lldb/Target/Target.h" |
Jim Ingham | f48169b | 2010-11-30 02:22:11 +0000 | [diff] [blame] | 53 | #include "lldb/Target/ThreadPlan.h" |
| 54 | #include "lldb/Target/ThreadPlanCallUserExpression.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 55 | #include "lldb/Utility/ConstString.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 56 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 57 | #include "lldb/Utility/StreamString.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 58 | |
Sean Callanan | 72e4940 | 2011-08-05 23:43:37 +0000 | [diff] [blame] | 59 | #include "clang/AST/DeclCXX.h" |
| 60 | #include "clang/AST/DeclObjC.h" |
| 61 | |
Tatyana Krasnukha | edebf12 | 2019-04-26 16:41:04 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/ScopeExit.h" |
| 63 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 64 | using namespace lldb_private; |
| 65 | |
Raphael Isemann | 52f3a2f | 2019-11-12 10:04:32 +0100 | [diff] [blame] | 66 | char ClangUserExpression::ID; |
| 67 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | ClangUserExpression::ClangUserExpression( |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 69 | ExecutionContextScope &exe_scope, llvm::StringRef expr, |
| 70 | llvm::StringRef prefix, lldb::LanguageType language, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 71 | ResultType desired_type, const EvaluateExpressionOptions &options, |
| 72 | ValueObject *ctx_obj) |
Zachary Turner | c5d7df9 | 2016-11-08 04:52:16 +0000 | [diff] [blame] | 73 | : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type, |
Raphael Isemann | 52f3a2f | 2019-11-12 10:04:32 +0100 | [diff] [blame] | 74 | options), |
Jonas Devlieghere | 70355ac | 2019-02-12 03:47:39 +0000 | [diff] [blame] | 75 | m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() == |
| 76 | eExecutionPolicyTopLevel), |
| 77 | m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | switch (m_language) { |
| 79 | case lldb::eLanguageTypeC_plus_plus: |
| 80 | m_allow_cxx = true; |
| 81 | break; |
| 82 | case lldb::eLanguageTypeObjC: |
| 83 | m_allow_objc = true; |
| 84 | break; |
| 85 | case lldb::eLanguageTypeObjC_plus_plus: |
| 86 | default: |
| 87 | m_allow_cxx = true; |
| 88 | m_allow_objc = true; |
| 89 | break; |
| 90 | } |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | ClangUserExpression::~ClangUserExpression() {} |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 94 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 95 | void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 7038508 | 2012-12-01 00:08:33 +0000 | [diff] [blame] | 97 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 98 | LLDB_LOGF(log, "ClangUserExpression::ScanContext()"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | |
| 100 | m_target = exe_ctx.GetTargetPtr(); |
| 101 | |
| 102 | if (!(m_allow_cxx || m_allow_objc)) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 103 | LLDB_LOGF(log, " [CUE::SC] Settings inhibit C++ and Objective-C"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | return; |
| 105 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | StackFrame *frame = exe_ctx.GetFramePtr(); |
Konrad Kleine | 248a130 | 2019-05-23 11:14:47 +0000 | [diff] [blame] | 108 | if (frame == nullptr) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 109 | LLDB_LOGF(log, " [CUE::SC] Null stack frame"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 112 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 113 | SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction | |
| 114 | lldb::eSymbolContextBlock); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | if (!sym_ctx.function) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 117 | LLDB_LOGF(log, " [CUE::SC] Null function"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | return; |
| 119 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | // Find the block that defines the function represented by "sym_ctx" |
| 122 | Block *function_block = sym_ctx.GetFunctionBlock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | if (!function_block) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 125 | LLDB_LOGF(log, " [CUE::SC] Null function block"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | CompilerDeclContext decl_context = function_block->GetDeclContext(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | if (!decl_context) { |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 132 | LLDB_LOGF(log, " [CUE::SC] Null decl context"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | return; |
| 134 | } |
Greg Clayton | 685c88c | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 135 | |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 136 | if (m_ctx_obj) { |
| 137 | switch (m_ctx_obj->GetObjectRuntimeLanguage()) { |
| 138 | case lldb::eLanguageTypeC: |
| 139 | case lldb::eLanguageTypeC89: |
| 140 | case lldb::eLanguageTypeC99: |
| 141 | case lldb::eLanguageTypeC11: |
| 142 | case lldb::eLanguageTypeC_plus_plus: |
| 143 | case lldb::eLanguageTypeC_plus_plus_03: |
| 144 | case lldb::eLanguageTypeC_plus_plus_11: |
| 145 | case lldb::eLanguageTypeC_plus_plus_14: |
| 146 | m_in_cplusplus_method = true; |
| 147 | break; |
| 148 | case lldb::eLanguageTypeObjC: |
| 149 | case lldb::eLanguageTypeObjC_plus_plus: |
| 150 | m_in_objectivec_method = true; |
| 151 | break; |
| 152 | default: |
| 153 | break; |
| 154 | } |
| 155 | m_needs_object_ptr = true; |
| 156 | } else if (clang::CXXMethodDecl *method_decl = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 157 | TypeSystemClang::DeclContextGetAsCXXMethodDecl(decl_context)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | if (m_allow_cxx && method_decl->isInstance()) { |
| 159 | if (m_enforce_valid_object) { |
| 160 | lldb::VariableListSP variable_list_sp( |
| 161 | function_block->GetBlockVariableList(true)); |
Greg Clayton | 685c88c | 2012-07-14 00:53:55 +0000 | [diff] [blame] | 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | const char *thisErrorString = "Stopped in a C++ method, but 'this' " |
| 164 | "isn't available; pretending we are in a " |
| 165 | "generic context"; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | if (!variable_list_sp) { |
| 168 | err.SetErrorString(thisErrorString); |
| 169 | return; |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 170 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | |
| 172 | lldb::VariableSP this_var_sp( |
| 173 | variable_list_sp->FindVariable(ConstString("this"))); |
| 174 | |
| 175 | if (!this_var_sp || !this_var_sp->IsInScope(frame) || |
| 176 | !this_var_sp->LocationIsValidForFrame(frame)) { |
| 177 | err.SetErrorString(thisErrorString); |
| 178 | return; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | m_in_cplusplus_method = true; |
| 183 | m_needs_object_ptr = true; |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 184 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | } else if (clang::ObjCMethodDecl *method_decl = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 186 | TypeSystemClang::DeclContextGetAsObjCMethodDecl( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | decl_context)) { |
| 188 | if (m_allow_objc) { |
| 189 | if (m_enforce_valid_object) { |
| 190 | lldb::VariableListSP variable_list_sp( |
| 191 | function_block->GetBlockVariableList(true)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | const char *selfErrorString = "Stopped in an Objective-C method, but " |
| 194 | "'self' isn't available; pretending we " |
| 195 | "are in a generic context"; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | if (!variable_list_sp) { |
| 198 | err.SetErrorString(selfErrorString); |
| 199 | return; |
| 200 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | lldb::VariableSP self_variable_sp = |
| 203 | variable_list_sp->FindVariable(ConstString("self")); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || |
| 206 | !self_variable_sp->LocationIsValidForFrame(frame)) { |
| 207 | err.SetErrorString(selfErrorString); |
| 208 | return; |
| 209 | } |
| 210 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | m_in_objectivec_method = true; |
| 213 | m_needs_object_ptr = true; |
| 214 | |
| 215 | if (!method_decl->isInstanceMethod()) |
| 216 | m_in_static_method = true; |
| 217 | } |
| 218 | } else if (clang::FunctionDecl *function_decl = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 219 | TypeSystemClang::DeclContextGetAsFunctionDecl(decl_context)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | // We might also have a function that said in the debug information that it |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 221 | // captured an object pointer. The best way to deal with getting to the |
| 222 | // ivars at present is by pretending that this is a method of a class in |
| 223 | // whatever runtime the debug info says the object pointer belongs to. Do |
| 224 | // that here. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | |
| 226 | ClangASTMetadata *metadata = |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 227 | TypeSystemClang::DeclContextGetMetaData(decl_context, function_decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | if (metadata && metadata->HasObjectPtr()) { |
| 229 | lldb::LanguageType language = metadata->GetObjectPtrLanguage(); |
| 230 | if (language == lldb::eLanguageTypeC_plus_plus) { |
| 231 | if (m_enforce_valid_object) { |
| 232 | lldb::VariableListSP variable_list_sp( |
| 233 | function_block->GetBlockVariableList(true)); |
| 234 | |
| 235 | const char *thisErrorString = "Stopped in a context claiming to " |
| 236 | "capture a C++ object pointer, but " |
| 237 | "'this' isn't available; pretending we " |
| 238 | "are in a generic context"; |
| 239 | |
| 240 | if (!variable_list_sp) { |
| 241 | err.SetErrorString(thisErrorString); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | lldb::VariableSP this_var_sp( |
| 246 | variable_list_sp->FindVariable(ConstString("this"))); |
| 247 | |
| 248 | if (!this_var_sp || !this_var_sp->IsInScope(frame) || |
| 249 | !this_var_sp->LocationIsValidForFrame(frame)) { |
| 250 | err.SetErrorString(thisErrorString); |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | m_in_cplusplus_method = true; |
| 256 | m_needs_object_ptr = true; |
| 257 | } else if (language == lldb::eLanguageTypeObjC) { |
| 258 | if (m_enforce_valid_object) { |
| 259 | lldb::VariableListSP variable_list_sp( |
| 260 | function_block->GetBlockVariableList(true)); |
| 261 | |
| 262 | const char *selfErrorString = |
| 263 | "Stopped in a context claiming to capture an Objective-C object " |
| 264 | "pointer, but 'self' isn't available; pretending we are in a " |
| 265 | "generic context"; |
| 266 | |
| 267 | if (!variable_list_sp) { |
| 268 | err.SetErrorString(selfErrorString); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | lldb::VariableSP self_variable_sp = |
| 273 | variable_list_sp->FindVariable(ConstString("self")); |
| 274 | |
| 275 | if (!self_variable_sp || !self_variable_sp->IsInScope(frame) || |
| 276 | !self_variable_sp->LocationIsValidForFrame(frame)) { |
| 277 | err.SetErrorString(selfErrorString); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | Type *self_type = self_variable_sp->GetType(); |
| 282 | |
| 283 | if (!self_type) { |
| 284 | err.SetErrorString(selfErrorString); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | CompilerType self_clang_type = self_type->GetForwardCompilerType(); |
| 289 | |
| 290 | if (!self_clang_type) { |
| 291 | err.SetErrorString(selfErrorString); |
| 292 | return; |
| 293 | } |
| 294 | |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 295 | if (TypeSystemClang::IsObjCClassType(self_clang_type)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | return; |
Raphael Isemann | 6e3b0cc | 2020-01-23 10:04:13 +0100 | [diff] [blame] | 297 | } else if (TypeSystemClang::IsObjCObjectPointerType( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 298 | self_clang_type)) { |
Dawn Perchik | 53f34c8 | 2015-07-01 00:54:02 +0000 | [diff] [blame] | 299 | m_in_objectivec_method = true; |
Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 300 | m_needs_object_ptr = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 301 | } else { |
| 302 | err.SetErrorString(selfErrorString); |
| 303 | return; |
| 304 | } |
| 305 | } else { |
| 306 | m_in_objectivec_method = true; |
| 307 | m_needs_object_ptr = true; |
Sean Callanan | 9bc8384 | 2011-09-26 18:45:31 +0000 | [diff] [blame] | 308 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 309 | } |
Sean Callanan | 3670ba5 | 2010-12-01 21:35:54 +0000 | [diff] [blame] | 310 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | } |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 314 | // This is a really nasty hack, meant to fix Objective-C expressions of the |
| 315 | // form (int)[myArray count]. Right now, because the type information for |
| 316 | // count is not available, [myArray count] returns id, which can't be directly |
| 317 | // cast to int without causing a clang error. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | static void ApplyObjcCastHack(std::string &expr) { |
Raphael Isemann | 3ee98a1 | 2019-09-17 07:58:01 +0000 | [diff] [blame] | 319 | const std::string from = "(int)["; |
| 320 | const std::string to = "(int)(long long)["; |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 321 | |
Raphael Isemann | 3ee98a1 | 2019-09-17 07:58:01 +0000 | [diff] [blame] | 322 | size_t offset; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 323 | |
Raphael Isemann | 3ee98a1 | 2019-09-17 07:58:01 +0000 | [diff] [blame] | 324 | while ((offset = expr.find(from)) != expr.npos) |
| 325 | expr.replace(offset, from.size(), to); |
Sean Callanan | cf5498f | 2010-10-22 23:25:16 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 328 | bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager, |
| 329 | ExecutionContext &exe_ctx) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 330 | if (Target *target = exe_ctx.GetTargetPtr()) { |
| 331 | if (PersistentExpressionState *persistent_state = |
| 332 | target->GetPersistentExpressionStateForLanguage( |
| 333 | lldb::eLanguageTypeC)) { |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 334 | m_clang_state = llvm::cast<ClangPersistentVariables>(persistent_state); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | m_result_delegate.RegisterPersistentState(persistent_state); |
| 336 | } else { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 337 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 338 | eDiagnosticSeverityError, |
| 339 | "couldn't start parsing (no persistent data)"); |
| 340 | return false; |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 341 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | } else { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 343 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 344 | "error: couldn't start parsing (no target)"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 345 | return false; |
| 346 | } |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 347 | return true; |
| 348 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 350 | static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | if (ClangModulesDeclVendor *decl_vendor = |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 352 | target->GetClangModulesDeclVendor()) { |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 353 | auto *persistent_state = llvm::cast<ClangPersistentVariables>( |
| 354 | target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC)); |
| 355 | if (!persistent_state) |
| 356 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 358 | persistent_state->GetHandLoadedClangModules(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 359 | ClangModulesDeclVendor::ModuleVector modules_for_macros; |
| 360 | |
| 361 | for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) { |
| 362 | modules_for_macros.push_back(module); |
| 363 | } |
| 364 | |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 365 | if (target->GetEnableAutoImportClangModules()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | if (StackFrame *frame = exe_ctx.GetFramePtr()) { |
| 367 | if (Block *block = frame->GetFrameBlock()) { |
| 368 | SymbolContext sc; |
| 369 | |
| 370 | block->CalculateSymbolContext(&sc); |
| 371 | |
| 372 | if (sc.comp_unit) { |
| 373 | StreamString error_stream; |
| 374 | |
| 375 | decl_vendor->AddModulesForCompileUnit( |
| 376 | *sc.comp_unit, modules_for_macros, error_stream); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 382 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 383 | |
Raphael Isemann | 5eb87cf | 2019-08-05 20:31:47 +0000 | [diff] [blame] | 384 | void ClangUserExpression::UpdateLanguageForExpr() { |
| 385 | m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown; |
| 386 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) |
| 387 | return; |
| 388 | if (m_in_cplusplus_method) |
| 389 | m_expr_lang = lldb::eLanguageTypeC_plus_plus; |
| 390 | else if (m_in_objectivec_method) |
| 391 | m_expr_lang = lldb::eLanguageTypeObjC; |
| 392 | else |
| 393 | m_expr_lang = lldb::eLanguageTypeC; |
| 394 | } |
| 395 | |
| 396 | void ClangUserExpression::CreateSourceCode( |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 397 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
Raphael Isemann | 71569d0 | 2019-05-02 10:12:56 +0000 | [diff] [blame] | 398 | std::vector<std::string> modules_to_import, bool for_completion) { |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 399 | |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 400 | m_filename = m_clang_state->GetNextExprFileName(); |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 401 | std::string prefix = m_expr_prefix; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | |
| 403 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
| 404 | m_transformed_text = m_expr_text; |
| 405 | } else { |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 406 | m_source_code.reset(ClangExpressionSourceCode::CreateWrapped( |
| 407 | m_filename, prefix.c_str(), m_expr_text.c_str())); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 408 | |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 409 | if (!m_source_code->GetText(m_transformed_text, m_expr_lang, |
| 410 | m_in_static_method, exe_ctx, !m_ctx_obj, |
| 411 | for_completion, modules_to_import)) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 412 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 413 | "couldn't construct expression body"); |
Raphael Isemann | dababf7 | 2018-09-27 10:12:54 +0000 | [diff] [blame] | 414 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | } |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 416 | |
| 417 | // Find and store the start position of the original code inside the |
| 418 | // transformed code. We need this later for the code completion. |
| 419 | std::size_t original_start; |
| 420 | std::size_t original_end; |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 421 | bool found_bounds = m_source_code->GetOriginalBodyBounds( |
Raphael Isemann | dababf7 | 2018-09-27 10:12:54 +0000 | [diff] [blame] | 422 | m_transformed_text, m_expr_lang, original_start, original_end); |
Raphael Isemann | d59c8d3 | 2019-04-24 12:55:00 +0000 | [diff] [blame] | 423 | if (found_bounds) |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 424 | m_user_expression_start_pos = original_start; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 425 | } |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 428 | static bool SupportsCxxModuleImport(lldb::LanguageType language) { |
| 429 | switch (language) { |
| 430 | case lldb::eLanguageTypeC_plus_plus: |
| 431 | case lldb::eLanguageTypeC_plus_plus_03: |
| 432 | case lldb::eLanguageTypeC_plus_plus_11: |
| 433 | case lldb::eLanguageTypeC_plus_plus_14: |
| 434 | case lldb::eLanguageTypeObjC_plus_plus: |
| 435 | return true; |
| 436 | default: |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 441 | /// Utility method that puts a message into the expression log and |
| 442 | /// returns an invalid module configuration. |
| 443 | static CppModuleConfiguration LogConfigError(const std::string &msg) { |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 444 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 445 | LLDB_LOG(log, "[C++ module config] {0}", msg); |
| 446 | return CppModuleConfiguration(); |
| 447 | } |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 448 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 449 | CppModuleConfiguration GetModuleConfig(lldb::LanguageType language, |
| 450 | ExecutionContext &exe_ctx) { |
Raphael Isemann | ccd54a1 | 2019-10-10 13:43:37 +0000 | [diff] [blame] | 451 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 452 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 453 | // Don't do anything if this is not a C++ module configuration. |
| 454 | if (!SupportsCxxModuleImport(language)) |
| 455 | return LogConfigError("Language doesn't support C++ modules"); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 456 | |
| 457 | Target *target = exe_ctx.GetTargetPtr(); |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 458 | if (!target) |
| 459 | return LogConfigError("No target"); |
| 460 | |
| 461 | if (!target->GetEnableImportStdModule()) |
| 462 | return LogConfigError("Importing std module not enabled in settings"); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 463 | |
| 464 | StackFrame *frame = exe_ctx.GetFramePtr(); |
| 465 | if (!frame) |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 466 | return LogConfigError("No frame"); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 467 | |
| 468 | Block *block = frame->GetFrameBlock(); |
| 469 | if (!block) |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 470 | return LogConfigError("No block"); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 471 | |
| 472 | SymbolContext sc; |
| 473 | block->CalculateSymbolContext(&sc); |
| 474 | if (!sc.comp_unit) |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 475 | return LogConfigError("Couldn't calculate symbol context"); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 476 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 477 | // Build a list of files we need to analyze to build the configuration. |
| 478 | FileSpecList files; |
| 479 | for (const FileSpec &f : sc.comp_unit->GetSupportFiles()) |
| 480 | files.AppendIfUnique(f); |
| 481 | // We also need to look at external modules in the case of -gmodules as they |
| 482 | // contain the support files for libc++ and the C library. |
Adrian Prantl | 0e45e60 | 2019-11-14 08:57:32 -0800 | [diff] [blame] | 483 | llvm::DenseSet<SymbolFile *> visited_symbol_files; |
| 484 | sc.comp_unit->ForEachExternalModule( |
| 485 | visited_symbol_files, [&files](Module &module) { |
| 486 | for (std::size_t i = 0; i < module.GetNumCompileUnits(); ++i) { |
| 487 | const FileSpecList &support_files = |
| 488 | module.GetCompileUnitAtIndex(i)->GetSupportFiles(); |
| 489 | for (const FileSpec &f : support_files) { |
| 490 | files.AppendIfUnique(f); |
| 491 | } |
| 492 | } |
| 493 | return false; |
| 494 | }); |
Raphael Isemann | ccd54a1 | 2019-10-10 13:43:37 +0000 | [diff] [blame] | 495 | |
| 496 | LLDB_LOG(log, "[C++ module config] Found {0} support files to analyze", |
| 497 | files.GetSize()); |
| 498 | if (log && log->GetVerbose()) { |
| 499 | for (const FileSpec &f : files) |
| 500 | LLDB_LOGV(log, "[C++ module config] Analyzing support file: {0}", |
| 501 | f.GetPath()); |
| 502 | } |
| 503 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 504 | // Try to create a configuration from the files. If there is no valid |
| 505 | // configuration possible with the files, this just returns an invalid |
| 506 | // configuration. |
| 507 | return CppModuleConfiguration(files); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 510 | bool ClangUserExpression::PrepareForParsing( |
Raphael Isemann | 71569d0 | 2019-05-02 10:12:56 +0000 | [diff] [blame] | 511 | DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx, |
| 512 | bool for_completion) { |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 513 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 514 | |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 515 | InstallContext(exe_ctx); |
| 516 | |
| 517 | if (!SetupPersistentState(diagnostic_manager, exe_ctx)) |
| 518 | return false; |
| 519 | |
| 520 | Status err; |
| 521 | ScanContext(exe_ctx, err); |
| 522 | |
| 523 | if (!err.Success()) { |
| 524 | diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString()); |
| 525 | } |
| 526 | |
| 527 | //////////////////////////////////// |
| 528 | // Generate the expression |
| 529 | // |
| 530 | |
| 531 | ApplyObjcCastHack(m_expr_text); |
| 532 | |
| 533 | SetupDeclVendor(exe_ctx, m_target); |
Raphael Isemann | dababf7 | 2018-09-27 10:12:54 +0000 | [diff] [blame] | 534 | |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 535 | CppModuleConfiguration module_config = GetModuleConfig(m_language, exe_ctx); |
| 536 | llvm::ArrayRef<std::string> imported_modules = |
| 537 | module_config.GetImportedModules(); |
| 538 | m_imported_cpp_modules = !imported_modules.empty(); |
| 539 | m_include_directories = module_config.GetIncludeDirs(); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 540 | |
| 541 | LLDB_LOG(log, "List of imported modules in expression: {0}", |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 542 | llvm::make_range(imported_modules.begin(), imported_modules.end())); |
| 543 | LLDB_LOG(log, "List of include directories gathered for modules: {0}", |
| 544 | llvm::make_range(m_include_directories.begin(), |
| 545 | m_include_directories.end())); |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 546 | |
Raphael Isemann | 5eb87cf | 2019-08-05 20:31:47 +0000 | [diff] [blame] | 547 | UpdateLanguageForExpr(); |
Raphael Isemann | 9379d19 | 2019-09-24 10:08:18 +0000 | [diff] [blame] | 548 | CreateSourceCode(diagnostic_manager, exe_ctx, imported_modules, |
| 549 | for_completion); |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 550 | return true; |
| 551 | } |
| 552 | |
| 553 | bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager, |
| 554 | ExecutionContext &exe_ctx, |
| 555 | lldb_private::ExecutionPolicy execution_policy, |
| 556 | bool keep_result_in_memory, |
| 557 | bool generate_debug_info) { |
| 558 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 559 | |
Raphael Isemann | 71569d0 | 2019-05-02 10:12:56 +0000 | [diff] [blame] | 560 | if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false)) |
Raphael Isemann | ba800e7d | 2018-07-10 22:12:39 +0000 | [diff] [blame] | 561 | return false; |
| 562 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 563 | LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | |
| 565 | //////////////////////////////////// |
| 566 | // Set up the target and compiler |
| 567 | // |
| 568 | |
| 569 | Target *target = exe_ctx.GetTargetPtr(); |
| 570 | |
| 571 | if (!target) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 572 | diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | return false; |
| 574 | } |
| 575 | |
| 576 | ////////////////////////// |
| 577 | // Parse the expression |
| 578 | // |
| 579 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 580 | m_materializer_up.reset(new Materializer()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 581 | |
| 582 | ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory); |
| 583 | |
Tatyana Krasnukha | edebf12 | 2019-04-26 16:41:04 +0000 | [diff] [blame] | 584 | auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | |
Raphael Isemann | ae10661 | 2019-11-04 09:05:24 +0100 | [diff] [blame] | 586 | if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 587 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | eDiagnosticSeverityError, |
| 589 | "current process state is unsuitable for expression parsing"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | return false; |
| 591 | } |
| 592 | |
| 593 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
| 594 | DeclMap()->SetLookupsEnabled(true); |
| 595 | } |
| 596 | |
| 597 | Process *process = exe_ctx.GetProcessPtr(); |
| 598 | ExecutionContextScope *exe_scope = process; |
| 599 | |
| 600 | if (!exe_scope) |
| 601 | exe_scope = exe_ctx.GetTargetPtr(); |
| 602 | |
| 603 | // We use a shared pointer here so we can use the original parser - if it |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 604 | // succeeds or the rewrite parser we might make if it fails. But the |
| 605 | // parser_sp will never be empty. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 606 | |
Raphael Isemann | 6c0bbfc | 2019-03-12 17:09:33 +0000 | [diff] [blame] | 607 | ClangExpressionParser parser(exe_scope, *this, generate_debug_info, |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 608 | m_include_directories, m_filename); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 609 | |
| 610 | unsigned num_errors = parser.Parse(diagnostic_manager); |
| 611 | |
| 612 | // Check here for FixItHints. If there are any try to apply the fixits and |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 613 | // set the fixed text in m_fixed_text before returning an error. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 614 | if (num_errors) { |
| 615 | if (diagnostic_manager.HasFixIts()) { |
| 616 | if (parser.RewriteExpression(diagnostic_manager)) { |
| 617 | size_t fixed_start; |
| 618 | size_t fixed_end; |
| 619 | const std::string &fixed_expression = |
| 620 | diagnostic_manager.GetFixedExpression(); |
Raphael Isemann | 06e98a8 | 2019-09-25 07:34:56 +0000 | [diff] [blame] | 621 | // Retrieve the original expression in case we don't have a top level |
| 622 | // expression (which has no surrounding source code). |
| 623 | if (m_source_code && |
| 624 | m_source_code->GetOriginalBodyBounds(fixed_expression, m_expr_lang, |
Raphael Isemann | 1442efe | 2019-09-18 08:53:35 +0000 | [diff] [blame] | 625 | fixed_start, fixed_end)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | m_fixed_text = |
| 627 | fixed_expression.substr(fixed_start, fixed_end - fixed_start); |
| 628 | } |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 629 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 630 | return false; |
| 631 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 632 | |
Raphael Isemann | 54d99bf | 2019-04-24 12:21:03 +0000 | [diff] [blame] | 633 | ////////////////////////////////////////////////////////////////////////////// |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 634 | // Prepare the output of the parser for execution, evaluating it statically |
| 635 | // if possible |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 636 | // |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 637 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 638 | { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 639 | Status jit_error = parser.PrepareForExecution( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 640 | m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx, |
| 641 | m_can_interpret, execution_policy); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 643 | if (!jit_error.Success()) { |
| 644 | const char *error_cstr = jit_error.AsCString(); |
| 645 | if (error_cstr && error_cstr[0]) |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 646 | diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 647 | else |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 648 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 649 | "expression can't be interpreted or run"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 653 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 654 | if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 655 | Status static_init_error = |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 657 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 658 | if (!static_init_error.Success()) { |
| 659 | const char *error_cstr = static_init_error.AsCString(); |
| 660 | if (error_cstr && error_cstr[0]) |
| 661 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 662 | "couldn't run static initializers: %s\n", |
| 663 | error_cstr); |
| 664 | else |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 665 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 666 | "couldn't run static initializers\n"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 667 | return false; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | if (m_execution_unit_sp) { |
| 672 | bool register_execution_unit = false; |
| 673 | |
| 674 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
| 675 | register_execution_unit = true; |
Sean Callanan | b995119 | 2011-08-01 18:18:33 +0000 | [diff] [blame] | 676 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 677 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 678 | // If there is more than one external function in the execution unit, it |
| 679 | // needs to keep living even if it's not top level, because the result |
| 680 | // could refer to that function. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | |
| 682 | if (m_execution_unit_sp->GetJittedFunctions().size() > 1) { |
| 683 | register_execution_unit = true; |
Sean Callanan | 00294b3 | 2016-03-22 21:05:51 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Adrian Prantl | e9331a5 | 2020-01-08 14:18:47 -0800 | [diff] [blame] | 686 | if (register_execution_unit) { |
| 687 | if (auto *persistent_state = |
| 688 | exe_ctx.GetTargetPtr()->GetPersistentExpressionStateForLanguage( |
| 689 | m_language)) |
| 690 | persistent_state->RegisterExecutionUnit(m_execution_unit_sp); |
| 691 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 693 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 694 | if (generate_debug_info) { |
| 695 | lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 696 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | if (jit_module_sp) { |
| 698 | ConstString const_func_name(FunctionName()); |
| 699 | FileSpec jit_file; |
| 700 | jit_file.GetFilename() = const_func_name; |
| 701 | jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString()); |
| 702 | m_jit_module_wp = jit_module_sp; |
| 703 | target->GetImages().Append(jit_module_sp); |
Sean Callanan | 00294b3 | 2016-03-22 21:05:51 +0000 | [diff] [blame] | 704 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 705 | } |
Sean Callanan | 00294b3 | 2016-03-22 21:05:51 +0000 | [diff] [blame] | 706 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 707 | if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS) |
| 708 | m_jit_process_wp = lldb::ProcessWP(process->shared_from_this()); |
| 709 | return true; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 712 | /// Converts an absolute position inside a given code string into |
| 713 | /// a column/line pair. |
| 714 | /// |
Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 715 | /// \param[in] abs_pos |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 716 | /// A absolute position in the code string that we want to convert |
| 717 | /// to a column/line pair. |
| 718 | /// |
Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 719 | /// \param[in] code |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 720 | /// A multi-line string usually representing source code. |
| 721 | /// |
Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 722 | /// \param[out] line |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 723 | /// The line in the code that contains the given absolute position. |
| 724 | /// The first line in the string is indexed as 1. |
| 725 | /// |
Adrian Prantl | f05b42e | 2019-03-11 17:09:29 +0000 | [diff] [blame] | 726 | /// \param[out] column |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 727 | /// The column in the line that contains the absolute position. |
| 728 | /// The first character in a line is indexed as 0. |
Raphael Isemann | bfc5ef6 | 2018-09-22 13:33:08 +0000 | [diff] [blame] | 729 | static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code, |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 730 | unsigned &line, unsigned &column) { |
| 731 | // Reset to code position to beginning of the file. |
| 732 | line = 0; |
| 733 | column = 0; |
| 734 | |
| 735 | assert(abs_pos <= code.size() && "Absolute position outside code string?"); |
| 736 | |
| 737 | // We have to walk up to the position and count lines/columns. |
| 738 | for (std::size_t i = 0; i < abs_pos; ++i) { |
| 739 | // If we hit a line break, we go back to column 0 and enter a new line. |
| 740 | // We only handle \n because that's what we internally use to make new |
| 741 | // lines for our temporary code strings. |
| 742 | if (code[i] == '\n') { |
| 743 | ++line; |
| 744 | column = 0; |
| 745 | continue; |
| 746 | } |
| 747 | ++column; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | bool ClangUserExpression::Complete(ExecutionContext &exe_ctx, |
Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 752 | CompletionRequest &request, |
| 753 | unsigned complete_pos) { |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 754 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 755 | |
| 756 | // We don't want any visible feedback when completing an expression. Mostly |
| 757 | // because the results we get from an incomplete invocation are probably not |
| 758 | // correct. |
| 759 | DiagnosticManager diagnostic_manager; |
| 760 | |
Raphael Isemann | 71569d0 | 2019-05-02 10:12:56 +0000 | [diff] [blame] | 761 | if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true)) |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 762 | return false; |
| 763 | |
Jonas Devlieghere | 63e5fb7 | 2019-07-24 17:56:10 +0000 | [diff] [blame] | 764 | LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str()); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 765 | |
| 766 | ////////////////////////// |
| 767 | // Parse the expression |
| 768 | // |
| 769 | |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 770 | m_materializer_up.reset(new Materializer()); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 771 | |
| 772 | ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true); |
| 773 | |
Tatyana Krasnukha | edebf12 | 2019-04-26 16:41:04 +0000 | [diff] [blame] | 774 | auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); }); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 775 | |
Raphael Isemann | ae10661 | 2019-11-04 09:05:24 +0100 | [diff] [blame] | 776 | if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) { |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 777 | diagnostic_manager.PutString( |
| 778 | eDiagnosticSeverityError, |
| 779 | "current process state is unsuitable for expression parsing"); |
| 780 | |
| 781 | return false; |
| 782 | } |
| 783 | |
| 784 | if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) { |
| 785 | DeclMap()->SetLookupsEnabled(true); |
| 786 | } |
| 787 | |
| 788 | Process *process = exe_ctx.GetProcessPtr(); |
| 789 | ExecutionContextScope *exe_scope = process; |
| 790 | |
| 791 | if (!exe_scope) |
| 792 | exe_scope = exe_ctx.GetTargetPtr(); |
| 793 | |
| 794 | ClangExpressionParser parser(exe_scope, *this, false); |
| 795 | |
| 796 | // We have to find the source code location where the user text is inside |
| 797 | // the transformed expression code. When creating the transformed text, we |
| 798 | // already stored the absolute position in the m_transformed_text string. The |
| 799 | // only thing left to do is to transform it into the line:column format that |
| 800 | // Clang expects. |
| 801 | |
| 802 | // The line and column of the user expression inside the transformed source |
| 803 | // code. |
| 804 | unsigned user_expr_line, user_expr_column; |
| 805 | if (m_user_expression_start_pos.hasValue()) |
| 806 | AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text, |
| 807 | user_expr_line, user_expr_column); |
| 808 | else |
| 809 | return false; |
| 810 | |
| 811 | // The actual column where we have to complete is the start column of the |
| 812 | // user expression + the offset inside the user code that we were given. |
| 813 | const unsigned completion_column = user_expr_column + complete_pos; |
Raphael Isemann | c11a780 | 2018-08-30 21:26:32 +0000 | [diff] [blame] | 814 | parser.Complete(request, user_expr_line, completion_column, complete_pos); |
Raphael Isemann | 7482973 | 2018-08-30 17:29:37 +0000 | [diff] [blame] | 815 | |
| 816 | return true; |
| 817 | } |
| 818 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 819 | bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx, |
| 820 | std::vector<lldb::addr_t> &args, |
| 821 | lldb::addr_t struct_address, |
| 822 | DiagnosticManager &diagnostic_manager) { |
| 823 | lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS; |
| 824 | lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS; |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 825 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 826 | if (m_needs_object_ptr) { |
| 827 | lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP(); |
| 828 | if (!frame_sp) |
| 829 | return true; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 830 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | ConstString object_name; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 832 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | if (m_in_cplusplus_method) { |
| 834 | object_name.SetCString("this"); |
| 835 | } else if (m_in_objectivec_method) { |
| 836 | object_name.SetCString("self"); |
| 837 | } else { |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 838 | diagnostic_manager.PutString( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | eDiagnosticSeverityError, |
| 840 | "need object pointer but don't know the language"); |
| 841 | return false; |
Jim Ingham | 51148e9 | 2015-11-05 00:24:18 +0000 | [diff] [blame] | 842 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 843 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 844 | Status object_ptr_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 845 | |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 846 | if (m_ctx_obj) { |
| 847 | AddressType address_type; |
| 848 | object_ptr = m_ctx_obj->GetAddressOf(false, &address_type); |
| 849 | if (object_ptr == LLDB_INVALID_ADDRESS || |
| 850 | address_type != eAddressTypeLoad) |
| 851 | object_ptr_error.SetErrorString("Can't get context object's " |
| 852 | "debuggee address"); |
| 853 | } else |
| 854 | object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 855 | |
| 856 | if (!object_ptr_error.Success()) { |
| 857 | exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf( |
Jason Molenda | 599558e | 2017-02-21 05:09:26 +0000 | [diff] [blame] | 858 | "warning: `%s' is not accessible (substituting 0)\n", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | object_name.AsCString()); |
| 860 | object_ptr = 0; |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 861 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 862 | |
| 863 | if (m_in_objectivec_method) { |
| 864 | ConstString cmd_name("_cmd"); |
| 865 | |
| 866 | cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error); |
| 867 | |
| 868 | if (!object_ptr_error.Success()) { |
| 869 | diagnostic_manager.Printf( |
| 870 | eDiagnosticSeverityWarning, |
| 871 | "couldn't get cmd pointer (substituting NULL): %s", |
| 872 | object_ptr_error.AsCString()); |
| 873 | cmd_ptr = 0; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | args.push_back(object_ptr); |
| 878 | |
| 879 | if (m_in_objectivec_method) |
| 880 | args.push_back(cmd_ptr); |
| 881 | |
| 882 | args.push_back(struct_address); |
| 883 | } else { |
| 884 | args.push_back(struct_address); |
| 885 | } |
| 886 | return true; |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 889 | lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization( |
| 890 | ExecutionContextScope *exe_scope) { |
| 891 | return m_result_delegate.GetVariable(); |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 894 | void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap( |
| 895 | ExecutionContext &exe_ctx, |
| 896 | Materializer::PersistentVariableDelegate &delegate, |
Aleksandr Urakov | 40624a0 | 2019-02-05 09:14:36 +0000 | [diff] [blame] | 897 | bool keep_result_in_memory, |
| 898 | ValueObject *ctx_obj) { |
Alex Langford | c4f6fbe | 2020-01-23 13:49:54 -0800 | [diff] [blame] | 899 | lldb::ClangASTImporterSP ast_importer; |
| 900 | auto *state = exe_ctx.GetTargetSP()->GetPersistentExpressionStateForLanguage( |
| 901 | lldb::eLanguageTypeC); |
| 902 | if (state) { |
| 903 | auto *persistent_vars = llvm::cast<ClangPersistentVariables>(state); |
| 904 | ast_importer = persistent_vars->GetClangASTImporter(); |
| 905 | } |
| 906 | m_expr_decl_map_up.reset( |
| 907 | new ClangExpressionDeclMap(keep_result_in_memory, &delegate, |
| 908 | exe_ctx.GetTargetSP(), ast_importer, ctx_obj)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 909 | } |
Jim Ingham | 36f3b36 | 2010-10-14 23:45:03 +0000 | [diff] [blame] | 910 | |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 911 | clang::ASTConsumer * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 912 | ClangUserExpression::ClangUserExpressionHelper::ASTTransformer( |
| 913 | clang::ASTConsumer *passthrough) { |
| 914 | m_result_synthesizer_up.reset( |
| 915 | new ASTResultSynthesizer(passthrough, m_top_level, m_target)); |
Sean Callanan | c673a6e | 2010-12-07 10:00:20 +0000 | [diff] [blame] | 916 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 917 | return m_result_synthesizer_up.get(); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 920 | void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() { |
Jonas Devlieghere | 70355ac | 2019-02-12 03:47:39 +0000 | [diff] [blame] | 921 | if (m_result_synthesizer_up) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 922 | m_result_synthesizer_up->CommitPersistentDecls(); |
| 923 | } |
Sean Callanan | 00294b3 | 2016-03-22 21:05:51 +0000 | [diff] [blame] | 924 | } |
| 925 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 926 | ConstString ClangUserExpression::ResultDelegate::GetName() { |
Adrian Prantl | 03219f7 | 2018-04-30 23:59:17 +0000 | [diff] [blame] | 927 | auto prefix = m_persistent_state->GetPersistentVariablePrefix(); |
| 928 | return m_persistent_state->GetNextPersistentVariableName(*m_target_sp, |
| 929 | prefix); |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 932 | void ClangUserExpression::ResultDelegate::DidDematerialize( |
| 933 | lldb::ExpressionVariableSP &variable) { |
| 934 | m_variable = variable; |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 937 | void ClangUserExpression::ResultDelegate::RegisterPersistentState( |
| 938 | PersistentExpressionState *persistent_state) { |
| 939 | m_persistent_state = persistent_state; |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 942 | lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() { |
| 943 | return m_variable; |
Sean Callanan | 9fda9d2 | 2015-10-03 09:09:01 +0000 | [diff] [blame] | 944 | } |