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#include "llvm/Support/ExtensibleRTTI.h"
19
20namespace lldb_private {
21
22class REPL : public IOHandlerDelegate,
23 public llvm::RTTIExtends<REPL, llvm::RTTIRoot> {
24public:
25 /// LLVM RTTI support
26 static char ID;
27
28 REPL(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.
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
78
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 llvm::StringRef 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
109protected:
110 /// Method that can be optionally overriden by subclasses to get notified
111 /// whenever an expression has been evaluated. The params of this method
112 /// include the inputs and outputs of the expression evaluation.
113 ///
114 /// Note: meta commands that start with : are not covered by this method.
115 ///
116 /// \return
117 /// An \a Error object that, if it is a failure, aborts the regular
118 /// REPL expression result handling.
119 virtual llvm::Error
120 OnExpressionEvaluated(const ExecutionContext &exe_ctx, llvm::StringRef code,
121 const EvaluateExpressionOptions &expr_options,
122 lldb::ExpressionResults execution_results,
123 const lldb::ValueObjectSP &result_valobj_sp,
124 const Status &error) {
125 return llvm::Error::success();
126 }
127
128 static int CalculateActualIndentation(const StringList &lines);
129
130 // Subclasses should override these functions to implement a functional REPL.
131
133
134 virtual llvm::StringRef GetSourceFileBasename() = 0;
135
136 virtual const char *GetAutoIndentCharacters() = 0;
137
138 virtual bool SourceIsComplete(const std::string &source) = 0;
139
141 const StringList &lines, int cursor_position,
142 int tab_size) = 0; // LLDB_INVALID_OFFSET means no change
143
145
146 virtual bool PrintOneVariable(Debugger &debugger,
147 lldb::StreamFileSP &output_sp,
148 lldb::ValueObjectSP &valobj_sp,
149 ExpressionVariable *var = nullptr) = 0;
150
151 virtual void CompleteCode(const std::string &current_code,
152 CompletionRequest &request) = 0;
153
158
160 std::string m_indent_str; // Use this string for each level of indentation
163
166
167 StringList m_code; // All accumulated REPL statements are saved here
168
171
172private:
173 std::string GetSourcePath();
174};
175
176} // namespace lldb_private
177
178#endif // LLDB_EXPRESSION_REPL_H
static llvm::raw_ostream & error(Stream &strm)
"lldb/Utility/ArgCompletionRequest.h"
A class to manage flag bits.
Definition: Debugger.h:80
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
A delegate class for use with IOHandler subclasses.
Definition: IOHandler.h:190
const char * IOHandlerGetHelpPrologue() override
Definition: REPL.cpp:129
static int CalculateActualIndentation(const StringList &lines)
Definition: REPL.cpp:155
static char ID
LLVM RTTI support.
Definition: REPL.h:26
virtual Status DoInitialization()=0
llvm::StringRef IOHandlerGetControlSequence(char ch) override
Definition: REPL.cpp:120
virtual lldb::offset_t GetDesiredIndentation(const StringList &lines, int cursor_position, int tab_size)=0
const char * IOHandlerGetFixIndentationCharacters() override
Definition: REPL.cpp:116
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:168
virtual const char * GetAutoIndentCharacters()=0
OptionGroupValueObjectDisplay m_varobj_options
Definition: REPL.h:155
uint32_t m_current_indent_level
Definition: REPL.h:162
bool IOHandlerInterrupt(IOHandler &io_handler) override
Definition: REPL.cpp:111
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:38
std::string m_current_indent_str
Definition: REPL.h:161
virtual llvm::StringRef GetSourceFileBasename()=0
std::string m_indent_str
Definition: REPL.h:160
virtual bool PrintOneVariable(Debugger &debugger, lldb::StreamFileSP &output_sp, lldb::ValueObjectSP &valobj_sp, ExpressionVariable *var=nullptr)=0
void IOHandlerComplete(IOHandler &io_handler, CompletionRequest &request) override
Definition: REPL.cpp:497
OptionGroupFormat m_format_options
Definition: REPL.h:154
EvaluateExpressionOptions m_expr_options
Definition: REPL.h:156
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:141
std::string GetSourcePath()
Definition: REPL.cpp:59
bool m_enable_auto_indent
Definition: REPL.h:159
virtual bool SourceIsComplete(const std::string &source)=0
const char * IOHandlerGetCommandPrefix() override
Definition: REPL.cpp:127
Status RunLoop()
Definition: REPL.cpp:560
void IOHandlerInputInterrupted(IOHandler &io_handler, std::string &line) override
Definition: REPL.cpp:113
void IOHandlerActivated(IOHandler &io_handler, bool interactive) override
Definition: REPL.cpp:102
virtual lldb::LanguageType GetLanguage()=0
void IOHandlerInputComplete(IOHandler &io_handler, std::string &line) override
Called when a line or lines have been retrieved.
Definition: REPL.cpp:221
lldb::IOHandlerSP m_io_handler_sp
Definition: REPL.h:170
void SetEvaluateOptions(const EvaluateExpressionOptions &options)
Definition: REPL.h:68
void SetValueObjectDisplayOptions(const OptionGroupValueObjectDisplay &options)
Definition: REPL.h:64
std::string m_compiler_options
Definition: REPL.h:157
StringList m_code
Definition: REPL.h:167
std::string m_repl_source_path
Definition: REPL.h:164
lldb::IOHandlerSP GetIOHandler()
Definition: REPL.cpp:73
bool m_dedicated_repl_mode
Definition: REPL.h:165
virtual void CompleteCode(const std::string &current_code, CompletionRequest &request)=0
void SetFormatOptions(const OptionGroupFormat &options)
Definition: REPL.h:59
void SetCompilerOptions(const char *options)
Definition: REPL.h:72
Target & m_target
Definition: REPL.h:169
virtual llvm::Error OnExpressionEvaluated(const ExecutionContext &exe_ctx, llvm::StringRef code, const EvaluateExpressionOptions &expr_options, lldb::ExpressionResults execution_results, const lldb::ValueObjectSP &result_valobj_sp, const Status &error)
Method that can be optionally overriden by subclasses to get notified whenever an expression has been...
Definition: REPL.h:120
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
std::shared_ptr< lldb_private::IOHandler > IOHandlerSP
Definition: lldb-forward.h:353
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:472
uint64_t offset_t
Definition: lldb-types.h:85
LanguageType
Programming language type.
ExpressionResults
The results of expression evaluation.
std::shared_ptr< lldb_private::StreamFile > StreamFileSP
Definition: lldb-forward.h:421
std::shared_ptr< lldb_private::REPL > REPLSP
Definition: lldb-forward.h:393