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