1 //===-- ClangUserExpression.cpp ---------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Host/Config.h"
10 
11 #include <stdio.h>
12 #if HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15 
16 #include <cstdlib>
17 #include <map>
18 #include <string>
19 
20 #include "ClangUserExpression.h"
21 
22 #include "ASTResultSynthesizer.h"
23 #include "ClangDiagnostic.h"
24 #include "ClangExpressionDeclMap.h"
25 #include "ClangExpressionParser.h"
26 #include "ClangModulesDeclVendor.h"
27 #include "ClangPersistentVariables.h"
28 #include "CppModuleConfiguration.h"
29 
30 #include "lldb/Core/Debugger.h"
31 #include "lldb/Core/Module.h"
32 #include "lldb/Core/StreamFile.h"
33 #include "lldb/Core/ValueObjectConstResult.h"
34 #include "lldb/Expression/ExpressionSourceCode.h"
35 #include "lldb/Expression/IRExecutionUnit.h"
36 #include "lldb/Expression/IRInterpreter.h"
37 #include "lldb/Expression/Materializer.h"
38 #include "lldb/Host/HostInfo.h"
39 #include "lldb/Symbol/Block.h"
40 #include "lldb/Symbol/ClangASTContext.h"
41 #include "lldb/Symbol/CompileUnit.h"
42 #include "lldb/Symbol/Function.h"
43 #include "lldb/Symbol/ObjectFile.h"
44 #include "lldb/Symbol/SymbolFile.h"
45 #include "lldb/Symbol/SymbolVendor.h"
46 #include "lldb/Symbol/Type.h"
47 #include "lldb/Symbol/VariableList.h"
48 #include "lldb/Target/ExecutionContext.h"
49 #include "lldb/Target/Process.h"
50 #include "lldb/Target/StackFrame.h"
51 #include "lldb/Target/Target.h"
52 #include "lldb/Target/ThreadPlan.h"
53 #include "lldb/Target/ThreadPlanCallUserExpression.h"
54 #include "lldb/Utility/ConstString.h"
55 #include "lldb/Utility/Log.h"
56 #include "lldb/Utility/StreamString.h"
57 
58 #include "clang/AST/DeclCXX.h"
59 #include "clang/AST/DeclObjC.h"
60 
61 #include "llvm/ADT/ScopeExit.h"
62 
63 using namespace lldb_private;
64 
65 char ClangUserExpression::ID;
66 
67 ClangUserExpression::ClangUserExpression(
68     ExecutionContextScope &exe_scope, llvm::StringRef expr,
69     llvm::StringRef prefix, lldb::LanguageType language,
70     ResultType desired_type, const EvaluateExpressionOptions &options,
71     ValueObject *ctx_obj)
72     : LLVMUserExpression(exe_scope, expr, prefix, language, desired_type,
73                          options),
74       m_type_system_helper(*m_target_wp.lock(), options.GetExecutionPolicy() ==
75                                                     eExecutionPolicyTopLevel),
76       m_result_delegate(exe_scope.CalculateTarget()), m_ctx_obj(ctx_obj) {
77   switch (m_language) {
78   case lldb::eLanguageTypeC_plus_plus:
79     m_allow_cxx = true;
80     break;
81   case lldb::eLanguageTypeObjC:
82     m_allow_objc = true;
83     break;
84   case lldb::eLanguageTypeObjC_plus_plus:
85   default:
86     m_allow_cxx = true;
87     m_allow_objc = true;
88     break;
89   }
90 }
91 
92 ClangUserExpression::~ClangUserExpression() {}
93 
94 void ClangUserExpression::ScanContext(ExecutionContext &exe_ctx, Status &err) {
95   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
96 
97   LLDB_LOGF(log, "ClangUserExpression::ScanContext()");
98 
99   m_target = exe_ctx.GetTargetPtr();
100 
101   if (!(m_allow_cxx || m_allow_objc)) {
102     LLDB_LOGF(log, "  [CUE::SC] Settings inhibit C++ and Objective-C");
103     return;
104   }
105 
106   StackFrame *frame = exe_ctx.GetFramePtr();
107   if (frame == nullptr) {
108     LLDB_LOGF(log, "  [CUE::SC] Null stack frame");
109     return;
110   }
111 
112   SymbolContext sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction |
113                                                   lldb::eSymbolContextBlock);
114 
115   if (!sym_ctx.function) {
116     LLDB_LOGF(log, "  [CUE::SC] Null function");
117     return;
118   }
119 
120   // Find the block that defines the function represented by "sym_ctx"
121   Block *function_block = sym_ctx.GetFunctionBlock();
122 
123   if (!function_block) {
124     LLDB_LOGF(log, "  [CUE::SC] Null function block");
125     return;
126   }
127 
128   CompilerDeclContext decl_context = function_block->GetDeclContext();
129 
130   if (!decl_context) {
131     LLDB_LOGF(log, "  [CUE::SC] Null decl context");
132     return;
133   }
134 
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 =
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));
161 
162         const char *thisErrorString = "Stopped in a C++ method, but 'this' "
163                                       "isn't available; pretending we are in a "
164                                       "generic context";
165 
166         if (!variable_list_sp) {
167           err.SetErrorString(thisErrorString);
168           return;
169         }
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;
183     }
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));
191 
192         const char *selfErrorString = "Stopped in an Objective-C method, but "
193                                       "'self' isn't available; pretending we "
194                                       "are in a generic context";
195 
196         if (!variable_list_sp) {
197           err.SetErrorString(selfErrorString);
198           return;
199         }
200 
201         lldb::VariableSP self_variable_sp =
202             variable_list_sp->FindVariable(ConstString("self"));
203 
204         if (!self_variable_sp || !self_variable_sp->IsInScope(frame) ||
205             !self_variable_sp->LocationIsValidForFrame(frame)) {
206           err.SetErrorString(selfErrorString);
207           return;
208         }
209       }
210 
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
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.
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)) {
298             m_in_objectivec_method = true;
299             m_needs_object_ptr = true;
300           } else {
301             err.SetErrorString(selfErrorString);
302             return;
303           }
304         } else {
305           m_in_objectivec_method = true;
306           m_needs_object_ptr = true;
307         }
308       }
309     }
310   }
311 }
312 
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.
317 static void ApplyObjcCastHack(std::string &expr) {
318   const std::string from = "(int)[";
319   const std::string to = "(int)(long long)[";
320 
321   size_t offset;
322 
323   while ((offset = expr.find(from)) != expr.npos)
324     expr.replace(offset, from.size(), to);
325 }
326 
327 bool ClangUserExpression::SetupPersistentState(DiagnosticManager &diagnostic_manager,
328                                  ExecutionContext &exe_ctx) {
329   if (Target *target = exe_ctx.GetTargetPtr()) {
330     if (PersistentExpressionState *persistent_state =
331             target->GetPersistentExpressionStateForLanguage(
332                 lldb::eLanguageTypeC)) {
333       m_clang_state = llvm::cast<ClangPersistentVariables>(persistent_state);
334       m_result_delegate.RegisterPersistentState(persistent_state);
335     } else {
336       diagnostic_manager.PutString(
337           eDiagnosticSeverityError,
338           "couldn't start parsing (no persistent data)");
339       return false;
340     }
341   } else {
342     diagnostic_manager.PutString(eDiagnosticSeverityError,
343                                  "error: couldn't start parsing (no target)");
344     return false;
345   }
346   return true;
347 }
348 
349 static void SetupDeclVendor(ExecutionContext &exe_ctx, Target *target) {
350   if (ClangModulesDeclVendor *decl_vendor =
351           target->GetClangModulesDeclVendor()) {
352     const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
353         llvm::cast<ClangPersistentVariables>(
354             target->GetPersistentExpressionStateForLanguage(
355                 lldb::eLanguageTypeC))
356             ->GetHandLoadedClangModules();
357     ClangModulesDeclVendor::ModuleVector modules_for_macros;
358 
359     for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
360       modules_for_macros.push_back(module);
361     }
362 
363     if (target->GetEnableAutoImportClangModules()) {
364       if (StackFrame *frame = exe_ctx.GetFramePtr()) {
365         if (Block *block = frame->GetFrameBlock()) {
366           SymbolContext sc;
367 
368           block->CalculateSymbolContext(&sc);
369 
370           if (sc.comp_unit) {
371             StreamString error_stream;
372 
373             decl_vendor->AddModulesForCompileUnit(
374                 *sc.comp_unit, modules_for_macros, error_stream);
375           }
376         }
377       }
378     }
379   }
380 }
381 
382 void ClangUserExpression::UpdateLanguageForExpr() {
383   m_expr_lang = lldb::LanguageType::eLanguageTypeUnknown;
384   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel)
385     return;
386   if (m_in_cplusplus_method)
387     m_expr_lang = lldb::eLanguageTypeC_plus_plus;
388   else if (m_in_objectivec_method)
389     m_expr_lang = lldb::eLanguageTypeObjC;
390   else
391     m_expr_lang = lldb::eLanguageTypeC;
392 }
393 
394 void ClangUserExpression::CreateSourceCode(
395     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
396     std::vector<std::string> modules_to_import, bool for_completion) {
397 
398   m_filename = m_clang_state->GetNextExprFileName();
399   std::string prefix = m_expr_prefix;
400 
401   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
402     m_transformed_text = m_expr_text;
403   } else {
404     m_source_code.reset(ClangExpressionSourceCode::CreateWrapped(
405         m_filename, prefix.c_str(), m_expr_text.c_str()));
406 
407     if (!m_source_code->GetText(m_transformed_text, m_expr_lang,
408                                 m_in_static_method, exe_ctx, !m_ctx_obj,
409                                 for_completion, modules_to_import)) {
410       diagnostic_manager.PutString(eDiagnosticSeverityError,
411                                    "couldn't construct expression body");
412       return;
413     }
414 
415     // Find and store the start position of the original code inside the
416     // transformed code. We need this later for the code completion.
417     std::size_t original_start;
418     std::size_t original_end;
419     bool found_bounds = m_source_code->GetOriginalBodyBounds(
420         m_transformed_text, m_expr_lang, original_start, original_end);
421     if (found_bounds)
422       m_user_expression_start_pos = original_start;
423   }
424 }
425 
426 static bool SupportsCxxModuleImport(lldb::LanguageType language) {
427   switch (language) {
428   case lldb::eLanguageTypeC_plus_plus:
429   case lldb::eLanguageTypeC_plus_plus_03:
430   case lldb::eLanguageTypeC_plus_plus_11:
431   case lldb::eLanguageTypeC_plus_plus_14:
432   case lldb::eLanguageTypeObjC_plus_plus:
433     return true;
434   default:
435     return false;
436   }
437 }
438 
439 /// Utility method that puts a message into the expression log and
440 /// returns an invalid module configuration.
441 static CppModuleConfiguration LogConfigError(const std::string &msg) {
442   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
443   LLDB_LOG(log, "[C++ module config] {0}", msg);
444   return CppModuleConfiguration();
445 }
446 
447 CppModuleConfiguration GetModuleConfig(lldb::LanguageType language,
448                                        ExecutionContext &exe_ctx) {
449   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
450 
451   // Don't do anything if this is not a C++ module configuration.
452   if (!SupportsCxxModuleImport(language))
453     return LogConfigError("Language doesn't support C++ modules");
454 
455   Target *target = exe_ctx.GetTargetPtr();
456   if (!target)
457     return LogConfigError("No target");
458 
459   if (!target->GetEnableImportStdModule())
460     return LogConfigError("Importing std module not enabled in settings");
461 
462   StackFrame *frame = exe_ctx.GetFramePtr();
463   if (!frame)
464     return LogConfigError("No frame");
465 
466   Block *block = frame->GetFrameBlock();
467   if (!block)
468     return LogConfigError("No block");
469 
470   SymbolContext sc;
471   block->CalculateSymbolContext(&sc);
472   if (!sc.comp_unit)
473     return LogConfigError("Couldn't calculate symbol context");
474 
475   // Build a list of files we need to analyze to build the configuration.
476   FileSpecList files;
477   for (const FileSpec &f : sc.comp_unit->GetSupportFiles())
478     files.AppendIfUnique(f);
479   // We also need to look at external modules in the case of -gmodules as they
480   // contain the support files for libc++ and the C library.
481   llvm::DenseSet<SymbolFile *> visited_symbol_files;
482   sc.comp_unit->ForEachExternalModule(
483       visited_symbol_files, [&files](Module &module) {
484         for (std::size_t i = 0; i < module.GetNumCompileUnits(); ++i) {
485           const FileSpecList &support_files =
486               module.GetCompileUnitAtIndex(i)->GetSupportFiles();
487           for (const FileSpec &f : support_files) {
488             files.AppendIfUnique(f);
489           }
490         }
491         return false;
492       });
493 
494   LLDB_LOG(log, "[C++ module config] Found {0} support files to analyze",
495            files.GetSize());
496   if (log && log->GetVerbose()) {
497     for (const FileSpec &f : files)
498       LLDB_LOGV(log, "[C++ module config] Analyzing support file: {0}",
499                 f.GetPath());
500   }
501 
502   // Try to create a configuration from the files. If there is no valid
503   // configuration possible with the files, this just returns an invalid
504   // configuration.
505   return CppModuleConfiguration(files);
506 }
507 
508 bool ClangUserExpression::PrepareForParsing(
509     DiagnosticManager &diagnostic_manager, ExecutionContext &exe_ctx,
510     bool for_completion) {
511   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
512 
513   InstallContext(exe_ctx);
514 
515   if (!SetupPersistentState(diagnostic_manager, exe_ctx))
516     return false;
517 
518   Status err;
519   ScanContext(exe_ctx, err);
520 
521   if (!err.Success()) {
522     diagnostic_manager.PutString(eDiagnosticSeverityWarning, err.AsCString());
523   }
524 
525   ////////////////////////////////////
526   // Generate the expression
527   //
528 
529   ApplyObjcCastHack(m_expr_text);
530 
531   SetupDeclVendor(exe_ctx, m_target);
532 
533   CppModuleConfiguration module_config = GetModuleConfig(m_language, exe_ctx);
534   llvm::ArrayRef<std::string> imported_modules =
535       module_config.GetImportedModules();
536   m_imported_cpp_modules = !imported_modules.empty();
537   m_include_directories = module_config.GetIncludeDirs();
538 
539   LLDB_LOG(log, "List of imported modules in expression: {0}",
540            llvm::make_range(imported_modules.begin(), imported_modules.end()));
541   LLDB_LOG(log, "List of include directories gathered for modules: {0}",
542            llvm::make_range(m_include_directories.begin(),
543                             m_include_directories.end()));
544 
545   UpdateLanguageForExpr();
546   CreateSourceCode(diagnostic_manager, exe_ctx, imported_modules,
547                    for_completion);
548   return true;
549 }
550 
551 bool ClangUserExpression::Parse(DiagnosticManager &diagnostic_manager,
552                                 ExecutionContext &exe_ctx,
553                                 lldb_private::ExecutionPolicy execution_policy,
554                                 bool keep_result_in_memory,
555                                 bool generate_debug_info) {
556   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
557 
558   if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ false))
559     return false;
560 
561   LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
562 
563   ////////////////////////////////////
564   // Set up the target and compiler
565   //
566 
567   Target *target = exe_ctx.GetTargetPtr();
568 
569   if (!target) {
570     diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
571     return false;
572   }
573 
574   //////////////////////////
575   // Parse the expression
576   //
577 
578   m_materializer_up.reset(new Materializer());
579 
580   ResetDeclMap(exe_ctx, m_result_delegate, keep_result_in_memory);
581 
582   auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
583 
584   if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) {
585     diagnostic_manager.PutString(
586         eDiagnosticSeverityError,
587         "current process state is unsuitable for expression parsing");
588     return false;
589   }
590 
591   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
592     DeclMap()->SetLookupsEnabled(true);
593   }
594 
595   Process *process = exe_ctx.GetProcessPtr();
596   ExecutionContextScope *exe_scope = process;
597 
598   if (!exe_scope)
599     exe_scope = exe_ctx.GetTargetPtr();
600 
601   // We use a shared pointer here so we can use the original parser - if it
602   // succeeds or the rewrite parser we might make if it fails.  But the
603   // parser_sp will never be empty.
604 
605   ClangExpressionParser parser(exe_scope, *this, generate_debug_info,
606                                m_include_directories, m_filename);
607 
608   unsigned num_errors = parser.Parse(diagnostic_manager);
609 
610   // Check here for FixItHints.  If there are any try to apply the fixits and
611   // set the fixed text in m_fixed_text before returning an error.
612   if (num_errors) {
613     if (diagnostic_manager.HasFixIts()) {
614       if (parser.RewriteExpression(diagnostic_manager)) {
615         size_t fixed_start;
616         size_t fixed_end;
617         const std::string &fixed_expression =
618             diagnostic_manager.GetFixedExpression();
619         // Retrieve the original expression in case we don't have a top level
620         // expression (which has no surrounding source code).
621         if (m_source_code &&
622             m_source_code->GetOriginalBodyBounds(fixed_expression, m_expr_lang,
623                                                  fixed_start, fixed_end))
624           m_fixed_text =
625               fixed_expression.substr(fixed_start, fixed_end - fixed_start);
626       }
627     }
628     return false;
629   }
630 
631   //////////////////////////////////////////////////////////////////////////////
632   // Prepare the output of the parser for execution, evaluating it statically
633   // if possible
634   //
635 
636   {
637     Status jit_error = parser.PrepareForExecution(
638         m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
639         m_can_interpret, execution_policy);
640 
641     if (!jit_error.Success()) {
642       const char *error_cstr = jit_error.AsCString();
643       if (error_cstr && error_cstr[0])
644         diagnostic_manager.PutString(eDiagnosticSeverityError, error_cstr);
645       else
646         diagnostic_manager.PutString(eDiagnosticSeverityError,
647                                      "expression can't be interpreted or run");
648       return false;
649     }
650   }
651 
652   if (exe_ctx.GetProcessPtr() && execution_policy == eExecutionPolicyTopLevel) {
653     Status static_init_error =
654         parser.RunStaticInitializers(m_execution_unit_sp, exe_ctx);
655 
656     if (!static_init_error.Success()) {
657       const char *error_cstr = static_init_error.AsCString();
658       if (error_cstr && error_cstr[0])
659         diagnostic_manager.Printf(eDiagnosticSeverityError,
660                                   "couldn't run static initializers: %s\n",
661                                   error_cstr);
662       else
663         diagnostic_manager.PutString(eDiagnosticSeverityError,
664                                      "couldn't run static initializers\n");
665       return false;
666     }
667   }
668 
669   if (m_execution_unit_sp) {
670     bool register_execution_unit = false;
671 
672     if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
673       register_execution_unit = true;
674     }
675 
676     // If there is more than one external function in the execution unit, it
677     // needs to keep living even if it's not top level, because the result
678     // could refer to that function.
679 
680     if (m_execution_unit_sp->GetJittedFunctions().size() > 1) {
681       register_execution_unit = true;
682     }
683 
684     if (register_execution_unit)
685       exe_ctx.GetTargetPtr()
686           ->GetPersistentExpressionStateForLanguage(m_language)
687           ->RegisterExecutionUnit(m_execution_unit_sp);
688   }
689 
690   if (generate_debug_info) {
691     lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
692 
693     if (jit_module_sp) {
694       ConstString const_func_name(FunctionName());
695       FileSpec jit_file;
696       jit_file.GetFilename() = const_func_name;
697       jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
698       m_jit_module_wp = jit_module_sp;
699       target->GetImages().Append(jit_module_sp);
700     }
701   }
702 
703   if (process && m_jit_start_addr != LLDB_INVALID_ADDRESS)
704     m_jit_process_wp = lldb::ProcessWP(process->shared_from_this());
705   return true;
706 }
707 
708 /// Converts an absolute position inside a given code string into
709 /// a column/line pair.
710 ///
711 /// \param[in] abs_pos
712 ///     A absolute position in the code string that we want to convert
713 ///     to a column/line pair.
714 ///
715 /// \param[in] code
716 ///     A multi-line string usually representing source code.
717 ///
718 /// \param[out] line
719 ///     The line in the code that contains the given absolute position.
720 ///     The first line in the string is indexed as 1.
721 ///
722 /// \param[out] column
723 ///     The column in the line that contains the absolute position.
724 ///     The first character in a line is indexed as 0.
725 static void AbsPosToLineColumnPos(size_t abs_pos, llvm::StringRef code,
726                                   unsigned &line, unsigned &column) {
727   // Reset to code position to beginning of the file.
728   line = 0;
729   column = 0;
730 
731   assert(abs_pos <= code.size() && "Absolute position outside code string?");
732 
733   // We have to walk up to the position and count lines/columns.
734   for (std::size_t i = 0; i < abs_pos; ++i) {
735     // If we hit a line break, we go back to column 0 and enter a new line.
736     // We only handle \n because that's what we internally use to make new
737     // lines for our temporary code strings.
738     if (code[i] == '\n') {
739       ++line;
740       column = 0;
741       continue;
742     }
743     ++column;
744   }
745 }
746 
747 bool ClangUserExpression::Complete(ExecutionContext &exe_ctx,
748                                    CompletionRequest &request,
749                                    unsigned complete_pos) {
750   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
751 
752   // We don't want any visible feedback when completing an expression. Mostly
753   // because the results we get from an incomplete invocation are probably not
754   // correct.
755   DiagnosticManager diagnostic_manager;
756 
757   if (!PrepareForParsing(diagnostic_manager, exe_ctx, /*for_completion*/ true))
758     return false;
759 
760   LLDB_LOGF(log, "Parsing the following code:\n%s", m_transformed_text.c_str());
761 
762   //////////////////////////
763   // Parse the expression
764   //
765 
766   m_materializer_up.reset(new Materializer());
767 
768   ResetDeclMap(exe_ctx, m_result_delegate, /*keep result in memory*/ true);
769 
770   auto on_exit = llvm::make_scope_exit([this]() { ResetDeclMap(); });
771 
772   if (!DeclMap()->WillParse(exe_ctx, GetMaterializer())) {
773     diagnostic_manager.PutString(
774         eDiagnosticSeverityError,
775         "current process state is unsuitable for expression parsing");
776 
777     return false;
778   }
779 
780   if (m_options.GetExecutionPolicy() == eExecutionPolicyTopLevel) {
781     DeclMap()->SetLookupsEnabled(true);
782   }
783 
784   Process *process = exe_ctx.GetProcessPtr();
785   ExecutionContextScope *exe_scope = process;
786 
787   if (!exe_scope)
788     exe_scope = exe_ctx.GetTargetPtr();
789 
790   ClangExpressionParser parser(exe_scope, *this, false);
791 
792   // We have to find the source code location where the user text is inside
793   // the transformed expression code. When creating the transformed text, we
794   // already stored the absolute position in the m_transformed_text string. The
795   // only thing left to do is to transform it into the line:column format that
796   // Clang expects.
797 
798   // The line and column of the user expression inside the transformed source
799   // code.
800   unsigned user_expr_line, user_expr_column;
801   if (m_user_expression_start_pos.hasValue())
802     AbsPosToLineColumnPos(*m_user_expression_start_pos, m_transformed_text,
803                           user_expr_line, user_expr_column);
804   else
805     return false;
806 
807   // The actual column where we have to complete is the start column of the
808   // user expression + the offset inside the user code that we were given.
809   const unsigned completion_column = user_expr_column + complete_pos;
810   parser.Complete(request, user_expr_line, completion_column, complete_pos);
811 
812   return true;
813 }
814 
815 bool ClangUserExpression::AddArguments(ExecutionContext &exe_ctx,
816                                        std::vector<lldb::addr_t> &args,
817                                        lldb::addr_t struct_address,
818                                        DiagnosticManager &diagnostic_manager) {
819   lldb::addr_t object_ptr = LLDB_INVALID_ADDRESS;
820   lldb::addr_t cmd_ptr = LLDB_INVALID_ADDRESS;
821 
822   if (m_needs_object_ptr) {
823     lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP();
824     if (!frame_sp)
825       return true;
826 
827     ConstString object_name;
828 
829     if (m_in_cplusplus_method) {
830       object_name.SetCString("this");
831     } else if (m_in_objectivec_method) {
832       object_name.SetCString("self");
833     } else {
834       diagnostic_manager.PutString(
835           eDiagnosticSeverityError,
836           "need object pointer but don't know the language");
837       return false;
838     }
839 
840     Status object_ptr_error;
841 
842     if (m_ctx_obj) {
843       AddressType address_type;
844       object_ptr = m_ctx_obj->GetAddressOf(false, &address_type);
845       if (object_ptr == LLDB_INVALID_ADDRESS ||
846           address_type != eAddressTypeLoad)
847         object_ptr_error.SetErrorString("Can't get context object's "
848                                         "debuggee address");
849     } else
850       object_ptr = GetObjectPointer(frame_sp, object_name, object_ptr_error);
851 
852     if (!object_ptr_error.Success()) {
853       exe_ctx.GetTargetRef().GetDebugger().GetAsyncOutputStream()->Printf(
854           "warning: `%s' is not accessible (substituting 0)\n",
855           object_name.AsCString());
856       object_ptr = 0;
857     }
858 
859     if (m_in_objectivec_method) {
860       ConstString cmd_name("_cmd");
861 
862       cmd_ptr = GetObjectPointer(frame_sp, cmd_name, object_ptr_error);
863 
864       if (!object_ptr_error.Success()) {
865         diagnostic_manager.Printf(
866             eDiagnosticSeverityWarning,
867             "couldn't get cmd pointer (substituting NULL): %s",
868             object_ptr_error.AsCString());
869         cmd_ptr = 0;
870       }
871     }
872 
873     args.push_back(object_ptr);
874 
875     if (m_in_objectivec_method)
876       args.push_back(cmd_ptr);
877 
878     args.push_back(struct_address);
879   } else {
880     args.push_back(struct_address);
881   }
882   return true;
883 }
884 
885 lldb::ExpressionVariableSP ClangUserExpression::GetResultAfterDematerialization(
886     ExecutionContextScope *exe_scope) {
887   return m_result_delegate.GetVariable();
888 }
889 
890 void ClangUserExpression::ClangUserExpressionHelper::ResetDeclMap(
891     ExecutionContext &exe_ctx,
892     Materializer::PersistentVariableDelegate &delegate,
893     bool keep_result_in_memory,
894     ValueObject *ctx_obj) {
895   m_expr_decl_map_up.reset(new ClangExpressionDeclMap(
896       keep_result_in_memory, &delegate, exe_ctx.GetTargetSP(),
897       exe_ctx.GetTargetRef().GetClangASTImporter(), ctx_obj));
898 }
899 
900 clang::ASTConsumer *
901 ClangUserExpression::ClangUserExpressionHelper::ASTTransformer(
902     clang::ASTConsumer *passthrough) {
903   m_result_synthesizer_up.reset(
904       new ASTResultSynthesizer(passthrough, m_top_level, m_target));
905 
906   return m_result_synthesizer_up.get();
907 }
908 
909 void ClangUserExpression::ClangUserExpressionHelper::CommitPersistentDecls() {
910   if (m_result_synthesizer_up) {
911     m_result_synthesizer_up->CommitPersistentDecls();
912   }
913 }
914 
915 ConstString ClangUserExpression::ResultDelegate::GetName() {
916   auto prefix = m_persistent_state->GetPersistentVariablePrefix();
917   return m_persistent_state->GetNextPersistentVariableName(*m_target_sp,
918                                                            prefix);
919 }
920 
921 void ClangUserExpression::ResultDelegate::DidDematerialize(
922     lldb::ExpressionVariableSP &variable) {
923   m_variable = variable;
924 }
925 
926 void ClangUserExpression::ResultDelegate::RegisterPersistentState(
927     PersistentExpressionState *persistent_state) {
928   m_persistent_state = persistent_state;
929 }
930 
931 lldb::ExpressionVariableSP &ClangUserExpression::ResultDelegate::GetVariable() {
932   return m_variable;
933 }
934