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