xref: /freebsd/contrib/llvm-project/lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionSourceCode.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
9 #include "ClangExpressionSourceCode.h"
10 
11 #include "ClangExpressionUtil.h"
12 
13 #include "clang/Basic/CharInfo.h"
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "clang/Lex/Lexer.h"
17 #include "llvm/ADT/ScopeExit.h"
18 #include "llvm/ADT/StringRef.h"
19 
20 #include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
21 #include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
22 #include "lldb/Symbol/Block.h"
23 #include "lldb/Symbol/CompileUnit.h"
24 #include "lldb/Symbol/DebugMacros.h"
25 #include "lldb/Symbol/TypeSystem.h"
26 #include "lldb/Symbol/VariableList.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Language.h"
29 #include "lldb/Target/Platform.h"
30 #include "lldb/Target/StackFrame.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Utility/StreamString.h"
33 #include "lldb/lldb-forward.h"
34 
35 using namespace lldb_private;
36 
37 #define PREFIX_NAME "<lldb wrapper prefix>"
38 #define SUFFIX_NAME "<lldb wrapper suffix>"
39 
40 const llvm::StringRef ClangExpressionSourceCode::g_prefix_file_name = PREFIX_NAME;
41 
42 const char *ClangExpressionSourceCode::g_expression_prefix =
43 "#line 1 \"" PREFIX_NAME R"("
44 #ifndef offsetof
45 #define offsetof(t, d) __builtin_offsetof(t, d)
46 #endif
47 #ifndef NULL
48 #define NULL (__null)
49 #endif
50 #ifndef Nil
51 #define Nil (__null)
52 #endif
53 #ifndef nil
54 #define nil (__null)
55 #endif
56 #ifndef YES
57 #define YES ((BOOL)1)
58 #endif
59 #ifndef NO
60 #define NO ((BOOL)0)
61 #endif
62 typedef __INT8_TYPE__ int8_t;
63 typedef __UINT8_TYPE__ uint8_t;
64 typedef __INT16_TYPE__ int16_t;
65 typedef __UINT16_TYPE__ uint16_t;
66 typedef __INT32_TYPE__ int32_t;
67 typedef __UINT32_TYPE__ uint32_t;
68 typedef __INT64_TYPE__ int64_t;
69 typedef __UINT64_TYPE__ uint64_t;
70 typedef __INTPTR_TYPE__ intptr_t;
71 typedef __UINTPTR_TYPE__ uintptr_t;
72 typedef __SIZE_TYPE__ size_t;
73 typedef __PTRDIFF_TYPE__ ptrdiff_t;
74 typedef unsigned short unichar;
75 extern "C"
76 {
77     int printf(const char * __restrict, ...);
78 }
79 )";
80 
81 const char *ClangExpressionSourceCode::g_expression_suffix =
82     "\n;\n#line 1 \"" SUFFIX_NAME "\"\n";
83 
84 namespace {
85 
86 class AddMacroState {
87   enum State {
88     CURRENT_FILE_NOT_YET_PUSHED,
89     CURRENT_FILE_PUSHED,
90     CURRENT_FILE_POPPED
91   };
92 
93 public:
AddMacroState(const FileSpec & current_file,const uint32_t current_file_line)94   AddMacroState(const FileSpec &current_file, const uint32_t current_file_line)
95       : m_current_file(current_file), m_current_file_line(current_file_line) {}
96 
StartFile(const FileSpec & file)97   void StartFile(const FileSpec &file) {
98     m_file_stack.push_back(file);
99     if (file == m_current_file)
100       m_state = CURRENT_FILE_PUSHED;
101   }
102 
EndFile()103   void EndFile() {
104     if (m_file_stack.size() == 0)
105       return;
106 
107     FileSpec old_top = m_file_stack.back();
108     m_file_stack.pop_back();
109     if (old_top == m_current_file)
110       m_state = CURRENT_FILE_POPPED;
111   }
112 
113   // An entry is valid if it occurs before the current line in the current
114   // file.
IsValidEntry(uint32_t line)115   bool IsValidEntry(uint32_t line) {
116     switch (m_state) {
117     case CURRENT_FILE_NOT_YET_PUSHED:
118       return true;
119     case CURRENT_FILE_PUSHED:
120       // If we are in file included in the current file, the entry should be
121       // added.
122       if (m_file_stack.back() != m_current_file)
123         return true;
124 
125       return line < m_current_file_line;
126     default:
127       return false;
128     }
129   }
130 
131 private:
132   std::vector<FileSpec> m_file_stack;
133   State m_state = CURRENT_FILE_NOT_YET_PUSHED;
134   FileSpec m_current_file;
135   uint32_t m_current_file_line;
136 };
137 
138 } // anonymous namespace
139 
AddMacros(const DebugMacros * dm,CompileUnit * comp_unit,AddMacroState & state,StreamString & stream)140 static void AddMacros(const DebugMacros *dm, CompileUnit *comp_unit,
141                       AddMacroState &state, StreamString &stream) {
142   if (dm == nullptr)
143     return;
144 
145   // The macros directives below can potentially redefine builtin macros of the
146   // Clang instance which parses the user expression. The Clang diagnostics
147   // caused by this are not useful for the user as the source code here is
148   // generated by LLDB.
149   stream << "#pragma clang diagnostic push\n";
150   stream << "#pragma clang diagnostic ignored \"-Wmacro-redefined\"\n";
151   stream << "#pragma clang diagnostic ignored \"-Wbuiltin-macro-redefined\"\n";
152   auto pop_warning = llvm::make_scope_exit([&stream](){
153     stream << "#pragma clang diagnostic pop\n";
154   });
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()) {
161     case DebugMacroEntry::DEFINE:
162       if (state.IsValidEntry(entry.GetLineNumber()))
163         stream.Printf("#define %s\n", entry.GetMacroString().AsCString());
164       else
165         return;
166       break;
167     case DebugMacroEntry::UNDEF:
168       if (state.IsValidEntry(entry.GetLineNumber()))
169         stream.Printf("#undef %s\n", entry.GetMacroString().AsCString());
170       else
171         return;
172       break;
173     case DebugMacroEntry::START_FILE:
174       line = entry.GetLineNumber();
175       if (state.IsValidEntry(line))
176         state.StartFile(entry.GetFileSpec(comp_unit));
177       else
178         return;
179       break;
180     case DebugMacroEntry::END_FILE:
181       state.EndFile();
182       break;
183     case DebugMacroEntry::INDIRECT:
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 
ClangExpressionSourceCode(llvm::StringRef filename,llvm::StringRef name,llvm::StringRef prefix,llvm::StringRef body,Wrapping wrap,WrapKind wrap_kind)193 lldb_private::ClangExpressionSourceCode::ClangExpressionSourceCode(
194     llvm::StringRef filename, llvm::StringRef name, llvm::StringRef prefix,
195     llvm::StringRef body, Wrapping wrap, WrapKind wrap_kind)
196     : ExpressionSourceCode(name, prefix, body, wrap), m_wrap_kind(wrap_kind) {
197   // Use #line markers to pretend that we have a single-line source file
198   // containing only the user expression. This will hide our wrapper code
199   // from the user when we render diagnostics with Clang.
200   m_start_marker = "#line 1 \"" + filename.str() + "\"\n";
201   m_end_marker = g_expression_suffix;
202 }
203 
204 namespace {
205 /// Allows checking if a token is contained in a given expression.
206 class TokenVerifier {
207   /// The tokens we found in the expression.
208   llvm::StringSet<> m_tokens;
209 
210 public:
211   TokenVerifier(std::string body);
212   /// Returns true iff the given expression body contained a token with the
213   /// given content.
hasToken(llvm::StringRef token) const214   bool hasToken(llvm::StringRef token) const {
215     return m_tokens.contains(token);
216   }
217 };
218 
219 // If we're evaluating from inside a lambda that captures a 'this' pointer,
220 // add a "using" declaration to 'stream' for each capture used in the
221 // expression (tokenized by 'verifier').
222 //
223 // If no 'this' capture exists, generate no using declarations. Instead
224 // capture lookups will get resolved by the same mechanism as class member
225 // variable lookup. That's because Clang generates an unnamed structure
226 // representing the lambda closure whose members are the captured variables.
AddLambdaCaptureDecls(StreamString & stream,StackFrame * frame,TokenVerifier const & verifier)227 void AddLambdaCaptureDecls(StreamString &stream, StackFrame *frame,
228                            TokenVerifier const &verifier) {
229   assert(frame);
230 
231   if (auto thisValSP = ClangExpressionUtil::GetLambdaValueObject(frame)) {
232     uint32_t numChildren = thisValSP->GetNumChildrenIgnoringErrors();
233     for (uint32_t i = 0; i < numChildren; ++i) {
234       auto childVal = thisValSP->GetChildAtIndex(i);
235       ConstString childName(childVal ? childVal->GetName() : ConstString(""));
236 
237       if (!childName.IsEmpty() && verifier.hasToken(childName.GetStringRef()) &&
238           childName != "this") {
239         stream.Printf("using $__lldb_local_vars::%s;\n",
240                       childName.GetCString());
241       }
242     }
243   }
244 }
245 
246 } // namespace
247 
TokenVerifier(std::string body)248 TokenVerifier::TokenVerifier(std::string body) {
249   using namespace clang;
250 
251   // We only care about tokens and not their original source locations. If we
252   // move the whole expression to only be in one line we can simplify the
253   // following code that extracts the token contents.
254   llvm::replace(body, '\n', ' ');
255   llvm::replace(body, '\r', ' ');
256 
257   FileSystemOptions file_opts;
258   FileManager file_mgr(file_opts,
259                        FileSystem::Instance().GetVirtualFileSystem());
260 
261   // Let's build the actual source code Clang needs and setup some utility
262   // objects.
263   llvm::IntrusiveRefCntPtr<DiagnosticIDs> diag_ids(new DiagnosticIDs());
264   DiagnosticOptions diags_opts;
265   DiagnosticsEngine diags(diag_ids, diags_opts);
266   clang::SourceManager SM(diags, file_mgr);
267   auto buf = llvm::MemoryBuffer::getMemBuffer(body);
268 
269   FileID FID = SM.createFileID(buf->getMemBufferRef());
270 
271   // Let's just enable the latest ObjC and C++ which should get most tokens
272   // right.
273   LangOptions Opts;
274   Opts.ObjC = true;
275   Opts.DollarIdents = true;
276   Opts.CPlusPlus20 = true;
277   Opts.LineComment = true;
278 
279   Lexer lex(FID, buf->getMemBufferRef(), SM, Opts);
280 
281   Token token;
282   bool exit = false;
283   while (!exit) {
284     // Returns true if this is the last token we get from the lexer.
285     exit = lex.LexFromRawLexer(token);
286 
287     // Extract the column number which we need to extract the token content.
288     // Our expression is just one line, so we don't need to handle any line
289     // numbers here.
290     bool invalid = false;
291     unsigned start = SM.getSpellingColumnNumber(token.getLocation(), &invalid);
292     if (invalid)
293       continue;
294     // Column numbers start at 1, but indexes in our string start at 0.
295     --start;
296 
297     // Annotations don't have a length, so let's skip them.
298     if (token.isAnnotation())
299       continue;
300 
301     // Extract the token string from our source code and store it.
302     std::string token_str = body.substr(start, token.getLength());
303     if (token_str.empty())
304       continue;
305     m_tokens.insert(token_str);
306   }
307 }
308 
AddLocalVariableDecls(StreamString & stream,const std::string & expr,StackFrame * frame) const309 void ClangExpressionSourceCode::AddLocalVariableDecls(StreamString &stream,
310                                                       const std::string &expr,
311                                                       StackFrame *frame) const {
312   assert(frame);
313   TokenVerifier tokens(expr);
314 
315   lldb::VariableListSP var_list_sp = frame->GetInScopeVariableList(false, true);
316 
317   for (size_t i = 0; i < var_list_sp->GetSize(); i++) {
318     lldb::VariableSP var_sp = var_list_sp->GetVariableAtIndex(i);
319 
320     ConstString var_name = var_sp->GetName();
321 
322     if (var_name == "this" && m_wrap_kind == WrapKind::CppMemberFunction) {
323       AddLambdaCaptureDecls(stream, frame, tokens);
324 
325       continue;
326     }
327 
328     // We can check for .block_descriptor w/o checking for langauge since this
329     // is not a valid identifier in either C or C++.
330     if (!var_name || var_name == ".block_descriptor")
331       continue;
332 
333     if (!expr.empty() && !tokens.hasToken(var_name.GetStringRef()))
334       continue;
335 
336     const bool is_objc = m_wrap_kind == WrapKind::ObjCInstanceMethod ||
337                          m_wrap_kind == WrapKind::ObjCStaticMethod;
338     if ((var_name == "self" || var_name == "_cmd") && is_objc)
339       continue;
340 
341     stream.Printf("using $__lldb_local_vars::%s;\n", var_name.AsCString());
342   }
343 }
344 
GetText(std::string & text,ExecutionContext & exe_ctx,bool add_locals,bool force_add_all_locals,llvm::ArrayRef<std::string> modules) const345 bool ClangExpressionSourceCode::GetText(
346     std::string &text, ExecutionContext &exe_ctx, bool add_locals,
347     bool force_add_all_locals, llvm::ArrayRef<std::string> modules) const {
348   const char *target_specific_defines = "typedef signed char BOOL;\n";
349   std::string module_macros;
350   llvm::raw_string_ostream module_macros_stream(module_macros);
351 
352   Target *target = exe_ctx.GetTargetPtr();
353   if (target) {
354     if (target->GetArchitecture().GetMachine() == llvm::Triple::aarch64 ||
355         target->GetArchitecture().GetMachine() == llvm::Triple::aarch64_32) {
356       target_specific_defines = "typedef bool BOOL;\n";
357     }
358     if (target->GetArchitecture().GetMachine() == llvm::Triple::x86_64) {
359       if (lldb::PlatformSP platform_sp = target->GetPlatform()) {
360         if (platform_sp->GetPluginName() == "ios-simulator") {
361           target_specific_defines = "typedef bool BOOL;\n";
362         }
363       }
364     }
365 
366     auto *persistent_vars = llvm::cast<ClangPersistentVariables>(
367         target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeC));
368     std::shared_ptr<ClangModulesDeclVendor> decl_vendor =
369         persistent_vars->GetClangModulesDeclVendor();
370     if (decl_vendor) {
371       const ClangModulesDeclVendor::ModuleVector &hand_imported_modules =
372           persistent_vars->GetHandLoadedClangModules();
373       ClangModulesDeclVendor::ModuleVector modules_for_macros;
374 
375       for (ClangModulesDeclVendor::ModuleID module : hand_imported_modules) {
376         modules_for_macros.push_back(module);
377       }
378 
379       if (target->GetEnableAutoImportClangModules()) {
380         if (StackFrame *frame = exe_ctx.GetFramePtr()) {
381           if (Block *block = frame->GetFrameBlock()) {
382             SymbolContext sc;
383 
384             block->CalculateSymbolContext(&sc);
385 
386             if (sc.comp_unit) {
387               StreamString error_stream;
388 
389               decl_vendor->AddModulesForCompileUnit(
390                   *sc.comp_unit, modules_for_macros, error_stream);
391             }
392           }
393         }
394       }
395 
396       decl_vendor->ForEachMacro(
397           modules_for_macros,
398           [&module_macros_stream](llvm::StringRef token,
399                                   llvm::StringRef expansion) -> bool {
400             // Check if the macro hasn't already been defined in the
401             // g_expression_prefix (which defines a few builtin macros).
402             module_macros_stream << "#ifndef " << token << "\n";
403             module_macros_stream << expansion << "\n";
404             module_macros_stream << "#endif\n";
405             return false;
406           });
407     }
408   }
409 
410   StreamString debug_macros_stream;
411   StreamString lldb_local_var_decls;
412   if (StackFrame *frame = exe_ctx.GetFramePtr()) {
413     const SymbolContext &sc = frame->GetSymbolContext(
414         lldb::eSymbolContextCompUnit | lldb::eSymbolContextLineEntry);
415 
416     if (sc.comp_unit && sc.line_entry.IsValid()) {
417       DebugMacros *dm = sc.comp_unit->GetDebugMacros();
418       if (dm) {
419         AddMacroState state(sc.line_entry.GetFile(), sc.line_entry.line);
420         AddMacros(dm, sc.comp_unit, state, debug_macros_stream);
421       }
422     }
423 
424     if (add_locals)
425       if (target->GetInjectLocalVariables(&exe_ctx)) {
426         AddLocalVariableDecls(lldb_local_var_decls,
427                               force_add_all_locals ? "" : m_body, frame);
428       }
429   }
430 
431   if (m_wrap) {
432     // Generate a list of @import statements that will import the specified
433     // module into our expression.
434     std::string module_imports;
435     for (const std::string &module : modules) {
436       module_imports.append("@import ");
437       module_imports.append(module);
438       module_imports.append(";\n");
439     }
440 
441     StreamString wrap_stream;
442 
443     wrap_stream.Printf("%s\n%s\n%s\n%s\n%s\n", g_expression_prefix,
444                        module_macros.c_str(), debug_macros_stream.GetData(),
445                        target_specific_defines, m_prefix.c_str());
446 
447     // First construct a tagged form of the user expression so we can find it
448     // later:
449     std::string tagged_body;
450     tagged_body.append(m_start_marker);
451     tagged_body.append(m_body);
452     tagged_body.append(m_end_marker);
453 
454     switch (m_wrap_kind) {
455     case WrapKind::Function:
456       wrap_stream.Printf("%s"
457                          "void                           \n"
458                          "%s(void *$__lldb_arg)          \n"
459                          "{                              \n"
460                          "    %s;                        \n"
461                          "%s"
462                          "}                              \n",
463                          module_imports.c_str(), m_name.c_str(),
464                          lldb_local_var_decls.GetData(), tagged_body.c_str());
465       break;
466     case WrapKind::CppMemberFunction:
467       wrap_stream.Printf("%s"
468                          "void                                   \n"
469                          "$__lldb_class::%s(void *$__lldb_arg)   \n"
470                          "{                                      \n"
471                          "    %s;                                \n"
472                          "%s"
473                          "}                                      \n",
474                          module_imports.c_str(), m_name.c_str(),
475                          lldb_local_var_decls.GetData(), tagged_body.c_str());
476       break;
477     case WrapKind::ObjCInstanceMethod:
478       wrap_stream.Printf(
479           "%s"
480           "@interface $__lldb_objc_class ($__lldb_category)       \n"
481           "-(void)%s:(void *)$__lldb_arg;                         \n"
482           "@end                                                   \n"
483           "@implementation $__lldb_objc_class ($__lldb_category)  \n"
484           "-(void)%s:(void *)$__lldb_arg                          \n"
485           "{                                                      \n"
486           "    %s;                                                \n"
487           "%s"
488           "}                                                      \n"
489           "@end                                                   \n",
490           module_imports.c_str(), m_name.c_str(), m_name.c_str(),
491           lldb_local_var_decls.GetData(), tagged_body.c_str());
492       break;
493 
494     case WrapKind::ObjCStaticMethod:
495       wrap_stream.Printf(
496           "%s"
497           "@interface $__lldb_objc_class ($__lldb_category)        \n"
498           "+(void)%s:(void *)$__lldb_arg;                          \n"
499           "@end                                                    \n"
500           "@implementation $__lldb_objc_class ($__lldb_category)   \n"
501           "+(void)%s:(void *)$__lldb_arg                           \n"
502           "{                                                       \n"
503           "    %s;                                                 \n"
504           "%s"
505           "}                                                       \n"
506           "@end                                                    \n",
507           module_imports.c_str(), m_name.c_str(), m_name.c_str(),
508           lldb_local_var_decls.GetData(), tagged_body.c_str());
509       break;
510     }
511 
512     text = std::string(wrap_stream.GetString());
513   } else {
514     text.append(m_body);
515   }
516 
517   return true;
518 }
519 
GetOriginalBodyBounds(std::string transformed_text,size_t & start_loc,size_t & end_loc)520 bool ClangExpressionSourceCode::GetOriginalBodyBounds(
521     std::string transformed_text, size_t &start_loc, size_t &end_loc) {
522   start_loc = transformed_text.find(m_start_marker);
523   if (start_loc == std::string::npos)
524     return false;
525   start_loc += m_start_marker.size();
526   end_loc = transformed_text.find(m_end_marker);
527   return end_loc != std::string::npos;
528 }
529