1 //===-- ClangExpressionSourceCode.h -----------------------------*- 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 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H 10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H 11 12 #include "lldb/Expression/Expression.h" 13 #include "lldb/Expression/ExpressionSourceCode.h" 14 #include "lldb/lldb-enumerations.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/StringRef.h" 17 18 #include <string> 19 20 namespace lldb_private { 21 22 class ExecutionContext; 23 24 class ClangExpressionSourceCode : public ExpressionSourceCode { 25 public: 26 /// The file name we use for the wrapper code that we inject before 27 /// the user expression. 28 static const llvm::StringRef g_prefix_file_name; 29 static const char *g_expression_prefix; 30 31 /// The possible ways an expression can be wrapped. 32 enum class WrapKind { 33 /// Wrapped in a non-static member function of a C++ class. 34 CppMemberFunction, 35 /// Wrapped in an instance Objective-C method. 36 ObjCInstanceMethod, 37 /// Wrapped in a static Objective-C method. 38 ObjCStaticMethod, 39 /// Wrapped in a non-member function. 40 /// Note that this is also used for static member functions of a C++ class. 41 Function 42 }; 43 CreateWrapped(llvm::StringRef filename,llvm::StringRef prefix,llvm::StringRef body,WrapKind wrap_kind)44 static ClangExpressionSourceCode *CreateWrapped(llvm::StringRef filename, 45 llvm::StringRef prefix, 46 llvm::StringRef body, 47 WrapKind wrap_kind) { 48 return new ClangExpressionSourceCode(filename, "$__lldb_expr", prefix, body, 49 Wrap, wrap_kind); 50 } 51 52 /// Generates the source code that will evaluate the expression. 53 /// 54 /// \param text output parameter containing the source code string. 55 /// \param exe_ctx The execution context in which the expression will be 56 /// evaluated. 57 /// \param add_locals True iff local variables should be injected into the 58 /// expression source code. 59 /// \param force_add_all_locals True iff all local variables should be 60 /// injected even if they are not used in the expression. 61 /// \param modules A list of (C++) modules that the expression should import. 62 /// 63 /// \return true iff the source code was successfully generated. 64 bool GetText(std::string &text, ExecutionContext &exe_ctx, bool add_locals, 65 bool force_add_all_locals, 66 llvm::ArrayRef<std::string> modules) const; 67 68 // Given a string returned by GetText, find the beginning and end of the body 69 // passed to CreateWrapped. Return true if the bounds could be found. This 70 // will also work on text with FixItHints applied. 71 bool GetOriginalBodyBounds(std::string transformed_text, 72 size_t &start_loc, size_t &end_loc); 73 74 protected: 75 ClangExpressionSourceCode(llvm::StringRef filename, llvm::StringRef name, 76 llvm::StringRef prefix, llvm::StringRef body, 77 Wrapping wrap, WrapKind wrap_kind); 78 79 private: 80 void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp, 81 StreamString &stream, 82 const std::string &expr) const; 83 84 /// String marking the start of the user expression. 85 std::string m_start_marker; 86 /// String marking the end of the user expression. 87 std::string m_end_marker; 88 /// How the expression has been wrapped. 89 const WrapKind m_wrap_kind; 90 }; 91 92 } // namespace lldb_private 93 94 #endif 95