1 //===-- ExpressionSourceCode.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 "lldb/Expression/ExpressionSourceCode.h"
12 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
13 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
14 #include "lldb/Symbol/Block.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/DebugMacros.h"
17 #include "lldb/Symbol/TypeSystem.h"
18 #include "lldb/Symbol/VariableList.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/Language.h"
21 #include "lldb/Target/Platform.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Utility/StreamString.h"
26 using namespace lldb_private;
28 const char *ExpressionSourceCode::g_expression_prefix = R"(
44 typedef __INT8_TYPE__ int8_t;
45 typedef __UINT8_TYPE__ uint8_t;
46 typedef __INT16_TYPE__ int16_t;
47 typedef __UINT16_TYPE__ uint16_t;
48 typedef __INT32_TYPE__ int32_t;
49 typedef __UINT32_TYPE__ uint32_t;
50 typedef __INT64_TYPE__ int64_t;
51 typedef __UINT64_TYPE__ uint64_t;
52 typedef __INTPTR_TYPE__ intptr_t;
53 typedef __UINTPTR_TYPE__ uintptr_t;
54 typedef __SIZE_TYPE__ size_t;
55 typedef __PTRDIFF_TYPE__ ptrdiff_t;
56 typedef unsigned short unichar;
59 int printf(const char * __restrict, ...);
63 static const char *c_start_marker = " /*LLDB_BODY_START*/\n ";
64 static const char *c_end_marker = ";\n /*LLDB_BODY_END*/\n";
70 CURRENT_FILE_NOT_YET_PUSHED,
76 AddMacroState(const FileSpec ¤t_file, const uint32_t current_file_line)
77 : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
78 m_current_file_line(current_file_line) {}
80 void StartFile(const FileSpec &file) {
81 m_file_stack.push_back(file);
82 if (file == m_current_file)
83 m_state = CURRENT_FILE_PUSHED;
87 if (m_file_stack.size() == 0)
90 FileSpec old_top = m_file_stack.back();
91 m_file_stack.pop_back();
92 if (old_top == m_current_file)
93 m_state = CURRENT_FILE_POPPED;
96 // An entry is valid if it occurs before the current line in
98 bool IsValidEntry(uint32_t line) {
100 case CURRENT_FILE_NOT_YET_PUSHED:
102 case CURRENT_FILE_PUSHED:
103 // If we are in file included in the current file,
104 // the entry should be added.
105 if (m_file_stack.back() != m_current_file)
108 if (line >= m_current_file_line)
118 std::vector<FileSpec> m_file_stack;
120 FileSpec m_current_file;
121 uint32_t m_current_file_line;
124 } // anonymous namespace
126 static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
127 AddMacroState &state, StreamString &stream) {
131 for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
132 const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
135 switch (entry.GetType()) {
136 case DebugMacroEntry::DEFINE:
137 if (state.IsValidEntry(entry.GetLineNumber()))
138 stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
142 case DebugMacroEntry::UNDEF:
143 if (state.IsValidEntry(entry.GetLineNumber()))
144 stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
148 case DebugMacroEntry::START_FILE:
149 line = entry.GetLineNumber();
150 if (state.IsValidEntry(line))
151 state.StartFile(entry.GetFileSpec(comp_unit));
155 case DebugMacroEntry::END_FILE:
158 case DebugMacroEntry::INDIRECT:
159 AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
162 // This is an unknown/invalid entry. Ignore.
168 static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
169 StreamString &stream) {
170 for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
171 lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
173 ConstString var_name = var_sp->GetName();
174 if (!var_name || var_name == ConstString("this") ||
175 var_name == ConstString(".block_descriptor"))
178 stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
182 bool ExpressionSourceCode::GetText(std::string &text,
183 lldb::LanguageType wrapping_language,
185 ExecutionContext &exe_ctx) const {
186 const char *target_specific_defines = "typedef signed char BOOL;\n";
187 std::string module_macros;
189 Target *target = exe_ctx.GetTargetPtr();
191 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) {
192 target_specific_defines = "typedef bool BOOL;\n";
194 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
195 if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
196 static ConstString g_platform_ios_simulator("ios-simulator");
197 if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
198 target_specific_defines = "typedef bool BOOL;\n";
203 if (ClangModulesDeclVendor *decl_vendor =
204 target->GetClangModulesDeclVendor()) {
205 ClangPersistentVariables *persistent_vars =
206 llvm::cast<ClangPersistentVariables>(
207 target->GetPersistentExpressionStateForLanguage(
208 lldb::eLanguageTypeC));
209 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
210 persistent_vars->GetHandLoadedClangModules();
211 ClangModulesDeclVendor::ModuleVector modules_for_macros;
213 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
214 modules_for_macros.push_back(module);
217 if (target->GetEnableAutoImportClangModules()) {
218 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
219 if (Block *block = frame->GetFrameBlock()) {
222 block->CalculateSymbolContext(&sc);
225 StreamString error_stream;
227 decl_vendor->AddModulesForCompileUnit(
228 *sc.comp_unit, modules_for_macros, error_stream);
234 decl_vendor->ForEachMacro(
236 [&module_macros](const std::string &expansion) -> bool {
237 module_macros.append(expansion);
238 module_macros.append("\n");
244 StreamString debug_macros_stream;
245 StreamString lldb_local_var_decls;
246 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
247 const SymbolContext &sc = frame->GetSymbolContext(
248 lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
250 if (sc.comp_unit && sc.line_entry.IsValid()) {
251 DebugMacros *dm = sc.comp_unit->GetDebugMacros();
253 AddMacroState state(sc.line_entry.file, sc.line_entry.line);
254 AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
258 ConstString object_name;
259 if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) {
260 if (target->GetInjectLocalVariables(&exe_ctx)) {
261 lldb::VariableListSP var_list_sp =
262 frame->GetInScopeVariableList(false, true);
263 AddLocalVariableDecls(var_list_sp, lldb_local_var_decls);
269 switch (wrapping_language) {
272 case lldb::eLanguageTypeC:
273 case lldb::eLanguageTypeC_plus_plus:
274 case lldb::eLanguageTypeObjC:
278 StreamString wrap_stream;
280 wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
281 debug_macros_stream.GetData(), g_expression_prefix,
282 target_specific_defines, m_prefix.c_str());
284 // First construct a tagged form of the user expression so we can find it
286 std::string tagged_body;
287 switch (wrapping_language) {
289 tagged_body = m_body;
291 case lldb::eLanguageTypeC:
292 case lldb::eLanguageTypeC_plus_plus:
293 case lldb::eLanguageTypeObjC:
294 tagged_body.append(c_start_marker);
295 tagged_body.append(m_body);
296 tagged_body.append(c_end_marker);
299 switch (wrapping_language) {
302 case lldb::eLanguageTypeC:
303 wrap_stream.Printf("void \n"
304 "%s(void *$__lldb_arg) \n"
309 m_name.c_str(), lldb_local_var_decls.GetData(),
310 tagged_body.c_str());
312 case lldb::eLanguageTypeC_plus_plus:
313 wrap_stream.Printf("void \n"
314 "$__lldb_class::%s(void *$__lldb_arg) \n"
319 m_name.c_str(), lldb_local_var_decls.GetData(),
320 tagged_body.c_str());
322 case lldb::eLanguageTypeObjC:
325 "@interface $__lldb_objc_class ($__lldb_category) \n"
326 "+(void)%s:(void *)$__lldb_arg; \n"
328 "@implementation $__lldb_objc_class ($__lldb_category) \n"
329 "+(void)%s:(void *)$__lldb_arg \n"
334 m_name.c_str(), m_name.c_str(), tagged_body.c_str());
337 "@interface $__lldb_objc_class ($__lldb_category) \n"
338 "-(void)%s:(void *)$__lldb_arg; \n"
340 "@implementation $__lldb_objc_class ($__lldb_category) \n"
341 "-(void)%s:(void *)$__lldb_arg \n"
346 m_name.c_str(), m_name.c_str(), tagged_body.c_str());
351 text = wrap_stream.GetString();
359 bool ExpressionSourceCode::GetOriginalBodyBounds(
360 std::string transformed_text, lldb::LanguageType wrapping_language,
361 size_t &start_loc, size_t &end_loc) {
362 const char *start_marker;
363 const char *end_marker;
365 switch (wrapping_language) {
368 case lldb::eLanguageTypeC:
369 case lldb::eLanguageTypeC_plus_plus:
370 case lldb::eLanguageTypeObjC:
371 start_marker = c_start_marker;
372 end_marker = c_end_marker;
376 start_loc = transformed_text.find(start_marker);
377 if (start_loc == std::string::npos)
379 start_loc += strlen(start_marker);
380 end_loc = transformed_text.find(end_marker);
381 if (end_loc == std::string::npos)