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