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