180814287SRaphael Isemann //===-- ClangExpressionSourceCode.cpp -------------------------------------===//
2ea401ec7SJim Ingham //
3ea401ec7SJim Ingham // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ea401ec7SJim Ingham // See https://llvm.org/LICENSE.txt for license information.
5ea401ec7SJim Ingham // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ea401ec7SJim Ingham //
7ea401ec7SJim Ingham //===----------------------------------------------------------------------===//
8ea401ec7SJim Ingham
9ea401ec7SJim Ingham #include "ClangExpressionSourceCode.h"
10ea401ec7SJim Ingham
118184b252SMichael Buch #include "ClangExpressionUtil.h"
128184b252SMichael Buch
1371569d0dSRaphael Isemann #include "clang/Basic/CharInfo.h"
14e08464fbSReid Kleckner #include "clang/Basic/FileManager.h"
1501718666SRaphael Isemann #include "clang/Basic/SourceManager.h"
1601718666SRaphael Isemann #include "clang/Lex/Lexer.h"
179f3a3e1fSRaphael Isemann #include "llvm/ADT/ScopeExit.h"
1871569d0dSRaphael Isemann #include "llvm/ADT/StringRef.h"
1971569d0dSRaphael Isemann
20ea401ec7SJim Ingham #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
21ea401ec7SJim Ingham #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
22ea401ec7SJim Ingham #include "lldb/Symbol/Block.h"
23ea401ec7SJim Ingham #include "lldb/Symbol/CompileUnit.h"
24ea401ec7SJim Ingham #include "lldb/Symbol/DebugMacros.h"
25ea401ec7SJim Ingham #include "lldb/Symbol/TypeSystem.h"
26ea401ec7SJim Ingham #include "lldb/Symbol/VariableList.h"
27ea401ec7SJim Ingham #include "lldb/Target/ExecutionContext.h"
28ea401ec7SJim Ingham #include "lldb/Target/Language.h"
29ea401ec7SJim Ingham #include "lldb/Target/Platform.h"
30ea401ec7SJim Ingham #include "lldb/Target/StackFrame.h"
31ea401ec7SJim Ingham #include "lldb/Target/Target.h"
32ea401ec7SJim Ingham #include "lldb/Utility/StreamString.h"
338184b252SMichael Buch #include "lldb/lldb-forward.h"
34ea401ec7SJim Ingham
35ea401ec7SJim Ingham using namespace lldb_private;
36ea401ec7SJim Ingham
37bd2a910aSRaphael Isemann #define PREFIX_NAME "<lldb wrapper prefix>"
3838dfb235SJonas Devlieghere #define SUFFIX_NAME "<lldb wrapper suffix>"
39bd2a910aSRaphael Isemann
40bd2a910aSRaphael Isemann const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;
41bd2a910aSRaphael Isemann
421442efeaSRaphael Isemann const char *ClangExpressionSourceCode::g_expression_prefix =
43bd2a910aSRaphael Isemann "#line 1 \"" PREFIX_NAME R"("
44e21fc877SRaphael Isemann #ifndef offsetof
45e21fc877SRaphael Isemann #define offsetof(t, d) __builtin_offsetof(t, d)
46e21fc877SRaphael Isemann #endif
47ea401ec7SJim Ingham #ifndef NULL
48ea401ec7SJim Ingham #define NULL (__null)
49ea401ec7SJim Ingham #endif
50ea401ec7SJim Ingham #ifndef Nil
51ea401ec7SJim Ingham #define Nil (__null)
52ea401ec7SJim Ingham #endif
53ea401ec7SJim Ingham #ifndef nil
54ea401ec7SJim Ingham #define nil (__null)
55ea401ec7SJim Ingham #endif
56ea401ec7SJim Ingham #ifndef YES
57ea401ec7SJim Ingham #define YES ((BOOL)1)
58ea401ec7SJim Ingham #endif
59ea401ec7SJim Ingham #ifndef NO
60ea401ec7SJim Ingham #define NO ((BOOL)0)
61ea401ec7SJim Ingham #endif
62ea401ec7SJim Ingham typedef __INT8_TYPE__ int8_t;
63ea401ec7SJim Ingham typedef __UINT8_TYPE__ uint8_t;
64ea401ec7SJim Ingham typedef __INT16_TYPE__ int16_t;
65ea401ec7SJim Ingham typedef __UINT16_TYPE__ uint16_t;
66ea401ec7SJim Ingham typedef __INT32_TYPE__ int32_t;
67ea401ec7SJim Ingham typedef __UINT32_TYPE__ uint32_t;
68ea401ec7SJim Ingham typedef __INT64_TYPE__ int64_t;
69ea401ec7SJim Ingham typedef __UINT64_TYPE__ uint64_t;
70ea401ec7SJim Ingham typedef __INTPTR_TYPE__ intptr_t;
71ea401ec7SJim Ingham typedef __UINTPTR_TYPE__ uintptr_t;
72ea401ec7SJim Ingham typedef __SIZE_TYPE__ size_t;
73ea401ec7SJim Ingham typedef __PTRDIFF_TYPE__ ptrdiff_t;
74ea401ec7SJim Ingham typedef unsigned short unichar;
75ea401ec7SJim Ingham extern "C"
76ea401ec7SJim Ingham {
77ea401ec7SJim Ingham int printf(const char * __restrict, ...);
78ea401ec7SJim Ingham }
79ea401ec7SJim Ingham )";
80ea401ec7SJim Ingham
8138dfb235SJonas Devlieghere const char *ClangExpressionSourceCode::g_expression_suffix =
8238dfb235SJonas Devlieghere "\n;\n#line 1 \"" SUFFIX_NAME "\"\n";
8338dfb235SJonas Devlieghere
84ea401ec7SJim Ingham namespace {
85ea401ec7SJim Ingham
86ea401ec7SJim Ingham class AddMacroState {
87ea401ec7SJim Ingham enum State {
88ea401ec7SJim Ingham CURRENT_FILE_NOT_YET_PUSHED,
89ea401ec7SJim Ingham CURRENT_FILE_PUSHED,
90ea401ec7SJim Ingham CURRENT_FILE_POPPED
91ea401ec7SJim Ingham };
92ea401ec7SJim Ingham
93ea401ec7SJim Ingham public:
AddMacroState(const FileSpec & current_file,const uint32_t current_file_line)94ea401ec7SJim Ingham AddMacroState(const FileSpec ¤t_file, const uint32_t current_file_line)
9528c878aeSShafik Yaghmour : m_current_file(current_file), m_current_file_line(current_file_line) {}
96ea401ec7SJim Ingham
StartFile(const FileSpec & file)97ea401ec7SJim Ingham void StartFile(const FileSpec &file) {
98ea401ec7SJim Ingham m_file_stack.push_back(file);
99ea401ec7SJim Ingham if (file == m_current_file)
100ea401ec7SJim Ingham m_state = CURRENT_FILE_PUSHED;
101ea401ec7SJim Ingham }
102ea401ec7SJim Ingham
EndFile()103ea401ec7SJim Ingham void EndFile() {
104ea401ec7SJim Ingham if (m_file_stack.size() == 0)
105ea401ec7SJim Ingham return;
106ea401ec7SJim Ingham
107ea401ec7SJim Ingham FileSpec old_top = m_file_stack.back();
108ea401ec7SJim Ingham m_file_stack.pop_back();
109ea401ec7SJim Ingham if (old_top == m_current_file)
110ea401ec7SJim Ingham m_state = CURRENT_FILE_POPPED;
111ea401ec7SJim Ingham }
112ea401ec7SJim Ingham
113ea401ec7SJim Ingham // An entry is valid if it occurs before the current line in the current
114ea401ec7SJim Ingham // file.
IsValidEntry(uint32_t line)115ea401ec7SJim Ingham bool IsValidEntry(uint32_t line) {
116ea401ec7SJim Ingham switch (m_state) {
117ea401ec7SJim Ingham case CURRENT_FILE_NOT_YET_PUSHED:
118ea401ec7SJim Ingham return true;
119ea401ec7SJim Ingham case CURRENT_FILE_PUSHED:
120ea401ec7SJim Ingham // If we are in file included in the current file, the entry should be
121ea401ec7SJim Ingham // added.
122ea401ec7SJim Ingham if (m_file_stack.back() != m_current_file)
123ea401ec7SJim Ingham return true;
124ea401ec7SJim Ingham
125ea401ec7SJim Ingham return line < m_current_file_line;
126ea401ec7SJim Ingham default:
127ea401ec7SJim Ingham return false;
128ea401ec7SJim Ingham }
129ea401ec7SJim Ingham }
130ea401ec7SJim Ingham
131ea401ec7SJim Ingham private:
132ea401ec7SJim Ingham std::vector<FileSpec> m_file_stack;
13328c878aeSShafik Yaghmour State m_state = CURRENT_FILE_NOT_YET_PUSHED;
134ea401ec7SJim Ingham FileSpec m_current_file;
135ea401ec7SJim Ingham uint32_t m_current_file_line;
136ea401ec7SJim Ingham };
137ea401ec7SJim Ingham
138ea401ec7SJim Ingham } // anonymous namespace
139ea401ec7SJim Ingham
AddMacros(const DebugMacros * dm,CompileUnit * comp_unit,AddMacroState & state,StreamString & stream)140ea401ec7SJim Ingham static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
141ea401ec7SJim Ingham AddMacroState &state, StreamString &stream) {
142ea401ec7SJim Ingham if (dm == nullptr)
143ea401ec7SJim Ingham return;
144ea401ec7SJim Ingham
1459f3a3e1fSRaphael Isemann // The macros directives below can potentially redefine builtin macros of the
1469f3a3e1fSRaphael Isemann // Clang instance which parses the user expression. The Clang diagnostics
1479f3a3e1fSRaphael Isemann // caused by this are not useful for the user as the source code here is
1489f3a3e1fSRaphael Isemann // generated by LLDB.
1499f3a3e1fSRaphael Isemann stream << "#pragma clang diagnostic push\n";
1509f3a3e1fSRaphael Isemann stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";
1519f3a3e1fSRaphael Isemann stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";
1529f3a3e1fSRaphael Isemann auto pop_warning = llvm::make_scope_exit([&stream](){
1539f3a3e1fSRaphael Isemann stream << "#pragma clang diagnostic pop\n";
1549f3a3e1fSRaphael Isemann });
1559f3a3e1fSRaphael Isemann
156ea401ec7SJim Ingham for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
157ea401ec7SJim Ingham const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
158ea401ec7SJim Ingham uint32_t line;
159ea401ec7SJim Ingham
160ea401ec7SJim Ingham switch (entry.GetType()) {
161ea401ec7SJim Ingham case DebugMacroEntry::DEFINE:
162ea401ec7SJim Ingham if (state.IsValidEntry(entry.GetLineNumber()))
163ea401ec7SJim Ingham stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
164ea401ec7SJim Ingham else
165ea401ec7SJim Ingham return;
166ea401ec7SJim Ingham break;
167ea401ec7SJim Ingham case DebugMacroEntry::UNDEF:
168ea401ec7SJim Ingham if (state.IsValidEntry(entry.GetLineNumber()))
169ea401ec7SJim Ingham stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
170ea401ec7SJim Ingham else
171ea401ec7SJim Ingham return;
172ea401ec7SJim Ingham break;
173ea401ec7SJim Ingham case DebugMacroEntry::START_FILE:
174ea401ec7SJim Ingham line = entry.GetLineNumber();
175ea401ec7SJim Ingham if (state.IsValidEntry(line))
176ea401ec7SJim Ingham state.StartFile(entry.GetFileSpec(comp_unit));
177ea401ec7SJim Ingham else
178ea401ec7SJim Ingham return;
179ea401ec7SJim Ingham break;
180ea401ec7SJim Ingham case DebugMacroEntry::END_FILE:
181ea401ec7SJim Ingham state.EndFile();
182ea401ec7SJim Ingham break;
183ea401ec7SJim Ingham case DebugMacroEntry::INDIRECT:
184ea401ec7SJim Ingham AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
185ea401ec7SJim Ingham break;
186ea401ec7SJim Ingham default:
187ea401ec7SJim Ingham // This is an unknown/invalid entry. Ignore.
188ea401ec7SJim Ingham break;
189ea401ec7SJim Ingham }
190ea401ec7SJim Ingham }
191ea401ec7SJim Ingham }
192ea401ec7SJim Ingham
ClangExpressionSourceCode(llvm::StringRef filename,llvm::StringRef name,llvm::StringRef prefix,llvm::StringRef body,Wrapping wrap,WrapKind wrap_kind)1931442efeaSRaphael Isemann lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(
1941442efeaSRaphael Isemann llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
1952b37c5b5SRaphael Isemann llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
1962b37c5b5SRaphael Isemann : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
1971442efeaSRaphael Isemann // Use #line markers to pretend that we have a single-line source file
1981442efeaSRaphael Isemann // containing only the user expression. This will hide our wrapper code
1991442efeaSRaphael Isemann // from the user when we render diagnostics with Clang.
2001442efeaSRaphael Isemann m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
20138dfb235SJonas Devlieghere m_end_marker = g_expression_suffix;
2021442efeaSRaphael Isemann }
2031442efeaSRaphael Isemann
20401718666SRaphael Isemann namespace {
20501718666SRaphael Isemann /// Allows checking if a token is contained in a given expression.
20601718666SRaphael Isemann class TokenVerifier {
20701718666SRaphael Isemann /// The tokens we found in the expression.
20801718666SRaphael Isemann llvm::StringSet<> m_tokens;
20971569d0dSRaphael Isemann
21001718666SRaphael Isemann public:
21101718666SRaphael Isemann TokenVerifier(std::string body);
21201718666SRaphael Isemann /// Returns true iff the given expression body contained a token with the
21301718666SRaphael Isemann /// given content.
hasToken(llvm::StringRef token) const21401718666SRaphael Isemann bool hasToken(llvm::StringRef token) const {
21565a2d6d6SKazu Hirata return m_tokens.contains(token);
21601718666SRaphael Isemann }
21701718666SRaphael Isemann };
2188184b252SMichael Buch
2198184b252SMichael Buch // If we're evaluating from inside a lambda that captures a 'this' pointer,
2208184b252SMichael Buch // add a "using" declaration to 'stream' for each capture used in the
2218184b252SMichael Buch // expression (tokenized by 'verifier').
2228184b252SMichael Buch //
2238184b252SMichael Buch // If no 'this' capture exists, generate no using declarations. Instead
2248184b252SMichael Buch // capture lookups will get resolved by the same mechanism as class member
2258184b252SMichael Buch // variable lookup. That's because Clang generates an unnamed structure
2268184b252SMichael Buch // representing the lambda closure whose members are the captured variables.
AddLambdaCaptureDecls(StreamString & stream,StackFrame * frame,TokenVerifier const & verifier)2278184b252SMichael Buch void AddLambdaCaptureDecls(StreamString &stream, StackFrame *frame,
2288184b252SMichael Buch TokenVerifier const &verifier) {
2298184b252SMichael Buch assert(frame);
2308184b252SMichael Buch
2318184b252SMichael Buch if (auto thisValSP = ClangExpressionUtil::GetLambdaValueObject(frame)) {
232624ea68cSAdrian Prantl uint32_t numChildren = thisValSP->GetNumChildrenIgnoringErrors();
2338184b252SMichael Buch for (uint32_t i = 0; i < numChildren; ++i) {
234a1a74f7cSDave Lee auto childVal = thisValSP->GetChildAtIndex(i);
2358184b252SMichael Buch ConstString childName(childVal ? childVal->GetName() : ConstString(""));
2368184b252SMichael Buch
2378184b252SMichael Buch if (!childName.IsEmpty() && verifier.hasToken(childName.GetStringRef()) &&
2388184b252SMichael Buch childName != "this") {
2398184b252SMichael Buch stream.Printf("using $__lldb_local_vars::%s;\n",
2408184b252SMichael Buch childName.GetCString());
2418184b252SMichael Buch }
2428184b252SMichael Buch }
2438184b252SMichael Buch }
2448184b252SMichael Buch }
2458184b252SMichael Buch
24601718666SRaphael Isemann } // namespace
24771569d0dSRaphael Isemann
TokenVerifier(std::string body)24801718666SRaphael Isemann TokenVerifier::TokenVerifier(std::string body) {
24901718666SRaphael Isemann using namespace clang;
25001718666SRaphael Isemann
25101718666SRaphael Isemann // We only care about tokens and not their original source locations. If we
25201718666SRaphael Isemann // move the whole expression to only be in one line we can simplify the
25301718666SRaphael Isemann // following code that extracts the token contents.
25401718666SRaphael Isemann std::replace(body.begin(), body.end(), '\n', ' ');
25501718666SRaphael Isemann std::replace(body.begin(), body.end(), '\r', ' ');
25601718666SRaphael Isemann
25701718666SRaphael Isemann FileSystemOptions file_opts;
25801718666SRaphael Isemann FileManager file_mgr(file_opts,
25901718666SRaphael Isemann FileSystem::Instance().GetVirtualFileSystem());
26001718666SRaphael Isemann
26101718666SRaphael Isemann // Let's build the actual source code Clang needs and setup some utility
26201718666SRaphael Isemann // objects.
26301718666SRaphael Isemann llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
26401718666SRaphael Isemann llvm::IntrusiveRefCntPtr<DiagnosticOptions> diags_opts(
26501718666SRaphael Isemann new DiagnosticOptions());
26601718666SRaphael Isemann DiagnosticsEngine diags(diag_ids, diags_opts);
26701718666SRaphael Isemann clang::SourceManager SM(diags, file_mgr);
26801718666SRaphael Isemann auto buf = llvm::MemoryBuffer::getMemBuffer(body);
26901718666SRaphael Isemann
270b333d6e1SDuncan P. N. Exon Smith FileID FID = SM.createFileID(buf->getMemBufferRef());
27101718666SRaphael Isemann
27201718666SRaphael Isemann // Let's just enable the latest ObjC and C++ which should get most tokens
27301718666SRaphael Isemann // right.
27401718666SRaphael Isemann LangOptions Opts;
27501718666SRaphael Isemann Opts.ObjC = true;
27601718666SRaphael Isemann Opts.DollarIdents = true;
27789cd0e8cSMichael Buch Opts.CPlusPlus20 = true;
27801718666SRaphael Isemann Opts.LineComment = true;
27901718666SRaphael Isemann
280f96e16bcSDuncan P. N. Exon Smith Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
28101718666SRaphael Isemann
28201718666SRaphael Isemann Token token;
28301718666SRaphael Isemann bool exit = false;
28401718666SRaphael Isemann while (!exit) {
28501718666SRaphael Isemann // Returns true if this is the last token we get from the lexer.
28601718666SRaphael Isemann exit = lex.LexFromRawLexer(token);
28701718666SRaphael Isemann
28801718666SRaphael Isemann // Extract the column number which we need to extract the token content.
28901718666SRaphael Isemann // Our expression is just one line, so we don't need to handle any line
29001718666SRaphael Isemann // numbers here.
29101718666SRaphael Isemann bool invalid = false;
29201718666SRaphael Isemann unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
29301718666SRaphael Isemann if (invalid)
29471569d0dSRaphael Isemann continue;
29501718666SRaphael Isemann // Column numbers start at 1, but indexes in our string start at 0.
29601718666SRaphael Isemann --start;
29701718666SRaphael Isemann
29801718666SRaphael Isemann // Annotations don't have a length, so let's skip them.
29901718666SRaphael Isemann if (token.isAnnotation())
30001718666SRaphael Isemann continue;
30101718666SRaphael Isemann
30201718666SRaphael Isemann // Extract the token string from our source code and store it.
30301718666SRaphael Isemann std::string token_str = body.substr(start, token.getLength());
30401718666SRaphael Isemann if (token_str.empty())
30501718666SRaphael Isemann continue;
30601718666SRaphael Isemann m_tokens.insert(token_str);
30771569d0dSRaphael Isemann }
30871569d0dSRaphael Isemann }
30971569d0dSRaphael Isemann
AddLocalVariableDecls(StreamString & stream,const std::string & expr,StackFrame * frame) const3108184b252SMichael Buch void ClangExpressionSourceCode::AddLocalVariableDecls(StreamString &stream,
3118184b252SMichael Buch const std::string &expr,
3128184b252SMichael Buch StackFrame *frame) const {
3138184b252SMichael Buch assert(frame);
31401718666SRaphael Isemann TokenVerifier tokens(expr);
31501718666SRaphael Isemann
3168184b252SMichael Buch lldb::VariableListSP var_list_sp = frame->GetInScopeVariableList(false, true);
3178184b252SMichael Buch
318ea401ec7SJim Ingham for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
319ea401ec7SJim Ingham lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
320ea401ec7SJim Ingham
321ea401ec7SJim Ingham ConstString var_name = var_sp->GetName();
322e5cbe782SShafik Yaghmour
3238184b252SMichael Buch if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction) {
3248184b252SMichael Buch AddLambdaCaptureDecls(stream, frame, tokens);
3258184b252SMichael Buch
3268184b252SMichael Buch continue;
3278184b252SMichael Buch }
328e5cbe782SShafik Yaghmour
329e5cbe782SShafik Yaghmour // We can check for .block_descriptor w/o checking for langauge since this
330e5cbe782SShafik Yaghmour // is not a valid identifier in either C or C++.
331090a5b29SRaphael Isemann if (!var_name || var_name == ".block_descriptor")
332ea401ec7SJim Ingham continue;
333ea401ec7SJim Ingham
33401718666SRaphael Isemann if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
33571569d0dSRaphael Isemann continue;
33671569d0dSRaphael Isemann
3372b37c5b5SRaphael Isemann const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
3382b37c5b5SRaphael Isemann m_wrap_kind == WrapKind::ObjCStaticMethod;
3392b37c5b5SRaphael Isemann if ((var_name == "self" || var_name == "_cmd") && is_objc)
340e5cbe782SShafik Yaghmour continue;
341e5cbe782SShafik Yaghmour
342ea401ec7SJim Ingham stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
343ea401ec7SJim Ingham }
344ea401ec7SJim Ingham }
345ea401ec7SJim Ingham
GetText(std::string & text,ExecutionContext & exe_ctx,bool add_locals,bool force_add_all_locals,llvm::ArrayRef<std::string> modules) const34671569d0dSRaphael Isemann bool ClangExpressionSourceCode::GetText(
3472b37c5b5SRaphael Isemann std::string &text, ExecutionContext &exe_ctx, bool add_locals,
3482b37c5b5SRaphael Isemann bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {
349ea401ec7SJim Ingham const char *target_specific_defines = "typedef signed char BOOL;\n";
350ea401ec7SJim Ingham std::string module_macros;
351a8350ce7SRaphael Isemann llvm::raw_string_ostream module_macros_stream(module_macros);
352ea401ec7SJim Ingham
353ea401ec7SJim Ingham Target *target = exe_ctx.GetTargetPtr();
354ea401ec7SJim Ingham if (target) {
3557dd7a360SJason Molenda if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
3567dd7a360SJason Molenda target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
357ea401ec7SJim Ingham target_specific_defines = "typedef bool BOOL;\n";
358ea401ec7SJim Ingham }
359ea401ec7SJim Ingham if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
360ea401ec7SJim Ingham if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
361a3939e15SPavel Labath if (platform_sp->GetPluginName() == "ios-simulator") {
362ea401ec7SJim Ingham target_specific_defines = "typedef bool BOOL;\n";
363ea401ec7SJim Ingham }
364ea401ec7SJim Ingham }
365ea401ec7SJim Ingham }
366ea401ec7SJim Ingham
367e9331a56SAdrian Prantl auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
368e9331a56SAdrian Prantl target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
3694c0b0de9SAlex Langford std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
3704c0b0de9SAlex Langford persistent_vars->GetClangModulesDeclVendor();
3714c0b0de9SAlex Langford if (decl_vendor) {
372ea401ec7SJim Ingham const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
373ea401ec7SJim Ingham persistent_vars->GetHandLoadedClangModules();
374ea401ec7SJim Ingham ClangModulesDeclVendor::ModuleVector modules_for_macros;
375ea401ec7SJim Ingham
376ea401ec7SJim Ingham for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
377ea401ec7SJim Ingham modules_for_macros.push_back(module);
378ea401ec7SJim Ingham }
379ea401ec7SJim Ingham
380ea401ec7SJim Ingham if (target->GetEnableAutoImportClangModules()) {
381ea401ec7SJim Ingham if (StackFrame *frame = exe_ctx.GetFramePtr()) {
382ea401ec7SJim Ingham if (Block *block = frame->GetFrameBlock()) {
383ea401ec7SJim Ingham SymbolContext sc;
384ea401ec7SJim Ingham
385ea401ec7SJim Ingham block->CalculateSymbolContext(&sc);
386ea401ec7SJim Ingham
387ea401ec7SJim Ingham if (sc.comp_unit) {
388ea401ec7SJim Ingham StreamString error_stream;
389ea401ec7SJim Ingham
390ea401ec7SJim Ingham decl_vendor->AddModulesForCompileUnit(
391ea401ec7SJim Ingham *sc.comp_unit, modules_for_macros, error_stream);
392ea401ec7SJim Ingham }
393ea401ec7SJim Ingham }
394ea401ec7SJim Ingham }
395ea401ec7SJim Ingham }
396ea401ec7SJim Ingham
397ea401ec7SJim Ingham decl_vendor->ForEachMacro(
398ea401ec7SJim Ingham modules_for_macros,
399a8350ce7SRaphael Isemann [&module_macros_stream](llvm::StringRef token,
400a8350ce7SRaphael Isemann llvm::StringRef expansion) -> bool {
401a8350ce7SRaphael Isemann // Check if the macro hasn't already been defined in the
402a8350ce7SRaphael Isemann // g_expression_prefix (which defines a few builtin macros).
403a8350ce7SRaphael Isemann module_macros_stream << "#ifndef " << token << "\n";
404a8350ce7SRaphael Isemann module_macros_stream << expansion << "\n";
405a8350ce7SRaphael Isemann module_macros_stream << "#endif\n";
406ea401ec7SJim Ingham return false;
407ea401ec7SJim Ingham });
408ea401ec7SJim Ingham }
409ea401ec7SJim Ingham }
410ea401ec7SJim Ingham
411ea401ec7SJim Ingham StreamString debug_macros_stream;
412ea401ec7SJim Ingham StreamString lldb_local_var_decls;
413ea401ec7SJim Ingham if (StackFrame *frame = exe_ctx.GetFramePtr()) {
414ea401ec7SJim Ingham const SymbolContext &sc = frame->GetSymbolContext(
415ea401ec7SJim Ingham lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
416ea401ec7SJim Ingham
417ea401ec7SJim Ingham if (sc.comp_unit && sc.line_entry.IsValid()) {
418ea401ec7SJim Ingham DebugMacros *dm = sc.comp_unit->GetDebugMacros();
419ea401ec7SJim Ingham if (dm) {
420*556fe5f2SJonas Devlieghere AddMacroState state(sc.line_entry.GetFile(), sc.line_entry.line);
421ea401ec7SJim Ingham AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
422ea401ec7SJim Ingham }
423ea401ec7SJim Ingham }
424ea401ec7SJim Ingham
425e5cbe782SShafik Yaghmour if (add_locals)
426ea401ec7SJim Ingham if (target->GetInjectLocalVariables(&exe_ctx)) {
4278184b252SMichael Buch AddLocalVariableDecls(lldb_local_var_decls,
4288184b252SMichael Buch force_add_all_locals ? "" : m_body, frame);
429ea401ec7SJim Ingham }
430ea401ec7SJim Ingham }
431ea401ec7SJim Ingham
432ea401ec7SJim Ingham if (m_wrap) {
4336c0bbfc0SRaphael Isemann // Generate a list of @import statements that will import the specified
4346c0bbfc0SRaphael Isemann // module into our expression.
4356c0bbfc0SRaphael Isemann std::string module_imports;
4366c0bbfc0SRaphael Isemann for (const std::string &module : modules) {
4376c0bbfc0SRaphael Isemann module_imports.append("@import ");
4386c0bbfc0SRaphael Isemann module_imports.append(module);
4396c0bbfc0SRaphael Isemann module_imports.append(";\n");
4406c0bbfc0SRaphael Isemann }
4416c0bbfc0SRaphael Isemann
442ea401ec7SJim Ingham StreamString wrap_stream;
443ea401ec7SJim Ingham
444a8350ce7SRaphael Isemann wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
445a8350ce7SRaphael Isemann module_macros.c_str(), debug_macros_stream.GetData(),
446ea401ec7SJim Ingham target_specific_defines, m_prefix.c_str());
447ea401ec7SJim Ingham
448ea401ec7SJim Ingham // First construct a tagged form of the user expression so we can find it
449ea401ec7SJim Ingham // later:
450ea401ec7SJim Ingham std::string tagged_body;
4511442efeaSRaphael Isemann tagged_body.append(m_start_marker);
452ea401ec7SJim Ingham tagged_body.append(m_body);
4531442efeaSRaphael Isemann tagged_body.append(m_end_marker);
4542b37c5b5SRaphael Isemann
4552b37c5b5SRaphael Isemann switch (m_wrap_kind) {
4562b37c5b5SRaphael Isemann case WrapKind::Function:
4576c0bbfc0SRaphael Isemann wrap_stream.Printf("%s"
4586c0bbfc0SRaphael Isemann "void \n"
459ea401ec7SJim Ingham "%s(void *$__lldb_arg) \n"
460ea401ec7SJim Ingham "{ \n"
461ea401ec7SJim Ingham " %s; \n"
462ea401ec7SJim Ingham "%s"
463ea401ec7SJim Ingham "} \n",
4646c0bbfc0SRaphael Isemann module_imports.c_str(), m_name.c_str(),
4656c0bbfc0SRaphael Isemann lldb_local_var_decls.GetData(), tagged_body.c_str());
466ea401ec7SJim Ingham break;
4672b37c5b5SRaphael Isemann case WrapKind::CppMemberFunction:
4686c0bbfc0SRaphael Isemann wrap_stream.Printf("%s"
4696c0bbfc0SRaphael Isemann "void \n"
470ea401ec7SJim Ingham "$__lldb_class::%s(void *$__lldb_arg) \n"
471ea401ec7SJim Ingham "{ \n"
472ea401ec7SJim Ingham " %s; \n"
473ea401ec7SJim Ingham "%s"
474ea401ec7SJim Ingham "} \n",
4756c0bbfc0SRaphael Isemann module_imports.c_str(), m_name.c_str(),
4766c0bbfc0SRaphael Isemann lldb_local_var_decls.GetData(), tagged_body.c_str());
477ea401ec7SJim Ingham break;
4782b37c5b5SRaphael Isemann case WrapKind::ObjCInstanceMethod:
479ea401ec7SJim Ingham wrap_stream.Printf(
4806c0bbfc0SRaphael Isemann "%s"
481ea401ec7SJim Ingham "@interface $__lldb_objc_class ($__lldb_category) \n"
482ea401ec7SJim Ingham "-(void)%s:(void *)$__lldb_arg; \n"
483ea401ec7SJim Ingham "@end \n"
484ea401ec7SJim Ingham "@implementation $__lldb_objc_class ($__lldb_category) \n"
485ea401ec7SJim Ingham "-(void)%s:(void *)$__lldb_arg \n"
486ea401ec7SJim Ingham "{ \n"
487e5cbe782SShafik Yaghmour " %s; \n"
488ea401ec7SJim Ingham "%s"
489ea401ec7SJim Ingham "} \n"
490ea401ec7SJim Ingham "@end \n",
4916c0bbfc0SRaphael Isemann module_imports.c_str(), m_name.c_str(), m_name.c_str(),
492e5cbe782SShafik Yaghmour lldb_local_var_decls.GetData(), tagged_body.c_str());
4932b37c5b5SRaphael Isemann break;
4942b37c5b5SRaphael Isemann
4952b37c5b5SRaphael Isemann case WrapKind::ObjCStaticMethod:
4962b37c5b5SRaphael Isemann wrap_stream.Printf(
4972b37c5b5SRaphael Isemann "%s"
4982b37c5b5SRaphael Isemann "@interface $__lldb_objc_class ($__lldb_category) \n"
4992b37c5b5SRaphael Isemann "+(void)%s:(void *)$__lldb_arg; \n"
5002b37c5b5SRaphael Isemann "@end \n"
5012b37c5b5SRaphael Isemann "@implementation $__lldb_objc_class ($__lldb_category) \n"
5022b37c5b5SRaphael Isemann "+(void)%s:(void *)$__lldb_arg \n"
5032b37c5b5SRaphael Isemann "{ \n"
5042b37c5b5SRaphael Isemann " %s; \n"
5052b37c5b5SRaphael Isemann "%s"
5062b37c5b5SRaphael Isemann "} \n"
5072b37c5b5SRaphael Isemann "@end \n",
5082b37c5b5SRaphael Isemann module_imports.c_str(), m_name.c_str(), m_name.c_str(),
5092b37c5b5SRaphael Isemann lldb_local_var_decls.GetData(), tagged_body.c_str());
510ea401ec7SJim Ingham break;
511ea401ec7SJim Ingham }
512ea401ec7SJim Ingham
513adcd0268SBenjamin Kramer text = std::string(wrap_stream.GetString());
514ea401ec7SJim Ingham } else {
515ea401ec7SJim Ingham text.append(m_body);
516ea401ec7SJim Ingham }
517ea401ec7SJim Ingham
518ea401ec7SJim Ingham return true;
519ea401ec7SJim Ingham }
520ea401ec7SJim Ingham
GetOriginalBodyBounds(std::string transformed_text,size_t & start_loc,size_t & end_loc)521ea401ec7SJim Ingham bool ClangExpressionSourceCode::GetOriginalBodyBounds(
5222b37c5b5SRaphael Isemann std::string transformed_text, size_t &start_loc, size_t &end_loc) {
5231442efeaSRaphael Isemann start_loc = transformed_text.find(m_start_marker);
524ea401ec7SJim Ingham if (start_loc == std::string::npos)
525ea401ec7SJim Ingham return false;
5261442efeaSRaphael Isemann start_loc += m_start_marker.size();
5271442efeaSRaphael Isemann end_loc = transformed_text.find(m_end_marker);
528ea401ec7SJim Ingham return end_loc != std::string::npos;
529ea401ec7SJim Ingham }
530