Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 1 | //===-- ClangExpressionParser.cpp -------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 10 | // C Includes |
| 11 | // C++ Includes |
| 12 | // Other libraries and framework includes |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTDiagnostic.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExternalASTSource.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 16 | #include "clang/Basic/DiagnosticIDs.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Jim Ingham | 151c032 | 2015-09-15 21:13:50 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | #include "clang/Basic/Version.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 21 | #include "clang/CodeGen/CodeGenAction.h" |
| 22 | #include "clang/CodeGen/ModuleBuilder.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 23 | #include "clang/Edit/Commit.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 24 | #include "clang/Edit/EditedSource.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | #include "clang/Edit/EditsReceiver.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/CompilerInstance.h" |
| 27 | #include "clang/Frontend/CompilerInvocation.h" |
| 28 | #include "clang/Frontend/FrontendActions.h" |
| 29 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 30 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 31 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 32 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Preprocessor.h" |
Sean Callanan | e2ef6e3 | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 34 | #include "clang/Parse/ParseAST.h" |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 35 | #include "clang/Rewrite/Core/Rewriter.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | #include "clang/Rewrite/Frontend/FrontendActions.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 37 | #include "clang/Sema/SemaConsumer.h" |
| 38 | |
| 39 | #include "llvm/ADT/StringRef.h" |
| 40 | #include "llvm/ExecutionEngine/ExecutionEngine.h" |
Sean Callanan | 3d654b3 | 2012-09-24 22:25:51 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Rafael Espindola | 4609ea8 | 2013-06-26 14:10:56 +0000 | [diff] [blame] | 42 | #include "llvm/Support/FileSystem.h" |
Sean Callanan | 880e680 | 2011-10-07 23:18:13 +0000 | [diff] [blame] | 43 | #include "llvm/Support/TargetSelect.h" |
Greg Clayton | 70b5765 | 2011-05-15 01:25:55 +0000 | [diff] [blame] | 44 | |
Greg Clayton | c9a078a | 2016-03-24 23:50:03 +0000 | [diff] [blame] | 45 | #pragma clang diagnostic push |
| 46 | #pragma clang diagnostic ignored "-Wglobal-constructors" |
Ed Maste | a09ed03 | 2014-03-18 18:55:06 +0000 | [diff] [blame] | 47 | #include "llvm/ExecutionEngine/MCJIT.h" |
Greg Clayton | c9a078a | 2016-03-24 23:50:03 +0000 | [diff] [blame] | 48 | #pragma clang diagnostic pop |
| 49 | |
Chandler Carruth | 1e15758 | 2013-01-02 12:20:07 +0000 | [diff] [blame] | 50 | #include "llvm/IR/LLVMContext.h" |
| 51 | #include "llvm/IR/Module.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 52 | #include "llvm/Support/DynamicLibrary.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ErrorHandling.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 54 | #include "llvm/Support/Host.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | #include "llvm/Support/MemoryBuffer.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 56 | #include "llvm/Support/Signals.h" |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 57 | |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 58 | // Project includes |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 59 | #include "ClangDiagnostic.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | #include "ClangExpressionParser.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 61 | |
| 62 | #include "ClangASTSource.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 63 | #include "ClangExpressionDeclMap.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | #include "ClangExpressionHelper.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 65 | #include "ClangModulesDeclVendor.h" |
| 66 | #include "ClangPersistentVariables.h" |
| 67 | #include "IRForTarget.h" |
| 68 | |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 69 | #include "lldb/Core/Debugger.h" |
| 70 | #include "lldb/Core/Disassembler.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 71 | #include "lldb/Core/Module.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 72 | #include "lldb/Core/StreamFile.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 73 | #include "lldb/Expression/IRDynamicChecks.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 74 | #include "lldb/Expression/IRExecutionUnit.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 75 | #include "lldb/Expression/IRInterpreter.h" |
| 76 | #include "lldb/Host/File.h" |
| 77 | #include "lldb/Host/HostInfo.h" |
| 78 | #include "lldb/Symbol/ClangASTContext.h" |
| 79 | #include "lldb/Symbol/SymbolVendor.h" |
| 80 | #include "lldb/Target/ExecutionContext.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 81 | #include "lldb/Target/Language.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 82 | #include "lldb/Target/ObjCLanguageRuntime.h" |
| 83 | #include "lldb/Target/Process.h" |
| 84 | #include "lldb/Target/Target.h" |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 85 | #include "lldb/Target/ThreadPlanCallFunction.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 86 | #include "lldb/Utility/DataBufferHeap.h" |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 87 | #include "lldb/Utility/LLDBAssert.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 88 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 89 | #include "lldb/Utility/Stream.h" |
| 90 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 573ab90 | 2017-03-21 18:25:04 +0000 | [diff] [blame] | 91 | #include "lldb/Utility/StringList.h" |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 92 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 93 | using namespace clang; |
| 94 | using namespace llvm; |
| 95 | using namespace lldb_private; |
| 96 | |
| 97 | //===----------------------------------------------------------------------===// |
| 98 | // Utility Methods for Clang |
| 99 | //===----------------------------------------------------------------------===// |
| 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | class ClangExpressionParser::LLDBPreprocessorCallbacks : public PPCallbacks { |
| 102 | ClangModulesDeclVendor &m_decl_vendor; |
| 103 | ClangPersistentVariables &m_persistent_vars; |
| 104 | StreamString m_error_stream; |
| 105 | bool m_has_errors = false; |
Eugene Zelenko | 4c3f2b9 | 2015-10-21 01:03:30 +0000 | [diff] [blame] | 106 | |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 107 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 108 | LLDBPreprocessorCallbacks(ClangModulesDeclVendor &decl_vendor, |
| 109 | ClangPersistentVariables &persistent_vars) |
| 110 | : m_decl_vendor(decl_vendor), m_persistent_vars(persistent_vars) {} |
| 111 | |
| 112 | void moduleImport(SourceLocation import_location, clang::ModuleIdPath path, |
| 113 | const clang::Module * /*null*/) override { |
| 114 | std::vector<ConstString> string_path; |
| 115 | |
| 116 | for (const std::pair<IdentifierInfo *, SourceLocation> &component : path) { |
| 117 | string_path.push_back(ConstString(component.first->getName())); |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 118 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | |
| 120 | StreamString error_stream; |
| 121 | |
| 122 | ClangModulesDeclVendor::ModuleVector exported_modules; |
| 123 | |
| 124 | if (!m_decl_vendor.AddModule(string_path, &exported_modules, |
| 125 | m_error_stream)) { |
| 126 | m_has_errors = true; |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 127 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | |
| 129 | for (ClangModulesDeclVendor::ModuleID module : exported_modules) { |
| 130 | m_persistent_vars.AddHandLoadedClangModule(module); |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 131 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | bool hasErrors() { return m_has_errors; } |
| 135 | |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 136 | llvm::StringRef getErrorString() { return m_error_stream.GetString(); } |
Sean Callanan | c8278af | 2014-12-05 01:27:35 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | class ClangDiagnosticManagerAdapter : public clang::DiagnosticConsumer { |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 140 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | ClangDiagnosticManagerAdapter() |
| 142 | : m_passthrough(new clang::TextDiagnosticBuffer) {} |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | ClangDiagnosticManagerAdapter( |
| 145 | const std::shared_ptr<clang::TextDiagnosticBuffer> &passthrough) |
| 146 | : m_passthrough(passthrough) {} |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | void ResetManager(DiagnosticManager *manager = nullptr) { |
| 149 | m_manager = manager; |
| 150 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 152 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 153 | const clang::Diagnostic &Info) { |
| 154 | if (m_manager) { |
| 155 | llvm::SmallVector<char, 32> diag_str; |
| 156 | Info.FormatDiagnostic(diag_str); |
| 157 | diag_str.push_back('\0'); |
| 158 | const char *data = diag_str.data(); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | lldb_private::DiagnosticSeverity severity; |
| 161 | bool make_new_diagnostic = true; |
| 162 | |
| 163 | switch (DiagLevel) { |
| 164 | case DiagnosticsEngine::Level::Fatal: |
| 165 | case DiagnosticsEngine::Level::Error: |
| 166 | severity = eDiagnosticSeverityError; |
| 167 | break; |
| 168 | case DiagnosticsEngine::Level::Warning: |
| 169 | severity = eDiagnosticSeverityWarning; |
| 170 | break; |
| 171 | case DiagnosticsEngine::Level::Remark: |
| 172 | case DiagnosticsEngine::Level::Ignored: |
| 173 | severity = eDiagnosticSeverityRemark; |
| 174 | break; |
| 175 | case DiagnosticsEngine::Level::Note: |
| 176 | m_manager->AppendMessageToDiagnostic(data); |
| 177 | make_new_diagnostic = false; |
| 178 | } |
| 179 | if (make_new_diagnostic) { |
| 180 | ClangDiagnostic *new_diagnostic = |
| 181 | new ClangDiagnostic(data, severity, Info.getID()); |
| 182 | m_manager->AddDiagnostic(new_diagnostic); |
| 183 | |
| 184 | // Don't store away warning fixits, since the compiler doesn't have |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 185 | // enough context in an expression for the warning to be useful. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | // FIXME: Should we try to filter out FixIts that apply to our generated |
| 187 | // code, and not the user's expression? |
| 188 | if (severity == eDiagnosticSeverityError) { |
| 189 | size_t num_fixit_hints = Info.getNumFixItHints(); |
| 190 | for (size_t i = 0; i < num_fixit_hints; i++) { |
| 191 | const clang::FixItHint &fixit = Info.getFixItHint(i); |
| 192 | if (!fixit.isNull()) |
| 193 | new_diagnostic->AddFixitHint(fixit); |
| 194 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 195 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | m_passthrough->HandleDiagnostic(DiagLevel, Info); |
| 200 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | void FlushDiagnostics(DiagnosticsEngine &Diags) { |
| 203 | m_passthrough->FlushDiagnostics(Diags); |
| 204 | } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const { |
| 207 | return new ClangDiagnosticManagerAdapter(m_passthrough); |
| 208 | } |
| 209 | |
| 210 | clang::TextDiagnosticBuffer *GetPassthrough() { return m_passthrough.get(); } |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 211 | |
| 212 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | DiagnosticManager *m_manager = nullptr; |
| 214 | std::shared_ptr<clang::TextDiagnosticBuffer> m_passthrough; |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 217 | //===----------------------------------------------------------------------===// |
| 218 | // Implementation of ClangExpressionParser |
| 219 | //===----------------------------------------------------------------------===// |
| 220 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 221 | ClangExpressionParser::ClangExpressionParser(ExecutionContextScope *exe_scope, |
| 222 | Expression &expr, |
| 223 | bool generate_debug_info) |
| 224 | : ExpressionParser(exe_scope, expr, generate_debug_info), m_compiler(), |
| 225 | m_code_generator(), m_pp_callbacks(nullptr) { |
| 226 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 227 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 228 | // We can't compile expressions without a target. So if the exe_scope is |
| 229 | // null or doesn't have a target, then we just need to get out of here. I'll |
| 230 | // lldb_assert and not make any of the compiler objects since |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | // I can't return errors directly from the constructor. Further calls will |
| 232 | // check if the compiler was made and |
| 233 | // bag out if it wasn't. |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | if (!exe_scope) { |
| 236 | lldb_assert(exe_scope, "Can't make an expression parser with a null scope.", |
| 237 | __FUNCTION__, __FILE__, __LINE__); |
| 238 | return; |
| 239 | } |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | lldb::TargetSP target_sp; |
| 242 | target_sp = exe_scope->CalculateTarget(); |
| 243 | if (!target_sp) { |
| 244 | lldb_assert(target_sp.get(), |
| 245 | "Can't make an expression parser with a null target.", |
| 246 | __FUNCTION__, __FILE__, __LINE__); |
| 247 | return; |
| 248 | } |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 249 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 250 | // 1. Create a new compiler instance. |
| 251 | m_compiler.reset(new CompilerInstance()); |
| 252 | lldb::LanguageType frame_lang = |
| 253 | expr.Language(); // defaults to lldb::eLanguageTypeUnknown |
| 254 | bool overridden_target_opts = false; |
| 255 | lldb_private::LanguageRuntime *lang_rt = nullptr; |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | std::string abi; |
| 258 | ArchSpec target_arch; |
| 259 | target_arch = target_sp->GetArchitecture(); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 260 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 261 | const auto target_machine = target_arch.GetMachine(); |
Jason Molenda | a332978 | 2014-03-29 18:54:20 +0000 | [diff] [blame] | 262 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 263 | // If the expression is being evaluated in the context of an existing stack |
| 264 | // frame, we introspect to see if the language runtime is available. |
Bhushan D. Attarde | 3592a6e | 2016-02-18 11:53:28 +0000 | [diff] [blame] | 265 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 266 | lldb::StackFrameSP frame_sp = exe_scope->CalculateStackFrame(); |
| 267 | lldb::ProcessSP process_sp = exe_scope->CalculateProcess(); |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 268 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 269 | // Make sure the user hasn't provided a preferred execution language with |
| 270 | // `expression --language X -- ...` |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 271 | if (frame_sp && frame_lang == lldb::eLanguageTypeUnknown) |
| 272 | frame_lang = frame_sp->GetLanguage(); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 273 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | if (process_sp && frame_lang != lldb::eLanguageTypeUnknown) { |
| 275 | lang_rt = process_sp->GetLanguageRuntime(frame_lang); |
Aidan Dodds | 1b6785a | 2016-02-03 12:33:05 +0000 | [diff] [blame] | 276 | if (log) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | log->Printf("Frame has language of type %s", |
| 278 | Language::GetNameForLanguageType(frame_lang)); |
| 279 | } |
Alp Toker | 5f83864 | 2014-07-06 05:36:57 +0000 | [diff] [blame] | 280 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 281 | // 2. Configure the compiler with a set of default options that are |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 282 | // appropriate for most situations. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 283 | if (target_arch.IsValid()) { |
| 284 | std::string triple = target_arch.GetTriple().str(); |
| 285 | m_compiler->getTargetOpts().Triple = triple; |
| 286 | if (log) |
| 287 | log->Printf("Using %s as the target triple", |
| 288 | m_compiler->getTargetOpts().Triple.c_str()); |
| 289 | } else { |
| 290 | // If we get here we don't have a valid target and just have to guess. |
| 291 | // Sometimes this will be ok to just use the host target triple (when we |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 292 | // evaluate say "2+3", but other expressions like breakpoint conditions and |
| 293 | // other things that _are_ target specific really shouldn't just be using |
| 294 | // the host triple. In such a case the language runtime should expose an |
| 295 | // overridden options set (3), below. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 296 | m_compiler->getTargetOpts().Triple = llvm::sys::getDefaultTargetTriple(); |
| 297 | if (log) |
| 298 | log->Printf("Using default target triple of %s", |
| 299 | m_compiler->getTargetOpts().Triple.c_str()); |
| 300 | } |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 301 | // Now add some special fixes for known architectures: Any arm32 iOS |
| 302 | // environment, but not on arm64 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | if (m_compiler->getTargetOpts().Triple.find("arm64") == std::string::npos && |
| 304 | m_compiler->getTargetOpts().Triple.find("arm") != std::string::npos && |
| 305 | m_compiler->getTargetOpts().Triple.find("ios") != std::string::npos) { |
| 306 | m_compiler->getTargetOpts().ABI = "apcs-gnu"; |
| 307 | } |
| 308 | // Supported subsets of x86 |
| 309 | if (target_machine == llvm::Triple::x86 || |
| 310 | target_machine == llvm::Triple::x86_64) { |
| 311 | m_compiler->getTargetOpts().Features.push_back("+sse"); |
| 312 | m_compiler->getTargetOpts().Features.push_back("+sse2"); |
| 313 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 314 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 315 | // Set the target CPU to generate code for. This will be empty for any CPU |
| 316 | // that doesn't really need to make a special |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 317 | // CPU string. |
| 318 | m_compiler->getTargetOpts().CPU = target_arch.GetClangTargetCPU(); |
Ewan Crawford | 7648dd3 | 2016-03-10 12:38:55 +0000 | [diff] [blame] | 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | // Set the target ABI |
| 321 | abi = GetClangTargetABI(target_arch); |
| 322 | if (!abi.empty()) |
| 323 | m_compiler->getTargetOpts().ABI = abi; |
| 324 | |
| 325 | // 3. Now allow the runtime to provide custom configuration options for the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 326 | // target. In this case, a specialized language runtime is available and we |
| 327 | // can query it for extra options. For 99% of use cases, this will not be |
| 328 | // needed and should be provided when basic platform detection is not enough. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 329 | if (lang_rt) |
| 330 | overridden_target_opts = |
| 331 | lang_rt->GetOverrideExprOptions(m_compiler->getTargetOpts()); |
| 332 | |
| 333 | if (overridden_target_opts) |
Pavel Labath | 6302bf6 | 2017-02-13 11:03:17 +0000 | [diff] [blame] | 334 | if (log && log->GetVerbose()) { |
| 335 | LLDB_LOGV( |
| 336 | log, "Using overridden target options for the expression evaluation"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 337 | |
| 338 | auto opts = m_compiler->getTargetOpts(); |
Pavel Labath | 6302bf6 | 2017-02-13 11:03:17 +0000 | [diff] [blame] | 339 | LLDB_LOGV(log, "Triple: '{0}'", opts.Triple); |
| 340 | LLDB_LOGV(log, "CPU: '{0}'", opts.CPU); |
| 341 | LLDB_LOGV(log, "FPMath: '{0}'", opts.FPMath); |
| 342 | LLDB_LOGV(log, "ABI: '{0}'", opts.ABI); |
| 343 | LLDB_LOGV(log, "LinkerVersion: '{0}'", opts.LinkerVersion); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | StringList::LogDump(log, opts.FeaturesAsWritten, "FeaturesAsWritten"); |
| 345 | StringList::LogDump(log, opts.Features, "Features"); |
Sean Callanan | c7b6506 | 2011-11-07 23:35:40 +0000 | [diff] [blame] | 346 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 347 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | // 4. Create and install the target on the compiler. |
| 349 | m_compiler->createDiagnostics(); |
| 350 | auto target_info = TargetInfo::CreateTargetInfo( |
| 351 | m_compiler->getDiagnostics(), m_compiler->getInvocation().TargetOpts); |
| 352 | if (log) { |
| 353 | log->Printf("Using SIMD alignment: %d", target_info->getSimdDefaultAlign()); |
| 354 | log->Printf("Target datalayout string: '%s'", |
| 355 | target_info->getDataLayout().getStringRepresentation().c_str()); |
| 356 | log->Printf("Target ABI: '%s'", target_info->getABI().str().c_str()); |
| 357 | log->Printf("Target vector alignment: %d", |
| 358 | target_info->getMaxVectorAlign()); |
| 359 | } |
| 360 | m_compiler->setTarget(target_info); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 361 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 362 | assert(m_compiler->hasTarget()); |
Tamas Berghammer | dccbfaf | 2015-03-31 10:21:50 +0000 | [diff] [blame] | 363 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | // 5. Set language options. |
| 365 | lldb::LanguageType language = expr.Language(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 366 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 367 | switch (language) { |
| 368 | case lldb::eLanguageTypeC: |
| 369 | case lldb::eLanguageTypeC89: |
| 370 | case lldb::eLanguageTypeC99: |
| 371 | case lldb::eLanguageTypeC11: |
| 372 | // FIXME: the following language option is a temporary workaround, |
| 373 | // to "ask for C, get C++." |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 374 | // For now, the expression parser must use C++ anytime the language is a C |
| 375 | // family language, because the expression parser uses features of C++ to |
| 376 | // capture values. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | m_compiler->getLangOpts().CPlusPlus = true; |
| 378 | break; |
| 379 | case lldb::eLanguageTypeObjC: |
| 380 | m_compiler->getLangOpts().ObjC1 = true; |
| 381 | m_compiler->getLangOpts().ObjC2 = true; |
| 382 | // FIXME: the following language option is a temporary workaround, |
| 383 | // to "ask for ObjC, get ObjC++" (see comment above). |
| 384 | m_compiler->getLangOpts().CPlusPlus = true; |
Davide Italiano | 89419f8 | 2017-12-15 00:50:43 +0000 | [diff] [blame] | 385 | |
| 386 | // Clang now sets as default C++14 as the default standard (with |
| 387 | // GNU extensions), so we do the same here to avoid mismatches that |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 388 | // cause compiler error when evaluating expressions (e.g. nullptr not found |
| 389 | // as it's a C++11 feature). Currently lldb evaluates C++14 as C++11 (see |
| 390 | // two lines below) so we decide to be consistent with that, but this could |
| 391 | // be re-evaluated in the future. |
Davide Italiano | 89419f8 | 2017-12-15 00:50:43 +0000 | [diff] [blame] | 392 | m_compiler->getLangOpts().CPlusPlus11 = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 393 | break; |
| 394 | case lldb::eLanguageTypeC_plus_plus: |
| 395 | case lldb::eLanguageTypeC_plus_plus_11: |
| 396 | case lldb::eLanguageTypeC_plus_plus_14: |
| 397 | m_compiler->getLangOpts().CPlusPlus11 = true; |
| 398 | m_compiler->getHeaderSearchOpts().UseLibcxx = true; |
| 399 | LLVM_FALLTHROUGH; |
| 400 | case lldb::eLanguageTypeC_plus_plus_03: |
| 401 | m_compiler->getLangOpts().CPlusPlus = true; |
| 402 | // FIXME: the following language option is a temporary workaround, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 403 | // to "ask for C++, get ObjC++". Apple hopes to remove this requirement on |
| 404 | // non-Apple platforms, but for now it is needed. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 405 | m_compiler->getLangOpts().ObjC1 = true; |
| 406 | break; |
| 407 | case lldb::eLanguageTypeObjC_plus_plus: |
| 408 | case lldb::eLanguageTypeUnknown: |
| 409 | default: |
| 410 | m_compiler->getLangOpts().ObjC1 = true; |
| 411 | m_compiler->getLangOpts().ObjC2 = true; |
| 412 | m_compiler->getLangOpts().CPlusPlus = true; |
| 413 | m_compiler->getLangOpts().CPlusPlus11 = true; |
| 414 | m_compiler->getHeaderSearchOpts().UseLibcxx = true; |
| 415 | break; |
| 416 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 417 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 418 | m_compiler->getLangOpts().Bool = true; |
| 419 | m_compiler->getLangOpts().WChar = true; |
| 420 | m_compiler->getLangOpts().Blocks = true; |
| 421 | m_compiler->getLangOpts().DebuggerSupport = |
| 422 | true; // Features specifically for debugger clients |
| 423 | if (expr.DesiredResultType() == Expression::eResultTypeId) |
| 424 | m_compiler->getLangOpts().DebuggerCastResultToId = true; |
| 425 | |
| 426 | m_compiler->getLangOpts().CharIsSigned = |
| 427 | ArchSpec(m_compiler->getTargetOpts().Triple.c_str()) |
| 428 | .CharIsSignedByDefault(); |
| 429 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 430 | // Spell checking is a nice feature, but it ends up completing a lot of types |
| 431 | // that we didn't strictly speaking need to complete. As a result, we spend a |
| 432 | // long time parsing and importing debug information. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 433 | m_compiler->getLangOpts().SpellChecking = false; |
| 434 | |
| 435 | if (process_sp && m_compiler->getLangOpts().ObjC1) { |
| 436 | if (process_sp->GetObjCLanguageRuntime()) { |
| 437 | if (process_sp->GetObjCLanguageRuntime()->GetRuntimeVersion() == |
| 438 | ObjCLanguageRuntime::ObjCRuntimeVersions::eAppleObjC_V2) |
| 439 | m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::MacOSX, |
| 440 | VersionTuple(10, 7)); |
| 441 | else |
| 442 | m_compiler->getLangOpts().ObjCRuntime.set(ObjCRuntime::FragileMacOSX, |
| 443 | VersionTuple(10, 7)); |
| 444 | |
| 445 | if (process_sp->GetObjCLanguageRuntime()->HasNewLiteralsAndIndexing()) |
| 446 | m_compiler->getLangOpts().DebuggerObjCLiteral = true; |
Sean Callanan | c3a1600 | 2011-01-17 23:42:46 +0000 | [diff] [blame] | 447 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 448 | } |
Greg Clayton | f83f32d | 2011-01-15 01:32:14 +0000 | [diff] [blame] | 449 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | m_compiler->getLangOpts().ThreadsafeStatics = false; |
| 451 | m_compiler->getLangOpts().AccessControl = |
| 452 | false; // Debuggers get universal access |
| 453 | m_compiler->getLangOpts().DollarIdents = |
| 454 | true; // $ indicates a persistent variable name |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 455 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 456 | // Set CodeGen options |
| 457 | m_compiler->getCodeGenOpts().EmitDeclMetadata = true; |
| 458 | m_compiler->getCodeGenOpts().InstrumentFunctions = false; |
| 459 | m_compiler->getCodeGenOpts().DisableFPElim = true; |
| 460 | m_compiler->getCodeGenOpts().OmitLeafFramePointer = false; |
| 461 | if (generate_debug_info) |
| 462 | m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo); |
| 463 | else |
| 464 | m_compiler->getCodeGenOpts().setDebugInfo(codegenoptions::NoDebugInfo); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 465 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 466 | // Disable some warnings. |
| 467 | m_compiler->getDiagnostics().setSeverityForGroup( |
| 468 | clang::diag::Flavor::WarningOrError, "unused-value", |
| 469 | clang::diag::Severity::Ignored, SourceLocation()); |
| 470 | m_compiler->getDiagnostics().setSeverityForGroup( |
| 471 | clang::diag::Flavor::WarningOrError, "odr", |
| 472 | clang::diag::Severity::Ignored, SourceLocation()); |
Alp Toker | 10933d1 | 2014-06-12 11:14:32 +0000 | [diff] [blame] | 473 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 474 | // Inform the target of the language options |
| 475 | // |
| 476 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 477 | // created. This complexity should be lifted elsewhere. |
| 478 | m_compiler->getTarget().adjust(m_compiler->getLangOpts()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 479 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 480 | // 6. Set up the diagnostic buffer for reporting errors |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 481 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 482 | m_compiler->getDiagnostics().setClient(new ClangDiagnosticManagerAdapter); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 483 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | // 7. Set up the source management objects inside the compiler |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 485 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 486 | clang::FileSystemOptions file_system_options; |
| 487 | m_file_manager.reset(new clang::FileManager(file_system_options)); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | if (!m_compiler->hasSourceManager()) |
| 490 | m_compiler->createSourceManager(*m_file_manager.get()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 491 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 492 | m_compiler->createFileManager(); |
| 493 | m_compiler->createPreprocessor(TU_Complete); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | if (ClangModulesDeclVendor *decl_vendor = |
| 496 | target_sp->GetClangModulesDeclVendor()) { |
| 497 | ClangPersistentVariables *clang_persistent_vars = |
| 498 | llvm::cast<ClangPersistentVariables>( |
| 499 | target_sp->GetPersistentExpressionStateForLanguage( |
| 500 | lldb::eLanguageTypeC)); |
| 501 | std::unique_ptr<PPCallbacks> pp_callbacks( |
| 502 | new LLDBPreprocessorCallbacks(*decl_vendor, *clang_persistent_vars)); |
| 503 | m_pp_callbacks = |
| 504 | static_cast<LLDBPreprocessorCallbacks *>(pp_callbacks.get()); |
| 505 | m_compiler->getPreprocessor().addPPCallbacks(std::move(pp_callbacks)); |
| 506 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 507 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 508 | // 8. Most of this we get from the CompilerInstance, but we also want to give |
| 509 | // the context an ExternalASTSource. |
Raphael Isemann | 778b308 | 2018-08-23 20:40:45 +0000 | [diff] [blame] | 510 | // We enable all builtin functions beside the builtins from libc/libm (e.g. |
| 511 | // 'fopen'). Those libc functions are already correctly handled by LLDB, and |
| 512 | // additionally enabling them as expandable builtins is breaking Clang. |
| 513 | m_compiler->getLangOpts().NoBuiltin = true; |
| 514 | |
| 515 | auto &PP = m_compiler->getPreprocessor(); |
| 516 | auto &builtin_context = PP.getBuiltinInfo(); |
| 517 | builtin_context.initializeBuiltins(PP.getIdentifierTable(), |
| 518 | m_compiler->getLangOpts()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 519 | |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame^] | 520 | m_compiler->createASTContext(); |
| 521 | clang::ASTContext &ast_context = m_compiler->getASTContext(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 522 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 523 | ClangExpressionHelper *type_system_helper = |
| 524 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 525 | ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap(); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 526 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 527 | if (decl_map) { |
| 528 | llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> ast_source( |
| 529 | decl_map->CreateProxy()); |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame^] | 530 | decl_map->InstallASTContext(ast_context, m_compiler->getFileManager()); |
| 531 | ast_context.setExternalSource(ast_source); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | m_ast_context.reset( |
| 535 | new ClangASTContext(m_compiler->getTargetOpts().Triple.c_str())); |
Raphael Isemann | 596709d | 2018-08-27 15:18:33 +0000 | [diff] [blame^] | 536 | m_ast_context->setASTContext(&ast_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 537 | |
| 538 | std::string module_name("$__lldb_module"); |
| 539 | |
| 540 | m_llvm_context.reset(new LLVMContext()); |
| 541 | m_code_generator.reset(CreateLLVMCodeGen( |
| 542 | m_compiler->getDiagnostics(), module_name, |
| 543 | m_compiler->getHeaderSearchOpts(), m_compiler->getPreprocessorOpts(), |
| 544 | m_compiler->getCodeGenOpts(), *m_llvm_context)); |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 547 | ClangExpressionParser::~ClangExpressionParser() {} |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | unsigned ClangExpressionParser::Parse(DiagnosticManager &diagnostic_manager) { |
| 550 | ClangDiagnosticManagerAdapter *adapter = |
| 551 | static_cast<ClangDiagnosticManagerAdapter *>( |
| 552 | m_compiler->getDiagnostics().getClient()); |
| 553 | clang::TextDiagnosticBuffer *diag_buf = adapter->GetPassthrough(); |
| 554 | diag_buf->FlushDiagnostics(m_compiler->getDiagnostics()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | adapter->ResetManager(&diagnostic_manager); |
Sean Callanan | 579e70c | 2016-03-19 00:03:59 +0000 | [diff] [blame] | 557 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 558 | const char *expr_text = m_expr.Text(); |
Rafael Espindola | 9cb97b6 | 2014-05-21 15:08:27 +0000 | [diff] [blame] | 559 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | clang::SourceManager &source_mgr = m_compiler->getSourceManager(); |
| 561 | bool created_main_file = false; |
| 562 | if (m_compiler->getCodeGenOpts().getDebugInfo() == |
| 563 | codegenoptions::FullDebugInfo) { |
| 564 | int temp_fd = -1; |
| 565 | llvm::SmallString<PATH_MAX> result_path; |
Pavel Labath | 60f028f | 2018-06-19 15:09:07 +0000 | [diff] [blame] | 566 | if (FileSpec tmpdir_file_spec = HostInfo::GetProcessTempDir()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 567 | tmpdir_file_spec.AppendPathComponent("lldb-%%%%%%.expr"); |
| 568 | std::string temp_source_path = tmpdir_file_spec.GetPath(); |
| 569 | llvm::sys::fs::createUniqueFile(temp_source_path, temp_fd, result_path); |
| 570 | } else { |
| 571 | llvm::sys::fs::createTemporaryFile("lldb", "expr", temp_fd, result_path); |
| 572 | } |
| 573 | |
| 574 | if (temp_fd != -1) { |
| 575 | lldb_private::File file(temp_fd, true); |
| 576 | const size_t expr_text_len = strlen(expr_text); |
| 577 | size_t bytes_written = expr_text_len; |
| 578 | if (file.Write(expr_text, bytes_written).Success()) { |
| 579 | if (bytes_written == expr_text_len) { |
| 580 | file.Close(); |
| 581 | source_mgr.setMainFileID( |
| 582 | source_mgr.createFileID(m_file_manager->getFile(result_path), |
| 583 | SourceLocation(), SrcMgr::C_User)); |
| 584 | created_main_file = true; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 585 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 586 | } |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 587 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 589 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 590 | if (!created_main_file) { |
| 591 | std::unique_ptr<MemoryBuffer> memory_buffer = |
| 592 | MemoryBuffer::getMemBufferCopy(expr_text, __FUNCTION__); |
| 593 | source_mgr.setMainFileID(source_mgr.createFileID(std::move(memory_buffer))); |
| 594 | } |
| 595 | |
| 596 | diag_buf->BeginSourceFile(m_compiler->getLangOpts(), |
| 597 | &m_compiler->getPreprocessor()); |
| 598 | |
| 599 | ClangExpressionHelper *type_system_helper = |
| 600 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 601 | |
| 602 | ASTConsumer *ast_transformer = |
| 603 | type_system_helper->ASTTransformer(m_code_generator.get()); |
| 604 | |
| 605 | if (ClangExpressionDeclMap *decl_map = type_system_helper->DeclMap()) |
| 606 | decl_map->InstallCodeGenerator(m_code_generator.get()); |
| 607 | |
| 608 | if (ast_transformer) { |
| 609 | ast_transformer->Initialize(m_compiler->getASTContext()); |
| 610 | ParseAST(m_compiler->getPreprocessor(), ast_transformer, |
| 611 | m_compiler->getASTContext()); |
| 612 | } else { |
| 613 | m_code_generator->Initialize(m_compiler->getASTContext()); |
| 614 | ParseAST(m_compiler->getPreprocessor(), m_code_generator.get(), |
| 615 | m_compiler->getASTContext()); |
| 616 | } |
| 617 | |
| 618 | diag_buf->EndSourceFile(); |
| 619 | |
| 620 | unsigned num_errors = diag_buf->getNumErrors(); |
| 621 | |
| 622 | if (m_pp_callbacks && m_pp_callbacks->hasErrors()) { |
| 623 | num_errors++; |
Zachary Turner | e2411fa | 2016-11-12 19:12:56 +0000 | [diff] [blame] | 624 | diagnostic_manager.PutString(eDiagnosticSeverityError, |
| 625 | "while importing modules:"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | diagnostic_manager.AppendMessageToDiagnostic( |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 627 | m_pp_callbacks->getErrorString()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | if (!num_errors) { |
| 631 | if (type_system_helper->DeclMap() && |
| 632 | !type_system_helper->DeclMap()->ResolveUnknownTypes()) { |
| 633 | diagnostic_manager.Printf(eDiagnosticSeverityError, |
| 634 | "Couldn't infer the type of a variable"); |
| 635 | num_errors++; |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 636 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 637 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 638 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 639 | if (!num_errors) { |
| 640 | type_system_helper->CommitPersistentDecls(); |
| 641 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 642 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 643 | adapter->ResetManager(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 644 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 645 | return num_errors; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 648 | std::string |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 649 | ClangExpressionParser::GetClangTargetABI(const ArchSpec &target_arch) { |
| 650 | std::string abi; |
| 651 | |
| 652 | if (target_arch.IsMIPS()) { |
| 653 | switch (target_arch.GetFlags() & ArchSpec::eMIPSABI_mask) { |
| 654 | case ArchSpec::eMIPSABI_N64: |
| 655 | abi = "n64"; |
| 656 | break; |
| 657 | case ArchSpec::eMIPSABI_N32: |
| 658 | abi = "n32"; |
| 659 | break; |
| 660 | case ArchSpec::eMIPSABI_O32: |
| 661 | abi = "o32"; |
| 662 | break; |
| 663 | default: |
| 664 | break; |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 665 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 666 | } |
| 667 | return abi; |
Sagar Thakur | adc1abe | 2016-05-13 11:04:47 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 670 | bool ClangExpressionParser::RewriteExpression( |
| 671 | DiagnosticManager &diagnostic_manager) { |
| 672 | clang::SourceManager &source_manager = m_compiler->getSourceManager(); |
| 673 | clang::edit::EditedSource editor(source_manager, m_compiler->getLangOpts(), |
| 674 | nullptr); |
| 675 | clang::edit::Commit commit(editor); |
| 676 | clang::Rewriter rewriter(source_manager, m_compiler->getLangOpts()); |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 677 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 678 | class RewritesReceiver : public edit::EditsReceiver { |
| 679 | Rewriter &rewrite; |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | public: |
| 682 | RewritesReceiver(Rewriter &in_rewrite) : rewrite(in_rewrite) {} |
| 683 | |
| 684 | void insert(SourceLocation loc, StringRef text) override { |
| 685 | rewrite.InsertText(loc, text); |
Jim Ingham | a1e541b | 2016-03-25 01:57:14 +0000 | [diff] [blame] | 686 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | void replace(CharSourceRange range, StringRef text) override { |
| 688 | rewrite.ReplaceText(range.getBegin(), rewrite.getRangeSize(range), text); |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 689 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 690 | }; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 691 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | RewritesReceiver rewrites_receiver(rewriter); |
| 693 | |
| 694 | const DiagnosticList &diagnostics = diagnostic_manager.Diagnostics(); |
| 695 | size_t num_diags = diagnostics.size(); |
| 696 | if (num_diags == 0) |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 697 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 698 | |
| 699 | for (const Diagnostic *diag : diagnostic_manager.Diagnostics()) { |
| 700 | const ClangDiagnostic *diagnostic = llvm::dyn_cast<ClangDiagnostic>(diag); |
| 701 | if (diagnostic && diagnostic->HasFixIts()) { |
| 702 | for (const FixItHint &fixit : diagnostic->FixIts()) { |
| 703 | // This is cobbed from clang::Rewrite::FixItRewriter. |
| 704 | if (fixit.CodeToInsert.empty()) { |
| 705 | if (fixit.InsertFromRange.isValid()) { |
| 706 | commit.insertFromRange(fixit.RemoveRange.getBegin(), |
| 707 | fixit.InsertFromRange, /*afterToken=*/false, |
| 708 | fixit.BeforePreviousInsertions); |
| 709 | } else |
| 710 | commit.remove(fixit.RemoveRange); |
| 711 | } else { |
| 712 | if (fixit.RemoveRange.isTokenRange() || |
| 713 | fixit.RemoveRange.getBegin() != fixit.RemoveRange.getEnd()) |
| 714 | commit.replace(fixit.RemoveRange, fixit.CodeToInsert); |
| 715 | else |
| 716 | commit.insert(fixit.RemoveRange.getBegin(), fixit.CodeToInsert, |
| 717 | /*afterToken=*/false, fixit.BeforePreviousInsertions); |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | // FIXME - do we want to try to propagate specific errors here? |
| 724 | if (!commit.isCommitable()) |
| 725 | return false; |
| 726 | else if (!editor.commit(commit)) |
| 727 | return false; |
| 728 | |
| 729 | // Now play all the edits, and stash the result in the diagnostic manager. |
| 730 | editor.applyRewrites(rewrites_receiver); |
| 731 | RewriteBuffer &main_file_buffer = |
| 732 | rewriter.getEditBuffer(source_manager.getMainFileID()); |
| 733 | |
| 734 | std::string fixed_expression; |
| 735 | llvm::raw_string_ostream out_stream(fixed_expression); |
| 736 | |
| 737 | main_file_buffer.write(out_stream); |
| 738 | out_stream.flush(); |
| 739 | diagnostic_manager.SetFixedExpression(fixed_expression); |
| 740 | |
| 741 | return true; |
Sean Callanan | fc55f5d | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | static bool FindFunctionInModule(ConstString &mangled_name, |
| 745 | llvm::Module *module, const char *orig_name) { |
| 746 | for (const auto &func : module->getFunctionList()) { |
| 747 | const StringRef &name = func.getName(); |
| 748 | if (name.find(orig_name) != StringRef::npos) { |
| 749 | mangled_name.SetString(name); |
| 750 | return true; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 751 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 752 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 753 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 754 | return false; |
Sean Callanan | 1a8d409 | 2010-08-27 01:01:44 +0000 | [diff] [blame] | 755 | } |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 756 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 757 | lldb_private::Status ClangExpressionParser::PrepareForExecution( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | lldb::addr_t &func_addr, lldb::addr_t &func_end, |
| 759 | lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx, |
| 760 | bool &can_interpret, ExecutionPolicy execution_policy) { |
| 761 | func_addr = LLDB_INVALID_ADDRESS; |
| 762 | func_end = LLDB_INVALID_ADDRESS; |
| 763 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); |
| 764 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 765 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 766 | |
| 767 | std::unique_ptr<llvm::Module> llvm_module_ap( |
| 768 | m_code_generator->ReleaseModule()); |
| 769 | |
| 770 | if (!llvm_module_ap.get()) { |
| 771 | err.SetErrorToGenericError(); |
| 772 | err.SetErrorString("IR doesn't contain a module"); |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 773 | return err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | ConstString function_name; |
| 777 | |
| 778 | if (execution_policy != eExecutionPolicyTopLevel) { |
| 779 | // Find the actual name of the function (it's often mangled somehow) |
| 780 | |
| 781 | if (!FindFunctionInModule(function_name, llvm_module_ap.get(), |
| 782 | m_expr.FunctionName())) { |
| 783 | err.SetErrorToGenericError(); |
| 784 | err.SetErrorStringWithFormat("Couldn't find %s() in the module", |
| 785 | m_expr.FunctionName()); |
| 786 | return err; |
| 787 | } else { |
| 788 | if (log) |
| 789 | log->Printf("Found function %s for %s", function_name.AsCString(), |
| 790 | m_expr.FunctionName()); |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | SymbolContext sc; |
| 795 | |
| 796 | if (lldb::StackFrameSP frame_sp = exe_ctx.GetFrameSP()) { |
| 797 | sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything); |
| 798 | } else if (lldb::TargetSP target_sp = exe_ctx.GetTargetSP()) { |
| 799 | sc.target_sp = target_sp; |
| 800 | } |
| 801 | |
| 802 | LLVMUserExpression::IRPasses custom_passes; |
| 803 | { |
| 804 | auto lang = m_expr.Language(); |
| 805 | if (log) |
Bruce Mitchener | 4ebdee0 | 2018-05-29 09:10:46 +0000 | [diff] [blame] | 806 | log->Printf("%s - Current expression language is %s\n", __FUNCTION__, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 807 | Language::GetNameForLanguageType(lang)); |
Jim Ingham | 2256215 | 2016-11-03 23:42:09 +0000 | [diff] [blame] | 808 | lldb::ProcessSP process_sp = exe_ctx.GetProcessSP(); |
| 809 | if (process_sp && lang != lldb::eLanguageTypeUnknown) { |
| 810 | auto runtime = process_sp->GetLanguageRuntime(lang); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 811 | if (runtime) |
| 812 | runtime->GetIRPasses(custom_passes); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | if (custom_passes.EarlyPasses) { |
| 817 | if (log) |
| 818 | log->Printf("%s - Running Early IR Passes from LanguageRuntime on " |
| 819 | "expression module '%s'", |
| 820 | __FUNCTION__, m_expr.FunctionName()); |
| 821 | |
| 822 | custom_passes.EarlyPasses->run(*llvm_module_ap); |
| 823 | } |
| 824 | |
| 825 | execution_unit_sp.reset( |
| 826 | new IRExecutionUnit(m_llvm_context, // handed off here |
| 827 | llvm_module_ap, // handed off here |
| 828 | function_name, exe_ctx.GetTargetSP(), sc, |
| 829 | m_compiler->getTargetOpts().Features)); |
| 830 | |
| 831 | ClangExpressionHelper *type_system_helper = |
| 832 | dyn_cast<ClangExpressionHelper>(m_expr.GetTypeSystemHelper()); |
| 833 | ClangExpressionDeclMap *decl_map = |
| 834 | type_system_helper->DeclMap(); // result can be NULL |
| 835 | |
| 836 | if (decl_map) { |
| 837 | Stream *error_stream = NULL; |
| 838 | Target *target = exe_ctx.GetTargetPtr(); |
| 839 | error_stream = target->GetDebugger().GetErrorFile().get(); |
| 840 | |
| 841 | IRForTarget ir_for_target(decl_map, m_expr.NeedsVariableResolution(), |
| 842 | *execution_unit_sp, *error_stream, |
| 843 | function_name.AsCString()); |
| 844 | |
| 845 | bool ir_can_run = |
| 846 | ir_for_target.runOnModule(*execution_unit_sp->GetModule()); |
| 847 | |
| 848 | if (!ir_can_run) { |
| 849 | err.SetErrorString( |
| 850 | "The expression could not be prepared to run in the target"); |
| 851 | return err; |
| 852 | } |
| 853 | |
| 854 | Process *process = exe_ctx.GetProcessPtr(); |
| 855 | |
| 856 | if (execution_policy != eExecutionPolicyAlways && |
| 857 | execution_policy != eExecutionPolicyTopLevel) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 858 | lldb_private::Status interpret_error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 859 | |
| 860 | bool interpret_function_calls = |
| 861 | !process ? false : process->CanInterpretFunctionCalls(); |
| 862 | can_interpret = IRInterpreter::CanInterpret( |
| 863 | *execution_unit_sp->GetModule(), *execution_unit_sp->GetFunction(), |
| 864 | interpret_error, interpret_function_calls); |
| 865 | |
| 866 | if (!can_interpret && execution_policy == eExecutionPolicyNever) { |
| 867 | err.SetErrorStringWithFormat("Can't run the expression locally: %s", |
| 868 | interpret_error.AsCString()); |
| 869 | return err; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | if (!process && execution_policy == eExecutionPolicyAlways) { |
| 874 | err.SetErrorString("Expression needed to run in the target, but the " |
| 875 | "target can't be run"); |
| 876 | return err; |
| 877 | } |
| 878 | |
| 879 | if (!process && execution_policy == eExecutionPolicyTopLevel) { |
| 880 | err.SetErrorString("Top-level code needs to be inserted into a runnable " |
| 881 | "target, but the target can't be run"); |
| 882 | return err; |
| 883 | } |
| 884 | |
| 885 | if (execution_policy == eExecutionPolicyAlways || |
| 886 | (execution_policy != eExecutionPolicyTopLevel && !can_interpret)) { |
| 887 | if (m_expr.NeedsValidation() && process) { |
| 888 | if (!process->GetDynamicCheckers()) { |
| 889 | DynamicCheckerFunctions *dynamic_checkers = |
| 890 | new DynamicCheckerFunctions(); |
| 891 | |
| 892 | DiagnosticManager install_diagnostics; |
| 893 | |
| 894 | if (!dynamic_checkers->Install(install_diagnostics, exe_ctx)) { |
| 895 | if (install_diagnostics.Diagnostics().size()) |
| 896 | err.SetErrorString("couldn't install checkers, unknown error"); |
| 897 | else |
| 898 | err.SetErrorString(install_diagnostics.GetString().c_str()); |
| 899 | |
| 900 | return err; |
| 901 | } |
| 902 | |
| 903 | process->SetDynamicCheckers(dynamic_checkers); |
| 904 | |
| 905 | if (log) |
| 906 | log->Printf("== [ClangUserExpression::Evaluate] Finished " |
| 907 | "installing dynamic checkers =="); |
| 908 | } |
| 909 | |
| 910 | IRDynamicChecks ir_dynamic_checks(*process->GetDynamicCheckers(), |
| 911 | function_name.AsCString()); |
| 912 | |
| 913 | llvm::Module *module = execution_unit_sp->GetModule(); |
| 914 | if (!module || !ir_dynamic_checks.runOnModule(*module)) { |
| 915 | err.SetErrorToGenericError(); |
| 916 | err.SetErrorString("Couldn't add dynamic checks to the expression"); |
| 917 | return err; |
| 918 | } |
| 919 | |
| 920 | if (custom_passes.LatePasses) { |
| 921 | if (log) |
| 922 | log->Printf("%s - Running Late IR Passes from LanguageRuntime on " |
| 923 | "expression module '%s'", |
| 924 | __FUNCTION__, m_expr.FunctionName()); |
| 925 | |
| 926 | custom_passes.LatePasses->run(*module); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | if (execution_policy == eExecutionPolicyAlways || |
| 932 | execution_policy == eExecutionPolicyTopLevel || !can_interpret) { |
| 933 | execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); |
| 934 | } |
| 935 | } else { |
| 936 | execution_unit_sp->GetRunnableInfo(err, func_addr, func_end); |
| 937 | } |
| 938 | |
| 939 | return err; |
| 940 | } |
| 941 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 942 | lldb_private::Status ClangExpressionParser::RunStaticInitializers( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 943 | lldb::IRExecutionUnitSP &execution_unit_sp, ExecutionContext &exe_ctx) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 944 | lldb_private::Status err; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 945 | |
| 946 | lldbassert(execution_unit_sp.get()); |
| 947 | lldbassert(exe_ctx.HasThreadScope()); |
| 948 | |
| 949 | if (!execution_unit_sp.get()) { |
| 950 | err.SetErrorString( |
| 951 | "can't run static initializers for a NULL execution unit"); |
| 952 | return err; |
| 953 | } |
| 954 | |
| 955 | if (!exe_ctx.HasThreadScope()) { |
| 956 | err.SetErrorString("can't run static initializers without a thread"); |
| 957 | return err; |
| 958 | } |
| 959 | |
| 960 | std::vector<lldb::addr_t> static_initializers; |
| 961 | |
| 962 | execution_unit_sp->GetStaticInitializers(static_initializers); |
| 963 | |
| 964 | for (lldb::addr_t static_initializer : static_initializers) { |
| 965 | EvaluateExpressionOptions options; |
| 966 | |
| 967 | lldb::ThreadPlanSP call_static_initializer(new ThreadPlanCallFunction( |
| 968 | exe_ctx.GetThreadRef(), Address(static_initializer), CompilerType(), |
| 969 | llvm::ArrayRef<lldb::addr_t>(), options)); |
| 970 | |
| 971 | DiagnosticManager execution_errors; |
| 972 | lldb::ExpressionResults results = |
| 973 | exe_ctx.GetThreadRef().GetProcess()->RunThreadPlan( |
| 974 | exe_ctx, call_static_initializer, options, execution_errors); |
| 975 | |
| 976 | if (results != lldb::eExpressionCompleted) { |
| 977 | err.SetErrorStringWithFormat("couldn't run static initializer: %s", |
| 978 | execution_errors.GetString().c_str()); |
| 979 | return err; |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | return err; |
Sean Callanan | bd4dc69 | 2016-03-21 22:23:38 +0000 | [diff] [blame] | 984 | } |