Coverage Report

Created: 2023-09-12 09:32

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