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