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 "lldb/Core/StreamString.h"
13 #include "lldb/Expression/ClangModulesDeclVendor.h"
14 #include "lldb/Expression/ClangPersistentVariables.h"
15 #include "lldb/Symbol/Block.h"
16 #include "lldb/Target/ExecutionContext.h"
17 #include "lldb/Target/Platform.h"
18 #include "lldb/Target/StackFrame.h"
19 #include "lldb/Target/Target.h"
21 using namespace lldb_private;
24 ExpressionSourceCode::g_expression_prefix = R"(
40 typedef __INT8_TYPE__ int8_t;
41 typedef __UINT8_TYPE__ uint8_t;
42 typedef __INT16_TYPE__ int16_t;
43 typedef __UINT16_TYPE__ uint16_t;
44 typedef __INT32_TYPE__ int32_t;
45 typedef __UINT32_TYPE__ uint32_t;
46 typedef __INT64_TYPE__ int64_t;
47 typedef __UINT64_TYPE__ uint64_t;
48 typedef __INTPTR_TYPE__ intptr_t;
49 typedef __UINTPTR_TYPE__ uintptr_t;
50 typedef __SIZE_TYPE__ size_t;
51 typedef __PTRDIFF_TYPE__ ptrdiff_t;
52 typedef unsigned short unichar;
55 int printf(const char * __restrict, ...);
60 bool ExpressionSourceCode::GetText (std::string &text, lldb::LanguageType wrapping_language, bool const_object, bool static_method, ExecutionContext &exe_ctx) const
62 const char *target_specific_defines = "typedef signed char BOOL;\n";
63 std::string module_macros;
65 if (Target *target = exe_ctx.GetTargetPtr())
67 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64)
69 target_specific_defines = "typedef bool BOOL;\n";
71 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64)
73 if (lldb::PlatformSP platform_sp = target->GetPlatform())
75 static ConstString g_platform_ios_simulator ("ios-simulator");
76 if (platform_sp->GetPluginName() == g_platform_ios_simulator)
78 target_specific_defines = "typedef bool BOOL;\n";
83 if (ClangModulesDeclVendor *decl_vendor = target->GetClangModulesDeclVendor())
85 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules = target->GetPersistentVariables().GetHandLoadedClangModules();
86 ClangModulesDeclVendor::ModuleVector modules_for_macros;
88 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules)
90 modules_for_macros.push_back(module);
93 if (target->GetEnableAutoImportClangModules())
95 if (StackFrame *frame = exe_ctx.GetFramePtr())
97 if (Block *block = frame->GetFrameBlock())
101 block->CalculateSymbolContext(&sc);
105 StreamString error_stream;
107 decl_vendor->AddModulesForCompileUnit(*sc.comp_unit, modules_for_macros, error_stream);
113 decl_vendor->ForEachMacro(modules_for_macros, [&module_macros] (const std::string &expansion) -> bool {
114 module_macros.append(expansion);
115 module_macros.append("\n");
124 switch (wrapping_language)
128 case lldb::eLanguageTypeC:
129 case lldb::eLanguageTypeC_plus_plus:
130 case lldb::eLanguageTypeObjC:
134 StreamString wrap_stream;
136 wrap_stream.Printf("%s\n%s\n%s\n%s\n",
137 module_macros.c_str(),
139 target_specific_defines,
142 switch (wrapping_language)
146 case lldb::eLanguageTypeC:
147 wrap_stream.Printf("void \n"
148 "%s(void *$__lldb_arg) \n"
155 case lldb::eLanguageTypeC_plus_plus:
156 wrap_stream.Printf("void \n"
157 "$__lldb_class::%s(void *$__lldb_arg) %s\n"
162 (const_object ? "const" : ""),
165 case lldb::eLanguageTypeObjC:
168 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
169 "+(void)%s:(void *)$__lldb_arg; \n"
171 "@implementation $__lldb_objc_class ($__lldb_category) \n"
172 "+(void)%s:(void *)$__lldb_arg \n"
183 wrap_stream.Printf("@interface $__lldb_objc_class ($__lldb_category) \n"
184 "-(void)%s:(void *)$__lldb_arg; \n"
186 "@implementation $__lldb_objc_class ($__lldb_category) \n"
187 "-(void)%s:(void *)$__lldb_arg \n"
199 text = wrap_stream.GetString();