1 //===-- ClangExpressionSourceCode.h -----------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONSOURCECODE_H
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"
20 namespace lldb_private
{
22 class ExecutionContext
;
24 class ClangExpressionSourceCode
: public ExpressionSourceCode
{
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
;
32 /// The possible ways an expression can be wrapped.
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.
45 static ClangExpressionSourceCode
*CreateWrapped(llvm::StringRef filename
,
46 llvm::StringRef prefix
,
49 return new ClangExpressionSourceCode(filename
, "$__lldb_expr", prefix
, body
,
53 /// Generates the source code that will evaluate the expression.
55 /// \param text output parameter containing the source code string.
56 /// \param exe_ctx The execution context in which the expression will be
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.
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;
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
);
76 ClangExpressionSourceCode(llvm::StringRef filename
, llvm::StringRef name
,
77 llvm::StringRef prefix
, llvm::StringRef body
,
78 Wrapping wrap
, WrapKind wrap_kind
);
81 /// Writes "using" declarations for local variables into the specified stream.
83 /// Behaviour is undefined if 'frame == nullptr'.
85 /// \param[out] stream Stream that this function generates "using"
86 /// declarations into.
88 /// \param[in] expr Expression source that we're evaluating.
90 /// \param[in] frame StackFrame which carries information about the local
91 /// variables that we're generating "using" declarations for.
92 void AddLocalVariableDecls(StreamString
&stream
, const std::string
&expr
,
93 StackFrame
*frame
) const;
95 /// String marking the start of the user expression.
96 std::string m_start_marker
;
97 /// String marking the end of the user expression.
98 std::string m_end_marker
;
99 /// How the expression has been wrapped.
100 const WrapKind m_wrap_kind
;
103 } // namespace lldb_private