1 //===-- ClangUtilityFunction.cpp ---------------------------------*- C++-*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ClangUtilityFunction.h"
11 #include "ClangExpressionDeclMap.h"
12 #include "ClangExpressionParser.h"
16 #include <sys/types.h>
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Expression/ExpressionSourceCode.h"
23 #include "lldb/Expression/IRExecutionUnit.h"
24 #include "lldb/Host/Host.h"
25 #include "lldb/Target/ExecutionContext.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/Stream.h"
31 using namespace lldb_private;
33 //------------------------------------------------------------------
37 /// The text of the function. Must be a full translation unit.
40 /// The name of the function, as used in the text.
41 //------------------------------------------------------------------
42 ClangUtilityFunction::ClangUtilityFunction(ExecutionContextScope &exe_scope,
43 const char *text, const char *name)
44 : UtilityFunction(exe_scope, text, name) {}
46 ClangUtilityFunction::~ClangUtilityFunction() {}
48 //------------------------------------------------------------------
49 /// Install the utility function into a process
51 /// @param[in] diagnostic_manager
52 /// A diagnostic manager to report errors and warnings to.
54 /// @param[in] exe_ctx
55 /// The execution context to install the utility function to.
58 /// True on success (no errors); false otherwise.
59 //------------------------------------------------------------------
60 bool ClangUtilityFunction::Install(DiagnosticManager &diagnostic_manager,
61 ExecutionContext &exe_ctx) {
62 if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
63 diagnostic_manager.PutString(eDiagnosticSeverityWarning,
68 ////////////////////////////////////
69 // Set up the target and compiler
72 Target *target = exe_ctx.GetTargetPtr();
75 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid target");
79 Process *process = exe_ctx.GetProcessPtr();
82 diagnostic_manager.PutString(eDiagnosticSeverityError, "invalid process");
86 //////////////////////////
87 // Parse the expression
90 bool keep_result_in_memory = false;
92 ResetDeclMap(exe_ctx, keep_result_in_memory);
94 if (!DeclMap()->WillParse(exe_ctx, NULL)) {
95 diagnostic_manager.PutString(
96 eDiagnosticSeverityError,
97 "current process state is unsuitable for expression parsing");
101 const bool generate_debug_info = true;
102 ClangExpressionParser parser(exe_ctx.GetBestExecutionContextScope(), *this,
103 generate_debug_info);
105 unsigned num_errors = parser.Parse(diagnostic_manager);
113 //////////////////////////////////
114 // JIT the output of the parser
117 bool can_interpret = false; // should stay that way
119 Status jit_error = parser.PrepareForExecution(
120 m_jit_start_addr, m_jit_end_addr, m_execution_unit_sp, exe_ctx,
121 can_interpret, eExecutionPolicyAlways);
123 if (m_jit_start_addr != LLDB_INVALID_ADDRESS) {
124 m_jit_process_wp = process->shared_from_this();
125 if (parser.GetGenerateDebugInfo()) {
126 lldb::ModuleSP jit_module_sp(m_execution_unit_sp->GetJITModule());
129 ConstString const_func_name(FunctionName());
131 jit_file.GetFilename() = const_func_name;
132 jit_module_sp->SetFileSpecAndObjectName(jit_file, ConstString());
133 m_jit_module_wp = jit_module_sp;
134 target->GetImages().Append(jit_module_sp);
139 DeclMap()->DidParse();
143 if (jit_error.Success()) {
146 const char *error_cstr = jit_error.AsCString();
147 if (error_cstr && error_cstr[0]) {
148 diagnostic_manager.Printf(eDiagnosticSeverityError, "%s", error_cstr);
150 diagnostic_manager.PutString(eDiagnosticSeverityError,
151 "expression can't be interpreted or run");
157 void ClangUtilityFunction::ClangUtilityFunctionHelper::ResetDeclMap(
158 ExecutionContext &exe_ctx, bool keep_result_in_memory) {
159 m_expr_decl_map_up.reset(
160 new ClangExpressionDeclMap(keep_result_in_memory, nullptr, exe_ctx));