LLDB  mainline
ClangExpressionParser.h
Go to the documentation of this file.
1 //===-- ClangExpressionParser.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_CLANGEXPRESSIONPARSER_H
10 #define LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
11 
14 #include "lldb/Utility/ArchSpec.h"
15 #include "lldb/Utility/Status.h"
16 #include "lldb/lldb-public.h"
17 
18 #include <string>
19 #include <vector>
20 
21 namespace llvm {
22 class LLVMContext;
23 }
24 
25 namespace clang {
26 class CodeGenerator;
27 class CodeCompleteConsumer;
28 class CompilerInstance;
29 } // namespace clang
30 
31 namespace lldb_private {
32 
33 class IRExecutionUnit;
34 class TypeSystemClang;
35 
36 /// \class ClangExpressionParser ClangExpressionParser.h
37 /// "lldb/Expression/ClangExpressionParser.h" Encapsulates an instance of
38 /// Clang that can parse expressions.
39 ///
40 /// ClangExpressionParser is responsible for preparing an instance of
41 /// ClangExpression for execution. ClangExpressionParser uses ClangExpression
42 /// as a glorified parameter list, performing the required parsing and
43 /// conversion to formats (DWARF bytecode, or JIT compiled machine code) that
44 /// can be executed.
46 public:
47  /// Constructor
48  ///
49  /// Initializes class variables.
50  ///
51  /// \param[in] exe_scope
52  /// If non-NULL, an execution context scope that can help to
53  /// correctly create an expression with a valid process for
54  /// optional tuning Objective-C runtime support. Can be NULL.
55  ///
56  /// \param[in] expr
57  /// The expression to be parsed.
58  ///
59  /// @param[in] include_directories
60  /// List of include directories that should be used when parsing the
61  /// expression.
62  ///
63  /// @param[in] filename
64  /// Name of the source file that should be used when rendering
65  /// diagnostics (i.e. errors, warnings or notes from Clang).
67  bool generate_debug_info,
68  std::vector<std::string> include_directories = {},
69  std::string filename = "<clang expression>");
70 
71  /// Destructor
72  ~ClangExpressionParser() override;
73 
74  bool Complete(CompletionRequest &request, unsigned line, unsigned pos,
75  unsigned typed_pos) override;
76 
77  /// Parse a single expression and convert it to IR using Clang. Don't wrap
78  /// the expression in anything at all.
79  ///
80  /// \param[in] diagnostic_manager
81  /// The diagnostic manager to report errors to.
82  ///
83  /// \return
84  /// The number of errors encountered during parsing. 0 means
85  /// success.
86  unsigned Parse(DiagnosticManager &diagnostic_manager);
87 
88  bool RewriteExpression(DiagnosticManager &diagnostic_manager) override;
89 
90  /// Ready an already-parsed expression for execution, possibly evaluating it
91  /// statically.
92  ///
93  /// \param[out] func_addr
94  /// The address to which the function has been written.
95  ///
96  /// \param[out] func_end
97  /// The end of the function's allocated memory region. (func_addr
98  /// and func_end do not delimit an allocated region; the allocated
99  /// region may begin before func_addr.)
100  ///
101  /// \param[in] execution_unit_sp
102  /// After parsing, ownership of the execution unit for
103  /// for the expression is handed to this shared pointer.
104  ///
105  /// \param[in] exe_ctx
106  /// The execution context to write the function into.
107  ///
108  /// \param[in] execution_policy
109  /// Determines whether the expression must be JIT-compiled, must be
110  /// evaluated statically, or whether this decision may be made
111  /// opportunistically.
112  ///
113  /// \return
114  /// An error code indicating the success or failure of the operation.
115  /// Test with Success().
116  Status
117  PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end,
118  lldb::IRExecutionUnitSP &execution_unit_sp,
119  ExecutionContext &exe_ctx, bool &can_interpret,
120  lldb_private::ExecutionPolicy execution_policy) override;
121 
122  /// Run all static initializers for an execution unit.
123  ///
124  /// \param[in] execution_unit_sp
125  /// The execution unit.
126  ///
127  /// \param[in] exe_ctx
128  /// The execution context to use when running them. Thread can't be null.
129  ///
130  /// \return
131  /// The error code indicating the
132  Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp,
133  ExecutionContext &exe_ctx);
134 
135  /// Returns a string representing current ABI.
136  ///
137  /// \param[in] target_arch
138  /// The target architecture.
139  ///
140  /// \return
141  /// A string representing target ABI for the current architecture.
142  std::string GetClangTargetABI(const ArchSpec &target_arch);
143 
144 private:
145  /// Parses the expression.
146  ///
147  /// \param[in] diagnostic_manager
148  /// The diagnostic manager that should receive the diagnostics
149  /// from the parsing process.
150  ///
151  /// \param[in] completion
152  /// The completion consumer that should be used during parsing
153  /// (or a nullptr if no consumer should be attached).
154  ///
155  /// \param[in] completion_line
156  /// The line in which the completion marker should be placed.
157  /// The first line is represented by the value 0.
158  ///
159  /// \param[in] completion_column
160  /// The column in which the completion marker should be placed.
161  /// The first column is represented by the value 0.
162  ///
163  /// \return
164  /// The number of parsing errors.
165  unsigned ParseInternal(DiagnosticManager &diagnostic_manager,
166  clang::CodeCompleteConsumer *completion = nullptr,
167  unsigned completion_line = 0,
168  unsigned completion_column = 0);
169 
170  std::unique_ptr<llvm::LLVMContext>
171  m_llvm_context; ///< The LLVM context to generate IR into
172  std::unique_ptr<clang::CompilerInstance>
173  m_compiler; ///< The Clang compiler used to parse expressions into IR
174  std::unique_ptr<clang::CodeGenerator>
175  m_code_generator; ///< The Clang object that generates IR
176 
178  LLDBPreprocessorCallbacks *m_pp_callbacks; ///< Called when the preprocessor
179  ///encounters module imports
180  std::unique_ptr<TypeSystemClang> m_ast_context;
181 
182  std::vector<std::string> m_include_directories;
183  /// File name used for the user expression.
185 };
186 }
187 
188 #endif // LLDB_SOURCE_PLUGINS_EXPRESSIONPARSER_CLANG_CLANGEXPRESSIONPARSER_H
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb-public.h
llvm
Definition: Debugger.h:49
lldb_private::ClangExpressionParser::ParseInternal
unsigned ParseInternal(DiagnosticManager &diagnostic_manager, clang::CodeCompleteConsumer *completion=nullptr, unsigned completion_line=0, unsigned completion_column=0)
Parses the expression.
Definition: ClangExpressionParser.cpp:1035
lldb_private::ArchSpec
Definition: ArchSpec.h:33
lldb_private::ExecutionPolicy
ExecutionPolicy
Expression execution policies.
Definition: lldb-private-enumerations.h:144
lldb_private::ClangExpressionParser::m_filename
std::string m_filename
File name used for the user expression.
Definition: ClangExpressionParser.h:184
lldb_private::ClangExpressionParser::m_pp_callbacks
LLDBPreprocessorCallbacks * m_pp_callbacks
Called when the preprocessor encounters module imports.
Definition: ClangExpressionParser.h:177
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::ClangExpressionParser::m_ast_context
std::unique_ptr< TypeSystemClang > m_ast_context
Definition: ClangExpressionParser.h:180
lldb_private::ClangExpressionParser::m_code_generator
std::unique_ptr< clang::CodeGenerator > m_code_generator
The Clang object that generates IR.
Definition: ClangExpressionParser.h:175
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ClangExpressionParser::PrepareForExecution
Status PrepareForExecution(lldb::addr_t &func_addr, lldb::addr_t &func_end, lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx, bool &can_interpret, lldb_private::ExecutionPolicy execution_policy) override
Ready an already-parsed expression for execution, possibly evaluating it statically.
Definition: ClangExpressionParser.cpp:1321
lldb_private::ClangExpressionParser::GetClangTargetABI
std::string GetClangTargetABI(const ArchSpec &target_arch)
Returns a string representing current ABI.
Definition: ClangExpressionParser.cpp:1205
lldb_private::ClangExpressionParser::LLDBPreprocessorCallbacks
Definition: ClangExpressionParser.cpp:107
lldb_private::ClangExpressionParser::Parse
unsigned Parse(DiagnosticManager &diagnostic_manager)
Parse a single expression and convert it to IR using Clang.
Definition: ClangExpressionParser.cpp:1030
lldb_private::ClangExpressionParser::RewriteExpression
bool RewriteExpression(DiagnosticManager &diagnostic_manager) override
Try to use the FixIts in the diagnostic_manager to rewrite the expression.
Definition: ClangExpressionParser.cpp:1248
lldb_private::ExpressionParser
Definition: ExpressionParser.h:26
include_directories
include_directories(.) set(lldbBase_SOURCES lldb.cpp) find_first_existing_vc_file("$
Definition: CMakeLists.txt:1
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::ClangExpressionParser::Complete
bool Complete(CompletionRequest &request, unsigned line, unsigned pos, unsigned typed_pos) override
Attempts to find possible command line completions for the given expression.
Definition: ClangExpressionParser.cpp:1010
lldb_private::ClangExpressionParser::m_include_directories
std::vector< std::string > m_include_directories
Definition: ClangExpressionParser.h:182
lldb_private::ClangExpressionParser::m_llvm_context
std::unique_ptr< llvm::LLVMContext > m_llvm_context
The LLVM context to generate IR into.
Definition: ClangExpressionParser.h:171
lldb_private::Status
Definition: Status.h:44
lldb_private::ClangExpressionParser
Definition: ClangExpressionParser.h:45
lldb_private::DiagnosticManager
Definition: DiagnosticManager.h:93
ArchSpec.h
clang
Definition: ASTResultSynthesizer.h:15
DiagnosticManager.h
lldb_private::ClangExpressionParser::m_compiler
std::unique_ptr< clang::CompilerInstance > m_compiler
The Clang compiler used to parse expressions into IR.
Definition: ClangExpressionParser.h:173
Status.h
lldb_private::ClangExpressionParser::~ClangExpressionParser
~ClangExpressionParser() override
Destructor.
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::ClangExpressionParser::ClangExpressionParser
ClangExpressionParser(ExecutionContextScope *exe_scope, Expression &expr, bool generate_debug_info, std::vector< std::string > include_directories={}, std::string filename="<clang expression>")
Constructor.
Definition: ClangExpressionParser.cpp:359
ExpressionParser.h
lldb_private::ClangExpressionParser::RunStaticInitializers
Status RunStaticInitializers(lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx)
Run all static initializers for an execution unit.
Definition: ClangExpressionParser.cpp:1500
lldb_private::Expression
Definition: Expression.h:33
lldb_private::CompletionRequest
Definition: CompletionRequest.h:100