blob: 0bc664c93f838c45a3b7e51c513f4d3a1f23da6a [file] [log] [blame]
Jim Inghamea401ec2019-03-06 22:43:25 +00001//===-- ClangExpressionSourceCode.cpp ---------------------------*- C++ -*-===//
Sean Callanan9bc83842011-09-26 18:45:31 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan9bc83842011-09-26 18:45:31 +00006//
7//===----------------------------------------------------------------------===//
8
Jim Inghamea401ec2019-03-06 22:43:25 +00009#include "ClangExpressionSourceCode.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000010
Raphael Isemann71569d02019-05-02 10:12:56 +000011#include "clang/Basic/CharInfo.h"
12#include "llvm/ADT/StringRef.h"
13
Sean Callanan4dbb2712015-09-25 20:35:58 +000014#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
15#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000016#include "lldb/Symbol/Block.h"
Siva Chandrad8335e92015-12-16 00:22:08 +000017#include "lldb/Symbol/CompileUnit.h"
18#include "lldb/Symbol/DebugMacros.h"
Sean Callanan8f1f9a12015-09-30 19:57:57 +000019#include "lldb/Symbol/TypeSystem.h"
Siva Chandra03ff5c82016-02-05 19:10:04 +000020#include "lldb/Symbol/VariableList.h"
Jason Molendaa3329782014-03-29 18:54:20 +000021#include "lldb/Target/ExecutionContext.h"
Siva Chandra03ff5c82016-02-05 19:10:04 +000022#include "lldb/Target/Language.h"
Jason Molendaa3329782014-03-29 18:54:20 +000023#include "lldb/Target/Platform.h"
Sean Callanan68f85e72015-04-30 21:49:58 +000024#include "lldb/Target/StackFrame.h"
Jason Molendaa3329782014-03-29 18:54:20 +000025#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000026#include "lldb/Utility/StreamString.h"
Sean Callanan9bc83842011-09-26 18:45:31 +000027
28using namespace lldb_private;
29
Jim Inghamea401ec2019-03-06 22:43:25 +000030const char *ClangExpressionSourceCode::g_expression_prefix = R"(
Sean Callanan68f85e72015-04-30 21:49:58 +000031#ifndef NULL
Greg Clayton399107a2013-02-13 23:57:48 +000032#define NULL (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000033#endif
34#ifndef Nil
Greg Clayton399107a2013-02-13 23:57:48 +000035#define Nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000036#endif
37#ifndef nil
Greg Clayton399107a2013-02-13 23:57:48 +000038#define nil (__null)
Sean Callanan68f85e72015-04-30 21:49:58 +000039#endif
40#ifndef YES
Greg Clayton399107a2013-02-13 23:57:48 +000041#define YES ((BOOL)1)
Sean Callanan68f85e72015-04-30 21:49:58 +000042#endif
43#ifndef NO
Greg Clayton399107a2013-02-13 23:57:48 +000044#define NO ((BOOL)0)
Sean Callanan68f85e72015-04-30 21:49:58 +000045#endif
Reid Kleckner56d861f2014-07-29 21:59:33 +000046typedef __INT8_TYPE__ int8_t;
47typedef __UINT8_TYPE__ uint8_t;
48typedef __INT16_TYPE__ int16_t;
49typedef __UINT16_TYPE__ uint16_t;
50typedef __INT32_TYPE__ int32_t;
51typedef __UINT32_TYPE__ uint32_t;
52typedef __INT64_TYPE__ int64_t;
53typedef __UINT64_TYPE__ uint64_t;
54typedef __INTPTR_TYPE__ intptr_t;
55typedef __UINTPTR_TYPE__ uintptr_t;
Greg Clayton399107a2013-02-13 23:57:48 +000056typedef __SIZE_TYPE__ size_t;
57typedef __PTRDIFF_TYPE__ ptrdiff_t;
58typedef unsigned short unichar;
Kate Stonecc391a02015-01-21 23:39:16 +000059extern "C"
60{
61 int printf(const char * __restrict, ...);
62}
Greg Clayton399107a2013-02-13 23:57:48 +000063)";
Sean Callanana199e822012-09-13 23:35:30 +000064
Jim Inghame5ee6f02016-03-29 22:00:08 +000065static const char *c_start_marker = " /*LLDB_BODY_START*/\n ";
Kate Stoneb9c1b512016-09-06 20:57:50 +000066static const char *c_end_marker = ";\n /*LLDB_BODY_END*/\n";
Jim Inghame5ee6f02016-03-29 22:00:08 +000067
Siva Chandrad8335e92015-12-16 00:22:08 +000068namespace {
69
Kate Stoneb9c1b512016-09-06 20:57:50 +000070class AddMacroState {
71 enum State {
72 CURRENT_FILE_NOT_YET_PUSHED,
73 CURRENT_FILE_PUSHED,
74 CURRENT_FILE_POPPED
75 };
Siva Chandrad8335e92015-12-16 00:22:08 +000076
77public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
79 : m_state(CURRENT_FILE_NOT_YET_PUSHED), m_current_file(current_file),
80 m_current_file_line(current_file_line) {}
Siva Chandrad8335e92015-12-16 00:22:08 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 void StartFile(const FileSpec &file) {
83 m_file_stack.push_back(file);
84 if (file == m_current_file)
85 m_state = CURRENT_FILE_PUSHED;
86 }
87
88 void EndFile() {
89 if (m_file_stack.size() == 0)
90 return;
91
92 FileSpec old_top = m_file_stack.back();
93 m_file_stack.pop_back();
94 if (old_top == m_current_file)
95 m_state = CURRENT_FILE_POPPED;
96 }
97
Adrian Prantl05097242018-04-30 16:49:04 +000098 // An entry is valid if it occurs before the current line in the current
99 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 bool IsValidEntry(uint32_t line) {
101 switch (m_state) {
102 case CURRENT_FILE_NOT_YET_PUSHED:
103 return true;
104 case CURRENT_FILE_PUSHED:
Adrian Prantl05097242018-04-30 16:49:04 +0000105 // If we are in file included in the current file, the entry should be
106 // added.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 if (m_file_stack.back() != m_current_file)
108 return true;
109
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000110 return line < m_current_file_line;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 default:
112 return false;
Siva Chandrad8335e92015-12-16 00:22:08 +0000113 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 }
Siva Chandrad8335e92015-12-16 00:22:08 +0000115
116private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 std::vector<FileSpec> m_file_stack;
118 State m_state;
119 FileSpec m_current_file;
120 uint32_t m_current_file_line;
Siva Chandrad8335e92015-12-16 00:22:08 +0000121};
122
123} // anonymous namespace
124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
126 AddMacroState &state, StreamString &stream) {
127 if (dm == nullptr)
128 return;
129
130 for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
131 const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
132 uint32_t line;
133
134 switch (entry.GetType()) {
135 case DebugMacroEntry::DEFINE:
136 if (state.IsValidEntry(entry.GetLineNumber()))
137 stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
138 else
Siva Chandrad8335e92015-12-16 00:22:08 +0000139 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 break;
141 case DebugMacroEntry::UNDEF:
142 if (state.IsValidEntry(entry.GetLineNumber()))
143 stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
144 else
145 return;
146 break;
147 case DebugMacroEntry::START_FILE:
148 line = entry.GetLineNumber();
149 if (state.IsValidEntry(line))
150 state.StartFile(entry.GetFileSpec(comp_unit));
151 else
152 return;
153 break;
154 case DebugMacroEntry::END_FILE:
155 state.EndFile();
156 break;
157 case DebugMacroEntry::INDIRECT:
158 AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
159 break;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000160 default:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 // This is an unknown/invalid entry. Ignore.
162 break;
163 }
164 }
165}
166
Raphael Isemann71569d02019-05-02 10:12:56 +0000167/// Checks if the expression body contains the given variable as a token.
168/// \param body The expression body.
169/// \param var The variable token we are looking for.
170/// \return True iff the expression body containes the variable as a token.
171static bool ExprBodyContainsVar(llvm::StringRef body, llvm::StringRef var) {
172 assert(var.find_if([](char c) { return !clang::isIdentifierBody(c); }) ==
173 llvm::StringRef::npos &&
174 "variable contains non-identifier chars?");
175
176 size_t start = 0;
177 // Iterate over all occurences of the variable string in our expression.
178 while ((start = body.find(var, start)) != llvm::StringRef::npos) {
179 // We found our variable name in the expression. Check that the token
180 // that contains our needle is equal to our variable and not just contains
181 // the character sequence by accident.
182 // Prevents situations where we for example inlcude the variable 'FOO' in an
183 // expression like 'FOObar + 1'.
184 bool has_characters_before =
185 start != 0 && clang::isIdentifierBody(body[start - 1]);
186 bool has_characters_after =
187 start + var.size() < body.size() &&
188 clang::isIdentifierBody(body[start + var.size()]);
189
190 // Our token just contained the variable name as a substring. Continue
191 // searching the rest of the expression.
192 if (has_characters_before || has_characters_after) {
193 ++start;
194 continue;
195 }
196 return true;
197 }
198 return false;
199}
200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
Raphael Isemann71569d02019-05-02 10:12:56 +0000202 StreamString &stream,
203 const std::string &expr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
205 lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
206
207 ConstString var_name = var_sp->GetName();
Raphael Isemann05cfdb02019-04-26 07:21:36 +0000208 if (!var_name || var_name == "this" || var_name == ".block_descriptor")
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 continue;
210
Raphael Isemann71569d02019-05-02 10:12:56 +0000211 if (!expr.empty() && !ExprBodyContainsVar(expr, var_name.GetStringRef()))
212 continue;
213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
215 }
216}
217
Raphael Isemann71569d02019-05-02 10:12:56 +0000218bool ClangExpressionSourceCode::GetText(
219 std::string &text, lldb::LanguageType wrapping_language, bool static_method,
220 ExecutionContext &exe_ctx, bool add_locals, bool force_add_all_locals,
221 llvm::ArrayRef<std::string> modules) const {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 const char *target_specific_defines = "typedef signed char BOOL;\n";
223 std::string module_macros;
224
225 Target *target = exe_ctx.GetTargetPtr();
226 if (target) {
227 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64) {
228 target_specific_defines = "typedef bool BOOL;\n";
229 }
230 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
231 if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
232 static ConstString g_platform_ios_simulator("ios-simulator");
233 if (platform_sp->GetPluginName() == g_platform_ios_simulator) {
234 target_specific_defines = "typedef bool BOOL;\n";
235 }
236 }
237 }
238
239 if (ClangModulesDeclVendor *decl_vendor =
240 target->GetClangModulesDeclVendor()) {
241 ClangPersistentVariables *persistent_vars =
242 llvm::cast<ClangPersistentVariables>(
243 target->GetPersistentExpressionStateForLanguage(
244 lldb::eLanguageTypeC));
245 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
246 persistent_vars->GetHandLoadedClangModules();
247 ClangModulesDeclVendor::ModuleVector modules_for_macros;
248
249 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
250 modules_for_macros.push_back(module);
251 }
252
253 if (target->GetEnableAutoImportClangModules()) {
254 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
255 if (Block *block = frame->GetFrameBlock()) {
256 SymbolContext sc;
257
258 block->CalculateSymbolContext(&sc);
259
260 if (sc.comp_unit) {
261 StreamString error_stream;
262
263 decl_vendor->AddModulesForCompileUnit(
264 *sc.comp_unit, modules_for_macros, error_stream);
265 }
266 }
267 }
268 }
269
270 decl_vendor->ForEachMacro(
271 modules_for_macros,
272 [&module_macros](const std::string &expansion) -> bool {
273 module_macros.append(expansion);
274 module_macros.append("\n");
275 return false;
276 });
277 }
278 }
279
280 StreamString debug_macros_stream;
281 StreamString lldb_local_var_decls;
282 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
283 const SymbolContext &sc = frame->GetSymbolContext(
284 lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
285
286 if (sc.comp_unit && sc.line_entry.IsValid()) {
287 DebugMacros *dm = sc.comp_unit->GetDebugMacros();
288 if (dm) {
289 AddMacroState state(sc.line_entry.file, sc.line_entry.line);
290 AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
291 }
292 }
293
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000294 if (add_locals) {
295 if (Language::LanguageIsCPlusPlus(frame->GetLanguage())) {
296 if (target->GetInjectLocalVariables(&exe_ctx)) {
297 lldb::VariableListSP var_list_sp =
298 frame->GetInScopeVariableList(false, true);
Raphael Isemann71569d02019-05-02 10:12:56 +0000299 AddLocalVariableDecls(var_list_sp, lldb_local_var_decls,
300 force_add_all_locals ? "" : m_body);
Aleksandr Urakov40624a02019-02-05 09:14:36 +0000301 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 }
303 }
304 }
305
306 if (m_wrap) {
307 switch (wrapping_language) {
308 default:
309 return false;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000310 case lldb::eLanguageTypeC:
311 case lldb::eLanguageTypeC_plus_plus:
312 case lldb::eLanguageTypeObjC:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000313 break;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000314 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000316 // Generate a list of @import statements that will import the specified
317 // module into our expression.
318 std::string module_imports;
319 for (const std::string &module : modules) {
320 module_imports.append("@import ");
321 module_imports.append(module);
322 module_imports.append(";\n");
323 }
324
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 StreamString wrap_stream;
326
327 wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", module_macros.c_str(),
328 debug_macros_stream.GetData(), g_expression_prefix,
329 target_specific_defines, m_prefix.c_str());
330
331 // First construct a tagged form of the user expression so we can find it
332 // later:
333 std::string tagged_body;
334 switch (wrapping_language) {
335 default:
336 tagged_body = m_body;
337 break;
338 case lldb::eLanguageTypeC:
339 case lldb::eLanguageTypeC_plus_plus:
340 case lldb::eLanguageTypeObjC:
341 tagged_body.append(c_start_marker);
342 tagged_body.append(m_body);
343 tagged_body.append(c_end_marker);
344 break;
345 }
346 switch (wrapping_language) {
347 default:
348 break;
349 case lldb::eLanguageTypeC:
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000350 wrap_stream.Printf("%s"
351 "void \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 "%s(void *$__lldb_arg) \n"
353 "{ \n"
354 " %s; \n"
355 "%s"
356 "} \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000357 module_imports.c_str(), m_name.c_str(),
358 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 break;
360 case lldb::eLanguageTypeC_plus_plus:
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000361 wrap_stream.Printf("%s"
362 "void \n"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 "$__lldb_class::%s(void *$__lldb_arg) \n"
364 "{ \n"
365 " %s; \n"
366 "%s"
367 "} \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000368 module_imports.c_str(), m_name.c_str(),
369 lldb_local_var_decls.GetData(), tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000370 break;
371 case lldb::eLanguageTypeObjC:
372 if (static_method) {
373 wrap_stream.Printf(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000374 "%s"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000375 "@interface $__lldb_objc_class ($__lldb_category) \n"
376 "+(void)%s:(void *)$__lldb_arg; \n"
377 "@end \n"
378 "@implementation $__lldb_objc_class ($__lldb_category) \n"
379 "+(void)%s:(void *)$__lldb_arg \n"
380 "{ \n"
381 "%s"
382 "} \n"
383 "@end \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000384 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
385 tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000386 } else {
387 wrap_stream.Printf(
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000388 "%s"
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 "@interface $__lldb_objc_class ($__lldb_category) \n"
390 "-(void)%s:(void *)$__lldb_arg; \n"
391 "@end \n"
392 "@implementation $__lldb_objc_class ($__lldb_category) \n"
393 "-(void)%s:(void *)$__lldb_arg \n"
394 "{ \n"
395 "%s"
396 "} \n"
397 "@end \n",
Raphael Isemann6c0bbfc2019-03-12 17:09:33 +0000398 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
399 tagged_body.c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000400 }
401 break;
402 }
403
404 text = wrap_stream.GetString();
405 } else {
406 text.append(m_body);
407 }
408
409 return true;
Jim Inghame5ee6f02016-03-29 22:00:08 +0000410}
411
Jim Inghamea401ec2019-03-06 22:43:25 +0000412bool ClangExpressionSourceCode::GetOriginalBodyBounds(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000413 std::string transformed_text, lldb::LanguageType wrapping_language,
414 size_t &start_loc, size_t &end_loc) {
415 const char *start_marker;
416 const char *end_marker;
417
418 switch (wrapping_language) {
419 default:
420 return false;
421 case lldb::eLanguageTypeC:
422 case lldb::eLanguageTypeC_plus_plus:
423 case lldb::eLanguageTypeObjC:
424 start_marker = c_start_marker;
425 end_marker = c_end_marker;
426 break;
427 }
428
429 start_loc = transformed_text.find(start_marker);
430 if (start_loc == std::string::npos)
431 return false;
432 start_loc += strlen(start_marker);
433 end_loc = transformed_text.find(end_marker);
Jonas Devliegherea6682a42018-12-15 00:15:33 +0000434 return end_loc != std::string::npos;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435}