LLDB mainline
ClangExpressionSourceCode.cpp
Go to the documentation of this file.
1//===-- ClangExpressionSourceCode.cpp -------------------------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8
10
11#include "ClangExpressionUtil.h"
12
13#include "clang/AST/TypeBase.h"
14#include "clang/Basic/CharInfo.h"
15#include "clang/Basic/FileManager.h"
16#include "clang/Basic/SourceManager.h"
17#include "clang/Lex/Lexer.h"
18#include "llvm/ADT/ScopeExit.h"
19#include "llvm/ADT/StringRef.h"
20
23#include "lldb/Symbol/Block.h"
32#include "lldb/Target/Target.h"
34#include "lldb/lldb-forward.h"
35
36using namespace lldb_private;
37
38#define PREFIX_NAME "<lldb wrapper prefix>"
39#define SUFFIX_NAME "<lldb wrapper suffix>"
40
42
44"#line 1 \"" PREFIX_NAME R"("
45#ifndef offsetof
46#define offsetof(t, d) __builtin_offsetof(t, d)
47#endif
48#ifndef NULL
49#define NULL (__null)
50#endif
51#ifndef Nil
52#define Nil (__null)
53#endif
54#ifndef nil
55#define nil (__null)
56#endif
57#ifndef YES
58#define YES ((BOOL)1)
59#endif
60#ifndef NO
61#define NO ((BOOL)0)
62#endif
63typedef __INT8_TYPE__ int8_t;
64typedef __UINT8_TYPE__ uint8_t;
65typedef __INT16_TYPE__ int16_t;
66typedef __UINT16_TYPE__ uint16_t;
67typedef __INT32_TYPE__ int32_t;
68typedef __UINT32_TYPE__ uint32_t;
69typedef __INT64_TYPE__ int64_t;
70typedef __UINT64_TYPE__ uint64_t;
71typedef __INTPTR_TYPE__ intptr_t;
72typedef __UINTPTR_TYPE__ uintptr_t;
73typedef __SIZE_TYPE__ size_t;
74typedef __PTRDIFF_TYPE__ ptrdiff_t;
75typedef unsigned short unichar;
76extern "C"
77{
78 int printf(const char * __restrict, ...);
79}
80)";
81
83 "\n;\n#line 1 \"" SUFFIX_NAME "\"\n";
84
85namespace {
86
87class AddMacroState {
88 enum State {
89 CURRENT_FILE_NOT_YET_PUSHED,
90 CURRENT_FILE_PUSHED,
91 CURRENT_FILE_POPPED
92 };
93
94public:
95 AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
96 : m_current_file(current_file), m_current_file_line(current_file_line) {}
97
98 void StartFile(const FileSpec &file) {
99 m_file_stack.push_back(file);
100 if (file == m_current_file)
101 m_state = CURRENT_FILE_PUSHED;
102 }
103
104 void EndFile() {
105 if (m_file_stack.size() == 0)
106 return;
107
108 FileSpec old_top = m_file_stack.back();
109 m_file_stack.pop_back();
110 if (old_top == m_current_file)
111 m_state = CURRENT_FILE_POPPED;
112 }
113
114 // An entry is valid if it occurs before the current line in the current
115 // file.
116 bool IsValidEntry(uint32_t line) {
117 switch (m_state) {
118 case CURRENT_FILE_NOT_YET_PUSHED:
119 return true;
120 case CURRENT_FILE_PUSHED:
121 // If we are in file included in the current file, the entry should be
122 // added.
123 if (m_file_stack.back() != m_current_file)
124 return true;
125
126 return line < m_current_file_line;
127 default:
128 return false;
129 }
130 }
131
132private:
133 std::vector<FileSpec> m_file_stack;
134 State m_state = CURRENT_FILE_NOT_YET_PUSHED;
135 FileSpec m_current_file;
136 uint32_t m_current_file_line;
137};
138
139} // anonymous namespace
140
141static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
142 AddMacroState &state, StreamString &stream) {
143 if (dm == nullptr)
144 return;
145
146 // The macros directives below can potentially redefine builtin macros of the
147 // Clang instance which parses the user expression. The Clang diagnostics
148 // caused by this are not useful for the user as the source code here is
149 // generated by LLDB.
150 stream << "#pragma clang diagnostic push\n";
151 stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";
152 stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";
153 llvm::scope_exit pop_warning(
154 [&stream]() { stream << "#pragma clang diagnostic pop\n"; });
155
156 for (size_t i = 0; i < dm->GetNumMacroEntries(); i++) {
157 const DebugMacroEntry &entry = dm->GetMacroEntryAtIndex(i);
158 uint32_t line;
159
160 switch (entry.GetType()) {
162 if (state.IsValidEntry(entry.GetLineNumber()))
163 stream.Format("#define {0}\n", entry.GetMacroString());
164 else
165 return;
166 break;
168 if (state.IsValidEntry(entry.GetLineNumber()))
169 stream.Format("#undef {0}\n", entry.GetMacroString());
170 else
171 return;
172 break;
174 line = entry.GetLineNumber();
175 if (state.IsValidEntry(line))
176 state.StartFile(entry.GetFileSpec(comp_unit));
177 else
178 return;
179 break;
181 state.EndFile();
182 break;
184 AddMacros(entry.GetIndirectDebugMacros(), comp_unit, state, stream);
185 break;
186 default:
187 // This is an unknown/invalid entry. Ignore.
188 break;
189 }
190 }
191}
192
193/// Return qualifers of the current C++ method.
194static clang::Qualifiers GetFrameCVQualifiers(StackFrame *frame) {
195 if (!frame)
196 return {};
197
198 auto this_sp = frame->FindVariable(ConstString("this"));
199 if (!this_sp)
200 return {};
201
202 // Lambdas that capture 'this' have a member variable called 'this' (DWARF) /
203 // '__this' (CodeView). The class context of __lldb_expr for a lambda is the
204 // class type of the 'this' capture (not the anonymous lambda structure). So
205 // use the qualifiers of the captured 'this'.
206 auto this_this_sp = this_sp->GetChildMemberWithName("this");
207 if (!this_this_sp)
208 this_this_sp = this_sp->GetChildMemberWithName("__this");
209 if (this_this_sp)
210 return clang::Qualifiers::fromCVRMask(
211 this_this_sp->GetCompilerType().GetPointeeType().GetTypeQualifiers());
212
213 // Not in a lambda. Return 'this' qualifiers.
214 return clang::Qualifiers::fromCVRMask(
215 this_sp->GetCompilerType().GetPointeeType().GetTypeQualifiers());
216}
217
219 llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
220 llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
221 : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
222 // Use #line markers to pretend that we have a single-line source file
223 // containing only the user expression. This will hide our wrapper code
224 // from the user when we render diagnostics with Clang.
225 m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
227}
228
229namespace {
230/// Allows checking if a token is contained in a given expression.
231class TokenVerifier {
232 /// The tokens we found in the expression.
233 llvm::StringSet<> m_tokens;
234
235public:
236 TokenVerifier(std::string body);
237 /// Returns true iff the given expression body contained a token with the
238 /// given content.
239 bool hasToken(llvm::StringRef token) const {
240 return m_tokens.contains(token);
241 }
242};
243
244// If we're evaluating from inside a lambda that captures a 'this' pointer,
245// add a "using" declaration to 'stream' for each capture used in the
246// expression (tokenized by 'verifier').
247//
248// If no 'this' capture exists, generate no using declarations. Instead
249// capture lookups will get resolved by the same mechanism as class member
250// variable lookup. That's because Clang generates an unnamed structure
251// representing the lambda closure whose members are the captured variables.
252void AddLambdaCaptureDecls(StreamString &stream, StackFrame *frame,
253 TokenVerifier const &verifier) {
254 assert(frame);
255
256 if (auto thisValSP = ClangExpressionUtil::GetLambdaValueObject(frame)) {
257 uint32_t numChildren = thisValSP->GetNumChildrenIgnoringErrors();
258 for (uint32_t i = 0; i < numChildren; ++i) {
259 auto childVal = thisValSP->GetChildAtIndex(i);
260 ConstString childName(childVal ? childVal->GetName() : ConstString(""));
261
262 if (!childName.IsEmpty() && verifier.hasToken(childName.GetStringRef()) &&
263 childName != "this") {
264 stream.Printf("using $__lldb_local_vars::%s;\n",
265 childName.GetCString());
266 }
267 }
268 }
269}
270
271} // namespace
272
273TokenVerifier::TokenVerifier(std::string body) {
274 using namespace clang;
275
276 // We only care about tokens and not their original source locations. If we
277 // move the whole expression to only be in one line we can simplify the
278 // following code that extracts the token contents.
279 llvm::replace(body, '\n', ' ');
280 llvm::replace(body, '\r', ' ');
281
282 FileSystemOptions file_opts;
283 FileManager file_mgr(file_opts,
284 FileSystem::Instance().GetVirtualFileSystem());
285
286 // Let's build the actual source code Clang needs and setup some utility
287 // objects.
288 DiagnosticOptions diags_opts;
289 DiagnosticsEngine diags(DiagnosticIDs::create(), diags_opts);
290 clang::SourceManager SM(diags, file_mgr);
291 auto buf = llvm::MemoryBuffer::getMemBuffer(body);
292
293 FileID FID = SM.createFileID(buf->getMemBufferRef());
294
295 // Let's just enable the latest ObjC and C++ which should get most tokens
296 // right.
297 LangOptions Opts;
298 Opts.ObjC = true;
299 Opts.DollarIdents = true;
300 Opts.CPlusPlus20 = true;
301 Opts.LineComment = true;
302
303 Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
304
305 Token token;
306 bool exit = false;
307 while (!exit) {
308 // Returns true if this is the last token we get from the lexer.
309 exit = lex.LexFromRawLexer(token);
310
311 // Extract the column number which we need to extract the token content.
312 // Our expression is just one line, so we don't need to handle any line
313 // numbers here.
314 bool invalid = false;
315 unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
316 if (invalid)
317 continue;
318 // Column numbers start at 1, but indexes in our string start at 0.
319 --start;
320
321 // Annotations don't have a length, so let's skip them.
322 if (token.isAnnotation())
323 continue;
324
325 // Extract the token string from our source code and store it.
326 std::string token_str = body.substr(start, token.getLength());
327 if (token_str.empty())
328 continue;
329 m_tokens.insert(token_str);
330 }
331}
332
334 const std::string &expr,
335 StackFrame *frame) const {
336 assert(frame);
337 TokenVerifier tokens(expr);
338
339 lldb::VariableListSP var_list_sp = frame->GetInScopeVariableList(false, true);
340
341 for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
342 lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
343
344 ConstString var_name = var_sp->GetName();
345
346 if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction) {
347 AddLambdaCaptureDecls(stream, frame, tokens);
348
349 continue;
350 }
351
352 // We can check for .block_descriptor w/o checking for language since this
353 // is not a valid identifier in either C or C++.
354 if (!var_name || var_name == ".block_descriptor")
355 continue;
356
357 if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
358 continue;
359
360 const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
362 if ((var_name == "self" || var_name == "_cmd") && is_objc)
363 continue;
364
365 stream.Format("using $__lldb_local_vars::{0};\n", var_name);
366 }
367}
368
370 ExecutionContext &exe_ctx,
371 bool add_locals,
372 bool force_add_all_locals,
373 llvm::ArrayRef<std::string> modules,
374 bool ignore_context_qualifiers) const {
375 const char *target_specific_defines = "typedef signed char BOOL;\n";
376 std::string module_macros;
377 llvm::raw_string_ostream module_macros_stream(module_macros);
378
379 Target *target = exe_ctx.GetTargetPtr();
380 if (target) {
381 if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
382 target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
383 target_specific_defines = "typedef bool BOOL;\n";
384 }
385 if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
386 if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
387 if (platform_sp->GetPluginName() == "ios-simulator") {
388 target_specific_defines = "typedef bool BOOL;\n";
389 }
390 }
391 }
392
393 auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
395 std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
396 persistent_vars->GetClangModulesDeclVendor();
397 if (decl_vendor) {
398 const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
399 persistent_vars->GetHandLoadedClangModules();
400 ClangModulesDeclVendor::ModuleVector modules_for_macros;
401
402 for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
403 modules_for_macros.push_back(module);
404 }
405
406 if (target->GetEnableAutoImportClangModules()) {
407 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
408 if (Block *block = frame->GetFrameBlock()) {
409 SymbolContext sc;
410
411 block->CalculateSymbolContext(&sc);
412
413 if (sc.comp_unit) {
414 if (auto err = decl_vendor->AddModulesForCompileUnit(
415 *sc.comp_unit, modules_for_macros))
417 GetLog(LLDBLog::Expressions), std::move(err),
418 "Error while loading hand-imported modules:\n{0}");
419 }
420 }
421 }
422 }
423
424 decl_vendor->ForEachMacro(
425 modules_for_macros,
426 [&module_macros_stream](llvm::StringRef token,
427 llvm::StringRef expansion) -> bool {
428 // Check if the macro hasn't already been defined in the
429 // g_expression_prefix (which defines a few builtin macros).
430 module_macros_stream << "#ifndef " << token << "\n";
431 module_macros_stream << expansion << "\n";
432 module_macros_stream << "#endif\n";
433 return false;
434 });
435 }
436 }
437
438 StreamString debug_macros_stream;
439 StreamString lldb_local_var_decls;
440 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
441 const SymbolContext &sc = frame->GetSymbolContext(
442 lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
443
444 if (sc.comp_unit && sc.line_entry.IsValid()) {
446 if (dm) {
447 AddMacroState state(sc.line_entry.GetFile(), sc.line_entry.line);
448 AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
449 }
450 }
451
452 if (add_locals)
453 if (target->GetInjectLocalVariables(&exe_ctx)) {
454 AddLocalVariableDecls(lldb_local_var_decls,
455 force_add_all_locals ? "" : m_body, frame);
456 }
457 }
458
459 if (m_wrap) {
460 // Generate a list of @import statements that will import the specified
461 // module into our expression.
462 std::string module_imports;
463 for (const std::string &module : modules) {
464 module_imports.append("@import ");
465 module_imports.append(module);
466 module_imports.append(";\n");
467 }
468
469 StreamString wrap_stream;
470
471 wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
472 module_macros.c_str(), debug_macros_stream.GetData(),
473 target_specific_defines, m_prefix.c_str());
474
475 // First construct a tagged form of the user expression so we can find it
476 // later:
477 std::string tagged_body;
478 tagged_body.append(m_start_marker);
479 tagged_body.append(m_body);
480 tagged_body.append(m_end_marker);
481
482 switch (m_wrap_kind) {
484 wrap_stream.Printf("%s"
485 "void \n"
486 "%s(void *$__lldb_arg) \n"
487 "{ \n"
488 " %s; \n"
489 "%s"
490 "} \n",
491 module_imports.c_str(), m_name.c_str(),
492 lldb_local_var_decls.GetData(), tagged_body.c_str());
493 break;
495 wrap_stream.Printf("%s"
496 "void \n"
497 "$__lldb_class::%s(void *$__lldb_arg) %s \n"
498 "{ \n"
499 " %s; \n"
500 "%s"
501 "} \n",
502 module_imports.c_str(), m_name.c_str(),
503 ignore_context_qualifiers
504 ? ""
506 .getAsString()
507 .c_str(),
508 lldb_local_var_decls.GetData(), tagged_body.c_str());
509 break;
511 wrap_stream.Printf(
512 "%s"
513 "@interface $__lldb_objc_class ($__lldb_category) \n"
514 "-(void)%s:(void *)$__lldb_arg; \n"
515 "@end \n"
516 "@implementation $__lldb_objc_class ($__lldb_category) \n"
517 "-(void)%s:(void *)$__lldb_arg \n"
518 "{ \n"
519 " %s; \n"
520 "%s"
521 "} \n"
522 "@end \n",
523 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
524 lldb_local_var_decls.GetData(), tagged_body.c_str());
525 break;
526
528 wrap_stream.Printf(
529 "%s"
530 "@interface $__lldb_objc_class ($__lldb_category) \n"
531 "+(void)%s:(void *)$__lldb_arg; \n"
532 "@end \n"
533 "@implementation $__lldb_objc_class ($__lldb_category) \n"
534 "+(void)%s:(void *)$__lldb_arg \n"
535 "{ \n"
536 " %s; \n"
537 "%s"
538 "} \n"
539 "@end \n",
540 module_imports.c_str(), m_name.c_str(), m_name.c_str(),
541 lldb_local_var_decls.GetData(), tagged_body.c_str());
542 break;
543 }
544
545 text = std::string(wrap_stream.GetString());
546 } else {
547 text.append(m_body);
548 }
549
550 return true;
551}
552
554 std::string transformed_text, size_t &start_loc, size_t &end_loc) {
555 start_loc = transformed_text.find(m_start_marker);
556 if (start_loc == std::string::npos)
557 return false;
558 start_loc += m_start_marker.size();
559 end_loc = transformed_text.find(m_end_marker);
560 return end_loc != std::string::npos;
561}
#define PREFIX_NAME
static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit, AddMacroState &state, StreamString &stream)
#define SUFFIX_NAME
static clang::Qualifiers GetFrameCVQualifiers(StackFrame *frame)
Return qualifers of the current C++ method.
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition ArchSpec.cpp:682
A class that describes a single lexical block.
Definition Block.h:41
ClangExpressionSourceCode(llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix, llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
WrapKind
The possible ways an expression can be wrapped.
@ ObjCStaticMethod
Wrapped in a static Objective-C method.
@ CppMemberFunction
Wrapped in a non-static member function of a C++ class.
@ ObjCInstanceMethod
Wrapped in an instance Objective-C method.
std::string m_start_marker
String marking the start of the user expression.
static const llvm::StringRef g_prefix_file_name
The file name we use for the wrapper code that we inject before the user expression.
bool GetText(std::string &text, ExecutionContext &exe_ctx, bool add_locals, bool force_add_all_locals, llvm::ArrayRef< std::string > modules, bool ignore_context_qualifiers) const
Generates the source code that will evaluate the expression.
bool GetOriginalBodyBounds(std::string transformed_text, size_t &start_loc, size_t &end_loc)
const WrapKind m_wrap_kind
How the expression has been wrapped.
std::string m_end_marker
String marking the end of the user expression.
void AddLocalVariableDecls(StreamString &stream, const std::string &expr, StackFrame *frame) const
Writes "using" declarations for local variables into the specified stream.
A class that describes a compilation unit.
Definition CompileUnit.h:43
DebugMacros * GetDebugMacros()
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
ConstString GetMacroString() const
Definition DebugMacros.h:50
const FileSpec & GetFileSpec(CompileUnit *comp_unit) const
DebugMacros * GetIndirectDebugMacros() const
Definition DebugMacros.h:54
EntryType GetType() const
Definition DebugMacros.h:46
uint64_t GetLineNumber() const
Definition DebugMacros.h:48
DebugMacroEntry GetMacroEntryAtIndex(const size_t index) const
Definition DebugMacros.h:83
size_t GetNumMacroEntries() const
Definition DebugMacros.h:81
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
StackFrame * GetFramePtr() const
Returns a pointer to the frame object.
Target * GetTargetPtr() const
Returns a pointer to the target object.
ExpressionSourceCode(llvm::StringRef name, llvm::StringRef prefix, llvm::StringRef body, Wrapping wrap)
A file utility class.
Definition FileSpec.h:57
static FileSystem & Instance()
This base class provides an interface to stack frames.
Definition StackFrame.h:44
virtual const SymbolContext & GetSymbolContext(lldb::SymbolContextItem resolve_scope)
Provide a SymbolContext for this StackFrame's current pc value.
virtual lldb::VariableListSP GetInScopeVariableList(bool get_file_globals, bool include_synthetic_vars=true, bool must_have_valid_location=false)
Retrieve the list of variables that are in scope at this StackFrame's pc.
virtual Block * GetFrameBlock()
Get the current lexical scope block for this StackFrame, if possible.
virtual lldb::ValueObjectSP FindVariable(ConstString name)
Attempt to reconstruct the ValueObject for a variable with a given name from within the current Stack...
const char * GetData() const
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
Defines a symbol context baton that can be handed other debug core functions.
CompileUnit * comp_unit
The CompileUnit for a given query.
LineEntry line_entry
The LineEntry for a given query.
bool GetEnableAutoImportClangModules() const
Definition Target.cpp:5454
bool GetInjectLocalVariables(ExecutionContext *exe_ctx) const
Definition Target.cpp:5137
PersistentExpressionState * GetPersistentExpressionStateForLanguage(lldb::LanguageType language)
Definition Target.cpp:2743
lldb::PlatformSP GetPlatform()
Definition Target.h:1969
const ArchSpec & GetArchitecture() const
Definition Target.h:1283
lldb::ValueObjectSP GetLambdaValueObject(StackFrame *frame)
Returns a ValueObject for the lambda class in the current frame.
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
std::shared_ptr< lldb_private::Platform > PlatformSP
@ eLanguageTypeC
Non-standardized C, such as K&R.
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP
bool IsValid() const
Check if a line entry object is valid.
Definition LineEntry.cpp:35
uint32_t line
The source line number, or LLDB_INVALID_LINE_NUMBER if there is no line number information.
Definition LineEntry.h:151
const FileSpec & GetFile() const
Helper to access the file.
Definition LineEntry.h:134