LLDB  mainline
REPL.h
Go to the documentation of this file.
1 //===-- REPL.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_EXPRESSION_REPL_H
10 #define LLDB_EXPRESSION_REPL_H
11 
12 #include <string>
13 
14 #include "lldb/Core/IOHandler.h"
17 #include "lldb/Target/Target.h"
18 
19 namespace lldb_private {
20 
21 class REPL : public IOHandlerDelegate {
22 public:
23  // See TypeSystem.h for how to add subclasses to this.
25 
26  LLVMCastKind getKind() const { return m_kind; }
27 
28  REPL(LLVMCastKind kind, Target &target);
29 
30  ~REPL() override;
31 
32  /// Get a REPL with an existing target (or, failing that, a debugger to use),
33  /// and (optional) extra arguments for the compiler.
34  ///
35  /// \param[out] Status
36  /// If this language is supported but the REPL couldn't be created, this
37  /// error is populated with the reason.
38  ///
39  /// \param[in] language
40  /// The language to create a REPL for.
41  ///
42  /// \param[in] debugger
43  /// If provided, and target is nullptr, the debugger to use when setting
44  /// up a top-level REPL.
45  ///
46  /// \param[in] target
47  /// If provided, the target to put the REPL inside.
48  ///
49  /// \param[in] repl_options
50  /// If provided, additional options for the compiler when parsing REPL
51  /// expressions.
52  ///
53  /// \return
54  /// The range of the containing object in the target process.
55  static lldb::REPLSP Create(Status &Status, lldb::LanguageType language,
56  Debugger *debugger, Target *target,
57  const char *repl_options);
58 
59  void SetFormatOptions(const OptionGroupFormat &options) {
60  m_format_options = options;
61  }
62 
63  void
65  m_varobj_options = options;
66  }
67 
69  m_expr_options = options;
70  }
71 
72  void SetCompilerOptions(const char *options) {
73  if (options)
74  m_compiler_options = options;
75  }
76 
77  lldb::IOHandlerSP GetIOHandler();
78 
79  Status RunLoop();
80 
81  // IOHandler::Delegate functions
82  void IOHandlerActivated(IOHandler &io_handler, bool interactive) override;
83 
84  bool IOHandlerInterrupt(IOHandler &io_handler) override;
85 
86  void IOHandlerInputInterrupted(IOHandler &io_handler,
87  std::string &line) override;
88 
89  const char *IOHandlerGetFixIndentationCharacters() override;
90 
91  ConstString IOHandlerGetControlSequence(char ch) override;
92 
93  const char *IOHandlerGetCommandPrefix() override;
94 
95  const char *IOHandlerGetHelpPrologue() override;
96 
97  bool IOHandlerIsInputComplete(IOHandler &io_handler,
98  StringList &lines) override;
99 
100  int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines,
101  int cursor_position) override;
102 
103  void IOHandlerInputComplete(IOHandler &io_handler,
104  std::string &line) override;
105 
106  void IOHandlerComplete(IOHandler &io_handler,
107  CompletionRequest &request) override;
108 
109 protected:
110  static int CalculateActualIndentation(const StringList &lines);
111 
112  // Subclasses should override these functions to implement a functional REPL.
113 
114  virtual Status DoInitialization() = 0;
115 
116  virtual ConstString GetSourceFileBasename() = 0;
117 
118  virtual const char *GetAutoIndentCharacters() = 0;
119 
120  virtual bool SourceIsComplete(const std::string &source) = 0;
121 
123  const StringList &lines, int cursor_position,
124  int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
125 
126  virtual lldb::LanguageType GetLanguage() = 0;
127 
128  virtual bool PrintOneVariable(Debugger &debugger,
129  lldb::StreamFileSP &output_sp,
130  lldb::ValueObjectSP &valobj_sp,
131  ExpressionVariable *var = nullptr) = 0;
132 
133  virtual void CompleteCode(const std::string &current_code,
134  CompletionRequest &request) = 0;
135 
140 
141  bool m_enable_auto_indent = true;
142  std::string m_indent_str; // Use this string for each level of indentation
145 
147  bool m_dedicated_repl_mode = false;
148 
149  StringList m_code; // All accumulated REPL statements are saved here
150 
152  lldb::IOHandlerSP m_io_handler_sp;
154 
155 private:
157 };
158 
159 } // namespace lldb_private
160 
161 #endif // LLDB_EXPRESSION_REPL_H
lldb_private::REPL::m_dedicated_repl_mode
bool m_dedicated_repl_mode
Definition: REPL.h:147
OptionGroupFormat.h
lldb_private::REPL
Definition: REPL.h:21
lldb_private::REPL::SetEvaluateOptions
void SetEvaluateOptions(const EvaluateExpressionOptions &options)
Definition: REPL.h:68
lldb_private::REPL::m_format_options
OptionGroupFormat m_format_options
Definition: REPL.h:136
IOHandler.h
lldb_private::REPL::GetIOHandler
lldb::IOHandlerSP GetIOHandler()
Definition: REPL.cpp:66
lldb_private::REPL::IOHandlerFixIndentation
int IOHandlerFixIndentation(IOHandler &io_handler, const StringList &lines, int cursor_position) override
Called when a new line is created or one of an identified set of indentation characters is typed.
Definition: REPL.cpp:160
lldb_private::REPL::m_expr_options
EvaluateExpressionOptions m_expr_options
Definition: REPL.h:138
lldb_private::REPL::GetAutoIndentCharacters
virtual const char * GetAutoIndentCharacters()=0
lldb_private::REPL::m_current_indent_str
std::string m_current_indent_str
Definition: REPL.h:143
lldb_private::EvaluateExpressionOptions
Definition: Target.h:259
lldb::LanguageType
LanguageType
Programming language type.
Definition: lldb-enumerations.h:436
lldb_private::REPL::IOHandlerComplete
void IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) override
Definition: REPL.cpp:486
lldb_private::REPL::m_code
StringList m_code
Definition: REPL.h:149
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::REPL::IOHandlerGetControlSequence
ConstString IOHandlerGetControlSequence(char ch) override
Definition: REPL.cpp:113
lldb_private::REPL::m_enable_auto_indent
bool m_enable_auto_indent
Definition: REPL.h:141
lldb_private::Target
Definition: Target.h:451
lldb_private::REPL::IOHandlerGetHelpPrologue
const char * IOHandlerGetHelpPrologue() override
Definition: REPL.cpp:121
lldb_private::REPL::GetSourcePath
std::string GetSourcePath()
Definition: REPL.cpp:52
lldb_private::REPL::SetCompilerOptions
void SetCompilerOptions(const char *options)
Definition: REPL.h:72
lldb_private::REPL::IOHandlerInputInterrupted
void IOHandlerInputInterrupted(IOHandler &io_handler, std::string &line) override
Definition: REPL.cpp:106
Target.h
OptionGroupValueObjectDisplay.h
lldb_private::REPL::m_target
Target & m_target
Definition: REPL.h:151
lldb_private::REPL::m_indent_str
std::string m_indent_str
Definition: REPL.h:142
lldb_private::REPL::m_io_handler_sp
lldb::IOHandlerSP m_io_handler_sp
Definition: REPL.h:152
lldb_private::REPL::~REPL
~REPL() override
lldb_private::OptionGroupFormat
Definition: OptionGroupFormat.h:24
lldb_private::REPL::m_varobj_options
OptionGroupValueObjectDisplay m_varobj_options
Definition: REPL.h:137
lldb_private::REPL::Create
static lldb::REPLSP Create(Status &Status, lldb::LanguageType language, Debugger *debugger, Target *target, const char *repl_options)
Get a REPL with an existing target (or, failing that, a debugger to use), and (optional) extra argume...
Definition: REPL.cpp:35
lldb_private::StringList
Definition: StringList.h:25
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::IOHandler
Definition: IOHandler.h:46
lldb_private::REPL::REPL
REPL(LLVMCastKind kind, Target &target)
Definition: REPL.cpp:25
lldb_private::Debugger
Definition: Debugger.h:70
lldb_private::REPL::PrintOneVariable
virtual bool PrintOneVariable(Debugger &debugger, lldb::StreamFileSP &output_sp, lldb::ValueObjectSP &valobj_sp, ExpressionVariable *var=nullptr)=0
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::OptionGroupValueObjectDisplay
Definition: OptionGroupValueObjectDisplay.h:19
lldb_private::REPL::eKindClang
@ eKindClang
Definition: REPL.h:24
lldb_private::REPL::GetDesiredIndentation
virtual lldb::offset_t GetDesiredIndentation(const StringList &lines, int cursor_position, int tab_size)=0
lldb_private::REPL::IOHandlerInputComplete
void IOHandlerInputComplete(IOHandler &io_handler, std::string &line) override
Called when a line or lines have been retrieved.
Definition: REPL.cpp:213
lldb_private::REPL::LLVMCastKind
LLVMCastKind
Definition: REPL.h:24
lldb_private::REPL::GetSourceFileBasename
virtual ConstString GetSourceFileBasename()=0
lldb_private::REPL::eKindGo
@ eKindGo
Definition: REPL.h:24
lldb_private::REPL::GetLanguage
virtual lldb::LanguageType GetLanguage()=0
lldb_private::REPL::eKindSwift
@ eKindSwift
Definition: REPL.h:24
lldb_private::REPL::SourceIsComplete
virtual bool SourceIsComplete(const std::string &source)=0
lldb_private::ExpressionVariable
Definition: ExpressionVariable.h:23
lldb_private::REPL::IOHandlerInterrupt
bool IOHandlerInterrupt(IOHandler &io_handler) override
Definition: REPL.cpp:104
lldb_private::Status
Definition: Status.h:44
lldb_private::REPL::IOHandlerActivated
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
Definition: REPL.cpp:95
uint32_t
lldb_private::REPL::CalculateActualIndentation
static int CalculateActualIndentation(const StringList &lines)
Definition: REPL.cpp:147
lldb_private::REPL::CompleteCode
virtual void CompleteCode(const std::string &current_code, CompletionRequest &request)=0
lldb_private::REPL::SetFormatOptions
void SetFormatOptions(const OptionGroupFormat &options)
Definition: REPL.h:59
lldb_private::REPL::m_current_indent_level
uint32_t m_current_indent_level
Definition: REPL.h:144
lldb_private::IOHandlerDelegate
A delegate class for use with IOHandler subclasses.
Definition: IOHandler.h:195
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::REPL::IOHandlerGetFixIndentationCharacters
const char * IOHandlerGetFixIndentationCharacters() override
Definition: REPL.cpp:109
lldb_private::REPL::getKind
LLVMCastKind getKind() const
Definition: REPL.h:26
lldb_private::REPL::SetValueObjectDisplayOptions
void SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options)
Definition: REPL.h:64
lldb_private::REPL::RunLoop
Status RunLoop()
Definition: REPL.cpp:551
lldb_private::REPL::m_kind
LLVMCastKind m_kind
Definition: REPL.h:153
lldb_private::REPL::IOHandlerIsInputComplete
bool IOHandlerIsInputComplete(IOHandler &io_handler, StringList &lines) override
Called to determine whether typing enter after the last line in lines should end input.
Definition: REPL.cpp:133
lldb::eFormatDefault
@ eFormatDefault
Definition: lldb-enumerations.h:156
lldb_private::REPL::DoInitialization
virtual Status DoInitialization()=0
lldb_private::REPL::m_repl_source_path
std::string m_repl_source_path
Definition: REPL.h:146
lldb_private::REPL::IOHandlerGetCommandPrefix
const char * IOHandlerGetCommandPrefix() override
Definition: REPL.cpp:119
lldb_private::REPL::kNumKinds
@ kNumKinds
Definition: REPL.h:24
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100
lldb_private::REPL::m_compiler_options
std::string m_compiler_options
Definition: REPL.h:139